blob: 990fc228b38e4dd27bca767d9b930ce106f791e1 [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(),
528 Loc, Depth, Position, ParamName, Typename,
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000529 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(),
653 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000654 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000655 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000656 Param->setAccess(AS_public);
657
Douglas Gregor72c3f312008-12-05 18:15:24 +0000658 if (Invalid)
659 Param->setInvalidDecl();
660
661 if (D.getIdentifier()) {
662 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000663 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000664 IdResolver.AddDecl(Param);
665 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000666
Douglas Gregor61c4d282011-01-05 15:48:55 +0000667 // C++0x [temp.param]p9:
668 // A default template-argument may be specified for any kind of
669 // template-parameter that is not a template parameter pack.
670 if (Default && IsParameterPack) {
671 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
672 Default = 0;
673 }
674
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000675 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000676 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000677 // Check for unexpanded parameter packs.
678 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
679 return Param;
680
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000681 TemplateArgument Converted;
682 if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) {
683 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000684 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000685 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000686
John McCall9ae2f072010-08-23 23:25:46 +0000687 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000688 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000689
John McCalld226f652010-08-21 09:40:31 +0000690 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000691}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000692
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000693/// ActOnTemplateTemplateParameter - Called when a C++ template template
694/// parameter (e.g. T in template <template <typename> class T> class array)
695/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000696Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
697 SourceLocation TmpLoc,
698 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000699 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000700 IdentifierInfo *Name,
701 SourceLocation NameLoc,
702 unsigned Depth,
703 unsigned Position,
704 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000705 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000706 assert(S->isTemplateParamScope() &&
707 "Template template parameter not in template parameter scope!");
708
709 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000710 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000711 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000712 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000713 NameLoc.isInvalid()? TmpLoc : NameLoc,
714 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000715 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000716 Param->setAccess(AS_public);
717
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000718 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000719 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000720 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000721 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000722 IdResolver.AddDecl(Param);
723 }
724
Douglas Gregor6f526752010-12-16 08:48:57 +0000725 if (Params->size() == 0) {
726 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
727 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
728 Param->setInvalidDecl();
729 }
730
Douglas Gregor61c4d282011-01-05 15:48:55 +0000731 // C++0x [temp.param]p9:
732 // A default template-argument may be specified for any kind of
733 // template-parameter that is not a template parameter pack.
734 if (IsParameterPack && !Default.isInvalid()) {
735 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
736 Default = ParsedTemplateArgument();
737 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000738
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000739 if (!Default.isInvalid()) {
740 // Check only that we have a template template argument. We don't want to
741 // try to check well-formedness now, because our template template parameter
742 // might have dependent types in its template parameters, which we wouldn't
743 // be able to match now.
744 //
745 // If none of the template template parameter's template arguments mention
746 // other template parameters, we could actually perform more checking here.
747 // However, it isn't worth doing.
748 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
749 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
750 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
751 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000752 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000753 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000754
Douglas Gregor6f526752010-12-16 08:48:57 +0000755 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000756 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000757 DefaultArg.getArgument().getAsTemplate(),
758 UPPC_DefaultArgument))
759 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000760
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000761 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000762 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000763
John McCalld226f652010-08-21 09:40:31 +0000764 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000765}
766
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000767/// ActOnTemplateParameterList - Builds a TemplateParameterList that
768/// contains the template parameters in Params/NumParams.
769Sema::TemplateParamsTy *
770Sema::ActOnTemplateParameterList(unsigned Depth,
771 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000772 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000773 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000774 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000775 SourceLocation RAngleLoc) {
776 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000777 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000778
Douglas Gregorddc29e12009-02-06 22:42:48 +0000779 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000780 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000781 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000782}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000783
John McCallb6217662010-03-15 10:12:16 +0000784static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
785 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000786 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000787}
788
John McCallf312b1e2010-08-26 23:41:50 +0000789DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000790Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000791 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000792 IdentifierInfo *Name, SourceLocation NameLoc,
793 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000794 TemplateParameterList *TemplateParams,
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000795 AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000796 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000797 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000798 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000799 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000800
801 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000802 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000803 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000804
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000805 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
806 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000807
808 // There is no such thing as an unnamed class template.
809 if (!Name) {
810 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000811 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000812 }
813
814 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000815 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000816 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000817 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000818 if (SS.isNotEmpty() && !SS.isInvalid()) {
819 SemanticContext = computeDeclContext(SS, true);
820 if (!SemanticContext) {
821 // FIXME: Produce a reasonable diagnostic here
822 return true;
823 }
Mike Stump1eb44332009-09-09 15:08:12 +0000824
John McCall77bb1aa2010-05-01 00:40:08 +0000825 if (RequireCompleteDeclContext(SS, SemanticContext))
826 return true;
827
John McCalla24dc2e2009-11-17 02:14:36 +0000828 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000829 } else {
830 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000831 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000832 }
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Douglas Gregor57265e32010-04-12 16:00:01 +0000834 if (Previous.isAmbiguous())
835 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000836
Douglas Gregorddc29e12009-02-06 22:42:48 +0000837 NamedDecl *PrevDecl = 0;
838 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000839 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000840
Douglas Gregorddc29e12009-02-06 22:42:48 +0000841 // If there is a previous declaration with the same name, check
842 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000843 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000844 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000845
846 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000847 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000848 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000849 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000850 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
851 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000852 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000853 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
854 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
855 PrevClassTemplate
856 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
857 ->getSpecializedTemplate();
858 }
859 }
860
John McCall65c49462009-12-18 11:25:59 +0000861 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000862 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000863 // [...] When looking for a prior declaration of a class or a function
864 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000865 // function is neither a qualified name nor a template-id, scopes outside
866 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000867 if (!SS.isSet()) {
868 DeclContext *OutermostContext = CurContext;
869 while (!OutermostContext->isFileContext())
870 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000871
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000872 if (PrevDecl &&
873 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
874 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
875 SemanticContext = PrevDecl->getDeclContext();
876 } else {
877 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000878 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000879 // declaration.
880 PrevDecl = PrevClassTemplate = 0;
881 SemanticContext = OutermostContext;
882 }
John McCalle129d442009-12-17 23:21:11 +0000883 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000884
John McCalle129d442009-12-17 23:21:11 +0000885 if (CurContext->isDependentContext()) {
886 // If this is a dependent context, we don't want to link the friend
887 // class template to the template in scope, because that would perform
888 // checking of the template parameter lists that can't be performed
889 // until the outer context is instantiated.
890 PrevDecl = PrevClassTemplate = 0;
891 }
892 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
893 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000894
Douglas Gregorddc29e12009-02-06 22:42:48 +0000895 if (PrevClassTemplate) {
896 // Ensure that the template parameter lists are compatible.
897 if (!TemplateParameterListsAreEqual(TemplateParams,
898 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000899 /*Complain=*/true,
900 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000901 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000902
903 // C++ [temp.class]p4:
904 // In a redeclaration, partial specialization, explicit
905 // specialization or explicit instantiation of a class template,
906 // the class-key shall agree in kind with the original class
907 // template declaration (7.1.5.3).
908 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000909 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000910 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000911 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000912 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000913 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000914 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000915 }
916
Douglas Gregorddc29e12009-02-06 22:42:48 +0000917 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000918 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000919 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000920 Diag(NameLoc, diag::err_redefinition) << Name;
921 Diag(Def->getLocation(), diag::note_previous_definition);
922 // FIXME: Would it make sense to try to "forget" the previous
923 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000924 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000925 }
926 }
927 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
928 // Maybe we will complain about the shadowed template parameter.
929 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
930 // Just pretend that we didn't see the previous declaration.
931 PrevDecl = 0;
932 } else if (PrevDecl) {
933 // C++ [temp]p5:
934 // A class template shall not have the same name as any other
935 // template, class, function, object, enumeration, enumerator,
936 // namespace, or type in the same scope (3.3), except as specified
937 // in (14.5.4).
938 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
939 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000940 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000941 }
942
Douglas Gregord684b002009-02-10 19:49:53 +0000943 // Check the template parameter list of this declaration, possibly
944 // merging in the template parameter list from the previous class
945 // template declaration.
946 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000947 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000948 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000949 SemanticContext->isRecord() &&
950 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000951 ? TPC_ClassTemplateMember
952 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000953 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Douglas Gregor57265e32010-04-12 16:00:01 +0000955 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000956 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000957 // template out-of-line.
958 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
959 !(TUK == TUK_Friend && CurContext->isDependentContext()))
960 Diag(NameLoc, diag::err_member_def_does_not_match)
961 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000962 }
963
Mike Stump1eb44332009-09-09 15:08:12 +0000964 CXXRecordDecl *NewClass =
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000965 CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000966 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000967 PrevClassTemplate->getTemplatedDecl() : 0,
968 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000969 SetNestedNameSpecifier(NewClass, SS);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000970
971 ClassTemplateDecl *NewTemplate
972 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
973 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000974 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000975 NewClass->setDescribedClassTemplate(NewTemplate);
976
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000977 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000978 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000979 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000980 assert(T->isDependentType() && "Class template type is not dependent?");
981 (void)T;
982
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000983 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000984 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000985 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000986 PrevClassTemplate->getInstantiatedFromMemberTemplate())
987 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000988
Anders Carlsson4cbe82c2009-03-26 01:24:28 +0000989 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +0000990 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +0000991 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Douglas Gregorddc29e12009-02-06 22:42:48 +0000993 // Set the lexical context of these templates
994 NewClass->setLexicalDeclContext(CurContext);
995 NewTemplate->setLexicalDeclContext(CurContext);
996
John McCall0f434ec2009-07-31 02:45:11 +0000997 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +0000998 NewClass->startDefinition();
999
1000 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001001 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001002
John McCall05b23ea2009-09-14 21:59:20 +00001003 if (TUK != TUK_Friend)
1004 PushOnScopeChains(NewTemplate, S);
1005 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001006 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001007 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001008 NewClass->setAccess(PrevClassTemplate->getAccess());
1009 }
John McCall05b23ea2009-09-14 21:59:20 +00001010
Douglas Gregord85bea22009-09-26 06:47:28 +00001011 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1012 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001013
John McCall05b23ea2009-09-14 21:59:20 +00001014 // Friend templates are visible in fairly strange ways.
1015 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001016 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001017 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1018 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1019 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001020 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001021 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001022
Douglas Gregord85bea22009-09-26 06:47:28 +00001023 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1024 NewClass->getLocation(),
1025 NewTemplate,
1026 /*FIXME:*/NewClass->getLocation());
1027 Friend->setAccess(AS_public);
1028 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001029 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001030
Douglas Gregord684b002009-02-10 19:49:53 +00001031 if (Invalid) {
1032 NewTemplate->setInvalidDecl();
1033 NewClass->setInvalidDecl();
1034 }
John McCalld226f652010-08-21 09:40:31 +00001035 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001036}
1037
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001038/// \brief Diagnose the presence of a default template argument on a
1039/// template parameter, which is ill-formed in certain contexts.
1040///
1041/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001042static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001043 Sema::TemplateParamListContext TPC,
1044 SourceLocation ParamLoc,
1045 SourceRange DefArgRange) {
1046 switch (TPC) {
1047 case Sema::TPC_ClassTemplate:
1048 return false;
1049
1050 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001051 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001052 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001053 // A default template-argument shall not be specified in a
1054 // function template declaration or a function template
1055 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001056 // If a friend function template declaration specifies a default
1057 // template-argument, that declaration shall be a definition and shall be
1058 // the only declaration of the function template in the translation unit.
1059 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001060 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001061 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001062 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001063 << DefArgRange;
1064 return false;
1065
1066 case Sema::TPC_ClassTemplateMember:
1067 // C++0x [temp.param]p9:
1068 // A default template-argument shall not be specified in the
1069 // template-parameter-lists of the definition of a member of a
1070 // class template that appears outside of the member's class.
1071 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1072 << DefArgRange;
1073 return true;
1074
1075 case Sema::TPC_FriendFunctionTemplate:
1076 // C++ [temp.param]p9:
1077 // A default template-argument shall not be specified in a
1078 // friend template declaration.
1079 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1080 << DefArgRange;
1081 return true;
1082
1083 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1084 // for friend function templates if there is only a single
1085 // declaration (and it is a definition). Strange!
1086 }
1087
1088 return false;
1089}
1090
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001091/// \brief Check for unexpanded parameter packs within the template parameters
1092/// of a template template parameter, recursively.
1093bool DiagnoseUnexpandedParameterPacks(Sema &S, TemplateTemplateParmDecl *TTP){
1094 TemplateParameterList *Params = TTP->getTemplateParameters();
1095 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1096 NamedDecl *P = Params->getParam(I);
1097 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001098 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001099 NTTP->getTypeSourceInfo(),
1100 Sema::UPPC_NonTypeTemplateParameterType))
1101 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001102
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001103 continue;
1104 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001105
1106 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001107 = dyn_cast<TemplateTemplateParmDecl>(P))
1108 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1109 return true;
1110 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001111
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001112 return false;
1113}
1114
Douglas Gregord684b002009-02-10 19:49:53 +00001115/// \brief Checks the validity of a template parameter list, possibly
1116/// considering the template parameter list from a previous
1117/// declaration.
1118///
1119/// If an "old" template parameter list is provided, it must be
1120/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1121/// template parameter list.
1122///
1123/// \param NewParams Template parameter list for a new template
1124/// declaration. This template parameter list will be updated with any
1125/// default arguments that are carried through from the previous
1126/// template parameter list.
1127///
1128/// \param OldParams If provided, template parameter list from a
1129/// previous declaration of the same template. Default template
1130/// arguments will be merged from the old template parameter list to
1131/// the new template parameter list.
1132///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001133/// \param TPC Describes the context in which we are checking the given
1134/// template parameter list.
1135///
Douglas Gregord684b002009-02-10 19:49:53 +00001136/// \returns true if an error occurred, false otherwise.
1137bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001138 TemplateParameterList *OldParams,
1139 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001140 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Douglas Gregord684b002009-02-10 19:49:53 +00001142 // C++ [temp.param]p10:
1143 // The set of default template-arguments available for use with a
1144 // template declaration or definition is obtained by merging the
1145 // default arguments from the definition (if in scope) and all
1146 // declarations in scope in the same way default function
1147 // arguments are (8.3.6).
1148 bool SawDefaultArgument = false;
1149 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001150
Anders Carlsson49d25572009-06-12 23:20:15 +00001151 bool SawParameterPack = false;
1152 SourceLocation ParameterPackLoc;
1153
Mike Stump1a35fde2009-02-11 23:03:27 +00001154 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001155 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001156 if (OldParams)
1157 OldParam = OldParams->begin();
1158
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001159 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001160 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1161 NewParamEnd = NewParams->end();
1162 NewParam != NewParamEnd; ++NewParam) {
1163 // Variables used to diagnose redundant default arguments
1164 bool RedundantDefaultArg = false;
1165 SourceLocation OldDefaultLoc;
1166 SourceLocation NewDefaultLoc;
1167
1168 // Variables used to diagnose missing default arguments
1169 bool MissingDefaultArg = false;
1170
Anders Carlsson49d25572009-06-12 23:20:15 +00001171 // C++0x [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001172 // If a template parameter of a primary class template is a template
Douglas Gregor1ed64762011-01-05 16:19:19 +00001173 // parameter pack, it shall be the last template parameter.
1174 if (SawParameterPack && TPC == TPC_ClassTemplate) {
Mike Stump1eb44332009-09-09 15:08:12 +00001175 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001176 diag::err_template_param_pack_must_be_last_template_parameter);
1177 Invalid = true;
1178 }
1179
Douglas Gregord684b002009-02-10 19:49:53 +00001180 if (TemplateTypeParmDecl *NewTypeParm
1181 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001182 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001183 if (NewTypeParm->hasDefaultArgument() &&
1184 DiagnoseDefaultTemplateArgument(*this, TPC,
1185 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001186 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001187 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001188 NewTypeParm->removeDefaultArgument();
1189
1190 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001191 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001192 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001193
Anders Carlsson49d25572009-06-12 23:20:15 +00001194 if (NewTypeParm->isParameterPack()) {
1195 assert(!NewTypeParm->hasDefaultArgument() &&
1196 "Parameter packs can't have a default argument!");
1197 SawParameterPack = true;
1198 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001199 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001200 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001201 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1202 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1203 SawDefaultArgument = true;
1204 RedundantDefaultArg = true;
1205 PreviousDefaultArgLoc = NewDefaultLoc;
1206 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1207 // Merge the default argument from the old declaration to the
1208 // new declaration.
1209 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001210 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001211 true);
1212 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1213 } else if (NewTypeParm->hasDefaultArgument()) {
1214 SawDefaultArgument = true;
1215 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1216 } else if (SawDefaultArgument)
1217 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001218 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001219 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001220 // Check for unexpanded parameter packs.
1221 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001222 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001223 UPPC_NonTypeTemplateParameterType)) {
1224 Invalid = true;
1225 continue;
1226 }
1227
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001228 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001229 if (NewNonTypeParm->hasDefaultArgument() &&
1230 DiagnoseDefaultTemplateArgument(*this, TPC,
1231 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001232 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001233 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001234 }
1235
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001236 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001237 NonTypeTemplateParmDecl *OldNonTypeParm
1238 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001239 if (NewNonTypeParm->isParameterPack()) {
1240 assert(!NewNonTypeParm->hasDefaultArgument() &&
1241 "Parameter packs can't have a default argument!");
1242 SawParameterPack = true;
1243 ParameterPackLoc = NewNonTypeParm->getLocation();
1244 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001245 NewNonTypeParm->hasDefaultArgument()) {
1246 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1247 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1248 SawDefaultArgument = true;
1249 RedundantDefaultArg = true;
1250 PreviousDefaultArgLoc = NewDefaultLoc;
1251 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1252 // Merge the default argument from the old declaration to the
1253 // new declaration.
1254 SawDefaultArgument = true;
1255 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001256 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001257 // parameter.
1258 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001259 OldNonTypeParm->getDefaultArgument(),
1260 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001261 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1262 } else if (NewNonTypeParm->hasDefaultArgument()) {
1263 SawDefaultArgument = true;
1264 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1265 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001266 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001267 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001268 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001269 TemplateTemplateParmDecl *NewTemplateParm
1270 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001271
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001272 // Check for unexpanded parameter packs, recursively.
1273 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1274 Invalid = true;
1275 continue;
1276 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001277
1278 if (NewTemplateParm->hasDefaultArgument() &&
1279 DiagnoseDefaultTemplateArgument(*this, TPC,
1280 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001281 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001282 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001283
1284 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001285 TemplateTemplateParmDecl *OldTemplateParm
1286 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001287 if (NewTemplateParm->isParameterPack()) {
1288 assert(!NewTemplateParm->hasDefaultArgument() &&
1289 "Parameter packs can't have a default argument!");
1290 SawParameterPack = true;
1291 ParameterPackLoc = NewTemplateParm->getLocation();
1292 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001293 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001294 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1295 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001296 SawDefaultArgument = true;
1297 RedundantDefaultArg = true;
1298 PreviousDefaultArgLoc = NewDefaultLoc;
1299 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1300 // Merge the default argument from the old declaration to the
1301 // new declaration.
1302 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001303 // FIXME: We need to create a new kind of "default argument" expression
1304 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001305 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001306 OldTemplateParm->getDefaultArgument(),
1307 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001308 PreviousDefaultArgLoc
1309 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001310 } else if (NewTemplateParm->hasDefaultArgument()) {
1311 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001312 PreviousDefaultArgLoc
1313 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001314 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001315 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001316 }
1317
1318 if (RedundantDefaultArg) {
1319 // C++ [temp.param]p12:
1320 // A template-parameter shall not be given default arguments
1321 // by two different declarations in the same scope.
1322 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1323 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1324 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001325 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001326 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001327 // If a template-parameter of a class template has a default
1328 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001329 // have a default template-argument supplied or be a template parameter
1330 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001331 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001332 diag::err_template_param_default_arg_missing);
1333 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1334 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001335 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001336 }
1337
1338 // If we have an old template parameter list that we're merging
1339 // in, move on to the next parameter.
1340 if (OldParams)
1341 ++OldParam;
1342 }
1343
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001344 // We were missing some default arguments at the end of the list, so remove
1345 // all of the default arguments.
1346 if (RemoveDefaultArguments) {
1347 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1348 NewParamEnd = NewParams->end();
1349 NewParam != NewParamEnd; ++NewParam) {
1350 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1351 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001352 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001353 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1354 NTTP->removeDefaultArgument();
1355 else
1356 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1357 }
1358 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001359
Douglas Gregord684b002009-02-10 19:49:53 +00001360 return Invalid;
1361}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001362
John McCall4e2cbb22010-10-20 05:44:58 +00001363namespace {
1364
1365/// A class which looks for a use of a certain level of template
1366/// parameter.
1367struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1368 typedef RecursiveASTVisitor<DependencyChecker> super;
1369
1370 unsigned Depth;
1371 bool Match;
1372
1373 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1374 NamedDecl *ND = Params->getParam(0);
1375 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1376 Depth = PD->getDepth();
1377 } else if (NonTypeTemplateParmDecl *PD =
1378 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1379 Depth = PD->getDepth();
1380 } else {
1381 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1382 }
1383 }
1384
1385 bool Matches(unsigned ParmDepth) {
1386 if (ParmDepth >= Depth) {
1387 Match = true;
1388 return true;
1389 }
1390 return false;
1391 }
1392
1393 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1394 return !Matches(T->getDepth());
1395 }
1396
1397 bool TraverseTemplateName(TemplateName N) {
1398 if (TemplateTemplateParmDecl *PD =
1399 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1400 if (Matches(PD->getDepth())) return false;
1401 return super::TraverseTemplateName(N);
1402 }
1403
1404 bool VisitDeclRefExpr(DeclRefExpr *E) {
1405 if (NonTypeTemplateParmDecl *PD =
1406 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1407 if (PD->getDepth() == Depth) {
1408 Match = true;
1409 return false;
1410 }
1411 }
1412 return super::VisitDeclRefExpr(E);
1413 }
1414};
1415}
1416
1417/// Determines whether a template-id depends on the given parameter
1418/// list.
1419static bool
1420DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId,
1421 TemplateParameterList *Params) {
1422 DependencyChecker Checker(Params);
1423 Checker.TraverseType(QualType(TemplateId, 0));
1424 return Checker.Match;
1425}
1426
Mike Stump1eb44332009-09-09 15:08:12 +00001427/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001428/// specifier, returning the template parameter list that applies to the
1429/// name.
1430///
1431/// \param DeclStartLoc the start of the declaration that has a scope
1432/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001433///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001434/// \param SS the scope specifier that will be matched to the given template
1435/// parameter lists. This scope specifier precedes a qualified name that is
1436/// being declared.
1437///
1438/// \param ParamLists the template parameter lists, from the outermost to the
1439/// innermost template parameter lists.
1440///
1441/// \param NumParamLists the number of template parameter lists in ParamLists.
1442///
John McCall77e8b112010-04-13 20:37:33 +00001443/// \param IsFriend Whether to apply the slightly different rules for
1444/// matching template parameters to scope specifiers in friend
1445/// declarations.
1446///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001447/// \param IsExplicitSpecialization will be set true if the entity being
1448/// declared is an explicit specialization, false otherwise.
1449///
Mike Stump1eb44332009-09-09 15:08:12 +00001450/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001451/// name that is preceded by the scope specifier @p SS. This template
1452/// parameter list may be have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001453/// template) or may have no template parameters (if we're declaring a
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001454/// template specialization), or may be NULL (if we were's declaring isn't
1455/// itself a template).
1456TemplateParameterList *
1457Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
1458 const CXXScopeSpec &SS,
1459 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001460 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001461 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001462 bool &IsExplicitSpecialization,
1463 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001464 IsExplicitSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001465
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001466 // Find the template-ids that occur within the nested-name-specifier. These
1467 // template-ids will match up with the template parameter lists.
1468 llvm::SmallVector<const TemplateSpecializationType *, 4>
1469 TemplateIdsInSpecifier;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001470 llvm::SmallVector<ClassTemplateSpecializationDecl *, 4>
1471 ExplicitSpecializationsInSpecifier;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001472 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
1473 NNS; NNS = NNS->getPrefix()) {
John McCall4b2b02b2009-12-15 02:19:47 +00001474 const Type *T = NNS->getAsType();
1475 if (!T) break;
1476
1477 // C++0x [temp.expl.spec]p17:
1478 // A member or a member template may be nested within many
1479 // enclosing class templates. In an explicit specialization for
1480 // such a member, the member declaration shall be preceded by a
1481 // template<> for each enclosing class template that is
1482 // explicitly specialized.
Douglas Gregorfe331062010-02-13 05:23:25 +00001483 //
1484 // Following the existing practice of GNU and EDG, we allow a typedef of a
1485 // template specialization type.
John McCall49f4e1c2010-12-10 11:01:00 +00001486 while (const TypedefType *TT = dyn_cast<TypedefType>(T))
1487 T = TT->getDecl()->getUnderlyingType().getTypePtr();
John McCall4b2b02b2009-12-15 02:19:47 +00001488
Mike Stump1eb44332009-09-09 15:08:12 +00001489 if (const TemplateSpecializationType *SpecType
Douglas Gregorfe331062010-02-13 05:23:25 +00001490 = dyn_cast<TemplateSpecializationType>(T)) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001491 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
1492 if (!Template)
1493 continue; // FIXME: should this be an error? probably...
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Ted Kremenek6217b802009-07-29 21:53:49 +00001495 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001496 ClassTemplateSpecializationDecl *SpecDecl
1497 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
1498 // If the nested name specifier refers to an explicit specialization,
1499 // we don't need a template<> header.
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001500 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
1501 ExplicitSpecializationsInSpecifier.push_back(SpecDecl);
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001502 continue;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001503 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001504 }
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001506 TemplateIdsInSpecifier.push_back(SpecType);
1507 }
1508 }
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001510 // Reverse the list of template-ids in the scope specifier, so that we can
1511 // more easily match up the template-ids and the template parameter lists.
1512 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001514 SourceLocation FirstTemplateLoc = DeclStartLoc;
1515 if (NumParamLists)
1516 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001518 // Match the template-ids found in the specifier to the template parameter
1519 // lists.
John McCall4e2cbb22010-10-20 05:44:58 +00001520 unsigned ParamIdx = 0, TemplateIdx = 0;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001521 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
John McCall4e2cbb22010-10-20 05:44:58 +00001522 TemplateIdx != NumTemplateIds; ++TemplateIdx) {
1523 const TemplateSpecializationType *TemplateId
1524 = TemplateIdsInSpecifier[TemplateIdx];
Douglas Gregorb88e8882009-07-30 17:40:51 +00001525 bool DependentTemplateId = TemplateId->isDependentType();
John McCall4e2cbb22010-10-20 05:44:58 +00001526
1527 // In friend declarations we can have template-ids which don't
1528 // depend on the corresponding template parameter lists. But
1529 // assume that empty parameter lists are supposed to match this
1530 // template-id.
1531 if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) {
1532 if (!DependentTemplateId ||
1533 !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx]))
1534 continue;
1535 }
1536
1537 if (ParamIdx >= NumParamLists) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001538 // We have a template-id without a corresponding template parameter
1539 // list.
John McCall77e8b112010-04-13 20:37:33 +00001540
1541 // ...which is fine if this is a friend declaration.
1542 if (IsFriend) {
1543 IsExplicitSpecialization = true;
1544 break;
1545 }
1546
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001547 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +00001548 // FIXME: the location information here isn't great.
1549 Diag(SS.getRange().getBegin(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001550 diag::err_template_spec_needs_template_parameters)
John McCall4e2cbb22010-10-20 05:44:58 +00001551 << QualType(TemplateId, 0)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001552 << SS.getRange();
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001553 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001554 } else {
1555 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
1556 << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +00001557 << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001558 IsExplicitSpecialization = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001559 }
1560 return 0;
1561 }
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001563 // Check the template parameter list against its corresponding template-id.
Douglas Gregorb88e8882009-07-30 17:40:51 +00001564 if (DependentTemplateId) {
John McCall31f17ec2010-04-27 00:57:59 +00001565 TemplateParameterList *ExpectedTemplateParams = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00001566
John McCall31f17ec2010-04-27 00:57:59 +00001567 // Are there cases in (e.g.) friends where this won't match?
1568 if (const InjectedClassNameType *Injected
1569 = TemplateId->getAs<InjectedClassNameType>()) {
1570 CXXRecordDecl *Record = Injected->getDecl();
1571 if (ClassTemplatePartialSpecializationDecl *Partial =
1572 dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
1573 ExpectedTemplateParams = Partial->getTemplateParameters();
1574 else
1575 ExpectedTemplateParams = Record->getDescribedClassTemplate()
1576 ->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001577 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001578
John McCall31f17ec2010-04-27 00:57:59 +00001579 if (ExpectedTemplateParams)
John McCall4e2cbb22010-10-20 05:44:58 +00001580 TemplateParameterListsAreEqual(ParamLists[ParamIdx],
John McCall31f17ec2010-04-27 00:57:59 +00001581 ExpectedTemplateParams,
1582 true, TPL_TemplateMatch);
1583
John McCall4e2cbb22010-10-20 05:44:58 +00001584 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1585 TPC_ClassTemplateMember);
1586 } else if (ParamLists[ParamIdx]->size() > 0)
1587 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00001588 diag::err_template_param_list_matches_nontemplate)
1589 << TemplateId
John McCall4e2cbb22010-10-20 05:44:58 +00001590 << ParamLists[ParamIdx]->getSourceRange();
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001591 else
1592 IsExplicitSpecialization = true;
John McCall4e2cbb22010-10-20 05:44:58 +00001593
1594 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001595 }
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001597 // If there were at least as many template-ids as there were template
1598 // parameter lists, then there are no template parameter lists remaining for
1599 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001600 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001601 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001603 // If there were too many template parameter lists, complain about that now.
John McCall4e2cbb22010-10-20 05:44:58 +00001604 if (ParamIdx != NumParamLists - 1) {
1605 while (ParamIdx < NumParamLists - 1) {
1606 bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0;
1607 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001608 isExplicitSpecHeader? diag::warn_template_spec_extra_headers
1609 : diag::err_template_spec_extra_headers)
John McCall4e2cbb22010-10-20 05:44:58 +00001610 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1611 ParamLists[ParamIdx]->getRAngleLoc());
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001612
1613 if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) {
1614 Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(),
1615 diag::note_explicit_template_spec_does_not_need_header)
1616 << ExplicitSpecializationsInSpecifier.back();
1617 ExplicitSpecializationsInSpecifier.pop_back();
1618 }
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001619
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001620 // We have a template parameter list with no corresponding scope, which
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001621 // means that the resulting template declaration can't be instantiated
1622 // properly (we'll end up with dependent nodes when we shouldn't).
1623 if (!isExplicitSpecHeader)
1624 Invalid = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001625
John McCall4e2cbb22010-10-20 05:44:58 +00001626 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001627 }
1628 }
Mike Stump1eb44332009-09-09 15:08:12 +00001629
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001630 // Return the last template parameter list, which corresponds to the
1631 // entity being declared.
1632 return ParamLists[NumParamLists - 1];
1633}
1634
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001635void Sema::NoteAllFoundTemplates(TemplateName Name) {
1636 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1637 Diag(Template->getLocation(), diag::note_template_declared_here)
1638 << (isa<FunctionTemplateDecl>(Template)? 0
1639 : isa<ClassTemplateDecl>(Template)? 1
1640 : 2)
1641 << Template->getDeclName();
1642 return;
1643 }
1644
1645 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1646 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1647 IEnd = OST->end();
1648 I != IEnd; ++I)
1649 Diag((*I)->getLocation(), diag::note_template_declared_here)
1650 << 0 << (*I)->getDeclName();
1651
1652 return;
1653 }
1654}
1655
1656
Douglas Gregor7532dc62009-03-30 22:58:21 +00001657QualType Sema::CheckTemplateIdType(TemplateName Name,
1658 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001659 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001660 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001661 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1662 // We might have a substituted template template parameter pack. If so,
1663 // build a template specialization type for it.
1664 if (Name.getAsSubstTemplateTemplateParmPack())
1665 return Context.getTemplateSpecializationType(Name, TemplateArgs);
1666
1667 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1668 << Name;
1669 NoteAllFoundTemplates(Name);
1670 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001671 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001672
Douglas Gregor40808ce2009-03-09 23:48:35 +00001673 // Check that the template argument list is well-formed for this
1674 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001675 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001676 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001677 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001678 return QualType();
1679
Douglas Gregor910f8002010-11-07 23:05:16 +00001680 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001681 "Converted template argument list is too short!");
1682
1683 QualType CanonType;
1684
Douglas Gregorcaddba02009-11-12 18:38:13 +00001685 if (Name.isDependent() ||
1686 TemplateSpecializationType::anyDependentTemplateArguments(
John McCalld5532b62009-11-23 01:53:49 +00001687 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001688 // This class template specialization is a dependent
1689 // type. Therefore, its canonical type is another class template
1690 // specialization type that contains all of the converted
1691 // arguments in canonical form. This ensures that, e.g., A<T> and
1692 // A<T, T> have identical types when A is declared as:
1693 //
1694 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001695 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001696 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001697 Converted.data(),
1698 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001699
Douglas Gregor1275ae02009-07-28 23:00:59 +00001700 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001701 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001702 // In the future, we need to teach getTemplateSpecializationType to only
1703 // build the canonical type and return that to us.
1704 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001705
1706 // This might work out to be a current instantiation, in which
1707 // case the canonical type needs to be the InjectedClassNameType.
1708 //
1709 // TODO: in theory this could be a simple hashtable lookup; most
1710 // changes to CurContext don't change the set of current
1711 // instantiations.
1712 if (isa<ClassTemplateDecl>(Template)) {
1713 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1714 // If we get out to a namespace, we're done.
1715 if (Ctx->isFileContext()) break;
1716
1717 // If this isn't a record, keep looking.
1718 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1719 if (!Record) continue;
1720
1721 // Look for one of the two cases with InjectedClassNameTypes
1722 // and check whether it's the same template.
1723 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1724 !Record->getDescribedClassTemplate())
1725 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001726
John McCall31f17ec2010-04-27 00:57:59 +00001727 // Fetch the injected class name type and check whether its
1728 // injected type is equal to the type we just built.
1729 QualType ICNT = Context.getTypeDeclType(Record);
1730 QualType Injected = cast<InjectedClassNameType>(ICNT)
1731 ->getInjectedSpecializationType();
1732
1733 if (CanonType != Injected->getCanonicalTypeInternal())
1734 continue;
1735
1736 // If so, the canonical type of this TST is the injected
1737 // class name type of the record we just found.
1738 assert(ICNT.isCanonical());
1739 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001740 break;
1741 }
1742 }
Mike Stump1eb44332009-09-09 15:08:12 +00001743 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001744 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001745 // Find the class template specialization declaration that
1746 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001747 void *InsertPos = 0;
1748 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001749 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001750 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001751 if (!Decl) {
1752 // This is the first time we have referenced this class template
1753 // specialization. Create the canonical declaration and add it to
1754 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001755 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001756 ClassTemplate->getTemplatedDecl()->getTagKind(),
1757 ClassTemplate->getDeclContext(),
1758 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001759 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001760 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001761 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001762 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001763 Decl->setLexicalDeclContext(CurContext);
1764 }
1765
1766 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00001767 assert(isa<RecordType>(CanonType) &&
1768 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00001769 }
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Douglas Gregor40808ce2009-03-09 23:48:35 +00001771 // Build the fully-sugared type for this class template
1772 // specialization, which refers back to the class template
1773 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00001774 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001775}
1776
John McCallf312b1e2010-08-26 23:41:50 +00001777TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00001778Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
1779 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001780 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001781 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00001782 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00001783 if (SS.isInvalid())
1784 return true;
1785
Douglas Gregor7532dc62009-03-30 22:58:21 +00001786 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001787
Douglas Gregor40808ce2009-03-09 23:48:35 +00001788 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00001789 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00001790 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001791
Douglas Gregora88f09f2011-02-28 17:23:35 +00001792 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1793 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
1794 DTN->getQualifier(),
1795 DTN->getIdentifier(),
1796 TemplateArgs);
1797
1798 // Build type-source information.
1799 TypeLocBuilder TLB;
1800 DependentTemplateSpecializationTypeLoc SpecTL
1801 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00001802 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00001803 SpecTL.setNameLoc(TemplateLoc);
1804 SpecTL.setLAngleLoc(LAngleLoc);
1805 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00001806 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00001807 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1808 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1809 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1810 }
1811
John McCalld5532b62009-11-23 01:53:49 +00001812 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001813 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00001814
1815 if (Result.isNull())
1816 return true;
1817
Douglas Gregor059101f2011-03-02 00:47:37 +00001818 // Build type-source information.
1819 TypeLocBuilder TLB;
1820 TemplateSpecializationTypeLoc SpecTL
1821 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1822 SpecTL.setTemplateNameLoc(TemplateLoc);
1823 SpecTL.setLAngleLoc(LAngleLoc);
1824 SpecTL.setRAngleLoc(RAngleLoc);
1825 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1826 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00001827
Douglas Gregor059101f2011-03-02 00:47:37 +00001828 if (SS.isNotEmpty()) {
1829 // Create an elaborated-type-specifier containing the nested-name-specifier.
1830 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
1831 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1832 ElabTL.setKeywordLoc(SourceLocation());
1833 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1834 }
1835
1836 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00001837}
John McCallf1bbbb42009-09-04 01:14:41 +00001838
Douglas Gregor059101f2011-03-02 00:47:37 +00001839TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00001840 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00001841 SourceLocation TagLoc,
1842 CXXScopeSpec &SS,
1843 TemplateTy TemplateD,
1844 SourceLocation TemplateLoc,
1845 SourceLocation LAngleLoc,
1846 ASTTemplateArgsPtr TemplateArgsIn,
1847 SourceLocation RAngleLoc) {
1848 TemplateName Template = TemplateD.getAsVal<TemplateName>();
1849
1850 // Translate the parser's template argument list in our AST format.
1851 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
1852 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
1853
1854 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001855 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00001856 ElaboratedTypeKeyword Keyword
1857 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00001858
Douglas Gregor059101f2011-03-02 00:47:37 +00001859 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1860 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
1861 DTN->getQualifier(),
1862 DTN->getIdentifier(),
1863 TemplateArgs);
1864
1865 // Build type-source information.
1866 TypeLocBuilder TLB;
1867 DependentTemplateSpecializationTypeLoc SpecTL
1868 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
1869 SpecTL.setKeywordLoc(TagLoc);
1870 SpecTL.setNameLoc(TemplateLoc);
1871 SpecTL.setLAngleLoc(LAngleLoc);
1872 SpecTL.setRAngleLoc(RAngleLoc);
1873 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
1874 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1875 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1876 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1877 }
1878
1879 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
1880 if (Result.isNull())
1881 return TypeResult();
1882
1883 // Check the tag kind
1884 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00001885 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00001886
John McCall6b2becf2009-09-08 17:47:29 +00001887 IdentifierInfo *Id = D->getIdentifier();
1888 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00001889
John McCall6b2becf2009-09-08 17:47:29 +00001890 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1891 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00001892 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00001893 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00001894 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00001895 }
1896 }
Douglas Gregor059101f2011-03-02 00:47:37 +00001897
1898 // Provide source-location information for the template specialization.
1899 TypeLocBuilder TLB;
1900 TemplateSpecializationTypeLoc SpecTL
1901 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1902 SpecTL.setTemplateNameLoc(TemplateLoc);
1903 SpecTL.setLAngleLoc(LAngleLoc);
1904 SpecTL.setRAngleLoc(RAngleLoc);
1905 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1906 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00001907
Douglas Gregor059101f2011-03-02 00:47:37 +00001908 // Construct an elaborated type containing the nested-name-specifier (if any)
1909 // and keyword.
1910 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
1911 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1912 ElabTL.setKeywordLoc(TagLoc);
1913 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1914 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00001915}
1916
John McCall60d7b3a2010-08-24 06:29:42 +00001917ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001918 LookupResult &R,
1919 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00001920 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001921 // FIXME: Can we do any checking at this point? I guess we could check the
1922 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00001923 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001924 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001925 // foo<int> could identify a single function unambiguously
1926 // This approach does NOT work, since f<int>(1);
1927 // gets resolved prior to resorting to overload resolution
1928 // i.e., template<class T> void f(double);
1929 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00001930
1931 // These should be filtered out by our callers.
1932 assert(!R.empty() && "empty lookup results when building templateid");
1933 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
1934
John McCallc373d482010-01-27 01:50:18 +00001935 // We don't want lookup warnings at this point.
1936 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001937
John McCallf7a1a742009-11-24 19:00:30 +00001938 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001939 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00001940 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00001941 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001942 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001943 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00001944
1945 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001946}
1947
John McCallf7a1a742009-11-24 19:00:30 +00001948// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00001949ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00001950Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00001951 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00001952 const TemplateArgumentListInfo &TemplateArgs) {
1953 DeclContext *DC;
1954 if (!(DC = computeDeclContext(SS, false)) ||
1955 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00001956 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00001957 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001958
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001959 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00001960 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001961 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
1962 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00001963
John McCallf7a1a742009-11-24 19:00:30 +00001964 if (R.isAmbiguous())
1965 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001966
John McCallf7a1a742009-11-24 19:00:30 +00001967 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001968 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
1969 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001970 return ExprError();
1971 }
1972
1973 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001974 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
1975 << (NestedNameSpecifier*) SS.getScopeRep()
1976 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001977 Diag(Temp->getLocation(), diag::note_referenced_class_template);
1978 return ExprError();
1979 }
1980
1981 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001982}
1983
Douglas Gregorc45c2322009-03-31 00:43:58 +00001984/// \brief Form a dependent template name.
1985///
1986/// This action forms a dependent template name given the template
1987/// name and its (presumably dependent) scope specifier. For
1988/// example, given "MetaFun::template apply", the scope specifier \p
1989/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
1990/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001991TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001992 SourceLocation TemplateKWLoc,
1993 CXXScopeSpec &SS,
1994 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00001995 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001996 bool EnteringContext,
1997 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00001998 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
1999 !getLangOptions().CPlusPlus0x)
2000 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002001 << FixItHint::CreateRemoval(TemplateKWLoc);
2002
Douglas Gregor0707bc52010-01-19 16:01:07 +00002003 DeclContext *LookupCtx = 0;
2004 if (SS.isSet())
2005 LookupCtx = computeDeclContext(SS, EnteringContext);
2006 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002007 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002008 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002009 // C++0x [temp.names]p5:
2010 // If a name prefixed by the keyword template is not the name of
2011 // a template, the program is ill-formed. [Note: the keyword
2012 // template may not be applied to non-template members of class
2013 // templates. -end note ] [ Note: as is the case with the
2014 // typename prefix, the template prefix is allowed in cases
2015 // where it is not strictly necessary; i.e., when the
2016 // nested-name-specifier or the expression on the left of the ->
2017 // or . is not dependent on a template-parameter, or the use
2018 // does not appear in the scope of a template. -end note]
2019 //
2020 // Note: C++03 was more strict here, because it banned the use of
2021 // the "template" keyword prior to a template-name that was not a
2022 // dependent name. C++ DR468 relaxed this requirement (the
2023 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002024 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002025 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002026 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2027 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002028 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002029 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2030 isa<CXXRecordDecl>(LookupCtx) &&
2031 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002032 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002033 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002034 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002035 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002036 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002037 << Name.getSourceRange()
2038 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002039 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002040 } else {
2041 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002042 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002043 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002044 }
2045
Mike Stump1eb44332009-09-09 15:08:12 +00002046 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002047 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002048
Douglas Gregor014e88d2009-11-03 23:16:33 +00002049 switch (Name.getKind()) {
2050 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002051 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002052 Name.Identifier));
2053 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002054
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002055 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002056 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002057 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002058 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002059
2060 case UnqualifiedId::IK_LiteralOperatorId:
2061 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
2062
Douglas Gregor014e88d2009-11-03 23:16:33 +00002063 default:
2064 break;
2065 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002066
2067 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002068 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002069 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002070 << Name.getSourceRange()
2071 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002072 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002073}
2074
Mike Stump1eb44332009-09-09 15:08:12 +00002075bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002076 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00002077 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002078 const TemplateArgument &Arg = AL.getArgument();
2079
Anders Carlsson436b1562009-06-13 00:33:33 +00002080 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002081 switch(Arg.getKind()) {
2082 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002083 // C++ [temp.arg.type]p1:
2084 // A template-argument for a template-parameter which is a
2085 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002086 break;
2087 case TemplateArgument::Template: {
2088 // We have a template type parameter but the template argument
2089 // is a template without any arguments.
2090 SourceRange SR = AL.getSourceRange();
2091 TemplateName Name = Arg.getAsTemplate();
2092 Diag(SR.getBegin(), diag::err_template_missing_args)
2093 << Name << SR;
2094 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2095 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002096
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002097 return true;
2098 }
2099 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002100 // We have a template type parameter but the template argument
2101 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002102 SourceRange SR = AL.getSourceRange();
2103 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002104 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002105
Anders Carlsson436b1562009-06-13 00:33:33 +00002106 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002107 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002108 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002109
John McCalla93c9342009-12-07 02:54:59 +00002110 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002111 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002112
Anders Carlsson436b1562009-06-13 00:33:33 +00002113 // Add the converted template type argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002114 Converted.push_back(
John McCall833ca992009-10-29 08:12:44 +00002115 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00002116 return false;
2117}
2118
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002119/// \brief Substitute template arguments into the default template argument for
2120/// the given template type parameter.
2121///
2122/// \param SemaRef the semantic analysis object for which we are performing
2123/// the substitution.
2124///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002125/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002126/// for.
2127///
2128/// \param TemplateLoc the location of the template name that started the
2129/// template-id we are checking.
2130///
2131/// \param RAngleLoc the location of the right angle bracket ('>') that
2132/// terminates the template-id.
2133///
2134/// \param Param the template template parameter whose default we are
2135/// substituting into.
2136///
2137/// \param Converted the list of template arguments provided for template
2138/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002139/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002140static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002141SubstDefaultTemplateArgument(Sema &SemaRef,
2142 TemplateDecl *Template,
2143 SourceLocation TemplateLoc,
2144 SourceLocation RAngleLoc,
2145 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002146 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002147 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002148
2149 // If the argument type is dependent, instantiate it now based
2150 // on the previously-computed template arguments.
2151 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002152 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002153 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002154
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002155 MultiLevelTemplateArgumentList AllTemplateArgs
2156 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2157
2158 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002159 Template, Converted.data(),
2160 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002161 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002162
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002163 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2164 Param->getDefaultArgumentLoc(),
2165 Param->getDeclName());
2166 }
2167
2168 return ArgType;
2169}
2170
2171/// \brief Substitute template arguments into the default template argument for
2172/// the given non-type template parameter.
2173///
2174/// \param SemaRef the semantic analysis object for which we are performing
2175/// the substitution.
2176///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002177/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002178/// for.
2179///
2180/// \param TemplateLoc the location of the template name that started the
2181/// template-id we are checking.
2182///
2183/// \param RAngleLoc the location of the right angle bracket ('>') that
2184/// terminates the template-id.
2185///
Douglas Gregor788cd062009-11-11 01:00:40 +00002186/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002187/// substituting into.
2188///
2189/// \param Converted the list of template arguments provided for template
2190/// parameters that precede \p Param in the template parameter list.
2191///
2192/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002193static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002194SubstDefaultTemplateArgument(Sema &SemaRef,
2195 TemplateDecl *Template,
2196 SourceLocation TemplateLoc,
2197 SourceLocation RAngleLoc,
2198 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002199 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002200 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002201 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002202
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002203 MultiLevelTemplateArgumentList AllTemplateArgs
2204 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002205
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002206 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002207 Template, Converted.data(),
2208 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002209 SourceRange(TemplateLoc, RAngleLoc));
2210
2211 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2212}
2213
Douglas Gregor788cd062009-11-11 01:00:40 +00002214/// \brief Substitute template arguments into the default template argument for
2215/// the given template template parameter.
2216///
2217/// \param SemaRef the semantic analysis object for which we are performing
2218/// the substitution.
2219///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002220/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002221/// for.
2222///
2223/// \param TemplateLoc the location of the template name that started the
2224/// template-id we are checking.
2225///
2226/// \param RAngleLoc the location of the right angle bracket ('>') that
2227/// terminates the template-id.
2228///
2229/// \param Param the template template parameter whose default we are
2230/// substituting into.
2231///
2232/// \param Converted the list of template arguments provided for template
2233/// parameters that precede \p Param in the template parameter list.
2234///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002235/// \param QualifierLoc Will be set to the nested-name-specifier (with
2236/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002237///
Douglas Gregor788cd062009-11-11 01:00:40 +00002238/// \returns the substituted template argument, or NULL if an error occurred.
2239static TemplateName
2240SubstDefaultTemplateArgument(Sema &SemaRef,
2241 TemplateDecl *Template,
2242 SourceLocation TemplateLoc,
2243 SourceLocation RAngleLoc,
2244 TemplateTemplateParmDecl *Param,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002245 llvm::SmallVectorImpl<TemplateArgument> &Converted,
2246 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002247 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002248 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002249
Douglas Gregor788cd062009-11-11 01:00:40 +00002250 MultiLevelTemplateArgumentList AllTemplateArgs
2251 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002252
Douglas Gregor788cd062009-11-11 01:00:40 +00002253 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002254 Template, Converted.data(),
2255 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002256 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002257
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002258 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002259 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002260 if (QualifierLoc) {
2261 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2262 AllTemplateArgs);
2263 if (!QualifierLoc)
2264 return TemplateName();
2265 }
2266
Douglas Gregor1d752d72011-03-02 18:46:51 +00002267 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002268 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002269 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002270 AllTemplateArgs);
2271}
2272
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002273/// \brief If the given template parameter has a default template
2274/// argument, substitute into that default template argument and
2275/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002276TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002277Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2278 SourceLocation TemplateLoc,
2279 SourceLocation RAngleLoc,
2280 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002281 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2282 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002283 if (!TypeParm->hasDefaultArgument())
2284 return TemplateArgumentLoc();
2285
John McCalla93c9342009-12-07 02:54:59 +00002286 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002287 TemplateLoc,
2288 RAngleLoc,
2289 TypeParm,
2290 Converted);
2291 if (DI)
2292 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2293
2294 return TemplateArgumentLoc();
2295 }
2296
2297 if (NonTypeTemplateParmDecl *NonTypeParm
2298 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2299 if (!NonTypeParm->hasDefaultArgument())
2300 return TemplateArgumentLoc();
2301
John McCall60d7b3a2010-08-24 06:29:42 +00002302 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002303 TemplateLoc,
2304 RAngleLoc,
2305 NonTypeParm,
2306 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002307 if (Arg.isInvalid())
2308 return TemplateArgumentLoc();
2309
2310 Expr *ArgE = Arg.takeAs<Expr>();
2311 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2312 }
2313
2314 TemplateTemplateParmDecl *TempTempParm
2315 = cast<TemplateTemplateParmDecl>(Param);
2316 if (!TempTempParm->hasDefaultArgument())
2317 return TemplateArgumentLoc();
2318
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002319
Douglas Gregor1d752d72011-03-02 18:46:51 +00002320 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002321 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002322 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002323 RAngleLoc,
2324 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002325 Converted,
2326 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002327 if (TName.isNull())
2328 return TemplateArgumentLoc();
2329
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002330 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002331 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002332 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2333}
2334
Douglas Gregore7526412009-11-11 19:31:23 +00002335/// \brief Check that the given template argument corresponds to the given
2336/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002337///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002338/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002339/// checked.
2340///
2341/// \param Arg The template argument.
2342///
2343/// \param Template The template in which the template argument resides.
2344///
2345/// \param TemplateLoc The location of the template name for the template
2346/// whose argument list we're matching.
2347///
2348/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2349/// the template argument list.
2350///
2351/// \param ArgumentPackIndex The index into the argument pack where this
2352/// argument will be placed. Only valid if the parameter is a parameter pack.
2353///
2354/// \param Converted The checked, converted argument will be added to the
2355/// end of this small vector.
2356///
2357/// \param CTAK Describes how we arrived at this particular template argument:
2358/// explicitly written, deduced, etc.
2359///
2360/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002361bool Sema::CheckTemplateArgument(NamedDecl *Param,
2362 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002363 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002364 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002365 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002366 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002367 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002368 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002369 // Check template type parameters.
2370 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002371 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002372
Douglas Gregord9e15302009-11-11 19:41:09 +00002373 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002374 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002375 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002376 // with the template arguments we've seen thus far. But if the
2377 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002378 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002379 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2380 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002381
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002382 if (NTTPType->isDependentType() &&
2383 !isa<TemplateTemplateParmDecl>(Template) &&
2384 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002385 // Do substitution on the type of the non-type template parameter.
2386 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002387 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002388 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002389
2390 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002391 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002392 NTTPType = SubstType(NTTPType,
2393 MultiLevelTemplateArgumentList(TemplateArgs),
2394 NTTP->getLocation(),
2395 NTTP->getDeclName());
2396 // If that worked, check the non-type template parameter type
2397 // for validity.
2398 if (!NTTPType.isNull())
2399 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2400 NTTP->getLocation());
2401 if (NTTPType.isNull())
2402 return true;
2403 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002404
Douglas Gregore7526412009-11-11 19:31:23 +00002405 switch (Arg.getArgument().getKind()) {
2406 case TemplateArgument::Null:
2407 assert(false && "Should never see a NULL template argument here");
2408 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002409
Douglas Gregore7526412009-11-11 19:31:23 +00002410 case TemplateArgument::Expression: {
2411 Expr *E = Arg.getArgument().getAsExpr();
2412 TemplateArgument Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002413 if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK))
Douglas Gregore7526412009-11-11 19:31:23 +00002414 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002415
Douglas Gregor910f8002010-11-07 23:05:16 +00002416 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002417 break;
2418 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002419
Douglas Gregore7526412009-11-11 19:31:23 +00002420 case TemplateArgument::Declaration:
2421 case TemplateArgument::Integral:
2422 // We've already checked this template argument, so just copy
2423 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002424 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002425 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002426
Douglas Gregore7526412009-11-11 19:31:23 +00002427 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002428 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002429 // We were given a template template argument. It may not be ill-formed;
2430 // see below.
2431 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002432 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2433 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002434 // We have a template argument such as \c T::template X, which we
2435 // parsed as a template template argument. However, since we now
2436 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002437 // template name into an expression.
2438
2439 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2440 Arg.getTemplateNameLoc());
2441
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002442 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002443 SS.Adopt(Arg.getTemplateQualifierLoc());
John McCallf7a1a742009-11-24 19:00:30 +00002444 Expr *E = DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002445 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002446 NameInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002447
Douglas Gregora7fc9012011-01-05 18:58:31 +00002448 // If we parsed the template argument as a pack expansion, create a
2449 // pack expansion expression.
2450 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002451 ExprResult Expansion = ActOnPackExpansion(E,
Douglas Gregora7fc9012011-01-05 18:58:31 +00002452 Arg.getTemplateEllipsisLoc());
2453 if (Expansion.isInvalid())
2454 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002455
Douglas Gregora7fc9012011-01-05 18:58:31 +00002456 E = Expansion.get();
2457 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002458
Douglas Gregore7526412009-11-11 19:31:23 +00002459 TemplateArgument Result;
2460 if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
2461 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002462
Douglas Gregor910f8002010-11-07 23:05:16 +00002463 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002464 break;
2465 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002466
Douglas Gregore7526412009-11-11 19:31:23 +00002467 // We have a template argument that actually does refer to a class
2468 // template, template alias, or template template parameter, and
2469 // therefore cannot be a non-type template argument.
2470 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2471 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002472
Douglas Gregore7526412009-11-11 19:31:23 +00002473 Diag(Param->getLocation(), diag::note_template_param_here);
2474 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002475
Douglas Gregore7526412009-11-11 19:31:23 +00002476 case TemplateArgument::Type: {
2477 // We have a non-type template parameter but the template
2478 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002479
Douglas Gregore7526412009-11-11 19:31:23 +00002480 // C++ [temp.arg]p2:
2481 // In a template-argument, an ambiguity between a type-id and
2482 // an expression is resolved to a type-id, regardless of the
2483 // form of the corresponding template-parameter.
2484 //
2485 // We warn specifically about this case, since it can be rather
2486 // confusing for users.
2487 QualType T = Arg.getArgument().getAsType();
2488 SourceRange SR = Arg.getSourceRange();
2489 if (T->isFunctionType())
2490 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2491 else
2492 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2493 Diag(Param->getLocation(), diag::note_template_param_here);
2494 return true;
2495 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002496
Douglas Gregore7526412009-11-11 19:31:23 +00002497 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002498 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002499 break;
2500 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002501
Douglas Gregore7526412009-11-11 19:31:23 +00002502 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002503 }
2504
2505
Douglas Gregore7526412009-11-11 19:31:23 +00002506 // Check template template parameters.
2507 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002508
Douglas Gregore7526412009-11-11 19:31:23 +00002509 // Substitute into the template parameter list of the template
2510 // template parameter, since previously-supplied template arguments
2511 // may appear within the template template parameter.
2512 {
2513 // Set up a template instantiation context.
2514 LocalInstantiationScope Scope(*this);
2515 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002516 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002517 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002518
2519 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002520 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002521 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002522 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002523 MultiLevelTemplateArgumentList(TemplateArgs)));
2524 if (!TempParm)
2525 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002526 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002527
Douglas Gregore7526412009-11-11 19:31:23 +00002528 switch (Arg.getArgument().getKind()) {
2529 case TemplateArgument::Null:
2530 assert(false && "Should never see a NULL template argument here");
2531 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002532
Douglas Gregore7526412009-11-11 19:31:23 +00002533 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002534 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002535 if (CheckTemplateArgument(TempParm, Arg))
2536 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002537
Douglas Gregor910f8002010-11-07 23:05:16 +00002538 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002539 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002540
Douglas Gregore7526412009-11-11 19:31:23 +00002541 case TemplateArgument::Expression:
2542 case TemplateArgument::Type:
2543 // We have a template template parameter but the template
2544 // argument does not refer to a template.
2545 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
2546 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002547
Douglas Gregore7526412009-11-11 19:31:23 +00002548 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002549 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002550 "Declaration argument with template template parameter");
2551 break;
2552 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002553 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002554 "Integral argument with template template parameter");
2555 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002556
Douglas Gregore7526412009-11-11 19:31:23 +00002557 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002558 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002559 break;
2560 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002561
Douglas Gregore7526412009-11-11 19:31:23 +00002562 return false;
2563}
2564
Douglas Gregorc15cb382009-02-09 23:23:08 +00002565/// \brief Check that the given template argument list is well-formed
2566/// for specializing the given template.
2567bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2568 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002569 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002570 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002571 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002572 TemplateParameterList *Params = Template->getTemplateParameters();
2573 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002574 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002575 bool Invalid = false;
2576
John McCalld5532b62009-11-23 01:53:49 +00002577 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2578
Mike Stump1eb44332009-09-09 15:08:12 +00002579 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002580 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002581
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002582 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002583 (NumArgs < Params->getMinRequiredArguments() &&
2584 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002585 // FIXME: point at either the first arg beyond what we can handle,
2586 // or the '>', depending on whether we have too many or too few
2587 // arguments.
2588 SourceRange Range;
2589 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002590 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002591 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2592 << (NumArgs > NumParams)
2593 << (isa<ClassTemplateDecl>(Template)? 0 :
2594 isa<FunctionTemplateDecl>(Template)? 1 :
2595 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2596 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002597 Diag(Template->getLocation(), diag::note_template_decl_here)
2598 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002599 Invalid = true;
2600 }
Mike Stump1eb44332009-09-09 15:08:12 +00002601
2602 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002603 // [...] The type and form of each template-argument specified in
2604 // a template-id shall match the type and form specified for the
2605 // corresponding parameter declared by the template in its
2606 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002607 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002608 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2609 TemplateParameterList::iterator Param = Params->begin(),
2610 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002611 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002612 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002613 while (Param != ParamEnd) {
Douglas Gregor16134c62009-07-01 00:28:38 +00002614 if (ArgIdx > NumArgs && PartialTemplateArgs)
2615 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002616
Douglas Gregorf35f8282009-11-11 21:54:23 +00002617 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002618 // If we have an expanded parameter pack, make sure we don't have too
2619 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002620 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002621 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002622 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002623 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2624 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2625 << true
2626 << (isa<ClassTemplateDecl>(Template)? 0 :
2627 isa<FunctionTemplateDecl>(Template)? 1 :
2628 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2629 << Template;
2630 Diag(Template->getLocation(), diag::note_template_decl_here)
2631 << Params->getSourceRange();
2632 return true;
2633 }
2634 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002635
Douglas Gregorf35f8282009-11-11 21:54:23 +00002636 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002637 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2638 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002639 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002640 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002641
Douglas Gregor14be16b2010-12-20 16:57:52 +00002642 if ((*Param)->isTemplateParameterPack()) {
2643 // The template parameter was a template parameter pack, so take the
2644 // deduced argument and place it on the argument pack. Note that we
2645 // stay on the same template parameter so that we can deduce more
2646 // arguments.
2647 ArgumentPack.push_back(Converted.back());
2648 Converted.pop_back();
2649 } else {
2650 // Move to the next template parameter.
2651 ++Param;
2652 }
2653 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002654 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002655 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002656
2657 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002658 // arguments, just break out now and we'll fill in the argument pack below.
2659 if ((*Param)->isTemplateParameterPack())
2660 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002661
Douglas Gregorf35f8282009-11-11 21:54:23 +00002662 // We have a default template argument that we will use.
2663 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002664
Douglas Gregorf35f8282009-11-11 21:54:23 +00002665 // Retrieve the default template argument from the template
2666 // parameter. For each kind of template parameter, we substitute the
2667 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002668 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002669 // the default argument.
2670 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2671 if (!TTP->hasDefaultArgument()) {
2672 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2673 break;
2674 }
2675
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002676 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002677 Template,
2678 TemplateLoc,
2679 RAngleLoc,
2680 TTP,
2681 Converted);
2682 if (!ArgType)
2683 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002684
Douglas Gregorf35f8282009-11-11 21:54:23 +00002685 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2686 ArgType);
2687 } else if (NonTypeTemplateParmDecl *NTTP
2688 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2689 if (!NTTP->hasDefaultArgument()) {
2690 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2691 break;
2692 }
2693
John McCall60d7b3a2010-08-24 06:29:42 +00002694 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002695 TemplateLoc,
2696 RAngleLoc,
2697 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002698 Converted);
2699 if (E.isInvalid())
2700 return true;
2701
2702 Expr *Ex = E.takeAs<Expr>();
2703 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2704 } else {
2705 TemplateTemplateParmDecl *TempParm
2706 = cast<TemplateTemplateParmDecl>(*Param);
2707
2708 if (!TempParm->hasDefaultArgument()) {
2709 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2710 break;
2711 }
2712
Douglas Gregor1d752d72011-03-02 18:46:51 +00002713 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002714 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002715 TemplateLoc,
2716 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002717 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002718 Converted,
2719 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002720 if (Name.isNull())
2721 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002722
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002723 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
2724 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00002725 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002726
Douglas Gregorf35f8282009-11-11 21:54:23 +00002727 // Introduce an instantiation record that describes where we are using
2728 // the default template argument.
2729 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002730 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002731 SourceRange(TemplateLoc, RAngleLoc));
2732
Douglas Gregorf35f8282009-11-11 21:54:23 +00002733 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002734 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002735 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00002736 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002737
Douglas Gregor67714232011-03-03 02:41:12 +00002738 // Core issue 150 (assumed resolution): if this is a template template
2739 // parameter, keep track of the default template arguments from the
2740 // template definition.
2741 if (isTemplateTemplateParameter)
2742 TemplateArgs.addArgument(Arg);
2743
Douglas Gregor14be16b2010-12-20 16:57:52 +00002744 // Move to the next template parameter and argument.
2745 ++Param;
2746 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002747 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002748
Douglas Gregor14be16b2010-12-20 16:57:52 +00002749 // Form argument packs for each of the parameter packs remaining.
2750 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00002751 // If we're checking a partial list of template arguments, don't fill
2752 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002753
2754 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002755 if (PartialTemplateArgs && ArgumentPack.empty()) {
2756 Converted.push_back(TemplateArgument());
Douglas Gregor203e6a32011-01-11 23:09:57 +00002757 } else if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00002758 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00002759 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002760 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2761 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00002762 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00002763 ArgumentPack.clear();
2764 }
2765 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002766
Douglas Gregor14be16b2010-12-20 16:57:52 +00002767 ++Param;
2768 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002769
Douglas Gregorc15cb382009-02-09 23:23:08 +00002770 return Invalid;
2771}
2772
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002773namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002774 class UnnamedLocalNoLinkageFinder
2775 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002776 {
2777 Sema &S;
2778 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002779
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002780 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002781
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002782 public:
2783 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
2784
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002785 bool Visit(QualType T) {
2786 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002787 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002788
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002789#define TYPE(Class, Parent) \
2790 bool Visit##Class##Type(const Class##Type *);
2791#define ABSTRACT_TYPE(Class, Parent) \
2792 bool Visit##Class##Type(const Class##Type *) { return false; }
2793#define NON_CANONICAL_TYPE(Class, Parent) \
2794 bool Visit##Class##Type(const Class##Type *) { return false; }
2795#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002796
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002797 bool VisitTagDecl(const TagDecl *Tag);
2798 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
2799 };
2800}
2801
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002802bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002803 return false;
2804}
2805
2806bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
2807 return Visit(T->getElementType());
2808}
2809
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002810bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002811 return Visit(T->getPointeeType());
2812}
2813
2814bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002815 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002816 return Visit(T->getPointeeType());
2817}
2818
2819bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002820 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002821 return Visit(T->getPointeeType());
2822}
2823
2824bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002825 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002826 return Visit(T->getPointeeType());
2827}
2828
2829bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002830 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002831 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
2832}
2833
2834bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002835 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002836 return Visit(T->getElementType());
2837}
2838
2839bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002840 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002841 return Visit(T->getElementType());
2842}
2843
2844bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002845 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002846 return Visit(T->getElementType());
2847}
2848
2849bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002850 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002851 return Visit(T->getElementType());
2852}
2853
2854bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002855 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002856 return Visit(T->getElementType());
2857}
2858
2859bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
2860 return Visit(T->getElementType());
2861}
2862
2863bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
2864 return Visit(T->getElementType());
2865}
2866
2867bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
2868 const FunctionProtoType* T) {
2869 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002870 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002871 A != AEnd; ++A) {
2872 if (Visit(*A))
2873 return true;
2874 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002875
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002876 return Visit(T->getResultType());
2877}
2878
2879bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
2880 const FunctionNoProtoType* T) {
2881 return Visit(T->getResultType());
2882}
2883
2884bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
2885 const UnresolvedUsingType*) {
2886 return false;
2887}
2888
2889bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
2890 return false;
2891}
2892
2893bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
2894 return Visit(T->getUnderlyingType());
2895}
2896
2897bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
2898 return false;
2899}
2900
Richard Smith34b41d92011-02-20 03:19:35 +00002901bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
2902 return Visit(T->getDeducedType());
2903}
2904
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002905bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
2906 return VisitTagDecl(T->getDecl());
2907}
2908
2909bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
2910 return VisitTagDecl(T->getDecl());
2911}
2912
2913bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
2914 const TemplateTypeParmType*) {
2915 return false;
2916}
2917
Douglas Gregorc3069d62011-01-14 02:55:32 +00002918bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
2919 const SubstTemplateTypeParmPackType *) {
2920 return false;
2921}
2922
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002923bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
2924 const TemplateSpecializationType*) {
2925 return false;
2926}
2927
2928bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
2929 const InjectedClassNameType* T) {
2930 return VisitTagDecl(T->getDecl());
2931}
2932
2933bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
2934 const DependentNameType* T) {
2935 return VisitNestedNameSpecifier(T->getQualifier());
2936}
2937
2938bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
2939 const DependentTemplateSpecializationType* T) {
2940 return VisitNestedNameSpecifier(T->getQualifier());
2941}
2942
Douglas Gregor7536dd52010-12-20 02:24:11 +00002943bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
2944 const PackExpansionType* T) {
2945 return Visit(T->getPattern());
2946}
2947
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002948bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
2949 return false;
2950}
2951
2952bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
2953 const ObjCInterfaceType *) {
2954 return false;
2955}
2956
2957bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
2958 const ObjCObjectPointerType *) {
2959 return false;
2960}
2961
2962bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
2963 if (Tag->getDeclContext()->isFunctionOrMethod()) {
2964 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
2965 << S.Context.getTypeDeclType(Tag) << SR;
2966 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002967 }
2968
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002969 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) {
2970 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
2971 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
2972 return true;
2973 }
2974
2975 return false;
2976}
2977
2978bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
2979 NestedNameSpecifier *NNS) {
2980 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
2981 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002982
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002983 switch (NNS->getKind()) {
2984 case NestedNameSpecifier::Identifier:
2985 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00002986 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002987 case NestedNameSpecifier::Global:
2988 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002989
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002990 case NestedNameSpecifier::TypeSpec:
2991 case NestedNameSpecifier::TypeSpecWithTemplate:
2992 return Visit(QualType(NNS->getAsType(), 0));
2993 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00002994 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002995}
2996
2997
Douglas Gregorc15cb382009-02-09 23:23:08 +00002998/// \brief Check a template argument against its corresponding
2999/// template type parameter.
3000///
3001/// This routine implements the semantics of C++ [temp.arg.type]. It
3002/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003003bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003004 TypeSourceInfo *ArgInfo) {
3005 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003006 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003007 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003008
3009 if (Arg->isVariablyModifiedType()) {
3010 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003011 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003012 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003013 }
3014
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003015 // C++03 [temp.arg.type]p2:
3016 // A local type, a type with no linkage, an unnamed type or a type
3017 // compounded from any of these types shall not be used as a
3018 // template-argument for a template type-parameter.
3019 //
3020 // C++0x allows these, and even in C++03 we allow them as an extension with
3021 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00003022 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003023 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3024 (void)Finder.Visit(Context.getCanonicalType(Arg));
3025 }
3026
Douglas Gregorc15cb382009-02-09 23:23:08 +00003027 return false;
3028}
3029
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003030/// \brief Checks whether the given template argument is the address
3031/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003032static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003033CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3034 NonTypeTemplateParmDecl *Param,
3035 QualType ParamType,
3036 Expr *ArgIn,
3037 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003038 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003039 Expr *Arg = ArgIn;
3040 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003041
3042 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003043 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003044 Arg = Cast->getSubExpr();
3045
3046 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003047 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003048 // A template-argument for a non-type, non-template
3049 // template-parameter shall be one of: [...]
3050 //
3051 // -- the address of an object or function with external
3052 // linkage, including function templates and function
3053 // template-ids but excluding non-static class members,
3054 // expressed as & id-expression where the & is optional if
3055 // the name refers to a function or array, or if the
3056 // corresponding template-parameter is a reference; or
3057 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003058
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003059 // In C++98/03 mode, give an extension warning on any extra parentheses.
3060 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3061 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003062 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003063 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003064 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003065 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003066 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003067 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003068 }
3069
3070 Arg = Parens->getSubExpr();
3071 }
3072
Douglas Gregorb7a09262010-04-01 18:32:35 +00003073 bool AddressTaken = false;
3074 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003075 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003076 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003077 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00003078 AddressTaken = true;
3079 AddrOpLoc = UnOp->getOperatorLoc();
3080 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003081 } else
3082 DRE = dyn_cast<DeclRefExpr>(Arg);
3083
Douglas Gregorb7a09262010-04-01 18:32:35 +00003084 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003085 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3086 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003087 S.Diag(Param->getLocation(), diag::note_template_param_here);
3088 return true;
3089 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003090
3091 // Stop checking the precise nature of the argument if it is value dependent,
3092 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003093 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003094 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003095 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003096 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003097
Douglas Gregorb7a09262010-04-01 18:32:35 +00003098 if (!isa<ValueDecl>(DRE->getDecl())) {
3099 S.Diag(Arg->getSourceRange().getBegin(),
3100 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003101 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003102 S.Diag(Param->getLocation(), diag::note_template_param_here);
3103 return true;
3104 }
3105
3106 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003107
3108 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003109 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3110 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003111 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003112 S.Diag(Param->getLocation(), diag::note_template_param_here);
3113 return true;
3114 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003115
3116 // Cannot refer to non-static member functions
3117 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003118 if (!Method->isStatic()) {
3119 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003120 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003121 S.Diag(Param->getLocation(), diag::note_template_param_here);
3122 return true;
3123 }
Mike Stump1eb44332009-09-09 15:08:12 +00003124
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003125 // Functions must have external linkage.
3126 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003127 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003128 S.Diag(Arg->getSourceRange().getBegin(),
3129 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003130 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003131 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003132 << true;
3133 return true;
3134 }
3135
3136 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003137 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003138
Douglas Gregorb7a09262010-04-01 18:32:35 +00003139 // If the template parameter has pointer type, the function decays.
3140 if (ParamType->isPointerType() && !AddressTaken)
3141 ArgType = S.Context.getPointerType(Func->getType());
3142 else if (AddressTaken && ParamType->isReferenceType()) {
3143 // If we originally had an address-of operator, but the
3144 // parameter has reference type, complain and (if things look
3145 // like they will work) drop the address-of operator.
3146 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3147 ParamType.getNonReferenceType())) {
3148 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3149 << ParamType;
3150 S.Diag(Param->getLocation(), diag::note_template_param_here);
3151 return true;
3152 }
3153
3154 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3155 << ParamType
3156 << FixItHint::CreateRemoval(AddrOpLoc);
3157 S.Diag(Param->getLocation(), diag::note_template_param_here);
3158
3159 ArgType = Func->getType();
3160 }
3161 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003162 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003163 S.Diag(Arg->getSourceRange().getBegin(),
3164 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003165 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003166 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003167 << true;
3168 return true;
3169 }
3170
Douglas Gregorb7a09262010-04-01 18:32:35 +00003171 // A value of reference type is not an object.
3172 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003173 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003174 diag::err_template_arg_reference_var)
3175 << Var->getType() << Arg->getSourceRange();
3176 S.Diag(Param->getLocation(), diag::note_template_param_here);
3177 return true;
3178 }
3179
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003180 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003181 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003182
3183 // If the template parameter has pointer type, we must have taken
3184 // the address of this object.
3185 if (ParamType->isReferenceType()) {
3186 if (AddressTaken) {
3187 // If we originally had an address-of operator, but the
3188 // parameter has reference type, complain and (if things look
3189 // like they will work) drop the address-of operator.
3190 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3191 ParamType.getNonReferenceType())) {
3192 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3193 << ParamType;
3194 S.Diag(Param->getLocation(), diag::note_template_param_here);
3195 return true;
3196 }
3197
3198 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3199 << ParamType
3200 << FixItHint::CreateRemoval(AddrOpLoc);
3201 S.Diag(Param->getLocation(), diag::note_template_param_here);
3202
3203 ArgType = Var->getType();
3204 }
3205 } else if (!AddressTaken && ParamType->isPointerType()) {
3206 if (Var->getType()->isArrayType()) {
3207 // Array-to-pointer decay.
3208 ArgType = S.Context.getArrayDecayedType(Var->getType());
3209 } else {
3210 // If the template parameter has pointer type but the address of
3211 // this object was not taken, complain and (possibly) recover by
3212 // taking the address of the entity.
3213 ArgType = S.Context.getPointerType(Var->getType());
3214 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3215 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3216 << ParamType;
3217 S.Diag(Param->getLocation(), diag::note_template_param_here);
3218 return true;
3219 }
3220
3221 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3222 << ParamType
3223 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3224
3225 S.Diag(Param->getLocation(), diag::note_template_param_here);
3226 }
3227 }
3228 } else {
3229 // We found something else, but we don't know specifically what it is.
3230 S.Diag(Arg->getSourceRange().getBegin(),
3231 diag::err_template_arg_not_object_or_func)
3232 << Arg->getSourceRange();
3233 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3234 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003235 }
Mike Stump1eb44332009-09-09 15:08:12 +00003236
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003237 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003238 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003239 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003240 // For pointer-to-object types, qualification conversions are
3241 // permitted.
3242 } else {
3243 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3244 if (!ParamRef->getPointeeType()->isFunctionType()) {
3245 // C++ [temp.arg.nontype]p5b3:
3246 // For a non-type template-parameter of type reference to
3247 // object, no conversions apply. The type referred to by the
3248 // reference may be more cv-qualified than the (otherwise
3249 // identical) type of the template- argument. The
3250 // template-parameter is bound directly to the
3251 // template-argument, which shall be an lvalue.
3252
3253 // FIXME: Other qualifiers?
3254 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3255 unsigned ArgQuals = ArgType.getCVRQualifiers();
3256
3257 if ((ParamQuals | ArgQuals) != ParamQuals) {
3258 S.Diag(Arg->getSourceRange().getBegin(),
3259 diag::err_template_arg_ref_bind_ignores_quals)
3260 << ParamType << Arg->getType()
3261 << Arg->getSourceRange();
3262 S.Diag(Param->getLocation(), diag::note_template_param_here);
3263 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003264 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003265 }
3266 }
3267
3268 // At this point, the template argument refers to an object or
3269 // function with external linkage. We now need to check whether the
3270 // argument and parameter types are compatible.
3271 if (!S.Context.hasSameUnqualifiedType(ArgType,
3272 ParamType.getNonReferenceType())) {
3273 // We can't perform this conversion or binding.
3274 if (ParamType->isReferenceType())
3275 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3276 << ParamType << Arg->getType() << Arg->getSourceRange();
3277 else
3278 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3279 << Arg->getType() << ParamType << Arg->getSourceRange();
3280 S.Diag(Param->getLocation(), diag::note_template_param_here);
3281 return true;
3282 }
3283 }
3284
3285 // Create the template argument.
3286 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003287 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003288 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003289}
3290
3291/// \brief Checks whether the given template argument is a pointer to
3292/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003293bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003294 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003295 bool Invalid = false;
3296
3297 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003298 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003299 Arg = Cast->getSubExpr();
3300
3301 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003302 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003303 // A template-argument for a non-type, non-template
3304 // template-parameter shall be one of: [...]
3305 //
3306 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003307 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003308
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003309 // In C++98/03 mode, give an extension warning on any extra parentheses.
3310 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3311 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003312 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003313 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003314 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003315 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003316 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003317 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003318 }
3319
3320 Arg = Parens->getSubExpr();
3321 }
3322
Douglas Gregorcaddba02009-11-12 18:38:13 +00003323 // A pointer-to-member constant written &Class::member.
3324 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003325 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003326 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3327 if (DRE && !DRE->getQualifier())
3328 DRE = 0;
3329 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003330 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003331 // A constant of pointer-to-member type.
3332 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3333 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3334 if (VD->getType()->isMemberPointerType()) {
3335 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003336 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003337 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3338 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003339 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003340 else
3341 Converted = TemplateArgument(VD->getCanonicalDecl());
3342 return Invalid;
3343 }
3344 }
3345 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003346
Douglas Gregorcaddba02009-11-12 18:38:13 +00003347 DRE = 0;
3348 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003349
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003350 if (!DRE)
3351 return Diag(Arg->getSourceRange().getBegin(),
3352 diag::err_template_arg_not_pointer_to_member_form)
3353 << Arg->getSourceRange();
3354
3355 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3356 assert((isa<FieldDecl>(DRE->getDecl()) ||
3357 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3358 "Only non-static member pointers can make it here");
3359
3360 // Okay: this is the address of a non-static member, and therefore
3361 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003362 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003363 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003364 else
3365 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003366 return Invalid;
3367 }
3368
3369 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003370 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003371 diag::err_template_arg_not_pointer_to_member_form)
3372 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003373 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003374 diag::note_template_arg_refers_here);
3375 return true;
3376}
3377
Douglas Gregorc15cb382009-02-09 23:23:08 +00003378/// \brief Check a template argument against its corresponding
3379/// non-type template parameter.
3380///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003381/// This routine implements the semantics of C++ [temp.arg.nontype].
3382/// It returns true if an error occurred, and false otherwise. \p
3383/// InstantiatedParamType is the type of the non-type template
3384/// parameter after it has been instantiated.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003385///
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003386/// If no error was detected, Converted receives the converted template argument.
Douglas Gregorc15cb382009-02-09 23:23:08 +00003387bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
Mike Stump1eb44332009-09-09 15:08:12 +00003388 QualType InstantiatedParamType, Expr *&Arg,
Douglas Gregor02024a92010-03-28 02:42:43 +00003389 TemplateArgument &Converted,
3390 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003391 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3392
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003393 // If either the parameter has a dependent type or the argument is
3394 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003395 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3396 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003397 Converted = TemplateArgument(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003398 return false;
Douglas Gregor40808ce2009-03-09 23:48:35 +00003399 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003400
3401 // C++ [temp.arg.nontype]p5:
3402 // The following conversions are performed on each expression used
3403 // as a non-type template-argument. If a non-type
3404 // template-argument cannot be converted to the type of the
3405 // corresponding template-parameter then the program is
3406 // ill-formed.
3407 //
3408 // -- for a non-type template-parameter of integral or
3409 // enumeration type, integral promotions (4.5) and integral
3410 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003411 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003412 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003413 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003414 // C++ [temp.arg.nontype]p1:
3415 // A template-argument for a non-type, non-template
3416 // template-parameter shall be one of:
3417 //
3418 // -- an integral constant-expression of integral or enumeration
3419 // type; or
3420 // -- the name of a non-type template-parameter; or
3421 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003422 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003423 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003424 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003425 diag::err_template_arg_not_integral_or_enumeral)
3426 << ArgType << Arg->getSourceRange();
3427 Diag(Param->getLocation(), diag::note_template_param_here);
3428 return true;
3429 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003430 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003431 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3432 << ArgType << Arg->getSourceRange();
3433 return true;
3434 }
3435
Douglas Gregor02024a92010-03-28 02:42:43 +00003436 // From here on out, all we care about are the unqualified forms
3437 // of the parameter and argument types.
3438 ParamType = ParamType.getUnqualifiedType();
3439 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003440
3441 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003442 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003443 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003444 } else if (CTAK == CTAK_Deduced) {
3445 // C++ [temp.deduct.type]p17:
3446 // If, in the declaration of a function template with a non-type
3447 // template-parameter, the non-type template- parameter is used
3448 // in an expression in the function parameter-list and, if the
3449 // corresponding template-argument is deduced, the
3450 // template-argument type shall match the type of the
3451 // template-parameter exactly, except that a template-argument
3452 // deduced from an array bound may be of any integral type.
3453 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3454 << ArgType << ParamType;
3455 Diag(Param->getLocation(), diag::note_template_param_here);
John McCalldaa8e4e2010-11-15 09:13:47 +00003456 return true;
3457 } else if (ParamType->isBooleanType()) {
3458 // This is an integral-to-boolean conversion.
3459 ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003460 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3461 !ParamType->isEnumeralType()) {
3462 // This is an integral promotion or conversion.
John McCall2de56d12010-08-25 11:45:40 +00003463 ImpCastExprToType(Arg, ParamType, CK_IntegralCast);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003464 } else {
3465 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003466 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003467 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003468 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003469 Diag(Param->getLocation(), diag::note_template_param_here);
3470 return true;
3471 }
3472
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003473 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003474 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003475 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003476
3477 if (!Arg->isValueDependent()) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003478 llvm::APSInt OldValue = Value;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003479
3480 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003481 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003482 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003483 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003484 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003485 Value.setIsSigned(IntegerType->isSignedIntegerType());
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003486
3487 // Complain if an unsigned parameter received a negative value.
3488 if (IntegerType->isUnsignedIntegerType()
3489 && (OldValue.isSigned() && OldValue.isNegative())) {
3490 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3491 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3492 << Arg->getSourceRange();
3493 Diag(Param->getLocation(), diag::note_template_param_here);
3494 }
3495
3496 // Complain if we overflowed the template parameter's type.
3497 unsigned RequiredBits;
3498 if (IntegerType->isUnsignedIntegerType())
3499 RequiredBits = OldValue.getActiveBits();
3500 else if (OldValue.isUnsigned())
3501 RequiredBits = OldValue.getActiveBits() + 1;
3502 else
3503 RequiredBits = OldValue.getMinSignedBits();
3504 if (RequiredBits > AllowedBits) {
3505 Diag(Arg->getSourceRange().getBegin(),
3506 diag::warn_template_arg_too_large)
3507 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3508 << Arg->getSourceRange();
3509 Diag(Param->getLocation(), diag::note_template_param_here);
3510 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003511 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003512
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003513 // Add the value of this argument to the list of converted
3514 // arguments. We use the bitwidth and signedness of the template
3515 // parameter.
3516 if (Arg->isValueDependent()) {
3517 // The argument is value-dependent. Create a new
3518 // TemplateArgument with the converted expression.
3519 Converted = TemplateArgument(Arg);
3520 return false;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003521 }
3522
John McCall833ca992009-10-29 08:12:44 +00003523 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003524 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003525 : IntegerType);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003526 return false;
3527 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003528
John McCall6bb80172010-03-30 21:47:33 +00003529 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3530
Douglas Gregorb7a09262010-04-01 18:32:35 +00003531 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3532 // from a template argument of type std::nullptr_t to a non-type
3533 // template parameter of type pointer to object, pointer to
3534 // function, or pointer-to-member, respectively.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003535 if (ArgType->isNullPtrType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003536 (ParamType->isPointerType() || ParamType->isMemberPointerType())) {
3537 Converted = TemplateArgument((NamedDecl *)0);
3538 return false;
3539 }
3540
Douglas Gregorb86b0572009-02-11 01:18:59 +00003541 // Handle pointer-to-function, reference-to-function, and
3542 // pointer-to-member-function all in (roughly) the same way.
3543 if (// -- For a non-type template-parameter of type pointer to
3544 // function, only the function-to-pointer conversion (4.3) is
3545 // applied. If the template-argument represents a set of
3546 // overloaded functions (or a pointer to such), the matching
3547 // function is selected from the set (13.4).
3548 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003549 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003550 // -- For a non-type template-parameter of type reference to
3551 // function, no conversions apply. If the template-argument
3552 // represents a set of overloaded functions, the matching
3553 // function is selected from the set (13.4).
3554 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003555 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003556 // -- For a non-type template-parameter of type pointer to
3557 // member function, no conversions apply. If the
3558 // template-argument represents a set of overloaded member
3559 // functions, the matching member function is selected from
3560 // the set (13.4).
3561 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003562 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003563 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003564
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003565 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003566 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003567 true,
3568 FoundResult)) {
3569 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3570 return true;
3571
3572 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3573 ArgType = Arg->getType();
3574 } else
Douglas Gregor48f3bb92009-02-18 21:56:37 +00003575 return true;
Douglas Gregora35284b2009-02-11 00:19:33 +00003576 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003577
Douglas Gregorb7a09262010-04-01 18:32:35 +00003578 if (!ParamType->isMemberPointerType())
3579 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003580 ParamType,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003581 Arg, Converted);
3582
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003583 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3584 false)) {
John McCall2de56d12010-08-25 11:45:40 +00003585 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregorb7a09262010-04-01 18:32:35 +00003586 } else if (!Context.hasSameUnqualifiedType(ArgType,
3587 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003588 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003589 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003590 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003591 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003592 Diag(Param->getLocation(), diag::note_template_param_here);
3593 return true;
3594 }
Mike Stump1eb44332009-09-09 15:08:12 +00003595
Douglas Gregorb7a09262010-04-01 18:32:35 +00003596 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregora35284b2009-02-11 00:19:33 +00003597 }
3598
Chris Lattnerfe90de72009-02-20 21:37:53 +00003599 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003600 // -- for a non-type template-parameter of type pointer to
3601 // object, qualification conversions (4.4) and the
3602 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003603 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003604 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003605 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003606
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003607 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003608 ParamType,
3609 Arg, Converted);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003610 }
Mike Stump1eb44332009-09-09 15:08:12 +00003611
Ted Kremenek6217b802009-07-29 21:53:49 +00003612 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003613 // -- For a non-type template-parameter of type reference to
3614 // object, no conversions apply. The type referred to by the
3615 // reference may be more cv-qualified than the (otherwise
3616 // identical) type of the template-argument. The
3617 // template-parameter is bound directly to the
3618 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003619 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003620 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003621
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003622 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003623 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3624 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003625 true,
3626 FoundResult)) {
3627 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3628 return true;
3629
3630 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3631 ArgType = Arg->getType();
3632 } else
Douglas Gregorb7a09262010-04-01 18:32:35 +00003633 return true;
Douglas Gregorb86b0572009-02-11 01:18:59 +00003634 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003635
3636 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003637 ParamType,
3638 Arg, Converted);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003639 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003640
3641 // -- For a non-type template-parameter of type pointer to data
3642 // member, qualification conversions (4.4) are applied.
3643 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3644
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003645 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003646 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003647 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John McCall2de56d12010-08-25 11:45:40 +00003648 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregor658bbb52009-02-11 16:16:59 +00003649 } else {
3650 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003651 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003652 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003653 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003654 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00003655 return true;
Douglas Gregor658bbb52009-02-11 16:16:59 +00003656 }
3657
Douglas Gregorcaddba02009-11-12 18:38:13 +00003658 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003659}
3660
3661/// \brief Check a template argument against its corresponding
3662/// template template parameter.
3663///
3664/// This routine implements the semantics of C++ [temp.arg.template].
3665/// It returns true if an error occurred, and false otherwise.
3666bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003667 const TemplateArgumentLoc &Arg) {
3668 TemplateName Name = Arg.getArgument().getAsTemplate();
3669 TemplateDecl *Template = Name.getAsTemplateDecl();
3670 if (!Template) {
3671 // Any dependent template name is fine.
3672 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3673 return false;
3674 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003675
3676 // C++ [temp.arg.template]p1:
3677 // A template-argument for a template template-parameter shall be
3678 // the name of a class template, expressed as id-expression. Only
3679 // primary class templates are considered when matching the
3680 // template template argument with the corresponding parameter;
3681 // partial specializations are not considered even if their
3682 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003683 //
3684 // Note that we also allow template template parameters here, which
3685 // will happen when we are dealing with, e.g., class template
3686 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003687 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003688 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003689 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003690 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003691 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003692 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003693 << Template;
3694 }
3695
3696 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
3697 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003698 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003699 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00003700 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00003701}
3702
Douglas Gregor02024a92010-03-28 02:42:43 +00003703/// \brief Given a non-type template argument that refers to a
3704/// declaration and the type of its corresponding non-type template
3705/// parameter, produce an expression that properly refers to that
3706/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003707ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003708Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
3709 QualType ParamType,
3710 SourceLocation Loc) {
3711 assert(Arg.getKind() == TemplateArgument::Declaration &&
3712 "Only declaration template arguments permitted here");
3713 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
3714
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003715 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00003716 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
3717 // If the value is a class member, we might have a pointer-to-member.
3718 // Determine whether the non-type template template parameter is of
3719 // pointer-to-member type. If so, we need to build an appropriate
3720 // expression for a pointer-to-member, since a "normal" DeclRefExpr
3721 // would refer to the member itself.
3722 if (ParamType->isMemberPointerType()) {
3723 QualType ClassType
3724 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
3725 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00003726 = NestedNameSpecifier::Create(Context, 0, false,
3727 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00003728 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00003729 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00003730
3731 // The actual value-ness of this is unimportant, but for
3732 // internal consistency's sake, references to instance methods
3733 // are r-values.
3734 ExprValueKind VK = VK_LValue;
3735 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
3736 VK = VK_RValue;
3737
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003738 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00003739 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00003740 VK,
John McCallf89e55a2010-11-18 06:31:45 +00003741 Loc,
3742 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00003743 if (RefExpr.isInvalid())
3744 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003745
John McCall2de56d12010-08-25 11:45:40 +00003746 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003747
Douglas Gregorc0c83002010-04-30 21:46:38 +00003748 // We might need to perform a trailing qualification conversion, since
3749 // the element type on the parameter could be more qualified than the
3750 // element type in the expression we constructed.
3751 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003752 ParamType.getUnqualifiedType(), false)) {
Douglas Gregorc0c83002010-04-30 21:46:38 +00003753 Expr *RefE = RefExpr.takeAs<Expr>();
John McCall2de56d12010-08-25 11:45:40 +00003754 ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CK_NoOp);
Douglas Gregorc0c83002010-04-30 21:46:38 +00003755 RefExpr = Owned(RefE);
3756 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003757
Douglas Gregor02024a92010-03-28 02:42:43 +00003758 assert(!RefExpr.isInvalid() &&
3759 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00003760 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00003761 return move(RefExpr);
3762 }
3763 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003764
Douglas Gregor02024a92010-03-28 02:42:43 +00003765 QualType T = VD->getType().getNonReferenceType();
3766 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003767 // When the non-type template parameter is a pointer, take the
3768 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00003769 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003770 if (RefExpr.isInvalid())
3771 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003772
3773 if (T->isFunctionType() || T->isArrayType()) {
3774 // Decay functions and arrays.
3775 Expr *RefE = (Expr *)RefExpr.get();
3776 DefaultFunctionArrayConversion(RefE);
3777 if (RefE != RefExpr.get()) {
3778 RefExpr.release();
3779 RefExpr = Owned(RefE);
3780 }
3781
3782 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00003783 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003784
Douglas Gregorb7a09262010-04-01 18:32:35 +00003785 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00003786 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00003787 }
3788
John McCallf89e55a2010-11-18 06:31:45 +00003789 ExprValueKind VK = VK_RValue;
3790
Douglas Gregor02024a92010-03-28 02:42:43 +00003791 // If the non-type template parameter has reference type, qualify the
3792 // resulting declaration reference with the extra qualifiers on the
3793 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00003794 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
3795 VK = VK_LValue;
3796 T = Context.getQualifiedType(T,
3797 TargetRef->getPointeeType().getQualifiers());
3798 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003799
John McCallf89e55a2010-11-18 06:31:45 +00003800 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003801}
3802
3803/// \brief Construct a new expression that refers to the given
3804/// integral template argument with the given source-location
3805/// information.
3806///
3807/// This routine takes care of the mapping from an integral template
3808/// argument (which may have any integral type) to the appropriate
3809/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003810ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003811Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
3812 SourceLocation Loc) {
3813 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00003814 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00003815 QualType T = Arg.getIntegralType();
3816 if (T->isCharType() || T->isWideCharType())
3817 return Owned(new (Context) CharacterLiteral(
3818 Arg.getAsIntegral()->getZExtValue(),
3819 T->isWideCharType(),
3820 T,
3821 Loc));
3822 if (T->isBooleanType())
3823 return Owned(new (Context) CXXBoolLiteralExpr(
3824 Arg.getAsIntegral()->getBoolValue(),
3825 T,
3826 Loc));
3827
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003828 QualType BT;
3829 if (const EnumType *ET = T->getAs<EnumType>())
3830 BT = ET->getDecl()->getPromotionType();
3831 else
3832 BT = T;
3833
3834 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00003835 if (T->isEnumeralType()) {
3836 // FIXME: This is a hack. We need a better way to handle substituted
3837 // non-type template parameters.
3838 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast,
3839 E, 0,
3840 Context.getTrivialTypeSourceInfo(T, Loc),
3841 Loc, Loc);
3842 }
3843
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003844 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00003845}
3846
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003847/// \brief Match two template parameters within template parameter lists.
3848static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
3849 bool Complain,
3850 Sema::TemplateParameterListEqualKind Kind,
3851 SourceLocation TemplateArgLoc) {
3852 // Check the actual kind (type, non-type, template).
3853 if (Old->getKind() != New->getKind()) {
3854 if (Complain) {
3855 unsigned NextDiag = diag::err_template_param_different_kind;
3856 if (TemplateArgLoc.isValid()) {
3857 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3858 NextDiag = diag::note_template_param_different_kind;
3859 }
3860 S.Diag(New->getLocation(), NextDiag)
3861 << (Kind != Sema::TPL_TemplateMatch);
3862 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
3863 << (Kind != Sema::TPL_TemplateMatch);
3864 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003865
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003866 return false;
3867 }
3868
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003869 // Check that both are parameter packs are neither are parameter packs.
3870 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00003871 // template template parameter, the template template parameter can have
3872 // a parameter pack where the template template argument does not.
3873 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
3874 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3875 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003876 if (Complain) {
3877 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
3878 if (TemplateArgLoc.isValid()) {
3879 S.Diag(TemplateArgLoc,
3880 diag::err_template_arg_template_params_mismatch);
3881 NextDiag = diag::note_template_parameter_pack_non_pack;
3882 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003883
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003884 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
3885 : isa<NonTypeTemplateParmDecl>(New)? 1
3886 : 2;
3887 S.Diag(New->getLocation(), NextDiag)
3888 << ParamKind << New->isParameterPack();
3889 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
3890 << ParamKind << Old->isParameterPack();
3891 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003892
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003893 return false;
3894 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003895
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003896 // For non-type template parameters, check the type of the parameter.
3897 if (NonTypeTemplateParmDecl *OldNTTP
3898 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
3899 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003900
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003901 // If we are matching a template template argument to a template
3902 // template parameter and one of the non-type template parameter types
3903 // is dependent, then we must wait until template instantiation time
3904 // to actually compare the arguments.
3905 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3906 (OldNTTP->getType()->isDependentType() ||
3907 NewNTTP->getType()->isDependentType()))
3908 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003909
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003910 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
3911 if (Complain) {
3912 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
3913 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003914 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003915 diag::err_template_arg_template_params_mismatch);
3916 NextDiag = diag::note_template_nontype_parm_different_type;
3917 }
3918 S.Diag(NewNTTP->getLocation(), NextDiag)
3919 << NewNTTP->getType()
3920 << (Kind != Sema::TPL_TemplateMatch);
3921 S.Diag(OldNTTP->getLocation(),
3922 diag::note_template_nontype_parm_prev_declaration)
3923 << OldNTTP->getType();
3924 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003925
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003926 return false;
3927 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003928
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003929 return true;
3930 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003931
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003932 // For template template parameters, check the template parameter types.
3933 // The template parameter lists of template template
3934 // parameters must agree.
3935 if (TemplateTemplateParmDecl *OldTTP
3936 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003937 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003938 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
3939 OldTTP->getTemplateParameters(),
3940 Complain,
3941 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003942 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003943 : Kind),
3944 TemplateArgLoc);
3945 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003946
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003947 return true;
3948}
Douglas Gregor02024a92010-03-28 02:42:43 +00003949
Douglas Gregora0347822011-01-13 00:08:50 +00003950/// \brief Diagnose a known arity mismatch when comparing template argument
3951/// lists.
3952static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003953void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00003954 TemplateParameterList *New,
3955 TemplateParameterList *Old,
3956 Sema::TemplateParameterListEqualKind Kind,
3957 SourceLocation TemplateArgLoc) {
3958 unsigned NextDiag = diag::err_template_param_list_different_arity;
3959 if (TemplateArgLoc.isValid()) {
3960 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3961 NextDiag = diag::note_template_param_list_different_arity;
3962 }
3963 S.Diag(New->getTemplateLoc(), NextDiag)
3964 << (New->size() > Old->size())
3965 << (Kind != Sema::TPL_TemplateMatch)
3966 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
3967 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
3968 << (Kind != Sema::TPL_TemplateMatch)
3969 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
3970}
3971
Douglas Gregorddc29e12009-02-06 22:42:48 +00003972/// \brief Determine whether the given template parameter lists are
3973/// equivalent.
3974///
Mike Stump1eb44332009-09-09 15:08:12 +00003975/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00003976/// source code as part of a new template declaration.
3977///
3978/// \param Old The old template parameter list, typically found via
3979/// name lookup of the template declared with this template parameter
3980/// list.
3981///
3982/// \param Complain If true, this routine will produce a diagnostic if
3983/// the template parameter lists are not equivalent.
3984///
Douglas Gregorfb898e12009-11-12 16:20:59 +00003985/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00003986///
3987/// \param TemplateArgLoc If this source location is valid, then we
3988/// are actually checking the template parameter list of a template
3989/// argument (New) against the template parameter list of its
3990/// corresponding template template parameter (Old). We produce
3991/// slightly different diagnostics in this scenario.
3992///
Douglas Gregorddc29e12009-02-06 22:42:48 +00003993/// \returns True if the template parameter lists are equal, false
3994/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003995bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00003996Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
3997 TemplateParameterList *Old,
3998 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003999 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004000 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004001 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4002 if (Complain)
4003 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4004 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004005
4006 return false;
4007 }
4008
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004009 // C++0x [temp.arg.template]p3:
4010 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004011 // when each of the template parameters in the template-parameter-list of
4012 // the template-argument's corresponding class template or template alias
4013 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004014 // template-parameter-list of P. [...]
4015 TemplateParameterList::iterator NewParm = New->begin();
4016 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004017 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004018 OldParmEnd = Old->end();
4019 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004020 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4021 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004022 if (NewParm == NewParmEnd) {
4023 if (Complain)
4024 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4025 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004026
Douglas Gregora0347822011-01-13 00:08:50 +00004027 return false;
4028 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004029
Douglas Gregora0347822011-01-13 00:08:50 +00004030 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4031 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004032 return false;
4033
Douglas Gregora0347822011-01-13 00:08:50 +00004034 ++NewParm;
4035 continue;
4036 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004037
Douglas Gregora0347822011-01-13 00:08:50 +00004038 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004039 // [...] When P's template- parameter-list contains a template parameter
4040 // pack (14.5.3), the template parameter pack will match zero or more
4041 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004042 // template-parameter-list of A with the same type and form as the
4043 // template parameter pack in P (ignoring whether those template
4044 // parameters are template parameter packs).
4045 for (; NewParm != NewParmEnd; ++NewParm) {
4046 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4047 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004048 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004049 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004050 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004051
Douglas Gregora0347822011-01-13 00:08:50 +00004052 // Make sure we exhausted all of the arguments.
4053 if (NewParm != NewParmEnd) {
4054 if (Complain)
4055 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4056 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004057
Douglas Gregora0347822011-01-13 00:08:50 +00004058 return false;
4059 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004060
Douglas Gregorddc29e12009-02-06 22:42:48 +00004061 return true;
4062}
4063
4064/// \brief Check whether a template can be declared within this scope.
4065///
4066/// If the template declaration is valid in this scope, returns
4067/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004068bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004069Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004070 // Find the nearest enclosing declaration scope.
4071 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4072 (S->getFlags() & Scope::TemplateParamScope) != 0)
4073 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004074
Douglas Gregorddc29e12009-02-06 22:42:48 +00004075 // C++ [temp]p2:
4076 // A template-declaration can appear only as a namespace scope or
4077 // class scope declaration.
4078 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004079 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4080 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004081 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004082 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004083
Eli Friedman1503f772009-07-31 01:43:05 +00004084 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004085 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004086
4087 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4088 return false;
4089
Mike Stump1eb44332009-09-09 15:08:12 +00004090 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004091 diag::err_template_outside_namespace_or_class_scope)
4092 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004093}
Douglas Gregorcc636682009-02-17 23:15:12 +00004094
Douglas Gregord5cb8762009-10-07 00:13:32 +00004095/// \brief Determine what kind of template specialization the given declaration
4096/// is.
4097static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4098 if (!D)
4099 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004100
Douglas Gregorf6b11852009-10-08 15:14:33 +00004101 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4102 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004103 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4104 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004105 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4106 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004107
Douglas Gregord5cb8762009-10-07 00:13:32 +00004108 return TSK_Undeclared;
4109}
4110
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004111/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004112/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004113///
Douglas Gregor9302da62009-10-14 23:50:59 +00004114/// This routine determines whether a template specialization can be declared
4115/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004116///
4117/// \param S the semantic analysis object for which this check is being
4118/// performed.
4119///
4120/// \param Specialized the entity being specialized or instantiated, which
4121/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004122/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004123/// member class).
4124///
4125/// \param PrevDecl the previous declaration of this entity, if any.
4126///
4127/// \param Loc the location of the explicit specialization or instantiation of
4128/// this entity.
4129///
4130/// \param IsPartialSpecialization whether this is a partial specialization of
4131/// a class template.
4132///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004133/// \returns true if there was an error that we cannot recover from, false
4134/// otherwise.
4135static bool CheckTemplateSpecializationScope(Sema &S,
4136 NamedDecl *Specialized,
4137 NamedDecl *PrevDecl,
4138 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004139 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004140 // Keep these "kind" numbers in sync with the %select statements in the
4141 // various diagnostics emitted by this routine.
4142 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004143 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004144 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004145 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004146 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004147 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004148 EntityKind = 3;
4149 else if (isa<VarDecl>(Specialized))
4150 EntityKind = 4;
4151 else if (isa<RecordDecl>(Specialized))
4152 EntityKind = 5;
4153 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004154 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4155 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004156 return true;
4157 }
4158
Douglas Gregor88b70942009-02-25 22:02:03 +00004159 // C++ [temp.expl.spec]p2:
4160 // An explicit specialization shall be declared in the namespace
4161 // of which the template is a member, or, for member templates, in
4162 // the namespace of which the enclosing class or enclosing class
4163 // template is a member. An explicit specialization of a member
4164 // function, member class or static data member of a class
4165 // template shall be declared in the namespace of which the class
4166 // template is a member. Such a declaration may also be a
4167 // definition. If the declaration is not a definition, the
4168 // specialization may be defined later in the name- space in which
4169 // the explicit specialization was declared, or in a namespace
4170 // that encloses the one in which the explicit specialization was
4171 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004172 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004173 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004174 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004175 return true;
4176 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004177
Douglas Gregor0a407472009-10-07 17:30:37 +00004178 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
4179 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004180 << Specialized;
Douglas Gregor0a407472009-10-07 17:30:37 +00004181 return true;
4182 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004183
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004184 // C++ [temp.class.spec]p6:
4185 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004186 // in any namespace scope in which its definition may be defined (14.5.1
4187 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004188 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004189 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004190 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004191 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004192 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004193 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4194 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004195 // C++ [temp.exp.spec]p2:
4196 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004197 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004198 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004199 // An explicit specialization of a member function, member class or
4200 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004201 // namespace of which the class template is a member.
4202 //
4203 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004204 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004205 // the specialized template.
4206 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4207 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004208 bool IsCPlusPlus0xExtension
4209 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004210 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004211 S.Diag(Loc, IsCPlusPlus0xExtension
4212 ? diag::ext_template_spec_decl_out_of_scope_global
4213 : diag::err_template_spec_decl_out_of_scope_global)
4214 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004215 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004216 S.Diag(Loc, IsCPlusPlus0xExtension
4217 ? diag::ext_template_spec_decl_out_of_scope
4218 : diag::err_template_spec_decl_out_of_scope)
4219 << EntityKind << Specialized
4220 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004221
Douglas Gregor9302da62009-10-14 23:50:59 +00004222 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4223 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004224 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004225 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004226
4227 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004228 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004229 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004230 // specializations of function templates, static data members, and member
4231 // functions, so we skip the check here for those kinds of entities.
4232 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004233 // Should we refactor that check, so that it occurs later?
4234 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004235 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4236 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004237 if (isa<TranslationUnitDecl>(SpecializedContext))
4238 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4239 << EntityKind << Specialized;
4240 else if (isa<NamespaceDecl>(SpecializedContext))
4241 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4242 << EntityKind << Specialized
4243 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004244
Douglas Gregor9302da62009-10-14 23:50:59 +00004245 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004246 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004247
Douglas Gregord5cb8762009-10-07 00:13:32 +00004248 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004249
Douglas Gregor88b70942009-02-25 22:02:03 +00004250 return false;
4251}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004252
Douglas Gregorbacb9492011-01-03 21:13:47 +00004253/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4254/// that checks non-type template partial specialization arguments.
4255static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4256 NonTypeTemplateParmDecl *Param,
4257 const TemplateArgument *Args,
4258 unsigned NumArgs) {
4259 for (unsigned I = 0; I != NumArgs; ++I) {
4260 if (Args[I].getKind() == TemplateArgument::Pack) {
4261 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004262 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004263 Args[I].pack_size()))
4264 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004265
Douglas Gregore94866f2009-06-12 21:21:02 +00004266 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004267 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004268
Douglas Gregorbacb9492011-01-03 21:13:47 +00004269 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004270 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004271 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004272 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004273
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004274 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004275 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4276 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004277
4278 // Strip off any implicit casts we added as part of type checking.
4279 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4280 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004281
Douglas Gregore94866f2009-06-12 21:21:02 +00004282 // C++ [temp.class.spec]p8:
4283 // A non-type argument is non-specialized if it is the name of a
4284 // non-type parameter. All other non-type arguments are
4285 // specialized.
4286 //
4287 // Below, we check the two conditions that only apply to
4288 // specialized non-type arguments, so skip any non-specialized
4289 // arguments.
4290 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004291 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004292 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004293
Douglas Gregore94866f2009-06-12 21:21:02 +00004294 // C++ [temp.class.spec]p9:
4295 // Within the argument list of a class template partial
4296 // specialization, the following restrictions apply:
4297 // -- A partially specialized non-type argument expression
4298 // shall not involve a template parameter of the partial
4299 // specialization except when the argument expression is a
4300 // simple identifier.
4301 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004302 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004303 diag::err_dependent_non_type_arg_in_partial_spec)
4304 << ArgExpr->getSourceRange();
4305 return true;
4306 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004307
Douglas Gregore94866f2009-06-12 21:21:02 +00004308 // -- The type of a template parameter corresponding to a
4309 // specialized non-type argument shall not be dependent on a
4310 // parameter of the specialization.
4311 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004312 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004313 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4314 << Param->getType()
4315 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004316 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004317 return true;
4318 }
4319 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004320
Douglas Gregorbacb9492011-01-03 21:13:47 +00004321 return false;
4322}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004323
Douglas Gregorbacb9492011-01-03 21:13:47 +00004324/// \brief Check the non-type template arguments of a class template
4325/// partial specialization according to C++ [temp.class.spec]p9.
4326///
4327/// \param TemplateParams the template parameters of the primary class
4328/// template.
4329///
4330/// \param TemplateArg the template arguments of the class template
4331/// partial specialization.
4332///
4333/// \returns true if there was an error, false otherwise.
4334static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4335 TemplateParameterList *TemplateParams,
4336 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4337 const TemplateArgument *ArgList = TemplateArgs.data();
4338
4339 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4340 NonTypeTemplateParmDecl *Param
4341 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4342 if (!Param)
4343 continue;
4344
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004345 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004346 &ArgList[I], 1))
4347 return true;
4348 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004349
4350 return false;
4351}
4352
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004353/// \brief Retrieve the previous declaration of the given declaration.
4354static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4355 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4356 return VD->getPreviousDeclaration();
4357 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4358 return FD->getPreviousDeclaration();
4359 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4360 return TD->getPreviousDeclaration();
4361 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND))
4362 return TD->getPreviousDeclaration();
4363 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4364 return FTD->getPreviousDeclaration();
4365 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4366 return CTD->getPreviousDeclaration();
4367 return 0;
4368}
4369
John McCalld226f652010-08-21 09:40:31 +00004370DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004371Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4372 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004373 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004374 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004375 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004376 SourceLocation TemplateNameLoc,
4377 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004378 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004379 SourceLocation RAngleLoc,
4380 AttributeList *Attr,
4381 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004382 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004383
Douglas Gregorcc636682009-02-17 23:15:12 +00004384 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004385 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004386 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004387 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4388
4389 if (!ClassTemplate) {
4390 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004391 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004392 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4393 return true;
4394 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004395
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004396 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004397 bool isPartialSpecialization = false;
4398
Douglas Gregor88b70942009-02-25 22:02:03 +00004399 // Check the validity of the template headers that introduce this
4400 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004401 // FIXME: We probably shouldn't complain about these headers for
4402 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004403 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004404 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00004405 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
4406 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004407 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004408 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004409 isExplicitSpecialization,
4410 Invalid);
4411 if (Invalid)
4412 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004413
Abramo Bagnara9b934882010-06-12 08:15:14 +00004414 unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size();
4415 if (TemplateParams)
4416 --NumMatchedTemplateParamLists;
4417
Douglas Gregor05396e22009-08-25 17:23:04 +00004418 if (TemplateParams && TemplateParams->size() > 0) {
4419 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004420
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004421 if (TUK == TUK_Friend) {
4422 Diag(KWLoc, diag::err_partial_specialization_friend)
4423 << SourceRange(LAngleLoc, RAngleLoc);
4424 return true;
4425 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004426
Douglas Gregor05396e22009-08-25 17:23:04 +00004427 // C++ [temp.class.spec]p10:
4428 // The template parameter list of a specialization shall not
4429 // contain default template argument values.
4430 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4431 Decl *Param = TemplateParams->getParam(I);
4432 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4433 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004434 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004435 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004436 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004437 }
4438 } else if (NonTypeTemplateParmDecl *NTTP
4439 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4440 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004441 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004442 diag::err_default_arg_in_partial_spec)
4443 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004444 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004445 }
4446 } else {
4447 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004448 if (TTP->hasDefaultArgument()) {
4449 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004450 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004451 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004452 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004453 }
4454 }
4455 }
Douglas Gregora735b202009-10-13 14:39:41 +00004456 } else if (TemplateParams) {
4457 if (TUK == TUK_Friend)
4458 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004459 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004460 SourceRange(TemplateParams->getTemplateLoc(),
4461 TemplateParams->getRAngleLoc()))
4462 << SourceRange(LAngleLoc, RAngleLoc);
4463 else
4464 isExplicitSpecialization = true;
4465 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004466 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004467 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004468 isExplicitSpecialization = true;
4469 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004470
Douglas Gregorcc636682009-02-17 23:15:12 +00004471 // Check that the specialization uses the same tag kind as the
4472 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004473 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4474 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004475 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004476 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004477 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004478 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004479 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004480 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004481 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004482 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004483 diag::note_previous_use);
4484 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4485 }
4486
Douglas Gregor40808ce2009-03-09 23:48:35 +00004487 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004488 TemplateArgumentListInfo TemplateArgs;
4489 TemplateArgs.setLAngleLoc(LAngleLoc);
4490 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004491 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004492
Douglas Gregor925910d2011-01-03 20:35:03 +00004493 // Check for unexpanded parameter packs in any of the template arguments.
4494 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004495 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004496 UPPC_PartialSpecialization))
4497 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004498
Douglas Gregorcc636682009-02-17 23:15:12 +00004499 // Check that the template argument list is well-formed for this
4500 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004501 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004502 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4503 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004504 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004505
Douglas Gregor910f8002010-11-07 23:05:16 +00004506 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004507 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004508
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004509 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004510 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004511 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004512 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004513 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004514 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004515 return true;
4516
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004517 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004518 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004519 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004520 TemplateArgs.size())) {
4521 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4522 << ClassTemplate->getDeclName();
4523 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004524 }
4525 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004526
Douglas Gregorcc636682009-02-17 23:15:12 +00004527 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004528 ClassTemplateSpecializationDecl *PrevDecl = 0;
4529
4530 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004531 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004532 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004533 = ClassTemplate->findPartialSpecialization(Converted.data(),
4534 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004535 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004536 else
4537 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004538 = ClassTemplate->findSpecialization(Converted.data(),
4539 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004540
4541 ClassTemplateSpecializationDecl *Specialization = 0;
4542
Douglas Gregor88b70942009-02-25 22:02:03 +00004543 // Check whether we can declare a class template specialization in
4544 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004545 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004546 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4547 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004548 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004549 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004550
Douglas Gregorb88e8882009-07-30 17:40:51 +00004551 // The canonical type
4552 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004553 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004554 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004555 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004556 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004557 // arguments was referenced but not declared, or we're only
4558 // referencing this specialization as a friend, reuse that
Douglas Gregorcc636682009-02-17 23:15:12 +00004559 // declaration node as our own, updating its source location to
4560 // reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004561 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004562 Specialization->setLocation(TemplateNameLoc);
Douglas Gregorcc636682009-02-17 23:15:12 +00004563 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004564 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004565 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004566 // Build the canonical type that describes the converted template
4567 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004568 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4569 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004570 Converted.data(),
4571 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004572
4573 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004574 ClassTemplate->getInjectedClassNameSpecialization())) {
4575 // C++ [temp.class.spec]p9b3:
4576 //
4577 // -- The argument list of the specialization shall not be identical
4578 // to the implicit argument list of the primary template.
4579 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4580 << (TUK == TUK_Definition)
4581 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4582 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4583 ClassTemplate->getIdentifier(),
4584 TemplateNameLoc,
4585 Attr,
4586 TemplateParams,
4587 AS_none);
4588 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004589
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004590 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004591 ClassTemplatePartialSpecializationDecl *PrevPartial
4592 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004593 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004594 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004595 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004596 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004597 ClassTemplate->getDeclContext(),
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004598 TemplateNameLoc,
4599 TemplateParams,
4600 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004601 Converted.data(),
4602 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004603 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004604 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004605 PrevPartial,
4606 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004607 SetNestedNameSpecifier(Partial, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004608 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004609 Partial->setTemplateParameterListsInfo(Context,
4610 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004611 (TemplateParameterList**) TemplateParameterLists.release());
4612 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004613
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004614 if (!PrevPartial)
4615 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004616 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004617
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004618 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004619 // template specialization, make a note of that.
4620 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4621 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004622
Douglas Gregor031a5882009-06-13 00:26:55 +00004623 // Check that all of the template parameters of the class template
4624 // partial specialization are deducible from the template
4625 // arguments. If not, this class template partial specialization
4626 // will never be used.
4627 llvm::SmallVector<bool, 8> DeducibleParams;
4628 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004629 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004630 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004631 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004632 unsigned NumNonDeducible = 0;
4633 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4634 if (!DeducibleParams[I])
4635 ++NumNonDeducible;
4636
4637 if (NumNonDeducible) {
4638 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4639 << (NumNonDeducible > 1)
4640 << SourceRange(TemplateNameLoc, RAngleLoc);
4641 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4642 if (!DeducibleParams[I]) {
4643 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4644 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004645 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004646 diag::note_partial_spec_unused_parameter)
4647 << Param->getDeclName();
4648 else
Mike Stump1eb44332009-09-09 15:08:12 +00004649 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004650 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004651 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004652 }
4653 }
4654 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004655 } else {
4656 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004657 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004658 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004659 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004660 ClassTemplate->getDeclContext(),
4661 TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004662 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004663 Converted.data(),
4664 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004665 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004666 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004667 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004668 Specialization->setTemplateParameterListsInfo(Context,
4669 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004670 (TemplateParameterList**) TemplateParameterLists.release());
4671 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004672
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004673 if (!PrevDecl)
4674 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004675
4676 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004677 }
4678
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004679 // C++ [temp.expl.spec]p6:
4680 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004681 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004682 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004683 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004684 // use occurs; no diagnostic is required.
4685 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004686 bool Okay = false;
4687 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4688 // Is there any previous explicit specialization declaration?
4689 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4690 Okay = true;
4691 break;
4692 }
4693 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004694
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004695 if (!Okay) {
4696 SourceRange Range(TemplateNameLoc, RAngleLoc);
4697 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
4698 << Context.getTypeDeclType(Specialization) << Range;
4699
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004700 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004701 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004702 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004703 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004704 return true;
4705 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004706 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004707
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004708 // If this is not a friend, note that this is an explicit specialization.
4709 if (TUK != TUK_Friend)
4710 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004711
4712 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004713 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00004714 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004715 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00004716 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004717 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00004718 Diag(Def->getLocation(), diag::note_previous_definition);
4719 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00004720 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004721 }
4722 }
4723
John McCall7f1b9872010-12-18 03:30:47 +00004724 if (Attr)
4725 ProcessDeclAttributeList(S, Specialization, Attr);
4726
Douglas Gregorfc705b82009-02-26 22:19:44 +00004727 // Build the fully-sugared type for this class template
4728 // specialization as the user wrote in the specialization
4729 // itself. This means that we'll pretty-print the type retrieved
4730 // from the specialization's declaration the way that the user
4731 // actually wrote the specialization, rather than formatting the
4732 // name based on the "canonical" representation used to store the
4733 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00004734 TypeSourceInfo *WrittenTy
4735 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
4736 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004737 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004738 Specialization->setTypeAsWritten(WrittenTy);
Douglas Gregor7e9b57b2010-07-06 18:33:12 +00004739 if (TemplateParams)
4740 Specialization->setTemplateKeywordLoc(TemplateParams->getTemplateLoc());
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004741 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00004742 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00004743
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004744 // C++ [temp.expl.spec]p9:
4745 // A template explicit specialization is in the scope of the
4746 // namespace in which the template was defined.
4747 //
4748 // We actually implement this paragraph where we set the semantic
4749 // context (in the creation of the ClassTemplateSpecializationDecl),
4750 // but we also maintain the lexical context where the actual
4751 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00004752 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00004753
Douglas Gregorcc636682009-02-17 23:15:12 +00004754 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004755 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00004756 Specialization->startDefinition();
4757
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004758 if (TUK == TUK_Friend) {
4759 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
4760 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00004761 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004762 /*FIXME:*/KWLoc);
4763 Friend->setAccess(AS_public);
4764 CurContext->addDecl(Friend);
4765 } else {
4766 // Add the specialization into its lexical context, so that it can
4767 // be seen when iterating through the list of declarations in that
4768 // context. However, specializations are not found by name lookup.
4769 CurContext->addDecl(Specialization);
4770 }
John McCalld226f652010-08-21 09:40:31 +00004771 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00004772}
Douglas Gregord57959a2009-03-27 23:10:48 +00004773
John McCalld226f652010-08-21 09:40:31 +00004774Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00004775 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004776 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00004777 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
4778}
4779
John McCalld226f652010-08-21 09:40:31 +00004780Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00004781 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004782 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00004783 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004784 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00004785
Douglas Gregor52591bf2009-06-24 00:54:41 +00004786 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00004787 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00004788 }
Mike Stump1eb44332009-09-09 15:08:12 +00004789
Douglas Gregor52591bf2009-06-24 00:54:41 +00004790 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004791
John McCalld226f652010-08-21 09:40:31 +00004792 Decl *DP = HandleDeclarator(ParentScope, D,
4793 move(TemplateParameterLists),
4794 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00004795 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00004796 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00004797 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00004798 FunctionTemplate->getTemplatedDecl());
4799 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
4800 return ActOnStartOfFunctionDef(FnBodyScope, Function);
4801 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00004802}
4803
John McCall75042392010-02-11 01:33:53 +00004804/// \brief Strips various properties off an implicit instantiation
4805/// that has just been explicitly specialized.
4806static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00004807 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00004808
4809 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4810 FD->setInlineSpecified(false);
4811 }
4812}
4813
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004814/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00004815/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004816/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00004817/// new specialization/instantiation will have any effect.
4818///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004819/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00004820/// instantiation.
4821///
4822/// \param NewTSK the kind of the new explicit specialization or instantiation.
4823///
4824/// \param PrevDecl the previous declaration of the entity.
4825///
4826/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
4827///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004828/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00004829/// declaration was instantiated (either implicitly or explicitly).
4830///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004831/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00004832/// specialization or instantiation has no effect and should be ignored.
4833///
4834/// \returns true if there was an error that should prevent the introduction of
4835/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00004836bool
4837Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
4838 TemplateSpecializationKind NewTSK,
4839 NamedDecl *PrevDecl,
4840 TemplateSpecializationKind PrevTSK,
4841 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004842 bool &HasNoEffect) {
4843 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004844
Douglas Gregor454885e2009-10-15 15:54:05 +00004845 switch (NewTSK) {
4846 case TSK_Undeclared:
4847 case TSK_ImplicitInstantiation:
4848 assert(false && "Don't check implicit instantiations here");
4849 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004850
Douglas Gregor454885e2009-10-15 15:54:05 +00004851 case TSK_ExplicitSpecialization:
4852 switch (PrevTSK) {
4853 case TSK_Undeclared:
4854 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004855 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00004856 // explicitly specialized or has merely been mentioned without any
4857 // instantiation.
4858 return false;
4859
4860 case TSK_ImplicitInstantiation:
4861 if (PrevPointOfInstantiation.isInvalid()) {
4862 // The declaration itself has not actually been instantiated, so it is
4863 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00004864 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00004865 return false;
4866 }
4867 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004868
Douglas Gregor454885e2009-10-15 15:54:05 +00004869 case TSK_ExplicitInstantiationDeclaration:
4870 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004871 assert((PrevTSK == TSK_ImplicitInstantiation ||
4872 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00004873 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004874
Douglas Gregor454885e2009-10-15 15:54:05 +00004875 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004876 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00004877 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004878 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00004879 // implicit instantiation to take place, in every translation unit in
4880 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004881 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4882 // Is there any previous explicit specialization declaration?
4883 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
4884 return false;
4885 }
4886
Douglas Gregor0d035142009-10-27 18:42:08 +00004887 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00004888 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004889 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00004890 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004891
Douglas Gregor454885e2009-10-15 15:54:05 +00004892 return true;
4893 }
4894 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004895
Douglas Gregor454885e2009-10-15 15:54:05 +00004896 case TSK_ExplicitInstantiationDeclaration:
4897 switch (PrevTSK) {
4898 case TSK_ExplicitInstantiationDeclaration:
4899 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004900 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004901 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004902
Douglas Gregor454885e2009-10-15 15:54:05 +00004903 case TSK_Undeclared:
4904 case TSK_ImplicitInstantiation:
4905 // We're explicitly instantiating something that may have already been
4906 // implicitly instantiated; that's fine.
4907 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004908
Douglas Gregor454885e2009-10-15 15:54:05 +00004909 case TSK_ExplicitSpecialization:
4910 // C++0x [temp.explicit]p4:
4911 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004912 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00004913 // specialization for that template, the explicit instantiation has no
4914 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004915 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004916 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004917
Douglas Gregor454885e2009-10-15 15:54:05 +00004918 case TSK_ExplicitInstantiationDefinition:
4919 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004920 // If an entity is the subject of both an explicit instantiation
4921 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00004922 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004923 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00004924 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004925 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004926 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00004927 assert(PrevPointOfInstantiation.isValid() &&
4928 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004929 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004930 return false;
4931 }
4932 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004933
Douglas Gregor454885e2009-10-15 15:54:05 +00004934 case TSK_ExplicitInstantiationDefinition:
4935 switch (PrevTSK) {
4936 case TSK_Undeclared:
4937 case TSK_ImplicitInstantiation:
4938 // We're explicitly instantiating something that may have already been
4939 // implicitly instantiated; that's fine.
4940 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004941
Douglas Gregor454885e2009-10-15 15:54:05 +00004942 case TSK_ExplicitSpecialization:
4943 // C++ DR 259, C++0x [temp.explicit]p4:
4944 // For a given set of template parameters, if an explicit
4945 // instantiation of a template appears after a declaration of
4946 // an explicit specialization for that template, the explicit
4947 // instantiation has no effect.
4948 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004949 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00004950 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00004951 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00004952 if (!getLangOptions().CPlusPlus0x) {
4953 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00004954 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004955 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00004956 diag::note_previous_template_specialization);
4957 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004958 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004959 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004960
Douglas Gregor454885e2009-10-15 15:54:05 +00004961 case TSK_ExplicitInstantiationDeclaration:
4962 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004963 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00004964 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004965
Douglas Gregor454885e2009-10-15 15:54:05 +00004966 case TSK_ExplicitInstantiationDefinition:
4967 // C++0x [temp.spec]p5:
4968 // For a given template and a given set of template-arguments,
4969 // - an explicit instantiation definition shall appear at most once
4970 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00004971 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00004972 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004973 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004974 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004975 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004976 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00004977 }
4978 break;
4979 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004980
Douglas Gregor454885e2009-10-15 15:54:05 +00004981 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004982
Douglas Gregor454885e2009-10-15 15:54:05 +00004983 return false;
4984}
4985
John McCallaf2094e2010-04-08 09:05:18 +00004986/// \brief Perform semantic analysis for the given dependent function
4987/// template specialization. The only possible way to get a dependent
4988/// function template specialization is with a friend declaration,
4989/// like so:
4990///
4991/// template <class T> void foo(T);
4992/// template <class T> class A {
4993/// friend void foo<>(T);
4994/// };
4995///
4996/// There really isn't any useful analysis we can do here, so we
4997/// just store the information.
4998bool
4999Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5000 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5001 LookupResult &Previous) {
5002 // Remove anything from Previous that isn't a function template in
5003 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005004 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005005 LookupResult::Filter F = Previous.makeFilter();
5006 while (F.hasNext()) {
5007 NamedDecl *D = F.next()->getUnderlyingDecl();
5008 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005009 !FDLookupContext->InEnclosingNamespaceSetOf(
5010 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005011 F.erase();
5012 }
5013 F.done();
5014
5015 // Should this be diagnosed here?
5016 if (Previous.empty()) return true;
5017
5018 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5019 ExplicitTemplateArgs);
5020 return false;
5021}
5022
Abramo Bagnarae03db982010-05-20 15:32:11 +00005023/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005024/// specialization.
5025///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005026/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005027/// explicit function template specialization. On successful completion,
5028/// the function declaration \p FD will become a function template
5029/// specialization.
5030///
5031/// \param FD the function declaration, which will be updated to become a
5032/// function template specialization.
5033///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005034/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5035/// if any. Note that this may be valid info even when 0 arguments are
5036/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5037/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005038///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005039/// \param PrevDecl the set of declarations that may be specialized by
5040/// this function specialization.
5041bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005042Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005043 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005044 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005045 // The set of function template specializations that could match this
5046 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005047 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005048
Sebastian Redl7a126a42010-08-31 00:36:30 +00005049 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005050 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5051 I != E; ++I) {
5052 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5053 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005054 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005055 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005056 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5057 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005058 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005059
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005060 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005061 // A trailing template-argument can be left unspecified in the
5062 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005063 // provided it can be deduced from the function argument type.
5064 // Perform template argument deduction to determine whether we may be
5065 // specializing this template.
5066 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005067 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005068 FunctionDecl *Specialization = 0;
5069 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005070 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005071 FD->getType(),
5072 Specialization,
5073 Info)) {
5074 // FIXME: Template argument deduction failed; record why it failed, so
5075 // that we can provide nifty diagnostics.
5076 (void)TDK;
5077 continue;
5078 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005079
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005080 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005081 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005082 }
5083 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005084
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005085 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005086 UnresolvedSetIterator Result
5087 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005088 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005089 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005090 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005091 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005092 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005093 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005094 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005095 return true;
John McCallc373d482010-01-27 01:50:18 +00005096
5097 // Ignore access information; it doesn't figure into redeclaration checking.
5098 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005099
5100 FunctionTemplateSpecializationInfo *SpecInfo
5101 = Specialization->getTemplateSpecializationInfo();
5102 assert(SpecInfo && "Function template specialization info missing?");
5103 {
5104 // Note: do not overwrite location info if previous template
5105 // specialization kind was explicit.
5106 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5107 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5108 Specialization->setLocation(FD->getLocation());
5109 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005110
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005111 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005112 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005113
5114 // If this is a friend declaration, then we're not really declaring
5115 // an explicit specialization.
5116 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005117
Douglas Gregord5cb8762009-10-07 00:13:32 +00005118 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005119 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005120 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005121 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005122 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005123 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005124 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005125
5126 // C++ [temp.expl.spec]p6:
5127 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005128 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005129 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005130 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005131 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005132 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005133 if (!isFriend &&
5134 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005135 TSK_ExplicitSpecialization,
5136 Specialization,
5137 SpecInfo->getTemplateSpecializationKind(),
5138 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005139 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005140 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005141
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005142 // Mark the prior declaration as an explicit specialization, so that later
5143 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005144 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005145 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005146 MarkUnusedFileScopedDecl(Specialization);
5147 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005148
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005149 // Turn the given function declaration into a function template
5150 // specialization, with the template arguments from the previous
5151 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005152 // Take copies of (semantic and syntactic) template argument lists.
5153 const TemplateArgumentList* TemplArgs = new (Context)
5154 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5155 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5156 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005157 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005158 TemplArgs, /*InsertPos=*/0,
5159 SpecInfo->getTemplateSpecializationKind(),
5160 TemplArgsAsWritten);
5161
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005162 // The "previous declaration" for this function template specialization is
5163 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005164 Previous.clear();
5165 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005166 return false;
5167}
5168
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005169/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005170/// specialization.
5171///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005172/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005173/// explicit member function specialization. On successful completion,
5174/// the function declaration \p FD will become a member function
5175/// specialization.
5176///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005177/// \param Member the member declaration, which will be updated to become a
5178/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005179///
John McCall68263142009-11-18 22:49:29 +00005180/// \param Previous the set of declarations, one of which may be specialized
5181/// by this function specialization; the set will be modified to contain the
5182/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005183bool
John McCall68263142009-11-18 22:49:29 +00005184Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005185 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005186
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005187 // Try to find the member we are instantiating.
5188 NamedDecl *Instantiation = 0;
5189 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005190 MemberSpecializationInfo *MSInfo = 0;
5191
John McCall68263142009-11-18 22:49:29 +00005192 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005193 // Nowhere to look anyway.
5194 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005195 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5196 I != E; ++I) {
5197 NamedDecl *D = (*I)->getUnderlyingDecl();
5198 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005199 if (Context.hasSameType(Function->getType(), Method->getType())) {
5200 Instantiation = Method;
5201 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005202 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005203 break;
5204 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005205 }
5206 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005207 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005208 VarDecl *PrevVar;
5209 if (Previous.isSingleResult() &&
5210 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005211 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005212 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005213 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005214 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005215 }
5216 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005217 CXXRecordDecl *PrevRecord;
5218 if (Previous.isSingleResult() &&
5219 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5220 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005221 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005222 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005223 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005224 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005225
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005226 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005227 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005228 // specializations are always out-of-line, the caller will complain about
5229 // this mismatch later.
5230 return false;
5231 }
John McCall77e8b112010-04-13 20:37:33 +00005232
5233 // If this is a friend, just bail out here before we start turning
5234 // things into explicit specializations.
5235 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5236 // Preserve instantiation information.
5237 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5238 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5239 cast<CXXMethodDecl>(InstantiatedFrom),
5240 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5241 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5242 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5243 cast<CXXRecordDecl>(InstantiatedFrom),
5244 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5245 }
5246
5247 Previous.clear();
5248 Previous.addDecl(Instantiation);
5249 return false;
5250 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005251
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005252 // Make sure that this is a specialization of a member.
5253 if (!InstantiatedFrom) {
5254 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5255 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005256 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5257 return true;
5258 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005259
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005260 // C++ [temp.expl.spec]p6:
5261 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005262 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005263 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005264 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005265 // use occurs; no diagnostic is required.
5266 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005267
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005268 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005269 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5270 TSK_ExplicitSpecialization,
5271 Instantiation,
5272 MSInfo->getTemplateSpecializationKind(),
5273 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005274 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005275 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005276
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005277 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005278 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005279 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005280 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005281 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005282 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005283
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005284 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005285 // the original declaration to note that it is an explicit specialization
5286 // (if it was previously an implicit instantiation). This latter step
5287 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005288 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005289 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5290 if (InstantiationFunction->getTemplateSpecializationKind() ==
5291 TSK_ImplicitInstantiation) {
5292 InstantiationFunction->setTemplateSpecializationKind(
5293 TSK_ExplicitSpecialization);
5294 InstantiationFunction->setLocation(Member->getLocation());
5295 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005296
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005297 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5298 cast<CXXMethodDecl>(InstantiatedFrom),
5299 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005300 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005301 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005302 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5303 if (InstantiationVar->getTemplateSpecializationKind() ==
5304 TSK_ImplicitInstantiation) {
5305 InstantiationVar->setTemplateSpecializationKind(
5306 TSK_ExplicitSpecialization);
5307 InstantiationVar->setLocation(Member->getLocation());
5308 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005309
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005310 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5311 cast<VarDecl>(InstantiatedFrom),
5312 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005313 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005314 } else {
5315 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005316 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5317 if (InstantiationClass->getTemplateSpecializationKind() ==
5318 TSK_ImplicitInstantiation) {
5319 InstantiationClass->setTemplateSpecializationKind(
5320 TSK_ExplicitSpecialization);
5321 InstantiationClass->setLocation(Member->getLocation());
5322 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005323
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005324 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005325 cast<CXXRecordDecl>(InstantiatedFrom),
5326 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005327 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005328
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005329 // Save the caller the trouble of having to figure out which declaration
5330 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005331 Previous.clear();
5332 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005333 return false;
5334}
5335
Douglas Gregor558c0322009-10-14 23:41:34 +00005336/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005337///
5338/// \returns true if a serious error occurs, false otherwise.
5339static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005340 SourceLocation InstLoc,
5341 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005342 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5343 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005344
Douglas Gregor669eed82010-07-13 00:10:04 +00005345 if (CurContext->isRecord()) {
5346 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5347 << D;
5348 return true;
5349 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005350
Douglas Gregor558c0322009-10-14 23:41:34 +00005351 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005352 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005353 // template.
5354 //
5355 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005356 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005357 !CurContext->Encloses(OrigContext)) {
5358 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005359 S.Diag(InstLoc,
5360 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005361 diag::err_explicit_instantiation_out_of_scope
5362 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005363 << D << NS;
5364 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005365 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005366 S.getLangOptions().CPlusPlus0x?
5367 diag::err_explicit_instantiation_must_be_global
5368 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005369 << D;
5370 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005371 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005372 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005373
Douglas Gregor558c0322009-10-14 23:41:34 +00005374 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005375 // If the name declared in the explicit instantiation is an unqualified
5376 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005377 // its template is declared or, if that namespace is inline (7.3.1), any
5378 // namespace from its enclosing namespace set.
5379 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005380 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005381
5382 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005383 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005384
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005385 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005386 S.getLangOptions().CPlusPlus0x?
5387 diag::err_explicit_instantiation_unqualified_wrong_namespace
5388 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005389 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005390 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005391 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005392}
5393
5394/// \brief Determine whether the given scope specifier has a template-id in it.
5395static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5396 if (!SS.isSet())
5397 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005398
Douglas Gregor558c0322009-10-14 23:41:34 +00005399 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005400 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005401 // or a static data member of a class template specialization, the name of
5402 // the class template specialization in the qualified-id for the member
5403 // name shall be a simple-template-id.
5404 //
5405 // C++98 has the same restriction, just worded differently.
5406 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5407 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005408 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005409 if (isa<TemplateSpecializationType>(T))
5410 return true;
5411
5412 return false;
5413}
5414
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005415// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005416DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005417Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005418 SourceLocation ExternLoc,
5419 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005420 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005421 SourceLocation KWLoc,
5422 const CXXScopeSpec &SS,
5423 TemplateTy TemplateD,
5424 SourceLocation TemplateNameLoc,
5425 SourceLocation LAngleLoc,
5426 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005427 SourceLocation RAngleLoc,
5428 AttributeList *Attr) {
5429 // Find the class template we're specializing
5430 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005431 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005432 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5433
5434 // Check that the specialization uses the same tag kind as the
5435 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005436 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5437 assert(Kind != TTK_Enum &&
5438 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005439 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005440 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005441 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005442 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005443 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005444 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005445 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005446 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005447 diag::note_previous_use);
5448 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5449 }
5450
Douglas Gregor558c0322009-10-14 23:41:34 +00005451 // C++0x [temp.explicit]p2:
5452 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005453 // definition and an explicit instantiation declaration. An explicit
5454 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005455 TemplateSpecializationKind TSK
5456 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5457 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005458
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005459 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005460 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005461 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005462
5463 // Check that the template argument list is well-formed for this
5464 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005465 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005466 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5467 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005468 return true;
5469
Douglas Gregor910f8002010-11-07 23:05:16 +00005470 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005471 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005472
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005473 // Find the class template specialization declaration that
5474 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005475 void *InsertPos = 0;
5476 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005477 = ClassTemplate->findSpecialization(Converted.data(),
5478 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005479
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005480 TemplateSpecializationKind PrevDecl_TSK
5481 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5482
Douglas Gregord5cb8762009-10-07 00:13:32 +00005483 // C++0x [temp.explicit]p2:
5484 // [...] An explicit instantiation shall appear in an enclosing
5485 // namespace of its template. [...]
5486 //
5487 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005488 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5489 SS.isSet()))
5490 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005491
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005492 ClassTemplateSpecializationDecl *Specialization = 0;
5493
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005494 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005495 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005496 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005497 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005498 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005499 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005500 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005501
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005502 // Even though HasNoEffect == true means that this explicit instantiation
5503 // has no effect on semantics, we go on to put its syntax in the AST.
5504
5505 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5506 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005507 // Since the only prior class template specialization with these
5508 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005509 // declaration node as our own, updating the source location
5510 // for the template name to reflect our new declaration.
5511 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005512 Specialization = PrevDecl;
5513 Specialization->setLocation(TemplateNameLoc);
5514 PrevDecl = 0;
5515 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005516 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005517
Douglas Gregor52604ab2009-09-11 21:19:12 +00005518 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005519 // Create a new class template specialization declaration node for
5520 // this explicit specialization.
5521 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005522 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005523 ClassTemplate->getDeclContext(),
5524 TemplateNameLoc,
5525 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005526 Converted.data(),
5527 Converted.size(),
5528 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005529 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005530
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005531 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005532 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005533 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005534 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005535 }
5536
5537 // Build the fully-sugared type for this explicit instantiation as
5538 // the user wrote in the explicit instantiation itself. This means
5539 // that we'll pretty-print the type retrieved from the
5540 // specialization's declaration the way that the user actually wrote
5541 // the explicit instantiation, rather than formatting the name based
5542 // on the "canonical" representation used to store the template
5543 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005544 TypeSourceInfo *WrittenTy
5545 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5546 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005547 Context.getTypeDeclType(Specialization));
5548 Specialization->setTypeAsWritten(WrittenTy);
5549 TemplateArgsIn.release();
5550
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005551 // Set source locations for keywords.
5552 Specialization->setExternLoc(ExternLoc);
5553 Specialization->setTemplateKeywordLoc(TemplateLoc);
5554
5555 // Add the explicit instantiation into its lexical context. However,
5556 // since explicit instantiations are never found by name lookup, we
5557 // just put it into the declaration context directly.
5558 Specialization->setLexicalDeclContext(CurContext);
5559 CurContext->addDecl(Specialization);
5560
5561 // Syntax is now OK, so return if it has no other effect on semantics.
5562 if (HasNoEffect) {
5563 // Set the template specialization kind.
5564 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005565 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005566 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005567
5568 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005569 // A definition of a class template or class member template
5570 // shall be in scope at the point of the explicit instantiation of
5571 // the class template or class member template.
5572 //
5573 // This check comes when we actually try to perform the
5574 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005575 ClassTemplateSpecializationDecl *Def
5576 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005577 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005578 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005579 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005580 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005581 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005582 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5583 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005584
Douglas Gregor0d035142009-10-27 18:42:08 +00005585 // Instantiate the members of this class template specialization.
5586 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005587 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005588 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005589 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5590
5591 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5592 // TSK_ExplicitInstantiationDefinition
5593 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5594 TSK == TSK_ExplicitInstantiationDefinition)
5595 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005596
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005597 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005598 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005599
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005600 // Set the template specialization kind.
5601 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005602 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005603}
5604
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005605// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005606DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005607Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005608 SourceLocation ExternLoc,
5609 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005610 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005611 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005612 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005613 IdentifierInfo *Name,
5614 SourceLocation NameLoc,
5615 AttributeList *Attr) {
5616
Douglas Gregor402abb52009-05-28 23:31:59 +00005617 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005618 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005619 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005620 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5621 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005622 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005623 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005624 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5625
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005626 if (!TagD)
5627 return true;
5628
John McCalld226f652010-08-21 09:40:31 +00005629 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005630 if (Tag->isEnum()) {
5631 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5632 << Context.getTypeDeclType(Tag);
5633 return true;
5634 }
5635
Douglas Gregord0c87372009-05-27 17:30:49 +00005636 if (Tag->isInvalidDecl())
5637 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005638
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005639 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5640 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5641 if (!Pattern) {
5642 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5643 << Context.getTypeDeclType(Record);
5644 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5645 return true;
5646 }
5647
Douglas Gregor558c0322009-10-14 23:41:34 +00005648 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005649 // If the explicit instantiation is for a class or member class, the
5650 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005651 // simple-template-id.
5652 //
5653 // C++98 has the same restriction, just worded differently.
5654 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005655 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005656 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005657
Douglas Gregor558c0322009-10-14 23:41:34 +00005658 // C++0x [temp.explicit]p2:
5659 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005660 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005661 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005662 TemplateSpecializationKind TSK
5663 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5664 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005665
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005666 // C++0x [temp.explicit]p2:
5667 // [...] An explicit instantiation shall appear in an enclosing
5668 // namespace of its template. [...]
5669 //
5670 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005671 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005672
Douglas Gregor454885e2009-10-15 15:54:05 +00005673 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005674 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005675 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005676 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005677 PrevDecl = Record;
5678 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005679 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005680 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005681 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005682 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005683 PrevDecl,
5684 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005685 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005686 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005687 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005688 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005689 return TagD;
5690 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005691
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005692 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00005693 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005694 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005695 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005696 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005697 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005698 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00005699 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005700 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00005701 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
5702 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005703 Diag(Pattern->getLocation(), diag::note_forward_declaration)
5704 << Pattern;
5705 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00005706 } else {
5707 if (InstantiateClass(NameLoc, Record, Def,
5708 getTemplateInstantiationArgs(Record),
5709 TSK))
5710 return true;
5711
Douglas Gregor952b0172010-02-11 01:04:33 +00005712 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00005713 if (!RecordDef)
5714 return true;
5715 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005716 }
5717
Douglas Gregor0d035142009-10-27 18:42:08 +00005718 // Instantiate all of the members of the class.
5719 InstantiateClassMembers(NameLoc, RecordDef,
5720 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005721
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005722 if (TSK == TSK_ExplicitInstantiationDefinition)
5723 MarkVTableUsed(NameLoc, RecordDef, true);
5724
Mike Stump390b4cc2009-05-16 07:39:55 +00005725 // FIXME: We don't have any representation for explicit instantiations of
5726 // member classes. Such a representation is not needed for compilation, but it
5727 // should be available for clients that want to see all of the declarations in
5728 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005729 return TagD;
5730}
5731
John McCallf312b1e2010-08-26 23:41:50 +00005732DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
5733 SourceLocation ExternLoc,
5734 SourceLocation TemplateLoc,
5735 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00005736 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00005737 // TODO: check if/when DNInfo should replace Name.
5738 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
5739 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005740 if (!Name) {
5741 if (!D.isInvalidType())
5742 Diag(D.getDeclSpec().getSourceRange().getBegin(),
5743 diag::err_explicit_instantiation_requires_name)
5744 << D.getDeclSpec().getSourceRange()
5745 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005746
Douglas Gregord5a423b2009-09-25 18:43:00 +00005747 return true;
5748 }
5749
5750 // The scope passed in may not be a decl scope. Zip up the scope tree until
5751 // we find one that is.
5752 while ((S->getFlags() & Scope::DeclScope) == 0 ||
5753 (S->getFlags() & Scope::TemplateParamScope) != 0)
5754 S = S->getParent();
5755
5756 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00005757 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
5758 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005759 if (R.isNull())
5760 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005761
Douglas Gregord5a423b2009-09-25 18:43:00 +00005762 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
5763 // Cannot explicitly instantiate a typedef.
5764 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
5765 << Name;
5766 return true;
5767 }
5768
Douglas Gregor663b5a02009-10-14 20:14:33 +00005769 // C++0x [temp.explicit]p1:
5770 // [...] An explicit instantiation of a function template shall not use the
5771 // inline or constexpr specifiers.
5772 // Presumably, this also applies to member functions of class templates as
5773 // well.
5774 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005775 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00005776 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00005777 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005778
Douglas Gregor663b5a02009-10-14 20:14:33 +00005779 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005780
Douglas Gregor558c0322009-10-14 23:41:34 +00005781 // C++0x [temp.explicit]p2:
5782 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005783 // definition and an explicit instantiation declaration. An explicit
5784 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00005785 TemplateSpecializationKind TSK
5786 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5787 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005788
Abramo Bagnara25777432010-08-11 22:01:17 +00005789 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00005790 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005791
5792 if (!R->isFunctionType()) {
5793 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005794 // A [...] static data member of a class template can be explicitly
5795 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005796 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00005797 if (Previous.isAmbiguous())
5798 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005799
John McCall1bcee0a2009-12-02 08:25:40 +00005800 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005801 if (!Prev || !Prev->isStaticDataMember()) {
5802 // We expect to see a data data member here.
5803 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
5804 << Name;
5805 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5806 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00005807 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00005808 return true;
5809 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005810
Douglas Gregord5a423b2009-09-25 18:43:00 +00005811 if (!Prev->getInstantiatedFromStaticDataMember()) {
5812 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005813 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005814 diag::err_explicit_instantiation_data_member_not_instantiated)
5815 << Prev;
5816 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
5817 // FIXME: Can we provide a note showing where this was declared?
5818 return true;
5819 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005820
Douglas Gregor558c0322009-10-14 23:41:34 +00005821 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005822 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005823 // or a static data member of a class template specialization, the name of
5824 // the class template specialization in the qualified-id for the member
5825 // name shall be a simple-template-id.
5826 //
5827 // C++98 has the same restriction, just worded differently.
5828 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005829 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005830 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005831 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005832
Douglas Gregor558c0322009-10-14 23:41:34 +00005833 // Check the scope of this explicit instantiation.
5834 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005835
Douglas Gregor454885e2009-10-15 15:54:05 +00005836 // Verify that it is okay to explicitly instantiate here.
5837 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
5838 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005839 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005840 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00005841 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005842 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005843 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005844 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005845 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005846 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005847
Douglas Gregord5a423b2009-09-25 18:43:00 +00005848 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005849 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005850 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005851 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005852
Douglas Gregord5a423b2009-09-25 18:43:00 +00005853 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00005854 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005855 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005856
5857 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00005858 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00005859 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00005860 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005861 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
5862 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00005863 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
5864 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00005865 ASTTemplateArgsPtr TemplateArgsPtr(*this,
5866 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00005867 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00005868 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00005869 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00005870 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00005871 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005872
Douglas Gregord5a423b2009-09-25 18:43:00 +00005873 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005874 // A [...] function [...] can be explicitly instantiated from its template.
5875 // A member function [...] of a class template can be explicitly
5876 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005877 // template.
John McCallc373d482010-01-27 01:50:18 +00005878 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005879 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5880 P != PEnd; ++P) {
5881 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00005882 if (!HasExplicitTemplateArgs) {
5883 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
5884 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
5885 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00005886
John McCallc373d482010-01-27 01:50:18 +00005887 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00005888 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
5889 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00005890 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00005891 }
5892 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005893
Douglas Gregord5a423b2009-09-25 18:43:00 +00005894 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
5895 if (!FunTmpl)
5896 continue;
5897
John McCall5769d612010-02-08 23:07:23 +00005898 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005899 FunctionDecl *Specialization = 0;
5900 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005901 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00005902 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005903 R, Specialization, Info)) {
5904 // FIXME: Keep track of almost-matches?
5905 (void)TDK;
5906 continue;
5907 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005908
John McCallc373d482010-01-27 01:50:18 +00005909 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005910 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005911
Douglas Gregord5a423b2009-09-25 18:43:00 +00005912 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005913 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005914 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005915 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005916 PDiag(diag::err_explicit_instantiation_not_known) << Name,
5917 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
5918 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00005919
John McCallc373d482010-01-27 01:50:18 +00005920 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00005921 return true;
John McCallc373d482010-01-27 01:50:18 +00005922
5923 // Ignore access control bits, we don't need them for redeclaration checking.
5924 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005925
Douglas Gregor0a897e32009-10-15 17:21:20 +00005926 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005927 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005928 diag::err_explicit_instantiation_member_function_not_instantiated)
5929 << Specialization
5930 << (Specialization->getTemplateSpecializationKind() ==
5931 TSK_ExplicitSpecialization);
5932 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
5933 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005934 }
5935
Douglas Gregor0a897e32009-10-15 17:21:20 +00005936 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00005937 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
5938 PrevDecl = Specialization;
5939
Douglas Gregor0a897e32009-10-15 17:21:20 +00005940 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005941 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005942 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005943 PrevDecl,
5944 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00005945 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005946 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00005947 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005948
Douglas Gregor0a897e32009-10-15 17:21:20 +00005949 // FIXME: We may still want to build some representation of this
5950 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005951 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005952 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00005953 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00005954
5955 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005956
Douglas Gregor0a897e32009-10-15 17:21:20 +00005957 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005958 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005959
Douglas Gregor558c0322009-10-14 23:41:34 +00005960 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005961 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005962 // or a static data member of a class template specialization, the name of
5963 // the class template specialization in the qualified-id for the member
5964 // name shall be a simple-template-id.
5965 //
5966 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005967 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005968 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005969 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00005970 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005971 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005972 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005973 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005974
Douglas Gregor558c0322009-10-14 23:41:34 +00005975 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005976 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00005977 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005978 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00005979 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005980
Douglas Gregord5a423b2009-09-25 18:43:00 +00005981 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00005982 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005983}
5984
John McCallf312b1e2010-08-26 23:41:50 +00005985TypeResult
John McCallc4e70192009-09-11 04:59:25 +00005986Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
5987 const CXXScopeSpec &SS, IdentifierInfo *Name,
5988 SourceLocation TagLoc, SourceLocation NameLoc) {
5989 // This has to hold, because SS is expected to be defined.
5990 assert(Name && "Expected a name in a dependent tag");
5991
5992 NestedNameSpecifier *NNS
5993 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
5994 if (!NNS)
5995 return true;
5996
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005997 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00005998
Douglas Gregor48c89f42010-04-24 16:38:41 +00005999 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6000 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006001 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006002 return true;
6003 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006004
Douglas Gregor059101f2011-03-02 00:47:37 +00006005 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006006 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006007 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6008
6009 // Create type-source location information for this type.
6010 TypeLocBuilder TLB;
6011 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6012 TL.setKeywordLoc(TagLoc);
6013 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6014 TL.setNameLoc(NameLoc);
6015 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006016}
6017
John McCallf312b1e2010-08-26 23:41:50 +00006018TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006019Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6020 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006021 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006022 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006023 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006024
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006025 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6026 !getLangOptions().CPlusPlus0x)
6027 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006028 << FixItHint::CreateRemoval(TypenameLoc);
6029
Douglas Gregor2494dd02011-03-01 01:34:45 +00006030 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006031 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6032 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006033 if (T.isNull())
6034 return true;
John McCall63b43852010-04-29 23:50:39 +00006035
6036 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6037 if (isa<DependentNameType>(T)) {
6038 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006039 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006040 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006041 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006042 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006043 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006044 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006045 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006046 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006047 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006048
John McCallb3d87482010-08-24 05:47:05 +00006049 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006050}
6051
John McCallf312b1e2010-08-26 23:41:50 +00006052TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006053Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6054 const CXXScopeSpec &SS,
6055 SourceLocation TemplateLoc,
6056 TemplateTy TemplateIn,
6057 SourceLocation TemplateNameLoc,
6058 SourceLocation LAngleLoc,
6059 ASTTemplateArgsPtr TemplateArgsIn,
6060 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006061 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6062 !getLangOptions().CPlusPlus0x)
6063 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006064 << FixItHint::CreateRemoval(TypenameLoc);
6065
6066 // Translate the parser's template argument list in our AST format.
6067 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6068 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6069
6070 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006071 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6072 // Construct a dependent template specialization type.
6073 assert(DTN && "dependent template has non-dependent name?");
6074 assert(DTN->getQualifier()
6075 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6076 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6077 DTN->getQualifier(),
6078 DTN->getIdentifier(),
6079 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006080
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006081 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006082 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006083 DependentTemplateSpecializationTypeLoc SpecTL
6084 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006085 SpecTL.setLAngleLoc(LAngleLoc);
6086 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006087 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006088 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006089 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006090 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6091 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006092 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006093 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006094
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006095 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6096 if (T.isNull())
6097 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006098
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006099 // Provide source-location information for the template specialization
6100 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006101 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006102 TemplateSpecializationTypeLoc SpecTL
6103 = Builder.push<TemplateSpecializationTypeLoc>(T);
6104
6105 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006106 SpecTL.setLAngleLoc(LAngleLoc);
6107 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006108 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006109 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6110 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6111
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006112 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6113 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6114 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006115 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6116
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006117 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6118 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006119}
6120
Douglas Gregora02411e2011-02-27 22:46:49 +00006121
Douglas Gregord57959a2009-03-27 23:10:48 +00006122/// \brief Build the type that describes a C++ typename specifier,
6123/// e.g., "typename T::type".
6124QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006125Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6126 SourceLocation KeywordLoc,
6127 NestedNameSpecifierLoc QualifierLoc,
6128 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006129 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006130 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006131 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006132
John McCall77bb1aa2010-05-01 00:40:08 +00006133 DeclContext *Ctx = computeDeclContext(SS);
6134 if (!Ctx) {
6135 // If the nested-name-specifier is dependent and couldn't be
6136 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006137 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6138 return Context.getDependentNameType(Keyword,
6139 QualifierLoc.getNestedNameSpecifier(),
6140 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006141 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006142
John McCall77bb1aa2010-05-01 00:40:08 +00006143 // If the nested-name-specifier refers to the current instantiation,
6144 // the "typename" keyword itself is superfluous. In C++03, the
6145 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6146 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006147 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006148
John McCall77bb1aa2010-05-01 00:40:08 +00006149 if (RequireCompleteDeclContext(SS, Ctx))
6150 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006151
6152 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006153 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006154 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006155 unsigned DiagID = 0;
6156 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006157 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006158 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006159 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006160 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006161
6162 case LookupResult::FoundUnresolvedValue: {
6163 // We found a using declaration that is a value. Most likely, the using
6164 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006165 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006166 IILoc);
6167 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6168 << Name << Ctx << FullRange;
6169 if (UnresolvedUsingValueDecl *Using
6170 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006171 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006172 Diag(Loc, diag::note_using_value_decl_missing_typename)
6173 << FixItHint::CreateInsertion(Loc, "typename ");
6174 }
6175 }
6176 // Fall through to create a dependent typename type, from which we can recover
6177 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006178
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006179 case LookupResult::NotFoundInCurrentInstantiation:
6180 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006181 return Context.getDependentNameType(Keyword,
6182 QualifierLoc.getNestedNameSpecifier(),
6183 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006184
6185 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006186 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006187 // We found a type. Build an ElaboratedType, since the
6188 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006189 return Context.getElaboratedType(ETK_Typename,
6190 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006191 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006192 }
6193
6194 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006195 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006196 break;
6197
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006198
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006199 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006200 return QualType();
6201
Douglas Gregord57959a2009-03-27 23:10:48 +00006202 case LookupResult::FoundOverloaded:
6203 DiagID = diag::err_typename_nested_not_type;
6204 Referenced = *Result.begin();
6205 break;
6206
John McCall6e247262009-10-10 05:48:19 +00006207 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006208 return QualType();
6209 }
6210
6211 // If we get here, it's because name lookup did not find a
6212 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006213 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006214 IILoc);
6215 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006216 if (Referenced)
6217 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6218 << Name;
6219 return QualType();
6220}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006221
6222namespace {
6223 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006224 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006225 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006226 SourceLocation Loc;
6227 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006228
Douglas Gregor4a959d82009-08-06 16:20:37 +00006229 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006230 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006231
Mike Stump1eb44332009-09-09 15:08:12 +00006232 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006233 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006234 DeclarationName Entity)
6235 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006236 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006237
6238 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006239 /// transformed.
6240 ///
6241 /// For the purposes of type reconstruction, a type has already been
6242 /// transformed if it is NULL or if it is not dependent.
6243 bool AlreadyTransformed(QualType T) {
6244 return T.isNull() || !T->isDependentType();
6245 }
Mike Stump1eb44332009-09-09 15:08:12 +00006246
6247 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006248 /// rebuilt.
6249 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006250
Douglas Gregor4a959d82009-08-06 16:20:37 +00006251 /// \brief Returns the name of the entity whose type is being rebuilt.
6252 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006253
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006254 /// \brief Sets the "base" location and entity when that
6255 /// information is known based on another transformation.
6256 void setBase(SourceLocation Loc, DeclarationName Entity) {
6257 this->Loc = Loc;
6258 this->Entity = Entity;
6259 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006260 };
6261}
6262
Douglas Gregor4a959d82009-08-06 16:20:37 +00006263/// \brief Rebuilds a type within the context of the current instantiation.
6264///
Mike Stump1eb44332009-09-09 15:08:12 +00006265/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006266/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006267/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006268/// partial specialization thereof). This routine will rebuild that type now
6269/// that we have entered the declarator's scope, which may produce different
6270/// canonical types, e.g.,
6271///
6272/// \code
6273/// template<typename T>
6274/// struct X {
6275/// typedef T* pointer;
6276/// pointer data();
6277/// };
6278///
6279/// template<typename T>
6280/// typename X<T>::pointer X<T>::data() { ... }
6281/// \endcode
6282///
Douglas Gregor4714c122010-03-31 17:34:00 +00006283/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006284/// since we do not know that we can look into X<T> when we parsed the type.
6285/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006286/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006287/// as the canonical type of T*, allowing the return types of the out-of-line
6288/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006289TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6290 SourceLocation Loc,
6291 DeclarationName Name) {
6292 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006293 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006294
Douglas Gregor4a959d82009-08-06 16:20:37 +00006295 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6296 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006297}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006298
John McCall60d7b3a2010-08-24 06:29:42 +00006299ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006300 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6301 DeclarationName());
6302 return Rebuilder.TransformExpr(E);
6303}
6304
John McCall63b43852010-04-29 23:50:39 +00006305bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006306 if (SS.isInvalid())
6307 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006308
Douglas Gregor7e384942011-02-25 16:07:42 +00006309 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006310 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6311 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006312 NestedNameSpecifierLoc Rebuilt
6313 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6314 if (!Rebuilt)
6315 return true;
John McCall63b43852010-04-29 23:50:39 +00006316
Douglas Gregor7e384942011-02-25 16:07:42 +00006317 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006318 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006319}
6320
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006321/// \brief Produces a formatted string that describes the binding of
6322/// template parameters to template arguments.
6323std::string
6324Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6325 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006326 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006327}
6328
6329std::string
6330Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6331 const TemplateArgument *Args,
6332 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006333 llvm::SmallString<128> Str;
6334 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006335
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006336 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006337 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006338
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006339 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006340 if (I >= NumArgs)
6341 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006342
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006343 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006344 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006345 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006346 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006347
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006348 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006349 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006350 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006351 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006352 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006353
Douglas Gregor87dd6972010-12-20 16:52:59 +00006354 Out << " = ";
6355 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006356 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006357
6358 Out << ']';
6359 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006360}