blob: 00f490cfad7880929d16ae768def19af0397e1d0 [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) {
371 NestedNameSpecifier *Qualifier
372 = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
John McCallea1471e2010-05-20 01:18:31 +0000373
374 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000375
John McCall2f841ba2009-12-02 03:53:29 +0000376 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000377 isa<CXXMethodDecl>(DC) &&
378 cast<CXXMethodDecl>(DC)->isInstance()) {
379 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000380
John McCallf7a1a742009-11-24 19:00:30 +0000381 // Since the 'this' expression is synthesized, we don't need to
382 // perform the double-lookup check.
383 NamedDecl *FirstQualifierInScope = 0;
384
John McCallaa81e162009-12-01 22:10:20 +0000385 return Owned(CXXDependentScopeMemberExpr::Create(Context,
386 /*This*/ 0, ThisType,
387 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000388 /*Op*/ SourceLocation(),
389 Qualifier, SS.getRange(),
390 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000391 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000392 TemplateArgs));
393 }
394
Abramo Bagnara25777432010-08-11 22:01:17 +0000395 return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000396}
397
John McCall60d7b3a2010-08-24 06:29:42 +0000398ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000399Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000400 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000401 const TemplateArgumentListInfo *TemplateArgs) {
402 return Owned(DependentScopeDeclRefExpr::Create(Context,
403 static_cast<NestedNameSpecifier*>(SS.getScopeRep()),
404 SS.getRange(),
Abramo Bagnara25777432010-08-11 22:01:17 +0000405 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000406 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000407}
408
Douglas Gregor72c3f312008-12-05 18:15:24 +0000409/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
410/// that the template parameter 'PrevDecl' is being shadowed by a new
411/// declaration at location Loc. Returns true to indicate that this is
412/// an error, and false otherwise.
413bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000414 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000415
416 // Microsoft Visual C++ permits template parameters to be shadowed.
417 if (getLangOptions().Microsoft)
418 return false;
419
420 // C++ [temp.local]p4:
421 // A template-parameter shall not be redeclared within its
422 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000423 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000424 << cast<NamedDecl>(PrevDecl)->getDeclName();
425 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
426 return true;
427}
428
Douglas Gregor2943aed2009-03-03 04:44:36 +0000429/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000430/// the parameter D to reference the templated declaration and return a pointer
431/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000432TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
433 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
434 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000435 return Temp;
436 }
437 return 0;
438}
439
Douglas Gregorba68eca2011-01-05 17:40:24 +0000440ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
441 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000442 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000443 "Only template template arguments can be pack expansions here");
444 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
445 "Template template argument pack expansion without packs");
446 ParsedTemplateArgument Result(*this);
447 Result.EllipsisLoc = EllipsisLoc;
448 return Result;
449}
450
Douglas Gregor788cd062009-11-11 01:00:40 +0000451static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
452 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000453
Douglas Gregor788cd062009-11-11 01:00:40 +0000454 switch (Arg.getKind()) {
455 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000456 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000457 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000458 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000459 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000460 return TemplateArgumentLoc(TemplateArgument(T), DI);
461 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000462
Douglas Gregor788cd062009-11-11 01:00:40 +0000463 case ParsedTemplateArgument::NonType: {
464 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
465 return TemplateArgumentLoc(TemplateArgument(E), E);
466 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000467
Douglas Gregor788cd062009-11-11 01:00:40 +0000468 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000469 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000470 TemplateArgument TArg;
471 if (Arg.getEllipsisLoc().isValid())
472 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
473 else
474 TArg = Template;
475 return TemplateArgumentLoc(TArg,
Douglas Gregor788cd062009-11-11 01:00:40 +0000476 Arg.getScopeSpec().getRange(),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000477 Arg.getLocation(),
478 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000479 }
480 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000481
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000482 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000483 return TemplateArgumentLoc();
484}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000485
Douglas Gregor788cd062009-11-11 01:00:40 +0000486/// \brief Translates template arguments as provided by the parser
487/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000488void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
489 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000490 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000491 TemplateArgs.addArgument(translateTemplateArgument(*this,
492 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000493}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000494
Douglas Gregor72c3f312008-12-05 18:15:24 +0000495/// ActOnTypeParameter - Called when a C++ template type parameter
496/// (e.g., "typename T") has been parsed. Typename specifies whether
497/// the keyword "typename" was used to declare the type parameter
498/// (otherwise, "class" was used), and KeyLoc is the location of the
499/// "class" or "typename" keyword. ParamName is the name of the
500/// parameter (NULL indicates an unnamed template parameter) and
Douglas Gregorefed5c82010-06-16 15:23:05 +0000501/// ParamName is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000502/// If the type parameter has a default argument, it will be added
503/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000504Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
505 SourceLocation EllipsisLoc,
506 SourceLocation KeyLoc,
507 IdentifierInfo *ParamName,
508 SourceLocation ParamNameLoc,
509 unsigned Depth, unsigned Position,
510 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000511 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000512 assert(S->isTemplateParamScope() &&
513 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000514 bool Invalid = false;
515
516 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000517 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000518 LookupOrdinaryName,
519 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000520 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000521 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000522 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000523 }
524
Douglas Gregorddc29e12009-02-06 22:42:48 +0000525 SourceLocation Loc = ParamNameLoc;
526 if (!ParamName)
527 Loc = KeyLoc;
528
Douglas Gregor72c3f312008-12-05 18:15:24 +0000529 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000530 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
531 Loc, Depth, Position, ParamName, Typename,
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000532 Ellipsis);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000533 if (Invalid)
534 Param->setInvalidDecl();
535
536 if (ParamName) {
537 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000538 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000539 IdResolver.AddDecl(Param);
540 }
541
Douglas Gregor61c4d282011-01-05 15:48:55 +0000542 // C++0x [temp.param]p9:
543 // A default template-argument may be specified for any kind of
544 // template-parameter that is not a template parameter pack.
545 if (DefaultArg && Ellipsis) {
546 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
547 DefaultArg = ParsedType();
548 }
549
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000550 // Handle the default argument, if provided.
551 if (DefaultArg) {
552 TypeSourceInfo *DefaultTInfo;
553 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000554
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000555 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000556
Douglas Gregor6f526752010-12-16 08:48:57 +0000557 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000558 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000559 UPPC_DefaultArgument))
560 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000561
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000562 // Check the template argument itself.
563 if (CheckTemplateArgument(Param, DefaultTInfo)) {
564 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000565 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000566 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000567
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000568 Param->setDefaultArgument(DefaultTInfo, false);
569 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000570
John McCalld226f652010-08-21 09:40:31 +0000571 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000572}
573
Douglas Gregor2943aed2009-03-03 04:44:36 +0000574/// \brief Check that the type of a non-type template parameter is
575/// well-formed.
576///
577/// \returns the (possibly-promoted) parameter type if valid;
578/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000579QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000580Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000581 // We don't allow variably-modified types as the type of non-type template
582 // parameters.
583 if (T->isVariablyModifiedType()) {
584 Diag(Loc, diag::err_variably_modified_nontype_template_param)
585 << T;
586 return QualType();
587 }
588
Douglas Gregor2943aed2009-03-03 04:44:36 +0000589 // C++ [temp.param]p4:
590 //
591 // A non-type template-parameter shall have one of the following
592 // (optionally cv-qualified) types:
593 //
594 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000595 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000596 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000597 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000598 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000599 T->isReferenceType() ||
600 // -- pointer to member.
601 T->isMemberPointerType() ||
602 // If T is a dependent type, we can't do the check now, so we
603 // assume that it is well-formed.
604 T->isDependentType())
605 return T;
606 // C++ [temp.param]p8:
607 //
608 // A non-type template-parameter of type "array of T" or
609 // "function returning T" is adjusted to be of type "pointer to
610 // T" or "pointer to function returning T", respectively.
611 else if (T->isArrayType())
612 // FIXME: Keep the type prior to promotion?
613 return Context.getArrayDecayedType(T);
614 else if (T->isFunctionType())
615 // FIXME: Keep the type prior to promotion?
616 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000617
Douglas Gregor2943aed2009-03-03 04:44:36 +0000618 Diag(Loc, diag::err_template_nontype_parm_bad_type)
619 << T;
620
621 return QualType();
622}
623
John McCalld226f652010-08-21 09:40:31 +0000624Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
625 unsigned Depth,
626 unsigned Position,
627 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000628 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000629 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
630 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000631
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000632 assert(S->isTemplateParamScope() &&
633 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000634 bool Invalid = false;
635
636 IdentifierInfo *ParamName = D.getIdentifier();
637 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000638 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000639 LookupOrdinaryName,
640 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000641 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000642 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000643 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000644 }
645
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000646 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
647 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000648 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000649 Invalid = true;
650 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000651
Douglas Gregor10738d32010-12-23 23:51:58 +0000652 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000653 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000654 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
655 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000656 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000657 IsParameterPack, TInfo);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000658 if (Invalid)
659 Param->setInvalidDecl();
660
661 if (D.getIdentifier()) {
662 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000663 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000664 IdResolver.AddDecl(Param);
665 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000666
Douglas Gregor61c4d282011-01-05 15:48:55 +0000667 // C++0x [temp.param]p9:
668 // A default template-argument may be specified for any kind of
669 // template-parameter that is not a template parameter pack.
670 if (Default && IsParameterPack) {
671 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
672 Default = 0;
673 }
674
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000675 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000676 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000677 // Check for unexpanded parameter packs.
678 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
679 return Param;
680
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000681 TemplateArgument Converted;
682 if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) {
683 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000684 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000685 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000686
John McCall9ae2f072010-08-23 23:25:46 +0000687 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000688 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000689
John McCalld226f652010-08-21 09:40:31 +0000690 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000691}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000692
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000693/// ActOnTemplateTemplateParameter - Called when a C++ template template
694/// parameter (e.g. T in template <template <typename> class T> class array)
695/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000696Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
697 SourceLocation TmpLoc,
698 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000699 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000700 IdentifierInfo *Name,
701 SourceLocation NameLoc,
702 unsigned Depth,
703 unsigned Position,
704 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000705 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000706 assert(S->isTemplateParamScope() &&
707 "Template template parameter not in template parameter scope!");
708
709 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000710 bool IsParameterPack = EllipsisLoc.isValid();
711 // FIXME: Pack-ness is dropped
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000712 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000713 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000714 NameLoc.isInvalid()? TmpLoc : NameLoc,
715 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000716 Name, Params);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000717
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000718 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000719 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000720 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000721 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000722 IdResolver.AddDecl(Param);
723 }
724
Douglas Gregor6f526752010-12-16 08:48:57 +0000725 if (Params->size() == 0) {
726 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
727 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
728 Param->setInvalidDecl();
729 }
730
Douglas Gregor61c4d282011-01-05 15:48:55 +0000731 // C++0x [temp.param]p9:
732 // A default template-argument may be specified for any kind of
733 // template-parameter that is not a template parameter pack.
734 if (IsParameterPack && !Default.isInvalid()) {
735 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
736 Default = ParsedTemplateArgument();
737 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000738
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000739 if (!Default.isInvalid()) {
740 // Check only that we have a template template argument. We don't want to
741 // try to check well-formedness now, because our template template parameter
742 // might have dependent types in its template parameters, which we wouldn't
743 // be able to match now.
744 //
745 // If none of the template template parameter's template arguments mention
746 // other template parameters, we could actually perform more checking here.
747 // However, it isn't worth doing.
748 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
749 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
750 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
751 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000752 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000753 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000754
Douglas Gregor6f526752010-12-16 08:48:57 +0000755 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000756 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000757 DefaultArg.getArgument().getAsTemplate(),
758 UPPC_DefaultArgument))
759 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000760
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000761 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000762 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000763
John McCalld226f652010-08-21 09:40:31 +0000764 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000765}
766
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000767/// ActOnTemplateParameterList - Builds a TemplateParameterList that
768/// contains the template parameters in Params/NumParams.
769Sema::TemplateParamsTy *
770Sema::ActOnTemplateParameterList(unsigned Depth,
771 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000772 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000773 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000774 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000775 SourceLocation RAngleLoc) {
776 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000777 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000778
Douglas Gregorddc29e12009-02-06 22:42:48 +0000779 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000780 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000781 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000782}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000783
John McCallb6217662010-03-15 10:12:16 +0000784static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
785 if (SS.isSet())
786 T->setQualifierInfo(static_cast<NestedNameSpecifier*>(SS.getScopeRep()),
787 SS.getRange());
788}
789
John McCallf312b1e2010-08-26 23:41:50 +0000790DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000791Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000792 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000793 IdentifierInfo *Name, SourceLocation NameLoc,
794 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000795 TemplateParameterList *TemplateParams,
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000796 AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000797 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000798 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000799 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000800 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000801
802 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000803 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000804 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000805
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000806 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
807 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000808
809 // There is no such thing as an unnamed class template.
810 if (!Name) {
811 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000812 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000813 }
814
815 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000816 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000817 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000818 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000819 if (SS.isNotEmpty() && !SS.isInvalid()) {
820 SemanticContext = computeDeclContext(SS, true);
821 if (!SemanticContext) {
822 // FIXME: Produce a reasonable diagnostic here
823 return true;
824 }
Mike Stump1eb44332009-09-09 15:08:12 +0000825
John McCall77bb1aa2010-05-01 00:40:08 +0000826 if (RequireCompleteDeclContext(SS, SemanticContext))
827 return true;
828
John McCalla24dc2e2009-11-17 02:14:36 +0000829 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000830 } else {
831 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000832 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000833 }
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Douglas Gregor57265e32010-04-12 16:00:01 +0000835 if (Previous.isAmbiguous())
836 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000837
Douglas Gregorddc29e12009-02-06 22:42:48 +0000838 NamedDecl *PrevDecl = 0;
839 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000840 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000841
Douglas Gregorddc29e12009-02-06 22:42:48 +0000842 // If there is a previous declaration with the same name, check
843 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000844 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000845 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000846
847 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000848 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000849 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000850 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000851 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
852 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000853 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000854 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
855 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
856 PrevClassTemplate
857 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
858 ->getSpecializedTemplate();
859 }
860 }
861
John McCall65c49462009-12-18 11:25:59 +0000862 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000863 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000864 // [...] When looking for a prior declaration of a class or a function
865 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000866 // function is neither a qualified name nor a template-id, scopes outside
867 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000868 if (!SS.isSet()) {
869 DeclContext *OutermostContext = CurContext;
870 while (!OutermostContext->isFileContext())
871 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000872
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000873 if (PrevDecl &&
874 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
875 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
876 SemanticContext = PrevDecl->getDeclContext();
877 } else {
878 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000879 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000880 // declaration.
881 PrevDecl = PrevClassTemplate = 0;
882 SemanticContext = OutermostContext;
883 }
John McCalle129d442009-12-17 23:21:11 +0000884 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000885
John McCalle129d442009-12-17 23:21:11 +0000886 if (CurContext->isDependentContext()) {
887 // If this is a dependent context, we don't want to link the friend
888 // class template to the template in scope, because that would perform
889 // checking of the template parameter lists that can't be performed
890 // until the outer context is instantiated.
891 PrevDecl = PrevClassTemplate = 0;
892 }
893 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
894 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000895
Douglas Gregorddc29e12009-02-06 22:42:48 +0000896 if (PrevClassTemplate) {
897 // Ensure that the template parameter lists are compatible.
898 if (!TemplateParameterListsAreEqual(TemplateParams,
899 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000900 /*Complain=*/true,
901 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000902 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000903
904 // C++ [temp.class]p4:
905 // In a redeclaration, partial specialization, explicit
906 // specialization or explicit instantiation of a class template,
907 // the class-key shall agree in kind with the original class
908 // template declaration (7.1.5.3).
909 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000910 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000911 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000912 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000913 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000914 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000915 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000916 }
917
Douglas Gregorddc29e12009-02-06 22:42:48 +0000918 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000919 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000920 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000921 Diag(NameLoc, diag::err_redefinition) << Name;
922 Diag(Def->getLocation(), diag::note_previous_definition);
923 // FIXME: Would it make sense to try to "forget" the previous
924 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000925 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000926 }
927 }
928 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
929 // Maybe we will complain about the shadowed template parameter.
930 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
931 // Just pretend that we didn't see the previous declaration.
932 PrevDecl = 0;
933 } else if (PrevDecl) {
934 // C++ [temp]p5:
935 // A class template shall not have the same name as any other
936 // template, class, function, object, enumeration, enumerator,
937 // namespace, or type in the same scope (3.3), except as specified
938 // in (14.5.4).
939 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
940 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000941 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000942 }
943
Douglas Gregord684b002009-02-10 19:49:53 +0000944 // Check the template parameter list of this declaration, possibly
945 // merging in the template parameter list from the previous class
946 // template declaration.
947 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000948 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
949 TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000950 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Douglas Gregor57265e32010-04-12 16:00:01 +0000952 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000953 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000954 // template out-of-line.
955 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
956 !(TUK == TUK_Friend && CurContext->isDependentContext()))
957 Diag(NameLoc, diag::err_member_def_does_not_match)
958 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000959 }
960
Mike Stump1eb44332009-09-09 15:08:12 +0000961 CXXRecordDecl *NewClass =
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000962 CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000963 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000964 PrevClassTemplate->getTemplatedDecl() : 0,
965 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000966 SetNestedNameSpecifier(NewClass, SS);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000967
968 ClassTemplateDecl *NewTemplate
969 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
970 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000971 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000972 NewClass->setDescribedClassTemplate(NewTemplate);
973
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000974 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000975 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000976 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000977 assert(T->isDependentType() && "Class template type is not dependent?");
978 (void)T;
979
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000980 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000981 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000982 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000983 PrevClassTemplate->getInstantiatedFromMemberTemplate())
984 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000985
Anders Carlsson4cbe82c2009-03-26 01:24:28 +0000986 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +0000987 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +0000988 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Douglas Gregorddc29e12009-02-06 22:42:48 +0000990 // Set the lexical context of these templates
991 NewClass->setLexicalDeclContext(CurContext);
992 NewTemplate->setLexicalDeclContext(CurContext);
993
John McCall0f434ec2009-07-31 02:45:11 +0000994 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +0000995 NewClass->startDefinition();
996
997 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000998 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000999
John McCall05b23ea2009-09-14 21:59:20 +00001000 if (TUK != TUK_Friend)
1001 PushOnScopeChains(NewTemplate, S);
1002 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001003 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001004 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001005 NewClass->setAccess(PrevClassTemplate->getAccess());
1006 }
John McCall05b23ea2009-09-14 21:59:20 +00001007
Douglas Gregord85bea22009-09-26 06:47:28 +00001008 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1009 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001010
John McCall05b23ea2009-09-14 21:59:20 +00001011 // Friend templates are visible in fairly strange ways.
1012 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001013 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001014 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1015 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1016 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001017 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001018 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001019
Douglas Gregord85bea22009-09-26 06:47:28 +00001020 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1021 NewClass->getLocation(),
1022 NewTemplate,
1023 /*FIXME:*/NewClass->getLocation());
1024 Friend->setAccess(AS_public);
1025 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001026 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001027
Douglas Gregord684b002009-02-10 19:49:53 +00001028 if (Invalid) {
1029 NewTemplate->setInvalidDecl();
1030 NewClass->setInvalidDecl();
1031 }
John McCalld226f652010-08-21 09:40:31 +00001032 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001033}
1034
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001035/// \brief Diagnose the presence of a default template argument on a
1036/// template parameter, which is ill-formed in certain contexts.
1037///
1038/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001039static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001040 Sema::TemplateParamListContext TPC,
1041 SourceLocation ParamLoc,
1042 SourceRange DefArgRange) {
1043 switch (TPC) {
1044 case Sema::TPC_ClassTemplate:
1045 return false;
1046
1047 case Sema::TPC_FunctionTemplate:
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 [...]
1052 // (This sentence is not in C++0x, per DR226).
1053 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001054 S.Diag(ParamLoc,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001055 diag::err_template_parameter_default_in_function_template)
1056 << DefArgRange;
1057 return false;
1058
1059 case Sema::TPC_ClassTemplateMember:
1060 // C++0x [temp.param]p9:
1061 // A default template-argument shall not be specified in the
1062 // template-parameter-lists of the definition of a member of a
1063 // class template that appears outside of the member's class.
1064 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1065 << DefArgRange;
1066 return true;
1067
1068 case Sema::TPC_FriendFunctionTemplate:
1069 // C++ [temp.param]p9:
1070 // A default template-argument shall not be specified in a
1071 // friend template declaration.
1072 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1073 << DefArgRange;
1074 return true;
1075
1076 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1077 // for friend function templates if there is only a single
1078 // declaration (and it is a definition). Strange!
1079 }
1080
1081 return false;
1082}
1083
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001084/// \brief Check for unexpanded parameter packs within the template parameters
1085/// of a template template parameter, recursively.
1086bool DiagnoseUnexpandedParameterPacks(Sema &S, TemplateTemplateParmDecl *TTP){
1087 TemplateParameterList *Params = TTP->getTemplateParameters();
1088 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1089 NamedDecl *P = Params->getParam(I);
1090 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001091 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001092 NTTP->getTypeSourceInfo(),
1093 Sema::UPPC_NonTypeTemplateParameterType))
1094 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001095
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001096 continue;
1097 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001098
1099 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001100 = dyn_cast<TemplateTemplateParmDecl>(P))
1101 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1102 return true;
1103 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001104
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001105 return false;
1106}
1107
Douglas Gregord684b002009-02-10 19:49:53 +00001108/// \brief Checks the validity of a template parameter list, possibly
1109/// considering the template parameter list from a previous
1110/// declaration.
1111///
1112/// If an "old" template parameter list is provided, it must be
1113/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1114/// template parameter list.
1115///
1116/// \param NewParams Template parameter list for a new template
1117/// declaration. This template parameter list will be updated with any
1118/// default arguments that are carried through from the previous
1119/// template parameter list.
1120///
1121/// \param OldParams If provided, template parameter list from a
1122/// previous declaration of the same template. Default template
1123/// arguments will be merged from the old template parameter list to
1124/// the new template parameter list.
1125///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001126/// \param TPC Describes the context in which we are checking the given
1127/// template parameter list.
1128///
Douglas Gregord684b002009-02-10 19:49:53 +00001129/// \returns true if an error occurred, false otherwise.
1130bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001131 TemplateParameterList *OldParams,
1132 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001133 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Douglas Gregord684b002009-02-10 19:49:53 +00001135 // C++ [temp.param]p10:
1136 // The set of default template-arguments available for use with a
1137 // template declaration or definition is obtained by merging the
1138 // default arguments from the definition (if in scope) and all
1139 // declarations in scope in the same way default function
1140 // arguments are (8.3.6).
1141 bool SawDefaultArgument = false;
1142 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001143
Anders Carlsson49d25572009-06-12 23:20:15 +00001144 bool SawParameterPack = false;
1145 SourceLocation ParameterPackLoc;
1146
Mike Stump1a35fde2009-02-11 23:03:27 +00001147 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001148 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001149 if (OldParams)
1150 OldParam = OldParams->begin();
1151
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001152 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001153 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1154 NewParamEnd = NewParams->end();
1155 NewParam != NewParamEnd; ++NewParam) {
1156 // Variables used to diagnose redundant default arguments
1157 bool RedundantDefaultArg = false;
1158 SourceLocation OldDefaultLoc;
1159 SourceLocation NewDefaultLoc;
1160
1161 // Variables used to diagnose missing default arguments
1162 bool MissingDefaultArg = false;
1163
Anders Carlsson49d25572009-06-12 23:20:15 +00001164 // C++0x [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001165 // If a template parameter of a primary class template is a template
Douglas Gregor1ed64762011-01-05 16:19:19 +00001166 // parameter pack, it shall be the last template parameter.
1167 if (SawParameterPack && TPC == TPC_ClassTemplate) {
Mike Stump1eb44332009-09-09 15:08:12 +00001168 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001169 diag::err_template_param_pack_must_be_last_template_parameter);
1170 Invalid = true;
1171 }
1172
Douglas Gregord684b002009-02-10 19:49:53 +00001173 if (TemplateTypeParmDecl *NewTypeParm
1174 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001175 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001176 if (NewTypeParm->hasDefaultArgument() &&
1177 DiagnoseDefaultTemplateArgument(*this, TPC,
1178 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001179 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001180 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001181 NewTypeParm->removeDefaultArgument();
1182
1183 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001184 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001185 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001186
Anders Carlsson49d25572009-06-12 23:20:15 +00001187 if (NewTypeParm->isParameterPack()) {
1188 assert(!NewTypeParm->hasDefaultArgument() &&
1189 "Parameter packs can't have a default argument!");
1190 SawParameterPack = true;
1191 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001192 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001193 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001194 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1195 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1196 SawDefaultArgument = true;
1197 RedundantDefaultArg = true;
1198 PreviousDefaultArgLoc = NewDefaultLoc;
1199 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1200 // Merge the default argument from the old declaration to the
1201 // new declaration.
1202 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001203 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001204 true);
1205 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1206 } else if (NewTypeParm->hasDefaultArgument()) {
1207 SawDefaultArgument = true;
1208 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1209 } else if (SawDefaultArgument)
1210 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001211 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001212 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001213 // Check for unexpanded parameter packs.
1214 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001215 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001216 UPPC_NonTypeTemplateParameterType)) {
1217 Invalid = true;
1218 continue;
1219 }
1220
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001221 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001222 if (NewNonTypeParm->hasDefaultArgument() &&
1223 DiagnoseDefaultTemplateArgument(*this, TPC,
1224 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001225 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001226 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001227 }
1228
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001229 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001230 NonTypeTemplateParmDecl *OldNonTypeParm
1231 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001232 if (NewNonTypeParm->isParameterPack()) {
1233 assert(!NewNonTypeParm->hasDefaultArgument() &&
1234 "Parameter packs can't have a default argument!");
1235 SawParameterPack = true;
1236 ParameterPackLoc = NewNonTypeParm->getLocation();
1237 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001238 NewNonTypeParm->hasDefaultArgument()) {
1239 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1240 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1241 SawDefaultArgument = true;
1242 RedundantDefaultArg = true;
1243 PreviousDefaultArgLoc = NewDefaultLoc;
1244 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1245 // Merge the default argument from the old declaration to the
1246 // new declaration.
1247 SawDefaultArgument = true;
1248 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001249 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001250 // parameter.
1251 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001252 OldNonTypeParm->getDefaultArgument(),
1253 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001254 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1255 } else if (NewNonTypeParm->hasDefaultArgument()) {
1256 SawDefaultArgument = true;
1257 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1258 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001259 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001260 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001261 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001262 TemplateTemplateParmDecl *NewTemplateParm
1263 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001264
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001265 // Check for unexpanded parameter packs, recursively.
1266 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1267 Invalid = true;
1268 continue;
1269 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001270
1271 if (NewTemplateParm->hasDefaultArgument() &&
1272 DiagnoseDefaultTemplateArgument(*this, TPC,
1273 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001274 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001275 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001276
1277 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001278 TemplateTemplateParmDecl *OldTemplateParm
1279 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001280 if (NewTemplateParm->isParameterPack()) {
1281 assert(!NewTemplateParm->hasDefaultArgument() &&
1282 "Parameter packs can't have a default argument!");
1283 SawParameterPack = true;
1284 ParameterPackLoc = NewTemplateParm->getLocation();
1285 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001286 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001287 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1288 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001289 SawDefaultArgument = true;
1290 RedundantDefaultArg = true;
1291 PreviousDefaultArgLoc = NewDefaultLoc;
1292 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1293 // Merge the default argument from the old declaration to the
1294 // new declaration.
1295 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001296 // FIXME: We need to create a new kind of "default argument" expression
1297 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001298 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001299 OldTemplateParm->getDefaultArgument(),
1300 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001301 PreviousDefaultArgLoc
1302 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001303 } else if (NewTemplateParm->hasDefaultArgument()) {
1304 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001305 PreviousDefaultArgLoc
1306 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001307 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001308 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001309 }
1310
1311 if (RedundantDefaultArg) {
1312 // C++ [temp.param]p12:
1313 // A template-parameter shall not be given default arguments
1314 // by two different declarations in the same scope.
1315 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1316 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1317 Invalid = true;
1318 } else if (MissingDefaultArg) {
1319 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001320 // If a template-parameter of a class template has a default
1321 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001322 // have a default template-argument supplied or be a template parameter
1323 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001324 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001325 diag::err_template_param_default_arg_missing);
1326 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1327 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001328 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001329 }
1330
1331 // If we have an old template parameter list that we're merging
1332 // in, move on to the next parameter.
1333 if (OldParams)
1334 ++OldParam;
1335 }
1336
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001337 // We were missing some default arguments at the end of the list, so remove
1338 // all of the default arguments.
1339 if (RemoveDefaultArguments) {
1340 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1341 NewParamEnd = NewParams->end();
1342 NewParam != NewParamEnd; ++NewParam) {
1343 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1344 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001345 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001346 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1347 NTTP->removeDefaultArgument();
1348 else
1349 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1350 }
1351 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001352
Douglas Gregord684b002009-02-10 19:49:53 +00001353 return Invalid;
1354}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001355
John McCall4e2cbb22010-10-20 05:44:58 +00001356namespace {
1357
1358/// A class which looks for a use of a certain level of template
1359/// parameter.
1360struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1361 typedef RecursiveASTVisitor<DependencyChecker> super;
1362
1363 unsigned Depth;
1364 bool Match;
1365
1366 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1367 NamedDecl *ND = Params->getParam(0);
1368 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1369 Depth = PD->getDepth();
1370 } else if (NonTypeTemplateParmDecl *PD =
1371 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1372 Depth = PD->getDepth();
1373 } else {
1374 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1375 }
1376 }
1377
1378 bool Matches(unsigned ParmDepth) {
1379 if (ParmDepth >= Depth) {
1380 Match = true;
1381 return true;
1382 }
1383 return false;
1384 }
1385
1386 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1387 return !Matches(T->getDepth());
1388 }
1389
1390 bool TraverseTemplateName(TemplateName N) {
1391 if (TemplateTemplateParmDecl *PD =
1392 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1393 if (Matches(PD->getDepth())) return false;
1394 return super::TraverseTemplateName(N);
1395 }
1396
1397 bool VisitDeclRefExpr(DeclRefExpr *E) {
1398 if (NonTypeTemplateParmDecl *PD =
1399 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1400 if (PD->getDepth() == Depth) {
1401 Match = true;
1402 return false;
1403 }
1404 }
1405 return super::VisitDeclRefExpr(E);
1406 }
1407};
1408}
1409
1410/// Determines whether a template-id depends on the given parameter
1411/// list.
1412static bool
1413DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId,
1414 TemplateParameterList *Params) {
1415 DependencyChecker Checker(Params);
1416 Checker.TraverseType(QualType(TemplateId, 0));
1417 return Checker.Match;
1418}
1419
Mike Stump1eb44332009-09-09 15:08:12 +00001420/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001421/// specifier, returning the template parameter list that applies to the
1422/// name.
1423///
1424/// \param DeclStartLoc the start of the declaration that has a scope
1425/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001426///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001427/// \param SS the scope specifier that will be matched to the given template
1428/// parameter lists. This scope specifier precedes a qualified name that is
1429/// being declared.
1430///
1431/// \param ParamLists the template parameter lists, from the outermost to the
1432/// innermost template parameter lists.
1433///
1434/// \param NumParamLists the number of template parameter lists in ParamLists.
1435///
John McCall77e8b112010-04-13 20:37:33 +00001436/// \param IsFriend Whether to apply the slightly different rules for
1437/// matching template parameters to scope specifiers in friend
1438/// declarations.
1439///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001440/// \param IsExplicitSpecialization will be set true if the entity being
1441/// declared is an explicit specialization, false otherwise.
1442///
Mike Stump1eb44332009-09-09 15:08:12 +00001443/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001444/// name that is preceded by the scope specifier @p SS. This template
1445/// parameter list may be have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001446/// template) or may have no template parameters (if we're declaring a
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001447/// template specialization), or may be NULL (if we were's declaring isn't
1448/// itself a template).
1449TemplateParameterList *
1450Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
1451 const CXXScopeSpec &SS,
1452 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001453 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001454 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001455 bool &IsExplicitSpecialization,
1456 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001457 IsExplicitSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001458
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001459 // Find the template-ids that occur within the nested-name-specifier. These
1460 // template-ids will match up with the template parameter lists.
1461 llvm::SmallVector<const TemplateSpecializationType *, 4>
1462 TemplateIdsInSpecifier;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001463 llvm::SmallVector<ClassTemplateSpecializationDecl *, 4>
1464 ExplicitSpecializationsInSpecifier;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001465 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
1466 NNS; NNS = NNS->getPrefix()) {
John McCall4b2b02b2009-12-15 02:19:47 +00001467 const Type *T = NNS->getAsType();
1468 if (!T) break;
1469
1470 // C++0x [temp.expl.spec]p17:
1471 // A member or a member template may be nested within many
1472 // enclosing class templates. In an explicit specialization for
1473 // such a member, the member declaration shall be preceded by a
1474 // template<> for each enclosing class template that is
1475 // explicitly specialized.
Douglas Gregorfe331062010-02-13 05:23:25 +00001476 //
1477 // Following the existing practice of GNU and EDG, we allow a typedef of a
1478 // template specialization type.
John McCall49f4e1c2010-12-10 11:01:00 +00001479 while (const TypedefType *TT = dyn_cast<TypedefType>(T))
1480 T = TT->getDecl()->getUnderlyingType().getTypePtr();
John McCall4b2b02b2009-12-15 02:19:47 +00001481
Mike Stump1eb44332009-09-09 15:08:12 +00001482 if (const TemplateSpecializationType *SpecType
Douglas Gregorfe331062010-02-13 05:23:25 +00001483 = dyn_cast<TemplateSpecializationType>(T)) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001484 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
1485 if (!Template)
1486 continue; // FIXME: should this be an error? probably...
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Ted Kremenek6217b802009-07-29 21:53:49 +00001488 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001489 ClassTemplateSpecializationDecl *SpecDecl
1490 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
1491 // If the nested name specifier refers to an explicit specialization,
1492 // we don't need a template<> header.
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001493 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
1494 ExplicitSpecializationsInSpecifier.push_back(SpecDecl);
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001495 continue;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001496 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001497 }
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001499 TemplateIdsInSpecifier.push_back(SpecType);
1500 }
1501 }
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001503 // Reverse the list of template-ids in the scope specifier, so that we can
1504 // more easily match up the template-ids and the template parameter lists.
1505 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001507 SourceLocation FirstTemplateLoc = DeclStartLoc;
1508 if (NumParamLists)
1509 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001511 // Match the template-ids found in the specifier to the template parameter
1512 // lists.
John McCall4e2cbb22010-10-20 05:44:58 +00001513 unsigned ParamIdx = 0, TemplateIdx = 0;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001514 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
John McCall4e2cbb22010-10-20 05:44:58 +00001515 TemplateIdx != NumTemplateIds; ++TemplateIdx) {
1516 const TemplateSpecializationType *TemplateId
1517 = TemplateIdsInSpecifier[TemplateIdx];
Douglas Gregorb88e8882009-07-30 17:40:51 +00001518 bool DependentTemplateId = TemplateId->isDependentType();
John McCall4e2cbb22010-10-20 05:44:58 +00001519
1520 // In friend declarations we can have template-ids which don't
1521 // depend on the corresponding template parameter lists. But
1522 // assume that empty parameter lists are supposed to match this
1523 // template-id.
1524 if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) {
1525 if (!DependentTemplateId ||
1526 !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx]))
1527 continue;
1528 }
1529
1530 if (ParamIdx >= NumParamLists) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001531 // We have a template-id without a corresponding template parameter
1532 // list.
John McCall77e8b112010-04-13 20:37:33 +00001533
1534 // ...which is fine if this is a friend declaration.
1535 if (IsFriend) {
1536 IsExplicitSpecialization = true;
1537 break;
1538 }
1539
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001540 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +00001541 // FIXME: the location information here isn't great.
1542 Diag(SS.getRange().getBegin(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001543 diag::err_template_spec_needs_template_parameters)
John McCall4e2cbb22010-10-20 05:44:58 +00001544 << QualType(TemplateId, 0)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001545 << SS.getRange();
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001546 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001547 } else {
1548 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
1549 << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +00001550 << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001551 IsExplicitSpecialization = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001552 }
1553 return 0;
1554 }
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001556 // Check the template parameter list against its corresponding template-id.
Douglas Gregorb88e8882009-07-30 17:40:51 +00001557 if (DependentTemplateId) {
John McCall31f17ec2010-04-27 00:57:59 +00001558 TemplateParameterList *ExpectedTemplateParams = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00001559
John McCall31f17ec2010-04-27 00:57:59 +00001560 // Are there cases in (e.g.) friends where this won't match?
1561 if (const InjectedClassNameType *Injected
1562 = TemplateId->getAs<InjectedClassNameType>()) {
1563 CXXRecordDecl *Record = Injected->getDecl();
1564 if (ClassTemplatePartialSpecializationDecl *Partial =
1565 dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
1566 ExpectedTemplateParams = Partial->getTemplateParameters();
1567 else
1568 ExpectedTemplateParams = Record->getDescribedClassTemplate()
1569 ->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001570 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001571
John McCall31f17ec2010-04-27 00:57:59 +00001572 if (ExpectedTemplateParams)
John McCall4e2cbb22010-10-20 05:44:58 +00001573 TemplateParameterListsAreEqual(ParamLists[ParamIdx],
John McCall31f17ec2010-04-27 00:57:59 +00001574 ExpectedTemplateParams,
1575 true, TPL_TemplateMatch);
1576
John McCall4e2cbb22010-10-20 05:44:58 +00001577 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1578 TPC_ClassTemplateMember);
1579 } else if (ParamLists[ParamIdx]->size() > 0)
1580 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00001581 diag::err_template_param_list_matches_nontemplate)
1582 << TemplateId
John McCall4e2cbb22010-10-20 05:44:58 +00001583 << ParamLists[ParamIdx]->getSourceRange();
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001584 else
1585 IsExplicitSpecialization = true;
John McCall4e2cbb22010-10-20 05:44:58 +00001586
1587 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001588 }
Mike Stump1eb44332009-09-09 15:08:12 +00001589
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001590 // If there were at least as many template-ids as there were template
1591 // parameter lists, then there are no template parameter lists remaining for
1592 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001593 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001594 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001596 // If there were too many template parameter lists, complain about that now.
John McCall4e2cbb22010-10-20 05:44:58 +00001597 if (ParamIdx != NumParamLists - 1) {
1598 while (ParamIdx < NumParamLists - 1) {
1599 bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0;
1600 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001601 isExplicitSpecHeader? diag::warn_template_spec_extra_headers
1602 : diag::err_template_spec_extra_headers)
John McCall4e2cbb22010-10-20 05:44:58 +00001603 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1604 ParamLists[ParamIdx]->getRAngleLoc());
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001605
1606 if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) {
1607 Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(),
1608 diag::note_explicit_template_spec_does_not_need_header)
1609 << ExplicitSpecializationsInSpecifier.back();
1610 ExplicitSpecializationsInSpecifier.pop_back();
1611 }
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001612
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001613 // We have a template parameter list with no corresponding scope, which
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001614 // means that the resulting template declaration can't be instantiated
1615 // properly (we'll end up with dependent nodes when we shouldn't).
1616 if (!isExplicitSpecHeader)
1617 Invalid = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001618
John McCall4e2cbb22010-10-20 05:44:58 +00001619 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001620 }
1621 }
Mike Stump1eb44332009-09-09 15:08:12 +00001622
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001623 // Return the last template parameter list, which corresponds to the
1624 // entity being declared.
1625 return ParamLists[NumParamLists - 1];
1626}
1627
Douglas Gregor7532dc62009-03-30 22:58:21 +00001628QualType Sema::CheckTemplateIdType(TemplateName Name,
1629 SourceLocation TemplateLoc,
John McCalld5532b62009-11-23 01:53:49 +00001630 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001631 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001632 if (!Template) {
1633 // The template name does not resolve to a template, so we just
1634 // build a dependent template-id type.
John McCalld5532b62009-11-23 01:53:49 +00001635 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001636 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001637
Douglas Gregor40808ce2009-03-09 23:48:35 +00001638 // Check that the template argument list is well-formed for this
1639 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001640 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001641 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001642 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001643 return QualType();
1644
Douglas Gregor910f8002010-11-07 23:05:16 +00001645 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001646 "Converted template argument list is too short!");
1647
1648 QualType CanonType;
1649
Douglas Gregorcaddba02009-11-12 18:38:13 +00001650 if (Name.isDependent() ||
1651 TemplateSpecializationType::anyDependentTemplateArguments(
John McCalld5532b62009-11-23 01:53:49 +00001652 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001653 // This class template specialization is a dependent
1654 // type. Therefore, its canonical type is another class template
1655 // specialization type that contains all of the converted
1656 // arguments in canonical form. This ensures that, e.g., A<T> and
1657 // A<T, T> have identical types when A is declared as:
1658 //
1659 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001660 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001661 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001662 Converted.data(),
1663 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Douglas Gregor1275ae02009-07-28 23:00:59 +00001665 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001666 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001667 // In the future, we need to teach getTemplateSpecializationType to only
1668 // build the canonical type and return that to us.
1669 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001670
1671 // This might work out to be a current instantiation, in which
1672 // case the canonical type needs to be the InjectedClassNameType.
1673 //
1674 // TODO: in theory this could be a simple hashtable lookup; most
1675 // changes to CurContext don't change the set of current
1676 // instantiations.
1677 if (isa<ClassTemplateDecl>(Template)) {
1678 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1679 // If we get out to a namespace, we're done.
1680 if (Ctx->isFileContext()) break;
1681
1682 // If this isn't a record, keep looking.
1683 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1684 if (!Record) continue;
1685
1686 // Look for one of the two cases with InjectedClassNameTypes
1687 // and check whether it's the same template.
1688 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1689 !Record->getDescribedClassTemplate())
1690 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001691
John McCall31f17ec2010-04-27 00:57:59 +00001692 // Fetch the injected class name type and check whether its
1693 // injected type is equal to the type we just built.
1694 QualType ICNT = Context.getTypeDeclType(Record);
1695 QualType Injected = cast<InjectedClassNameType>(ICNT)
1696 ->getInjectedSpecializationType();
1697
1698 if (CanonType != Injected->getCanonicalTypeInternal())
1699 continue;
1700
1701 // If so, the canonical type of this TST is the injected
1702 // class name type of the record we just found.
1703 assert(ICNT.isCanonical());
1704 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001705 break;
1706 }
1707 }
Mike Stump1eb44332009-09-09 15:08:12 +00001708 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001709 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001710 // Find the class template specialization declaration that
1711 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001712 void *InsertPos = 0;
1713 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001714 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001715 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001716 if (!Decl) {
1717 // This is the first time we have referenced this class template
1718 // specialization. Create the canonical declaration and add it to
1719 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001720 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001721 ClassTemplate->getTemplatedDecl()->getTagKind(),
1722 ClassTemplate->getDeclContext(),
1723 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001724 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001725 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001726 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001727 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001728 Decl->setLexicalDeclContext(CurContext);
1729 }
1730
1731 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00001732 assert(isa<RecordType>(CanonType) &&
1733 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00001734 }
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Douglas Gregor40808ce2009-03-09 23:48:35 +00001736 // Build the fully-sugared type for this class template
1737 // specialization, which refers back to the class template
1738 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00001739 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001740}
1741
John McCallf312b1e2010-08-26 23:41:50 +00001742TypeResult
Douglas Gregor7532dc62009-03-30 22:58:21 +00001743Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001744 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001745 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00001746 SourceLocation RAngleLoc) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001747 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001748
Douglas Gregor40808ce2009-03-09 23:48:35 +00001749 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00001750 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00001751 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001752
John McCalld5532b62009-11-23 01:53:49 +00001753 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001754 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00001755
1756 if (Result.isNull())
1757 return true;
1758
John McCalla93c9342009-12-07 02:54:59 +00001759 TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result);
John McCall833ca992009-10-29 08:12:44 +00001760 TemplateSpecializationTypeLoc TL
1761 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1762 TL.setTemplateNameLoc(TemplateLoc);
1763 TL.setLAngleLoc(LAngleLoc);
1764 TL.setRAngleLoc(RAngleLoc);
1765 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1766 TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
1767
John McCallb3d87482010-08-24 05:47:05 +00001768 return CreateParsedType(Result, DI);
John McCall6b2becf2009-09-08 17:47:29 +00001769}
John McCallf1bbbb42009-09-04 01:14:41 +00001770
Craig Silverstein45ab4b52010-11-18 08:32:02 +00001771TypeResult Sema::ActOnTagTemplateIdType(CXXScopeSpec &SS,
1772 TypeResult TypeResult,
John McCallf312b1e2010-08-26 23:41:50 +00001773 TagUseKind TUK,
1774 TypeSpecifierType TagSpec,
1775 SourceLocation TagLoc) {
John McCall6b2becf2009-09-08 17:47:29 +00001776 if (TypeResult.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00001777 return ::TypeResult();
John McCallf1bbbb42009-09-04 01:14:41 +00001778
John McCalla93c9342009-12-07 02:54:59 +00001779 TypeSourceInfo *DI;
John McCall833ca992009-10-29 08:12:44 +00001780 QualType Type = GetTypeFromParser(TypeResult.get(), &DI);
John McCallf1bbbb42009-09-04 01:14:41 +00001781
John McCall6b2becf2009-09-08 17:47:29 +00001782 // Verify the tag specifier.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001783 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Mike Stump1eb44332009-09-09 15:08:12 +00001784
John McCall6b2becf2009-09-08 17:47:29 +00001785 if (const RecordType *RT = Type->getAs<RecordType>()) {
1786 RecordDecl *D = RT->getDecl();
1787
1788 IdentifierInfo *Id = D->getIdentifier();
1789 assert(Id && "templated class must have an identifier");
1790
1791 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1792 Diag(TagLoc, diag::err_use_with_wrong_tag)
John McCallc4e70192009-09-11 04:59:25 +00001793 << Type
Douglas Gregor849b2432010-03-31 17:46:05 +00001794 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00001795 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00001796 }
1797 }
1798
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001799 ElaboratedTypeKeyword Keyword
1800 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
1801 QualType ElabType = Context.getElaboratedType(Keyword, /*NNS=*/0, Type);
John McCall6b2becf2009-09-08 17:47:29 +00001802
Craig Silverstein45ab4b52010-11-18 08:32:02 +00001803 TypeSourceInfo *ElabDI = Context.CreateTypeSourceInfo(ElabType);
1804 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(ElabDI->getTypeLoc());
1805 TL.setKeywordLoc(TagLoc);
1806 TL.setQualifierRange(SS.getRange());
1807 TL.getNamedTypeLoc().initializeFullCopy(DI->getTypeLoc());
1808 return CreateParsedType(ElabType, ElabDI);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001809}
1810
John McCall60d7b3a2010-08-24 06:29:42 +00001811ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +00001812 LookupResult &R,
1813 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00001814 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001815 // FIXME: Can we do any checking at this point? I guess we could check the
1816 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00001817 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001818 // though.
John McCallf7a1a742009-11-24 19:00:30 +00001819
1820 // These should be filtered out by our callers.
1821 assert(!R.empty() && "empty lookup results when building templateid");
1822 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
1823
1824 NestedNameSpecifier *Qualifier = 0;
1825 SourceRange QualifierRange;
1826 if (SS.isSet()) {
1827 Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
1828 QualifierRange = SS.getRange();
Douglas Gregora9e29aa2009-10-22 07:19:14 +00001829 }
John McCallc373d482010-01-27 01:50:18 +00001830
1831 // We don't want lookup warnings at this point.
1832 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001833
John McCallf7a1a742009-11-24 19:00:30 +00001834 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001835 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
John McCallf7a1a742009-11-24 19:00:30 +00001836 Qualifier, QualifierRange,
Abramo Bagnara25777432010-08-11 22:01:17 +00001837 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001838 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001839 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00001840
1841 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001842}
1843
John McCallf7a1a742009-11-24 19:00:30 +00001844// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00001845ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00001846Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00001847 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00001848 const TemplateArgumentListInfo &TemplateArgs) {
1849 DeclContext *DC;
1850 if (!(DC = computeDeclContext(SS, false)) ||
1851 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00001852 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00001853 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001854
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001855 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00001856 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001857 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
1858 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00001859
John McCallf7a1a742009-11-24 19:00:30 +00001860 if (R.isAmbiguous())
1861 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001862
John McCallf7a1a742009-11-24 19:00:30 +00001863 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001864 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
1865 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001866 return ExprError();
1867 }
1868
1869 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001870 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
1871 << (NestedNameSpecifier*) SS.getScopeRep()
1872 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001873 Diag(Temp->getLocation(), diag::note_referenced_class_template);
1874 return ExprError();
1875 }
1876
1877 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001878}
1879
Douglas Gregorc45c2322009-03-31 00:43:58 +00001880/// \brief Form a dependent template name.
1881///
1882/// This action forms a dependent template name given the template
1883/// name and its (presumably dependent) scope specifier. For
1884/// example, given "MetaFun::template apply", the scope specifier \p
1885/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
1886/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001887TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001888 SourceLocation TemplateKWLoc,
1889 CXXScopeSpec &SS,
1890 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00001891 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001892 bool EnteringContext,
1893 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00001894 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
1895 !getLangOptions().CPlusPlus0x)
1896 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001897 << FixItHint::CreateRemoval(TemplateKWLoc);
1898
Douglas Gregor0707bc52010-01-19 16:01:07 +00001899 DeclContext *LookupCtx = 0;
1900 if (SS.isSet())
1901 LookupCtx = computeDeclContext(SS, EnteringContext);
1902 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00001903 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00001904 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00001905 // C++0x [temp.names]p5:
1906 // If a name prefixed by the keyword template is not the name of
1907 // a template, the program is ill-formed. [Note: the keyword
1908 // template may not be applied to non-template members of class
1909 // templates. -end note ] [ Note: as is the case with the
1910 // typename prefix, the template prefix is allowed in cases
1911 // where it is not strictly necessary; i.e., when the
1912 // nested-name-specifier or the expression on the left of the ->
1913 // or . is not dependent on a template-parameter, or the use
1914 // does not appear in the scope of a template. -end note]
1915 //
1916 // Note: C++03 was more strict here, because it banned the use of
1917 // the "template" keyword prior to a template-name that was not a
1918 // dependent name. C++ DR468 relaxed this requirement (the
1919 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00001920 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001921 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001922 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
1923 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001924 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00001925 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
1926 isa<CXXRecordDecl>(LookupCtx) &&
1927 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00001928 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00001929 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001930 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00001931 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00001932 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00001933 << Name.getSourceRange()
1934 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00001935 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00001936 } else {
1937 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00001938 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00001939 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00001940 }
1941
Mike Stump1eb44332009-09-09 15:08:12 +00001942 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001943 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001944
Douglas Gregor014e88d2009-11-03 23:16:33 +00001945 switch (Name.getKind()) {
1946 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001947 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001948 Name.Identifier));
1949 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001950
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001951 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00001952 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001953 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00001954 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00001955
1956 case UnqualifiedId::IK_LiteralOperatorId:
1957 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
1958
Douglas Gregor014e88d2009-11-03 23:16:33 +00001959 default:
1960 break;
1961 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001962
1963 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00001964 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00001965 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00001966 << Name.getSourceRange()
1967 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00001968 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00001969}
1970
Mike Stump1eb44332009-09-09 15:08:12 +00001971bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00001972 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00001973 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00001974 const TemplateArgument &Arg = AL.getArgument();
1975
Anders Carlsson436b1562009-06-13 00:33:33 +00001976 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00001977 switch(Arg.getKind()) {
1978 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00001979 // C++ [temp.arg.type]p1:
1980 // A template-argument for a template-parameter which is a
1981 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00001982 break;
1983 case TemplateArgument::Template: {
1984 // We have a template type parameter but the template argument
1985 // is a template without any arguments.
1986 SourceRange SR = AL.getSourceRange();
1987 TemplateName Name = Arg.getAsTemplate();
1988 Diag(SR.getBegin(), diag::err_template_missing_args)
1989 << Name << SR;
1990 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
1991 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00001992
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00001993 return true;
1994 }
1995 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00001996 // We have a template type parameter but the template argument
1997 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00001998 SourceRange SR = AL.getSourceRange();
1999 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002000 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002001
Anders Carlsson436b1562009-06-13 00:33:33 +00002002 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002003 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002004 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002005
John McCalla93c9342009-12-07 02:54:59 +00002006 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002007 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002008
Anders Carlsson436b1562009-06-13 00:33:33 +00002009 // Add the converted template type argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002010 Converted.push_back(
John McCall833ca992009-10-29 08:12:44 +00002011 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00002012 return false;
2013}
2014
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002015/// \brief Substitute template arguments into the default template argument for
2016/// the given template type parameter.
2017///
2018/// \param SemaRef the semantic analysis object for which we are performing
2019/// the substitution.
2020///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002021/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002022/// for.
2023///
2024/// \param TemplateLoc the location of the template name that started the
2025/// template-id we are checking.
2026///
2027/// \param RAngleLoc the location of the right angle bracket ('>') that
2028/// terminates the template-id.
2029///
2030/// \param Param the template template parameter whose default we are
2031/// substituting into.
2032///
2033/// \param Converted the list of template arguments provided for template
2034/// parameters that precede \p Param in the template parameter list.
2035///
2036/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002037static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002038SubstDefaultTemplateArgument(Sema &SemaRef,
2039 TemplateDecl *Template,
2040 SourceLocation TemplateLoc,
2041 SourceLocation RAngleLoc,
2042 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002043 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002044 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002045
2046 // If the argument type is dependent, instantiate it now based
2047 // on the previously-computed template arguments.
2048 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002049 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002050 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002051
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002052 MultiLevelTemplateArgumentList AllTemplateArgs
2053 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2054
2055 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002056 Template, Converted.data(),
2057 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002058 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002059
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002060 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2061 Param->getDefaultArgumentLoc(),
2062 Param->getDeclName());
2063 }
2064
2065 return ArgType;
2066}
2067
2068/// \brief Substitute template arguments into the default template argument for
2069/// the given non-type template parameter.
2070///
2071/// \param SemaRef the semantic analysis object for which we are performing
2072/// the substitution.
2073///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002074/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002075/// for.
2076///
2077/// \param TemplateLoc the location of the template name that started the
2078/// template-id we are checking.
2079///
2080/// \param RAngleLoc the location of the right angle bracket ('>') that
2081/// terminates the template-id.
2082///
Douglas Gregor788cd062009-11-11 01:00:40 +00002083/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002084/// substituting into.
2085///
2086/// \param Converted the list of template arguments provided for template
2087/// parameters that precede \p Param in the template parameter list.
2088///
2089/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002090static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002091SubstDefaultTemplateArgument(Sema &SemaRef,
2092 TemplateDecl *Template,
2093 SourceLocation TemplateLoc,
2094 SourceLocation RAngleLoc,
2095 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002096 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002097 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002098 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002099
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002100 MultiLevelTemplateArgumentList AllTemplateArgs
2101 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002102
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002103 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002104 Template, Converted.data(),
2105 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002106 SourceRange(TemplateLoc, RAngleLoc));
2107
2108 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2109}
2110
Douglas Gregor788cd062009-11-11 01:00:40 +00002111/// \brief Substitute template arguments into the default template argument for
2112/// the given template template parameter.
2113///
2114/// \param SemaRef the semantic analysis object for which we are performing
2115/// the substitution.
2116///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002117/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002118/// for.
2119///
2120/// \param TemplateLoc the location of the template name that started the
2121/// template-id we are checking.
2122///
2123/// \param RAngleLoc the location of the right angle bracket ('>') that
2124/// terminates the template-id.
2125///
2126/// \param Param the template template parameter whose default we are
2127/// substituting into.
2128///
2129/// \param Converted the list of template arguments provided for template
2130/// parameters that precede \p Param in the template parameter list.
2131///
2132/// \returns the substituted template argument, or NULL if an error occurred.
2133static TemplateName
2134SubstDefaultTemplateArgument(Sema &SemaRef,
2135 TemplateDecl *Template,
2136 SourceLocation TemplateLoc,
2137 SourceLocation RAngleLoc,
2138 TemplateTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002139 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002140 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002141 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002142
Douglas Gregor788cd062009-11-11 01:00:40 +00002143 MultiLevelTemplateArgumentList AllTemplateArgs
2144 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002145
Douglas Gregor788cd062009-11-11 01:00:40 +00002146 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002147 Template, Converted.data(),
2148 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002149 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002150
Douglas Gregor788cd062009-11-11 01:00:40 +00002151 return SemaRef.SubstTemplateName(
2152 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002153 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002154 AllTemplateArgs);
2155}
2156
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002157/// \brief If the given template parameter has a default template
2158/// argument, substitute into that default template argument and
2159/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002160TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002161Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2162 SourceLocation TemplateLoc,
2163 SourceLocation RAngleLoc,
2164 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002165 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2166 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002167 if (!TypeParm->hasDefaultArgument())
2168 return TemplateArgumentLoc();
2169
John McCalla93c9342009-12-07 02:54:59 +00002170 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002171 TemplateLoc,
2172 RAngleLoc,
2173 TypeParm,
2174 Converted);
2175 if (DI)
2176 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2177
2178 return TemplateArgumentLoc();
2179 }
2180
2181 if (NonTypeTemplateParmDecl *NonTypeParm
2182 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2183 if (!NonTypeParm->hasDefaultArgument())
2184 return TemplateArgumentLoc();
2185
John McCall60d7b3a2010-08-24 06:29:42 +00002186 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002187 TemplateLoc,
2188 RAngleLoc,
2189 NonTypeParm,
2190 Converted);
2191 if (Arg.isInvalid())
2192 return TemplateArgumentLoc();
2193
2194 Expr *ArgE = Arg.takeAs<Expr>();
2195 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2196 }
2197
2198 TemplateTemplateParmDecl *TempTempParm
2199 = cast<TemplateTemplateParmDecl>(Param);
2200 if (!TempTempParm->hasDefaultArgument())
2201 return TemplateArgumentLoc();
2202
2203 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002204 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002205 RAngleLoc,
2206 TempTempParm,
2207 Converted);
2208 if (TName.isNull())
2209 return TemplateArgumentLoc();
2210
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002211 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002212 TempTempParm->getDefaultArgument().getTemplateQualifierRange(),
2213 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2214}
2215
Douglas Gregore7526412009-11-11 19:31:23 +00002216/// \brief Check that the given template argument corresponds to the given
2217/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002218///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002219/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002220/// checked.
2221///
2222/// \param Arg The template argument.
2223///
2224/// \param Template The template in which the template argument resides.
2225///
2226/// \param TemplateLoc The location of the template name for the template
2227/// whose argument list we're matching.
2228///
2229/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2230/// the template argument list.
2231///
2232/// \param ArgumentPackIndex The index into the argument pack where this
2233/// argument will be placed. Only valid if the parameter is a parameter pack.
2234///
2235/// \param Converted The checked, converted argument will be added to the
2236/// end of this small vector.
2237///
2238/// \param CTAK Describes how we arrived at this particular template argument:
2239/// explicitly written, deduced, etc.
2240///
2241/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002242bool Sema::CheckTemplateArgument(NamedDecl *Param,
2243 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002244 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002245 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002246 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002247 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002248 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002249 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002250 // Check template type parameters.
2251 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002252 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002253
Douglas Gregord9e15302009-11-11 19:41:09 +00002254 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002255 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002256 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002257 // with the template arguments we've seen thus far. But if the
2258 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002259 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002260 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2261 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002262
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002263 if (NTTPType->isDependentType() &&
2264 !isa<TemplateTemplateParmDecl>(Template) &&
2265 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002266 // Do substitution on the type of the non-type template parameter.
2267 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002268 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002269 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002270
2271 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002272 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002273 NTTPType = SubstType(NTTPType,
2274 MultiLevelTemplateArgumentList(TemplateArgs),
2275 NTTP->getLocation(),
2276 NTTP->getDeclName());
2277 // If that worked, check the non-type template parameter type
2278 // for validity.
2279 if (!NTTPType.isNull())
2280 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2281 NTTP->getLocation());
2282 if (NTTPType.isNull())
2283 return true;
2284 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002285
Douglas Gregore7526412009-11-11 19:31:23 +00002286 switch (Arg.getArgument().getKind()) {
2287 case TemplateArgument::Null:
2288 assert(false && "Should never see a NULL template argument here");
2289 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002290
Douglas Gregore7526412009-11-11 19:31:23 +00002291 case TemplateArgument::Expression: {
2292 Expr *E = Arg.getArgument().getAsExpr();
2293 TemplateArgument Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002294 if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK))
Douglas Gregore7526412009-11-11 19:31:23 +00002295 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002296
Douglas Gregor910f8002010-11-07 23:05:16 +00002297 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002298 break;
2299 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002300
Douglas Gregore7526412009-11-11 19:31:23 +00002301 case TemplateArgument::Declaration:
2302 case TemplateArgument::Integral:
2303 // We've already checked this template argument, so just copy
2304 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002305 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002306 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002307
Douglas Gregore7526412009-11-11 19:31:23 +00002308 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002309 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002310 // We were given a template template argument. It may not be ill-formed;
2311 // see below.
2312 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002313 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2314 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002315 // We have a template argument such as \c T::template X, which we
2316 // parsed as a template template argument. However, since we now
2317 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002318 // template name into an expression.
2319
2320 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2321 Arg.getTemplateNameLoc());
2322
John McCallf7a1a742009-11-24 19:00:30 +00002323 Expr *E = DependentScopeDeclRefExpr::Create(Context,
2324 DTN->getQualifier(),
Douglas Gregore7526412009-11-11 19:31:23 +00002325 Arg.getTemplateQualifierRange(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002326 NameInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002327
Douglas Gregora7fc9012011-01-05 18:58:31 +00002328 // If we parsed the template argument as a pack expansion, create a
2329 // pack expansion expression.
2330 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002331 ExprResult Expansion = ActOnPackExpansion(E,
Douglas Gregora7fc9012011-01-05 18:58:31 +00002332 Arg.getTemplateEllipsisLoc());
2333 if (Expansion.isInvalid())
2334 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002335
Douglas Gregora7fc9012011-01-05 18:58:31 +00002336 E = Expansion.get();
2337 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002338
Douglas Gregore7526412009-11-11 19:31:23 +00002339 TemplateArgument Result;
2340 if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
2341 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002342
Douglas Gregor910f8002010-11-07 23:05:16 +00002343 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002344 break;
2345 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002346
Douglas Gregore7526412009-11-11 19:31:23 +00002347 // We have a template argument that actually does refer to a class
2348 // template, template alias, or template template parameter, and
2349 // therefore cannot be a non-type template argument.
2350 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2351 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002352
Douglas Gregore7526412009-11-11 19:31:23 +00002353 Diag(Param->getLocation(), diag::note_template_param_here);
2354 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002355
Douglas Gregore7526412009-11-11 19:31:23 +00002356 case TemplateArgument::Type: {
2357 // We have a non-type template parameter but the template
2358 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002359
Douglas Gregore7526412009-11-11 19:31:23 +00002360 // C++ [temp.arg]p2:
2361 // In a template-argument, an ambiguity between a type-id and
2362 // an expression is resolved to a type-id, regardless of the
2363 // form of the corresponding template-parameter.
2364 //
2365 // We warn specifically about this case, since it can be rather
2366 // confusing for users.
2367 QualType T = Arg.getArgument().getAsType();
2368 SourceRange SR = Arg.getSourceRange();
2369 if (T->isFunctionType())
2370 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2371 else
2372 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2373 Diag(Param->getLocation(), diag::note_template_param_here);
2374 return true;
2375 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002376
Douglas Gregore7526412009-11-11 19:31:23 +00002377 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002378 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002379 break;
2380 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002381
Douglas Gregore7526412009-11-11 19:31:23 +00002382 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002383 }
2384
2385
Douglas Gregore7526412009-11-11 19:31:23 +00002386 // Check template template parameters.
2387 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002388
Douglas Gregore7526412009-11-11 19:31:23 +00002389 // Substitute into the template parameter list of the template
2390 // template parameter, since previously-supplied template arguments
2391 // may appear within the template template parameter.
2392 {
2393 // Set up a template instantiation context.
2394 LocalInstantiationScope Scope(*this);
2395 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002396 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002397 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002398
2399 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002400 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002401 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002402 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002403 MultiLevelTemplateArgumentList(TemplateArgs)));
2404 if (!TempParm)
2405 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002406 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002407
Douglas Gregore7526412009-11-11 19:31:23 +00002408 switch (Arg.getArgument().getKind()) {
2409 case TemplateArgument::Null:
2410 assert(false && "Should never see a NULL template argument here");
2411 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002412
Douglas Gregore7526412009-11-11 19:31:23 +00002413 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002414 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002415 if (CheckTemplateArgument(TempParm, Arg))
2416 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002417
Douglas Gregor910f8002010-11-07 23:05:16 +00002418 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002419 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002420
Douglas Gregore7526412009-11-11 19:31:23 +00002421 case TemplateArgument::Expression:
2422 case TemplateArgument::Type:
2423 // We have a template template parameter but the template
2424 // argument does not refer to a template.
2425 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
2426 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002427
Douglas Gregore7526412009-11-11 19:31:23 +00002428 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002429 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002430 "Declaration argument with template template parameter");
2431 break;
2432 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002433 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002434 "Integral argument with template template parameter");
2435 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002436
Douglas Gregore7526412009-11-11 19:31:23 +00002437 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002438 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002439 break;
2440 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002441
Douglas Gregore7526412009-11-11 19:31:23 +00002442 return false;
2443}
2444
Douglas Gregorc15cb382009-02-09 23:23:08 +00002445/// \brief Check that the given template argument list is well-formed
2446/// for specializing the given template.
2447bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2448 SourceLocation TemplateLoc,
John McCalld5532b62009-11-23 01:53:49 +00002449 const TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002450 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002451 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002452 TemplateParameterList *Params = Template->getTemplateParameters();
2453 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002454 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002455 bool Invalid = false;
2456
John McCalld5532b62009-11-23 01:53:49 +00002457 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2458
Mike Stump1eb44332009-09-09 15:08:12 +00002459 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002460 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002461
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002462 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002463 (NumArgs < Params->getMinRequiredArguments() &&
2464 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002465 // FIXME: point at either the first arg beyond what we can handle,
2466 // or the '>', depending on whether we have too many or too few
2467 // arguments.
2468 SourceRange Range;
2469 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002470 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002471 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2472 << (NumArgs > NumParams)
2473 << (isa<ClassTemplateDecl>(Template)? 0 :
2474 isa<FunctionTemplateDecl>(Template)? 1 :
2475 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2476 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002477 Diag(Template->getLocation(), diag::note_template_decl_here)
2478 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002479 Invalid = true;
2480 }
Mike Stump1eb44332009-09-09 15:08:12 +00002481
2482 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002483 // [...] The type and form of each template-argument specified in
2484 // a template-id shall match the type and form specified for the
2485 // corresponding parameter declared by the template in its
2486 // template-parameter-list.
Douglas Gregor14be16b2010-12-20 16:57:52 +00002487 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2488 TemplateParameterList::iterator Param = Params->begin(),
2489 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002490 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002491 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002492 while (Param != ParamEnd) {
Douglas Gregor16134c62009-07-01 00:28:38 +00002493 if (ArgIdx > NumArgs && PartialTemplateArgs)
2494 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002495
Douglas Gregorf35f8282009-11-11 21:54:23 +00002496 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002497 // If we have an expanded parameter pack, make sure we don't have too
2498 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002499 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002500 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002501 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002502 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2503 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2504 << true
2505 << (isa<ClassTemplateDecl>(Template)? 0 :
2506 isa<FunctionTemplateDecl>(Template)? 1 :
2507 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2508 << Template;
2509 Diag(Template->getLocation(), diag::note_template_decl_here)
2510 << Params->getSourceRange();
2511 return true;
2512 }
2513 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002514
Douglas Gregorf35f8282009-11-11 21:54:23 +00002515 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002516 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2517 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002518 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002519 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002520
Douglas Gregor14be16b2010-12-20 16:57:52 +00002521 if ((*Param)->isTemplateParameterPack()) {
2522 // The template parameter was a template parameter pack, so take the
2523 // deduced argument and place it on the argument pack. Note that we
2524 // stay on the same template parameter so that we can deduce more
2525 // arguments.
2526 ArgumentPack.push_back(Converted.back());
2527 Converted.pop_back();
2528 } else {
2529 // Move to the next template parameter.
2530 ++Param;
2531 }
2532 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002533 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002534 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002535
2536 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002537 // arguments, just break out now and we'll fill in the argument pack below.
2538 if ((*Param)->isTemplateParameterPack())
2539 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002540
Douglas Gregorf35f8282009-11-11 21:54:23 +00002541 // We have a default template argument that we will use.
2542 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002543
Douglas Gregorf35f8282009-11-11 21:54:23 +00002544 // Retrieve the default template argument from the template
2545 // parameter. For each kind of template parameter, we substitute the
2546 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002547 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002548 // the default argument.
2549 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2550 if (!TTP->hasDefaultArgument()) {
2551 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2552 break;
2553 }
2554
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002555 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002556 Template,
2557 TemplateLoc,
2558 RAngleLoc,
2559 TTP,
2560 Converted);
2561 if (!ArgType)
2562 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002563
Douglas Gregorf35f8282009-11-11 21:54:23 +00002564 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2565 ArgType);
2566 } else if (NonTypeTemplateParmDecl *NTTP
2567 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2568 if (!NTTP->hasDefaultArgument()) {
2569 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2570 break;
2571 }
2572
John McCall60d7b3a2010-08-24 06:29:42 +00002573 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002574 TemplateLoc,
2575 RAngleLoc,
2576 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002577 Converted);
2578 if (E.isInvalid())
2579 return true;
2580
2581 Expr *Ex = E.takeAs<Expr>();
2582 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2583 } else {
2584 TemplateTemplateParmDecl *TempParm
2585 = cast<TemplateTemplateParmDecl>(*Param);
2586
2587 if (!TempParm->hasDefaultArgument()) {
2588 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2589 break;
2590 }
2591
2592 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002593 TemplateLoc,
2594 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002595 TempParm,
2596 Converted);
2597 if (Name.isNull())
2598 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002599
2600 Arg = TemplateArgumentLoc(TemplateArgument(Name),
Douglas Gregorf35f8282009-11-11 21:54:23 +00002601 TempParm->getDefaultArgument().getTemplateQualifierRange(),
2602 TempParm->getDefaultArgument().getTemplateNameLoc());
2603 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002604
Douglas Gregorf35f8282009-11-11 21:54:23 +00002605 // Introduce an instantiation record that describes where we are using
2606 // the default template argument.
2607 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002608 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002609 SourceRange(TemplateLoc, RAngleLoc));
2610
Douglas Gregorf35f8282009-11-11 21:54:23 +00002611 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002612 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002613 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00002614 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002615
Douglas Gregor14be16b2010-12-20 16:57:52 +00002616 // Move to the next template parameter and argument.
2617 ++Param;
2618 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002619 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002620
Douglas Gregor14be16b2010-12-20 16:57:52 +00002621 // Form argument packs for each of the parameter packs remaining.
2622 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00002623 // If we're checking a partial list of template arguments, don't fill
2624 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002625
2626 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002627 if (PartialTemplateArgs && ArgumentPack.empty()) {
2628 Converted.push_back(TemplateArgument());
Douglas Gregor203e6a32011-01-11 23:09:57 +00002629 } else if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00002630 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00002631 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002632 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2633 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00002634 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00002635 ArgumentPack.clear();
2636 }
2637 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002638
Douglas Gregor14be16b2010-12-20 16:57:52 +00002639 ++Param;
2640 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002641
Douglas Gregorc15cb382009-02-09 23:23:08 +00002642 return Invalid;
2643}
2644
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002645namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002646 class UnnamedLocalNoLinkageFinder
2647 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002648 {
2649 Sema &S;
2650 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002651
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002652 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002653
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002654 public:
2655 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
2656
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002657 bool Visit(QualType T) {
2658 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002659 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002660
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002661#define TYPE(Class, Parent) \
2662 bool Visit##Class##Type(const Class##Type *);
2663#define ABSTRACT_TYPE(Class, Parent) \
2664 bool Visit##Class##Type(const Class##Type *) { return false; }
2665#define NON_CANONICAL_TYPE(Class, Parent) \
2666 bool Visit##Class##Type(const Class##Type *) { return false; }
2667#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002668
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002669 bool VisitTagDecl(const TagDecl *Tag);
2670 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
2671 };
2672}
2673
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002674bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002675 return false;
2676}
2677
2678bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
2679 return Visit(T->getElementType());
2680}
2681
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002682bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002683 return Visit(T->getPointeeType());
2684}
2685
2686bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002687 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002688 return Visit(T->getPointeeType());
2689}
2690
2691bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002692 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002693 return Visit(T->getPointeeType());
2694}
2695
2696bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002697 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002698 return Visit(T->getPointeeType());
2699}
2700
2701bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002702 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002703 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
2704}
2705
2706bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002707 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002708 return Visit(T->getElementType());
2709}
2710
2711bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002712 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002713 return Visit(T->getElementType());
2714}
2715
2716bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002717 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002718 return Visit(T->getElementType());
2719}
2720
2721bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002722 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002723 return Visit(T->getElementType());
2724}
2725
2726bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002727 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002728 return Visit(T->getElementType());
2729}
2730
2731bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
2732 return Visit(T->getElementType());
2733}
2734
2735bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
2736 return Visit(T->getElementType());
2737}
2738
2739bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
2740 const FunctionProtoType* T) {
2741 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002742 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002743 A != AEnd; ++A) {
2744 if (Visit(*A))
2745 return true;
2746 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002747
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002748 return Visit(T->getResultType());
2749}
2750
2751bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
2752 const FunctionNoProtoType* T) {
2753 return Visit(T->getResultType());
2754}
2755
2756bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
2757 const UnresolvedUsingType*) {
2758 return false;
2759}
2760
2761bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
2762 return false;
2763}
2764
2765bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
2766 return Visit(T->getUnderlyingType());
2767}
2768
2769bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
2770 return false;
2771}
2772
2773bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
2774 return VisitTagDecl(T->getDecl());
2775}
2776
2777bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
2778 return VisitTagDecl(T->getDecl());
2779}
2780
2781bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
2782 const TemplateTypeParmType*) {
2783 return false;
2784}
2785
Douglas Gregorc3069d62011-01-14 02:55:32 +00002786bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
2787 const SubstTemplateTypeParmPackType *) {
2788 return false;
2789}
2790
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002791bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
2792 const TemplateSpecializationType*) {
2793 return false;
2794}
2795
2796bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
2797 const InjectedClassNameType* T) {
2798 return VisitTagDecl(T->getDecl());
2799}
2800
2801bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
2802 const DependentNameType* T) {
2803 return VisitNestedNameSpecifier(T->getQualifier());
2804}
2805
2806bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
2807 const DependentTemplateSpecializationType* T) {
2808 return VisitNestedNameSpecifier(T->getQualifier());
2809}
2810
Douglas Gregor7536dd52010-12-20 02:24:11 +00002811bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
2812 const PackExpansionType* T) {
2813 return Visit(T->getPattern());
2814}
2815
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002816bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
2817 return false;
2818}
2819
2820bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
2821 const ObjCInterfaceType *) {
2822 return false;
2823}
2824
2825bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
2826 const ObjCObjectPointerType *) {
2827 return false;
2828}
2829
2830bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
2831 if (Tag->getDeclContext()->isFunctionOrMethod()) {
2832 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
2833 << S.Context.getTypeDeclType(Tag) << SR;
2834 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002835 }
2836
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002837 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) {
2838 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
2839 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
2840 return true;
2841 }
2842
2843 return false;
2844}
2845
2846bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
2847 NestedNameSpecifier *NNS) {
2848 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
2849 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002850
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002851 switch (NNS->getKind()) {
2852 case NestedNameSpecifier::Identifier:
2853 case NestedNameSpecifier::Namespace:
2854 case NestedNameSpecifier::Global:
2855 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002856
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002857 case NestedNameSpecifier::TypeSpec:
2858 case NestedNameSpecifier::TypeSpecWithTemplate:
2859 return Visit(QualType(NNS->getAsType(), 0));
2860 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00002861 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002862}
2863
2864
Douglas Gregorc15cb382009-02-09 23:23:08 +00002865/// \brief Check a template argument against its corresponding
2866/// template type parameter.
2867///
2868/// This routine implements the semantics of C++ [temp.arg.type]. It
2869/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00002870bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00002871 TypeSourceInfo *ArgInfo) {
2872 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00002873 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00002874 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00002875
2876 if (Arg->isVariablyModifiedType()) {
2877 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00002878 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00002879 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002880 }
2881
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002882 // C++03 [temp.arg.type]p2:
2883 // A local type, a type with no linkage, an unnamed type or a type
2884 // compounded from any of these types shall not be used as a
2885 // template-argument for a template type-parameter.
2886 //
2887 // C++0x allows these, and even in C++03 we allow them as an extension with
2888 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00002889 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002890 UnnamedLocalNoLinkageFinder Finder(*this, SR);
2891 (void)Finder.Visit(Context.getCanonicalType(Arg));
2892 }
2893
Douglas Gregorc15cb382009-02-09 23:23:08 +00002894 return false;
2895}
2896
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002897/// \brief Checks whether the given template argument is the address
2898/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002899static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00002900CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
2901 NonTypeTemplateParmDecl *Param,
2902 QualType ParamType,
2903 Expr *ArgIn,
2904 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002905 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00002906 Expr *Arg = ArgIn;
2907 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002908
2909 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00002910 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002911 Arg = Cast->getSubExpr();
2912
2913 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00002914 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002915 // A template-argument for a non-type, non-template
2916 // template-parameter shall be one of: [...]
2917 //
2918 // -- the address of an object or function with external
2919 // linkage, including function templates and function
2920 // template-ids but excluding non-static class members,
2921 // expressed as & id-expression where the & is optional if
2922 // the name refers to a function or array, or if the
2923 // corresponding template-parameter is a reference; or
2924 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002925
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002926 // In C++98/03 mode, give an extension warning on any extra parentheses.
2927 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
2928 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002929 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002930 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00002931 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002932 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002933 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002934 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002935 }
2936
2937 Arg = Parens->getSubExpr();
2938 }
2939
Douglas Gregorb7a09262010-04-01 18:32:35 +00002940 bool AddressTaken = false;
2941 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002942 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00002943 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002944 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00002945 AddressTaken = true;
2946 AddrOpLoc = UnOp->getOperatorLoc();
2947 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002948 } else
2949 DRE = dyn_cast<DeclRefExpr>(Arg);
2950
Douglas Gregorb7a09262010-04-01 18:32:35 +00002951 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00002952 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
2953 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00002954 S.Diag(Param->getLocation(), diag::note_template_param_here);
2955 return true;
2956 }
Chandler Carruth038cc392010-01-31 10:01:20 +00002957
2958 // Stop checking the precise nature of the argument if it is value dependent,
2959 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00002960 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00002961 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00002962 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00002963 }
Chandler Carruth038cc392010-01-31 10:01:20 +00002964
Douglas Gregorb7a09262010-04-01 18:32:35 +00002965 if (!isa<ValueDecl>(DRE->getDecl())) {
2966 S.Diag(Arg->getSourceRange().getBegin(),
2967 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002968 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00002969 S.Diag(Param->getLocation(), diag::note_template_param_here);
2970 return true;
2971 }
2972
2973 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002974
2975 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00002976 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
2977 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002978 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00002979 S.Diag(Param->getLocation(), diag::note_template_param_here);
2980 return true;
2981 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002982
2983 // Cannot refer to non-static member functions
2984 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00002985 if (!Method->isStatic()) {
2986 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002987 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00002988 S.Diag(Param->getLocation(), diag::note_template_param_here);
2989 return true;
2990 }
Mike Stump1eb44332009-09-09 15:08:12 +00002991
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002992 // Functions must have external linkage.
2993 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00002994 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00002995 S.Diag(Arg->getSourceRange().getBegin(),
2996 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002997 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00002998 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002999 << true;
3000 return true;
3001 }
3002
3003 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003004 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003005
Douglas Gregorb7a09262010-04-01 18:32:35 +00003006 // If the template parameter has pointer type, the function decays.
3007 if (ParamType->isPointerType() && !AddressTaken)
3008 ArgType = S.Context.getPointerType(Func->getType());
3009 else if (AddressTaken && ParamType->isReferenceType()) {
3010 // If we originally had an address-of operator, but the
3011 // parameter has reference type, complain and (if things look
3012 // like they will work) drop the address-of operator.
3013 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3014 ParamType.getNonReferenceType())) {
3015 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3016 << ParamType;
3017 S.Diag(Param->getLocation(), diag::note_template_param_here);
3018 return true;
3019 }
3020
3021 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3022 << ParamType
3023 << FixItHint::CreateRemoval(AddrOpLoc);
3024 S.Diag(Param->getLocation(), diag::note_template_param_here);
3025
3026 ArgType = Func->getType();
3027 }
3028 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003029 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003030 S.Diag(Arg->getSourceRange().getBegin(),
3031 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003032 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003033 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003034 << true;
3035 return true;
3036 }
3037
Douglas Gregorb7a09262010-04-01 18:32:35 +00003038 // A value of reference type is not an object.
3039 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003040 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003041 diag::err_template_arg_reference_var)
3042 << Var->getType() << Arg->getSourceRange();
3043 S.Diag(Param->getLocation(), diag::note_template_param_here);
3044 return true;
3045 }
3046
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003047 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003048 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003049
3050 // If the template parameter has pointer type, we must have taken
3051 // the address of this object.
3052 if (ParamType->isReferenceType()) {
3053 if (AddressTaken) {
3054 // If we originally had an address-of operator, but the
3055 // parameter has reference type, complain and (if things look
3056 // like they will work) drop the address-of operator.
3057 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3058 ParamType.getNonReferenceType())) {
3059 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3060 << ParamType;
3061 S.Diag(Param->getLocation(), diag::note_template_param_here);
3062 return true;
3063 }
3064
3065 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3066 << ParamType
3067 << FixItHint::CreateRemoval(AddrOpLoc);
3068 S.Diag(Param->getLocation(), diag::note_template_param_here);
3069
3070 ArgType = Var->getType();
3071 }
3072 } else if (!AddressTaken && ParamType->isPointerType()) {
3073 if (Var->getType()->isArrayType()) {
3074 // Array-to-pointer decay.
3075 ArgType = S.Context.getArrayDecayedType(Var->getType());
3076 } else {
3077 // If the template parameter has pointer type but the address of
3078 // this object was not taken, complain and (possibly) recover by
3079 // taking the address of the entity.
3080 ArgType = S.Context.getPointerType(Var->getType());
3081 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3082 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3083 << ParamType;
3084 S.Diag(Param->getLocation(), diag::note_template_param_here);
3085 return true;
3086 }
3087
3088 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3089 << ParamType
3090 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3091
3092 S.Diag(Param->getLocation(), diag::note_template_param_here);
3093 }
3094 }
3095 } else {
3096 // We found something else, but we don't know specifically what it is.
3097 S.Diag(Arg->getSourceRange().getBegin(),
3098 diag::err_template_arg_not_object_or_func)
3099 << Arg->getSourceRange();
3100 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3101 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003102 }
Mike Stump1eb44332009-09-09 15:08:12 +00003103
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003104 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003105 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003106 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003107 // For pointer-to-object types, qualification conversions are
3108 // permitted.
3109 } else {
3110 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3111 if (!ParamRef->getPointeeType()->isFunctionType()) {
3112 // C++ [temp.arg.nontype]p5b3:
3113 // For a non-type template-parameter of type reference to
3114 // object, no conversions apply. The type referred to by the
3115 // reference may be more cv-qualified than the (otherwise
3116 // identical) type of the template- argument. The
3117 // template-parameter is bound directly to the
3118 // template-argument, which shall be an lvalue.
3119
3120 // FIXME: Other qualifiers?
3121 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3122 unsigned ArgQuals = ArgType.getCVRQualifiers();
3123
3124 if ((ParamQuals | ArgQuals) != ParamQuals) {
3125 S.Diag(Arg->getSourceRange().getBegin(),
3126 diag::err_template_arg_ref_bind_ignores_quals)
3127 << ParamType << Arg->getType()
3128 << Arg->getSourceRange();
3129 S.Diag(Param->getLocation(), diag::note_template_param_here);
3130 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003131 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003132 }
3133 }
3134
3135 // At this point, the template argument refers to an object or
3136 // function with external linkage. We now need to check whether the
3137 // argument and parameter types are compatible.
3138 if (!S.Context.hasSameUnqualifiedType(ArgType,
3139 ParamType.getNonReferenceType())) {
3140 // We can't perform this conversion or binding.
3141 if (ParamType->isReferenceType())
3142 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3143 << ParamType << Arg->getType() << Arg->getSourceRange();
3144 else
3145 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3146 << Arg->getType() << ParamType << Arg->getSourceRange();
3147 S.Diag(Param->getLocation(), diag::note_template_param_here);
3148 return true;
3149 }
3150 }
3151
3152 // Create the template argument.
3153 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003154 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003155 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003156}
3157
3158/// \brief Checks whether the given template argument is a pointer to
3159/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003160bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003161 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003162 bool Invalid = false;
3163
3164 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003165 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003166 Arg = Cast->getSubExpr();
3167
3168 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003169 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003170 // A template-argument for a non-type, non-template
3171 // template-parameter shall be one of: [...]
3172 //
3173 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003174 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003175
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003176 // In C++98/03 mode, give an extension warning on any extra parentheses.
3177 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3178 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003179 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003180 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003181 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003182 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003183 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003184 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003185 }
3186
3187 Arg = Parens->getSubExpr();
3188 }
3189
Douglas Gregorcaddba02009-11-12 18:38:13 +00003190 // A pointer-to-member constant written &Class::member.
3191 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003192 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003193 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3194 if (DRE && !DRE->getQualifier())
3195 DRE = 0;
3196 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003197 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003198 // A constant of pointer-to-member type.
3199 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3200 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3201 if (VD->getType()->isMemberPointerType()) {
3202 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003203 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003204 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3205 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003206 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003207 else
3208 Converted = TemplateArgument(VD->getCanonicalDecl());
3209 return Invalid;
3210 }
3211 }
3212 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003213
Douglas Gregorcaddba02009-11-12 18:38:13 +00003214 DRE = 0;
3215 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003216
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003217 if (!DRE)
3218 return Diag(Arg->getSourceRange().getBegin(),
3219 diag::err_template_arg_not_pointer_to_member_form)
3220 << Arg->getSourceRange();
3221
3222 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3223 assert((isa<FieldDecl>(DRE->getDecl()) ||
3224 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3225 "Only non-static member pointers can make it here");
3226
3227 // Okay: this is the address of a non-static member, and therefore
3228 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003229 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003230 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003231 else
3232 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003233 return Invalid;
3234 }
3235
3236 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003237 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003238 diag::err_template_arg_not_pointer_to_member_form)
3239 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003240 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003241 diag::note_template_arg_refers_here);
3242 return true;
3243}
3244
Douglas Gregorc15cb382009-02-09 23:23:08 +00003245/// \brief Check a template argument against its corresponding
3246/// non-type template parameter.
3247///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003248/// This routine implements the semantics of C++ [temp.arg.nontype].
3249/// It returns true if an error occurred, and false otherwise. \p
3250/// InstantiatedParamType is the type of the non-type template
3251/// parameter after it has been instantiated.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003252///
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003253/// If no error was detected, Converted receives the converted template argument.
Douglas Gregorc15cb382009-02-09 23:23:08 +00003254bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
Mike Stump1eb44332009-09-09 15:08:12 +00003255 QualType InstantiatedParamType, Expr *&Arg,
Douglas Gregor02024a92010-03-28 02:42:43 +00003256 TemplateArgument &Converted,
3257 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003258 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3259
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003260 // If either the parameter has a dependent type or the argument is
3261 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003262 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3263 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003264 Converted = TemplateArgument(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003265 return false;
Douglas Gregor40808ce2009-03-09 23:48:35 +00003266 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003267
3268 // C++ [temp.arg.nontype]p5:
3269 // The following conversions are performed on each expression used
3270 // as a non-type template-argument. If a non-type
3271 // template-argument cannot be converted to the type of the
3272 // corresponding template-parameter then the program is
3273 // ill-formed.
3274 //
3275 // -- for a non-type template-parameter of integral or
3276 // enumeration type, integral promotions (4.5) and integral
3277 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003278 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003279 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003280 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003281 // C++ [temp.arg.nontype]p1:
3282 // A template-argument for a non-type, non-template
3283 // template-parameter shall be one of:
3284 //
3285 // -- an integral constant-expression of integral or enumeration
3286 // type; or
3287 // -- the name of a non-type template-parameter; or
3288 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003289 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003290 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003291 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003292 diag::err_template_arg_not_integral_or_enumeral)
3293 << ArgType << Arg->getSourceRange();
3294 Diag(Param->getLocation(), diag::note_template_param_here);
3295 return true;
3296 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003297 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003298 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3299 << ArgType << Arg->getSourceRange();
3300 return true;
3301 }
3302
Douglas Gregor02024a92010-03-28 02:42:43 +00003303 // From here on out, all we care about are the unqualified forms
3304 // of the parameter and argument types.
3305 ParamType = ParamType.getUnqualifiedType();
3306 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003307
3308 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003309 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003310 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003311 } else if (CTAK == CTAK_Deduced) {
3312 // C++ [temp.deduct.type]p17:
3313 // If, in the declaration of a function template with a non-type
3314 // template-parameter, the non-type template- parameter is used
3315 // in an expression in the function parameter-list and, if the
3316 // corresponding template-argument is deduced, the
3317 // template-argument type shall match the type of the
3318 // template-parameter exactly, except that a template-argument
3319 // deduced from an array bound may be of any integral type.
3320 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3321 << ArgType << ParamType;
3322 Diag(Param->getLocation(), diag::note_template_param_here);
John McCalldaa8e4e2010-11-15 09:13:47 +00003323 return true;
3324 } else if (ParamType->isBooleanType()) {
3325 // This is an integral-to-boolean conversion.
3326 ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003327 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3328 !ParamType->isEnumeralType()) {
3329 // This is an integral promotion or conversion.
John McCall2de56d12010-08-25 11:45:40 +00003330 ImpCastExprToType(Arg, ParamType, CK_IntegralCast);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003331 } else {
3332 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003333 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003334 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003335 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003336 Diag(Param->getLocation(), diag::note_template_param_here);
3337 return true;
3338 }
3339
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003340 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003341 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003342 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003343
3344 if (!Arg->isValueDependent()) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003345 llvm::APSInt OldValue = Value;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003346
3347 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003348 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003349 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003350 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003351 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003352 Value.setIsSigned(IntegerType->isSignedIntegerType());
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003353
3354 // Complain if an unsigned parameter received a negative value.
3355 if (IntegerType->isUnsignedIntegerType()
3356 && (OldValue.isSigned() && OldValue.isNegative())) {
3357 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3358 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3359 << Arg->getSourceRange();
3360 Diag(Param->getLocation(), diag::note_template_param_here);
3361 }
3362
3363 // Complain if we overflowed the template parameter's type.
3364 unsigned RequiredBits;
3365 if (IntegerType->isUnsignedIntegerType())
3366 RequiredBits = OldValue.getActiveBits();
3367 else if (OldValue.isUnsigned())
3368 RequiredBits = OldValue.getActiveBits() + 1;
3369 else
3370 RequiredBits = OldValue.getMinSignedBits();
3371 if (RequiredBits > AllowedBits) {
3372 Diag(Arg->getSourceRange().getBegin(),
3373 diag::warn_template_arg_too_large)
3374 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3375 << Arg->getSourceRange();
3376 Diag(Param->getLocation(), diag::note_template_param_here);
3377 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003378 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003379
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003380 // Add the value of this argument to the list of converted
3381 // arguments. We use the bitwidth and signedness of the template
3382 // parameter.
3383 if (Arg->isValueDependent()) {
3384 // The argument is value-dependent. Create a new
3385 // TemplateArgument with the converted expression.
3386 Converted = TemplateArgument(Arg);
3387 return false;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003388 }
3389
John McCall833ca992009-10-29 08:12:44 +00003390 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003391 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003392 : IntegerType);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003393 return false;
3394 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003395
John McCall6bb80172010-03-30 21:47:33 +00003396 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3397
Douglas Gregorb7a09262010-04-01 18:32:35 +00003398 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3399 // from a template argument of type std::nullptr_t to a non-type
3400 // template parameter of type pointer to object, pointer to
3401 // function, or pointer-to-member, respectively.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003402 if (ArgType->isNullPtrType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003403 (ParamType->isPointerType() || ParamType->isMemberPointerType())) {
3404 Converted = TemplateArgument((NamedDecl *)0);
3405 return false;
3406 }
3407
Douglas Gregorb86b0572009-02-11 01:18:59 +00003408 // Handle pointer-to-function, reference-to-function, and
3409 // pointer-to-member-function all in (roughly) the same way.
3410 if (// -- For a non-type template-parameter of type pointer to
3411 // function, only the function-to-pointer conversion (4.3) is
3412 // applied. If the template-argument represents a set of
3413 // overloaded functions (or a pointer to such), the matching
3414 // function is selected from the set (13.4).
3415 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003416 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003417 // -- For a non-type template-parameter of type reference to
3418 // function, no conversions apply. If the template-argument
3419 // represents a set of overloaded functions, the matching
3420 // function is selected from the set (13.4).
3421 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003422 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003423 // -- For a non-type template-parameter of type pointer to
3424 // member function, no conversions apply. If the
3425 // template-argument represents a set of overloaded member
3426 // functions, the matching member function is selected from
3427 // the set (13.4).
3428 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003429 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003430 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003431
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003432 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003433 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003434 true,
3435 FoundResult)) {
3436 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3437 return true;
3438
3439 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3440 ArgType = Arg->getType();
3441 } else
Douglas Gregor48f3bb92009-02-18 21:56:37 +00003442 return true;
Douglas Gregora35284b2009-02-11 00:19:33 +00003443 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003444
Douglas Gregorb7a09262010-04-01 18:32:35 +00003445 if (!ParamType->isMemberPointerType())
3446 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003447 ParamType,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003448 Arg, Converted);
3449
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003450 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3451 false)) {
John McCall2de56d12010-08-25 11:45:40 +00003452 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregorb7a09262010-04-01 18:32:35 +00003453 } else if (!Context.hasSameUnqualifiedType(ArgType,
3454 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003455 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003456 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003457 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003458 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003459 Diag(Param->getLocation(), diag::note_template_param_here);
3460 return true;
3461 }
Mike Stump1eb44332009-09-09 15:08:12 +00003462
Douglas Gregorb7a09262010-04-01 18:32:35 +00003463 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregora35284b2009-02-11 00:19:33 +00003464 }
3465
Chris Lattnerfe90de72009-02-20 21:37:53 +00003466 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003467 // -- for a non-type template-parameter of type pointer to
3468 // object, qualification conversions (4.4) and the
3469 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003470 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003471 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003472 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003473
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003474 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003475 ParamType,
3476 Arg, Converted);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003477 }
Mike Stump1eb44332009-09-09 15:08:12 +00003478
Ted Kremenek6217b802009-07-29 21:53:49 +00003479 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003480 // -- For a non-type template-parameter of type reference to
3481 // object, no conversions apply. The type referred to by the
3482 // reference may be more cv-qualified than the (otherwise
3483 // identical) type of the template-argument. The
3484 // template-parameter is bound directly to the
3485 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003486 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003487 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003488
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003489 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003490 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3491 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003492 true,
3493 FoundResult)) {
3494 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3495 return true;
3496
3497 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3498 ArgType = Arg->getType();
3499 } else
Douglas Gregorb7a09262010-04-01 18:32:35 +00003500 return true;
Douglas Gregorb86b0572009-02-11 01:18:59 +00003501 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003502
3503 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003504 ParamType,
3505 Arg, Converted);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003506 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003507
3508 // -- For a non-type template-parameter of type pointer to data
3509 // member, qualification conversions (4.4) are applied.
3510 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3511
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003512 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003513 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003514 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John McCall2de56d12010-08-25 11:45:40 +00003515 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregor658bbb52009-02-11 16:16:59 +00003516 } else {
3517 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003518 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003519 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003520 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003521 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00003522 return true;
Douglas Gregor658bbb52009-02-11 16:16:59 +00003523 }
3524
Douglas Gregorcaddba02009-11-12 18:38:13 +00003525 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003526}
3527
3528/// \brief Check a template argument against its corresponding
3529/// template template parameter.
3530///
3531/// This routine implements the semantics of C++ [temp.arg.template].
3532/// It returns true if an error occurred, and false otherwise.
3533bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003534 const TemplateArgumentLoc &Arg) {
3535 TemplateName Name = Arg.getArgument().getAsTemplate();
3536 TemplateDecl *Template = Name.getAsTemplateDecl();
3537 if (!Template) {
3538 // Any dependent template name is fine.
3539 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3540 return false;
3541 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003542
3543 // C++ [temp.arg.template]p1:
3544 // A template-argument for a template template-parameter shall be
3545 // the name of a class template, expressed as id-expression. Only
3546 // primary class templates are considered when matching the
3547 // template template argument with the corresponding parameter;
3548 // partial specializations are not considered even if their
3549 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003550 //
3551 // Note that we also allow template template parameters here, which
3552 // will happen when we are dealing with, e.g., class template
3553 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003554 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003555 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003556 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003557 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003558 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003559 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003560 << Template;
3561 }
3562
3563 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
3564 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003565 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003566 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00003567 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00003568}
3569
Douglas Gregor02024a92010-03-28 02:42:43 +00003570/// \brief Given a non-type template argument that refers to a
3571/// declaration and the type of its corresponding non-type template
3572/// parameter, produce an expression that properly refers to that
3573/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003574ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003575Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
3576 QualType ParamType,
3577 SourceLocation Loc) {
3578 assert(Arg.getKind() == TemplateArgument::Declaration &&
3579 "Only declaration template arguments permitted here");
3580 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
3581
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003582 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00003583 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
3584 // If the value is a class member, we might have a pointer-to-member.
3585 // Determine whether the non-type template template parameter is of
3586 // pointer-to-member type. If so, we need to build an appropriate
3587 // expression for a pointer-to-member, since a "normal" DeclRefExpr
3588 // would refer to the member itself.
3589 if (ParamType->isMemberPointerType()) {
3590 QualType ClassType
3591 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
3592 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00003593 = NestedNameSpecifier::Create(Context, 0, false,
3594 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00003595 CXXScopeSpec SS;
3596 SS.setScopeRep(Qualifier);
John McCalldfa1edb2010-11-23 20:48:44 +00003597
3598 // The actual value-ness of this is unimportant, but for
3599 // internal consistency's sake, references to instance methods
3600 // are r-values.
3601 ExprValueKind VK = VK_LValue;
3602 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
3603 VK = VK_RValue;
3604
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003605 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00003606 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00003607 VK,
John McCallf89e55a2010-11-18 06:31:45 +00003608 Loc,
3609 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00003610 if (RefExpr.isInvalid())
3611 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003612
John McCall2de56d12010-08-25 11:45:40 +00003613 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003614
Douglas Gregorc0c83002010-04-30 21:46:38 +00003615 // We might need to perform a trailing qualification conversion, since
3616 // the element type on the parameter could be more qualified than the
3617 // element type in the expression we constructed.
3618 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003619 ParamType.getUnqualifiedType(), false)) {
Douglas Gregorc0c83002010-04-30 21:46:38 +00003620 Expr *RefE = RefExpr.takeAs<Expr>();
John McCall2de56d12010-08-25 11:45:40 +00003621 ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CK_NoOp);
Douglas Gregorc0c83002010-04-30 21:46:38 +00003622 RefExpr = Owned(RefE);
3623 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003624
Douglas Gregor02024a92010-03-28 02:42:43 +00003625 assert(!RefExpr.isInvalid() &&
3626 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00003627 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00003628 return move(RefExpr);
3629 }
3630 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003631
Douglas Gregor02024a92010-03-28 02:42:43 +00003632 QualType T = VD->getType().getNonReferenceType();
3633 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003634 // When the non-type template parameter is a pointer, take the
3635 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00003636 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003637 if (RefExpr.isInvalid())
3638 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003639
3640 if (T->isFunctionType() || T->isArrayType()) {
3641 // Decay functions and arrays.
3642 Expr *RefE = (Expr *)RefExpr.get();
3643 DefaultFunctionArrayConversion(RefE);
3644 if (RefE != RefExpr.get()) {
3645 RefExpr.release();
3646 RefExpr = Owned(RefE);
3647 }
3648
3649 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00003650 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003651
Douglas Gregorb7a09262010-04-01 18:32:35 +00003652 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00003653 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00003654 }
3655
John McCallf89e55a2010-11-18 06:31:45 +00003656 ExprValueKind VK = VK_RValue;
3657
Douglas Gregor02024a92010-03-28 02:42:43 +00003658 // If the non-type template parameter has reference type, qualify the
3659 // resulting declaration reference with the extra qualifiers on the
3660 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00003661 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
3662 VK = VK_LValue;
3663 T = Context.getQualifiedType(T,
3664 TargetRef->getPointeeType().getQualifiers());
3665 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003666
John McCallf89e55a2010-11-18 06:31:45 +00003667 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003668}
3669
3670/// \brief Construct a new expression that refers to the given
3671/// integral template argument with the given source-location
3672/// information.
3673///
3674/// This routine takes care of the mapping from an integral template
3675/// argument (which may have any integral type) to the appropriate
3676/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003677ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003678Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
3679 SourceLocation Loc) {
3680 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00003681 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00003682 QualType T = Arg.getIntegralType();
3683 if (T->isCharType() || T->isWideCharType())
3684 return Owned(new (Context) CharacterLiteral(
3685 Arg.getAsIntegral()->getZExtValue(),
3686 T->isWideCharType(),
3687 T,
3688 Loc));
3689 if (T->isBooleanType())
3690 return Owned(new (Context) CXXBoolLiteralExpr(
3691 Arg.getAsIntegral()->getBoolValue(),
3692 T,
3693 Loc));
3694
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003695 QualType BT;
3696 if (const EnumType *ET = T->getAs<EnumType>())
3697 BT = ET->getDecl()->getPromotionType();
3698 else
3699 BT = T;
3700
3701 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
3702 ImpCastExprToType(E, T, CK_IntegralCast);
3703
3704 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00003705}
3706
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003707/// \brief Match two template parameters within template parameter lists.
3708static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
3709 bool Complain,
3710 Sema::TemplateParameterListEqualKind Kind,
3711 SourceLocation TemplateArgLoc) {
3712 // Check the actual kind (type, non-type, template).
3713 if (Old->getKind() != New->getKind()) {
3714 if (Complain) {
3715 unsigned NextDiag = diag::err_template_param_different_kind;
3716 if (TemplateArgLoc.isValid()) {
3717 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3718 NextDiag = diag::note_template_param_different_kind;
3719 }
3720 S.Diag(New->getLocation(), NextDiag)
3721 << (Kind != Sema::TPL_TemplateMatch);
3722 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
3723 << (Kind != Sema::TPL_TemplateMatch);
3724 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003725
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003726 return false;
3727 }
3728
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003729 // Check that both are parameter packs are neither are parameter packs.
3730 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00003731 // template template parameter, the template template parameter can have
3732 // a parameter pack where the template template argument does not.
3733 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
3734 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3735 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003736 if (Complain) {
3737 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
3738 if (TemplateArgLoc.isValid()) {
3739 S.Diag(TemplateArgLoc,
3740 diag::err_template_arg_template_params_mismatch);
3741 NextDiag = diag::note_template_parameter_pack_non_pack;
3742 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003743
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003744 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
3745 : isa<NonTypeTemplateParmDecl>(New)? 1
3746 : 2;
3747 S.Diag(New->getLocation(), NextDiag)
3748 << ParamKind << New->isParameterPack();
3749 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
3750 << ParamKind << Old->isParameterPack();
3751 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003752
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003753 return false;
3754 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003755
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003756 // For non-type template parameters, check the type of the parameter.
3757 if (NonTypeTemplateParmDecl *OldNTTP
3758 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
3759 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003760
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003761 // If we are matching a template template argument to a template
3762 // template parameter and one of the non-type template parameter types
3763 // is dependent, then we must wait until template instantiation time
3764 // to actually compare the arguments.
3765 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3766 (OldNTTP->getType()->isDependentType() ||
3767 NewNTTP->getType()->isDependentType()))
3768 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003769
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003770 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
3771 if (Complain) {
3772 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
3773 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003774 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003775 diag::err_template_arg_template_params_mismatch);
3776 NextDiag = diag::note_template_nontype_parm_different_type;
3777 }
3778 S.Diag(NewNTTP->getLocation(), NextDiag)
3779 << NewNTTP->getType()
3780 << (Kind != Sema::TPL_TemplateMatch);
3781 S.Diag(OldNTTP->getLocation(),
3782 diag::note_template_nontype_parm_prev_declaration)
3783 << OldNTTP->getType();
3784 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003785
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003786 return false;
3787 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003788
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003789 return true;
3790 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003791
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003792 // For template template parameters, check the template parameter types.
3793 // The template parameter lists of template template
3794 // parameters must agree.
3795 if (TemplateTemplateParmDecl *OldTTP
3796 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003797 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003798 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
3799 OldTTP->getTemplateParameters(),
3800 Complain,
3801 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003802 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003803 : Kind),
3804 TemplateArgLoc);
3805 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003806
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003807 return true;
3808}
Douglas Gregor02024a92010-03-28 02:42:43 +00003809
Douglas Gregora0347822011-01-13 00:08:50 +00003810/// \brief Diagnose a known arity mismatch when comparing template argument
3811/// lists.
3812static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003813void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00003814 TemplateParameterList *New,
3815 TemplateParameterList *Old,
3816 Sema::TemplateParameterListEqualKind Kind,
3817 SourceLocation TemplateArgLoc) {
3818 unsigned NextDiag = diag::err_template_param_list_different_arity;
3819 if (TemplateArgLoc.isValid()) {
3820 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3821 NextDiag = diag::note_template_param_list_different_arity;
3822 }
3823 S.Diag(New->getTemplateLoc(), NextDiag)
3824 << (New->size() > Old->size())
3825 << (Kind != Sema::TPL_TemplateMatch)
3826 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
3827 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
3828 << (Kind != Sema::TPL_TemplateMatch)
3829 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
3830}
3831
Douglas Gregorddc29e12009-02-06 22:42:48 +00003832/// \brief Determine whether the given template parameter lists are
3833/// equivalent.
3834///
Mike Stump1eb44332009-09-09 15:08:12 +00003835/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00003836/// source code as part of a new template declaration.
3837///
3838/// \param Old The old template parameter list, typically found via
3839/// name lookup of the template declared with this template parameter
3840/// list.
3841///
3842/// \param Complain If true, this routine will produce a diagnostic if
3843/// the template parameter lists are not equivalent.
3844///
Douglas Gregorfb898e12009-11-12 16:20:59 +00003845/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00003846///
3847/// \param TemplateArgLoc If this source location is valid, then we
3848/// are actually checking the template parameter list of a template
3849/// argument (New) against the template parameter list of its
3850/// corresponding template template parameter (Old). We produce
3851/// slightly different diagnostics in this scenario.
3852///
Douglas Gregorddc29e12009-02-06 22:42:48 +00003853/// \returns True if the template parameter lists are equal, false
3854/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003855bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00003856Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
3857 TemplateParameterList *Old,
3858 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003859 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00003860 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00003861 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
3862 if (Complain)
3863 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3864 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00003865
3866 return false;
3867 }
3868
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003869 // C++0x [temp.arg.template]p3:
3870 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003871 // when each of the template parameters in the template-parameter-list of
3872 // the template-argument's corresponding class template or template alias
3873 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00003874 // template-parameter-list of P. [...]
3875 TemplateParameterList::iterator NewParm = New->begin();
3876 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00003877 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00003878 OldParmEnd = Old->end();
3879 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00003880 if (Kind != TPL_TemplateTemplateArgumentMatch ||
3881 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00003882 if (NewParm == NewParmEnd) {
3883 if (Complain)
3884 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3885 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003886
Douglas Gregora0347822011-01-13 00:08:50 +00003887 return false;
3888 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003889
Douglas Gregora0347822011-01-13 00:08:50 +00003890 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
3891 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003892 return false;
3893
Douglas Gregora0347822011-01-13 00:08:50 +00003894 ++NewParm;
3895 continue;
3896 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003897
Douglas Gregora0347822011-01-13 00:08:50 +00003898 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003899 // [...] When P's template- parameter-list contains a template parameter
3900 // pack (14.5.3), the template parameter pack will match zero or more
3901 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00003902 // template-parameter-list of A with the same type and form as the
3903 // template parameter pack in P (ignoring whether those template
3904 // parameters are template parameter packs).
3905 for (; NewParm != NewParmEnd; ++NewParm) {
3906 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
3907 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003908 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00003909 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00003910 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003911
Douglas Gregora0347822011-01-13 00:08:50 +00003912 // Make sure we exhausted all of the arguments.
3913 if (NewParm != NewParmEnd) {
3914 if (Complain)
3915 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3916 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003917
Douglas Gregora0347822011-01-13 00:08:50 +00003918 return false;
3919 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003920
Douglas Gregorddc29e12009-02-06 22:42:48 +00003921 return true;
3922}
3923
3924/// \brief Check whether a template can be declared within this scope.
3925///
3926/// If the template declaration is valid in this scope, returns
3927/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00003928bool
Douglas Gregor05396e22009-08-25 17:23:04 +00003929Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00003930 // Find the nearest enclosing declaration scope.
3931 while ((S->getFlags() & Scope::DeclScope) == 0 ||
3932 (S->getFlags() & Scope::TemplateParamScope) != 0)
3933 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00003934
Douglas Gregorddc29e12009-02-06 22:42:48 +00003935 // C++ [temp]p2:
3936 // A template-declaration can appear only as a namespace scope or
3937 // class scope declaration.
3938 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00003939 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
3940 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00003941 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00003942 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003943
Eli Friedman1503f772009-07-31 01:43:05 +00003944 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00003945 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00003946
3947 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
3948 return false;
3949
Mike Stump1eb44332009-09-09 15:08:12 +00003950 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00003951 diag::err_template_outside_namespace_or_class_scope)
3952 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00003953}
Douglas Gregorcc636682009-02-17 23:15:12 +00003954
Douglas Gregord5cb8762009-10-07 00:13:32 +00003955/// \brief Determine what kind of template specialization the given declaration
3956/// is.
3957static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
3958 if (!D)
3959 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003960
Douglas Gregorf6b11852009-10-08 15:14:33 +00003961 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
3962 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00003963 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
3964 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003965 if (VarDecl *Var = dyn_cast<VarDecl>(D))
3966 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003967
Douglas Gregord5cb8762009-10-07 00:13:32 +00003968 return TSK_Undeclared;
3969}
3970
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003971/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00003972/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00003973///
Douglas Gregor9302da62009-10-14 23:50:59 +00003974/// This routine determines whether a template specialization can be declared
3975/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00003976///
3977/// \param S the semantic analysis object for which this check is being
3978/// performed.
3979///
3980/// \param Specialized the entity being specialized or instantiated, which
3981/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003982/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00003983/// member class).
3984///
3985/// \param PrevDecl the previous declaration of this entity, if any.
3986///
3987/// \param Loc the location of the explicit specialization or instantiation of
3988/// this entity.
3989///
3990/// \param IsPartialSpecialization whether this is a partial specialization of
3991/// a class template.
3992///
Douglas Gregord5cb8762009-10-07 00:13:32 +00003993/// \returns true if there was an error that we cannot recover from, false
3994/// otherwise.
3995static bool CheckTemplateSpecializationScope(Sema &S,
3996 NamedDecl *Specialized,
3997 NamedDecl *PrevDecl,
3998 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00003999 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004000 // Keep these "kind" numbers in sync with the %select statements in the
4001 // various diagnostics emitted by this routine.
4002 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004003 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004004 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004005 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004006 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004007 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004008 EntityKind = 3;
4009 else if (isa<VarDecl>(Specialized))
4010 EntityKind = 4;
4011 else if (isa<RecordDecl>(Specialized))
4012 EntityKind = 5;
4013 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004014 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4015 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004016 return true;
4017 }
4018
Douglas Gregor88b70942009-02-25 22:02:03 +00004019 // C++ [temp.expl.spec]p2:
4020 // An explicit specialization shall be declared in the namespace
4021 // of which the template is a member, or, for member templates, in
4022 // the namespace of which the enclosing class or enclosing class
4023 // template is a member. An explicit specialization of a member
4024 // function, member class or static data member of a class
4025 // template shall be declared in the namespace of which the class
4026 // template is a member. Such a declaration may also be a
4027 // definition. If the declaration is not a definition, the
4028 // specialization may be defined later in the name- space in which
4029 // the explicit specialization was declared, or in a namespace
4030 // that encloses the one in which the explicit specialization was
4031 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004032 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004033 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004034 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004035 return true;
4036 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004037
Douglas Gregor0a407472009-10-07 17:30:37 +00004038 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
4039 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004040 << Specialized;
Douglas Gregor0a407472009-10-07 17:30:37 +00004041 return true;
4042 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004043
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004044 // C++ [temp.class.spec]p6:
4045 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004046 // in any namespace scope in which its definition may be defined (14.5.1
4047 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004048 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004049 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004050 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004051 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004052 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004053 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4054 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004055 // C++ [temp.exp.spec]p2:
4056 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004057 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004058 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004059 // An explicit specialization of a member function, member class or
4060 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004061 // namespace of which the class template is a member.
4062 //
4063 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004064 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004065 // the specialized template.
4066 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4067 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004068 bool IsCPlusPlus0xExtension
4069 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004070 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004071 S.Diag(Loc, IsCPlusPlus0xExtension
4072 ? diag::ext_template_spec_decl_out_of_scope_global
4073 : diag::err_template_spec_decl_out_of_scope_global)
4074 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004075 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004076 S.Diag(Loc, IsCPlusPlus0xExtension
4077 ? diag::ext_template_spec_decl_out_of_scope
4078 : diag::err_template_spec_decl_out_of_scope)
4079 << EntityKind << Specialized
4080 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004081
Douglas Gregor9302da62009-10-14 23:50:59 +00004082 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4083 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004084 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004085 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004086
4087 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004088 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004089 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004090 // specializations of function templates, static data members, and member
4091 // functions, so we skip the check here for those kinds of entities.
4092 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004093 // Should we refactor that check, so that it occurs later?
4094 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004095 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4096 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004097 if (isa<TranslationUnitDecl>(SpecializedContext))
4098 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4099 << EntityKind << Specialized;
4100 else if (isa<NamespaceDecl>(SpecializedContext))
4101 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4102 << EntityKind << Specialized
4103 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004104
Douglas Gregor9302da62009-10-14 23:50:59 +00004105 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004106 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004107
Douglas Gregord5cb8762009-10-07 00:13:32 +00004108 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004109
Douglas Gregor88b70942009-02-25 22:02:03 +00004110 return false;
4111}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004112
Douglas Gregorbacb9492011-01-03 21:13:47 +00004113/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4114/// that checks non-type template partial specialization arguments.
4115static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4116 NonTypeTemplateParmDecl *Param,
4117 const TemplateArgument *Args,
4118 unsigned NumArgs) {
4119 for (unsigned I = 0; I != NumArgs; ++I) {
4120 if (Args[I].getKind() == TemplateArgument::Pack) {
4121 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004122 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004123 Args[I].pack_size()))
4124 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004125
Douglas Gregore94866f2009-06-12 21:21:02 +00004126 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004127 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004128
Douglas Gregorbacb9492011-01-03 21:13:47 +00004129 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004130 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004131 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004132 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004133
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004134 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004135 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4136 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004137
4138 // Strip off any implicit casts we added as part of type checking.
4139 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4140 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004141
Douglas Gregore94866f2009-06-12 21:21:02 +00004142 // C++ [temp.class.spec]p8:
4143 // A non-type argument is non-specialized if it is the name of a
4144 // non-type parameter. All other non-type arguments are
4145 // specialized.
4146 //
4147 // Below, we check the two conditions that only apply to
4148 // specialized non-type arguments, so skip any non-specialized
4149 // arguments.
4150 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004151 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004152 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004153
Douglas Gregore94866f2009-06-12 21:21:02 +00004154 // C++ [temp.class.spec]p9:
4155 // Within the argument list of a class template partial
4156 // specialization, the following restrictions apply:
4157 // -- A partially specialized non-type argument expression
4158 // shall not involve a template parameter of the partial
4159 // specialization except when the argument expression is a
4160 // simple identifier.
4161 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004162 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004163 diag::err_dependent_non_type_arg_in_partial_spec)
4164 << ArgExpr->getSourceRange();
4165 return true;
4166 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004167
Douglas Gregore94866f2009-06-12 21:21:02 +00004168 // -- The type of a template parameter corresponding to a
4169 // specialized non-type argument shall not be dependent on a
4170 // parameter of the specialization.
4171 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004172 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004173 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4174 << Param->getType()
4175 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004176 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004177 return true;
4178 }
4179 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004180
Douglas Gregorbacb9492011-01-03 21:13:47 +00004181 return false;
4182}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004183
Douglas Gregorbacb9492011-01-03 21:13:47 +00004184/// \brief Check the non-type template arguments of a class template
4185/// partial specialization according to C++ [temp.class.spec]p9.
4186///
4187/// \param TemplateParams the template parameters of the primary class
4188/// template.
4189///
4190/// \param TemplateArg the template arguments of the class template
4191/// partial specialization.
4192///
4193/// \returns true if there was an error, false otherwise.
4194static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4195 TemplateParameterList *TemplateParams,
4196 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4197 const TemplateArgument *ArgList = TemplateArgs.data();
4198
4199 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4200 NonTypeTemplateParmDecl *Param
4201 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4202 if (!Param)
4203 continue;
4204
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004205 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004206 &ArgList[I], 1))
4207 return true;
4208 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004209
4210 return false;
4211}
4212
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004213/// \brief Retrieve the previous declaration of the given declaration.
4214static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4215 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4216 return VD->getPreviousDeclaration();
4217 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4218 return FD->getPreviousDeclaration();
4219 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4220 return TD->getPreviousDeclaration();
4221 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND))
4222 return TD->getPreviousDeclaration();
4223 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4224 return FTD->getPreviousDeclaration();
4225 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4226 return CTD->getPreviousDeclaration();
4227 return 0;
4228}
4229
John McCalld226f652010-08-21 09:40:31 +00004230DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004231Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4232 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004233 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004234 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004235 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004236 SourceLocation TemplateNameLoc,
4237 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004238 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004239 SourceLocation RAngleLoc,
4240 AttributeList *Attr,
4241 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004242 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004243
Douglas Gregorcc636682009-02-17 23:15:12 +00004244 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004245 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004246 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004247 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4248
4249 if (!ClassTemplate) {
4250 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004251 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004252 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4253 return true;
4254 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004255
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004256 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004257 bool isPartialSpecialization = false;
4258
Douglas Gregor88b70942009-02-25 22:02:03 +00004259 // Check the validity of the template headers that introduce this
4260 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004261 // FIXME: We probably shouldn't complain about these headers for
4262 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004263 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004264 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00004265 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
4266 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004267 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004268 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004269 isExplicitSpecialization,
4270 Invalid);
4271 if (Invalid)
4272 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004273
Abramo Bagnara9b934882010-06-12 08:15:14 +00004274 unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size();
4275 if (TemplateParams)
4276 --NumMatchedTemplateParamLists;
4277
Douglas Gregor05396e22009-08-25 17:23:04 +00004278 if (TemplateParams && TemplateParams->size() > 0) {
4279 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004280
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004281 if (TUK == TUK_Friend) {
4282 Diag(KWLoc, diag::err_partial_specialization_friend)
4283 << SourceRange(LAngleLoc, RAngleLoc);
4284 return true;
4285 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004286
Douglas Gregor05396e22009-08-25 17:23:04 +00004287 // C++ [temp.class.spec]p10:
4288 // The template parameter list of a specialization shall not
4289 // contain default template argument values.
4290 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4291 Decl *Param = TemplateParams->getParam(I);
4292 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4293 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004294 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004295 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004296 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004297 }
4298 } else if (NonTypeTemplateParmDecl *NTTP
4299 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4300 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004301 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004302 diag::err_default_arg_in_partial_spec)
4303 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004304 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004305 }
4306 } else {
4307 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004308 if (TTP->hasDefaultArgument()) {
4309 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004310 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004311 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004312 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004313 }
4314 }
4315 }
Douglas Gregora735b202009-10-13 14:39:41 +00004316 } else if (TemplateParams) {
4317 if (TUK == TUK_Friend)
4318 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004319 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004320 SourceRange(TemplateParams->getTemplateLoc(),
4321 TemplateParams->getRAngleLoc()))
4322 << SourceRange(LAngleLoc, RAngleLoc);
4323 else
4324 isExplicitSpecialization = true;
4325 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004326 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004327 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004328 isExplicitSpecialization = true;
4329 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004330
Douglas Gregorcc636682009-02-17 23:15:12 +00004331 // Check that the specialization uses the same tag kind as the
4332 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004333 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4334 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004335 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004336 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004337 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004338 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004339 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004340 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004341 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004342 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004343 diag::note_previous_use);
4344 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4345 }
4346
Douglas Gregor40808ce2009-03-09 23:48:35 +00004347 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004348 TemplateArgumentListInfo TemplateArgs;
4349 TemplateArgs.setLAngleLoc(LAngleLoc);
4350 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004351 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004352
Douglas Gregor925910d2011-01-03 20:35:03 +00004353 // Check for unexpanded parameter packs in any of the template arguments.
4354 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004355 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004356 UPPC_PartialSpecialization))
4357 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004358
Douglas Gregorcc636682009-02-17 23:15:12 +00004359 // Check that the template argument list is well-formed for this
4360 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004361 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004362 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4363 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004364 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004365
Douglas Gregor910f8002010-11-07 23:05:16 +00004366 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004367 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004368
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004369 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004370 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004371 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004372 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004373 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004374 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004375 return true;
4376
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004377 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004378 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004379 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004380 TemplateArgs.size())) {
4381 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4382 << ClassTemplate->getDeclName();
4383 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004384 }
4385 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004386
Douglas Gregorcc636682009-02-17 23:15:12 +00004387 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004388 ClassTemplateSpecializationDecl *PrevDecl = 0;
4389
4390 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004391 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004392 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004393 = ClassTemplate->findPartialSpecialization(Converted.data(),
4394 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004395 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004396 else
4397 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004398 = ClassTemplate->findSpecialization(Converted.data(),
4399 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004400
4401 ClassTemplateSpecializationDecl *Specialization = 0;
4402
Douglas Gregor88b70942009-02-25 22:02:03 +00004403 // Check whether we can declare a class template specialization in
4404 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004405 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004406 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4407 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004408 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004409 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004410
Douglas Gregorb88e8882009-07-30 17:40:51 +00004411 // The canonical type
4412 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004413 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004414 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004415 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004416 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004417 // arguments was referenced but not declared, or we're only
4418 // referencing this specialization as a friend, reuse that
Douglas Gregorcc636682009-02-17 23:15:12 +00004419 // declaration node as our own, updating its source location to
4420 // reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004421 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004422 Specialization->setLocation(TemplateNameLoc);
Douglas Gregorcc636682009-02-17 23:15:12 +00004423 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004424 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004425 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004426 // Build the canonical type that describes the converted template
4427 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004428 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4429 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004430 Converted.data(),
4431 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004432
4433 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004434 ClassTemplate->getInjectedClassNameSpecialization())) {
4435 // C++ [temp.class.spec]p9b3:
4436 //
4437 // -- The argument list of the specialization shall not be identical
4438 // to the implicit argument list of the primary template.
4439 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4440 << (TUK == TUK_Definition)
4441 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4442 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4443 ClassTemplate->getIdentifier(),
4444 TemplateNameLoc,
4445 Attr,
4446 TemplateParams,
4447 AS_none);
4448 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004449
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004450 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004451 ClassTemplatePartialSpecializationDecl *PrevPartial
4452 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004453 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004454 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004455 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004456 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004457 ClassTemplate->getDeclContext(),
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004458 TemplateNameLoc,
4459 TemplateParams,
4460 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004461 Converted.data(),
4462 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004463 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004464 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004465 PrevPartial,
4466 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004467 SetNestedNameSpecifier(Partial, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004468 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004469 Partial->setTemplateParameterListsInfo(Context,
4470 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004471 (TemplateParameterList**) TemplateParameterLists.release());
4472 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004473
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004474 if (!PrevPartial)
4475 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004476 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004477
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004478 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004479 // template specialization, make a note of that.
4480 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4481 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004482
Douglas Gregor031a5882009-06-13 00:26:55 +00004483 // Check that all of the template parameters of the class template
4484 // partial specialization are deducible from the template
4485 // arguments. If not, this class template partial specialization
4486 // will never be used.
4487 llvm::SmallVector<bool, 8> DeducibleParams;
4488 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004489 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004490 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004491 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004492 unsigned NumNonDeducible = 0;
4493 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4494 if (!DeducibleParams[I])
4495 ++NumNonDeducible;
4496
4497 if (NumNonDeducible) {
4498 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4499 << (NumNonDeducible > 1)
4500 << SourceRange(TemplateNameLoc, RAngleLoc);
4501 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4502 if (!DeducibleParams[I]) {
4503 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4504 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004505 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004506 diag::note_partial_spec_unused_parameter)
4507 << Param->getDeclName();
4508 else
Mike Stump1eb44332009-09-09 15:08:12 +00004509 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004510 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004511 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004512 }
4513 }
4514 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004515 } else {
4516 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004517 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004518 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004519 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004520 ClassTemplate->getDeclContext(),
4521 TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004522 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004523 Converted.data(),
4524 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004525 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004526 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004527 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004528 Specialization->setTemplateParameterListsInfo(Context,
4529 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004530 (TemplateParameterList**) TemplateParameterLists.release());
4531 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004532
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004533 if (!PrevDecl)
4534 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004535
4536 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004537 }
4538
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004539 // C++ [temp.expl.spec]p6:
4540 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004541 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004542 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004543 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004544 // use occurs; no diagnostic is required.
4545 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004546 bool Okay = false;
4547 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4548 // Is there any previous explicit specialization declaration?
4549 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4550 Okay = true;
4551 break;
4552 }
4553 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004554
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004555 if (!Okay) {
4556 SourceRange Range(TemplateNameLoc, RAngleLoc);
4557 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
4558 << Context.getTypeDeclType(Specialization) << Range;
4559
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004560 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004561 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004562 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004563 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004564 return true;
4565 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004566 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004567
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004568 // If this is not a friend, note that this is an explicit specialization.
4569 if (TUK != TUK_Friend)
4570 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004571
4572 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004573 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00004574 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004575 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00004576 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004577 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00004578 Diag(Def->getLocation(), diag::note_previous_definition);
4579 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00004580 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004581 }
4582 }
4583
John McCall7f1b9872010-12-18 03:30:47 +00004584 if (Attr)
4585 ProcessDeclAttributeList(S, Specialization, Attr);
4586
Douglas Gregorfc705b82009-02-26 22:19:44 +00004587 // Build the fully-sugared type for this class template
4588 // specialization as the user wrote in the specialization
4589 // itself. This means that we'll pretty-print the type retrieved
4590 // from the specialization's declaration the way that the user
4591 // actually wrote the specialization, rather than formatting the
4592 // name based on the "canonical" representation used to store the
4593 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00004594 TypeSourceInfo *WrittenTy
4595 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
4596 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004597 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004598 Specialization->setTypeAsWritten(WrittenTy);
Douglas Gregor7e9b57b2010-07-06 18:33:12 +00004599 if (TemplateParams)
4600 Specialization->setTemplateKeywordLoc(TemplateParams->getTemplateLoc());
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004601 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00004602 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00004603
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004604 // C++ [temp.expl.spec]p9:
4605 // A template explicit specialization is in the scope of the
4606 // namespace in which the template was defined.
4607 //
4608 // We actually implement this paragraph where we set the semantic
4609 // context (in the creation of the ClassTemplateSpecializationDecl),
4610 // but we also maintain the lexical context where the actual
4611 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00004612 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00004613
Douglas Gregorcc636682009-02-17 23:15:12 +00004614 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004615 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00004616 Specialization->startDefinition();
4617
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004618 if (TUK == TUK_Friend) {
4619 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
4620 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00004621 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004622 /*FIXME:*/KWLoc);
4623 Friend->setAccess(AS_public);
4624 CurContext->addDecl(Friend);
4625 } else {
4626 // Add the specialization into its lexical context, so that it can
4627 // be seen when iterating through the list of declarations in that
4628 // context. However, specializations are not found by name lookup.
4629 CurContext->addDecl(Specialization);
4630 }
John McCalld226f652010-08-21 09:40:31 +00004631 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00004632}
Douglas Gregord57959a2009-03-27 23:10:48 +00004633
John McCalld226f652010-08-21 09:40:31 +00004634Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00004635 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004636 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00004637 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
4638}
4639
John McCalld226f652010-08-21 09:40:31 +00004640Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00004641 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004642 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00004643 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004644 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00004645
Douglas Gregor52591bf2009-06-24 00:54:41 +00004646 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00004647 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00004648 }
Mike Stump1eb44332009-09-09 15:08:12 +00004649
Douglas Gregor52591bf2009-06-24 00:54:41 +00004650 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004651
John McCalld226f652010-08-21 09:40:31 +00004652 Decl *DP = HandleDeclarator(ParentScope, D,
4653 move(TemplateParameterLists),
4654 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00004655 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00004656 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00004657 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00004658 FunctionTemplate->getTemplatedDecl());
4659 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
4660 return ActOnStartOfFunctionDef(FnBodyScope, Function);
4661 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00004662}
4663
John McCall75042392010-02-11 01:33:53 +00004664/// \brief Strips various properties off an implicit instantiation
4665/// that has just been explicitly specialized.
4666static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00004667 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00004668
4669 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4670 FD->setInlineSpecified(false);
4671 }
4672}
4673
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004674/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00004675/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004676/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00004677/// new specialization/instantiation will have any effect.
4678///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004679/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00004680/// instantiation.
4681///
4682/// \param NewTSK the kind of the new explicit specialization or instantiation.
4683///
4684/// \param PrevDecl the previous declaration of the entity.
4685///
4686/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
4687///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004688/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00004689/// declaration was instantiated (either implicitly or explicitly).
4690///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004691/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00004692/// specialization or instantiation has no effect and should be ignored.
4693///
4694/// \returns true if there was an error that should prevent the introduction of
4695/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00004696bool
4697Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
4698 TemplateSpecializationKind NewTSK,
4699 NamedDecl *PrevDecl,
4700 TemplateSpecializationKind PrevTSK,
4701 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004702 bool &HasNoEffect) {
4703 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004704
Douglas Gregor454885e2009-10-15 15:54:05 +00004705 switch (NewTSK) {
4706 case TSK_Undeclared:
4707 case TSK_ImplicitInstantiation:
4708 assert(false && "Don't check implicit instantiations here");
4709 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004710
Douglas Gregor454885e2009-10-15 15:54:05 +00004711 case TSK_ExplicitSpecialization:
4712 switch (PrevTSK) {
4713 case TSK_Undeclared:
4714 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004715 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00004716 // explicitly specialized or has merely been mentioned without any
4717 // instantiation.
4718 return false;
4719
4720 case TSK_ImplicitInstantiation:
4721 if (PrevPointOfInstantiation.isInvalid()) {
4722 // The declaration itself has not actually been instantiated, so it is
4723 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00004724 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00004725 return false;
4726 }
4727 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004728
Douglas Gregor454885e2009-10-15 15:54:05 +00004729 case TSK_ExplicitInstantiationDeclaration:
4730 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004731 assert((PrevTSK == TSK_ImplicitInstantiation ||
4732 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00004733 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004734
Douglas Gregor454885e2009-10-15 15:54:05 +00004735 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004736 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00004737 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004738 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00004739 // implicit instantiation to take place, in every translation unit in
4740 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004741 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4742 // Is there any previous explicit specialization declaration?
4743 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
4744 return false;
4745 }
4746
Douglas Gregor0d035142009-10-27 18:42:08 +00004747 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00004748 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004749 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00004750 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004751
Douglas Gregor454885e2009-10-15 15:54:05 +00004752 return true;
4753 }
4754 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004755
Douglas Gregor454885e2009-10-15 15:54:05 +00004756 case TSK_ExplicitInstantiationDeclaration:
4757 switch (PrevTSK) {
4758 case TSK_ExplicitInstantiationDeclaration:
4759 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004760 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004761 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004762
Douglas Gregor454885e2009-10-15 15:54:05 +00004763 case TSK_Undeclared:
4764 case TSK_ImplicitInstantiation:
4765 // We're explicitly instantiating something that may have already been
4766 // implicitly instantiated; that's fine.
4767 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004768
Douglas Gregor454885e2009-10-15 15:54:05 +00004769 case TSK_ExplicitSpecialization:
4770 // C++0x [temp.explicit]p4:
4771 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004772 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00004773 // specialization for that template, the explicit instantiation has no
4774 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004775 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004776 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004777
Douglas Gregor454885e2009-10-15 15:54:05 +00004778 case TSK_ExplicitInstantiationDefinition:
4779 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004780 // If an entity is the subject of both an explicit instantiation
4781 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00004782 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004783 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00004784 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004785 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004786 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00004787 assert(PrevPointOfInstantiation.isValid() &&
4788 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004789 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004790 return false;
4791 }
4792 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004793
Douglas Gregor454885e2009-10-15 15:54:05 +00004794 case TSK_ExplicitInstantiationDefinition:
4795 switch (PrevTSK) {
4796 case TSK_Undeclared:
4797 case TSK_ImplicitInstantiation:
4798 // We're explicitly instantiating something that may have already been
4799 // implicitly instantiated; that's fine.
4800 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004801
Douglas Gregor454885e2009-10-15 15:54:05 +00004802 case TSK_ExplicitSpecialization:
4803 // C++ DR 259, C++0x [temp.explicit]p4:
4804 // For a given set of template parameters, if an explicit
4805 // instantiation of a template appears after a declaration of
4806 // an explicit specialization for that template, the explicit
4807 // instantiation has no effect.
4808 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004809 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00004810 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00004811 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00004812 if (!getLangOptions().CPlusPlus0x) {
4813 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00004814 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004815 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00004816 diag::note_previous_template_specialization);
4817 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004818 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004819 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004820
Douglas Gregor454885e2009-10-15 15:54:05 +00004821 case TSK_ExplicitInstantiationDeclaration:
4822 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004823 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00004824 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004825
Douglas Gregor454885e2009-10-15 15:54:05 +00004826 case TSK_ExplicitInstantiationDefinition:
4827 // C++0x [temp.spec]p5:
4828 // For a given template and a given set of template-arguments,
4829 // - an explicit instantiation definition shall appear at most once
4830 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00004831 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00004832 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004833 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004834 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004835 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004836 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00004837 }
4838 break;
4839 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004840
Douglas Gregor454885e2009-10-15 15:54:05 +00004841 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004842
Douglas Gregor454885e2009-10-15 15:54:05 +00004843 return false;
4844}
4845
John McCallaf2094e2010-04-08 09:05:18 +00004846/// \brief Perform semantic analysis for the given dependent function
4847/// template specialization. The only possible way to get a dependent
4848/// function template specialization is with a friend declaration,
4849/// like so:
4850///
4851/// template <class T> void foo(T);
4852/// template <class T> class A {
4853/// friend void foo<>(T);
4854/// };
4855///
4856/// There really isn't any useful analysis we can do here, so we
4857/// just store the information.
4858bool
4859Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
4860 const TemplateArgumentListInfo &ExplicitTemplateArgs,
4861 LookupResult &Previous) {
4862 // Remove anything from Previous that isn't a function template in
4863 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004864 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00004865 LookupResult::Filter F = Previous.makeFilter();
4866 while (F.hasNext()) {
4867 NamedDecl *D = F.next()->getUnderlyingDecl();
4868 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00004869 !FDLookupContext->InEnclosingNamespaceSetOf(
4870 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00004871 F.erase();
4872 }
4873 F.done();
4874
4875 // Should this be diagnosed here?
4876 if (Previous.empty()) return true;
4877
4878 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
4879 ExplicitTemplateArgs);
4880 return false;
4881}
4882
Abramo Bagnarae03db982010-05-20 15:32:11 +00004883/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004884/// specialization.
4885///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004886/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004887/// explicit function template specialization. On successful completion,
4888/// the function declaration \p FD will become a function template
4889/// specialization.
4890///
4891/// \param FD the function declaration, which will be updated to become a
4892/// function template specialization.
4893///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004894/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
4895/// if any. Note that this may be valid info even when 0 arguments are
4896/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
4897/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004898///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004899/// \param PrevDecl the set of declarations that may be specialized by
4900/// this function specialization.
4901bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004902Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
John McCalld5532b62009-11-23 01:53:49 +00004903 const TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00004904 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004905 // The set of function template specializations that could match this
4906 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00004907 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004908
Sebastian Redl7a126a42010-08-31 00:36:30 +00004909 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00004910 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
4911 I != E; ++I) {
4912 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
4913 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004914 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004915 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004916 if (!FDLookupContext->InEnclosingNamespaceSetOf(
4917 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004918 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004919
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004920 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004921 // A trailing template-argument can be left unspecified in the
4922 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004923 // provided it can be deduced from the function argument type.
4924 // Perform template argument deduction to determine whether we may be
4925 // specializing this template.
4926 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00004927 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004928 FunctionDecl *Specialization = 0;
4929 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00004930 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004931 FD->getType(),
4932 Specialization,
4933 Info)) {
4934 // FIXME: Template argument deduction failed; record why it failed, so
4935 // that we can provide nifty diagnostics.
4936 (void)TDK;
4937 continue;
4938 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004939
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004940 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00004941 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004942 }
4943 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004944
Douglas Gregorc5df30f2009-09-26 03:41:46 +00004945 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00004946 UnresolvedSetIterator Result
4947 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00004948 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004949 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00004950 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00004951 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00004952 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00004953 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00004954 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004955 return true;
John McCallc373d482010-01-27 01:50:18 +00004956
4957 // Ignore access information; it doesn't figure into redeclaration checking.
4958 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Douglas Gregorc42b6522010-04-09 21:02:29 +00004959 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004960
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004961 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004962 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00004963
4964 // If this is a friend declaration, then we're not really declaring
4965 // an explicit specialization.
4966 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004967
Douglas Gregord5cb8762009-10-07 00:13:32 +00004968 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00004969 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004970 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004971 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004972 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00004973 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004974 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004975
4976 // C++ [temp.expl.spec]p6:
4977 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004978 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004979 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004980 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004981 // use occurs; no diagnostic is required.
4982 FunctionTemplateSpecializationInfo *SpecInfo
4983 = Specialization->getTemplateSpecializationInfo();
4984 assert(SpecInfo && "Function template specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00004985
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004986 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00004987 if (!isFriend &&
4988 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00004989 TSK_ExplicitSpecialization,
4990 Specialization,
4991 SpecInfo->getTemplateSpecializationKind(),
4992 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004993 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004994 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004995
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004996 // Mark the prior declaration as an explicit specialization, so that later
4997 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00004998 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00004999 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005000 MarkUnusedFileScopedDecl(Specialization);
5001 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005002
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005003 // Turn the given function declaration into a function template
5004 // specialization, with the template arguments from the previous
5005 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005006 // Take copies of (semantic and syntactic) template argument lists.
5007 const TemplateArgumentList* TemplArgs = new (Context)
5008 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5009 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5010 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005011 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005012 TemplArgs, /*InsertPos=*/0,
5013 SpecInfo->getTemplateSpecializationKind(),
5014 TemplArgsAsWritten);
5015
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005016 // The "previous declaration" for this function template specialization is
5017 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005018 Previous.clear();
5019 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005020 return false;
5021}
5022
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005023/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005024/// specialization.
5025///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005026/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005027/// explicit member function specialization. On successful completion,
5028/// the function declaration \p FD will become a member function
5029/// specialization.
5030///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005031/// \param Member the member declaration, which will be updated to become a
5032/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005033///
John McCall68263142009-11-18 22:49:29 +00005034/// \param Previous the set of declarations, one of which may be specialized
5035/// by this function specialization; the set will be modified to contain the
5036/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005037bool
John McCall68263142009-11-18 22:49:29 +00005038Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005039 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005040
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005041 // Try to find the member we are instantiating.
5042 NamedDecl *Instantiation = 0;
5043 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005044 MemberSpecializationInfo *MSInfo = 0;
5045
John McCall68263142009-11-18 22:49:29 +00005046 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005047 // Nowhere to look anyway.
5048 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005049 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5050 I != E; ++I) {
5051 NamedDecl *D = (*I)->getUnderlyingDecl();
5052 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005053 if (Context.hasSameType(Function->getType(), Method->getType())) {
5054 Instantiation = Method;
5055 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005056 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005057 break;
5058 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005059 }
5060 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005061 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005062 VarDecl *PrevVar;
5063 if (Previous.isSingleResult() &&
5064 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005065 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005066 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005067 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005068 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005069 }
5070 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005071 CXXRecordDecl *PrevRecord;
5072 if (Previous.isSingleResult() &&
5073 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5074 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005075 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005076 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005077 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005078 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005079
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005080 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005081 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005082 // specializations are always out-of-line, the caller will complain about
5083 // this mismatch later.
5084 return false;
5085 }
John McCall77e8b112010-04-13 20:37:33 +00005086
5087 // If this is a friend, just bail out here before we start turning
5088 // things into explicit specializations.
5089 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5090 // Preserve instantiation information.
5091 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5092 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5093 cast<CXXMethodDecl>(InstantiatedFrom),
5094 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5095 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5096 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5097 cast<CXXRecordDecl>(InstantiatedFrom),
5098 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5099 }
5100
5101 Previous.clear();
5102 Previous.addDecl(Instantiation);
5103 return false;
5104 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005105
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005106 // Make sure that this is a specialization of a member.
5107 if (!InstantiatedFrom) {
5108 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5109 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005110 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5111 return true;
5112 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005113
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005114 // C++ [temp.expl.spec]p6:
5115 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005116 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005117 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005118 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005119 // use occurs; no diagnostic is required.
5120 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005121
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005122 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005123 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5124 TSK_ExplicitSpecialization,
5125 Instantiation,
5126 MSInfo->getTemplateSpecializationKind(),
5127 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005128 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005129 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005130
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005131 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005132 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005133 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005134 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005135 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005136 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005137
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005138 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005139 // the original declaration to note that it is an explicit specialization
5140 // (if it was previously an implicit instantiation). This latter step
5141 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005142 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005143 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5144 if (InstantiationFunction->getTemplateSpecializationKind() ==
5145 TSK_ImplicitInstantiation) {
5146 InstantiationFunction->setTemplateSpecializationKind(
5147 TSK_ExplicitSpecialization);
5148 InstantiationFunction->setLocation(Member->getLocation());
5149 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005150
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005151 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5152 cast<CXXMethodDecl>(InstantiatedFrom),
5153 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005154 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005155 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005156 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5157 if (InstantiationVar->getTemplateSpecializationKind() ==
5158 TSK_ImplicitInstantiation) {
5159 InstantiationVar->setTemplateSpecializationKind(
5160 TSK_ExplicitSpecialization);
5161 InstantiationVar->setLocation(Member->getLocation());
5162 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005163
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005164 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5165 cast<VarDecl>(InstantiatedFrom),
5166 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005167 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005168 } else {
5169 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005170 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5171 if (InstantiationClass->getTemplateSpecializationKind() ==
5172 TSK_ImplicitInstantiation) {
5173 InstantiationClass->setTemplateSpecializationKind(
5174 TSK_ExplicitSpecialization);
5175 InstantiationClass->setLocation(Member->getLocation());
5176 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005177
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005178 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005179 cast<CXXRecordDecl>(InstantiatedFrom),
5180 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005181 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005182
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005183 // Save the caller the trouble of having to figure out which declaration
5184 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005185 Previous.clear();
5186 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005187 return false;
5188}
5189
Douglas Gregor558c0322009-10-14 23:41:34 +00005190/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005191///
5192/// \returns true if a serious error occurs, false otherwise.
5193static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005194 SourceLocation InstLoc,
5195 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005196 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5197 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005198
Douglas Gregor669eed82010-07-13 00:10:04 +00005199 if (CurContext->isRecord()) {
5200 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5201 << D;
5202 return true;
5203 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005204
Douglas Gregor558c0322009-10-14 23:41:34 +00005205 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005206 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005207 // template.
5208 //
5209 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005210 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005211 !CurContext->Encloses(OrigContext)) {
5212 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005213 S.Diag(InstLoc,
5214 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005215 diag::err_explicit_instantiation_out_of_scope
5216 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005217 << D << NS;
5218 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005219 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005220 S.getLangOptions().CPlusPlus0x?
5221 diag::err_explicit_instantiation_must_be_global
5222 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005223 << D;
5224 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005225 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005226 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005227
Douglas Gregor558c0322009-10-14 23:41:34 +00005228 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005229 // If the name declared in the explicit instantiation is an unqualified
5230 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005231 // its template is declared or, if that namespace is inline (7.3.1), any
5232 // namespace from its enclosing namespace set.
5233 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005234 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005235
5236 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005237 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005238
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005239 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005240 S.getLangOptions().CPlusPlus0x?
5241 diag::err_explicit_instantiation_unqualified_wrong_namespace
5242 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005243 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005244 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005245 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005246}
5247
5248/// \brief Determine whether the given scope specifier has a template-id in it.
5249static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5250 if (!SS.isSet())
5251 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005252
Douglas Gregor558c0322009-10-14 23:41:34 +00005253 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005254 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005255 // or a static data member of a class template specialization, the name of
5256 // the class template specialization in the qualified-id for the member
5257 // name shall be a simple-template-id.
5258 //
5259 // C++98 has the same restriction, just worded differently.
5260 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5261 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005262 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005263 if (isa<TemplateSpecializationType>(T))
5264 return true;
5265
5266 return false;
5267}
5268
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005269// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005270DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005271Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005272 SourceLocation ExternLoc,
5273 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005274 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005275 SourceLocation KWLoc,
5276 const CXXScopeSpec &SS,
5277 TemplateTy TemplateD,
5278 SourceLocation TemplateNameLoc,
5279 SourceLocation LAngleLoc,
5280 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005281 SourceLocation RAngleLoc,
5282 AttributeList *Attr) {
5283 // Find the class template we're specializing
5284 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005285 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005286 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5287
5288 // Check that the specialization uses the same tag kind as the
5289 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005290 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5291 assert(Kind != TTK_Enum &&
5292 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005293 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005294 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005295 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005296 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005297 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005298 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005299 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005300 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005301 diag::note_previous_use);
5302 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5303 }
5304
Douglas Gregor558c0322009-10-14 23:41:34 +00005305 // C++0x [temp.explicit]p2:
5306 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005307 // definition and an explicit instantiation declaration. An explicit
5308 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005309 TemplateSpecializationKind TSK
5310 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5311 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005312
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005313 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005314 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005315 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005316
5317 // Check that the template argument list is well-formed for this
5318 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005319 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005320 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5321 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005322 return true;
5323
Douglas Gregor910f8002010-11-07 23:05:16 +00005324 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005325 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005326
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005327 // Find the class template specialization declaration that
5328 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005329 void *InsertPos = 0;
5330 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005331 = ClassTemplate->findSpecialization(Converted.data(),
5332 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005333
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005334 TemplateSpecializationKind PrevDecl_TSK
5335 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5336
Douglas Gregord5cb8762009-10-07 00:13:32 +00005337 // C++0x [temp.explicit]p2:
5338 // [...] An explicit instantiation shall appear in an enclosing
5339 // namespace of its template. [...]
5340 //
5341 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005342 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5343 SS.isSet()))
5344 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005345
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005346 ClassTemplateSpecializationDecl *Specialization = 0;
5347
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005348 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005349 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005350 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005351 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005352 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005353 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005354 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005355
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005356 // Even though HasNoEffect == true means that this explicit instantiation
5357 // has no effect on semantics, we go on to put its syntax in the AST.
5358
5359 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5360 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005361 // Since the only prior class template specialization with these
5362 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005363 // declaration node as our own, updating the source location
5364 // for the template name to reflect our new declaration.
5365 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005366 Specialization = PrevDecl;
5367 Specialization->setLocation(TemplateNameLoc);
5368 PrevDecl = 0;
5369 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005370 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005371
Douglas Gregor52604ab2009-09-11 21:19:12 +00005372 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005373 // Create a new class template specialization declaration node for
5374 // this explicit specialization.
5375 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005376 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005377 ClassTemplate->getDeclContext(),
5378 TemplateNameLoc,
5379 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005380 Converted.data(),
5381 Converted.size(),
5382 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005383 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005384
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005385 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005386 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005387 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005388 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005389 }
5390
5391 // Build the fully-sugared type for this explicit instantiation as
5392 // the user wrote in the explicit instantiation itself. This means
5393 // that we'll pretty-print the type retrieved from the
5394 // specialization's declaration the way that the user actually wrote
5395 // the explicit instantiation, rather than formatting the name based
5396 // on the "canonical" representation used to store the template
5397 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005398 TypeSourceInfo *WrittenTy
5399 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5400 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005401 Context.getTypeDeclType(Specialization));
5402 Specialization->setTypeAsWritten(WrittenTy);
5403 TemplateArgsIn.release();
5404
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005405 // Set source locations for keywords.
5406 Specialization->setExternLoc(ExternLoc);
5407 Specialization->setTemplateKeywordLoc(TemplateLoc);
5408
5409 // Add the explicit instantiation into its lexical context. However,
5410 // since explicit instantiations are never found by name lookup, we
5411 // just put it into the declaration context directly.
5412 Specialization->setLexicalDeclContext(CurContext);
5413 CurContext->addDecl(Specialization);
5414
5415 // Syntax is now OK, so return if it has no other effect on semantics.
5416 if (HasNoEffect) {
5417 // Set the template specialization kind.
5418 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005419 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005420 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005421
5422 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005423 // A definition of a class template or class member template
5424 // shall be in scope at the point of the explicit instantiation of
5425 // the class template or class member template.
5426 //
5427 // This check comes when we actually try to perform the
5428 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005429 ClassTemplateSpecializationDecl *Def
5430 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005431 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005432 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005433 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005434 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005435 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005436 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5437 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005438
Douglas Gregor0d035142009-10-27 18:42:08 +00005439 // Instantiate the members of this class template specialization.
5440 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005441 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005442 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005443 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5444
5445 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5446 // TSK_ExplicitInstantiationDefinition
5447 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5448 TSK == TSK_ExplicitInstantiationDefinition)
5449 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005450
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005451 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005452 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005453
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005454 // Set the template specialization kind.
5455 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005456 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005457}
5458
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005459// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005460DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005461Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005462 SourceLocation ExternLoc,
5463 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005464 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005465 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005466 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005467 IdentifierInfo *Name,
5468 SourceLocation NameLoc,
5469 AttributeList *Attr) {
5470
Douglas Gregor402abb52009-05-28 23:31:59 +00005471 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005472 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005473 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005474 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5475 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005476 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005477 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005478 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5479
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005480 if (!TagD)
5481 return true;
5482
John McCalld226f652010-08-21 09:40:31 +00005483 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005484 if (Tag->isEnum()) {
5485 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5486 << Context.getTypeDeclType(Tag);
5487 return true;
5488 }
5489
Douglas Gregord0c87372009-05-27 17:30:49 +00005490 if (Tag->isInvalidDecl())
5491 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005492
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005493 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5494 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5495 if (!Pattern) {
5496 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5497 << Context.getTypeDeclType(Record);
5498 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5499 return true;
5500 }
5501
Douglas Gregor558c0322009-10-14 23:41:34 +00005502 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005503 // If the explicit instantiation is for a class or member class, the
5504 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005505 // simple-template-id.
5506 //
5507 // C++98 has the same restriction, just worded differently.
5508 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005509 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005510 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005511
Douglas Gregor558c0322009-10-14 23:41:34 +00005512 // C++0x [temp.explicit]p2:
5513 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005514 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005515 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005516 TemplateSpecializationKind TSK
5517 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5518 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005519
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005520 // C++0x [temp.explicit]p2:
5521 // [...] An explicit instantiation shall appear in an enclosing
5522 // namespace of its template. [...]
5523 //
5524 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005525 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005526
Douglas Gregor454885e2009-10-15 15:54:05 +00005527 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005528 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005529 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005530 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005531 PrevDecl = Record;
5532 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005533 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005534 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005535 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005536 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005537 PrevDecl,
5538 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005539 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005540 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005541 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005542 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005543 return TagD;
5544 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005545
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005546 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00005547 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005548 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005549 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005550 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005551 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005552 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00005553 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005554 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00005555 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
5556 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005557 Diag(Pattern->getLocation(), diag::note_forward_declaration)
5558 << Pattern;
5559 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00005560 } else {
5561 if (InstantiateClass(NameLoc, Record, Def,
5562 getTemplateInstantiationArgs(Record),
5563 TSK))
5564 return true;
5565
Douglas Gregor952b0172010-02-11 01:04:33 +00005566 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00005567 if (!RecordDef)
5568 return true;
5569 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005570 }
5571
Douglas Gregor0d035142009-10-27 18:42:08 +00005572 // Instantiate all of the members of the class.
5573 InstantiateClassMembers(NameLoc, RecordDef,
5574 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005575
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005576 if (TSK == TSK_ExplicitInstantiationDefinition)
5577 MarkVTableUsed(NameLoc, RecordDef, true);
5578
Mike Stump390b4cc2009-05-16 07:39:55 +00005579 // FIXME: We don't have any representation for explicit instantiations of
5580 // member classes. Such a representation is not needed for compilation, but it
5581 // should be available for clients that want to see all of the declarations in
5582 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005583 return TagD;
5584}
5585
John McCallf312b1e2010-08-26 23:41:50 +00005586DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
5587 SourceLocation ExternLoc,
5588 SourceLocation TemplateLoc,
5589 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00005590 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00005591 // TODO: check if/when DNInfo should replace Name.
5592 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
5593 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005594 if (!Name) {
5595 if (!D.isInvalidType())
5596 Diag(D.getDeclSpec().getSourceRange().getBegin(),
5597 diag::err_explicit_instantiation_requires_name)
5598 << D.getDeclSpec().getSourceRange()
5599 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005600
Douglas Gregord5a423b2009-09-25 18:43:00 +00005601 return true;
5602 }
5603
5604 // The scope passed in may not be a decl scope. Zip up the scope tree until
5605 // we find one that is.
5606 while ((S->getFlags() & Scope::DeclScope) == 0 ||
5607 (S->getFlags() & Scope::TemplateParamScope) != 0)
5608 S = S->getParent();
5609
5610 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00005611 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
5612 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005613 if (R.isNull())
5614 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005615
Douglas Gregord5a423b2009-09-25 18:43:00 +00005616 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
5617 // Cannot explicitly instantiate a typedef.
5618 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
5619 << Name;
5620 return true;
5621 }
5622
Douglas Gregor663b5a02009-10-14 20:14:33 +00005623 // C++0x [temp.explicit]p1:
5624 // [...] An explicit instantiation of a function template shall not use the
5625 // inline or constexpr specifiers.
5626 // Presumably, this also applies to member functions of class templates as
5627 // well.
5628 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005629 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00005630 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00005631 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005632
Douglas Gregor663b5a02009-10-14 20:14:33 +00005633 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005634
Douglas Gregor558c0322009-10-14 23:41:34 +00005635 // C++0x [temp.explicit]p2:
5636 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005637 // definition and an explicit instantiation declaration. An explicit
5638 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00005639 TemplateSpecializationKind TSK
5640 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5641 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005642
Abramo Bagnara25777432010-08-11 22:01:17 +00005643 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00005644 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005645
5646 if (!R->isFunctionType()) {
5647 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005648 // A [...] static data member of a class template can be explicitly
5649 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005650 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00005651 if (Previous.isAmbiguous())
5652 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005653
John McCall1bcee0a2009-12-02 08:25:40 +00005654 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005655 if (!Prev || !Prev->isStaticDataMember()) {
5656 // We expect to see a data data member here.
5657 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
5658 << Name;
5659 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5660 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00005661 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00005662 return true;
5663 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005664
Douglas Gregord5a423b2009-09-25 18:43:00 +00005665 if (!Prev->getInstantiatedFromStaticDataMember()) {
5666 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005667 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005668 diag::err_explicit_instantiation_data_member_not_instantiated)
5669 << Prev;
5670 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
5671 // FIXME: Can we provide a note showing where this was declared?
5672 return true;
5673 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005674
Douglas Gregor558c0322009-10-14 23:41:34 +00005675 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005676 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005677 // or a static data member of a class template specialization, the name of
5678 // the class template specialization in the qualified-id for the member
5679 // name shall be a simple-template-id.
5680 //
5681 // C++98 has the same restriction, just worded differently.
5682 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005683 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005684 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005685 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005686
Douglas Gregor558c0322009-10-14 23:41:34 +00005687 // Check the scope of this explicit instantiation.
5688 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005689
Douglas Gregor454885e2009-10-15 15:54:05 +00005690 // Verify that it is okay to explicitly instantiate here.
5691 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
5692 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005693 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005694 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00005695 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005696 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005697 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005698 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005699 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005700 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005701
Douglas Gregord5a423b2009-09-25 18:43:00 +00005702 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005703 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005704 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005705 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005706
Douglas Gregord5a423b2009-09-25 18:43:00 +00005707 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00005708 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005709 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005710
5711 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00005712 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00005713 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00005714 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005715 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
5716 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00005717 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
5718 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00005719 ASTTemplateArgsPtr TemplateArgsPtr(*this,
5720 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00005721 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00005722 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00005723 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00005724 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00005725 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005726
Douglas Gregord5a423b2009-09-25 18:43:00 +00005727 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005728 // A [...] function [...] can be explicitly instantiated from its template.
5729 // A member function [...] of a class template can be explicitly
5730 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005731 // template.
John McCallc373d482010-01-27 01:50:18 +00005732 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005733 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5734 P != PEnd; ++P) {
5735 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00005736 if (!HasExplicitTemplateArgs) {
5737 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
5738 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
5739 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00005740
John McCallc373d482010-01-27 01:50:18 +00005741 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00005742 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
5743 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00005744 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00005745 }
5746 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005747
Douglas Gregord5a423b2009-09-25 18:43:00 +00005748 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
5749 if (!FunTmpl)
5750 continue;
5751
John McCall5769d612010-02-08 23:07:23 +00005752 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005753 FunctionDecl *Specialization = 0;
5754 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005755 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00005756 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005757 R, Specialization, Info)) {
5758 // FIXME: Keep track of almost-matches?
5759 (void)TDK;
5760 continue;
5761 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005762
John McCallc373d482010-01-27 01:50:18 +00005763 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005764 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005765
Douglas Gregord5a423b2009-09-25 18:43:00 +00005766 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005767 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005768 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005769 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005770 PDiag(diag::err_explicit_instantiation_not_known) << Name,
5771 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
5772 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00005773
John McCallc373d482010-01-27 01:50:18 +00005774 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00005775 return true;
John McCallc373d482010-01-27 01:50:18 +00005776
5777 // Ignore access control bits, we don't need them for redeclaration checking.
5778 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005779
Douglas Gregor0a897e32009-10-15 17:21:20 +00005780 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005781 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005782 diag::err_explicit_instantiation_member_function_not_instantiated)
5783 << Specialization
5784 << (Specialization->getTemplateSpecializationKind() ==
5785 TSK_ExplicitSpecialization);
5786 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
5787 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005788 }
5789
Douglas Gregor0a897e32009-10-15 17:21:20 +00005790 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00005791 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
5792 PrevDecl = Specialization;
5793
Douglas Gregor0a897e32009-10-15 17:21:20 +00005794 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005795 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005796 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005797 PrevDecl,
5798 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00005799 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005800 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00005801 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005802
Douglas Gregor0a897e32009-10-15 17:21:20 +00005803 // FIXME: We may still want to build some representation of this
5804 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005805 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005806 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00005807 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00005808
5809 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005810
Douglas Gregor0a897e32009-10-15 17:21:20 +00005811 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005812 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005813
Douglas Gregor558c0322009-10-14 23:41:34 +00005814 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005815 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005816 // or a static data member of a class template specialization, the name of
5817 // the class template specialization in the qualified-id for the member
5818 // name shall be a simple-template-id.
5819 //
5820 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005821 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005822 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005823 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00005824 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005825 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005826 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005827 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005828
Douglas Gregor558c0322009-10-14 23:41:34 +00005829 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005830 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00005831 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005832 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00005833 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005834
Douglas Gregord5a423b2009-09-25 18:43:00 +00005835 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00005836 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005837}
5838
John McCallf312b1e2010-08-26 23:41:50 +00005839TypeResult
John McCallc4e70192009-09-11 04:59:25 +00005840Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
5841 const CXXScopeSpec &SS, IdentifierInfo *Name,
5842 SourceLocation TagLoc, SourceLocation NameLoc) {
5843 // This has to hold, because SS is expected to be defined.
5844 assert(Name && "Expected a name in a dependent tag");
5845
5846 NestedNameSpecifier *NNS
5847 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
5848 if (!NNS)
5849 return true;
5850
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005851 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00005852
Douglas Gregor48c89f42010-04-24 16:38:41 +00005853 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
5854 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005855 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00005856 return true;
5857 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005858
5859 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
John McCallb3d87482010-08-24 05:47:05 +00005860 return ParsedType::make(Context.getDependentNameType(Kwd, NNS, Name));
John McCallc4e70192009-09-11 04:59:25 +00005861}
5862
John McCallf312b1e2010-08-26 23:41:50 +00005863TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005864Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
5865 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005866 SourceLocation IdLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00005867 NestedNameSpecifier *NNS
Douglas Gregord57959a2009-03-27 23:10:48 +00005868 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
5869 if (!NNS)
5870 return true;
5871
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005872 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
5873 !getLangOptions().CPlusPlus0x)
5874 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005875 << FixItHint::CreateRemoval(TypenameLoc);
5876
Douglas Gregor107de902010-04-24 15:35:55 +00005877 QualType T = CheckTypenameType(ETK_Typename, NNS, II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00005878 TypenameLoc, SS.getRange(), IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00005879 if (T.isNull())
5880 return true;
John McCall63b43852010-04-29 23:50:39 +00005881
5882 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
5883 if (isa<DependentNameType>(T)) {
5884 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00005885 TL.setKeywordLoc(TypenameLoc);
5886 TL.setQualifierRange(SS.getRange());
5887 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00005888 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005889 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00005890 TL.setKeywordLoc(TypenameLoc);
5891 TL.setQualifierRange(SS.getRange());
5892 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00005893 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005894
John McCallb3d87482010-08-24 05:47:05 +00005895 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00005896}
5897
John McCallf312b1e2010-08-26 23:41:50 +00005898TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005899Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
5900 const CXXScopeSpec &SS, SourceLocation TemplateLoc,
John McCallb3d87482010-08-24 05:47:05 +00005901 ParsedType Ty) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005902 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
5903 !getLangOptions().CPlusPlus0x)
5904 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005905 << FixItHint::CreateRemoval(TypenameLoc);
5906
John McCall4e449832010-05-28 23:32:21 +00005907 TypeSourceInfo *InnerTSI = 0;
5908 QualType T = GetTypeFromParser(Ty, &InnerTSI);
John McCall4e449832010-05-28 23:32:21 +00005909
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005910 assert(isa<TemplateSpecializationType>(T) &&
John McCall4e449832010-05-28 23:32:21 +00005911 "Expected a template specialization type");
Douglas Gregor17343172009-04-01 00:28:59 +00005912
Douglas Gregor6946baf2009-09-02 13:05:45 +00005913 if (computeDeclContext(SS, false)) {
5914 // If we can compute a declaration context, then the "typename"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005915 // keyword was superfluous. Just build an ElaboratedType to keep
Douglas Gregor6946baf2009-09-02 13:05:45 +00005916 // track of the nested-name-specifier.
John McCall4e449832010-05-28 23:32:21 +00005917
5918 // Push the inner type, preserving its source locations if possible.
5919 TypeLocBuilder Builder;
5920 if (InnerTSI)
5921 Builder.pushFullCopy(InnerTSI->getTypeLoc());
5922 else
Douglas Gregorc21c7e92011-01-25 19:13:18 +00005923 Builder.push<TemplateSpecializationTypeLoc>(T).initialize(Context,
5924 TemplateLoc);
John McCall4e449832010-05-28 23:32:21 +00005925
Abramo Bagnara22f638a2010-08-10 13:46:45 +00005926 /* Note: NNS already embedded in template specialization type T. */
5927 T = Context.getElaboratedType(ETK_Typename, /*NNS=*/0, T);
John McCall4e449832010-05-28 23:32:21 +00005928 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
5929 TL.setKeywordLoc(TypenameLoc);
5930 TL.setQualifierRange(SS.getRange());
5931
5932 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
John McCallb3d87482010-08-24 05:47:05 +00005933 return CreateParsedType(T, TSI);
Douglas Gregor6946baf2009-09-02 13:05:45 +00005934 }
Mike Stump1eb44332009-09-09 15:08:12 +00005935
John McCall33500952010-06-11 00:33:02 +00005936 // TODO: it's really silly that we make a template specialization
5937 // type earlier only to drop it again here.
John McCallf4c73712011-01-19 06:33:43 +00005938 const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T);
John McCall33500952010-06-11 00:33:02 +00005939 DependentTemplateName *DTN =
5940 TST->getTemplateName().getAsDependentTemplateName();
5941 assert(DTN && "dependent template has non-dependent name?");
Abramo Bagnara22f638a2010-08-10 13:46:45 +00005942 assert(DTN->getQualifier()
5943 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
5944 T = Context.getDependentTemplateSpecializationType(ETK_Typename,
5945 DTN->getQualifier(),
John McCall33500952010-06-11 00:33:02 +00005946 DTN->getIdentifier(),
5947 TST->getNumArgs(),
5948 TST->getArgs());
John McCall63b43852010-04-29 23:50:39 +00005949 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
John McCall33500952010-06-11 00:33:02 +00005950 DependentTemplateSpecializationTypeLoc TL =
5951 cast<DependentTemplateSpecializationTypeLoc>(TSI->getTypeLoc());
5952 if (InnerTSI) {
5953 TemplateSpecializationTypeLoc TSTL =
5954 cast<TemplateSpecializationTypeLoc>(InnerTSI->getTypeLoc());
5955 TL.setLAngleLoc(TSTL.getLAngleLoc());
5956 TL.setRAngleLoc(TSTL.getRAngleLoc());
5957 for (unsigned I = 0, E = TST->getNumArgs(); I != E; ++I)
5958 TL.setArgLocInfo(I, TSTL.getArgLocInfo(I));
5959 } else {
Douglas Gregorc21c7e92011-01-25 19:13:18 +00005960 // FIXME: Poor source-location information here.
5961 TL.initializeLocal(Context, TemplateLoc);
John McCall33500952010-06-11 00:33:02 +00005962 }
John McCall4e449832010-05-28 23:32:21 +00005963 TL.setKeywordLoc(TypenameLoc);
5964 TL.setQualifierRange(SS.getRange());
John McCallb3d87482010-08-24 05:47:05 +00005965 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00005966}
5967
Douglas Gregord57959a2009-03-27 23:10:48 +00005968/// \brief Build the type that describes a C++ typename specifier,
5969/// e.g., "typename T::type".
5970QualType
Douglas Gregor107de902010-04-24 15:35:55 +00005971Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
5972 NestedNameSpecifier *NNS, const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00005973 SourceLocation KeywordLoc, SourceRange NNSRange,
5974 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00005975 CXXScopeSpec SS;
5976 SS.setScopeRep(NNS);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00005977 SS.setRange(NNSRange);
Douglas Gregord57959a2009-03-27 23:10:48 +00005978
John McCall77bb1aa2010-05-01 00:40:08 +00005979 DeclContext *Ctx = computeDeclContext(SS);
5980 if (!Ctx) {
5981 // If the nested-name-specifier is dependent and couldn't be
5982 // resolved to a type, build a typename type.
5983 assert(NNS->isDependent());
5984 return Context.getDependentNameType(Keyword, NNS, &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00005985 }
Douglas Gregord57959a2009-03-27 23:10:48 +00005986
John McCall77bb1aa2010-05-01 00:40:08 +00005987 // If the nested-name-specifier refers to the current instantiation,
5988 // the "typename" keyword itself is superfluous. In C++03, the
5989 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
5990 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00005991 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00005992
John McCall77bb1aa2010-05-01 00:40:08 +00005993 if (RequireCompleteDeclContext(SS, Ctx))
5994 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00005995
5996 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00005997 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00005998 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00005999 unsigned DiagID = 0;
6000 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006001 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006002 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006003 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006004 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006005
6006 case LookupResult::FoundUnresolvedValue: {
6007 // We found a using declaration that is a value. Most likely, the using
6008 // declaration itself is meant to have the 'typename' keyword.
6009 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(),
6010 IILoc);
6011 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6012 << Name << Ctx << FullRange;
6013 if (UnresolvedUsingValueDecl *Using
6014 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
6015 SourceLocation Loc = Using->getTargetNestedNameRange().getBegin();
6016 Diag(Loc, diag::note_using_value_decl_missing_typename)
6017 << FixItHint::CreateInsertion(Loc, "typename ");
6018 }
6019 }
6020 // Fall through to create a dependent typename type, from which we can recover
6021 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006022
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006023 case LookupResult::NotFoundInCurrentInstantiation:
6024 // Okay, it's a member of an unknown instantiation.
Douglas Gregor107de902010-04-24 15:35:55 +00006025 return Context.getDependentNameType(Keyword, NNS, &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006026
6027 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006028 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006029 // We found a type. Build an ElaboratedType, since the
6030 // typename-specifier was just sugar.
6031 return Context.getElaboratedType(ETK_Typename, NNS,
6032 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006033 }
6034
6035 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006036 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006037 break;
6038
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006039
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006040 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006041 return QualType();
6042
Douglas Gregord57959a2009-03-27 23:10:48 +00006043 case LookupResult::FoundOverloaded:
6044 DiagID = diag::err_typename_nested_not_type;
6045 Referenced = *Result.begin();
6046 break;
6047
John McCall6e247262009-10-10 05:48:19 +00006048 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006049 return QualType();
6050 }
6051
6052 // If we get here, it's because name lookup did not find a
6053 // type. Emit an appropriate diagnostic and return an error.
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006054 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(),
6055 IILoc);
6056 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006057 if (Referenced)
6058 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6059 << Name;
6060 return QualType();
6061}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006062
6063namespace {
6064 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006065 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006066 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006067 SourceLocation Loc;
6068 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006069
Douglas Gregor4a959d82009-08-06 16:20:37 +00006070 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006071 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006072
Mike Stump1eb44332009-09-09 15:08:12 +00006073 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006074 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006075 DeclarationName Entity)
6076 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006077 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006078
6079 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006080 /// transformed.
6081 ///
6082 /// For the purposes of type reconstruction, a type has already been
6083 /// transformed if it is NULL or if it is not dependent.
6084 bool AlreadyTransformed(QualType T) {
6085 return T.isNull() || !T->isDependentType();
6086 }
Mike Stump1eb44332009-09-09 15:08:12 +00006087
6088 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006089 /// rebuilt.
6090 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006091
Douglas Gregor4a959d82009-08-06 16:20:37 +00006092 /// \brief Returns the name of the entity whose type is being rebuilt.
6093 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006094
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006095 /// \brief Sets the "base" location and entity when that
6096 /// information is known based on another transformation.
6097 void setBase(SourceLocation Loc, DeclarationName Entity) {
6098 this->Loc = Loc;
6099 this->Entity = Entity;
6100 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006101 };
6102}
6103
Douglas Gregor4a959d82009-08-06 16:20:37 +00006104/// \brief Rebuilds a type within the context of the current instantiation.
6105///
Mike Stump1eb44332009-09-09 15:08:12 +00006106/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006107/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006108/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006109/// partial specialization thereof). This routine will rebuild that type now
6110/// that we have entered the declarator's scope, which may produce different
6111/// canonical types, e.g.,
6112///
6113/// \code
6114/// template<typename T>
6115/// struct X {
6116/// typedef T* pointer;
6117/// pointer data();
6118/// };
6119///
6120/// template<typename T>
6121/// typename X<T>::pointer X<T>::data() { ... }
6122/// \endcode
6123///
Douglas Gregor4714c122010-03-31 17:34:00 +00006124/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006125/// since we do not know that we can look into X<T> when we parsed the type.
6126/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006127/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006128/// as the canonical type of T*, allowing the return types of the out-of-line
6129/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006130TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6131 SourceLocation Loc,
6132 DeclarationName Name) {
6133 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006134 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006135
Douglas Gregor4a959d82009-08-06 16:20:37 +00006136 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6137 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006138}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006139
John McCall60d7b3a2010-08-24 06:29:42 +00006140ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006141 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6142 DeclarationName());
6143 return Rebuilder.TransformExpr(E);
6144}
6145
John McCall63b43852010-04-29 23:50:39 +00006146bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
6147 if (SS.isInvalid()) return true;
John McCall31f17ec2010-04-27 00:57:59 +00006148
6149 NestedNameSpecifier *NNS = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
6150 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6151 DeclarationName());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006152 NestedNameSpecifier *Rebuilt =
John McCall31f17ec2010-04-27 00:57:59 +00006153 Rebuilder.TransformNestedNameSpecifier(NNS, SS.getRange());
John McCall63b43852010-04-29 23:50:39 +00006154 if (!Rebuilt) return true;
6155
6156 SS.setScopeRep(Rebuilt);
6157 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006158}
6159
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006160/// \brief Produces a formatted string that describes the binding of
6161/// template parameters to template arguments.
6162std::string
6163Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6164 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006165 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006166}
6167
6168std::string
6169Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6170 const TemplateArgument *Args,
6171 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006172 llvm::SmallString<128> Str;
6173 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006174
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006175 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006176 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006177
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006178 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006179 if (I >= NumArgs)
6180 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006181
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006182 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006183 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006184 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006185 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006186
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006187 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006188 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006189 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006190 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006191 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006192
Douglas Gregor87dd6972010-12-20 16:52:59 +00006193 Out << " = ";
6194 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006195 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006196
6197 Out << ']';
6198 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006199}