blob: 0d290969dbec2995b59d7902db29520d8ae64bfe [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 Gregor788cd062009-11-11 01:00:40 +0000472 Arg.getScopeSpec().getRange(),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000473 Arg.getLocation(),
474 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000475 }
476 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000477
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000478 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000479 return TemplateArgumentLoc();
480}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000481
Douglas Gregor788cd062009-11-11 01:00:40 +0000482/// \brief Translates template arguments as provided by the parser
483/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000484void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
485 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000486 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000487 TemplateArgs.addArgument(translateTemplateArgument(*this,
488 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000489}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000490
Douglas Gregor72c3f312008-12-05 18:15:24 +0000491/// ActOnTypeParameter - Called when a C++ template type parameter
492/// (e.g., "typename T") has been parsed. Typename specifies whether
493/// the keyword "typename" was used to declare the type parameter
494/// (otherwise, "class" was used), and KeyLoc is the location of the
495/// "class" or "typename" keyword. ParamName is the name of the
496/// parameter (NULL indicates an unnamed template parameter) and
Douglas Gregorefed5c82010-06-16 15:23:05 +0000497/// ParamName is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000498/// If the type parameter has a default argument, it will be added
499/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000500Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
501 SourceLocation EllipsisLoc,
502 SourceLocation KeyLoc,
503 IdentifierInfo *ParamName,
504 SourceLocation ParamNameLoc,
505 unsigned Depth, unsigned Position,
506 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000507 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000508 assert(S->isTemplateParamScope() &&
509 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000510 bool Invalid = false;
511
512 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000513 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000514 LookupOrdinaryName,
515 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000516 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000517 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000518 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000519 }
520
Douglas Gregorddc29e12009-02-06 22:42:48 +0000521 SourceLocation Loc = ParamNameLoc;
522 if (!ParamName)
523 Loc = KeyLoc;
524
Douglas Gregor72c3f312008-12-05 18:15:24 +0000525 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000526 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
527 Loc, Depth, Position, ParamName, Typename,
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000528 Ellipsis);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000529 if (Invalid)
530 Param->setInvalidDecl();
531
532 if (ParamName) {
533 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000534 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000535 IdResolver.AddDecl(Param);
536 }
537
Douglas Gregor61c4d282011-01-05 15:48:55 +0000538 // C++0x [temp.param]p9:
539 // A default template-argument may be specified for any kind of
540 // template-parameter that is not a template parameter pack.
541 if (DefaultArg && Ellipsis) {
542 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
543 DefaultArg = ParsedType();
544 }
545
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000546 // Handle the default argument, if provided.
547 if (DefaultArg) {
548 TypeSourceInfo *DefaultTInfo;
549 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000550
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000551 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000552
Douglas Gregor6f526752010-12-16 08:48:57 +0000553 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000554 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000555 UPPC_DefaultArgument))
556 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000557
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000558 // Check the template argument itself.
559 if (CheckTemplateArgument(Param, DefaultTInfo)) {
560 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000561 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000562 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000563
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000564 Param->setDefaultArgument(DefaultTInfo, false);
565 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000566
John McCalld226f652010-08-21 09:40:31 +0000567 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000568}
569
Douglas Gregor2943aed2009-03-03 04:44:36 +0000570/// \brief Check that the type of a non-type template parameter is
571/// well-formed.
572///
573/// \returns the (possibly-promoted) parameter type if valid;
574/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000575QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000576Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000577 // We don't allow variably-modified types as the type of non-type template
578 // parameters.
579 if (T->isVariablyModifiedType()) {
580 Diag(Loc, diag::err_variably_modified_nontype_template_param)
581 << T;
582 return QualType();
583 }
584
Douglas Gregor2943aed2009-03-03 04:44:36 +0000585 // C++ [temp.param]p4:
586 //
587 // A non-type template-parameter shall have one of the following
588 // (optionally cv-qualified) types:
589 //
590 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000591 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000592 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000593 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000594 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000595 T->isReferenceType() ||
596 // -- pointer to member.
597 T->isMemberPointerType() ||
598 // If T is a dependent type, we can't do the check now, so we
599 // assume that it is well-formed.
600 T->isDependentType())
601 return T;
602 // C++ [temp.param]p8:
603 //
604 // A non-type template-parameter of type "array of T" or
605 // "function returning T" is adjusted to be of type "pointer to
606 // T" or "pointer to function returning T", respectively.
607 else if (T->isArrayType())
608 // FIXME: Keep the type prior to promotion?
609 return Context.getArrayDecayedType(T);
610 else if (T->isFunctionType())
611 // FIXME: Keep the type prior to promotion?
612 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000613
Douglas Gregor2943aed2009-03-03 04:44:36 +0000614 Diag(Loc, diag::err_template_nontype_parm_bad_type)
615 << T;
616
617 return QualType();
618}
619
John McCalld226f652010-08-21 09:40:31 +0000620Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
621 unsigned Depth,
622 unsigned Position,
623 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000624 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000625 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
626 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000627
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000628 assert(S->isTemplateParamScope() &&
629 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000630 bool Invalid = false;
631
632 IdentifierInfo *ParamName = D.getIdentifier();
633 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000634 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000635 LookupOrdinaryName,
636 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000637 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000638 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000639 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000640 }
641
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000642 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
643 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000644 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000645 Invalid = true;
646 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000647
Douglas Gregor10738d32010-12-23 23:51:58 +0000648 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000649 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000650 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
651 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000652 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000653 IsParameterPack, TInfo);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000654 if (Invalid)
655 Param->setInvalidDecl();
656
657 if (D.getIdentifier()) {
658 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000659 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000660 IdResolver.AddDecl(Param);
661 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000662
Douglas Gregor61c4d282011-01-05 15:48:55 +0000663 // C++0x [temp.param]p9:
664 // A default template-argument may be specified for any kind of
665 // template-parameter that is not a template parameter pack.
666 if (Default && IsParameterPack) {
667 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
668 Default = 0;
669 }
670
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000671 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000672 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000673 // Check for unexpanded parameter packs.
674 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
675 return Param;
676
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000677 TemplateArgument Converted;
678 if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) {
679 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000680 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000681 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000682
John McCall9ae2f072010-08-23 23:25:46 +0000683 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000684 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000685
John McCalld226f652010-08-21 09:40:31 +0000686 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000687}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000688
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000689/// ActOnTemplateTemplateParameter - Called when a C++ template template
690/// parameter (e.g. T in template <template <typename> class T> class array)
691/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000692Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
693 SourceLocation TmpLoc,
694 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000695 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000696 IdentifierInfo *Name,
697 SourceLocation NameLoc,
698 unsigned Depth,
699 unsigned Position,
700 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000701 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000702 assert(S->isTemplateParamScope() &&
703 "Template template parameter not in template parameter scope!");
704
705 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000706 bool IsParameterPack = EllipsisLoc.isValid();
707 // FIXME: Pack-ness is dropped
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000708 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000709 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000710 NameLoc.isInvalid()? TmpLoc : NameLoc,
711 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000712 Name, Params);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000713
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000714 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000715 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000716 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000717 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000718 IdResolver.AddDecl(Param);
719 }
720
Douglas Gregor6f526752010-12-16 08:48:57 +0000721 if (Params->size() == 0) {
722 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
723 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
724 Param->setInvalidDecl();
725 }
726
Douglas Gregor61c4d282011-01-05 15:48:55 +0000727 // C++0x [temp.param]p9:
728 // A default template-argument may be specified for any kind of
729 // template-parameter that is not a template parameter pack.
730 if (IsParameterPack && !Default.isInvalid()) {
731 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
732 Default = ParsedTemplateArgument();
733 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000734
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000735 if (!Default.isInvalid()) {
736 // Check only that we have a template template argument. We don't want to
737 // try to check well-formedness now, because our template template parameter
738 // might have dependent types in its template parameters, which we wouldn't
739 // be able to match now.
740 //
741 // If none of the template template parameter's template arguments mention
742 // other template parameters, we could actually perform more checking here.
743 // However, it isn't worth doing.
744 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
745 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
746 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
747 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000748 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000749 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000750
Douglas Gregor6f526752010-12-16 08:48:57 +0000751 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000752 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000753 DefaultArg.getArgument().getAsTemplate(),
754 UPPC_DefaultArgument))
755 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000756
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000757 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000758 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000759
John McCalld226f652010-08-21 09:40:31 +0000760 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000761}
762
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000763/// ActOnTemplateParameterList - Builds a TemplateParameterList that
764/// contains the template parameters in Params/NumParams.
765Sema::TemplateParamsTy *
766Sema::ActOnTemplateParameterList(unsigned Depth,
767 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000768 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000769 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000770 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000771 SourceLocation RAngleLoc) {
772 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000773 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000774
Douglas Gregorddc29e12009-02-06 22:42:48 +0000775 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000776 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000777 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000778}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000779
John McCallb6217662010-03-15 10:12:16 +0000780static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
781 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000782 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000783}
784
John McCallf312b1e2010-08-26 23:41:50 +0000785DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000786Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000787 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000788 IdentifierInfo *Name, SourceLocation NameLoc,
789 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000790 TemplateParameterList *TemplateParams,
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000791 AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000792 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000793 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000794 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000795 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000796
797 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000798 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000799 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000800
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000801 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
802 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000803
804 // There is no such thing as an unnamed class template.
805 if (!Name) {
806 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000807 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000808 }
809
810 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000811 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000812 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000813 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000814 if (SS.isNotEmpty() && !SS.isInvalid()) {
815 SemanticContext = computeDeclContext(SS, true);
816 if (!SemanticContext) {
817 // FIXME: Produce a reasonable diagnostic here
818 return true;
819 }
Mike Stump1eb44332009-09-09 15:08:12 +0000820
John McCall77bb1aa2010-05-01 00:40:08 +0000821 if (RequireCompleteDeclContext(SS, SemanticContext))
822 return true;
823
John McCalla24dc2e2009-11-17 02:14:36 +0000824 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000825 } else {
826 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000827 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000828 }
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Douglas Gregor57265e32010-04-12 16:00:01 +0000830 if (Previous.isAmbiguous())
831 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000832
Douglas Gregorddc29e12009-02-06 22:42:48 +0000833 NamedDecl *PrevDecl = 0;
834 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000835 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000836
Douglas Gregorddc29e12009-02-06 22:42:48 +0000837 // If there is a previous declaration with the same name, check
838 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000839 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000840 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000841
842 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000843 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000844 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000845 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000846 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
847 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000848 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000849 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
850 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
851 PrevClassTemplate
852 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
853 ->getSpecializedTemplate();
854 }
855 }
856
John McCall65c49462009-12-18 11:25:59 +0000857 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000858 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000859 // [...] When looking for a prior declaration of a class or a function
860 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000861 // function is neither a qualified name nor a template-id, scopes outside
862 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000863 if (!SS.isSet()) {
864 DeclContext *OutermostContext = CurContext;
865 while (!OutermostContext->isFileContext())
866 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000867
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000868 if (PrevDecl &&
869 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
870 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
871 SemanticContext = PrevDecl->getDeclContext();
872 } else {
873 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000874 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000875 // declaration.
876 PrevDecl = PrevClassTemplate = 0;
877 SemanticContext = OutermostContext;
878 }
John McCalle129d442009-12-17 23:21:11 +0000879 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000880
John McCalle129d442009-12-17 23:21:11 +0000881 if (CurContext->isDependentContext()) {
882 // If this is a dependent context, we don't want to link the friend
883 // class template to the template in scope, because that would perform
884 // checking of the template parameter lists that can't be performed
885 // until the outer context is instantiated.
886 PrevDecl = PrevClassTemplate = 0;
887 }
888 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
889 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000890
Douglas Gregorddc29e12009-02-06 22:42:48 +0000891 if (PrevClassTemplate) {
892 // Ensure that the template parameter lists are compatible.
893 if (!TemplateParameterListsAreEqual(TemplateParams,
894 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000895 /*Complain=*/true,
896 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000897 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000898
899 // C++ [temp.class]p4:
900 // In a redeclaration, partial specialization, explicit
901 // specialization or explicit instantiation of a class template,
902 // the class-key shall agree in kind with the original class
903 // template declaration (7.1.5.3).
904 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000905 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000906 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000907 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000908 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000909 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000910 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000911 }
912
Douglas Gregorddc29e12009-02-06 22:42:48 +0000913 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000914 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000915 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000916 Diag(NameLoc, diag::err_redefinition) << Name;
917 Diag(Def->getLocation(), diag::note_previous_definition);
918 // FIXME: Would it make sense to try to "forget" the previous
919 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000920 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000921 }
922 }
923 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
924 // Maybe we will complain about the shadowed template parameter.
925 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
926 // Just pretend that we didn't see the previous declaration.
927 PrevDecl = 0;
928 } else if (PrevDecl) {
929 // C++ [temp]p5:
930 // A class template shall not have the same name as any other
931 // template, class, function, object, enumeration, enumerator,
932 // namespace, or type in the same scope (3.3), except as specified
933 // in (14.5.4).
934 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
935 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000936 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000937 }
938
Douglas Gregord684b002009-02-10 19:49:53 +0000939 // Check the template parameter list of this declaration, possibly
940 // merging in the template parameter list from the previous class
941 // template declaration.
942 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000943 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000944 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000945 SemanticContext->isRecord() &&
946 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000947 ? TPC_ClassTemplateMember
948 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000949 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Douglas Gregor57265e32010-04-12 16:00:01 +0000951 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000952 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000953 // template out-of-line.
954 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
955 !(TUK == TUK_Friend && CurContext->isDependentContext()))
956 Diag(NameLoc, diag::err_member_def_does_not_match)
957 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000958 }
959
Mike Stump1eb44332009-09-09 15:08:12 +0000960 CXXRecordDecl *NewClass =
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000961 CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000962 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000963 PrevClassTemplate->getTemplatedDecl() : 0,
964 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000965 SetNestedNameSpecifier(NewClass, SS);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000966
967 ClassTemplateDecl *NewTemplate
968 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
969 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000970 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000971 NewClass->setDescribedClassTemplate(NewTemplate);
972
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000973 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000974 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000975 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000976 assert(T->isDependentType() && "Class template type is not dependent?");
977 (void)T;
978
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000979 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000980 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000981 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000982 PrevClassTemplate->getInstantiatedFromMemberTemplate())
983 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000984
Anders Carlsson4cbe82c2009-03-26 01:24:28 +0000985 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +0000986 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +0000987 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Douglas Gregorddc29e12009-02-06 22:42:48 +0000989 // Set the lexical context of these templates
990 NewClass->setLexicalDeclContext(CurContext);
991 NewTemplate->setLexicalDeclContext(CurContext);
992
John McCall0f434ec2009-07-31 02:45:11 +0000993 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +0000994 NewClass->startDefinition();
995
996 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000997 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000998
John McCall05b23ea2009-09-14 21:59:20 +0000999 if (TUK != TUK_Friend)
1000 PushOnScopeChains(NewTemplate, S);
1001 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001002 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001003 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001004 NewClass->setAccess(PrevClassTemplate->getAccess());
1005 }
John McCall05b23ea2009-09-14 21:59:20 +00001006
Douglas Gregord85bea22009-09-26 06:47:28 +00001007 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1008 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001009
John McCall05b23ea2009-09-14 21:59:20 +00001010 // Friend templates are visible in fairly strange ways.
1011 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001012 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001013 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1014 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1015 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001016 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001017 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001018
Douglas Gregord85bea22009-09-26 06:47:28 +00001019 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1020 NewClass->getLocation(),
1021 NewTemplate,
1022 /*FIXME:*/NewClass->getLocation());
1023 Friend->setAccess(AS_public);
1024 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001025 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001026
Douglas Gregord684b002009-02-10 19:49:53 +00001027 if (Invalid) {
1028 NewTemplate->setInvalidDecl();
1029 NewClass->setInvalidDecl();
1030 }
John McCalld226f652010-08-21 09:40:31 +00001031 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001032}
1033
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001034/// \brief Diagnose the presence of a default template argument on a
1035/// template parameter, which is ill-formed in certain contexts.
1036///
1037/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001038static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001039 Sema::TemplateParamListContext TPC,
1040 SourceLocation ParamLoc,
1041 SourceRange DefArgRange) {
1042 switch (TPC) {
1043 case Sema::TPC_ClassTemplate:
1044 return false;
1045
1046 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001047 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001048 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001049 // A default template-argument shall not be specified in a
1050 // function template declaration or a function template
1051 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001052 // If a friend function template declaration specifies a default
1053 // template-argument, that declaration shall be a definition and shall be
1054 // the only declaration of the function template in the translation unit.
1055 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001056 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001057 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001058 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001059 << DefArgRange;
1060 return false;
1061
1062 case Sema::TPC_ClassTemplateMember:
1063 // C++0x [temp.param]p9:
1064 // A default template-argument shall not be specified in the
1065 // template-parameter-lists of the definition of a member of a
1066 // class template that appears outside of the member's class.
1067 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1068 << DefArgRange;
1069 return true;
1070
1071 case Sema::TPC_FriendFunctionTemplate:
1072 // C++ [temp.param]p9:
1073 // A default template-argument shall not be specified in a
1074 // friend template declaration.
1075 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1076 << DefArgRange;
1077 return true;
1078
1079 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1080 // for friend function templates if there is only a single
1081 // declaration (and it is a definition). Strange!
1082 }
1083
1084 return false;
1085}
1086
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001087/// \brief Check for unexpanded parameter packs within the template parameters
1088/// of a template template parameter, recursively.
1089bool DiagnoseUnexpandedParameterPacks(Sema &S, TemplateTemplateParmDecl *TTP){
1090 TemplateParameterList *Params = TTP->getTemplateParameters();
1091 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1092 NamedDecl *P = Params->getParam(I);
1093 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001094 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001095 NTTP->getTypeSourceInfo(),
1096 Sema::UPPC_NonTypeTemplateParameterType))
1097 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001098
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001099 continue;
1100 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001101
1102 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001103 = dyn_cast<TemplateTemplateParmDecl>(P))
1104 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1105 return true;
1106 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001107
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001108 return false;
1109}
1110
Douglas Gregord684b002009-02-10 19:49:53 +00001111/// \brief Checks the validity of a template parameter list, possibly
1112/// considering the template parameter list from a previous
1113/// declaration.
1114///
1115/// If an "old" template parameter list is provided, it must be
1116/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1117/// template parameter list.
1118///
1119/// \param NewParams Template parameter list for a new template
1120/// declaration. This template parameter list will be updated with any
1121/// default arguments that are carried through from the previous
1122/// template parameter list.
1123///
1124/// \param OldParams If provided, template parameter list from a
1125/// previous declaration of the same template. Default template
1126/// arguments will be merged from the old template parameter list to
1127/// the new template parameter list.
1128///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001129/// \param TPC Describes the context in which we are checking the given
1130/// template parameter list.
1131///
Douglas Gregord684b002009-02-10 19:49:53 +00001132/// \returns true if an error occurred, false otherwise.
1133bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001134 TemplateParameterList *OldParams,
1135 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001136 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Douglas Gregord684b002009-02-10 19:49:53 +00001138 // C++ [temp.param]p10:
1139 // The set of default template-arguments available for use with a
1140 // template declaration or definition is obtained by merging the
1141 // default arguments from the definition (if in scope) and all
1142 // declarations in scope in the same way default function
1143 // arguments are (8.3.6).
1144 bool SawDefaultArgument = false;
1145 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001146
Anders Carlsson49d25572009-06-12 23:20:15 +00001147 bool SawParameterPack = false;
1148 SourceLocation ParameterPackLoc;
1149
Mike Stump1a35fde2009-02-11 23:03:27 +00001150 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001151 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001152 if (OldParams)
1153 OldParam = OldParams->begin();
1154
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001155 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001156 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1157 NewParamEnd = NewParams->end();
1158 NewParam != NewParamEnd; ++NewParam) {
1159 // Variables used to diagnose redundant default arguments
1160 bool RedundantDefaultArg = false;
1161 SourceLocation OldDefaultLoc;
1162 SourceLocation NewDefaultLoc;
1163
1164 // Variables used to diagnose missing default arguments
1165 bool MissingDefaultArg = false;
1166
Anders Carlsson49d25572009-06-12 23:20:15 +00001167 // C++0x [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001168 // If a template parameter of a primary class template is a template
Douglas Gregor1ed64762011-01-05 16:19:19 +00001169 // parameter pack, it shall be the last template parameter.
1170 if (SawParameterPack && TPC == TPC_ClassTemplate) {
Mike Stump1eb44332009-09-09 15:08:12 +00001171 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001172 diag::err_template_param_pack_must_be_last_template_parameter);
1173 Invalid = true;
1174 }
1175
Douglas Gregord684b002009-02-10 19:49:53 +00001176 if (TemplateTypeParmDecl *NewTypeParm
1177 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001178 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001179 if (NewTypeParm->hasDefaultArgument() &&
1180 DiagnoseDefaultTemplateArgument(*this, TPC,
1181 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001182 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001183 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001184 NewTypeParm->removeDefaultArgument();
1185
1186 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001187 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001188 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Anders Carlsson49d25572009-06-12 23:20:15 +00001190 if (NewTypeParm->isParameterPack()) {
1191 assert(!NewTypeParm->hasDefaultArgument() &&
1192 "Parameter packs can't have a default argument!");
1193 SawParameterPack = true;
1194 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001195 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001196 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001197 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1198 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1199 SawDefaultArgument = true;
1200 RedundantDefaultArg = true;
1201 PreviousDefaultArgLoc = NewDefaultLoc;
1202 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1203 // Merge the default argument from the old declaration to the
1204 // new declaration.
1205 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001206 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001207 true);
1208 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1209 } else if (NewTypeParm->hasDefaultArgument()) {
1210 SawDefaultArgument = true;
1211 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1212 } else if (SawDefaultArgument)
1213 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001214 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001215 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001216 // Check for unexpanded parameter packs.
1217 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001218 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001219 UPPC_NonTypeTemplateParameterType)) {
1220 Invalid = true;
1221 continue;
1222 }
1223
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001224 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001225 if (NewNonTypeParm->hasDefaultArgument() &&
1226 DiagnoseDefaultTemplateArgument(*this, TPC,
1227 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001228 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001229 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001230 }
1231
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001232 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001233 NonTypeTemplateParmDecl *OldNonTypeParm
1234 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001235 if (NewNonTypeParm->isParameterPack()) {
1236 assert(!NewNonTypeParm->hasDefaultArgument() &&
1237 "Parameter packs can't have a default argument!");
1238 SawParameterPack = true;
1239 ParameterPackLoc = NewNonTypeParm->getLocation();
1240 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001241 NewNonTypeParm->hasDefaultArgument()) {
1242 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1243 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1244 SawDefaultArgument = true;
1245 RedundantDefaultArg = true;
1246 PreviousDefaultArgLoc = NewDefaultLoc;
1247 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1248 // Merge the default argument from the old declaration to the
1249 // new declaration.
1250 SawDefaultArgument = true;
1251 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001252 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001253 // parameter.
1254 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001255 OldNonTypeParm->getDefaultArgument(),
1256 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001257 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1258 } else if (NewNonTypeParm->hasDefaultArgument()) {
1259 SawDefaultArgument = true;
1260 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1261 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001262 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001263 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001264 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001265 TemplateTemplateParmDecl *NewTemplateParm
1266 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001267
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001268 // Check for unexpanded parameter packs, recursively.
1269 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1270 Invalid = true;
1271 continue;
1272 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001273
1274 if (NewTemplateParm->hasDefaultArgument() &&
1275 DiagnoseDefaultTemplateArgument(*this, TPC,
1276 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001277 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001278 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001279
1280 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001281 TemplateTemplateParmDecl *OldTemplateParm
1282 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001283 if (NewTemplateParm->isParameterPack()) {
1284 assert(!NewTemplateParm->hasDefaultArgument() &&
1285 "Parameter packs can't have a default argument!");
1286 SawParameterPack = true;
1287 ParameterPackLoc = NewTemplateParm->getLocation();
1288 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001289 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001290 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1291 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001292 SawDefaultArgument = true;
1293 RedundantDefaultArg = true;
1294 PreviousDefaultArgLoc = NewDefaultLoc;
1295 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1296 // Merge the default argument from the old declaration to the
1297 // new declaration.
1298 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001299 // FIXME: We need to create a new kind of "default argument" expression
1300 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001301 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001302 OldTemplateParm->getDefaultArgument(),
1303 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001304 PreviousDefaultArgLoc
1305 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001306 } else if (NewTemplateParm->hasDefaultArgument()) {
1307 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001308 PreviousDefaultArgLoc
1309 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001310 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001311 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001312 }
1313
1314 if (RedundantDefaultArg) {
1315 // C++ [temp.param]p12:
1316 // A template-parameter shall not be given default arguments
1317 // by two different declarations in the same scope.
1318 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1319 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1320 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001321 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001322 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001323 // If a template-parameter of a class template has a default
1324 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001325 // have a default template-argument supplied or be a template parameter
1326 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001327 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001328 diag::err_template_param_default_arg_missing);
1329 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1330 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001331 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001332 }
1333
1334 // If we have an old template parameter list that we're merging
1335 // in, move on to the next parameter.
1336 if (OldParams)
1337 ++OldParam;
1338 }
1339
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001340 // We were missing some default arguments at the end of the list, so remove
1341 // all of the default arguments.
1342 if (RemoveDefaultArguments) {
1343 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1344 NewParamEnd = NewParams->end();
1345 NewParam != NewParamEnd; ++NewParam) {
1346 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1347 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001348 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001349 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1350 NTTP->removeDefaultArgument();
1351 else
1352 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1353 }
1354 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001355
Douglas Gregord684b002009-02-10 19:49:53 +00001356 return Invalid;
1357}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001358
John McCall4e2cbb22010-10-20 05:44:58 +00001359namespace {
1360
1361/// A class which looks for a use of a certain level of template
1362/// parameter.
1363struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1364 typedef RecursiveASTVisitor<DependencyChecker> super;
1365
1366 unsigned Depth;
1367 bool Match;
1368
1369 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1370 NamedDecl *ND = Params->getParam(0);
1371 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1372 Depth = PD->getDepth();
1373 } else if (NonTypeTemplateParmDecl *PD =
1374 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1375 Depth = PD->getDepth();
1376 } else {
1377 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1378 }
1379 }
1380
1381 bool Matches(unsigned ParmDepth) {
1382 if (ParmDepth >= Depth) {
1383 Match = true;
1384 return true;
1385 }
1386 return false;
1387 }
1388
1389 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1390 return !Matches(T->getDepth());
1391 }
1392
1393 bool TraverseTemplateName(TemplateName N) {
1394 if (TemplateTemplateParmDecl *PD =
1395 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1396 if (Matches(PD->getDepth())) return false;
1397 return super::TraverseTemplateName(N);
1398 }
1399
1400 bool VisitDeclRefExpr(DeclRefExpr *E) {
1401 if (NonTypeTemplateParmDecl *PD =
1402 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1403 if (PD->getDepth() == Depth) {
1404 Match = true;
1405 return false;
1406 }
1407 }
1408 return super::VisitDeclRefExpr(E);
1409 }
1410};
1411}
1412
1413/// Determines whether a template-id depends on the given parameter
1414/// list.
1415static bool
1416DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId,
1417 TemplateParameterList *Params) {
1418 DependencyChecker Checker(Params);
1419 Checker.TraverseType(QualType(TemplateId, 0));
1420 return Checker.Match;
1421}
1422
Mike Stump1eb44332009-09-09 15:08:12 +00001423/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001424/// specifier, returning the template parameter list that applies to the
1425/// name.
1426///
1427/// \param DeclStartLoc the start of the declaration that has a scope
1428/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001429///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001430/// \param SS the scope specifier that will be matched to the given template
1431/// parameter lists. This scope specifier precedes a qualified name that is
1432/// being declared.
1433///
1434/// \param ParamLists the template parameter lists, from the outermost to the
1435/// innermost template parameter lists.
1436///
1437/// \param NumParamLists the number of template parameter lists in ParamLists.
1438///
John McCall77e8b112010-04-13 20:37:33 +00001439/// \param IsFriend Whether to apply the slightly different rules for
1440/// matching template parameters to scope specifiers in friend
1441/// declarations.
1442///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001443/// \param IsExplicitSpecialization will be set true if the entity being
1444/// declared is an explicit specialization, false otherwise.
1445///
Mike Stump1eb44332009-09-09 15:08:12 +00001446/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001447/// name that is preceded by the scope specifier @p SS. This template
1448/// parameter list may be have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001449/// template) or may have no template parameters (if we're declaring a
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001450/// template specialization), or may be NULL (if we were's declaring isn't
1451/// itself a template).
1452TemplateParameterList *
1453Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
1454 const CXXScopeSpec &SS,
1455 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001456 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001457 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001458 bool &IsExplicitSpecialization,
1459 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001460 IsExplicitSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001461
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001462 // Find the template-ids that occur within the nested-name-specifier. These
1463 // template-ids will match up with the template parameter lists.
1464 llvm::SmallVector<const TemplateSpecializationType *, 4>
1465 TemplateIdsInSpecifier;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001466 llvm::SmallVector<ClassTemplateSpecializationDecl *, 4>
1467 ExplicitSpecializationsInSpecifier;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001468 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
1469 NNS; NNS = NNS->getPrefix()) {
John McCall4b2b02b2009-12-15 02:19:47 +00001470 const Type *T = NNS->getAsType();
1471 if (!T) break;
1472
1473 // C++0x [temp.expl.spec]p17:
1474 // A member or a member template may be nested within many
1475 // enclosing class templates. In an explicit specialization for
1476 // such a member, the member declaration shall be preceded by a
1477 // template<> for each enclosing class template that is
1478 // explicitly specialized.
Douglas Gregorfe331062010-02-13 05:23:25 +00001479 //
1480 // Following the existing practice of GNU and EDG, we allow a typedef of a
1481 // template specialization type.
John McCall49f4e1c2010-12-10 11:01:00 +00001482 while (const TypedefType *TT = dyn_cast<TypedefType>(T))
1483 T = TT->getDecl()->getUnderlyingType().getTypePtr();
John McCall4b2b02b2009-12-15 02:19:47 +00001484
Mike Stump1eb44332009-09-09 15:08:12 +00001485 if (const TemplateSpecializationType *SpecType
Douglas Gregorfe331062010-02-13 05:23:25 +00001486 = dyn_cast<TemplateSpecializationType>(T)) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001487 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
1488 if (!Template)
1489 continue; // FIXME: should this be an error? probably...
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Ted Kremenek6217b802009-07-29 21:53:49 +00001491 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001492 ClassTemplateSpecializationDecl *SpecDecl
1493 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
1494 // If the nested name specifier refers to an explicit specialization,
1495 // we don't need a template<> header.
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001496 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
1497 ExplicitSpecializationsInSpecifier.push_back(SpecDecl);
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001498 continue;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001499 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001500 }
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001502 TemplateIdsInSpecifier.push_back(SpecType);
1503 }
1504 }
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001506 // Reverse the list of template-ids in the scope specifier, so that we can
1507 // more easily match up the template-ids and the template parameter lists.
1508 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001510 SourceLocation FirstTemplateLoc = DeclStartLoc;
1511 if (NumParamLists)
1512 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001514 // Match the template-ids found in the specifier to the template parameter
1515 // lists.
John McCall4e2cbb22010-10-20 05:44:58 +00001516 unsigned ParamIdx = 0, TemplateIdx = 0;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001517 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
John McCall4e2cbb22010-10-20 05:44:58 +00001518 TemplateIdx != NumTemplateIds; ++TemplateIdx) {
1519 const TemplateSpecializationType *TemplateId
1520 = TemplateIdsInSpecifier[TemplateIdx];
Douglas Gregorb88e8882009-07-30 17:40:51 +00001521 bool DependentTemplateId = TemplateId->isDependentType();
John McCall4e2cbb22010-10-20 05:44:58 +00001522
1523 // In friend declarations we can have template-ids which don't
1524 // depend on the corresponding template parameter lists. But
1525 // assume that empty parameter lists are supposed to match this
1526 // template-id.
1527 if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) {
1528 if (!DependentTemplateId ||
1529 !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx]))
1530 continue;
1531 }
1532
1533 if (ParamIdx >= NumParamLists) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001534 // We have a template-id without a corresponding template parameter
1535 // list.
John McCall77e8b112010-04-13 20:37:33 +00001536
1537 // ...which is fine if this is a friend declaration.
1538 if (IsFriend) {
1539 IsExplicitSpecialization = true;
1540 break;
1541 }
1542
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001543 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +00001544 // FIXME: the location information here isn't great.
1545 Diag(SS.getRange().getBegin(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001546 diag::err_template_spec_needs_template_parameters)
John McCall4e2cbb22010-10-20 05:44:58 +00001547 << QualType(TemplateId, 0)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001548 << SS.getRange();
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001549 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001550 } else {
1551 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
1552 << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +00001553 << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001554 IsExplicitSpecialization = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001555 }
1556 return 0;
1557 }
Mike Stump1eb44332009-09-09 15:08:12 +00001558
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001559 // Check the template parameter list against its corresponding template-id.
Douglas Gregorb88e8882009-07-30 17:40:51 +00001560 if (DependentTemplateId) {
John McCall31f17ec2010-04-27 00:57:59 +00001561 TemplateParameterList *ExpectedTemplateParams = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00001562
John McCall31f17ec2010-04-27 00:57:59 +00001563 // Are there cases in (e.g.) friends where this won't match?
1564 if (const InjectedClassNameType *Injected
1565 = TemplateId->getAs<InjectedClassNameType>()) {
1566 CXXRecordDecl *Record = Injected->getDecl();
1567 if (ClassTemplatePartialSpecializationDecl *Partial =
1568 dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
1569 ExpectedTemplateParams = Partial->getTemplateParameters();
1570 else
1571 ExpectedTemplateParams = Record->getDescribedClassTemplate()
1572 ->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001573 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001574
John McCall31f17ec2010-04-27 00:57:59 +00001575 if (ExpectedTemplateParams)
John McCall4e2cbb22010-10-20 05:44:58 +00001576 TemplateParameterListsAreEqual(ParamLists[ParamIdx],
John McCall31f17ec2010-04-27 00:57:59 +00001577 ExpectedTemplateParams,
1578 true, TPL_TemplateMatch);
1579
John McCall4e2cbb22010-10-20 05:44:58 +00001580 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1581 TPC_ClassTemplateMember);
1582 } else if (ParamLists[ParamIdx]->size() > 0)
1583 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00001584 diag::err_template_param_list_matches_nontemplate)
1585 << TemplateId
John McCall4e2cbb22010-10-20 05:44:58 +00001586 << ParamLists[ParamIdx]->getSourceRange();
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001587 else
1588 IsExplicitSpecialization = true;
John McCall4e2cbb22010-10-20 05:44:58 +00001589
1590 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001591 }
Mike Stump1eb44332009-09-09 15:08:12 +00001592
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001593 // If there were at least as many template-ids as there were template
1594 // parameter lists, then there are no template parameter lists remaining for
1595 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001596 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001597 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001599 // If there were too many template parameter lists, complain about that now.
John McCall4e2cbb22010-10-20 05:44:58 +00001600 if (ParamIdx != NumParamLists - 1) {
1601 while (ParamIdx < NumParamLists - 1) {
1602 bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0;
1603 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001604 isExplicitSpecHeader? diag::warn_template_spec_extra_headers
1605 : diag::err_template_spec_extra_headers)
John McCall4e2cbb22010-10-20 05:44:58 +00001606 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1607 ParamLists[ParamIdx]->getRAngleLoc());
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001608
1609 if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) {
1610 Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(),
1611 diag::note_explicit_template_spec_does_not_need_header)
1612 << ExplicitSpecializationsInSpecifier.back();
1613 ExplicitSpecializationsInSpecifier.pop_back();
1614 }
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001615
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001616 // We have a template parameter list with no corresponding scope, which
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001617 // means that the resulting template declaration can't be instantiated
1618 // properly (we'll end up with dependent nodes when we shouldn't).
1619 if (!isExplicitSpecHeader)
1620 Invalid = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001621
John McCall4e2cbb22010-10-20 05:44:58 +00001622 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001623 }
1624 }
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001626 // Return the last template parameter list, which corresponds to the
1627 // entity being declared.
1628 return ParamLists[NumParamLists - 1];
1629}
1630
Douglas Gregor7532dc62009-03-30 22:58:21 +00001631QualType Sema::CheckTemplateIdType(TemplateName Name,
1632 SourceLocation TemplateLoc,
John McCalld5532b62009-11-23 01:53:49 +00001633 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001634 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001635 if (!Template) {
1636 // The template name does not resolve to a template, so we just
1637 // build a dependent template-id type.
John McCalld5532b62009-11-23 01:53:49 +00001638 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001639 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001640
Douglas Gregor40808ce2009-03-09 23:48:35 +00001641 // Check that the template argument list is well-formed for this
1642 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001643 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001644 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001645 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001646 return QualType();
1647
Douglas Gregor910f8002010-11-07 23:05:16 +00001648 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001649 "Converted template argument list is too short!");
1650
1651 QualType CanonType;
1652
Douglas Gregorcaddba02009-11-12 18:38:13 +00001653 if (Name.isDependent() ||
1654 TemplateSpecializationType::anyDependentTemplateArguments(
John McCalld5532b62009-11-23 01:53:49 +00001655 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001656 // This class template specialization is a dependent
1657 // type. Therefore, its canonical type is another class template
1658 // specialization type that contains all of the converted
1659 // arguments in canonical form. This ensures that, e.g., A<T> and
1660 // A<T, T> have identical types when A is declared as:
1661 //
1662 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001663 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001664 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001665 Converted.data(),
1666 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001667
Douglas Gregor1275ae02009-07-28 23:00:59 +00001668 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001669 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001670 // In the future, we need to teach getTemplateSpecializationType to only
1671 // build the canonical type and return that to us.
1672 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001673
1674 // This might work out to be a current instantiation, in which
1675 // case the canonical type needs to be the InjectedClassNameType.
1676 //
1677 // TODO: in theory this could be a simple hashtable lookup; most
1678 // changes to CurContext don't change the set of current
1679 // instantiations.
1680 if (isa<ClassTemplateDecl>(Template)) {
1681 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1682 // If we get out to a namespace, we're done.
1683 if (Ctx->isFileContext()) break;
1684
1685 // If this isn't a record, keep looking.
1686 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1687 if (!Record) continue;
1688
1689 // Look for one of the two cases with InjectedClassNameTypes
1690 // and check whether it's the same template.
1691 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1692 !Record->getDescribedClassTemplate())
1693 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001694
John McCall31f17ec2010-04-27 00:57:59 +00001695 // Fetch the injected class name type and check whether its
1696 // injected type is equal to the type we just built.
1697 QualType ICNT = Context.getTypeDeclType(Record);
1698 QualType Injected = cast<InjectedClassNameType>(ICNT)
1699 ->getInjectedSpecializationType();
1700
1701 if (CanonType != Injected->getCanonicalTypeInternal())
1702 continue;
1703
1704 // If so, the canonical type of this TST is the injected
1705 // class name type of the record we just found.
1706 assert(ICNT.isCanonical());
1707 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001708 break;
1709 }
1710 }
Mike Stump1eb44332009-09-09 15:08:12 +00001711 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001712 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001713 // Find the class template specialization declaration that
1714 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001715 void *InsertPos = 0;
1716 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001717 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001718 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001719 if (!Decl) {
1720 // This is the first time we have referenced this class template
1721 // specialization. Create the canonical declaration and add it to
1722 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001723 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001724 ClassTemplate->getTemplatedDecl()->getTagKind(),
1725 ClassTemplate->getDeclContext(),
1726 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001727 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001728 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001729 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001730 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001731 Decl->setLexicalDeclContext(CurContext);
1732 }
1733
1734 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00001735 assert(isa<RecordType>(CanonType) &&
1736 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00001737 }
Mike Stump1eb44332009-09-09 15:08:12 +00001738
Douglas Gregor40808ce2009-03-09 23:48:35 +00001739 // Build the fully-sugared type for this class template
1740 // specialization, which refers back to the class template
1741 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00001742 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001743}
1744
John McCallf312b1e2010-08-26 23:41:50 +00001745TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00001746Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
1747 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001748 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001749 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00001750 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00001751 if (SS.isInvalid())
1752 return true;
1753
Douglas Gregor7532dc62009-03-30 22:58:21 +00001754 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001755
Douglas Gregor40808ce2009-03-09 23:48:35 +00001756 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00001757 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00001758 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001759
Douglas Gregora88f09f2011-02-28 17:23:35 +00001760 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1761 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
1762 DTN->getQualifier(),
1763 DTN->getIdentifier(),
1764 TemplateArgs);
1765
1766 // Build type-source information.
1767 TypeLocBuilder TLB;
1768 DependentTemplateSpecializationTypeLoc SpecTL
1769 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00001770 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00001771 SpecTL.setNameLoc(TemplateLoc);
1772 SpecTL.setLAngleLoc(LAngleLoc);
1773 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00001774 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00001775 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1776 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1777 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1778 }
1779
John McCalld5532b62009-11-23 01:53:49 +00001780 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001781 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00001782
1783 if (Result.isNull())
1784 return true;
1785
Douglas Gregor059101f2011-03-02 00:47:37 +00001786 // Build type-source information.
1787 TypeLocBuilder TLB;
1788 TemplateSpecializationTypeLoc SpecTL
1789 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1790 SpecTL.setTemplateNameLoc(TemplateLoc);
1791 SpecTL.setLAngleLoc(LAngleLoc);
1792 SpecTL.setRAngleLoc(RAngleLoc);
1793 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1794 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00001795
Douglas Gregor059101f2011-03-02 00:47:37 +00001796 if (SS.isNotEmpty()) {
1797 // Create an elaborated-type-specifier containing the nested-name-specifier.
1798 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
1799 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1800 ElabTL.setKeywordLoc(SourceLocation());
1801 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1802 }
1803
1804 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00001805}
John McCallf1bbbb42009-09-04 01:14:41 +00001806
Douglas Gregor059101f2011-03-02 00:47:37 +00001807TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00001808 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00001809 SourceLocation TagLoc,
1810 CXXScopeSpec &SS,
1811 TemplateTy TemplateD,
1812 SourceLocation TemplateLoc,
1813 SourceLocation LAngleLoc,
1814 ASTTemplateArgsPtr TemplateArgsIn,
1815 SourceLocation RAngleLoc) {
1816 TemplateName Template = TemplateD.getAsVal<TemplateName>();
1817
1818 // Translate the parser's template argument list in our AST format.
1819 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
1820 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
1821
1822 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001823 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00001824 ElaboratedTypeKeyword Keyword
1825 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00001826
Douglas Gregor059101f2011-03-02 00:47:37 +00001827 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1828 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
1829 DTN->getQualifier(),
1830 DTN->getIdentifier(),
1831 TemplateArgs);
1832
1833 // Build type-source information.
1834 TypeLocBuilder TLB;
1835 DependentTemplateSpecializationTypeLoc SpecTL
1836 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
1837 SpecTL.setKeywordLoc(TagLoc);
1838 SpecTL.setNameLoc(TemplateLoc);
1839 SpecTL.setLAngleLoc(LAngleLoc);
1840 SpecTL.setRAngleLoc(RAngleLoc);
1841 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
1842 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1843 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1844 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1845 }
1846
1847 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
1848 if (Result.isNull())
1849 return TypeResult();
1850
1851 // Check the tag kind
1852 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00001853 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00001854
John McCall6b2becf2009-09-08 17:47:29 +00001855 IdentifierInfo *Id = D->getIdentifier();
1856 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00001857
John McCall6b2becf2009-09-08 17:47:29 +00001858 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1859 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00001860 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00001861 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00001862 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00001863 }
1864 }
Douglas Gregor059101f2011-03-02 00:47:37 +00001865
1866 // Provide source-location information for the template specialization.
1867 TypeLocBuilder TLB;
1868 TemplateSpecializationTypeLoc SpecTL
1869 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1870 SpecTL.setTemplateNameLoc(TemplateLoc);
1871 SpecTL.setLAngleLoc(LAngleLoc);
1872 SpecTL.setRAngleLoc(RAngleLoc);
1873 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1874 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00001875
Douglas Gregor059101f2011-03-02 00:47:37 +00001876 // Construct an elaborated type containing the nested-name-specifier (if any)
1877 // and keyword.
1878 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
1879 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1880 ElabTL.setKeywordLoc(TagLoc);
1881 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1882 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00001883}
1884
John McCall60d7b3a2010-08-24 06:29:42 +00001885ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001886 LookupResult &R,
1887 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00001888 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001889 // FIXME: Can we do any checking at this point? I guess we could check the
1890 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00001891 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001892 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001893 // foo<int> could identify a single function unambiguously
1894 // This approach does NOT work, since f<int>(1);
1895 // gets resolved prior to resorting to overload resolution
1896 // i.e., template<class T> void f(double);
1897 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00001898
1899 // These should be filtered out by our callers.
1900 assert(!R.empty() && "empty lookup results when building templateid");
1901 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
1902
John McCallc373d482010-01-27 01:50:18 +00001903 // We don't want lookup warnings at this point.
1904 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001905
John McCallf7a1a742009-11-24 19:00:30 +00001906 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001907 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00001908 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00001909 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001910 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001911 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00001912
1913 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001914}
1915
John McCallf7a1a742009-11-24 19:00:30 +00001916// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00001917ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00001918Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00001919 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00001920 const TemplateArgumentListInfo &TemplateArgs) {
1921 DeclContext *DC;
1922 if (!(DC = computeDeclContext(SS, false)) ||
1923 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00001924 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00001925 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001926
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001927 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00001928 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001929 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
1930 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00001931
John McCallf7a1a742009-11-24 19:00:30 +00001932 if (R.isAmbiguous())
1933 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001934
John McCallf7a1a742009-11-24 19:00:30 +00001935 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001936 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
1937 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001938 return ExprError();
1939 }
1940
1941 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001942 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
1943 << (NestedNameSpecifier*) SS.getScopeRep()
1944 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001945 Diag(Temp->getLocation(), diag::note_referenced_class_template);
1946 return ExprError();
1947 }
1948
1949 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001950}
1951
Douglas Gregorc45c2322009-03-31 00:43:58 +00001952/// \brief Form a dependent template name.
1953///
1954/// This action forms a dependent template name given the template
1955/// name and its (presumably dependent) scope specifier. For
1956/// example, given "MetaFun::template apply", the scope specifier \p
1957/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
1958/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001959TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001960 SourceLocation TemplateKWLoc,
1961 CXXScopeSpec &SS,
1962 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00001963 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001964 bool EnteringContext,
1965 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00001966 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
1967 !getLangOptions().CPlusPlus0x)
1968 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001969 << FixItHint::CreateRemoval(TemplateKWLoc);
1970
Douglas Gregor0707bc52010-01-19 16:01:07 +00001971 DeclContext *LookupCtx = 0;
1972 if (SS.isSet())
1973 LookupCtx = computeDeclContext(SS, EnteringContext);
1974 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00001975 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00001976 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00001977 // C++0x [temp.names]p5:
1978 // If a name prefixed by the keyword template is not the name of
1979 // a template, the program is ill-formed. [Note: the keyword
1980 // template may not be applied to non-template members of class
1981 // templates. -end note ] [ Note: as is the case with the
1982 // typename prefix, the template prefix is allowed in cases
1983 // where it is not strictly necessary; i.e., when the
1984 // nested-name-specifier or the expression on the left of the ->
1985 // or . is not dependent on a template-parameter, or the use
1986 // does not appear in the scope of a template. -end note]
1987 //
1988 // Note: C++03 was more strict here, because it banned the use of
1989 // the "template" keyword prior to a template-name that was not a
1990 // dependent name. C++ DR468 relaxed this requirement (the
1991 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00001992 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001993 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001994 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
1995 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001996 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00001997 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
1998 isa<CXXRecordDecl>(LookupCtx) &&
1999 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002000 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002001 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002002 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002003 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002004 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002005 << Name.getSourceRange()
2006 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002007 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002008 } else {
2009 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002010 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002011 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002012 }
2013
Mike Stump1eb44332009-09-09 15:08:12 +00002014 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002015 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002016
Douglas Gregor014e88d2009-11-03 23:16:33 +00002017 switch (Name.getKind()) {
2018 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002019 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002020 Name.Identifier));
2021 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002022
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002023 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002024 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002025 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002026 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002027
2028 case UnqualifiedId::IK_LiteralOperatorId:
2029 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
2030
Douglas Gregor014e88d2009-11-03 23:16:33 +00002031 default:
2032 break;
2033 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002034
2035 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002036 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002037 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002038 << Name.getSourceRange()
2039 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002040 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002041}
2042
Mike Stump1eb44332009-09-09 15:08:12 +00002043bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002044 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00002045 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002046 const TemplateArgument &Arg = AL.getArgument();
2047
Anders Carlsson436b1562009-06-13 00:33:33 +00002048 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002049 switch(Arg.getKind()) {
2050 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002051 // C++ [temp.arg.type]p1:
2052 // A template-argument for a template-parameter which is a
2053 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002054 break;
2055 case TemplateArgument::Template: {
2056 // We have a template type parameter but the template argument
2057 // is a template without any arguments.
2058 SourceRange SR = AL.getSourceRange();
2059 TemplateName Name = Arg.getAsTemplate();
2060 Diag(SR.getBegin(), diag::err_template_missing_args)
2061 << Name << SR;
2062 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2063 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002064
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002065 return true;
2066 }
2067 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002068 // We have a template type parameter but the template argument
2069 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002070 SourceRange SR = AL.getSourceRange();
2071 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002072 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002073
Anders Carlsson436b1562009-06-13 00:33:33 +00002074 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002075 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002076 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002077
John McCalla93c9342009-12-07 02:54:59 +00002078 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002079 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002080
Anders Carlsson436b1562009-06-13 00:33:33 +00002081 // Add the converted template type argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002082 Converted.push_back(
John McCall833ca992009-10-29 08:12:44 +00002083 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00002084 return false;
2085}
2086
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002087/// \brief Substitute template arguments into the default template argument for
2088/// the given template type parameter.
2089///
2090/// \param SemaRef the semantic analysis object for which we are performing
2091/// the substitution.
2092///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002093/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002094/// for.
2095///
2096/// \param TemplateLoc the location of the template name that started the
2097/// template-id we are checking.
2098///
2099/// \param RAngleLoc the location of the right angle bracket ('>') that
2100/// terminates the template-id.
2101///
2102/// \param Param the template template parameter whose default we are
2103/// substituting into.
2104///
2105/// \param Converted the list of template arguments provided for template
2106/// parameters that precede \p Param in the template parameter list.
2107///
2108/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002109static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002110SubstDefaultTemplateArgument(Sema &SemaRef,
2111 TemplateDecl *Template,
2112 SourceLocation TemplateLoc,
2113 SourceLocation RAngleLoc,
2114 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002115 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002116 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002117
2118 // If the argument type is dependent, instantiate it now based
2119 // on the previously-computed template arguments.
2120 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002121 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002122 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002123
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002124 MultiLevelTemplateArgumentList AllTemplateArgs
2125 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2126
2127 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002128 Template, Converted.data(),
2129 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002130 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002131
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002132 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2133 Param->getDefaultArgumentLoc(),
2134 Param->getDeclName());
2135 }
2136
2137 return ArgType;
2138}
2139
2140/// \brief Substitute template arguments into the default template argument for
2141/// the given non-type template parameter.
2142///
2143/// \param SemaRef the semantic analysis object for which we are performing
2144/// the substitution.
2145///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002146/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002147/// for.
2148///
2149/// \param TemplateLoc the location of the template name that started the
2150/// template-id we are checking.
2151///
2152/// \param RAngleLoc the location of the right angle bracket ('>') that
2153/// terminates the template-id.
2154///
Douglas Gregor788cd062009-11-11 01:00:40 +00002155/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002156/// substituting into.
2157///
2158/// \param Converted the list of template arguments provided for template
2159/// parameters that precede \p Param in the template parameter list.
2160///
2161/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002162static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002163SubstDefaultTemplateArgument(Sema &SemaRef,
2164 TemplateDecl *Template,
2165 SourceLocation TemplateLoc,
2166 SourceLocation RAngleLoc,
2167 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002168 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002169 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002170 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002171
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002172 MultiLevelTemplateArgumentList AllTemplateArgs
2173 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002174
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002175 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002176 Template, Converted.data(),
2177 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002178 SourceRange(TemplateLoc, RAngleLoc));
2179
2180 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2181}
2182
Douglas Gregor788cd062009-11-11 01:00:40 +00002183/// \brief Substitute template arguments into the default template argument for
2184/// the given template template parameter.
2185///
2186/// \param SemaRef the semantic analysis object for which we are performing
2187/// the substitution.
2188///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002189/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002190/// for.
2191///
2192/// \param TemplateLoc the location of the template name that started the
2193/// template-id we are checking.
2194///
2195/// \param RAngleLoc the location of the right angle bracket ('>') that
2196/// terminates the template-id.
2197///
2198/// \param Param the template template parameter whose default we are
2199/// substituting into.
2200///
2201/// \param Converted the list of template arguments provided for template
2202/// parameters that precede \p Param in the template parameter list.
2203///
2204/// \returns the substituted template argument, or NULL if an error occurred.
2205static TemplateName
2206SubstDefaultTemplateArgument(Sema &SemaRef,
2207 TemplateDecl *Template,
2208 SourceLocation TemplateLoc,
2209 SourceLocation RAngleLoc,
2210 TemplateTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002211 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002212 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002213 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002214
Douglas Gregor788cd062009-11-11 01:00:40 +00002215 MultiLevelTemplateArgumentList AllTemplateArgs
2216 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002217
Douglas Gregor788cd062009-11-11 01:00:40 +00002218 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002219 Template, Converted.data(),
2220 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002221 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002222
Douglas Gregor788cd062009-11-11 01:00:40 +00002223 return SemaRef.SubstTemplateName(
2224 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002225 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002226 AllTemplateArgs);
2227}
2228
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002229/// \brief If the given template parameter has a default template
2230/// argument, substitute into that default template argument and
2231/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002232TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002233Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2234 SourceLocation TemplateLoc,
2235 SourceLocation RAngleLoc,
2236 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002237 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2238 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002239 if (!TypeParm->hasDefaultArgument())
2240 return TemplateArgumentLoc();
2241
John McCalla93c9342009-12-07 02:54:59 +00002242 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002243 TemplateLoc,
2244 RAngleLoc,
2245 TypeParm,
2246 Converted);
2247 if (DI)
2248 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2249
2250 return TemplateArgumentLoc();
2251 }
2252
2253 if (NonTypeTemplateParmDecl *NonTypeParm
2254 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2255 if (!NonTypeParm->hasDefaultArgument())
2256 return TemplateArgumentLoc();
2257
John McCall60d7b3a2010-08-24 06:29:42 +00002258 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002259 TemplateLoc,
2260 RAngleLoc,
2261 NonTypeParm,
2262 Converted);
2263 if (Arg.isInvalid())
2264 return TemplateArgumentLoc();
2265
2266 Expr *ArgE = Arg.takeAs<Expr>();
2267 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2268 }
2269
2270 TemplateTemplateParmDecl *TempTempParm
2271 = cast<TemplateTemplateParmDecl>(Param);
2272 if (!TempTempParm->hasDefaultArgument())
2273 return TemplateArgumentLoc();
2274
2275 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002276 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002277 RAngleLoc,
2278 TempTempParm,
2279 Converted);
2280 if (TName.isNull())
2281 return TemplateArgumentLoc();
2282
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002283 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002284 TempTempParm->getDefaultArgument().getTemplateQualifierRange(),
2285 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2286}
2287
Douglas Gregore7526412009-11-11 19:31:23 +00002288/// \brief Check that the given template argument corresponds to the given
2289/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002290///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002291/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002292/// checked.
2293///
2294/// \param Arg The template argument.
2295///
2296/// \param Template The template in which the template argument resides.
2297///
2298/// \param TemplateLoc The location of the template name for the template
2299/// whose argument list we're matching.
2300///
2301/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2302/// the template argument list.
2303///
2304/// \param ArgumentPackIndex The index into the argument pack where this
2305/// argument will be placed. Only valid if the parameter is a parameter pack.
2306///
2307/// \param Converted The checked, converted argument will be added to the
2308/// end of this small vector.
2309///
2310/// \param CTAK Describes how we arrived at this particular template argument:
2311/// explicitly written, deduced, etc.
2312///
2313/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002314bool Sema::CheckTemplateArgument(NamedDecl *Param,
2315 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002316 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002317 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002318 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002319 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002320 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002321 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002322 // Check template type parameters.
2323 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002324 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002325
Douglas Gregord9e15302009-11-11 19:41:09 +00002326 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002327 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002328 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002329 // with the template arguments we've seen thus far. But if the
2330 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002331 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002332 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2333 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002334
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002335 if (NTTPType->isDependentType() &&
2336 !isa<TemplateTemplateParmDecl>(Template) &&
2337 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002338 // Do substitution on the type of the non-type template parameter.
2339 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002340 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002341 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002342
2343 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002344 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002345 NTTPType = SubstType(NTTPType,
2346 MultiLevelTemplateArgumentList(TemplateArgs),
2347 NTTP->getLocation(),
2348 NTTP->getDeclName());
2349 // If that worked, check the non-type template parameter type
2350 // for validity.
2351 if (!NTTPType.isNull())
2352 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2353 NTTP->getLocation());
2354 if (NTTPType.isNull())
2355 return true;
2356 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002357
Douglas Gregore7526412009-11-11 19:31:23 +00002358 switch (Arg.getArgument().getKind()) {
2359 case TemplateArgument::Null:
2360 assert(false && "Should never see a NULL template argument here");
2361 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002362
Douglas Gregore7526412009-11-11 19:31:23 +00002363 case TemplateArgument::Expression: {
2364 Expr *E = Arg.getArgument().getAsExpr();
2365 TemplateArgument Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002366 if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK))
Douglas Gregore7526412009-11-11 19:31:23 +00002367 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002368
Douglas Gregor910f8002010-11-07 23:05:16 +00002369 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002370 break;
2371 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002372
Douglas Gregore7526412009-11-11 19:31:23 +00002373 case TemplateArgument::Declaration:
2374 case TemplateArgument::Integral:
2375 // We've already checked this template argument, so just copy
2376 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002377 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002378 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002379
Douglas Gregore7526412009-11-11 19:31:23 +00002380 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002381 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002382 // We were given a template template argument. It may not be ill-formed;
2383 // see below.
2384 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002385 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2386 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002387 // We have a template argument such as \c T::template X, which we
2388 // parsed as a template template argument. However, since we now
2389 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002390 // template name into an expression.
2391
2392 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2393 Arg.getTemplateNameLoc());
2394
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002395 // FIXME: TemplateArgumentLoc should store a NestedNameSpecifierLoc
2396 // for the template name.
2397 CXXScopeSpec SS;
2398 SS.MakeTrivial(Context, DTN->getQualifier(),
2399 Arg.getTemplateQualifierRange());
John McCallf7a1a742009-11-24 19:00:30 +00002400 Expr *E = DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002401 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002402 NameInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002403
Douglas Gregora7fc9012011-01-05 18:58:31 +00002404 // If we parsed the template argument as a pack expansion, create a
2405 // pack expansion expression.
2406 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002407 ExprResult Expansion = ActOnPackExpansion(E,
Douglas Gregora7fc9012011-01-05 18:58:31 +00002408 Arg.getTemplateEllipsisLoc());
2409 if (Expansion.isInvalid())
2410 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002411
Douglas Gregora7fc9012011-01-05 18:58:31 +00002412 E = Expansion.get();
2413 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002414
Douglas Gregore7526412009-11-11 19:31:23 +00002415 TemplateArgument Result;
2416 if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
2417 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002418
Douglas Gregor910f8002010-11-07 23:05:16 +00002419 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002420 break;
2421 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002422
Douglas Gregore7526412009-11-11 19:31:23 +00002423 // We have a template argument that actually does refer to a class
2424 // template, template alias, or template template parameter, and
2425 // therefore cannot be a non-type template argument.
2426 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2427 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002428
Douglas Gregore7526412009-11-11 19:31:23 +00002429 Diag(Param->getLocation(), diag::note_template_param_here);
2430 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002431
Douglas Gregore7526412009-11-11 19:31:23 +00002432 case TemplateArgument::Type: {
2433 // We have a non-type template parameter but the template
2434 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002435
Douglas Gregore7526412009-11-11 19:31:23 +00002436 // C++ [temp.arg]p2:
2437 // In a template-argument, an ambiguity between a type-id and
2438 // an expression is resolved to a type-id, regardless of the
2439 // form of the corresponding template-parameter.
2440 //
2441 // We warn specifically about this case, since it can be rather
2442 // confusing for users.
2443 QualType T = Arg.getArgument().getAsType();
2444 SourceRange SR = Arg.getSourceRange();
2445 if (T->isFunctionType())
2446 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2447 else
2448 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2449 Diag(Param->getLocation(), diag::note_template_param_here);
2450 return true;
2451 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002452
Douglas Gregore7526412009-11-11 19:31:23 +00002453 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002454 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002455 break;
2456 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002457
Douglas Gregore7526412009-11-11 19:31:23 +00002458 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002459 }
2460
2461
Douglas Gregore7526412009-11-11 19:31:23 +00002462 // Check template template parameters.
2463 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002464
Douglas Gregore7526412009-11-11 19:31:23 +00002465 // Substitute into the template parameter list of the template
2466 // template parameter, since previously-supplied template arguments
2467 // may appear within the template template parameter.
2468 {
2469 // Set up a template instantiation context.
2470 LocalInstantiationScope Scope(*this);
2471 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002472 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002473 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002474
2475 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002476 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002477 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002478 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002479 MultiLevelTemplateArgumentList(TemplateArgs)));
2480 if (!TempParm)
2481 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002482 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002483
Douglas Gregore7526412009-11-11 19:31:23 +00002484 switch (Arg.getArgument().getKind()) {
2485 case TemplateArgument::Null:
2486 assert(false && "Should never see a NULL template argument here");
2487 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002488
Douglas Gregore7526412009-11-11 19:31:23 +00002489 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002490 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002491 if (CheckTemplateArgument(TempParm, Arg))
2492 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002493
Douglas Gregor910f8002010-11-07 23:05:16 +00002494 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002495 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002496
Douglas Gregore7526412009-11-11 19:31:23 +00002497 case TemplateArgument::Expression:
2498 case TemplateArgument::Type:
2499 // We have a template template parameter but the template
2500 // argument does not refer to a template.
2501 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
2502 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002503
Douglas Gregore7526412009-11-11 19:31:23 +00002504 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002505 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002506 "Declaration argument with template template parameter");
2507 break;
2508 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002509 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002510 "Integral argument with template template parameter");
2511 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002512
Douglas Gregore7526412009-11-11 19:31:23 +00002513 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002514 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002515 break;
2516 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002517
Douglas Gregore7526412009-11-11 19:31:23 +00002518 return false;
2519}
2520
Douglas Gregorc15cb382009-02-09 23:23:08 +00002521/// \brief Check that the given template argument list is well-formed
2522/// for specializing the given template.
2523bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2524 SourceLocation TemplateLoc,
John McCalld5532b62009-11-23 01:53:49 +00002525 const TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002526 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002527 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002528 TemplateParameterList *Params = Template->getTemplateParameters();
2529 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002530 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002531 bool Invalid = false;
2532
John McCalld5532b62009-11-23 01:53:49 +00002533 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2534
Mike Stump1eb44332009-09-09 15:08:12 +00002535 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002536 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002537
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002538 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002539 (NumArgs < Params->getMinRequiredArguments() &&
2540 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002541 // FIXME: point at either the first arg beyond what we can handle,
2542 // or the '>', depending on whether we have too many or too few
2543 // arguments.
2544 SourceRange Range;
2545 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002546 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002547 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2548 << (NumArgs > NumParams)
2549 << (isa<ClassTemplateDecl>(Template)? 0 :
2550 isa<FunctionTemplateDecl>(Template)? 1 :
2551 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2552 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002553 Diag(Template->getLocation(), diag::note_template_decl_here)
2554 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002555 Invalid = true;
2556 }
Mike Stump1eb44332009-09-09 15:08:12 +00002557
2558 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002559 // [...] The type and form of each template-argument specified in
2560 // a template-id shall match the type and form specified for the
2561 // corresponding parameter declared by the template in its
2562 // template-parameter-list.
Douglas Gregor14be16b2010-12-20 16:57:52 +00002563 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2564 TemplateParameterList::iterator Param = Params->begin(),
2565 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002566 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002567 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002568 while (Param != ParamEnd) {
Douglas Gregor16134c62009-07-01 00:28:38 +00002569 if (ArgIdx > NumArgs && PartialTemplateArgs)
2570 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002571
Douglas Gregorf35f8282009-11-11 21:54:23 +00002572 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002573 // If we have an expanded parameter pack, make sure we don't have too
2574 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002575 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002576 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002577 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002578 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2579 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2580 << true
2581 << (isa<ClassTemplateDecl>(Template)? 0 :
2582 isa<FunctionTemplateDecl>(Template)? 1 :
2583 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2584 << Template;
2585 Diag(Template->getLocation(), diag::note_template_decl_here)
2586 << Params->getSourceRange();
2587 return true;
2588 }
2589 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002590
Douglas Gregorf35f8282009-11-11 21:54:23 +00002591 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002592 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2593 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002594 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002595 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002596
Douglas Gregor14be16b2010-12-20 16:57:52 +00002597 if ((*Param)->isTemplateParameterPack()) {
2598 // The template parameter was a template parameter pack, so take the
2599 // deduced argument and place it on the argument pack. Note that we
2600 // stay on the same template parameter so that we can deduce more
2601 // arguments.
2602 ArgumentPack.push_back(Converted.back());
2603 Converted.pop_back();
2604 } else {
2605 // Move to the next template parameter.
2606 ++Param;
2607 }
2608 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002609 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002610 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002611
2612 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002613 // arguments, just break out now and we'll fill in the argument pack below.
2614 if ((*Param)->isTemplateParameterPack())
2615 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002616
Douglas Gregorf35f8282009-11-11 21:54:23 +00002617 // We have a default template argument that we will use.
2618 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002619
Douglas Gregorf35f8282009-11-11 21:54:23 +00002620 // Retrieve the default template argument from the template
2621 // parameter. For each kind of template parameter, we substitute the
2622 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002623 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002624 // the default argument.
2625 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2626 if (!TTP->hasDefaultArgument()) {
2627 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2628 break;
2629 }
2630
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002631 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002632 Template,
2633 TemplateLoc,
2634 RAngleLoc,
2635 TTP,
2636 Converted);
2637 if (!ArgType)
2638 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002639
Douglas Gregorf35f8282009-11-11 21:54:23 +00002640 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2641 ArgType);
2642 } else if (NonTypeTemplateParmDecl *NTTP
2643 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2644 if (!NTTP->hasDefaultArgument()) {
2645 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2646 break;
2647 }
2648
John McCall60d7b3a2010-08-24 06:29:42 +00002649 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002650 TemplateLoc,
2651 RAngleLoc,
2652 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002653 Converted);
2654 if (E.isInvalid())
2655 return true;
2656
2657 Expr *Ex = E.takeAs<Expr>();
2658 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2659 } else {
2660 TemplateTemplateParmDecl *TempParm
2661 = cast<TemplateTemplateParmDecl>(*Param);
2662
2663 if (!TempParm->hasDefaultArgument()) {
2664 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2665 break;
2666 }
2667
2668 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002669 TemplateLoc,
2670 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002671 TempParm,
2672 Converted);
2673 if (Name.isNull())
2674 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002675
2676 Arg = TemplateArgumentLoc(TemplateArgument(Name),
Douglas Gregorf35f8282009-11-11 21:54:23 +00002677 TempParm->getDefaultArgument().getTemplateQualifierRange(),
2678 TempParm->getDefaultArgument().getTemplateNameLoc());
2679 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002680
Douglas Gregorf35f8282009-11-11 21:54:23 +00002681 // Introduce an instantiation record that describes where we are using
2682 // the default template argument.
2683 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002684 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002685 SourceRange(TemplateLoc, RAngleLoc));
2686
Douglas Gregorf35f8282009-11-11 21:54:23 +00002687 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002688 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002689 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00002690 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002691
Douglas Gregor14be16b2010-12-20 16:57:52 +00002692 // Move to the next template parameter and argument.
2693 ++Param;
2694 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002695 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002696
Douglas Gregor14be16b2010-12-20 16:57:52 +00002697 // Form argument packs for each of the parameter packs remaining.
2698 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00002699 // If we're checking a partial list of template arguments, don't fill
2700 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002701
2702 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002703 if (PartialTemplateArgs && ArgumentPack.empty()) {
2704 Converted.push_back(TemplateArgument());
Douglas Gregor203e6a32011-01-11 23:09:57 +00002705 } else if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00002706 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00002707 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002708 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2709 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00002710 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00002711 ArgumentPack.clear();
2712 }
2713 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002714
Douglas Gregor14be16b2010-12-20 16:57:52 +00002715 ++Param;
2716 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002717
Douglas Gregorc15cb382009-02-09 23:23:08 +00002718 return Invalid;
2719}
2720
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002721namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002722 class UnnamedLocalNoLinkageFinder
2723 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002724 {
2725 Sema &S;
2726 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002727
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002728 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002729
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002730 public:
2731 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
2732
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002733 bool Visit(QualType T) {
2734 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002735 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002736
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002737#define TYPE(Class, Parent) \
2738 bool Visit##Class##Type(const Class##Type *);
2739#define ABSTRACT_TYPE(Class, Parent) \
2740 bool Visit##Class##Type(const Class##Type *) { return false; }
2741#define NON_CANONICAL_TYPE(Class, Parent) \
2742 bool Visit##Class##Type(const Class##Type *) { return false; }
2743#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002744
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002745 bool VisitTagDecl(const TagDecl *Tag);
2746 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
2747 };
2748}
2749
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002750bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002751 return false;
2752}
2753
2754bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
2755 return Visit(T->getElementType());
2756}
2757
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002758bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002759 return Visit(T->getPointeeType());
2760}
2761
2762bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002763 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002764 return Visit(T->getPointeeType());
2765}
2766
2767bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002768 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002769 return Visit(T->getPointeeType());
2770}
2771
2772bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002773 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002774 return Visit(T->getPointeeType());
2775}
2776
2777bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002778 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002779 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
2780}
2781
2782bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002783 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002784 return Visit(T->getElementType());
2785}
2786
2787bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002788 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002789 return Visit(T->getElementType());
2790}
2791
2792bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002793 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002794 return Visit(T->getElementType());
2795}
2796
2797bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002798 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002799 return Visit(T->getElementType());
2800}
2801
2802bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002803 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002804 return Visit(T->getElementType());
2805}
2806
2807bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
2808 return Visit(T->getElementType());
2809}
2810
2811bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
2812 return Visit(T->getElementType());
2813}
2814
2815bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
2816 const FunctionProtoType* T) {
2817 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002818 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002819 A != AEnd; ++A) {
2820 if (Visit(*A))
2821 return true;
2822 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002823
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002824 return Visit(T->getResultType());
2825}
2826
2827bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
2828 const FunctionNoProtoType* T) {
2829 return Visit(T->getResultType());
2830}
2831
2832bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
2833 const UnresolvedUsingType*) {
2834 return false;
2835}
2836
2837bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
2838 return false;
2839}
2840
2841bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
2842 return Visit(T->getUnderlyingType());
2843}
2844
2845bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
2846 return false;
2847}
2848
Richard Smith34b41d92011-02-20 03:19:35 +00002849bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
2850 return Visit(T->getDeducedType());
2851}
2852
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002853bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
2854 return VisitTagDecl(T->getDecl());
2855}
2856
2857bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
2858 return VisitTagDecl(T->getDecl());
2859}
2860
2861bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
2862 const TemplateTypeParmType*) {
2863 return false;
2864}
2865
Douglas Gregorc3069d62011-01-14 02:55:32 +00002866bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
2867 const SubstTemplateTypeParmPackType *) {
2868 return false;
2869}
2870
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002871bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
2872 const TemplateSpecializationType*) {
2873 return false;
2874}
2875
2876bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
2877 const InjectedClassNameType* T) {
2878 return VisitTagDecl(T->getDecl());
2879}
2880
2881bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
2882 const DependentNameType* T) {
2883 return VisitNestedNameSpecifier(T->getQualifier());
2884}
2885
2886bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
2887 const DependentTemplateSpecializationType* T) {
2888 return VisitNestedNameSpecifier(T->getQualifier());
2889}
2890
Douglas Gregor7536dd52010-12-20 02:24:11 +00002891bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
2892 const PackExpansionType* T) {
2893 return Visit(T->getPattern());
2894}
2895
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002896bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
2897 return false;
2898}
2899
2900bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
2901 const ObjCInterfaceType *) {
2902 return false;
2903}
2904
2905bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
2906 const ObjCObjectPointerType *) {
2907 return false;
2908}
2909
2910bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
2911 if (Tag->getDeclContext()->isFunctionOrMethod()) {
2912 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
2913 << S.Context.getTypeDeclType(Tag) << SR;
2914 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002915 }
2916
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002917 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) {
2918 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
2919 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
2920 return true;
2921 }
2922
2923 return false;
2924}
2925
2926bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
2927 NestedNameSpecifier *NNS) {
2928 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
2929 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002930
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002931 switch (NNS->getKind()) {
2932 case NestedNameSpecifier::Identifier:
2933 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00002934 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002935 case NestedNameSpecifier::Global:
2936 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002937
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002938 case NestedNameSpecifier::TypeSpec:
2939 case NestedNameSpecifier::TypeSpecWithTemplate:
2940 return Visit(QualType(NNS->getAsType(), 0));
2941 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00002942 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002943}
2944
2945
Douglas Gregorc15cb382009-02-09 23:23:08 +00002946/// \brief Check a template argument against its corresponding
2947/// template type parameter.
2948///
2949/// This routine implements the semantics of C++ [temp.arg.type]. It
2950/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00002951bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00002952 TypeSourceInfo *ArgInfo) {
2953 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00002954 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00002955 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00002956
2957 if (Arg->isVariablyModifiedType()) {
2958 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00002959 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00002960 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002961 }
2962
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002963 // C++03 [temp.arg.type]p2:
2964 // A local type, a type with no linkage, an unnamed type or a type
2965 // compounded from any of these types shall not be used as a
2966 // template-argument for a template type-parameter.
2967 //
2968 // C++0x allows these, and even in C++03 we allow them as an extension with
2969 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00002970 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002971 UnnamedLocalNoLinkageFinder Finder(*this, SR);
2972 (void)Finder.Visit(Context.getCanonicalType(Arg));
2973 }
2974
Douglas Gregorc15cb382009-02-09 23:23:08 +00002975 return false;
2976}
2977
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002978/// \brief Checks whether the given template argument is the address
2979/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002980static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00002981CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
2982 NonTypeTemplateParmDecl *Param,
2983 QualType ParamType,
2984 Expr *ArgIn,
2985 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002986 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00002987 Expr *Arg = ArgIn;
2988 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002989
2990 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00002991 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002992 Arg = Cast->getSubExpr();
2993
2994 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00002995 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002996 // A template-argument for a non-type, non-template
2997 // template-parameter shall be one of: [...]
2998 //
2999 // -- the address of an object or function with external
3000 // linkage, including function templates and function
3001 // template-ids but excluding non-static class members,
3002 // expressed as & id-expression where the & is optional if
3003 // the name refers to a function or array, or if the
3004 // corresponding template-parameter is a reference; or
3005 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003006
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003007 // In C++98/03 mode, give an extension warning on any extra parentheses.
3008 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3009 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003010 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003011 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003012 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003013 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003014 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003015 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003016 }
3017
3018 Arg = Parens->getSubExpr();
3019 }
3020
Douglas Gregorb7a09262010-04-01 18:32:35 +00003021 bool AddressTaken = false;
3022 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003023 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003024 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003025 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00003026 AddressTaken = true;
3027 AddrOpLoc = UnOp->getOperatorLoc();
3028 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003029 } else
3030 DRE = dyn_cast<DeclRefExpr>(Arg);
3031
Douglas Gregorb7a09262010-04-01 18:32:35 +00003032 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003033 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3034 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003035 S.Diag(Param->getLocation(), diag::note_template_param_here);
3036 return true;
3037 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003038
3039 // Stop checking the precise nature of the argument if it is value dependent,
3040 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003041 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003042 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003043 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003044 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003045
Douglas Gregorb7a09262010-04-01 18:32:35 +00003046 if (!isa<ValueDecl>(DRE->getDecl())) {
3047 S.Diag(Arg->getSourceRange().getBegin(),
3048 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003049 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003050 S.Diag(Param->getLocation(), diag::note_template_param_here);
3051 return true;
3052 }
3053
3054 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003055
3056 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003057 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3058 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003059 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003060 S.Diag(Param->getLocation(), diag::note_template_param_here);
3061 return true;
3062 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003063
3064 // Cannot refer to non-static member functions
3065 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003066 if (!Method->isStatic()) {
3067 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003068 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003069 S.Diag(Param->getLocation(), diag::note_template_param_here);
3070 return true;
3071 }
Mike Stump1eb44332009-09-09 15:08:12 +00003072
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003073 // Functions must have external linkage.
3074 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003075 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003076 S.Diag(Arg->getSourceRange().getBegin(),
3077 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003078 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003079 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003080 << true;
3081 return true;
3082 }
3083
3084 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003085 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003086
Douglas Gregorb7a09262010-04-01 18:32:35 +00003087 // If the template parameter has pointer type, the function decays.
3088 if (ParamType->isPointerType() && !AddressTaken)
3089 ArgType = S.Context.getPointerType(Func->getType());
3090 else if (AddressTaken && ParamType->isReferenceType()) {
3091 // If we originally had an address-of operator, but the
3092 // parameter has reference type, complain and (if things look
3093 // like they will work) drop the address-of operator.
3094 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3095 ParamType.getNonReferenceType())) {
3096 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3097 << ParamType;
3098 S.Diag(Param->getLocation(), diag::note_template_param_here);
3099 return true;
3100 }
3101
3102 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3103 << ParamType
3104 << FixItHint::CreateRemoval(AddrOpLoc);
3105 S.Diag(Param->getLocation(), diag::note_template_param_here);
3106
3107 ArgType = Func->getType();
3108 }
3109 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003110 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003111 S.Diag(Arg->getSourceRange().getBegin(),
3112 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003113 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003114 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003115 << true;
3116 return true;
3117 }
3118
Douglas Gregorb7a09262010-04-01 18:32:35 +00003119 // A value of reference type is not an object.
3120 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003121 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003122 diag::err_template_arg_reference_var)
3123 << Var->getType() << Arg->getSourceRange();
3124 S.Diag(Param->getLocation(), diag::note_template_param_here);
3125 return true;
3126 }
3127
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003128 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003129 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003130
3131 // If the template parameter has pointer type, we must have taken
3132 // the address of this object.
3133 if (ParamType->isReferenceType()) {
3134 if (AddressTaken) {
3135 // If we originally had an address-of operator, but the
3136 // parameter has reference type, complain and (if things look
3137 // like they will work) drop the address-of operator.
3138 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3139 ParamType.getNonReferenceType())) {
3140 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3141 << ParamType;
3142 S.Diag(Param->getLocation(), diag::note_template_param_here);
3143 return true;
3144 }
3145
3146 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3147 << ParamType
3148 << FixItHint::CreateRemoval(AddrOpLoc);
3149 S.Diag(Param->getLocation(), diag::note_template_param_here);
3150
3151 ArgType = Var->getType();
3152 }
3153 } else if (!AddressTaken && ParamType->isPointerType()) {
3154 if (Var->getType()->isArrayType()) {
3155 // Array-to-pointer decay.
3156 ArgType = S.Context.getArrayDecayedType(Var->getType());
3157 } else {
3158 // If the template parameter has pointer type but the address of
3159 // this object was not taken, complain and (possibly) recover by
3160 // taking the address of the entity.
3161 ArgType = S.Context.getPointerType(Var->getType());
3162 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3163 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3164 << ParamType;
3165 S.Diag(Param->getLocation(), diag::note_template_param_here);
3166 return true;
3167 }
3168
3169 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3170 << ParamType
3171 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3172
3173 S.Diag(Param->getLocation(), diag::note_template_param_here);
3174 }
3175 }
3176 } else {
3177 // We found something else, but we don't know specifically what it is.
3178 S.Diag(Arg->getSourceRange().getBegin(),
3179 diag::err_template_arg_not_object_or_func)
3180 << Arg->getSourceRange();
3181 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3182 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003183 }
Mike Stump1eb44332009-09-09 15:08:12 +00003184
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003185 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003186 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003187 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003188 // For pointer-to-object types, qualification conversions are
3189 // permitted.
3190 } else {
3191 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3192 if (!ParamRef->getPointeeType()->isFunctionType()) {
3193 // C++ [temp.arg.nontype]p5b3:
3194 // For a non-type template-parameter of type reference to
3195 // object, no conversions apply. The type referred to by the
3196 // reference may be more cv-qualified than the (otherwise
3197 // identical) type of the template- argument. The
3198 // template-parameter is bound directly to the
3199 // template-argument, which shall be an lvalue.
3200
3201 // FIXME: Other qualifiers?
3202 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3203 unsigned ArgQuals = ArgType.getCVRQualifiers();
3204
3205 if ((ParamQuals | ArgQuals) != ParamQuals) {
3206 S.Diag(Arg->getSourceRange().getBegin(),
3207 diag::err_template_arg_ref_bind_ignores_quals)
3208 << ParamType << Arg->getType()
3209 << Arg->getSourceRange();
3210 S.Diag(Param->getLocation(), diag::note_template_param_here);
3211 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003212 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003213 }
3214 }
3215
3216 // At this point, the template argument refers to an object or
3217 // function with external linkage. We now need to check whether the
3218 // argument and parameter types are compatible.
3219 if (!S.Context.hasSameUnqualifiedType(ArgType,
3220 ParamType.getNonReferenceType())) {
3221 // We can't perform this conversion or binding.
3222 if (ParamType->isReferenceType())
3223 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3224 << ParamType << Arg->getType() << Arg->getSourceRange();
3225 else
3226 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3227 << Arg->getType() << ParamType << Arg->getSourceRange();
3228 S.Diag(Param->getLocation(), diag::note_template_param_here);
3229 return true;
3230 }
3231 }
3232
3233 // Create the template argument.
3234 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003235 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003236 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003237}
3238
3239/// \brief Checks whether the given template argument is a pointer to
3240/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003241bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003242 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003243 bool Invalid = false;
3244
3245 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003246 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003247 Arg = Cast->getSubExpr();
3248
3249 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003250 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003251 // A template-argument for a non-type, non-template
3252 // template-parameter shall be one of: [...]
3253 //
3254 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003255 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003256
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003257 // In C++98/03 mode, give an extension warning on any extra parentheses.
3258 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3259 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003260 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003261 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003262 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003263 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003264 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003265 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003266 }
3267
3268 Arg = Parens->getSubExpr();
3269 }
3270
Douglas Gregorcaddba02009-11-12 18:38:13 +00003271 // A pointer-to-member constant written &Class::member.
3272 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003273 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003274 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3275 if (DRE && !DRE->getQualifier())
3276 DRE = 0;
3277 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003278 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003279 // A constant of pointer-to-member type.
3280 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3281 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3282 if (VD->getType()->isMemberPointerType()) {
3283 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003284 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003285 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3286 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003287 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003288 else
3289 Converted = TemplateArgument(VD->getCanonicalDecl());
3290 return Invalid;
3291 }
3292 }
3293 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003294
Douglas Gregorcaddba02009-11-12 18:38:13 +00003295 DRE = 0;
3296 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003297
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003298 if (!DRE)
3299 return Diag(Arg->getSourceRange().getBegin(),
3300 diag::err_template_arg_not_pointer_to_member_form)
3301 << Arg->getSourceRange();
3302
3303 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3304 assert((isa<FieldDecl>(DRE->getDecl()) ||
3305 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3306 "Only non-static member pointers can make it here");
3307
3308 // Okay: this is the address of a non-static member, and therefore
3309 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003310 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003311 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003312 else
3313 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003314 return Invalid;
3315 }
3316
3317 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003318 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003319 diag::err_template_arg_not_pointer_to_member_form)
3320 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003321 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003322 diag::note_template_arg_refers_here);
3323 return true;
3324}
3325
Douglas Gregorc15cb382009-02-09 23:23:08 +00003326/// \brief Check a template argument against its corresponding
3327/// non-type template parameter.
3328///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003329/// This routine implements the semantics of C++ [temp.arg.nontype].
3330/// It returns true if an error occurred, and false otherwise. \p
3331/// InstantiatedParamType is the type of the non-type template
3332/// parameter after it has been instantiated.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003333///
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003334/// If no error was detected, Converted receives the converted template argument.
Douglas Gregorc15cb382009-02-09 23:23:08 +00003335bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
Mike Stump1eb44332009-09-09 15:08:12 +00003336 QualType InstantiatedParamType, Expr *&Arg,
Douglas Gregor02024a92010-03-28 02:42:43 +00003337 TemplateArgument &Converted,
3338 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003339 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3340
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003341 // If either the parameter has a dependent type or the argument is
3342 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003343 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3344 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003345 Converted = TemplateArgument(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003346 return false;
Douglas Gregor40808ce2009-03-09 23:48:35 +00003347 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003348
3349 // C++ [temp.arg.nontype]p5:
3350 // The following conversions are performed on each expression used
3351 // as a non-type template-argument. If a non-type
3352 // template-argument cannot be converted to the type of the
3353 // corresponding template-parameter then the program is
3354 // ill-formed.
3355 //
3356 // -- for a non-type template-parameter of integral or
3357 // enumeration type, integral promotions (4.5) and integral
3358 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003359 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003360 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003361 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003362 // C++ [temp.arg.nontype]p1:
3363 // A template-argument for a non-type, non-template
3364 // template-parameter shall be one of:
3365 //
3366 // -- an integral constant-expression of integral or enumeration
3367 // type; or
3368 // -- the name of a non-type template-parameter; or
3369 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003370 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003371 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003372 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003373 diag::err_template_arg_not_integral_or_enumeral)
3374 << ArgType << Arg->getSourceRange();
3375 Diag(Param->getLocation(), diag::note_template_param_here);
3376 return true;
3377 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003378 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003379 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3380 << ArgType << Arg->getSourceRange();
3381 return true;
3382 }
3383
Douglas Gregor02024a92010-03-28 02:42:43 +00003384 // From here on out, all we care about are the unqualified forms
3385 // of the parameter and argument types.
3386 ParamType = ParamType.getUnqualifiedType();
3387 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003388
3389 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003390 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003391 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003392 } else if (CTAK == CTAK_Deduced) {
3393 // C++ [temp.deduct.type]p17:
3394 // If, in the declaration of a function template with a non-type
3395 // template-parameter, the non-type template- parameter is used
3396 // in an expression in the function parameter-list and, if the
3397 // corresponding template-argument is deduced, the
3398 // template-argument type shall match the type of the
3399 // template-parameter exactly, except that a template-argument
3400 // deduced from an array bound may be of any integral type.
3401 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3402 << ArgType << ParamType;
3403 Diag(Param->getLocation(), diag::note_template_param_here);
John McCalldaa8e4e2010-11-15 09:13:47 +00003404 return true;
3405 } else if (ParamType->isBooleanType()) {
3406 // This is an integral-to-boolean conversion.
3407 ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003408 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3409 !ParamType->isEnumeralType()) {
3410 // This is an integral promotion or conversion.
John McCall2de56d12010-08-25 11:45:40 +00003411 ImpCastExprToType(Arg, ParamType, CK_IntegralCast);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003412 } else {
3413 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003414 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003415 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003416 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003417 Diag(Param->getLocation(), diag::note_template_param_here);
3418 return true;
3419 }
3420
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003421 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003422 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003423 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003424
3425 if (!Arg->isValueDependent()) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003426 llvm::APSInt OldValue = Value;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003427
3428 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003429 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003430 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003431 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003432 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003433 Value.setIsSigned(IntegerType->isSignedIntegerType());
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003434
3435 // Complain if an unsigned parameter received a negative value.
3436 if (IntegerType->isUnsignedIntegerType()
3437 && (OldValue.isSigned() && OldValue.isNegative())) {
3438 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3439 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3440 << Arg->getSourceRange();
3441 Diag(Param->getLocation(), diag::note_template_param_here);
3442 }
3443
3444 // Complain if we overflowed the template parameter's type.
3445 unsigned RequiredBits;
3446 if (IntegerType->isUnsignedIntegerType())
3447 RequiredBits = OldValue.getActiveBits();
3448 else if (OldValue.isUnsigned())
3449 RequiredBits = OldValue.getActiveBits() + 1;
3450 else
3451 RequiredBits = OldValue.getMinSignedBits();
3452 if (RequiredBits > AllowedBits) {
3453 Diag(Arg->getSourceRange().getBegin(),
3454 diag::warn_template_arg_too_large)
3455 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3456 << Arg->getSourceRange();
3457 Diag(Param->getLocation(), diag::note_template_param_here);
3458 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003459 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003460
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003461 // Add the value of this argument to the list of converted
3462 // arguments. We use the bitwidth and signedness of the template
3463 // parameter.
3464 if (Arg->isValueDependent()) {
3465 // The argument is value-dependent. Create a new
3466 // TemplateArgument with the converted expression.
3467 Converted = TemplateArgument(Arg);
3468 return false;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003469 }
3470
John McCall833ca992009-10-29 08:12:44 +00003471 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003472 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003473 : IntegerType);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003474 return false;
3475 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003476
John McCall6bb80172010-03-30 21:47:33 +00003477 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3478
Douglas Gregorb7a09262010-04-01 18:32:35 +00003479 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3480 // from a template argument of type std::nullptr_t to a non-type
3481 // template parameter of type pointer to object, pointer to
3482 // function, or pointer-to-member, respectively.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003483 if (ArgType->isNullPtrType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003484 (ParamType->isPointerType() || ParamType->isMemberPointerType())) {
3485 Converted = TemplateArgument((NamedDecl *)0);
3486 return false;
3487 }
3488
Douglas Gregorb86b0572009-02-11 01:18:59 +00003489 // Handle pointer-to-function, reference-to-function, and
3490 // pointer-to-member-function all in (roughly) the same way.
3491 if (// -- For a non-type template-parameter of type pointer to
3492 // function, only the function-to-pointer conversion (4.3) is
3493 // applied. If the template-argument represents a set of
3494 // overloaded functions (or a pointer to such), the matching
3495 // function is selected from the set (13.4).
3496 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003497 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003498 // -- For a non-type template-parameter of type reference to
3499 // function, no conversions apply. If the template-argument
3500 // represents a set of overloaded functions, the matching
3501 // function is selected from the set (13.4).
3502 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003503 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003504 // -- For a non-type template-parameter of type pointer to
3505 // member function, no conversions apply. If the
3506 // template-argument represents a set of overloaded member
3507 // functions, the matching member function is selected from
3508 // the set (13.4).
3509 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003510 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003511 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003512
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003513 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003514 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003515 true,
3516 FoundResult)) {
3517 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3518 return true;
3519
3520 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3521 ArgType = Arg->getType();
3522 } else
Douglas Gregor48f3bb92009-02-18 21:56:37 +00003523 return true;
Douglas Gregora35284b2009-02-11 00:19:33 +00003524 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003525
Douglas Gregorb7a09262010-04-01 18:32:35 +00003526 if (!ParamType->isMemberPointerType())
3527 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003528 ParamType,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003529 Arg, Converted);
3530
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003531 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3532 false)) {
John McCall2de56d12010-08-25 11:45:40 +00003533 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregorb7a09262010-04-01 18:32:35 +00003534 } else if (!Context.hasSameUnqualifiedType(ArgType,
3535 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003536 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003537 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003538 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003539 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003540 Diag(Param->getLocation(), diag::note_template_param_here);
3541 return true;
3542 }
Mike Stump1eb44332009-09-09 15:08:12 +00003543
Douglas Gregorb7a09262010-04-01 18:32:35 +00003544 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregora35284b2009-02-11 00:19:33 +00003545 }
3546
Chris Lattnerfe90de72009-02-20 21:37:53 +00003547 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003548 // -- for a non-type template-parameter of type pointer to
3549 // object, qualification conversions (4.4) and the
3550 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003551 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003552 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003553 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003554
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003555 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003556 ParamType,
3557 Arg, Converted);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003558 }
Mike Stump1eb44332009-09-09 15:08:12 +00003559
Ted Kremenek6217b802009-07-29 21:53:49 +00003560 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003561 // -- For a non-type template-parameter of type reference to
3562 // object, no conversions apply. The type referred to by the
3563 // reference may be more cv-qualified than the (otherwise
3564 // identical) type of the template-argument. The
3565 // template-parameter is bound directly to the
3566 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003567 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003568 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003569
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003570 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003571 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3572 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003573 true,
3574 FoundResult)) {
3575 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3576 return true;
3577
3578 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3579 ArgType = Arg->getType();
3580 } else
Douglas Gregorb7a09262010-04-01 18:32:35 +00003581 return true;
Douglas Gregorb86b0572009-02-11 01:18:59 +00003582 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003583
3584 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003585 ParamType,
3586 Arg, Converted);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003587 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003588
3589 // -- For a non-type template-parameter of type pointer to data
3590 // member, qualification conversions (4.4) are applied.
3591 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3592
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003593 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003594 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003595 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John McCall2de56d12010-08-25 11:45:40 +00003596 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregor658bbb52009-02-11 16:16:59 +00003597 } else {
3598 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003599 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003600 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003601 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003602 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00003603 return true;
Douglas Gregor658bbb52009-02-11 16:16:59 +00003604 }
3605
Douglas Gregorcaddba02009-11-12 18:38:13 +00003606 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003607}
3608
3609/// \brief Check a template argument against its corresponding
3610/// template template parameter.
3611///
3612/// This routine implements the semantics of C++ [temp.arg.template].
3613/// It returns true if an error occurred, and false otherwise.
3614bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003615 const TemplateArgumentLoc &Arg) {
3616 TemplateName Name = Arg.getArgument().getAsTemplate();
3617 TemplateDecl *Template = Name.getAsTemplateDecl();
3618 if (!Template) {
3619 // Any dependent template name is fine.
3620 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3621 return false;
3622 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003623
3624 // C++ [temp.arg.template]p1:
3625 // A template-argument for a template template-parameter shall be
3626 // the name of a class template, expressed as id-expression. Only
3627 // primary class templates are considered when matching the
3628 // template template argument with the corresponding parameter;
3629 // partial specializations are not considered even if their
3630 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003631 //
3632 // Note that we also allow template template parameters here, which
3633 // will happen when we are dealing with, e.g., class template
3634 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003635 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003636 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003637 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003638 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003639 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003640 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003641 << Template;
3642 }
3643
3644 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
3645 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003646 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003647 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00003648 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00003649}
3650
Douglas Gregor02024a92010-03-28 02:42:43 +00003651/// \brief Given a non-type template argument that refers to a
3652/// declaration and the type of its corresponding non-type template
3653/// parameter, produce an expression that properly refers to that
3654/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003655ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003656Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
3657 QualType ParamType,
3658 SourceLocation Loc) {
3659 assert(Arg.getKind() == TemplateArgument::Declaration &&
3660 "Only declaration template arguments permitted here");
3661 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
3662
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003663 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00003664 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
3665 // If the value is a class member, we might have a pointer-to-member.
3666 // Determine whether the non-type template template parameter is of
3667 // pointer-to-member type. If so, we need to build an appropriate
3668 // expression for a pointer-to-member, since a "normal" DeclRefExpr
3669 // would refer to the member itself.
3670 if (ParamType->isMemberPointerType()) {
3671 QualType ClassType
3672 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
3673 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00003674 = NestedNameSpecifier::Create(Context, 0, false,
3675 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00003676 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00003677 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00003678
3679 // The actual value-ness of this is unimportant, but for
3680 // internal consistency's sake, references to instance methods
3681 // are r-values.
3682 ExprValueKind VK = VK_LValue;
3683 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
3684 VK = VK_RValue;
3685
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003686 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00003687 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00003688 VK,
John McCallf89e55a2010-11-18 06:31:45 +00003689 Loc,
3690 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00003691 if (RefExpr.isInvalid())
3692 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003693
John McCall2de56d12010-08-25 11:45:40 +00003694 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003695
Douglas Gregorc0c83002010-04-30 21:46:38 +00003696 // We might need to perform a trailing qualification conversion, since
3697 // the element type on the parameter could be more qualified than the
3698 // element type in the expression we constructed.
3699 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003700 ParamType.getUnqualifiedType(), false)) {
Douglas Gregorc0c83002010-04-30 21:46:38 +00003701 Expr *RefE = RefExpr.takeAs<Expr>();
John McCall2de56d12010-08-25 11:45:40 +00003702 ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CK_NoOp);
Douglas Gregorc0c83002010-04-30 21:46:38 +00003703 RefExpr = Owned(RefE);
3704 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003705
Douglas Gregor02024a92010-03-28 02:42:43 +00003706 assert(!RefExpr.isInvalid() &&
3707 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00003708 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00003709 return move(RefExpr);
3710 }
3711 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003712
Douglas Gregor02024a92010-03-28 02:42:43 +00003713 QualType T = VD->getType().getNonReferenceType();
3714 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003715 // When the non-type template parameter is a pointer, take the
3716 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00003717 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003718 if (RefExpr.isInvalid())
3719 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003720
3721 if (T->isFunctionType() || T->isArrayType()) {
3722 // Decay functions and arrays.
3723 Expr *RefE = (Expr *)RefExpr.get();
3724 DefaultFunctionArrayConversion(RefE);
3725 if (RefE != RefExpr.get()) {
3726 RefExpr.release();
3727 RefExpr = Owned(RefE);
3728 }
3729
3730 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00003731 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003732
Douglas Gregorb7a09262010-04-01 18:32:35 +00003733 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00003734 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00003735 }
3736
John McCallf89e55a2010-11-18 06:31:45 +00003737 ExprValueKind VK = VK_RValue;
3738
Douglas Gregor02024a92010-03-28 02:42:43 +00003739 // If the non-type template parameter has reference type, qualify the
3740 // resulting declaration reference with the extra qualifiers on the
3741 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00003742 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
3743 VK = VK_LValue;
3744 T = Context.getQualifiedType(T,
3745 TargetRef->getPointeeType().getQualifiers());
3746 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003747
John McCallf89e55a2010-11-18 06:31:45 +00003748 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003749}
3750
3751/// \brief Construct a new expression that refers to the given
3752/// integral template argument with the given source-location
3753/// information.
3754///
3755/// This routine takes care of the mapping from an integral template
3756/// argument (which may have any integral type) to the appropriate
3757/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003758ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003759Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
3760 SourceLocation Loc) {
3761 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00003762 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00003763 QualType T = Arg.getIntegralType();
3764 if (T->isCharType() || T->isWideCharType())
3765 return Owned(new (Context) CharacterLiteral(
3766 Arg.getAsIntegral()->getZExtValue(),
3767 T->isWideCharType(),
3768 T,
3769 Loc));
3770 if (T->isBooleanType())
3771 return Owned(new (Context) CXXBoolLiteralExpr(
3772 Arg.getAsIntegral()->getBoolValue(),
3773 T,
3774 Loc));
3775
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003776 QualType BT;
3777 if (const EnumType *ET = T->getAs<EnumType>())
3778 BT = ET->getDecl()->getPromotionType();
3779 else
3780 BT = T;
3781
3782 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00003783 if (T->isEnumeralType()) {
3784 // FIXME: This is a hack. We need a better way to handle substituted
3785 // non-type template parameters.
3786 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast,
3787 E, 0,
3788 Context.getTrivialTypeSourceInfo(T, Loc),
3789 Loc, Loc);
3790 }
3791
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003792 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00003793}
3794
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003795/// \brief Match two template parameters within template parameter lists.
3796static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
3797 bool Complain,
3798 Sema::TemplateParameterListEqualKind Kind,
3799 SourceLocation TemplateArgLoc) {
3800 // Check the actual kind (type, non-type, template).
3801 if (Old->getKind() != New->getKind()) {
3802 if (Complain) {
3803 unsigned NextDiag = diag::err_template_param_different_kind;
3804 if (TemplateArgLoc.isValid()) {
3805 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3806 NextDiag = diag::note_template_param_different_kind;
3807 }
3808 S.Diag(New->getLocation(), NextDiag)
3809 << (Kind != Sema::TPL_TemplateMatch);
3810 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
3811 << (Kind != Sema::TPL_TemplateMatch);
3812 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003813
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003814 return false;
3815 }
3816
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003817 // Check that both are parameter packs are neither are parameter packs.
3818 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00003819 // template template parameter, the template template parameter can have
3820 // a parameter pack where the template template argument does not.
3821 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
3822 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3823 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003824 if (Complain) {
3825 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
3826 if (TemplateArgLoc.isValid()) {
3827 S.Diag(TemplateArgLoc,
3828 diag::err_template_arg_template_params_mismatch);
3829 NextDiag = diag::note_template_parameter_pack_non_pack;
3830 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003831
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003832 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
3833 : isa<NonTypeTemplateParmDecl>(New)? 1
3834 : 2;
3835 S.Diag(New->getLocation(), NextDiag)
3836 << ParamKind << New->isParameterPack();
3837 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
3838 << ParamKind << Old->isParameterPack();
3839 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003840
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003841 return false;
3842 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003843
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003844 // For non-type template parameters, check the type of the parameter.
3845 if (NonTypeTemplateParmDecl *OldNTTP
3846 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
3847 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003848
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003849 // If we are matching a template template argument to a template
3850 // template parameter and one of the non-type template parameter types
3851 // is dependent, then we must wait until template instantiation time
3852 // to actually compare the arguments.
3853 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3854 (OldNTTP->getType()->isDependentType() ||
3855 NewNTTP->getType()->isDependentType()))
3856 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003857
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003858 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
3859 if (Complain) {
3860 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
3861 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003862 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003863 diag::err_template_arg_template_params_mismatch);
3864 NextDiag = diag::note_template_nontype_parm_different_type;
3865 }
3866 S.Diag(NewNTTP->getLocation(), NextDiag)
3867 << NewNTTP->getType()
3868 << (Kind != Sema::TPL_TemplateMatch);
3869 S.Diag(OldNTTP->getLocation(),
3870 diag::note_template_nontype_parm_prev_declaration)
3871 << OldNTTP->getType();
3872 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003873
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003874 return false;
3875 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003876
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003877 return true;
3878 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003879
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003880 // For template template parameters, check the template parameter types.
3881 // The template parameter lists of template template
3882 // parameters must agree.
3883 if (TemplateTemplateParmDecl *OldTTP
3884 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003885 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003886 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
3887 OldTTP->getTemplateParameters(),
3888 Complain,
3889 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003890 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003891 : Kind),
3892 TemplateArgLoc);
3893 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003894
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003895 return true;
3896}
Douglas Gregor02024a92010-03-28 02:42:43 +00003897
Douglas Gregora0347822011-01-13 00:08:50 +00003898/// \brief Diagnose a known arity mismatch when comparing template argument
3899/// lists.
3900static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003901void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00003902 TemplateParameterList *New,
3903 TemplateParameterList *Old,
3904 Sema::TemplateParameterListEqualKind Kind,
3905 SourceLocation TemplateArgLoc) {
3906 unsigned NextDiag = diag::err_template_param_list_different_arity;
3907 if (TemplateArgLoc.isValid()) {
3908 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3909 NextDiag = diag::note_template_param_list_different_arity;
3910 }
3911 S.Diag(New->getTemplateLoc(), NextDiag)
3912 << (New->size() > Old->size())
3913 << (Kind != Sema::TPL_TemplateMatch)
3914 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
3915 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
3916 << (Kind != Sema::TPL_TemplateMatch)
3917 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
3918}
3919
Douglas Gregorddc29e12009-02-06 22:42:48 +00003920/// \brief Determine whether the given template parameter lists are
3921/// equivalent.
3922///
Mike Stump1eb44332009-09-09 15:08:12 +00003923/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00003924/// source code as part of a new template declaration.
3925///
3926/// \param Old The old template parameter list, typically found via
3927/// name lookup of the template declared with this template parameter
3928/// list.
3929///
3930/// \param Complain If true, this routine will produce a diagnostic if
3931/// the template parameter lists are not equivalent.
3932///
Douglas Gregorfb898e12009-11-12 16:20:59 +00003933/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00003934///
3935/// \param TemplateArgLoc If this source location is valid, then we
3936/// are actually checking the template parameter list of a template
3937/// argument (New) against the template parameter list of its
3938/// corresponding template template parameter (Old). We produce
3939/// slightly different diagnostics in this scenario.
3940///
Douglas Gregorddc29e12009-02-06 22:42:48 +00003941/// \returns True if the template parameter lists are equal, false
3942/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003943bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00003944Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
3945 TemplateParameterList *Old,
3946 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003947 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00003948 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00003949 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
3950 if (Complain)
3951 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3952 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00003953
3954 return false;
3955 }
3956
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003957 // C++0x [temp.arg.template]p3:
3958 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003959 // when each of the template parameters in the template-parameter-list of
3960 // the template-argument's corresponding class template or template alias
3961 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00003962 // template-parameter-list of P. [...]
3963 TemplateParameterList::iterator NewParm = New->begin();
3964 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00003965 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00003966 OldParmEnd = Old->end();
3967 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00003968 if (Kind != TPL_TemplateTemplateArgumentMatch ||
3969 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00003970 if (NewParm == NewParmEnd) {
3971 if (Complain)
3972 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3973 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003974
Douglas Gregora0347822011-01-13 00:08:50 +00003975 return false;
3976 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003977
Douglas Gregora0347822011-01-13 00:08:50 +00003978 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
3979 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003980 return false;
3981
Douglas Gregora0347822011-01-13 00:08:50 +00003982 ++NewParm;
3983 continue;
3984 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003985
Douglas Gregora0347822011-01-13 00:08:50 +00003986 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003987 // [...] When P's template- parameter-list contains a template parameter
3988 // pack (14.5.3), the template parameter pack will match zero or more
3989 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00003990 // template-parameter-list of A with the same type and form as the
3991 // template parameter pack in P (ignoring whether those template
3992 // parameters are template parameter packs).
3993 for (; NewParm != NewParmEnd; ++NewParm) {
3994 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
3995 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003996 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00003997 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00003998 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003999
Douglas Gregora0347822011-01-13 00:08:50 +00004000 // Make sure we exhausted all of the arguments.
4001 if (NewParm != NewParmEnd) {
4002 if (Complain)
4003 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4004 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004005
Douglas Gregora0347822011-01-13 00:08:50 +00004006 return false;
4007 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004008
Douglas Gregorddc29e12009-02-06 22:42:48 +00004009 return true;
4010}
4011
4012/// \brief Check whether a template can be declared within this scope.
4013///
4014/// If the template declaration is valid in this scope, returns
4015/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004016bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004017Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004018 // Find the nearest enclosing declaration scope.
4019 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4020 (S->getFlags() & Scope::TemplateParamScope) != 0)
4021 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004022
Douglas Gregorddc29e12009-02-06 22:42:48 +00004023 // C++ [temp]p2:
4024 // A template-declaration can appear only as a namespace scope or
4025 // class scope declaration.
4026 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004027 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4028 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004029 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004030 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004031
Eli Friedman1503f772009-07-31 01:43:05 +00004032 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004033 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004034
4035 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4036 return false;
4037
Mike Stump1eb44332009-09-09 15:08:12 +00004038 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004039 diag::err_template_outside_namespace_or_class_scope)
4040 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004041}
Douglas Gregorcc636682009-02-17 23:15:12 +00004042
Douglas Gregord5cb8762009-10-07 00:13:32 +00004043/// \brief Determine what kind of template specialization the given declaration
4044/// is.
4045static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4046 if (!D)
4047 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004048
Douglas Gregorf6b11852009-10-08 15:14:33 +00004049 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4050 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004051 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4052 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004053 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4054 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004055
Douglas Gregord5cb8762009-10-07 00:13:32 +00004056 return TSK_Undeclared;
4057}
4058
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004059/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004060/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004061///
Douglas Gregor9302da62009-10-14 23:50:59 +00004062/// This routine determines whether a template specialization can be declared
4063/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004064///
4065/// \param S the semantic analysis object for which this check is being
4066/// performed.
4067///
4068/// \param Specialized the entity being specialized or instantiated, which
4069/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004070/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004071/// member class).
4072///
4073/// \param PrevDecl the previous declaration of this entity, if any.
4074///
4075/// \param Loc the location of the explicit specialization or instantiation of
4076/// this entity.
4077///
4078/// \param IsPartialSpecialization whether this is a partial specialization of
4079/// a class template.
4080///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004081/// \returns true if there was an error that we cannot recover from, false
4082/// otherwise.
4083static bool CheckTemplateSpecializationScope(Sema &S,
4084 NamedDecl *Specialized,
4085 NamedDecl *PrevDecl,
4086 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004087 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004088 // Keep these "kind" numbers in sync with the %select statements in the
4089 // various diagnostics emitted by this routine.
4090 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004091 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004092 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004093 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004094 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004095 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004096 EntityKind = 3;
4097 else if (isa<VarDecl>(Specialized))
4098 EntityKind = 4;
4099 else if (isa<RecordDecl>(Specialized))
4100 EntityKind = 5;
4101 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004102 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4103 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004104 return true;
4105 }
4106
Douglas Gregor88b70942009-02-25 22:02:03 +00004107 // C++ [temp.expl.spec]p2:
4108 // An explicit specialization shall be declared in the namespace
4109 // of which the template is a member, or, for member templates, in
4110 // the namespace of which the enclosing class or enclosing class
4111 // template is a member. An explicit specialization of a member
4112 // function, member class or static data member of a class
4113 // template shall be declared in the namespace of which the class
4114 // template is a member. Such a declaration may also be a
4115 // definition. If the declaration is not a definition, the
4116 // specialization may be defined later in the name- space in which
4117 // the explicit specialization was declared, or in a namespace
4118 // that encloses the one in which the explicit specialization was
4119 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004120 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004121 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004122 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004123 return true;
4124 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004125
Douglas Gregor0a407472009-10-07 17:30:37 +00004126 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
4127 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004128 << Specialized;
Douglas Gregor0a407472009-10-07 17:30:37 +00004129 return true;
4130 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004131
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004132 // C++ [temp.class.spec]p6:
4133 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004134 // in any namespace scope in which its definition may be defined (14.5.1
4135 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004136 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004137 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004138 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004139 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004140 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004141 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4142 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004143 // C++ [temp.exp.spec]p2:
4144 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004145 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004146 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004147 // An explicit specialization of a member function, member class or
4148 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004149 // namespace of which the class template is a member.
4150 //
4151 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004152 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004153 // the specialized template.
4154 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4155 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004156 bool IsCPlusPlus0xExtension
4157 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004158 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004159 S.Diag(Loc, IsCPlusPlus0xExtension
4160 ? diag::ext_template_spec_decl_out_of_scope_global
4161 : diag::err_template_spec_decl_out_of_scope_global)
4162 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004163 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004164 S.Diag(Loc, IsCPlusPlus0xExtension
4165 ? diag::ext_template_spec_decl_out_of_scope
4166 : diag::err_template_spec_decl_out_of_scope)
4167 << EntityKind << Specialized
4168 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004169
Douglas Gregor9302da62009-10-14 23:50:59 +00004170 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4171 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004172 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004173 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004174
4175 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004176 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004177 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004178 // specializations of function templates, static data members, and member
4179 // functions, so we skip the check here for those kinds of entities.
4180 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004181 // Should we refactor that check, so that it occurs later?
4182 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004183 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4184 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004185 if (isa<TranslationUnitDecl>(SpecializedContext))
4186 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4187 << EntityKind << Specialized;
4188 else if (isa<NamespaceDecl>(SpecializedContext))
4189 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4190 << EntityKind << Specialized
4191 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004192
Douglas Gregor9302da62009-10-14 23:50:59 +00004193 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004194 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004195
Douglas Gregord5cb8762009-10-07 00:13:32 +00004196 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004197
Douglas Gregor88b70942009-02-25 22:02:03 +00004198 return false;
4199}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004200
Douglas Gregorbacb9492011-01-03 21:13:47 +00004201/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4202/// that checks non-type template partial specialization arguments.
4203static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4204 NonTypeTemplateParmDecl *Param,
4205 const TemplateArgument *Args,
4206 unsigned NumArgs) {
4207 for (unsigned I = 0; I != NumArgs; ++I) {
4208 if (Args[I].getKind() == TemplateArgument::Pack) {
4209 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004210 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004211 Args[I].pack_size()))
4212 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004213
Douglas Gregore94866f2009-06-12 21:21:02 +00004214 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004215 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004216
Douglas Gregorbacb9492011-01-03 21:13:47 +00004217 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004218 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004219 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004220 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004221
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004222 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004223 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4224 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004225
4226 // Strip off any implicit casts we added as part of type checking.
4227 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4228 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004229
Douglas Gregore94866f2009-06-12 21:21:02 +00004230 // C++ [temp.class.spec]p8:
4231 // A non-type argument is non-specialized if it is the name of a
4232 // non-type parameter. All other non-type arguments are
4233 // specialized.
4234 //
4235 // Below, we check the two conditions that only apply to
4236 // specialized non-type arguments, so skip any non-specialized
4237 // arguments.
4238 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004239 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004240 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004241
Douglas Gregore94866f2009-06-12 21:21:02 +00004242 // C++ [temp.class.spec]p9:
4243 // Within the argument list of a class template partial
4244 // specialization, the following restrictions apply:
4245 // -- A partially specialized non-type argument expression
4246 // shall not involve a template parameter of the partial
4247 // specialization except when the argument expression is a
4248 // simple identifier.
4249 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004250 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004251 diag::err_dependent_non_type_arg_in_partial_spec)
4252 << ArgExpr->getSourceRange();
4253 return true;
4254 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004255
Douglas Gregore94866f2009-06-12 21:21:02 +00004256 // -- The type of a template parameter corresponding to a
4257 // specialized non-type argument shall not be dependent on a
4258 // parameter of the specialization.
4259 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004260 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004261 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4262 << Param->getType()
4263 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004264 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004265 return true;
4266 }
4267 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004268
Douglas Gregorbacb9492011-01-03 21:13:47 +00004269 return false;
4270}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004271
Douglas Gregorbacb9492011-01-03 21:13:47 +00004272/// \brief Check the non-type template arguments of a class template
4273/// partial specialization according to C++ [temp.class.spec]p9.
4274///
4275/// \param TemplateParams the template parameters of the primary class
4276/// template.
4277///
4278/// \param TemplateArg the template arguments of the class template
4279/// partial specialization.
4280///
4281/// \returns true if there was an error, false otherwise.
4282static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4283 TemplateParameterList *TemplateParams,
4284 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4285 const TemplateArgument *ArgList = TemplateArgs.data();
4286
4287 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4288 NonTypeTemplateParmDecl *Param
4289 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4290 if (!Param)
4291 continue;
4292
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004293 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004294 &ArgList[I], 1))
4295 return true;
4296 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004297
4298 return false;
4299}
4300
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004301/// \brief Retrieve the previous declaration of the given declaration.
4302static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4303 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4304 return VD->getPreviousDeclaration();
4305 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4306 return FD->getPreviousDeclaration();
4307 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4308 return TD->getPreviousDeclaration();
4309 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND))
4310 return TD->getPreviousDeclaration();
4311 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4312 return FTD->getPreviousDeclaration();
4313 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4314 return CTD->getPreviousDeclaration();
4315 return 0;
4316}
4317
John McCalld226f652010-08-21 09:40:31 +00004318DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004319Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4320 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004321 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004322 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004323 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004324 SourceLocation TemplateNameLoc,
4325 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004326 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004327 SourceLocation RAngleLoc,
4328 AttributeList *Attr,
4329 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004330 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004331
Douglas Gregorcc636682009-02-17 23:15:12 +00004332 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004333 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004334 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004335 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4336
4337 if (!ClassTemplate) {
4338 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004339 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004340 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4341 return true;
4342 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004343
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004344 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004345 bool isPartialSpecialization = false;
4346
Douglas Gregor88b70942009-02-25 22:02:03 +00004347 // Check the validity of the template headers that introduce this
4348 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004349 // FIXME: We probably shouldn't complain about these headers for
4350 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004351 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004352 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00004353 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
4354 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004355 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004356 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004357 isExplicitSpecialization,
4358 Invalid);
4359 if (Invalid)
4360 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004361
Abramo Bagnara9b934882010-06-12 08:15:14 +00004362 unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size();
4363 if (TemplateParams)
4364 --NumMatchedTemplateParamLists;
4365
Douglas Gregor05396e22009-08-25 17:23:04 +00004366 if (TemplateParams && TemplateParams->size() > 0) {
4367 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004368
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004369 if (TUK == TUK_Friend) {
4370 Diag(KWLoc, diag::err_partial_specialization_friend)
4371 << SourceRange(LAngleLoc, RAngleLoc);
4372 return true;
4373 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004374
Douglas Gregor05396e22009-08-25 17:23:04 +00004375 // C++ [temp.class.spec]p10:
4376 // The template parameter list of a specialization shall not
4377 // contain default template argument values.
4378 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4379 Decl *Param = TemplateParams->getParam(I);
4380 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4381 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004382 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004383 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004384 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004385 }
4386 } else if (NonTypeTemplateParmDecl *NTTP
4387 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4388 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004389 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004390 diag::err_default_arg_in_partial_spec)
4391 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004392 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004393 }
4394 } else {
4395 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004396 if (TTP->hasDefaultArgument()) {
4397 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004398 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004399 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004400 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004401 }
4402 }
4403 }
Douglas Gregora735b202009-10-13 14:39:41 +00004404 } else if (TemplateParams) {
4405 if (TUK == TUK_Friend)
4406 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004407 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004408 SourceRange(TemplateParams->getTemplateLoc(),
4409 TemplateParams->getRAngleLoc()))
4410 << SourceRange(LAngleLoc, RAngleLoc);
4411 else
4412 isExplicitSpecialization = true;
4413 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004414 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004415 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004416 isExplicitSpecialization = true;
4417 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004418
Douglas Gregorcc636682009-02-17 23:15:12 +00004419 // Check that the specialization uses the same tag kind as the
4420 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004421 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4422 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004423 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004424 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004425 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004426 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004427 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004428 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004429 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004430 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004431 diag::note_previous_use);
4432 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4433 }
4434
Douglas Gregor40808ce2009-03-09 23:48:35 +00004435 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004436 TemplateArgumentListInfo TemplateArgs;
4437 TemplateArgs.setLAngleLoc(LAngleLoc);
4438 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004439 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004440
Douglas Gregor925910d2011-01-03 20:35:03 +00004441 // Check for unexpanded parameter packs in any of the template arguments.
4442 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004443 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004444 UPPC_PartialSpecialization))
4445 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004446
Douglas Gregorcc636682009-02-17 23:15:12 +00004447 // Check that the template argument list is well-formed for this
4448 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004449 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004450 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4451 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004452 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004453
Douglas Gregor910f8002010-11-07 23:05:16 +00004454 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004455 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004456
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004457 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004458 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004459 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004460 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004461 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004462 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004463 return true;
4464
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004465 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004466 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004467 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004468 TemplateArgs.size())) {
4469 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4470 << ClassTemplate->getDeclName();
4471 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004472 }
4473 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004474
Douglas Gregorcc636682009-02-17 23:15:12 +00004475 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004476 ClassTemplateSpecializationDecl *PrevDecl = 0;
4477
4478 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004479 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004480 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004481 = ClassTemplate->findPartialSpecialization(Converted.data(),
4482 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004483 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004484 else
4485 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004486 = ClassTemplate->findSpecialization(Converted.data(),
4487 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004488
4489 ClassTemplateSpecializationDecl *Specialization = 0;
4490
Douglas Gregor88b70942009-02-25 22:02:03 +00004491 // Check whether we can declare a class template specialization in
4492 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004493 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004494 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4495 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004496 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004497 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004498
Douglas Gregorb88e8882009-07-30 17:40:51 +00004499 // The canonical type
4500 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004501 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004502 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004503 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004504 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004505 // arguments was referenced but not declared, or we're only
4506 // referencing this specialization as a friend, reuse that
Douglas Gregorcc636682009-02-17 23:15:12 +00004507 // declaration node as our own, updating its source location to
4508 // reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004509 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004510 Specialization->setLocation(TemplateNameLoc);
Douglas Gregorcc636682009-02-17 23:15:12 +00004511 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004512 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004513 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004514 // Build the canonical type that describes the converted template
4515 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004516 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4517 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004518 Converted.data(),
4519 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004520
4521 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004522 ClassTemplate->getInjectedClassNameSpecialization())) {
4523 // C++ [temp.class.spec]p9b3:
4524 //
4525 // -- The argument list of the specialization shall not be identical
4526 // to the implicit argument list of the primary template.
4527 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4528 << (TUK == TUK_Definition)
4529 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4530 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4531 ClassTemplate->getIdentifier(),
4532 TemplateNameLoc,
4533 Attr,
4534 TemplateParams,
4535 AS_none);
4536 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004537
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004538 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004539 ClassTemplatePartialSpecializationDecl *PrevPartial
4540 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004541 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004542 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004543 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004544 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004545 ClassTemplate->getDeclContext(),
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004546 TemplateNameLoc,
4547 TemplateParams,
4548 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004549 Converted.data(),
4550 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004551 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004552 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004553 PrevPartial,
4554 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004555 SetNestedNameSpecifier(Partial, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004556 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004557 Partial->setTemplateParameterListsInfo(Context,
4558 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004559 (TemplateParameterList**) TemplateParameterLists.release());
4560 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004561
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004562 if (!PrevPartial)
4563 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004564 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004565
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004566 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004567 // template specialization, make a note of that.
4568 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4569 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004570
Douglas Gregor031a5882009-06-13 00:26:55 +00004571 // Check that all of the template parameters of the class template
4572 // partial specialization are deducible from the template
4573 // arguments. If not, this class template partial specialization
4574 // will never be used.
4575 llvm::SmallVector<bool, 8> DeducibleParams;
4576 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004577 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004578 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004579 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004580 unsigned NumNonDeducible = 0;
4581 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4582 if (!DeducibleParams[I])
4583 ++NumNonDeducible;
4584
4585 if (NumNonDeducible) {
4586 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4587 << (NumNonDeducible > 1)
4588 << SourceRange(TemplateNameLoc, RAngleLoc);
4589 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4590 if (!DeducibleParams[I]) {
4591 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4592 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004593 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004594 diag::note_partial_spec_unused_parameter)
4595 << Param->getDeclName();
4596 else
Mike Stump1eb44332009-09-09 15:08:12 +00004597 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004598 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004599 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004600 }
4601 }
4602 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004603 } else {
4604 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004605 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004606 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004607 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004608 ClassTemplate->getDeclContext(),
4609 TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004610 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004611 Converted.data(),
4612 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004613 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004614 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004615 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004616 Specialization->setTemplateParameterListsInfo(Context,
4617 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004618 (TemplateParameterList**) TemplateParameterLists.release());
4619 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004620
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004621 if (!PrevDecl)
4622 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004623
4624 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004625 }
4626
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004627 // C++ [temp.expl.spec]p6:
4628 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004629 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004630 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004631 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004632 // use occurs; no diagnostic is required.
4633 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004634 bool Okay = false;
4635 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4636 // Is there any previous explicit specialization declaration?
4637 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4638 Okay = true;
4639 break;
4640 }
4641 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004642
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004643 if (!Okay) {
4644 SourceRange Range(TemplateNameLoc, RAngleLoc);
4645 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
4646 << Context.getTypeDeclType(Specialization) << Range;
4647
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004648 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004649 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004650 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004651 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004652 return true;
4653 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004654 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004655
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004656 // If this is not a friend, note that this is an explicit specialization.
4657 if (TUK != TUK_Friend)
4658 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004659
4660 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004661 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00004662 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004663 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00004664 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004665 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00004666 Diag(Def->getLocation(), diag::note_previous_definition);
4667 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00004668 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004669 }
4670 }
4671
John McCall7f1b9872010-12-18 03:30:47 +00004672 if (Attr)
4673 ProcessDeclAttributeList(S, Specialization, Attr);
4674
Douglas Gregorfc705b82009-02-26 22:19:44 +00004675 // Build the fully-sugared type for this class template
4676 // specialization as the user wrote in the specialization
4677 // itself. This means that we'll pretty-print the type retrieved
4678 // from the specialization's declaration the way that the user
4679 // actually wrote the specialization, rather than formatting the
4680 // name based on the "canonical" representation used to store the
4681 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00004682 TypeSourceInfo *WrittenTy
4683 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
4684 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004685 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004686 Specialization->setTypeAsWritten(WrittenTy);
Douglas Gregor7e9b57b2010-07-06 18:33:12 +00004687 if (TemplateParams)
4688 Specialization->setTemplateKeywordLoc(TemplateParams->getTemplateLoc());
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004689 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00004690 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00004691
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004692 // C++ [temp.expl.spec]p9:
4693 // A template explicit specialization is in the scope of the
4694 // namespace in which the template was defined.
4695 //
4696 // We actually implement this paragraph where we set the semantic
4697 // context (in the creation of the ClassTemplateSpecializationDecl),
4698 // but we also maintain the lexical context where the actual
4699 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00004700 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00004701
Douglas Gregorcc636682009-02-17 23:15:12 +00004702 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004703 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00004704 Specialization->startDefinition();
4705
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004706 if (TUK == TUK_Friend) {
4707 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
4708 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00004709 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004710 /*FIXME:*/KWLoc);
4711 Friend->setAccess(AS_public);
4712 CurContext->addDecl(Friend);
4713 } else {
4714 // Add the specialization into its lexical context, so that it can
4715 // be seen when iterating through the list of declarations in that
4716 // context. However, specializations are not found by name lookup.
4717 CurContext->addDecl(Specialization);
4718 }
John McCalld226f652010-08-21 09:40:31 +00004719 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00004720}
Douglas Gregord57959a2009-03-27 23:10:48 +00004721
John McCalld226f652010-08-21 09:40:31 +00004722Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00004723 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004724 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00004725 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
4726}
4727
John McCalld226f652010-08-21 09:40:31 +00004728Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00004729 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004730 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00004731 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004732 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00004733
Douglas Gregor52591bf2009-06-24 00:54:41 +00004734 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00004735 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00004736 }
Mike Stump1eb44332009-09-09 15:08:12 +00004737
Douglas Gregor52591bf2009-06-24 00:54:41 +00004738 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004739
John McCalld226f652010-08-21 09:40:31 +00004740 Decl *DP = HandleDeclarator(ParentScope, D,
4741 move(TemplateParameterLists),
4742 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00004743 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00004744 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00004745 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00004746 FunctionTemplate->getTemplatedDecl());
4747 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
4748 return ActOnStartOfFunctionDef(FnBodyScope, Function);
4749 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00004750}
4751
John McCall75042392010-02-11 01:33:53 +00004752/// \brief Strips various properties off an implicit instantiation
4753/// that has just been explicitly specialized.
4754static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00004755 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00004756
4757 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4758 FD->setInlineSpecified(false);
4759 }
4760}
4761
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004762/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00004763/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004764/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00004765/// new specialization/instantiation will have any effect.
4766///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004767/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00004768/// instantiation.
4769///
4770/// \param NewTSK the kind of the new explicit specialization or instantiation.
4771///
4772/// \param PrevDecl the previous declaration of the entity.
4773///
4774/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
4775///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004776/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00004777/// declaration was instantiated (either implicitly or explicitly).
4778///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004779/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00004780/// specialization or instantiation has no effect and should be ignored.
4781///
4782/// \returns true if there was an error that should prevent the introduction of
4783/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00004784bool
4785Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
4786 TemplateSpecializationKind NewTSK,
4787 NamedDecl *PrevDecl,
4788 TemplateSpecializationKind PrevTSK,
4789 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004790 bool &HasNoEffect) {
4791 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004792
Douglas Gregor454885e2009-10-15 15:54:05 +00004793 switch (NewTSK) {
4794 case TSK_Undeclared:
4795 case TSK_ImplicitInstantiation:
4796 assert(false && "Don't check implicit instantiations here");
4797 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004798
Douglas Gregor454885e2009-10-15 15:54:05 +00004799 case TSK_ExplicitSpecialization:
4800 switch (PrevTSK) {
4801 case TSK_Undeclared:
4802 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004803 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00004804 // explicitly specialized or has merely been mentioned without any
4805 // instantiation.
4806 return false;
4807
4808 case TSK_ImplicitInstantiation:
4809 if (PrevPointOfInstantiation.isInvalid()) {
4810 // The declaration itself has not actually been instantiated, so it is
4811 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00004812 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00004813 return false;
4814 }
4815 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004816
Douglas Gregor454885e2009-10-15 15:54:05 +00004817 case TSK_ExplicitInstantiationDeclaration:
4818 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004819 assert((PrevTSK == TSK_ImplicitInstantiation ||
4820 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00004821 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004822
Douglas Gregor454885e2009-10-15 15:54:05 +00004823 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004824 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00004825 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004826 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00004827 // implicit instantiation to take place, in every translation unit in
4828 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004829 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4830 // Is there any previous explicit specialization declaration?
4831 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
4832 return false;
4833 }
4834
Douglas Gregor0d035142009-10-27 18:42:08 +00004835 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00004836 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004837 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00004838 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004839
Douglas Gregor454885e2009-10-15 15:54:05 +00004840 return true;
4841 }
4842 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004843
Douglas Gregor454885e2009-10-15 15:54:05 +00004844 case TSK_ExplicitInstantiationDeclaration:
4845 switch (PrevTSK) {
4846 case TSK_ExplicitInstantiationDeclaration:
4847 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004848 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004849 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004850
Douglas Gregor454885e2009-10-15 15:54:05 +00004851 case TSK_Undeclared:
4852 case TSK_ImplicitInstantiation:
4853 // We're explicitly instantiating something that may have already been
4854 // implicitly instantiated; that's fine.
4855 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004856
Douglas Gregor454885e2009-10-15 15:54:05 +00004857 case TSK_ExplicitSpecialization:
4858 // C++0x [temp.explicit]p4:
4859 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004860 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00004861 // specialization for that template, the explicit instantiation has no
4862 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004863 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004864 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004865
Douglas Gregor454885e2009-10-15 15:54:05 +00004866 case TSK_ExplicitInstantiationDefinition:
4867 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004868 // If an entity is the subject of both an explicit instantiation
4869 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00004870 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004871 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00004872 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004873 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004874 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00004875 assert(PrevPointOfInstantiation.isValid() &&
4876 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004877 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004878 return false;
4879 }
4880 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004881
Douglas Gregor454885e2009-10-15 15:54:05 +00004882 case TSK_ExplicitInstantiationDefinition:
4883 switch (PrevTSK) {
4884 case TSK_Undeclared:
4885 case TSK_ImplicitInstantiation:
4886 // We're explicitly instantiating something that may have already been
4887 // implicitly instantiated; that's fine.
4888 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004889
Douglas Gregor454885e2009-10-15 15:54:05 +00004890 case TSK_ExplicitSpecialization:
4891 // C++ DR 259, C++0x [temp.explicit]p4:
4892 // For a given set of template parameters, if an explicit
4893 // instantiation of a template appears after a declaration of
4894 // an explicit specialization for that template, the explicit
4895 // instantiation has no effect.
4896 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004897 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00004898 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00004899 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00004900 if (!getLangOptions().CPlusPlus0x) {
4901 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00004902 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004903 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00004904 diag::note_previous_template_specialization);
4905 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004906 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004907 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004908
Douglas Gregor454885e2009-10-15 15:54:05 +00004909 case TSK_ExplicitInstantiationDeclaration:
4910 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004911 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00004912 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004913
Douglas Gregor454885e2009-10-15 15:54:05 +00004914 case TSK_ExplicitInstantiationDefinition:
4915 // C++0x [temp.spec]p5:
4916 // For a given template and a given set of template-arguments,
4917 // - an explicit instantiation definition shall appear at most once
4918 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00004919 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00004920 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004921 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004922 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004923 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004924 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00004925 }
4926 break;
4927 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004928
Douglas Gregor454885e2009-10-15 15:54:05 +00004929 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004930
Douglas Gregor454885e2009-10-15 15:54:05 +00004931 return false;
4932}
4933
John McCallaf2094e2010-04-08 09:05:18 +00004934/// \brief Perform semantic analysis for the given dependent function
4935/// template specialization. The only possible way to get a dependent
4936/// function template specialization is with a friend declaration,
4937/// like so:
4938///
4939/// template <class T> void foo(T);
4940/// template <class T> class A {
4941/// friend void foo<>(T);
4942/// };
4943///
4944/// There really isn't any useful analysis we can do here, so we
4945/// just store the information.
4946bool
4947Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
4948 const TemplateArgumentListInfo &ExplicitTemplateArgs,
4949 LookupResult &Previous) {
4950 // Remove anything from Previous that isn't a function template in
4951 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004952 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00004953 LookupResult::Filter F = Previous.makeFilter();
4954 while (F.hasNext()) {
4955 NamedDecl *D = F.next()->getUnderlyingDecl();
4956 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00004957 !FDLookupContext->InEnclosingNamespaceSetOf(
4958 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00004959 F.erase();
4960 }
4961 F.done();
4962
4963 // Should this be diagnosed here?
4964 if (Previous.empty()) return true;
4965
4966 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
4967 ExplicitTemplateArgs);
4968 return false;
4969}
4970
Abramo Bagnarae03db982010-05-20 15:32:11 +00004971/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004972/// specialization.
4973///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004974/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004975/// explicit function template specialization. On successful completion,
4976/// the function declaration \p FD will become a function template
4977/// specialization.
4978///
4979/// \param FD the function declaration, which will be updated to become a
4980/// function template specialization.
4981///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004982/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
4983/// if any. Note that this may be valid info even when 0 arguments are
4984/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
4985/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004986///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004987/// \param PrevDecl the set of declarations that may be specialized by
4988/// this function specialization.
4989bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004990Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
John McCalld5532b62009-11-23 01:53:49 +00004991 const TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00004992 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004993 // The set of function template specializations that could match this
4994 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00004995 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004996
Sebastian Redl7a126a42010-08-31 00:36:30 +00004997 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00004998 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
4999 I != E; ++I) {
5000 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5001 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005002 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005003 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005004 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5005 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005006 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005007
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005008 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005009 // A trailing template-argument can be left unspecified in the
5010 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005011 // provided it can be deduced from the function argument type.
5012 // Perform template argument deduction to determine whether we may be
5013 // specializing this template.
5014 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005015 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005016 FunctionDecl *Specialization = 0;
5017 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005018 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005019 FD->getType(),
5020 Specialization,
5021 Info)) {
5022 // FIXME: Template argument deduction failed; record why it failed, so
5023 // that we can provide nifty diagnostics.
5024 (void)TDK;
5025 continue;
5026 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005027
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005028 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005029 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005030 }
5031 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005032
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005033 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005034 UnresolvedSetIterator Result
5035 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005036 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005037 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005038 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005039 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005040 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005041 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005042 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005043 return true;
John McCallc373d482010-01-27 01:50:18 +00005044
5045 // Ignore access information; it doesn't figure into redeclaration checking.
5046 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Douglas Gregorc42b6522010-04-09 21:02:29 +00005047 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005048
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005049 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005050 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005051
5052 // If this is a friend declaration, then we're not really declaring
5053 // an explicit specialization.
5054 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005055
Douglas Gregord5cb8762009-10-07 00:13:32 +00005056 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005057 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005058 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005059 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005060 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005061 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005062 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005063
5064 // C++ [temp.expl.spec]p6:
5065 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005066 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005067 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005068 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005069 // use occurs; no diagnostic is required.
5070 FunctionTemplateSpecializationInfo *SpecInfo
5071 = Specialization->getTemplateSpecializationInfo();
5072 assert(SpecInfo && "Function template specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005073
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005074 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005075 if (!isFriend &&
5076 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005077 TSK_ExplicitSpecialization,
5078 Specialization,
5079 SpecInfo->getTemplateSpecializationKind(),
5080 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005081 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005082 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005083
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005084 // Mark the prior declaration as an explicit specialization, so that later
5085 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005086 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005087 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005088 MarkUnusedFileScopedDecl(Specialization);
5089 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005090
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005091 // Turn the given function declaration into a function template
5092 // specialization, with the template arguments from the previous
5093 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005094 // Take copies of (semantic and syntactic) template argument lists.
5095 const TemplateArgumentList* TemplArgs = new (Context)
5096 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5097 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5098 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005099 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005100 TemplArgs, /*InsertPos=*/0,
5101 SpecInfo->getTemplateSpecializationKind(),
5102 TemplArgsAsWritten);
5103
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005104 // The "previous declaration" for this function template specialization is
5105 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005106 Previous.clear();
5107 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005108 return false;
5109}
5110
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005111/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005112/// specialization.
5113///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005114/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005115/// explicit member function specialization. On successful completion,
5116/// the function declaration \p FD will become a member function
5117/// specialization.
5118///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005119/// \param Member the member declaration, which will be updated to become a
5120/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005121///
John McCall68263142009-11-18 22:49:29 +00005122/// \param Previous the set of declarations, one of which may be specialized
5123/// by this function specialization; the set will be modified to contain the
5124/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005125bool
John McCall68263142009-11-18 22:49:29 +00005126Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005127 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005128
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005129 // Try to find the member we are instantiating.
5130 NamedDecl *Instantiation = 0;
5131 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005132 MemberSpecializationInfo *MSInfo = 0;
5133
John McCall68263142009-11-18 22:49:29 +00005134 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005135 // Nowhere to look anyway.
5136 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005137 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5138 I != E; ++I) {
5139 NamedDecl *D = (*I)->getUnderlyingDecl();
5140 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005141 if (Context.hasSameType(Function->getType(), Method->getType())) {
5142 Instantiation = Method;
5143 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005144 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005145 break;
5146 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005147 }
5148 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005149 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005150 VarDecl *PrevVar;
5151 if (Previous.isSingleResult() &&
5152 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005153 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005154 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005155 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005156 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005157 }
5158 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005159 CXXRecordDecl *PrevRecord;
5160 if (Previous.isSingleResult() &&
5161 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5162 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005163 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005164 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005165 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005166 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005167
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005168 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005169 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005170 // specializations are always out-of-line, the caller will complain about
5171 // this mismatch later.
5172 return false;
5173 }
John McCall77e8b112010-04-13 20:37:33 +00005174
5175 // If this is a friend, just bail out here before we start turning
5176 // things into explicit specializations.
5177 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5178 // Preserve instantiation information.
5179 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5180 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5181 cast<CXXMethodDecl>(InstantiatedFrom),
5182 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5183 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5184 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5185 cast<CXXRecordDecl>(InstantiatedFrom),
5186 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5187 }
5188
5189 Previous.clear();
5190 Previous.addDecl(Instantiation);
5191 return false;
5192 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005193
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005194 // Make sure that this is a specialization of a member.
5195 if (!InstantiatedFrom) {
5196 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5197 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005198 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5199 return true;
5200 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005201
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005202 // C++ [temp.expl.spec]p6:
5203 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005204 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005205 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005206 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005207 // use occurs; no diagnostic is required.
5208 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005209
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005210 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005211 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5212 TSK_ExplicitSpecialization,
5213 Instantiation,
5214 MSInfo->getTemplateSpecializationKind(),
5215 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005216 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005217 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005218
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005219 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005220 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005221 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005222 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005223 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005224 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005225
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005226 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005227 // the original declaration to note that it is an explicit specialization
5228 // (if it was previously an implicit instantiation). This latter step
5229 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005230 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005231 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5232 if (InstantiationFunction->getTemplateSpecializationKind() ==
5233 TSK_ImplicitInstantiation) {
5234 InstantiationFunction->setTemplateSpecializationKind(
5235 TSK_ExplicitSpecialization);
5236 InstantiationFunction->setLocation(Member->getLocation());
5237 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005238
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005239 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5240 cast<CXXMethodDecl>(InstantiatedFrom),
5241 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005242 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005243 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005244 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5245 if (InstantiationVar->getTemplateSpecializationKind() ==
5246 TSK_ImplicitInstantiation) {
5247 InstantiationVar->setTemplateSpecializationKind(
5248 TSK_ExplicitSpecialization);
5249 InstantiationVar->setLocation(Member->getLocation());
5250 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005251
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005252 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5253 cast<VarDecl>(InstantiatedFrom),
5254 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005255 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005256 } else {
5257 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005258 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5259 if (InstantiationClass->getTemplateSpecializationKind() ==
5260 TSK_ImplicitInstantiation) {
5261 InstantiationClass->setTemplateSpecializationKind(
5262 TSK_ExplicitSpecialization);
5263 InstantiationClass->setLocation(Member->getLocation());
5264 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005265
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005266 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005267 cast<CXXRecordDecl>(InstantiatedFrom),
5268 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005269 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005270
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005271 // Save the caller the trouble of having to figure out which declaration
5272 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005273 Previous.clear();
5274 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005275 return false;
5276}
5277
Douglas Gregor558c0322009-10-14 23:41:34 +00005278/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005279///
5280/// \returns true if a serious error occurs, false otherwise.
5281static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005282 SourceLocation InstLoc,
5283 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005284 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5285 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005286
Douglas Gregor669eed82010-07-13 00:10:04 +00005287 if (CurContext->isRecord()) {
5288 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5289 << D;
5290 return true;
5291 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005292
Douglas Gregor558c0322009-10-14 23:41:34 +00005293 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005294 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005295 // template.
5296 //
5297 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005298 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005299 !CurContext->Encloses(OrigContext)) {
5300 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005301 S.Diag(InstLoc,
5302 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005303 diag::err_explicit_instantiation_out_of_scope
5304 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005305 << D << NS;
5306 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005307 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005308 S.getLangOptions().CPlusPlus0x?
5309 diag::err_explicit_instantiation_must_be_global
5310 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005311 << D;
5312 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005313 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005314 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005315
Douglas Gregor558c0322009-10-14 23:41:34 +00005316 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005317 // If the name declared in the explicit instantiation is an unqualified
5318 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005319 // its template is declared or, if that namespace is inline (7.3.1), any
5320 // namespace from its enclosing namespace set.
5321 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005322 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005323
5324 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005325 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005326
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005327 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005328 S.getLangOptions().CPlusPlus0x?
5329 diag::err_explicit_instantiation_unqualified_wrong_namespace
5330 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005331 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005332 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005333 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005334}
5335
5336/// \brief Determine whether the given scope specifier has a template-id in it.
5337static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5338 if (!SS.isSet())
5339 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005340
Douglas Gregor558c0322009-10-14 23:41:34 +00005341 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005342 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005343 // or a static data member of a class template specialization, the name of
5344 // the class template specialization in the qualified-id for the member
5345 // name shall be a simple-template-id.
5346 //
5347 // C++98 has the same restriction, just worded differently.
5348 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5349 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005350 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005351 if (isa<TemplateSpecializationType>(T))
5352 return true;
5353
5354 return false;
5355}
5356
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005357// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005358DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005359Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005360 SourceLocation ExternLoc,
5361 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005362 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005363 SourceLocation KWLoc,
5364 const CXXScopeSpec &SS,
5365 TemplateTy TemplateD,
5366 SourceLocation TemplateNameLoc,
5367 SourceLocation LAngleLoc,
5368 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005369 SourceLocation RAngleLoc,
5370 AttributeList *Attr) {
5371 // Find the class template we're specializing
5372 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005373 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005374 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5375
5376 // Check that the specialization uses the same tag kind as the
5377 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005378 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5379 assert(Kind != TTK_Enum &&
5380 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005381 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005382 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005383 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005384 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005385 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005386 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005387 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005388 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005389 diag::note_previous_use);
5390 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5391 }
5392
Douglas Gregor558c0322009-10-14 23:41:34 +00005393 // C++0x [temp.explicit]p2:
5394 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005395 // definition and an explicit instantiation declaration. An explicit
5396 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005397 TemplateSpecializationKind TSK
5398 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5399 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005400
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005401 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005402 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005403 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005404
5405 // Check that the template argument list is well-formed for this
5406 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005407 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005408 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5409 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005410 return true;
5411
Douglas Gregor910f8002010-11-07 23:05:16 +00005412 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005413 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005414
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005415 // Find the class template specialization declaration that
5416 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005417 void *InsertPos = 0;
5418 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005419 = ClassTemplate->findSpecialization(Converted.data(),
5420 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005421
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005422 TemplateSpecializationKind PrevDecl_TSK
5423 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5424
Douglas Gregord5cb8762009-10-07 00:13:32 +00005425 // C++0x [temp.explicit]p2:
5426 // [...] An explicit instantiation shall appear in an enclosing
5427 // namespace of its template. [...]
5428 //
5429 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005430 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5431 SS.isSet()))
5432 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005433
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005434 ClassTemplateSpecializationDecl *Specialization = 0;
5435
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005436 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005437 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005438 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005439 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005440 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005441 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005442 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005443
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005444 // Even though HasNoEffect == true means that this explicit instantiation
5445 // has no effect on semantics, we go on to put its syntax in the AST.
5446
5447 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5448 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005449 // Since the only prior class template specialization with these
5450 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005451 // declaration node as our own, updating the source location
5452 // for the template name to reflect our new declaration.
5453 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005454 Specialization = PrevDecl;
5455 Specialization->setLocation(TemplateNameLoc);
5456 PrevDecl = 0;
5457 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005458 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005459
Douglas Gregor52604ab2009-09-11 21:19:12 +00005460 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005461 // Create a new class template specialization declaration node for
5462 // this explicit specialization.
5463 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005464 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005465 ClassTemplate->getDeclContext(),
5466 TemplateNameLoc,
5467 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005468 Converted.data(),
5469 Converted.size(),
5470 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005471 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005472
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005473 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005474 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005475 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005476 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005477 }
5478
5479 // Build the fully-sugared type for this explicit instantiation as
5480 // the user wrote in the explicit instantiation itself. This means
5481 // that we'll pretty-print the type retrieved from the
5482 // specialization's declaration the way that the user actually wrote
5483 // the explicit instantiation, rather than formatting the name based
5484 // on the "canonical" representation used to store the template
5485 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005486 TypeSourceInfo *WrittenTy
5487 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5488 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005489 Context.getTypeDeclType(Specialization));
5490 Specialization->setTypeAsWritten(WrittenTy);
5491 TemplateArgsIn.release();
5492
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005493 // Set source locations for keywords.
5494 Specialization->setExternLoc(ExternLoc);
5495 Specialization->setTemplateKeywordLoc(TemplateLoc);
5496
5497 // Add the explicit instantiation into its lexical context. However,
5498 // since explicit instantiations are never found by name lookup, we
5499 // just put it into the declaration context directly.
5500 Specialization->setLexicalDeclContext(CurContext);
5501 CurContext->addDecl(Specialization);
5502
5503 // Syntax is now OK, so return if it has no other effect on semantics.
5504 if (HasNoEffect) {
5505 // Set the template specialization kind.
5506 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005507 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005508 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005509
5510 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005511 // A definition of a class template or class member template
5512 // shall be in scope at the point of the explicit instantiation of
5513 // the class template or class member template.
5514 //
5515 // This check comes when we actually try to perform the
5516 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005517 ClassTemplateSpecializationDecl *Def
5518 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005519 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005520 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005521 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005522 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005523 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005524 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5525 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005526
Douglas Gregor0d035142009-10-27 18:42:08 +00005527 // Instantiate the members of this class template specialization.
5528 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005529 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005530 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005531 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5532
5533 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5534 // TSK_ExplicitInstantiationDefinition
5535 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5536 TSK == TSK_ExplicitInstantiationDefinition)
5537 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005538
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005539 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005540 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005541
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005542 // Set the template specialization kind.
5543 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005544 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005545}
5546
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005547// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005548DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005549Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005550 SourceLocation ExternLoc,
5551 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005552 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005553 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005554 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005555 IdentifierInfo *Name,
5556 SourceLocation NameLoc,
5557 AttributeList *Attr) {
5558
Douglas Gregor402abb52009-05-28 23:31:59 +00005559 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005560 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005561 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005562 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5563 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005564 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005565 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005566 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5567
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005568 if (!TagD)
5569 return true;
5570
John McCalld226f652010-08-21 09:40:31 +00005571 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005572 if (Tag->isEnum()) {
5573 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5574 << Context.getTypeDeclType(Tag);
5575 return true;
5576 }
5577
Douglas Gregord0c87372009-05-27 17:30:49 +00005578 if (Tag->isInvalidDecl())
5579 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005580
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005581 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5582 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5583 if (!Pattern) {
5584 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5585 << Context.getTypeDeclType(Record);
5586 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5587 return true;
5588 }
5589
Douglas Gregor558c0322009-10-14 23:41:34 +00005590 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005591 // If the explicit instantiation is for a class or member class, the
5592 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005593 // simple-template-id.
5594 //
5595 // C++98 has the same restriction, just worded differently.
5596 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005597 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005598 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005599
Douglas Gregor558c0322009-10-14 23:41:34 +00005600 // C++0x [temp.explicit]p2:
5601 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005602 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005603 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005604 TemplateSpecializationKind TSK
5605 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5606 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005607
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005608 // C++0x [temp.explicit]p2:
5609 // [...] An explicit instantiation shall appear in an enclosing
5610 // namespace of its template. [...]
5611 //
5612 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005613 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005614
Douglas Gregor454885e2009-10-15 15:54:05 +00005615 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005616 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005617 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005618 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005619 PrevDecl = Record;
5620 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005621 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005622 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005623 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005624 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005625 PrevDecl,
5626 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005627 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005628 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005629 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005630 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005631 return TagD;
5632 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005633
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005634 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00005635 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005636 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005637 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005638 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005639 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005640 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00005641 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005642 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00005643 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
5644 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005645 Diag(Pattern->getLocation(), diag::note_forward_declaration)
5646 << Pattern;
5647 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00005648 } else {
5649 if (InstantiateClass(NameLoc, Record, Def,
5650 getTemplateInstantiationArgs(Record),
5651 TSK))
5652 return true;
5653
Douglas Gregor952b0172010-02-11 01:04:33 +00005654 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00005655 if (!RecordDef)
5656 return true;
5657 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005658 }
5659
Douglas Gregor0d035142009-10-27 18:42:08 +00005660 // Instantiate all of the members of the class.
5661 InstantiateClassMembers(NameLoc, RecordDef,
5662 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005663
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005664 if (TSK == TSK_ExplicitInstantiationDefinition)
5665 MarkVTableUsed(NameLoc, RecordDef, true);
5666
Mike Stump390b4cc2009-05-16 07:39:55 +00005667 // FIXME: We don't have any representation for explicit instantiations of
5668 // member classes. Such a representation is not needed for compilation, but it
5669 // should be available for clients that want to see all of the declarations in
5670 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005671 return TagD;
5672}
5673
John McCallf312b1e2010-08-26 23:41:50 +00005674DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
5675 SourceLocation ExternLoc,
5676 SourceLocation TemplateLoc,
5677 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00005678 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00005679 // TODO: check if/when DNInfo should replace Name.
5680 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
5681 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005682 if (!Name) {
5683 if (!D.isInvalidType())
5684 Diag(D.getDeclSpec().getSourceRange().getBegin(),
5685 diag::err_explicit_instantiation_requires_name)
5686 << D.getDeclSpec().getSourceRange()
5687 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005688
Douglas Gregord5a423b2009-09-25 18:43:00 +00005689 return true;
5690 }
5691
5692 // The scope passed in may not be a decl scope. Zip up the scope tree until
5693 // we find one that is.
5694 while ((S->getFlags() & Scope::DeclScope) == 0 ||
5695 (S->getFlags() & Scope::TemplateParamScope) != 0)
5696 S = S->getParent();
5697
5698 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00005699 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
5700 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005701 if (R.isNull())
5702 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005703
Douglas Gregord5a423b2009-09-25 18:43:00 +00005704 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
5705 // Cannot explicitly instantiate a typedef.
5706 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
5707 << Name;
5708 return true;
5709 }
5710
Douglas Gregor663b5a02009-10-14 20:14:33 +00005711 // C++0x [temp.explicit]p1:
5712 // [...] An explicit instantiation of a function template shall not use the
5713 // inline or constexpr specifiers.
5714 // Presumably, this also applies to member functions of class templates as
5715 // well.
5716 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005717 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00005718 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00005719 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005720
Douglas Gregor663b5a02009-10-14 20:14:33 +00005721 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005722
Douglas Gregor558c0322009-10-14 23:41:34 +00005723 // C++0x [temp.explicit]p2:
5724 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005725 // definition and an explicit instantiation declaration. An explicit
5726 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00005727 TemplateSpecializationKind TSK
5728 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5729 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005730
Abramo Bagnara25777432010-08-11 22:01:17 +00005731 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00005732 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005733
5734 if (!R->isFunctionType()) {
5735 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005736 // A [...] static data member of a class template can be explicitly
5737 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005738 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00005739 if (Previous.isAmbiguous())
5740 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005741
John McCall1bcee0a2009-12-02 08:25:40 +00005742 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005743 if (!Prev || !Prev->isStaticDataMember()) {
5744 // We expect to see a data data member here.
5745 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
5746 << Name;
5747 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5748 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00005749 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00005750 return true;
5751 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005752
Douglas Gregord5a423b2009-09-25 18:43:00 +00005753 if (!Prev->getInstantiatedFromStaticDataMember()) {
5754 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005755 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005756 diag::err_explicit_instantiation_data_member_not_instantiated)
5757 << Prev;
5758 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
5759 // FIXME: Can we provide a note showing where this was declared?
5760 return true;
5761 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005762
Douglas Gregor558c0322009-10-14 23:41:34 +00005763 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005764 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005765 // or a static data member of a class template specialization, the name of
5766 // the class template specialization in the qualified-id for the member
5767 // name shall be a simple-template-id.
5768 //
5769 // C++98 has the same restriction, just worded differently.
5770 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005771 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005772 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005773 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005774
Douglas Gregor558c0322009-10-14 23:41:34 +00005775 // Check the scope of this explicit instantiation.
5776 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005777
Douglas Gregor454885e2009-10-15 15:54:05 +00005778 // Verify that it is okay to explicitly instantiate here.
5779 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
5780 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005781 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005782 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00005783 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005784 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005785 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005786 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005787 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005788 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005789
Douglas Gregord5a423b2009-09-25 18:43:00 +00005790 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005791 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005792 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005793 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005794
Douglas Gregord5a423b2009-09-25 18:43:00 +00005795 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00005796 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005797 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005798
5799 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00005800 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00005801 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00005802 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005803 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
5804 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00005805 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
5806 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00005807 ASTTemplateArgsPtr TemplateArgsPtr(*this,
5808 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00005809 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00005810 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00005811 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00005812 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00005813 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005814
Douglas Gregord5a423b2009-09-25 18:43:00 +00005815 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005816 // A [...] function [...] can be explicitly instantiated from its template.
5817 // A member function [...] of a class template can be explicitly
5818 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005819 // template.
John McCallc373d482010-01-27 01:50:18 +00005820 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005821 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5822 P != PEnd; ++P) {
5823 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00005824 if (!HasExplicitTemplateArgs) {
5825 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
5826 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
5827 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00005828
John McCallc373d482010-01-27 01:50:18 +00005829 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00005830 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
5831 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00005832 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00005833 }
5834 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005835
Douglas Gregord5a423b2009-09-25 18:43:00 +00005836 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
5837 if (!FunTmpl)
5838 continue;
5839
John McCall5769d612010-02-08 23:07:23 +00005840 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005841 FunctionDecl *Specialization = 0;
5842 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005843 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00005844 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005845 R, Specialization, Info)) {
5846 // FIXME: Keep track of almost-matches?
5847 (void)TDK;
5848 continue;
5849 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005850
John McCallc373d482010-01-27 01:50:18 +00005851 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005852 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005853
Douglas Gregord5a423b2009-09-25 18:43:00 +00005854 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005855 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005856 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005857 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005858 PDiag(diag::err_explicit_instantiation_not_known) << Name,
5859 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
5860 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00005861
John McCallc373d482010-01-27 01:50:18 +00005862 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00005863 return true;
John McCallc373d482010-01-27 01:50:18 +00005864
5865 // Ignore access control bits, we don't need them for redeclaration checking.
5866 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005867
Douglas Gregor0a897e32009-10-15 17:21:20 +00005868 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005869 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005870 diag::err_explicit_instantiation_member_function_not_instantiated)
5871 << Specialization
5872 << (Specialization->getTemplateSpecializationKind() ==
5873 TSK_ExplicitSpecialization);
5874 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
5875 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005876 }
5877
Douglas Gregor0a897e32009-10-15 17:21:20 +00005878 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00005879 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
5880 PrevDecl = Specialization;
5881
Douglas Gregor0a897e32009-10-15 17:21:20 +00005882 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005883 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005884 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005885 PrevDecl,
5886 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00005887 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005888 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00005889 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005890
Douglas Gregor0a897e32009-10-15 17:21:20 +00005891 // FIXME: We may still want to build some representation of this
5892 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005893 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005894 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00005895 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00005896
5897 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005898
Douglas Gregor0a897e32009-10-15 17:21:20 +00005899 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005900 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005901
Douglas Gregor558c0322009-10-14 23:41:34 +00005902 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005903 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005904 // or a static data member of a class template specialization, the name of
5905 // the class template specialization in the qualified-id for the member
5906 // name shall be a simple-template-id.
5907 //
5908 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005909 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005910 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005911 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00005912 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005913 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005914 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005915 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005916
Douglas Gregor558c0322009-10-14 23:41:34 +00005917 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005918 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00005919 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005920 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00005921 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005922
Douglas Gregord5a423b2009-09-25 18:43:00 +00005923 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00005924 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005925}
5926
John McCallf312b1e2010-08-26 23:41:50 +00005927TypeResult
John McCallc4e70192009-09-11 04:59:25 +00005928Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
5929 const CXXScopeSpec &SS, IdentifierInfo *Name,
5930 SourceLocation TagLoc, SourceLocation NameLoc) {
5931 // This has to hold, because SS is expected to be defined.
5932 assert(Name && "Expected a name in a dependent tag");
5933
5934 NestedNameSpecifier *NNS
5935 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
5936 if (!NNS)
5937 return true;
5938
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005939 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00005940
Douglas Gregor48c89f42010-04-24 16:38:41 +00005941 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
5942 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005943 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00005944 return true;
5945 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005946
Douglas Gregor059101f2011-03-02 00:47:37 +00005947 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005948 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00005949 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
5950
5951 // Create type-source location information for this type.
5952 TypeLocBuilder TLB;
5953 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
5954 TL.setKeywordLoc(TagLoc);
5955 TL.setQualifierLoc(SS.getWithLocInContext(Context));
5956 TL.setNameLoc(NameLoc);
5957 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00005958}
5959
John McCallf312b1e2010-08-26 23:41:50 +00005960TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005961Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
5962 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005963 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00005964 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00005965 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00005966
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005967 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
5968 !getLangOptions().CPlusPlus0x)
5969 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005970 << FixItHint::CreateRemoval(TypenameLoc);
5971
Douglas Gregor2494dd02011-03-01 01:34:45 +00005972 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00005973 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
5974 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00005975 if (T.isNull())
5976 return true;
John McCall63b43852010-04-29 23:50:39 +00005977
5978 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
5979 if (isa<DependentNameType>(T)) {
5980 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00005981 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00005982 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00005983 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00005984 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005985 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00005986 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00005987 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00005988 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00005989 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005990
John McCallb3d87482010-08-24 05:47:05 +00005991 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00005992}
5993
John McCallf312b1e2010-08-26 23:41:50 +00005994TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00005995Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
5996 const CXXScopeSpec &SS,
5997 SourceLocation TemplateLoc,
5998 TemplateTy TemplateIn,
5999 SourceLocation TemplateNameLoc,
6000 SourceLocation LAngleLoc,
6001 ASTTemplateArgsPtr TemplateArgsIn,
6002 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006003 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6004 !getLangOptions().CPlusPlus0x)
6005 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006006 << FixItHint::CreateRemoval(TypenameLoc);
6007
6008 // Translate the parser's template argument list in our AST format.
6009 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6010 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6011
6012 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006013 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6014 // Construct a dependent template specialization type.
6015 assert(DTN && "dependent template has non-dependent name?");
6016 assert(DTN->getQualifier()
6017 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6018 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6019 DTN->getQualifier(),
6020 DTN->getIdentifier(),
6021 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006022
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006023 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006024 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006025 DependentTemplateSpecializationTypeLoc SpecTL
6026 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006027 SpecTL.setLAngleLoc(LAngleLoc);
6028 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006029 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006030 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006031 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006032 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6033 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006034 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006035 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006036
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006037 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6038 if (T.isNull())
6039 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006040
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006041 // Provide source-location information for the template specialization
6042 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006043 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006044 TemplateSpecializationTypeLoc SpecTL
6045 = Builder.push<TemplateSpecializationTypeLoc>(T);
6046
6047 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006048 SpecTL.setLAngleLoc(LAngleLoc);
6049 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006050 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006051 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6052 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6053
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006054 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6055 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6056 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006057 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6058
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006059 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6060 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006061}
6062
Douglas Gregora02411e2011-02-27 22:46:49 +00006063
Douglas Gregord57959a2009-03-27 23:10:48 +00006064/// \brief Build the type that describes a C++ typename specifier,
6065/// e.g., "typename T::type".
6066QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006067Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6068 SourceLocation KeywordLoc,
6069 NestedNameSpecifierLoc QualifierLoc,
6070 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006071 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006072 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006073 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006074
John McCall77bb1aa2010-05-01 00:40:08 +00006075 DeclContext *Ctx = computeDeclContext(SS);
6076 if (!Ctx) {
6077 // If the nested-name-specifier is dependent and couldn't be
6078 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006079 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6080 return Context.getDependentNameType(Keyword,
6081 QualifierLoc.getNestedNameSpecifier(),
6082 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006083 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006084
John McCall77bb1aa2010-05-01 00:40:08 +00006085 // If the nested-name-specifier refers to the current instantiation,
6086 // the "typename" keyword itself is superfluous. In C++03, the
6087 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6088 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006089 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006090
John McCall77bb1aa2010-05-01 00:40:08 +00006091 if (RequireCompleteDeclContext(SS, Ctx))
6092 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006093
6094 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006095 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006096 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006097 unsigned DiagID = 0;
6098 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006099 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006100 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006101 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006102 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006103
6104 case LookupResult::FoundUnresolvedValue: {
6105 // We found a using declaration that is a value. Most likely, the using
6106 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006107 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006108 IILoc);
6109 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6110 << Name << Ctx << FullRange;
6111 if (UnresolvedUsingValueDecl *Using
6112 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006113 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006114 Diag(Loc, diag::note_using_value_decl_missing_typename)
6115 << FixItHint::CreateInsertion(Loc, "typename ");
6116 }
6117 }
6118 // Fall through to create a dependent typename type, from which we can recover
6119 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006120
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006121 case LookupResult::NotFoundInCurrentInstantiation:
6122 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006123 return Context.getDependentNameType(Keyword,
6124 QualifierLoc.getNestedNameSpecifier(),
6125 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006126
6127 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006128 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006129 // We found a type. Build an ElaboratedType, since the
6130 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006131 return Context.getElaboratedType(ETK_Typename,
6132 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006133 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006134 }
6135
6136 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006137 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006138 break;
6139
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006140
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006141 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006142 return QualType();
6143
Douglas Gregord57959a2009-03-27 23:10:48 +00006144 case LookupResult::FoundOverloaded:
6145 DiagID = diag::err_typename_nested_not_type;
6146 Referenced = *Result.begin();
6147 break;
6148
John McCall6e247262009-10-10 05:48:19 +00006149 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006150 return QualType();
6151 }
6152
6153 // If we get here, it's because name lookup did not find a
6154 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006155 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006156 IILoc);
6157 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006158 if (Referenced)
6159 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6160 << Name;
6161 return QualType();
6162}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006163
6164namespace {
6165 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006166 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006167 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006168 SourceLocation Loc;
6169 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006170
Douglas Gregor4a959d82009-08-06 16:20:37 +00006171 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006172 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006173
Mike Stump1eb44332009-09-09 15:08:12 +00006174 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006175 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006176 DeclarationName Entity)
6177 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006178 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006179
6180 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006181 /// transformed.
6182 ///
6183 /// For the purposes of type reconstruction, a type has already been
6184 /// transformed if it is NULL or if it is not dependent.
6185 bool AlreadyTransformed(QualType T) {
6186 return T.isNull() || !T->isDependentType();
6187 }
Mike Stump1eb44332009-09-09 15:08:12 +00006188
6189 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006190 /// rebuilt.
6191 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006192
Douglas Gregor4a959d82009-08-06 16:20:37 +00006193 /// \brief Returns the name of the entity whose type is being rebuilt.
6194 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006195
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006196 /// \brief Sets the "base" location and entity when that
6197 /// information is known based on another transformation.
6198 void setBase(SourceLocation Loc, DeclarationName Entity) {
6199 this->Loc = Loc;
6200 this->Entity = Entity;
6201 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006202 };
6203}
6204
Douglas Gregor4a959d82009-08-06 16:20:37 +00006205/// \brief Rebuilds a type within the context of the current instantiation.
6206///
Mike Stump1eb44332009-09-09 15:08:12 +00006207/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006208/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006209/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006210/// partial specialization thereof). This routine will rebuild that type now
6211/// that we have entered the declarator's scope, which may produce different
6212/// canonical types, e.g.,
6213///
6214/// \code
6215/// template<typename T>
6216/// struct X {
6217/// typedef T* pointer;
6218/// pointer data();
6219/// };
6220///
6221/// template<typename T>
6222/// typename X<T>::pointer X<T>::data() { ... }
6223/// \endcode
6224///
Douglas Gregor4714c122010-03-31 17:34:00 +00006225/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006226/// since we do not know that we can look into X<T> when we parsed the type.
6227/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006228/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006229/// as the canonical type of T*, allowing the return types of the out-of-line
6230/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006231TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6232 SourceLocation Loc,
6233 DeclarationName Name) {
6234 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006235 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006236
Douglas Gregor4a959d82009-08-06 16:20:37 +00006237 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6238 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006239}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006240
John McCall60d7b3a2010-08-24 06:29:42 +00006241ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006242 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6243 DeclarationName());
6244 return Rebuilder.TransformExpr(E);
6245}
6246
John McCall63b43852010-04-29 23:50:39 +00006247bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006248 if (SS.isInvalid())
6249 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006250
Douglas Gregor7e384942011-02-25 16:07:42 +00006251 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006252 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6253 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006254 NestedNameSpecifierLoc Rebuilt
6255 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6256 if (!Rebuilt)
6257 return true;
John McCall63b43852010-04-29 23:50:39 +00006258
Douglas Gregor7e384942011-02-25 16:07:42 +00006259 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006260 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006261}
6262
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006263/// \brief Produces a formatted string that describes the binding of
6264/// template parameters to template arguments.
6265std::string
6266Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6267 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006268 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006269}
6270
6271std::string
6272Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6273 const TemplateArgument *Args,
6274 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006275 llvm::SmallString<128> Str;
6276 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006277
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006278 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006279 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006280
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006281 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006282 if (I >= NumArgs)
6283 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006284
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006285 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006286 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006287 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006288 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006289
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006290 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006291 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006292 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006293 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006294 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006295
Douglas Gregor87dd6972010-12-20 16:52:59 +00006296 Out << " = ";
6297 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006298 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006299
6300 Out << ']';
6301 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006302}