blob: 5b09dfcee1572b1029b9529d4ec19ea0463da4dd [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())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000786 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000787}
788
John McCallf312b1e2010-08-26 23:41:50 +0000789DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000790Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000791 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000792 IdentifierInfo *Name, SourceLocation NameLoc,
793 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000794 TemplateParameterList *TemplateParams,
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000795 AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000796 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000797 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000798 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000799 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000800
801 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000802 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000803 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000804
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000805 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
806 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000807
808 // There is no such thing as an unnamed class template.
809 if (!Name) {
810 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000811 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000812 }
813
814 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000815 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000816 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000817 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000818 if (SS.isNotEmpty() && !SS.isInvalid()) {
819 SemanticContext = computeDeclContext(SS, true);
820 if (!SemanticContext) {
821 // FIXME: Produce a reasonable diagnostic here
822 return true;
823 }
Mike Stump1eb44332009-09-09 15:08:12 +0000824
John McCall77bb1aa2010-05-01 00:40:08 +0000825 if (RequireCompleteDeclContext(SS, SemanticContext))
826 return true;
827
John McCalla24dc2e2009-11-17 02:14:36 +0000828 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000829 } else {
830 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000831 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000832 }
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Douglas Gregor57265e32010-04-12 16:00:01 +0000834 if (Previous.isAmbiguous())
835 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000836
Douglas Gregorddc29e12009-02-06 22:42:48 +0000837 NamedDecl *PrevDecl = 0;
838 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000839 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000840
Douglas Gregorddc29e12009-02-06 22:42:48 +0000841 // If there is a previous declaration with the same name, check
842 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000843 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000844 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000845
846 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000847 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000848 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000849 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000850 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
851 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000852 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000853 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
854 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
855 PrevClassTemplate
856 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
857 ->getSpecializedTemplate();
858 }
859 }
860
John McCall65c49462009-12-18 11:25:59 +0000861 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000862 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000863 // [...] When looking for a prior declaration of a class or a function
864 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000865 // function is neither a qualified name nor a template-id, scopes outside
866 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000867 if (!SS.isSet()) {
868 DeclContext *OutermostContext = CurContext;
869 while (!OutermostContext->isFileContext())
870 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000871
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000872 if (PrevDecl &&
873 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
874 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
875 SemanticContext = PrevDecl->getDeclContext();
876 } else {
877 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000878 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000879 // declaration.
880 PrevDecl = PrevClassTemplate = 0;
881 SemanticContext = OutermostContext;
882 }
John McCalle129d442009-12-17 23:21:11 +0000883 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000884
John McCalle129d442009-12-17 23:21:11 +0000885 if (CurContext->isDependentContext()) {
886 // If this is a dependent context, we don't want to link the friend
887 // class template to the template in scope, because that would perform
888 // checking of the template parameter lists that can't be performed
889 // until the outer context is instantiated.
890 PrevDecl = PrevClassTemplate = 0;
891 }
892 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
893 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000894
Douglas Gregorddc29e12009-02-06 22:42:48 +0000895 if (PrevClassTemplate) {
896 // Ensure that the template parameter lists are compatible.
897 if (!TemplateParameterListsAreEqual(TemplateParams,
898 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000899 /*Complain=*/true,
900 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000901 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000902
903 // C++ [temp.class]p4:
904 // In a redeclaration, partial specialization, explicit
905 // specialization or explicit instantiation of a class template,
906 // the class-key shall agree in kind with the original class
907 // template declaration (7.1.5.3).
908 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000909 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000910 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000911 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000912 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000913 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000914 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000915 }
916
Douglas Gregorddc29e12009-02-06 22:42:48 +0000917 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000918 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000919 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000920 Diag(NameLoc, diag::err_redefinition) << Name;
921 Diag(Def->getLocation(), diag::note_previous_definition);
922 // FIXME: Would it make sense to try to "forget" the previous
923 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000924 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000925 }
926 }
927 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
928 // Maybe we will complain about the shadowed template parameter.
929 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
930 // Just pretend that we didn't see the previous declaration.
931 PrevDecl = 0;
932 } else if (PrevDecl) {
933 // C++ [temp]p5:
934 // A class template shall not have the same name as any other
935 // template, class, function, object, enumeration, enumerator,
936 // namespace, or type in the same scope (3.3), except as specified
937 // in (14.5.4).
938 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
939 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000940 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000941 }
942
Douglas Gregord684b002009-02-10 19:49:53 +0000943 // Check the template parameter list of this declaration, possibly
944 // merging in the template parameter list from the previous class
945 // template declaration.
946 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000947 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000948 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000949 SemanticContext->isRecord() &&
950 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000951 ? TPC_ClassTemplateMember
952 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000953 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Douglas Gregor57265e32010-04-12 16:00:01 +0000955 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000956 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000957 // template out-of-line.
958 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
959 !(TUK == TUK_Friend && CurContext->isDependentContext()))
960 Diag(NameLoc, diag::err_member_def_does_not_match)
961 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000962 }
963
Mike Stump1eb44332009-09-09 15:08:12 +0000964 CXXRecordDecl *NewClass =
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000965 CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000966 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000967 PrevClassTemplate->getTemplatedDecl() : 0,
968 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000969 SetNestedNameSpecifier(NewClass, SS);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000970
971 ClassTemplateDecl *NewTemplate
972 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
973 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000974 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000975 NewClass->setDescribedClassTemplate(NewTemplate);
976
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000977 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000978 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000979 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000980 assert(T->isDependentType() && "Class template type is not dependent?");
981 (void)T;
982
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000983 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000984 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000985 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000986 PrevClassTemplate->getInstantiatedFromMemberTemplate())
987 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000988
Anders Carlsson4cbe82c2009-03-26 01:24:28 +0000989 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +0000990 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +0000991 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Douglas Gregorddc29e12009-02-06 22:42:48 +0000993 // Set the lexical context of these templates
994 NewClass->setLexicalDeclContext(CurContext);
995 NewTemplate->setLexicalDeclContext(CurContext);
996
John McCall0f434ec2009-07-31 02:45:11 +0000997 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +0000998 NewClass->startDefinition();
999
1000 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001001 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001002
John McCall05b23ea2009-09-14 21:59:20 +00001003 if (TUK != TUK_Friend)
1004 PushOnScopeChains(NewTemplate, S);
1005 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001006 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001007 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001008 NewClass->setAccess(PrevClassTemplate->getAccess());
1009 }
John McCall05b23ea2009-09-14 21:59:20 +00001010
Douglas Gregord85bea22009-09-26 06:47:28 +00001011 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1012 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001013
John McCall05b23ea2009-09-14 21:59:20 +00001014 // Friend templates are visible in fairly strange ways.
1015 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001016 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001017 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1018 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1019 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001020 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001021 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001022
Douglas Gregord85bea22009-09-26 06:47:28 +00001023 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1024 NewClass->getLocation(),
1025 NewTemplate,
1026 /*FIXME:*/NewClass->getLocation());
1027 Friend->setAccess(AS_public);
1028 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001029 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001030
Douglas Gregord684b002009-02-10 19:49:53 +00001031 if (Invalid) {
1032 NewTemplate->setInvalidDecl();
1033 NewClass->setInvalidDecl();
1034 }
John McCalld226f652010-08-21 09:40:31 +00001035 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001036}
1037
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001038/// \brief Diagnose the presence of a default template argument on a
1039/// template parameter, which is ill-formed in certain contexts.
1040///
1041/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001042static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001043 Sema::TemplateParamListContext TPC,
1044 SourceLocation ParamLoc,
1045 SourceRange DefArgRange) {
1046 switch (TPC) {
1047 case Sema::TPC_ClassTemplate:
1048 return false;
1049
1050 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001051 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001052 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001053 // A default template-argument shall not be specified in a
1054 // function template declaration or a function template
1055 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001056 // If a friend function template declaration specifies a default
1057 // template-argument, that declaration shall be a definition and shall be
1058 // the only declaration of the function template in the translation unit.
1059 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001060 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001061 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001062 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001063 << DefArgRange;
1064 return false;
1065
1066 case Sema::TPC_ClassTemplateMember:
1067 // C++0x [temp.param]p9:
1068 // A default template-argument shall not be specified in the
1069 // template-parameter-lists of the definition of a member of a
1070 // class template that appears outside of the member's class.
1071 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1072 << DefArgRange;
1073 return true;
1074
1075 case Sema::TPC_FriendFunctionTemplate:
1076 // C++ [temp.param]p9:
1077 // A default template-argument shall not be specified in a
1078 // friend template declaration.
1079 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1080 << DefArgRange;
1081 return true;
1082
1083 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1084 // for friend function templates if there is only a single
1085 // declaration (and it is a definition). Strange!
1086 }
1087
1088 return false;
1089}
1090
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001091/// \brief Check for unexpanded parameter packs within the template parameters
1092/// of a template template parameter, recursively.
1093bool DiagnoseUnexpandedParameterPacks(Sema &S, TemplateTemplateParmDecl *TTP){
1094 TemplateParameterList *Params = TTP->getTemplateParameters();
1095 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1096 NamedDecl *P = Params->getParam(I);
1097 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001098 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001099 NTTP->getTypeSourceInfo(),
1100 Sema::UPPC_NonTypeTemplateParameterType))
1101 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001102
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001103 continue;
1104 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001105
1106 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001107 = dyn_cast<TemplateTemplateParmDecl>(P))
1108 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1109 return true;
1110 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001111
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001112 return false;
1113}
1114
Douglas Gregord684b002009-02-10 19:49:53 +00001115/// \brief Checks the validity of a template parameter list, possibly
1116/// considering the template parameter list from a previous
1117/// declaration.
1118///
1119/// If an "old" template parameter list is provided, it must be
1120/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1121/// template parameter list.
1122///
1123/// \param NewParams Template parameter list for a new template
1124/// declaration. This template parameter list will be updated with any
1125/// default arguments that are carried through from the previous
1126/// template parameter list.
1127///
1128/// \param OldParams If provided, template parameter list from a
1129/// previous declaration of the same template. Default template
1130/// arguments will be merged from the old template parameter list to
1131/// the new template parameter list.
1132///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001133/// \param TPC Describes the context in which we are checking the given
1134/// template parameter list.
1135///
Douglas Gregord684b002009-02-10 19:49:53 +00001136/// \returns true if an error occurred, false otherwise.
1137bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001138 TemplateParameterList *OldParams,
1139 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001140 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Douglas Gregord684b002009-02-10 19:49:53 +00001142 // C++ [temp.param]p10:
1143 // The set of default template-arguments available for use with a
1144 // template declaration or definition is obtained by merging the
1145 // default arguments from the definition (if in scope) and all
1146 // declarations in scope in the same way default function
1147 // arguments are (8.3.6).
1148 bool SawDefaultArgument = false;
1149 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001150
Anders Carlsson49d25572009-06-12 23:20:15 +00001151 bool SawParameterPack = false;
1152 SourceLocation ParameterPackLoc;
1153
Mike Stump1a35fde2009-02-11 23:03:27 +00001154 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001155 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001156 if (OldParams)
1157 OldParam = OldParams->begin();
1158
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001159 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001160 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1161 NewParamEnd = NewParams->end();
1162 NewParam != NewParamEnd; ++NewParam) {
1163 // Variables used to diagnose redundant default arguments
1164 bool RedundantDefaultArg = false;
1165 SourceLocation OldDefaultLoc;
1166 SourceLocation NewDefaultLoc;
1167
1168 // Variables used to diagnose missing default arguments
1169 bool MissingDefaultArg = false;
1170
Anders Carlsson49d25572009-06-12 23:20:15 +00001171 // C++0x [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001172 // If a template parameter of a primary class template is a template
Douglas Gregor1ed64762011-01-05 16:19:19 +00001173 // parameter pack, it shall be the last template parameter.
1174 if (SawParameterPack && TPC == TPC_ClassTemplate) {
Mike Stump1eb44332009-09-09 15:08:12 +00001175 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001176 diag::err_template_param_pack_must_be_last_template_parameter);
1177 Invalid = true;
1178 }
1179
Douglas Gregord684b002009-02-10 19:49:53 +00001180 if (TemplateTypeParmDecl *NewTypeParm
1181 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001182 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001183 if (NewTypeParm->hasDefaultArgument() &&
1184 DiagnoseDefaultTemplateArgument(*this, TPC,
1185 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001186 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001187 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001188 NewTypeParm->removeDefaultArgument();
1189
1190 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001191 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001192 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001193
Anders Carlsson49d25572009-06-12 23:20:15 +00001194 if (NewTypeParm->isParameterPack()) {
1195 assert(!NewTypeParm->hasDefaultArgument() &&
1196 "Parameter packs can't have a default argument!");
1197 SawParameterPack = true;
1198 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001199 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001200 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001201 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1202 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1203 SawDefaultArgument = true;
1204 RedundantDefaultArg = true;
1205 PreviousDefaultArgLoc = NewDefaultLoc;
1206 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1207 // Merge the default argument from the old declaration to the
1208 // new declaration.
1209 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001210 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001211 true);
1212 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1213 } else if (NewTypeParm->hasDefaultArgument()) {
1214 SawDefaultArgument = true;
1215 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1216 } else if (SawDefaultArgument)
1217 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001218 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001219 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001220 // Check for unexpanded parameter packs.
1221 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001222 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001223 UPPC_NonTypeTemplateParameterType)) {
1224 Invalid = true;
1225 continue;
1226 }
1227
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001228 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001229 if (NewNonTypeParm->hasDefaultArgument() &&
1230 DiagnoseDefaultTemplateArgument(*this, TPC,
1231 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001232 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001233 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001234 }
1235
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001236 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001237 NonTypeTemplateParmDecl *OldNonTypeParm
1238 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001239 if (NewNonTypeParm->isParameterPack()) {
1240 assert(!NewNonTypeParm->hasDefaultArgument() &&
1241 "Parameter packs can't have a default argument!");
1242 SawParameterPack = true;
1243 ParameterPackLoc = NewNonTypeParm->getLocation();
1244 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001245 NewNonTypeParm->hasDefaultArgument()) {
1246 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1247 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1248 SawDefaultArgument = true;
1249 RedundantDefaultArg = true;
1250 PreviousDefaultArgLoc = NewDefaultLoc;
1251 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1252 // Merge the default argument from the old declaration to the
1253 // new declaration.
1254 SawDefaultArgument = true;
1255 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001256 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001257 // parameter.
1258 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001259 OldNonTypeParm->getDefaultArgument(),
1260 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001261 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1262 } else if (NewNonTypeParm->hasDefaultArgument()) {
1263 SawDefaultArgument = true;
1264 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1265 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001266 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001267 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001268 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001269 TemplateTemplateParmDecl *NewTemplateParm
1270 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001271
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001272 // Check for unexpanded parameter packs, recursively.
1273 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1274 Invalid = true;
1275 continue;
1276 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001277
1278 if (NewTemplateParm->hasDefaultArgument() &&
1279 DiagnoseDefaultTemplateArgument(*this, TPC,
1280 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001281 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001282 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001283
1284 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001285 TemplateTemplateParmDecl *OldTemplateParm
1286 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001287 if (NewTemplateParm->isParameterPack()) {
1288 assert(!NewTemplateParm->hasDefaultArgument() &&
1289 "Parameter packs can't have a default argument!");
1290 SawParameterPack = true;
1291 ParameterPackLoc = NewTemplateParm->getLocation();
1292 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001293 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001294 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1295 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001296 SawDefaultArgument = true;
1297 RedundantDefaultArg = true;
1298 PreviousDefaultArgLoc = NewDefaultLoc;
1299 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1300 // Merge the default argument from the old declaration to the
1301 // new declaration.
1302 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001303 // FIXME: We need to create a new kind of "default argument" expression
1304 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001305 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001306 OldTemplateParm->getDefaultArgument(),
1307 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001308 PreviousDefaultArgLoc
1309 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001310 } else if (NewTemplateParm->hasDefaultArgument()) {
1311 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001312 PreviousDefaultArgLoc
1313 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001314 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001315 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001316 }
1317
1318 if (RedundantDefaultArg) {
1319 // C++ [temp.param]p12:
1320 // A template-parameter shall not be given default arguments
1321 // by two different declarations in the same scope.
1322 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1323 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1324 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001325 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001326 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001327 // If a template-parameter of a class template has a default
1328 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001329 // have a default template-argument supplied or be a template parameter
1330 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001331 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001332 diag::err_template_param_default_arg_missing);
1333 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1334 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001335 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001336 }
1337
1338 // If we have an old template parameter list that we're merging
1339 // in, move on to the next parameter.
1340 if (OldParams)
1341 ++OldParam;
1342 }
1343
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001344 // We were missing some default arguments at the end of the list, so remove
1345 // all of the default arguments.
1346 if (RemoveDefaultArguments) {
1347 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1348 NewParamEnd = NewParams->end();
1349 NewParam != NewParamEnd; ++NewParam) {
1350 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1351 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001352 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001353 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1354 NTTP->removeDefaultArgument();
1355 else
1356 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1357 }
1358 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001359
Douglas Gregord684b002009-02-10 19:49:53 +00001360 return Invalid;
1361}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001362
John McCall4e2cbb22010-10-20 05:44:58 +00001363namespace {
1364
1365/// A class which looks for a use of a certain level of template
1366/// parameter.
1367struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1368 typedef RecursiveASTVisitor<DependencyChecker> super;
1369
1370 unsigned Depth;
1371 bool Match;
1372
1373 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1374 NamedDecl *ND = Params->getParam(0);
1375 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1376 Depth = PD->getDepth();
1377 } else if (NonTypeTemplateParmDecl *PD =
1378 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1379 Depth = PD->getDepth();
1380 } else {
1381 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1382 }
1383 }
1384
1385 bool Matches(unsigned ParmDepth) {
1386 if (ParmDepth >= Depth) {
1387 Match = true;
1388 return true;
1389 }
1390 return false;
1391 }
1392
1393 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1394 return !Matches(T->getDepth());
1395 }
1396
1397 bool TraverseTemplateName(TemplateName N) {
1398 if (TemplateTemplateParmDecl *PD =
1399 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1400 if (Matches(PD->getDepth())) return false;
1401 return super::TraverseTemplateName(N);
1402 }
1403
1404 bool VisitDeclRefExpr(DeclRefExpr *E) {
1405 if (NonTypeTemplateParmDecl *PD =
1406 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1407 if (PD->getDepth() == Depth) {
1408 Match = true;
1409 return false;
1410 }
1411 }
1412 return super::VisitDeclRefExpr(E);
1413 }
1414};
1415}
1416
1417/// Determines whether a template-id depends on the given parameter
1418/// list.
1419static bool
1420DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId,
1421 TemplateParameterList *Params) {
1422 DependencyChecker Checker(Params);
1423 Checker.TraverseType(QualType(TemplateId, 0));
1424 return Checker.Match;
1425}
1426
Mike Stump1eb44332009-09-09 15:08:12 +00001427/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001428/// specifier, returning the template parameter list that applies to the
1429/// name.
1430///
1431/// \param DeclStartLoc the start of the declaration that has a scope
1432/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001433///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001434/// \param SS the scope specifier that will be matched to the given template
1435/// parameter lists. This scope specifier precedes a qualified name that is
1436/// being declared.
1437///
1438/// \param ParamLists the template parameter lists, from the outermost to the
1439/// innermost template parameter lists.
1440///
1441/// \param NumParamLists the number of template parameter lists in ParamLists.
1442///
John McCall77e8b112010-04-13 20:37:33 +00001443/// \param IsFriend Whether to apply the slightly different rules for
1444/// matching template parameters to scope specifiers in friend
1445/// declarations.
1446///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001447/// \param IsExplicitSpecialization will be set true if the entity being
1448/// declared is an explicit specialization, false otherwise.
1449///
Mike Stump1eb44332009-09-09 15:08:12 +00001450/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001451/// name that is preceded by the scope specifier @p SS. This template
1452/// parameter list may be have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001453/// template) or may have no template parameters (if we're declaring a
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001454/// template specialization), or may be NULL (if we were's declaring isn't
1455/// itself a template).
1456TemplateParameterList *
1457Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
1458 const CXXScopeSpec &SS,
1459 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001460 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001461 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001462 bool &IsExplicitSpecialization,
1463 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001464 IsExplicitSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001465
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001466 // Find the template-ids that occur within the nested-name-specifier. These
1467 // template-ids will match up with the template parameter lists.
1468 llvm::SmallVector<const TemplateSpecializationType *, 4>
1469 TemplateIdsInSpecifier;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001470 llvm::SmallVector<ClassTemplateSpecializationDecl *, 4>
1471 ExplicitSpecializationsInSpecifier;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001472 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
1473 NNS; NNS = NNS->getPrefix()) {
John McCall4b2b02b2009-12-15 02:19:47 +00001474 const Type *T = NNS->getAsType();
1475 if (!T) break;
1476
1477 // C++0x [temp.expl.spec]p17:
1478 // A member or a member template may be nested within many
1479 // enclosing class templates. In an explicit specialization for
1480 // such a member, the member declaration shall be preceded by a
1481 // template<> for each enclosing class template that is
1482 // explicitly specialized.
Douglas Gregorfe331062010-02-13 05:23:25 +00001483 //
1484 // Following the existing practice of GNU and EDG, we allow a typedef of a
1485 // template specialization type.
John McCall49f4e1c2010-12-10 11:01:00 +00001486 while (const TypedefType *TT = dyn_cast<TypedefType>(T))
1487 T = TT->getDecl()->getUnderlyingType().getTypePtr();
John McCall4b2b02b2009-12-15 02:19:47 +00001488
Mike Stump1eb44332009-09-09 15:08:12 +00001489 if (const TemplateSpecializationType *SpecType
Douglas Gregorfe331062010-02-13 05:23:25 +00001490 = dyn_cast<TemplateSpecializationType>(T)) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001491 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
1492 if (!Template)
1493 continue; // FIXME: should this be an error? probably...
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Ted Kremenek6217b802009-07-29 21:53:49 +00001495 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001496 ClassTemplateSpecializationDecl *SpecDecl
1497 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
1498 // If the nested name specifier refers to an explicit specialization,
1499 // we don't need a template<> header.
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001500 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
1501 ExplicitSpecializationsInSpecifier.push_back(SpecDecl);
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001502 continue;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001503 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001504 }
Mike Stump1eb44332009-09-09 15:08:12 +00001505
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001506 TemplateIdsInSpecifier.push_back(SpecType);
1507 }
1508 }
Mike Stump1eb44332009-09-09 15:08:12 +00001509
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001510 // Reverse the list of template-ids in the scope specifier, so that we can
1511 // more easily match up the template-ids and the template parameter lists.
1512 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001514 SourceLocation FirstTemplateLoc = DeclStartLoc;
1515 if (NumParamLists)
1516 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001518 // Match the template-ids found in the specifier to the template parameter
1519 // lists.
John McCall4e2cbb22010-10-20 05:44:58 +00001520 unsigned ParamIdx = 0, TemplateIdx = 0;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001521 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
John McCall4e2cbb22010-10-20 05:44:58 +00001522 TemplateIdx != NumTemplateIds; ++TemplateIdx) {
1523 const TemplateSpecializationType *TemplateId
1524 = TemplateIdsInSpecifier[TemplateIdx];
Douglas Gregorb88e8882009-07-30 17:40:51 +00001525 bool DependentTemplateId = TemplateId->isDependentType();
John McCall4e2cbb22010-10-20 05:44:58 +00001526
1527 // In friend declarations we can have template-ids which don't
1528 // depend on the corresponding template parameter lists. But
1529 // assume that empty parameter lists are supposed to match this
1530 // template-id.
1531 if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) {
1532 if (!DependentTemplateId ||
1533 !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx]))
1534 continue;
1535 }
1536
1537 if (ParamIdx >= NumParamLists) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001538 // We have a template-id without a corresponding template parameter
1539 // list.
John McCall77e8b112010-04-13 20:37:33 +00001540
1541 // ...which is fine if this is a friend declaration.
1542 if (IsFriend) {
1543 IsExplicitSpecialization = true;
1544 break;
1545 }
1546
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001547 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +00001548 // FIXME: the location information here isn't great.
1549 Diag(SS.getRange().getBegin(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001550 diag::err_template_spec_needs_template_parameters)
John McCall4e2cbb22010-10-20 05:44:58 +00001551 << QualType(TemplateId, 0)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001552 << SS.getRange();
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001553 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001554 } else {
1555 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
1556 << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +00001557 << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001558 IsExplicitSpecialization = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001559 }
1560 return 0;
1561 }
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001563 // Check the template parameter list against its corresponding template-id.
Douglas Gregorb88e8882009-07-30 17:40:51 +00001564 if (DependentTemplateId) {
John McCall31f17ec2010-04-27 00:57:59 +00001565 TemplateParameterList *ExpectedTemplateParams = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00001566
John McCall31f17ec2010-04-27 00:57:59 +00001567 // Are there cases in (e.g.) friends where this won't match?
1568 if (const InjectedClassNameType *Injected
1569 = TemplateId->getAs<InjectedClassNameType>()) {
1570 CXXRecordDecl *Record = Injected->getDecl();
1571 if (ClassTemplatePartialSpecializationDecl *Partial =
1572 dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
1573 ExpectedTemplateParams = Partial->getTemplateParameters();
1574 else
1575 ExpectedTemplateParams = Record->getDescribedClassTemplate()
1576 ->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001577 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001578
John McCall31f17ec2010-04-27 00:57:59 +00001579 if (ExpectedTemplateParams)
John McCall4e2cbb22010-10-20 05:44:58 +00001580 TemplateParameterListsAreEqual(ParamLists[ParamIdx],
John McCall31f17ec2010-04-27 00:57:59 +00001581 ExpectedTemplateParams,
1582 true, TPL_TemplateMatch);
1583
John McCall4e2cbb22010-10-20 05:44:58 +00001584 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1585 TPC_ClassTemplateMember);
1586 } else if (ParamLists[ParamIdx]->size() > 0)
1587 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00001588 diag::err_template_param_list_matches_nontemplate)
1589 << TemplateId
John McCall4e2cbb22010-10-20 05:44:58 +00001590 << ParamLists[ParamIdx]->getSourceRange();
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001591 else
1592 IsExplicitSpecialization = true;
John McCall4e2cbb22010-10-20 05:44:58 +00001593
1594 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001595 }
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001597 // If there were at least as many template-ids as there were template
1598 // parameter lists, then there are no template parameter lists remaining for
1599 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001600 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001601 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001603 // If there were too many template parameter lists, complain about that now.
John McCall4e2cbb22010-10-20 05:44:58 +00001604 if (ParamIdx != NumParamLists - 1) {
1605 while (ParamIdx < NumParamLists - 1) {
1606 bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0;
1607 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001608 isExplicitSpecHeader? diag::warn_template_spec_extra_headers
1609 : diag::err_template_spec_extra_headers)
John McCall4e2cbb22010-10-20 05:44:58 +00001610 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1611 ParamLists[ParamIdx]->getRAngleLoc());
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001612
1613 if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) {
1614 Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(),
1615 diag::note_explicit_template_spec_does_not_need_header)
1616 << ExplicitSpecializationsInSpecifier.back();
1617 ExplicitSpecializationsInSpecifier.pop_back();
1618 }
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001619
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001620 // We have a template parameter list with no corresponding scope, which
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001621 // means that the resulting template declaration can't be instantiated
1622 // properly (we'll end up with dependent nodes when we shouldn't).
1623 if (!isExplicitSpecHeader)
1624 Invalid = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001625
John McCall4e2cbb22010-10-20 05:44:58 +00001626 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001627 }
1628 }
Mike Stump1eb44332009-09-09 15:08:12 +00001629
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001630 // Return the last template parameter list, which corresponds to the
1631 // entity being declared.
1632 return ParamLists[NumParamLists - 1];
1633}
1634
Douglas Gregor7532dc62009-03-30 22:58:21 +00001635QualType Sema::CheckTemplateIdType(TemplateName Name,
1636 SourceLocation TemplateLoc,
John McCalld5532b62009-11-23 01:53:49 +00001637 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001638 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001639 if (!Template) {
1640 // The template name does not resolve to a template, so we just
1641 // build a dependent template-id type.
John McCalld5532b62009-11-23 01:53:49 +00001642 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001643 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001644
Douglas Gregor40808ce2009-03-09 23:48:35 +00001645 // Check that the template argument list is well-formed for this
1646 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001647 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001648 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001649 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001650 return QualType();
1651
Douglas Gregor910f8002010-11-07 23:05:16 +00001652 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001653 "Converted template argument list is too short!");
1654
1655 QualType CanonType;
1656
Douglas Gregorcaddba02009-11-12 18:38:13 +00001657 if (Name.isDependent() ||
1658 TemplateSpecializationType::anyDependentTemplateArguments(
John McCalld5532b62009-11-23 01:53:49 +00001659 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001660 // This class template specialization is a dependent
1661 // type. Therefore, its canonical type is another class template
1662 // specialization type that contains all of the converted
1663 // arguments in canonical form. This ensures that, e.g., A<T> and
1664 // A<T, T> have identical types when A is declared as:
1665 //
1666 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001667 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001668 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001669 Converted.data(),
1670 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001671
Douglas Gregor1275ae02009-07-28 23:00:59 +00001672 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001673 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001674 // In the future, we need to teach getTemplateSpecializationType to only
1675 // build the canonical type and return that to us.
1676 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001677
1678 // This might work out to be a current instantiation, in which
1679 // case the canonical type needs to be the InjectedClassNameType.
1680 //
1681 // TODO: in theory this could be a simple hashtable lookup; most
1682 // changes to CurContext don't change the set of current
1683 // instantiations.
1684 if (isa<ClassTemplateDecl>(Template)) {
1685 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1686 // If we get out to a namespace, we're done.
1687 if (Ctx->isFileContext()) break;
1688
1689 // If this isn't a record, keep looking.
1690 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1691 if (!Record) continue;
1692
1693 // Look for one of the two cases with InjectedClassNameTypes
1694 // and check whether it's the same template.
1695 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1696 !Record->getDescribedClassTemplate())
1697 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001698
John McCall31f17ec2010-04-27 00:57:59 +00001699 // Fetch the injected class name type and check whether its
1700 // injected type is equal to the type we just built.
1701 QualType ICNT = Context.getTypeDeclType(Record);
1702 QualType Injected = cast<InjectedClassNameType>(ICNT)
1703 ->getInjectedSpecializationType();
1704
1705 if (CanonType != Injected->getCanonicalTypeInternal())
1706 continue;
1707
1708 // If so, the canonical type of this TST is the injected
1709 // class name type of the record we just found.
1710 assert(ICNT.isCanonical());
1711 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001712 break;
1713 }
1714 }
Mike Stump1eb44332009-09-09 15:08:12 +00001715 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001716 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001717 // Find the class template specialization declaration that
1718 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001719 void *InsertPos = 0;
1720 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001721 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001722 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001723 if (!Decl) {
1724 // This is the first time we have referenced this class template
1725 // specialization. Create the canonical declaration and add it to
1726 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001727 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001728 ClassTemplate->getTemplatedDecl()->getTagKind(),
1729 ClassTemplate->getDeclContext(),
1730 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001731 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001732 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001733 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001734 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001735 Decl->setLexicalDeclContext(CurContext);
1736 }
1737
1738 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00001739 assert(isa<RecordType>(CanonType) &&
1740 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00001741 }
Mike Stump1eb44332009-09-09 15:08:12 +00001742
Douglas Gregor40808ce2009-03-09 23:48:35 +00001743 // Build the fully-sugared type for this class template
1744 // specialization, which refers back to the class template
1745 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00001746 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001747}
1748
John McCallf312b1e2010-08-26 23:41:50 +00001749TypeResult
Douglas Gregor7532dc62009-03-30 22:58:21 +00001750Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001751 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001752 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00001753 SourceLocation RAngleLoc) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001754 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001755
Douglas Gregor40808ce2009-03-09 23:48:35 +00001756 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00001757 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00001758 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001759
John McCalld5532b62009-11-23 01:53:49 +00001760 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001761 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00001762
1763 if (Result.isNull())
1764 return true;
1765
John McCalla93c9342009-12-07 02:54:59 +00001766 TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result);
John McCall833ca992009-10-29 08:12:44 +00001767 TemplateSpecializationTypeLoc TL
1768 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1769 TL.setTemplateNameLoc(TemplateLoc);
1770 TL.setLAngleLoc(LAngleLoc);
1771 TL.setRAngleLoc(RAngleLoc);
1772 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1773 TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
1774
John McCallb3d87482010-08-24 05:47:05 +00001775 return CreateParsedType(Result, DI);
John McCall6b2becf2009-09-08 17:47:29 +00001776}
John McCallf1bbbb42009-09-04 01:14:41 +00001777
Craig Silverstein45ab4b52010-11-18 08:32:02 +00001778TypeResult Sema::ActOnTagTemplateIdType(CXXScopeSpec &SS,
1779 TypeResult TypeResult,
John McCallf312b1e2010-08-26 23:41:50 +00001780 TagUseKind TUK,
1781 TypeSpecifierType TagSpec,
1782 SourceLocation TagLoc) {
John McCall6b2becf2009-09-08 17:47:29 +00001783 if (TypeResult.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00001784 return ::TypeResult();
John McCallf1bbbb42009-09-04 01:14:41 +00001785
John McCalla93c9342009-12-07 02:54:59 +00001786 TypeSourceInfo *DI;
John McCall833ca992009-10-29 08:12:44 +00001787 QualType Type = GetTypeFromParser(TypeResult.get(), &DI);
John McCallf1bbbb42009-09-04 01:14:41 +00001788
John McCall6b2becf2009-09-08 17:47:29 +00001789 // Verify the tag specifier.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001790 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Mike Stump1eb44332009-09-09 15:08:12 +00001791
John McCall6b2becf2009-09-08 17:47:29 +00001792 if (const RecordType *RT = Type->getAs<RecordType>()) {
1793 RecordDecl *D = RT->getDecl();
1794
1795 IdentifierInfo *Id = D->getIdentifier();
1796 assert(Id && "templated class must have an identifier");
1797
1798 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1799 Diag(TagLoc, diag::err_use_with_wrong_tag)
John McCallc4e70192009-09-11 04:59:25 +00001800 << Type
Douglas Gregor849b2432010-03-31 17:46:05 +00001801 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00001802 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00001803 }
1804 }
1805
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001806 ElaboratedTypeKeyword Keyword
1807 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
1808 QualType ElabType = Context.getElaboratedType(Keyword, /*NNS=*/0, Type);
John McCall6b2becf2009-09-08 17:47:29 +00001809
Craig Silverstein45ab4b52010-11-18 08:32:02 +00001810 TypeSourceInfo *ElabDI = Context.CreateTypeSourceInfo(ElabType);
1811 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(ElabDI->getTypeLoc());
1812 TL.setKeywordLoc(TagLoc);
1813 TL.setQualifierRange(SS.getRange());
1814 TL.getNamedTypeLoc().initializeFullCopy(DI->getTypeLoc());
1815 return CreateParsedType(ElabType, ElabDI);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001816}
1817
John McCall60d7b3a2010-08-24 06:29:42 +00001818ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +00001819 LookupResult &R,
1820 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00001821 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001822 // FIXME: Can we do any checking at this point? I guess we could check the
1823 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00001824 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001825 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001826 // foo<int> could identify a single function unambiguously
1827 // This approach does NOT work, since f<int>(1);
1828 // gets resolved prior to resorting to overload resolution
1829 // i.e., template<class T> void f(double);
1830 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00001831
1832 // These should be filtered out by our callers.
1833 assert(!R.empty() && "empty lookup results when building templateid");
1834 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
1835
1836 NestedNameSpecifier *Qualifier = 0;
1837 SourceRange QualifierRange;
1838 if (SS.isSet()) {
1839 Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
1840 QualifierRange = SS.getRange();
Douglas Gregora9e29aa2009-10-22 07:19:14 +00001841 }
John McCallc373d482010-01-27 01:50:18 +00001842
1843 // We don't want lookup warnings at this point.
1844 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001845
John McCallf7a1a742009-11-24 19:00:30 +00001846 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001847 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
John McCallf7a1a742009-11-24 19:00:30 +00001848 Qualifier, QualifierRange,
Abramo Bagnara25777432010-08-11 22:01:17 +00001849 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001850 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001851 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00001852
1853 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001854}
1855
John McCallf7a1a742009-11-24 19:00:30 +00001856// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00001857ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00001858Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00001859 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00001860 const TemplateArgumentListInfo &TemplateArgs) {
1861 DeclContext *DC;
1862 if (!(DC = computeDeclContext(SS, false)) ||
1863 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00001864 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00001865 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001866
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001867 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00001868 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001869 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
1870 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00001871
John McCallf7a1a742009-11-24 19:00:30 +00001872 if (R.isAmbiguous())
1873 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001874
John McCallf7a1a742009-11-24 19:00:30 +00001875 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001876 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
1877 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001878 return ExprError();
1879 }
1880
1881 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001882 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
1883 << (NestedNameSpecifier*) SS.getScopeRep()
1884 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001885 Diag(Temp->getLocation(), diag::note_referenced_class_template);
1886 return ExprError();
1887 }
1888
1889 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001890}
1891
Douglas Gregorc45c2322009-03-31 00:43:58 +00001892/// \brief Form a dependent template name.
1893///
1894/// This action forms a dependent template name given the template
1895/// name and its (presumably dependent) scope specifier. For
1896/// example, given "MetaFun::template apply", the scope specifier \p
1897/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
1898/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001899TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001900 SourceLocation TemplateKWLoc,
1901 CXXScopeSpec &SS,
1902 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00001903 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001904 bool EnteringContext,
1905 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00001906 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
1907 !getLangOptions().CPlusPlus0x)
1908 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001909 << FixItHint::CreateRemoval(TemplateKWLoc);
1910
Douglas Gregor0707bc52010-01-19 16:01:07 +00001911 DeclContext *LookupCtx = 0;
1912 if (SS.isSet())
1913 LookupCtx = computeDeclContext(SS, EnteringContext);
1914 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00001915 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00001916 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00001917 // C++0x [temp.names]p5:
1918 // If a name prefixed by the keyword template is not the name of
1919 // a template, the program is ill-formed. [Note: the keyword
1920 // template may not be applied to non-template members of class
1921 // templates. -end note ] [ Note: as is the case with the
1922 // typename prefix, the template prefix is allowed in cases
1923 // where it is not strictly necessary; i.e., when the
1924 // nested-name-specifier or the expression on the left of the ->
1925 // or . is not dependent on a template-parameter, or the use
1926 // does not appear in the scope of a template. -end note]
1927 //
1928 // Note: C++03 was more strict here, because it banned the use of
1929 // the "template" keyword prior to a template-name that was not a
1930 // dependent name. C++ DR468 relaxed this requirement (the
1931 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00001932 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001933 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001934 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
1935 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001936 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00001937 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
1938 isa<CXXRecordDecl>(LookupCtx) &&
1939 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00001940 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00001941 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001942 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00001943 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00001944 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00001945 << Name.getSourceRange()
1946 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00001947 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00001948 } else {
1949 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00001950 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00001951 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00001952 }
1953
Mike Stump1eb44332009-09-09 15:08:12 +00001954 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001955 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001956
Douglas Gregor014e88d2009-11-03 23:16:33 +00001957 switch (Name.getKind()) {
1958 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001959 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001960 Name.Identifier));
1961 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001962
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001963 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00001964 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001965 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00001966 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00001967
1968 case UnqualifiedId::IK_LiteralOperatorId:
1969 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
1970
Douglas Gregor014e88d2009-11-03 23:16:33 +00001971 default:
1972 break;
1973 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001974
1975 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00001976 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00001977 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00001978 << Name.getSourceRange()
1979 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00001980 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00001981}
1982
Mike Stump1eb44332009-09-09 15:08:12 +00001983bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00001984 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00001985 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00001986 const TemplateArgument &Arg = AL.getArgument();
1987
Anders Carlsson436b1562009-06-13 00:33:33 +00001988 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00001989 switch(Arg.getKind()) {
1990 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00001991 // C++ [temp.arg.type]p1:
1992 // A template-argument for a template-parameter which is a
1993 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00001994 break;
1995 case TemplateArgument::Template: {
1996 // We have a template type parameter but the template argument
1997 // is a template without any arguments.
1998 SourceRange SR = AL.getSourceRange();
1999 TemplateName Name = Arg.getAsTemplate();
2000 Diag(SR.getBegin(), diag::err_template_missing_args)
2001 << Name << SR;
2002 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2003 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002004
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002005 return true;
2006 }
2007 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002008 // We have a template type parameter but the template argument
2009 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002010 SourceRange SR = AL.getSourceRange();
2011 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002012 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002013
Anders Carlsson436b1562009-06-13 00:33:33 +00002014 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002015 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002016 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002017
John McCalla93c9342009-12-07 02:54:59 +00002018 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002019 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Anders Carlsson436b1562009-06-13 00:33:33 +00002021 // Add the converted template type argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002022 Converted.push_back(
John McCall833ca992009-10-29 08:12:44 +00002023 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00002024 return false;
2025}
2026
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002027/// \brief Substitute template arguments into the default template argument for
2028/// the given template type parameter.
2029///
2030/// \param SemaRef the semantic analysis object for which we are performing
2031/// the substitution.
2032///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002033/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002034/// for.
2035///
2036/// \param TemplateLoc the location of the template name that started the
2037/// template-id we are checking.
2038///
2039/// \param RAngleLoc the location of the right angle bracket ('>') that
2040/// terminates the template-id.
2041///
2042/// \param Param the template template parameter whose default we are
2043/// substituting into.
2044///
2045/// \param Converted the list of template arguments provided for template
2046/// parameters that precede \p Param in the template parameter list.
2047///
2048/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002049static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002050SubstDefaultTemplateArgument(Sema &SemaRef,
2051 TemplateDecl *Template,
2052 SourceLocation TemplateLoc,
2053 SourceLocation RAngleLoc,
2054 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002055 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002056 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002057
2058 // If the argument type is dependent, instantiate it now based
2059 // on the previously-computed template arguments.
2060 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002061 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002062 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002063
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002064 MultiLevelTemplateArgumentList AllTemplateArgs
2065 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2066
2067 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002068 Template, Converted.data(),
2069 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002070 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002071
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002072 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2073 Param->getDefaultArgumentLoc(),
2074 Param->getDeclName());
2075 }
2076
2077 return ArgType;
2078}
2079
2080/// \brief Substitute template arguments into the default template argument for
2081/// the given non-type template parameter.
2082///
2083/// \param SemaRef the semantic analysis object for which we are performing
2084/// the substitution.
2085///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002086/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002087/// for.
2088///
2089/// \param TemplateLoc the location of the template name that started the
2090/// template-id we are checking.
2091///
2092/// \param RAngleLoc the location of the right angle bracket ('>') that
2093/// terminates the template-id.
2094///
Douglas Gregor788cd062009-11-11 01:00:40 +00002095/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002096/// substituting into.
2097///
2098/// \param Converted the list of template arguments provided for template
2099/// parameters that precede \p Param in the template parameter list.
2100///
2101/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002102static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002103SubstDefaultTemplateArgument(Sema &SemaRef,
2104 TemplateDecl *Template,
2105 SourceLocation TemplateLoc,
2106 SourceLocation RAngleLoc,
2107 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002108 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002109 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002110 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002111
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002112 MultiLevelTemplateArgumentList AllTemplateArgs
2113 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002114
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002115 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002116 Template, Converted.data(),
2117 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002118 SourceRange(TemplateLoc, RAngleLoc));
2119
2120 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2121}
2122
Douglas Gregor788cd062009-11-11 01:00:40 +00002123/// \brief Substitute template arguments into the default template argument for
2124/// the given template template parameter.
2125///
2126/// \param SemaRef the semantic analysis object for which we are performing
2127/// the substitution.
2128///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002129/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002130/// for.
2131///
2132/// \param TemplateLoc the location of the template name that started the
2133/// template-id we are checking.
2134///
2135/// \param RAngleLoc the location of the right angle bracket ('>') that
2136/// terminates the template-id.
2137///
2138/// \param Param the template template parameter whose default we are
2139/// substituting into.
2140///
2141/// \param Converted the list of template arguments provided for template
2142/// parameters that precede \p Param in the template parameter list.
2143///
2144/// \returns the substituted template argument, or NULL if an error occurred.
2145static TemplateName
2146SubstDefaultTemplateArgument(Sema &SemaRef,
2147 TemplateDecl *Template,
2148 SourceLocation TemplateLoc,
2149 SourceLocation RAngleLoc,
2150 TemplateTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002151 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002152 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002153 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002154
Douglas Gregor788cd062009-11-11 01:00:40 +00002155 MultiLevelTemplateArgumentList AllTemplateArgs
2156 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002157
Douglas Gregor788cd062009-11-11 01:00:40 +00002158 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002159 Template, Converted.data(),
2160 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002161 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002162
Douglas Gregor788cd062009-11-11 01:00:40 +00002163 return SemaRef.SubstTemplateName(
2164 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002165 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002166 AllTemplateArgs);
2167}
2168
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002169/// \brief If the given template parameter has a default template
2170/// argument, substitute into that default template argument and
2171/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002172TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002173Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2174 SourceLocation TemplateLoc,
2175 SourceLocation RAngleLoc,
2176 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002177 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2178 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002179 if (!TypeParm->hasDefaultArgument())
2180 return TemplateArgumentLoc();
2181
John McCalla93c9342009-12-07 02:54:59 +00002182 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002183 TemplateLoc,
2184 RAngleLoc,
2185 TypeParm,
2186 Converted);
2187 if (DI)
2188 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2189
2190 return TemplateArgumentLoc();
2191 }
2192
2193 if (NonTypeTemplateParmDecl *NonTypeParm
2194 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2195 if (!NonTypeParm->hasDefaultArgument())
2196 return TemplateArgumentLoc();
2197
John McCall60d7b3a2010-08-24 06:29:42 +00002198 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002199 TemplateLoc,
2200 RAngleLoc,
2201 NonTypeParm,
2202 Converted);
2203 if (Arg.isInvalid())
2204 return TemplateArgumentLoc();
2205
2206 Expr *ArgE = Arg.takeAs<Expr>();
2207 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2208 }
2209
2210 TemplateTemplateParmDecl *TempTempParm
2211 = cast<TemplateTemplateParmDecl>(Param);
2212 if (!TempTempParm->hasDefaultArgument())
2213 return TemplateArgumentLoc();
2214
2215 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002216 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002217 RAngleLoc,
2218 TempTempParm,
2219 Converted);
2220 if (TName.isNull())
2221 return TemplateArgumentLoc();
2222
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002223 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002224 TempTempParm->getDefaultArgument().getTemplateQualifierRange(),
2225 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2226}
2227
Douglas Gregore7526412009-11-11 19:31:23 +00002228/// \brief Check that the given template argument corresponds to the given
2229/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002230///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002231/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002232/// checked.
2233///
2234/// \param Arg The template argument.
2235///
2236/// \param Template The template in which the template argument resides.
2237///
2238/// \param TemplateLoc The location of the template name for the template
2239/// whose argument list we're matching.
2240///
2241/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2242/// the template argument list.
2243///
2244/// \param ArgumentPackIndex The index into the argument pack where this
2245/// argument will be placed. Only valid if the parameter is a parameter pack.
2246///
2247/// \param Converted The checked, converted argument will be added to the
2248/// end of this small vector.
2249///
2250/// \param CTAK Describes how we arrived at this particular template argument:
2251/// explicitly written, deduced, etc.
2252///
2253/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002254bool Sema::CheckTemplateArgument(NamedDecl *Param,
2255 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002256 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002257 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002258 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002259 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002260 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002261 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002262 // Check template type parameters.
2263 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002264 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002265
Douglas Gregord9e15302009-11-11 19:41:09 +00002266 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002267 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002268 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002269 // with the template arguments we've seen thus far. But if the
2270 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002271 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002272 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2273 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002274
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002275 if (NTTPType->isDependentType() &&
2276 !isa<TemplateTemplateParmDecl>(Template) &&
2277 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002278 // Do substitution on the type of the non-type template parameter.
2279 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002280 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002281 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002282
2283 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002284 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002285 NTTPType = SubstType(NTTPType,
2286 MultiLevelTemplateArgumentList(TemplateArgs),
2287 NTTP->getLocation(),
2288 NTTP->getDeclName());
2289 // If that worked, check the non-type template parameter type
2290 // for validity.
2291 if (!NTTPType.isNull())
2292 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2293 NTTP->getLocation());
2294 if (NTTPType.isNull())
2295 return true;
2296 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002297
Douglas Gregore7526412009-11-11 19:31:23 +00002298 switch (Arg.getArgument().getKind()) {
2299 case TemplateArgument::Null:
2300 assert(false && "Should never see a NULL template argument here");
2301 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002302
Douglas Gregore7526412009-11-11 19:31:23 +00002303 case TemplateArgument::Expression: {
2304 Expr *E = Arg.getArgument().getAsExpr();
2305 TemplateArgument Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002306 if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK))
Douglas Gregore7526412009-11-11 19:31:23 +00002307 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002308
Douglas Gregor910f8002010-11-07 23:05:16 +00002309 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002310 break;
2311 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002312
Douglas Gregore7526412009-11-11 19:31:23 +00002313 case TemplateArgument::Declaration:
2314 case TemplateArgument::Integral:
2315 // We've already checked this template argument, so just copy
2316 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002317 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002318 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002319
Douglas Gregore7526412009-11-11 19:31:23 +00002320 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002321 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002322 // We were given a template template argument. It may not be ill-formed;
2323 // see below.
2324 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002325 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2326 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002327 // We have a template argument such as \c T::template X, which we
2328 // parsed as a template template argument. However, since we now
2329 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002330 // template name into an expression.
2331
2332 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2333 Arg.getTemplateNameLoc());
2334
John McCallf7a1a742009-11-24 19:00:30 +00002335 Expr *E = DependentScopeDeclRefExpr::Create(Context,
2336 DTN->getQualifier(),
Douglas Gregore7526412009-11-11 19:31:23 +00002337 Arg.getTemplateQualifierRange(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002338 NameInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002339
Douglas Gregora7fc9012011-01-05 18:58:31 +00002340 // If we parsed the template argument as a pack expansion, create a
2341 // pack expansion expression.
2342 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002343 ExprResult Expansion = ActOnPackExpansion(E,
Douglas Gregora7fc9012011-01-05 18:58:31 +00002344 Arg.getTemplateEllipsisLoc());
2345 if (Expansion.isInvalid())
2346 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002347
Douglas Gregora7fc9012011-01-05 18:58:31 +00002348 E = Expansion.get();
2349 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002350
Douglas Gregore7526412009-11-11 19:31:23 +00002351 TemplateArgument Result;
2352 if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
2353 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002354
Douglas Gregor910f8002010-11-07 23:05:16 +00002355 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002356 break;
2357 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002358
Douglas Gregore7526412009-11-11 19:31:23 +00002359 // We have a template argument that actually does refer to a class
2360 // template, template alias, or template template parameter, and
2361 // therefore cannot be a non-type template argument.
2362 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2363 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002364
Douglas Gregore7526412009-11-11 19:31:23 +00002365 Diag(Param->getLocation(), diag::note_template_param_here);
2366 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002367
Douglas Gregore7526412009-11-11 19:31:23 +00002368 case TemplateArgument::Type: {
2369 // We have a non-type template parameter but the template
2370 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002371
Douglas Gregore7526412009-11-11 19:31:23 +00002372 // C++ [temp.arg]p2:
2373 // In a template-argument, an ambiguity between a type-id and
2374 // an expression is resolved to a type-id, regardless of the
2375 // form of the corresponding template-parameter.
2376 //
2377 // We warn specifically about this case, since it can be rather
2378 // confusing for users.
2379 QualType T = Arg.getArgument().getAsType();
2380 SourceRange SR = Arg.getSourceRange();
2381 if (T->isFunctionType())
2382 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2383 else
2384 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2385 Diag(Param->getLocation(), diag::note_template_param_here);
2386 return true;
2387 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002388
Douglas Gregore7526412009-11-11 19:31:23 +00002389 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002390 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002391 break;
2392 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002393
Douglas Gregore7526412009-11-11 19:31:23 +00002394 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002395 }
2396
2397
Douglas Gregore7526412009-11-11 19:31:23 +00002398 // Check template template parameters.
2399 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002400
Douglas Gregore7526412009-11-11 19:31:23 +00002401 // Substitute into the template parameter list of the template
2402 // template parameter, since previously-supplied template arguments
2403 // may appear within the template template parameter.
2404 {
2405 // Set up a template instantiation context.
2406 LocalInstantiationScope Scope(*this);
2407 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002408 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002409 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002410
2411 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002412 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002413 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002414 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002415 MultiLevelTemplateArgumentList(TemplateArgs)));
2416 if (!TempParm)
2417 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002418 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002419
Douglas Gregore7526412009-11-11 19:31:23 +00002420 switch (Arg.getArgument().getKind()) {
2421 case TemplateArgument::Null:
2422 assert(false && "Should never see a NULL template argument here");
2423 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002424
Douglas Gregore7526412009-11-11 19:31:23 +00002425 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002426 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002427 if (CheckTemplateArgument(TempParm, Arg))
2428 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002429
Douglas Gregor910f8002010-11-07 23:05:16 +00002430 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002431 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002432
Douglas Gregore7526412009-11-11 19:31:23 +00002433 case TemplateArgument::Expression:
2434 case TemplateArgument::Type:
2435 // We have a template template parameter but the template
2436 // argument does not refer to a template.
2437 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
2438 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002439
Douglas Gregore7526412009-11-11 19:31:23 +00002440 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002441 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002442 "Declaration argument with template template parameter");
2443 break;
2444 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002445 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002446 "Integral argument with template template parameter");
2447 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002448
Douglas Gregore7526412009-11-11 19:31:23 +00002449 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002450 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002451 break;
2452 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002453
Douglas Gregore7526412009-11-11 19:31:23 +00002454 return false;
2455}
2456
Douglas Gregorc15cb382009-02-09 23:23:08 +00002457/// \brief Check that the given template argument list is well-formed
2458/// for specializing the given template.
2459bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2460 SourceLocation TemplateLoc,
John McCalld5532b62009-11-23 01:53:49 +00002461 const TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002462 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002463 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002464 TemplateParameterList *Params = Template->getTemplateParameters();
2465 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002466 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002467 bool Invalid = false;
2468
John McCalld5532b62009-11-23 01:53:49 +00002469 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2470
Mike Stump1eb44332009-09-09 15:08:12 +00002471 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002472 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002473
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002474 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002475 (NumArgs < Params->getMinRequiredArguments() &&
2476 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002477 // FIXME: point at either the first arg beyond what we can handle,
2478 // or the '>', depending on whether we have too many or too few
2479 // arguments.
2480 SourceRange Range;
2481 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002482 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002483 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2484 << (NumArgs > NumParams)
2485 << (isa<ClassTemplateDecl>(Template)? 0 :
2486 isa<FunctionTemplateDecl>(Template)? 1 :
2487 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2488 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002489 Diag(Template->getLocation(), diag::note_template_decl_here)
2490 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002491 Invalid = true;
2492 }
Mike Stump1eb44332009-09-09 15:08:12 +00002493
2494 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002495 // [...] The type and form of each template-argument specified in
2496 // a template-id shall match the type and form specified for the
2497 // corresponding parameter declared by the template in its
2498 // template-parameter-list.
Douglas Gregor14be16b2010-12-20 16:57:52 +00002499 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2500 TemplateParameterList::iterator Param = Params->begin(),
2501 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002502 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002503 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002504 while (Param != ParamEnd) {
Douglas Gregor16134c62009-07-01 00:28:38 +00002505 if (ArgIdx > NumArgs && PartialTemplateArgs)
2506 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002507
Douglas Gregorf35f8282009-11-11 21:54:23 +00002508 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002509 // If we have an expanded parameter pack, make sure we don't have too
2510 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002511 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002512 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002513 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002514 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2515 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2516 << true
2517 << (isa<ClassTemplateDecl>(Template)? 0 :
2518 isa<FunctionTemplateDecl>(Template)? 1 :
2519 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2520 << Template;
2521 Diag(Template->getLocation(), diag::note_template_decl_here)
2522 << Params->getSourceRange();
2523 return true;
2524 }
2525 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002526
Douglas Gregorf35f8282009-11-11 21:54:23 +00002527 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002528 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2529 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002530 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002531 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002532
Douglas Gregor14be16b2010-12-20 16:57:52 +00002533 if ((*Param)->isTemplateParameterPack()) {
2534 // The template parameter was a template parameter pack, so take the
2535 // deduced argument and place it on the argument pack. Note that we
2536 // stay on the same template parameter so that we can deduce more
2537 // arguments.
2538 ArgumentPack.push_back(Converted.back());
2539 Converted.pop_back();
2540 } else {
2541 // Move to the next template parameter.
2542 ++Param;
2543 }
2544 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002545 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002546 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002547
2548 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002549 // arguments, just break out now and we'll fill in the argument pack below.
2550 if ((*Param)->isTemplateParameterPack())
2551 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002552
Douglas Gregorf35f8282009-11-11 21:54:23 +00002553 // We have a default template argument that we will use.
2554 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002555
Douglas Gregorf35f8282009-11-11 21:54:23 +00002556 // Retrieve the default template argument from the template
2557 // parameter. For each kind of template parameter, we substitute the
2558 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002559 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002560 // the default argument.
2561 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2562 if (!TTP->hasDefaultArgument()) {
2563 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2564 break;
2565 }
2566
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002567 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002568 Template,
2569 TemplateLoc,
2570 RAngleLoc,
2571 TTP,
2572 Converted);
2573 if (!ArgType)
2574 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002575
Douglas Gregorf35f8282009-11-11 21:54:23 +00002576 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2577 ArgType);
2578 } else if (NonTypeTemplateParmDecl *NTTP
2579 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2580 if (!NTTP->hasDefaultArgument()) {
2581 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2582 break;
2583 }
2584
John McCall60d7b3a2010-08-24 06:29:42 +00002585 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002586 TemplateLoc,
2587 RAngleLoc,
2588 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002589 Converted);
2590 if (E.isInvalid())
2591 return true;
2592
2593 Expr *Ex = E.takeAs<Expr>();
2594 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2595 } else {
2596 TemplateTemplateParmDecl *TempParm
2597 = cast<TemplateTemplateParmDecl>(*Param);
2598
2599 if (!TempParm->hasDefaultArgument()) {
2600 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2601 break;
2602 }
2603
2604 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002605 TemplateLoc,
2606 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002607 TempParm,
2608 Converted);
2609 if (Name.isNull())
2610 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002611
2612 Arg = TemplateArgumentLoc(TemplateArgument(Name),
Douglas Gregorf35f8282009-11-11 21:54:23 +00002613 TempParm->getDefaultArgument().getTemplateQualifierRange(),
2614 TempParm->getDefaultArgument().getTemplateNameLoc());
2615 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002616
Douglas Gregorf35f8282009-11-11 21:54:23 +00002617 // Introduce an instantiation record that describes where we are using
2618 // the default template argument.
2619 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002620 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002621 SourceRange(TemplateLoc, RAngleLoc));
2622
Douglas Gregorf35f8282009-11-11 21:54:23 +00002623 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002624 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002625 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00002626 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002627
Douglas Gregor14be16b2010-12-20 16:57:52 +00002628 // Move to the next template parameter and argument.
2629 ++Param;
2630 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002631 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002632
Douglas Gregor14be16b2010-12-20 16:57:52 +00002633 // Form argument packs for each of the parameter packs remaining.
2634 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00002635 // If we're checking a partial list of template arguments, don't fill
2636 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002637
2638 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002639 if (PartialTemplateArgs && ArgumentPack.empty()) {
2640 Converted.push_back(TemplateArgument());
Douglas Gregor203e6a32011-01-11 23:09:57 +00002641 } else if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00002642 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00002643 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002644 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2645 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00002646 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00002647 ArgumentPack.clear();
2648 }
2649 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002650
Douglas Gregor14be16b2010-12-20 16:57:52 +00002651 ++Param;
2652 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002653
Douglas Gregorc15cb382009-02-09 23:23:08 +00002654 return Invalid;
2655}
2656
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002657namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002658 class UnnamedLocalNoLinkageFinder
2659 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002660 {
2661 Sema &S;
2662 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002663
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002664 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002665
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002666 public:
2667 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
2668
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002669 bool Visit(QualType T) {
2670 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002671 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002672
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002673#define TYPE(Class, Parent) \
2674 bool Visit##Class##Type(const Class##Type *);
2675#define ABSTRACT_TYPE(Class, Parent) \
2676 bool Visit##Class##Type(const Class##Type *) { return false; }
2677#define NON_CANONICAL_TYPE(Class, Parent) \
2678 bool Visit##Class##Type(const Class##Type *) { return false; }
2679#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002680
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002681 bool VisitTagDecl(const TagDecl *Tag);
2682 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
2683 };
2684}
2685
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002686bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002687 return false;
2688}
2689
2690bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
2691 return Visit(T->getElementType());
2692}
2693
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002694bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002695 return Visit(T->getPointeeType());
2696}
2697
2698bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002699 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002700 return Visit(T->getPointeeType());
2701}
2702
2703bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002704 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002705 return Visit(T->getPointeeType());
2706}
2707
2708bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002709 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002710 return Visit(T->getPointeeType());
2711}
2712
2713bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002714 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002715 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
2716}
2717
2718bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002719 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002720 return Visit(T->getElementType());
2721}
2722
2723bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002724 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002725 return Visit(T->getElementType());
2726}
2727
2728bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002729 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002730 return Visit(T->getElementType());
2731}
2732
2733bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002734 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002735 return Visit(T->getElementType());
2736}
2737
2738bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002739 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002740 return Visit(T->getElementType());
2741}
2742
2743bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
2744 return Visit(T->getElementType());
2745}
2746
2747bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
2748 return Visit(T->getElementType());
2749}
2750
2751bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
2752 const FunctionProtoType* T) {
2753 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002754 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002755 A != AEnd; ++A) {
2756 if (Visit(*A))
2757 return true;
2758 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002759
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002760 return Visit(T->getResultType());
2761}
2762
2763bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
2764 const FunctionNoProtoType* T) {
2765 return Visit(T->getResultType());
2766}
2767
2768bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
2769 const UnresolvedUsingType*) {
2770 return false;
2771}
2772
2773bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
2774 return false;
2775}
2776
2777bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
2778 return Visit(T->getUnderlyingType());
2779}
2780
2781bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
2782 return false;
2783}
2784
Richard Smith34b41d92011-02-20 03:19:35 +00002785bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
2786 return Visit(T->getDeducedType());
2787}
2788
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002789bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
2790 return VisitTagDecl(T->getDecl());
2791}
2792
2793bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
2794 return VisitTagDecl(T->getDecl());
2795}
2796
2797bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
2798 const TemplateTypeParmType*) {
2799 return false;
2800}
2801
Douglas Gregorc3069d62011-01-14 02:55:32 +00002802bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
2803 const SubstTemplateTypeParmPackType *) {
2804 return false;
2805}
2806
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002807bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
2808 const TemplateSpecializationType*) {
2809 return false;
2810}
2811
2812bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
2813 const InjectedClassNameType* T) {
2814 return VisitTagDecl(T->getDecl());
2815}
2816
2817bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
2818 const DependentNameType* T) {
2819 return VisitNestedNameSpecifier(T->getQualifier());
2820}
2821
2822bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
2823 const DependentTemplateSpecializationType* T) {
2824 return VisitNestedNameSpecifier(T->getQualifier());
2825}
2826
Douglas Gregor7536dd52010-12-20 02:24:11 +00002827bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
2828 const PackExpansionType* T) {
2829 return Visit(T->getPattern());
2830}
2831
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002832bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
2833 return false;
2834}
2835
2836bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
2837 const ObjCInterfaceType *) {
2838 return false;
2839}
2840
2841bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
2842 const ObjCObjectPointerType *) {
2843 return false;
2844}
2845
2846bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
2847 if (Tag->getDeclContext()->isFunctionOrMethod()) {
2848 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
2849 << S.Context.getTypeDeclType(Tag) << SR;
2850 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002851 }
2852
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002853 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) {
2854 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
2855 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
2856 return true;
2857 }
2858
2859 return false;
2860}
2861
2862bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
2863 NestedNameSpecifier *NNS) {
2864 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
2865 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002866
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002867 switch (NNS->getKind()) {
2868 case NestedNameSpecifier::Identifier:
2869 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00002870 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002871 case NestedNameSpecifier::Global:
2872 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002873
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002874 case NestedNameSpecifier::TypeSpec:
2875 case NestedNameSpecifier::TypeSpecWithTemplate:
2876 return Visit(QualType(NNS->getAsType(), 0));
2877 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00002878 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002879}
2880
2881
Douglas Gregorc15cb382009-02-09 23:23:08 +00002882/// \brief Check a template argument against its corresponding
2883/// template type parameter.
2884///
2885/// This routine implements the semantics of C++ [temp.arg.type]. It
2886/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00002887bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00002888 TypeSourceInfo *ArgInfo) {
2889 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00002890 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00002891 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00002892
2893 if (Arg->isVariablyModifiedType()) {
2894 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00002895 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00002896 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002897 }
2898
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002899 // C++03 [temp.arg.type]p2:
2900 // A local type, a type with no linkage, an unnamed type or a type
2901 // compounded from any of these types shall not be used as a
2902 // template-argument for a template type-parameter.
2903 //
2904 // C++0x allows these, and even in C++03 we allow them as an extension with
2905 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00002906 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002907 UnnamedLocalNoLinkageFinder Finder(*this, SR);
2908 (void)Finder.Visit(Context.getCanonicalType(Arg));
2909 }
2910
Douglas Gregorc15cb382009-02-09 23:23:08 +00002911 return false;
2912}
2913
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002914/// \brief Checks whether the given template argument is the address
2915/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002916static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00002917CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
2918 NonTypeTemplateParmDecl *Param,
2919 QualType ParamType,
2920 Expr *ArgIn,
2921 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002922 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00002923 Expr *Arg = ArgIn;
2924 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002925
2926 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00002927 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002928 Arg = Cast->getSubExpr();
2929
2930 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00002931 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002932 // A template-argument for a non-type, non-template
2933 // template-parameter shall be one of: [...]
2934 //
2935 // -- the address of an object or function with external
2936 // linkage, including function templates and function
2937 // template-ids but excluding non-static class members,
2938 // expressed as & id-expression where the & is optional if
2939 // the name refers to a function or array, or if the
2940 // corresponding template-parameter is a reference; or
2941 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002942
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002943 // In C++98/03 mode, give an extension warning on any extra parentheses.
2944 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
2945 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002946 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002947 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00002948 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002949 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002950 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002951 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002952 }
2953
2954 Arg = Parens->getSubExpr();
2955 }
2956
Douglas Gregorb7a09262010-04-01 18:32:35 +00002957 bool AddressTaken = false;
2958 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002959 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00002960 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002961 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00002962 AddressTaken = true;
2963 AddrOpLoc = UnOp->getOperatorLoc();
2964 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002965 } else
2966 DRE = dyn_cast<DeclRefExpr>(Arg);
2967
Douglas Gregorb7a09262010-04-01 18:32:35 +00002968 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00002969 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
2970 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00002971 S.Diag(Param->getLocation(), diag::note_template_param_here);
2972 return true;
2973 }
Chandler Carruth038cc392010-01-31 10:01:20 +00002974
2975 // Stop checking the precise nature of the argument if it is value dependent,
2976 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00002977 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00002978 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00002979 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00002980 }
Chandler Carruth038cc392010-01-31 10:01:20 +00002981
Douglas Gregorb7a09262010-04-01 18:32:35 +00002982 if (!isa<ValueDecl>(DRE->getDecl())) {
2983 S.Diag(Arg->getSourceRange().getBegin(),
2984 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002985 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00002986 S.Diag(Param->getLocation(), diag::note_template_param_here);
2987 return true;
2988 }
2989
2990 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002991
2992 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00002993 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
2994 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002995 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00002996 S.Diag(Param->getLocation(), diag::note_template_param_here);
2997 return true;
2998 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002999
3000 // Cannot refer to non-static member functions
3001 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003002 if (!Method->isStatic()) {
3003 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003004 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003005 S.Diag(Param->getLocation(), diag::note_template_param_here);
3006 return true;
3007 }
Mike Stump1eb44332009-09-09 15:08:12 +00003008
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003009 // Functions must have external linkage.
3010 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003011 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003012 S.Diag(Arg->getSourceRange().getBegin(),
3013 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003014 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003015 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003016 << true;
3017 return true;
3018 }
3019
3020 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003021 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003022
Douglas Gregorb7a09262010-04-01 18:32:35 +00003023 // If the template parameter has pointer type, the function decays.
3024 if (ParamType->isPointerType() && !AddressTaken)
3025 ArgType = S.Context.getPointerType(Func->getType());
3026 else if (AddressTaken && ParamType->isReferenceType()) {
3027 // If we originally had an address-of operator, but the
3028 // parameter has reference type, complain and (if things look
3029 // like they will work) drop the address-of operator.
3030 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3031 ParamType.getNonReferenceType())) {
3032 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3033 << ParamType;
3034 S.Diag(Param->getLocation(), diag::note_template_param_here);
3035 return true;
3036 }
3037
3038 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3039 << ParamType
3040 << FixItHint::CreateRemoval(AddrOpLoc);
3041 S.Diag(Param->getLocation(), diag::note_template_param_here);
3042
3043 ArgType = Func->getType();
3044 }
3045 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003046 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003047 S.Diag(Arg->getSourceRange().getBegin(),
3048 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003049 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003050 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003051 << true;
3052 return true;
3053 }
3054
Douglas Gregorb7a09262010-04-01 18:32:35 +00003055 // A value of reference type is not an object.
3056 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003057 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003058 diag::err_template_arg_reference_var)
3059 << Var->getType() << Arg->getSourceRange();
3060 S.Diag(Param->getLocation(), diag::note_template_param_here);
3061 return true;
3062 }
3063
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003064 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003065 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003066
3067 // If the template parameter has pointer type, we must have taken
3068 // the address of this object.
3069 if (ParamType->isReferenceType()) {
3070 if (AddressTaken) {
3071 // If we originally had an address-of operator, but the
3072 // parameter has reference type, complain and (if things look
3073 // like they will work) drop the address-of operator.
3074 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3075 ParamType.getNonReferenceType())) {
3076 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3077 << ParamType;
3078 S.Diag(Param->getLocation(), diag::note_template_param_here);
3079 return true;
3080 }
3081
3082 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3083 << ParamType
3084 << FixItHint::CreateRemoval(AddrOpLoc);
3085 S.Diag(Param->getLocation(), diag::note_template_param_here);
3086
3087 ArgType = Var->getType();
3088 }
3089 } else if (!AddressTaken && ParamType->isPointerType()) {
3090 if (Var->getType()->isArrayType()) {
3091 // Array-to-pointer decay.
3092 ArgType = S.Context.getArrayDecayedType(Var->getType());
3093 } else {
3094 // If the template parameter has pointer type but the address of
3095 // this object was not taken, complain and (possibly) recover by
3096 // taking the address of the entity.
3097 ArgType = S.Context.getPointerType(Var->getType());
3098 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3099 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3100 << ParamType;
3101 S.Diag(Param->getLocation(), diag::note_template_param_here);
3102 return true;
3103 }
3104
3105 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3106 << ParamType
3107 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3108
3109 S.Diag(Param->getLocation(), diag::note_template_param_here);
3110 }
3111 }
3112 } else {
3113 // We found something else, but we don't know specifically what it is.
3114 S.Diag(Arg->getSourceRange().getBegin(),
3115 diag::err_template_arg_not_object_or_func)
3116 << Arg->getSourceRange();
3117 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3118 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003119 }
Mike Stump1eb44332009-09-09 15:08:12 +00003120
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003121 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003122 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003123 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003124 // For pointer-to-object types, qualification conversions are
3125 // permitted.
3126 } else {
3127 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3128 if (!ParamRef->getPointeeType()->isFunctionType()) {
3129 // C++ [temp.arg.nontype]p5b3:
3130 // For a non-type template-parameter of type reference to
3131 // object, no conversions apply. The type referred to by the
3132 // reference may be more cv-qualified than the (otherwise
3133 // identical) type of the template- argument. The
3134 // template-parameter is bound directly to the
3135 // template-argument, which shall be an lvalue.
3136
3137 // FIXME: Other qualifiers?
3138 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3139 unsigned ArgQuals = ArgType.getCVRQualifiers();
3140
3141 if ((ParamQuals | ArgQuals) != ParamQuals) {
3142 S.Diag(Arg->getSourceRange().getBegin(),
3143 diag::err_template_arg_ref_bind_ignores_quals)
3144 << ParamType << Arg->getType()
3145 << Arg->getSourceRange();
3146 S.Diag(Param->getLocation(), diag::note_template_param_here);
3147 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003148 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003149 }
3150 }
3151
3152 // At this point, the template argument refers to an object or
3153 // function with external linkage. We now need to check whether the
3154 // argument and parameter types are compatible.
3155 if (!S.Context.hasSameUnqualifiedType(ArgType,
3156 ParamType.getNonReferenceType())) {
3157 // We can't perform this conversion or binding.
3158 if (ParamType->isReferenceType())
3159 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3160 << ParamType << Arg->getType() << Arg->getSourceRange();
3161 else
3162 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3163 << Arg->getType() << ParamType << Arg->getSourceRange();
3164 S.Diag(Param->getLocation(), diag::note_template_param_here);
3165 return true;
3166 }
3167 }
3168
3169 // Create the template argument.
3170 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003171 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003172 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003173}
3174
3175/// \brief Checks whether the given template argument is a pointer to
3176/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003177bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003178 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003179 bool Invalid = false;
3180
3181 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003182 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003183 Arg = Cast->getSubExpr();
3184
3185 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003186 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003187 // A template-argument for a non-type, non-template
3188 // template-parameter shall be one of: [...]
3189 //
3190 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003191 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003192
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003193 // In C++98/03 mode, give an extension warning on any extra parentheses.
3194 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3195 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003196 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003197 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003198 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003199 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003200 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003201 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003202 }
3203
3204 Arg = Parens->getSubExpr();
3205 }
3206
Douglas Gregorcaddba02009-11-12 18:38:13 +00003207 // A pointer-to-member constant written &Class::member.
3208 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003209 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003210 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3211 if (DRE && !DRE->getQualifier())
3212 DRE = 0;
3213 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003214 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003215 // A constant of pointer-to-member type.
3216 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3217 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3218 if (VD->getType()->isMemberPointerType()) {
3219 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003220 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003221 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3222 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003223 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003224 else
3225 Converted = TemplateArgument(VD->getCanonicalDecl());
3226 return Invalid;
3227 }
3228 }
3229 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003230
Douglas Gregorcaddba02009-11-12 18:38:13 +00003231 DRE = 0;
3232 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003233
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003234 if (!DRE)
3235 return Diag(Arg->getSourceRange().getBegin(),
3236 diag::err_template_arg_not_pointer_to_member_form)
3237 << Arg->getSourceRange();
3238
3239 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3240 assert((isa<FieldDecl>(DRE->getDecl()) ||
3241 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3242 "Only non-static member pointers can make it here");
3243
3244 // Okay: this is the address of a non-static member, and therefore
3245 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003246 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003247 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003248 else
3249 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003250 return Invalid;
3251 }
3252
3253 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003254 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003255 diag::err_template_arg_not_pointer_to_member_form)
3256 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003257 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003258 diag::note_template_arg_refers_here);
3259 return true;
3260}
3261
Douglas Gregorc15cb382009-02-09 23:23:08 +00003262/// \brief Check a template argument against its corresponding
3263/// non-type template parameter.
3264///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003265/// This routine implements the semantics of C++ [temp.arg.nontype].
3266/// It returns true if an error occurred, and false otherwise. \p
3267/// InstantiatedParamType is the type of the non-type template
3268/// parameter after it has been instantiated.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003269///
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003270/// If no error was detected, Converted receives the converted template argument.
Douglas Gregorc15cb382009-02-09 23:23:08 +00003271bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
Mike Stump1eb44332009-09-09 15:08:12 +00003272 QualType InstantiatedParamType, Expr *&Arg,
Douglas Gregor02024a92010-03-28 02:42:43 +00003273 TemplateArgument &Converted,
3274 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003275 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3276
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003277 // If either the parameter has a dependent type or the argument is
3278 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003279 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3280 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003281 Converted = TemplateArgument(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003282 return false;
Douglas Gregor40808ce2009-03-09 23:48:35 +00003283 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003284
3285 // C++ [temp.arg.nontype]p5:
3286 // The following conversions are performed on each expression used
3287 // as a non-type template-argument. If a non-type
3288 // template-argument cannot be converted to the type of the
3289 // corresponding template-parameter then the program is
3290 // ill-formed.
3291 //
3292 // -- for a non-type template-parameter of integral or
3293 // enumeration type, integral promotions (4.5) and integral
3294 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003295 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003296 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003297 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003298 // C++ [temp.arg.nontype]p1:
3299 // A template-argument for a non-type, non-template
3300 // template-parameter shall be one of:
3301 //
3302 // -- an integral constant-expression of integral or enumeration
3303 // type; or
3304 // -- the name of a non-type template-parameter; or
3305 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003306 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003307 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003308 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003309 diag::err_template_arg_not_integral_or_enumeral)
3310 << ArgType << Arg->getSourceRange();
3311 Diag(Param->getLocation(), diag::note_template_param_here);
3312 return true;
3313 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003314 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003315 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3316 << ArgType << Arg->getSourceRange();
3317 return true;
3318 }
3319
Douglas Gregor02024a92010-03-28 02:42:43 +00003320 // From here on out, all we care about are the unqualified forms
3321 // of the parameter and argument types.
3322 ParamType = ParamType.getUnqualifiedType();
3323 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003324
3325 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003326 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003327 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003328 } else if (CTAK == CTAK_Deduced) {
3329 // C++ [temp.deduct.type]p17:
3330 // If, in the declaration of a function template with a non-type
3331 // template-parameter, the non-type template- parameter is used
3332 // in an expression in the function parameter-list and, if the
3333 // corresponding template-argument is deduced, the
3334 // template-argument type shall match the type of the
3335 // template-parameter exactly, except that a template-argument
3336 // deduced from an array bound may be of any integral type.
3337 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3338 << ArgType << ParamType;
3339 Diag(Param->getLocation(), diag::note_template_param_here);
John McCalldaa8e4e2010-11-15 09:13:47 +00003340 return true;
3341 } else if (ParamType->isBooleanType()) {
3342 // This is an integral-to-boolean conversion.
3343 ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003344 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3345 !ParamType->isEnumeralType()) {
3346 // This is an integral promotion or conversion.
John McCall2de56d12010-08-25 11:45:40 +00003347 ImpCastExprToType(Arg, ParamType, CK_IntegralCast);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003348 } else {
3349 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003350 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003351 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003352 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003353 Diag(Param->getLocation(), diag::note_template_param_here);
3354 return true;
3355 }
3356
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003357 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003358 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003359 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003360
3361 if (!Arg->isValueDependent()) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003362 llvm::APSInt OldValue = Value;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003363
3364 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003365 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003366 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003367 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003368 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003369 Value.setIsSigned(IntegerType->isSignedIntegerType());
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003370
3371 // Complain if an unsigned parameter received a negative value.
3372 if (IntegerType->isUnsignedIntegerType()
3373 && (OldValue.isSigned() && OldValue.isNegative())) {
3374 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3375 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3376 << Arg->getSourceRange();
3377 Diag(Param->getLocation(), diag::note_template_param_here);
3378 }
3379
3380 // Complain if we overflowed the template parameter's type.
3381 unsigned RequiredBits;
3382 if (IntegerType->isUnsignedIntegerType())
3383 RequiredBits = OldValue.getActiveBits();
3384 else if (OldValue.isUnsigned())
3385 RequiredBits = OldValue.getActiveBits() + 1;
3386 else
3387 RequiredBits = OldValue.getMinSignedBits();
3388 if (RequiredBits > AllowedBits) {
3389 Diag(Arg->getSourceRange().getBegin(),
3390 diag::warn_template_arg_too_large)
3391 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3392 << Arg->getSourceRange();
3393 Diag(Param->getLocation(), diag::note_template_param_here);
3394 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003395 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003396
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003397 // Add the value of this argument to the list of converted
3398 // arguments. We use the bitwidth and signedness of the template
3399 // parameter.
3400 if (Arg->isValueDependent()) {
3401 // The argument is value-dependent. Create a new
3402 // TemplateArgument with the converted expression.
3403 Converted = TemplateArgument(Arg);
3404 return false;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003405 }
3406
John McCall833ca992009-10-29 08:12:44 +00003407 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003408 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003409 : IntegerType);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003410 return false;
3411 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003412
John McCall6bb80172010-03-30 21:47:33 +00003413 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3414
Douglas Gregorb7a09262010-04-01 18:32:35 +00003415 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3416 // from a template argument of type std::nullptr_t to a non-type
3417 // template parameter of type pointer to object, pointer to
3418 // function, or pointer-to-member, respectively.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003419 if (ArgType->isNullPtrType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003420 (ParamType->isPointerType() || ParamType->isMemberPointerType())) {
3421 Converted = TemplateArgument((NamedDecl *)0);
3422 return false;
3423 }
3424
Douglas Gregorb86b0572009-02-11 01:18:59 +00003425 // Handle pointer-to-function, reference-to-function, and
3426 // pointer-to-member-function all in (roughly) the same way.
3427 if (// -- For a non-type template-parameter of type pointer to
3428 // function, only the function-to-pointer conversion (4.3) is
3429 // applied. If the template-argument represents a set of
3430 // overloaded functions (or a pointer to such), the matching
3431 // function is selected from the set (13.4).
3432 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003433 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003434 // -- For a non-type template-parameter of type reference to
3435 // function, no conversions apply. If the template-argument
3436 // represents a set of overloaded functions, the matching
3437 // function is selected from the set (13.4).
3438 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003439 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003440 // -- For a non-type template-parameter of type pointer to
3441 // member function, no conversions apply. If the
3442 // template-argument represents a set of overloaded member
3443 // functions, the matching member function is selected from
3444 // the set (13.4).
3445 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003446 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003447 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003448
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003449 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003450 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003451 true,
3452 FoundResult)) {
3453 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3454 return true;
3455
3456 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3457 ArgType = Arg->getType();
3458 } else
Douglas Gregor48f3bb92009-02-18 21:56:37 +00003459 return true;
Douglas Gregora35284b2009-02-11 00:19:33 +00003460 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003461
Douglas Gregorb7a09262010-04-01 18:32:35 +00003462 if (!ParamType->isMemberPointerType())
3463 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003464 ParamType,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003465 Arg, Converted);
3466
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003467 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3468 false)) {
John McCall2de56d12010-08-25 11:45:40 +00003469 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregorb7a09262010-04-01 18:32:35 +00003470 } else if (!Context.hasSameUnqualifiedType(ArgType,
3471 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003472 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003473 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003474 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003475 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003476 Diag(Param->getLocation(), diag::note_template_param_here);
3477 return true;
3478 }
Mike Stump1eb44332009-09-09 15:08:12 +00003479
Douglas Gregorb7a09262010-04-01 18:32:35 +00003480 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregora35284b2009-02-11 00:19:33 +00003481 }
3482
Chris Lattnerfe90de72009-02-20 21:37:53 +00003483 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003484 // -- for a non-type template-parameter of type pointer to
3485 // object, qualification conversions (4.4) and the
3486 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003487 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003488 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003489 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003490
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003491 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003492 ParamType,
3493 Arg, Converted);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003494 }
Mike Stump1eb44332009-09-09 15:08:12 +00003495
Ted Kremenek6217b802009-07-29 21:53:49 +00003496 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003497 // -- For a non-type template-parameter of type reference to
3498 // object, no conversions apply. The type referred to by the
3499 // reference may be more cv-qualified than the (otherwise
3500 // identical) type of the template-argument. The
3501 // template-parameter is bound directly to the
3502 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003503 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003504 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003505
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003506 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003507 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3508 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003509 true,
3510 FoundResult)) {
3511 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3512 return true;
3513
3514 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3515 ArgType = Arg->getType();
3516 } else
Douglas Gregorb7a09262010-04-01 18:32:35 +00003517 return true;
Douglas Gregorb86b0572009-02-11 01:18:59 +00003518 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003519
3520 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003521 ParamType,
3522 Arg, Converted);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003523 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003524
3525 // -- For a non-type template-parameter of type pointer to data
3526 // member, qualification conversions (4.4) are applied.
3527 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3528
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003529 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003530 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003531 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John McCall2de56d12010-08-25 11:45:40 +00003532 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregor658bbb52009-02-11 16:16:59 +00003533 } else {
3534 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003535 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003536 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003537 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003538 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00003539 return true;
Douglas Gregor658bbb52009-02-11 16:16:59 +00003540 }
3541
Douglas Gregorcaddba02009-11-12 18:38:13 +00003542 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003543}
3544
3545/// \brief Check a template argument against its corresponding
3546/// template template parameter.
3547///
3548/// This routine implements the semantics of C++ [temp.arg.template].
3549/// It returns true if an error occurred, and false otherwise.
3550bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003551 const TemplateArgumentLoc &Arg) {
3552 TemplateName Name = Arg.getArgument().getAsTemplate();
3553 TemplateDecl *Template = Name.getAsTemplateDecl();
3554 if (!Template) {
3555 // Any dependent template name is fine.
3556 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3557 return false;
3558 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003559
3560 // C++ [temp.arg.template]p1:
3561 // A template-argument for a template template-parameter shall be
3562 // the name of a class template, expressed as id-expression. Only
3563 // primary class templates are considered when matching the
3564 // template template argument with the corresponding parameter;
3565 // partial specializations are not considered even if their
3566 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003567 //
3568 // Note that we also allow template template parameters here, which
3569 // will happen when we are dealing with, e.g., class template
3570 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003571 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003572 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003573 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003574 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003575 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003576 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003577 << Template;
3578 }
3579
3580 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
3581 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003582 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003583 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00003584 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00003585}
3586
Douglas Gregor02024a92010-03-28 02:42:43 +00003587/// \brief Given a non-type template argument that refers to a
3588/// declaration and the type of its corresponding non-type template
3589/// parameter, produce an expression that properly refers to that
3590/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003591ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003592Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
3593 QualType ParamType,
3594 SourceLocation Loc) {
3595 assert(Arg.getKind() == TemplateArgument::Declaration &&
3596 "Only declaration template arguments permitted here");
3597 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
3598
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003599 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00003600 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
3601 // If the value is a class member, we might have a pointer-to-member.
3602 // Determine whether the non-type template template parameter is of
3603 // pointer-to-member type. If so, we need to build an appropriate
3604 // expression for a pointer-to-member, since a "normal" DeclRefExpr
3605 // would refer to the member itself.
3606 if (ParamType->isMemberPointerType()) {
3607 QualType ClassType
3608 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
3609 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00003610 = NestedNameSpecifier::Create(Context, 0, false,
3611 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00003612 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00003613 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00003614
3615 // The actual value-ness of this is unimportant, but for
3616 // internal consistency's sake, references to instance methods
3617 // are r-values.
3618 ExprValueKind VK = VK_LValue;
3619 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
3620 VK = VK_RValue;
3621
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003622 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00003623 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00003624 VK,
John McCallf89e55a2010-11-18 06:31:45 +00003625 Loc,
3626 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00003627 if (RefExpr.isInvalid())
3628 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003629
John McCall2de56d12010-08-25 11:45:40 +00003630 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003631
Douglas Gregorc0c83002010-04-30 21:46:38 +00003632 // We might need to perform a trailing qualification conversion, since
3633 // the element type on the parameter could be more qualified than the
3634 // element type in the expression we constructed.
3635 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003636 ParamType.getUnqualifiedType(), false)) {
Douglas Gregorc0c83002010-04-30 21:46:38 +00003637 Expr *RefE = RefExpr.takeAs<Expr>();
John McCall2de56d12010-08-25 11:45:40 +00003638 ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CK_NoOp);
Douglas Gregorc0c83002010-04-30 21:46:38 +00003639 RefExpr = Owned(RefE);
3640 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003641
Douglas Gregor02024a92010-03-28 02:42:43 +00003642 assert(!RefExpr.isInvalid() &&
3643 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00003644 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00003645 return move(RefExpr);
3646 }
3647 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003648
Douglas Gregor02024a92010-03-28 02:42:43 +00003649 QualType T = VD->getType().getNonReferenceType();
3650 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003651 // When the non-type template parameter is a pointer, take the
3652 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00003653 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003654 if (RefExpr.isInvalid())
3655 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003656
3657 if (T->isFunctionType() || T->isArrayType()) {
3658 // Decay functions and arrays.
3659 Expr *RefE = (Expr *)RefExpr.get();
3660 DefaultFunctionArrayConversion(RefE);
3661 if (RefE != RefExpr.get()) {
3662 RefExpr.release();
3663 RefExpr = Owned(RefE);
3664 }
3665
3666 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00003667 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003668
Douglas Gregorb7a09262010-04-01 18:32:35 +00003669 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00003670 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00003671 }
3672
John McCallf89e55a2010-11-18 06:31:45 +00003673 ExprValueKind VK = VK_RValue;
3674
Douglas Gregor02024a92010-03-28 02:42:43 +00003675 // If the non-type template parameter has reference type, qualify the
3676 // resulting declaration reference with the extra qualifiers on the
3677 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00003678 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
3679 VK = VK_LValue;
3680 T = Context.getQualifiedType(T,
3681 TargetRef->getPointeeType().getQualifiers());
3682 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003683
John McCallf89e55a2010-11-18 06:31:45 +00003684 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003685}
3686
3687/// \brief Construct a new expression that refers to the given
3688/// integral template argument with the given source-location
3689/// information.
3690///
3691/// This routine takes care of the mapping from an integral template
3692/// argument (which may have any integral type) to the appropriate
3693/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003694ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003695Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
3696 SourceLocation Loc) {
3697 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00003698 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00003699 QualType T = Arg.getIntegralType();
3700 if (T->isCharType() || T->isWideCharType())
3701 return Owned(new (Context) CharacterLiteral(
3702 Arg.getAsIntegral()->getZExtValue(),
3703 T->isWideCharType(),
3704 T,
3705 Loc));
3706 if (T->isBooleanType())
3707 return Owned(new (Context) CXXBoolLiteralExpr(
3708 Arg.getAsIntegral()->getBoolValue(),
3709 T,
3710 Loc));
3711
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003712 QualType BT;
3713 if (const EnumType *ET = T->getAs<EnumType>())
3714 BT = ET->getDecl()->getPromotionType();
3715 else
3716 BT = T;
3717
3718 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00003719 if (T->isEnumeralType()) {
3720 // FIXME: This is a hack. We need a better way to handle substituted
3721 // non-type template parameters.
3722 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast,
3723 E, 0,
3724 Context.getTrivialTypeSourceInfo(T, Loc),
3725 Loc, Loc);
3726 }
3727
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003728 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00003729}
3730
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003731/// \brief Match two template parameters within template parameter lists.
3732static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
3733 bool Complain,
3734 Sema::TemplateParameterListEqualKind Kind,
3735 SourceLocation TemplateArgLoc) {
3736 // Check the actual kind (type, non-type, template).
3737 if (Old->getKind() != New->getKind()) {
3738 if (Complain) {
3739 unsigned NextDiag = diag::err_template_param_different_kind;
3740 if (TemplateArgLoc.isValid()) {
3741 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3742 NextDiag = diag::note_template_param_different_kind;
3743 }
3744 S.Diag(New->getLocation(), NextDiag)
3745 << (Kind != Sema::TPL_TemplateMatch);
3746 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
3747 << (Kind != Sema::TPL_TemplateMatch);
3748 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003749
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003750 return false;
3751 }
3752
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003753 // Check that both are parameter packs are neither are parameter packs.
3754 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00003755 // template template parameter, the template template parameter can have
3756 // a parameter pack where the template template argument does not.
3757 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
3758 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3759 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003760 if (Complain) {
3761 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
3762 if (TemplateArgLoc.isValid()) {
3763 S.Diag(TemplateArgLoc,
3764 diag::err_template_arg_template_params_mismatch);
3765 NextDiag = diag::note_template_parameter_pack_non_pack;
3766 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003767
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003768 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
3769 : isa<NonTypeTemplateParmDecl>(New)? 1
3770 : 2;
3771 S.Diag(New->getLocation(), NextDiag)
3772 << ParamKind << New->isParameterPack();
3773 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
3774 << ParamKind << Old->isParameterPack();
3775 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003776
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003777 return false;
3778 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003779
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003780 // For non-type template parameters, check the type of the parameter.
3781 if (NonTypeTemplateParmDecl *OldNTTP
3782 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
3783 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003784
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003785 // If we are matching a template template argument to a template
3786 // template parameter and one of the non-type template parameter types
3787 // is dependent, then we must wait until template instantiation time
3788 // to actually compare the arguments.
3789 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3790 (OldNTTP->getType()->isDependentType() ||
3791 NewNTTP->getType()->isDependentType()))
3792 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003793
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003794 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
3795 if (Complain) {
3796 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
3797 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003798 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003799 diag::err_template_arg_template_params_mismatch);
3800 NextDiag = diag::note_template_nontype_parm_different_type;
3801 }
3802 S.Diag(NewNTTP->getLocation(), NextDiag)
3803 << NewNTTP->getType()
3804 << (Kind != Sema::TPL_TemplateMatch);
3805 S.Diag(OldNTTP->getLocation(),
3806 diag::note_template_nontype_parm_prev_declaration)
3807 << OldNTTP->getType();
3808 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003809
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003810 return false;
3811 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003812
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003813 return true;
3814 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003815
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003816 // For template template parameters, check the template parameter types.
3817 // The template parameter lists of template template
3818 // parameters must agree.
3819 if (TemplateTemplateParmDecl *OldTTP
3820 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003821 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003822 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
3823 OldTTP->getTemplateParameters(),
3824 Complain,
3825 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003826 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003827 : Kind),
3828 TemplateArgLoc);
3829 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003830
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003831 return true;
3832}
Douglas Gregor02024a92010-03-28 02:42:43 +00003833
Douglas Gregora0347822011-01-13 00:08:50 +00003834/// \brief Diagnose a known arity mismatch when comparing template argument
3835/// lists.
3836static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003837void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00003838 TemplateParameterList *New,
3839 TemplateParameterList *Old,
3840 Sema::TemplateParameterListEqualKind Kind,
3841 SourceLocation TemplateArgLoc) {
3842 unsigned NextDiag = diag::err_template_param_list_different_arity;
3843 if (TemplateArgLoc.isValid()) {
3844 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3845 NextDiag = diag::note_template_param_list_different_arity;
3846 }
3847 S.Diag(New->getTemplateLoc(), NextDiag)
3848 << (New->size() > Old->size())
3849 << (Kind != Sema::TPL_TemplateMatch)
3850 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
3851 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
3852 << (Kind != Sema::TPL_TemplateMatch)
3853 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
3854}
3855
Douglas Gregorddc29e12009-02-06 22:42:48 +00003856/// \brief Determine whether the given template parameter lists are
3857/// equivalent.
3858///
Mike Stump1eb44332009-09-09 15:08:12 +00003859/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00003860/// source code as part of a new template declaration.
3861///
3862/// \param Old The old template parameter list, typically found via
3863/// name lookup of the template declared with this template parameter
3864/// list.
3865///
3866/// \param Complain If true, this routine will produce a diagnostic if
3867/// the template parameter lists are not equivalent.
3868///
Douglas Gregorfb898e12009-11-12 16:20:59 +00003869/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00003870///
3871/// \param TemplateArgLoc If this source location is valid, then we
3872/// are actually checking the template parameter list of a template
3873/// argument (New) against the template parameter list of its
3874/// corresponding template template parameter (Old). We produce
3875/// slightly different diagnostics in this scenario.
3876///
Douglas Gregorddc29e12009-02-06 22:42:48 +00003877/// \returns True if the template parameter lists are equal, false
3878/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003879bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00003880Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
3881 TemplateParameterList *Old,
3882 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003883 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00003884 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00003885 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
3886 if (Complain)
3887 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3888 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00003889
3890 return false;
3891 }
3892
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003893 // C++0x [temp.arg.template]p3:
3894 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003895 // when each of the template parameters in the template-parameter-list of
3896 // the template-argument's corresponding class template or template alias
3897 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00003898 // template-parameter-list of P. [...]
3899 TemplateParameterList::iterator NewParm = New->begin();
3900 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00003901 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00003902 OldParmEnd = Old->end();
3903 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00003904 if (Kind != TPL_TemplateTemplateArgumentMatch ||
3905 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00003906 if (NewParm == NewParmEnd) {
3907 if (Complain)
3908 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3909 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003910
Douglas Gregora0347822011-01-13 00:08:50 +00003911 return false;
3912 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003913
Douglas Gregora0347822011-01-13 00:08:50 +00003914 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
3915 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003916 return false;
3917
Douglas Gregora0347822011-01-13 00:08:50 +00003918 ++NewParm;
3919 continue;
3920 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003921
Douglas Gregora0347822011-01-13 00:08:50 +00003922 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003923 // [...] When P's template- parameter-list contains a template parameter
3924 // pack (14.5.3), the template parameter pack will match zero or more
3925 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00003926 // template-parameter-list of A with the same type and form as the
3927 // template parameter pack in P (ignoring whether those template
3928 // parameters are template parameter packs).
3929 for (; NewParm != NewParmEnd; ++NewParm) {
3930 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
3931 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003932 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00003933 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00003934 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003935
Douglas Gregora0347822011-01-13 00:08:50 +00003936 // Make sure we exhausted all of the arguments.
3937 if (NewParm != NewParmEnd) {
3938 if (Complain)
3939 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3940 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003941
Douglas Gregora0347822011-01-13 00:08:50 +00003942 return false;
3943 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003944
Douglas Gregorddc29e12009-02-06 22:42:48 +00003945 return true;
3946}
3947
3948/// \brief Check whether a template can be declared within this scope.
3949///
3950/// If the template declaration is valid in this scope, returns
3951/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00003952bool
Douglas Gregor05396e22009-08-25 17:23:04 +00003953Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00003954 // Find the nearest enclosing declaration scope.
3955 while ((S->getFlags() & Scope::DeclScope) == 0 ||
3956 (S->getFlags() & Scope::TemplateParamScope) != 0)
3957 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00003958
Douglas Gregorddc29e12009-02-06 22:42:48 +00003959 // C++ [temp]p2:
3960 // A template-declaration can appear only as a namespace scope or
3961 // class scope declaration.
3962 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00003963 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
3964 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00003965 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00003966 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003967
Eli Friedman1503f772009-07-31 01:43:05 +00003968 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00003969 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00003970
3971 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
3972 return false;
3973
Mike Stump1eb44332009-09-09 15:08:12 +00003974 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00003975 diag::err_template_outside_namespace_or_class_scope)
3976 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00003977}
Douglas Gregorcc636682009-02-17 23:15:12 +00003978
Douglas Gregord5cb8762009-10-07 00:13:32 +00003979/// \brief Determine what kind of template specialization the given declaration
3980/// is.
3981static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
3982 if (!D)
3983 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003984
Douglas Gregorf6b11852009-10-08 15:14:33 +00003985 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
3986 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00003987 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
3988 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003989 if (VarDecl *Var = dyn_cast<VarDecl>(D))
3990 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003991
Douglas Gregord5cb8762009-10-07 00:13:32 +00003992 return TSK_Undeclared;
3993}
3994
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003995/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00003996/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00003997///
Douglas Gregor9302da62009-10-14 23:50:59 +00003998/// This routine determines whether a template specialization can be declared
3999/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004000///
4001/// \param S the semantic analysis object for which this check is being
4002/// performed.
4003///
4004/// \param Specialized the entity being specialized or instantiated, which
4005/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004006/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004007/// member class).
4008///
4009/// \param PrevDecl the previous declaration of this entity, if any.
4010///
4011/// \param Loc the location of the explicit specialization or instantiation of
4012/// this entity.
4013///
4014/// \param IsPartialSpecialization whether this is a partial specialization of
4015/// a class template.
4016///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004017/// \returns true if there was an error that we cannot recover from, false
4018/// otherwise.
4019static bool CheckTemplateSpecializationScope(Sema &S,
4020 NamedDecl *Specialized,
4021 NamedDecl *PrevDecl,
4022 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004023 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004024 // Keep these "kind" numbers in sync with the %select statements in the
4025 // various diagnostics emitted by this routine.
4026 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004027 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004028 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004029 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004030 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004031 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004032 EntityKind = 3;
4033 else if (isa<VarDecl>(Specialized))
4034 EntityKind = 4;
4035 else if (isa<RecordDecl>(Specialized))
4036 EntityKind = 5;
4037 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004038 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4039 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004040 return true;
4041 }
4042
Douglas Gregor88b70942009-02-25 22:02:03 +00004043 // C++ [temp.expl.spec]p2:
4044 // An explicit specialization shall be declared in the namespace
4045 // of which the template is a member, or, for member templates, in
4046 // the namespace of which the enclosing class or enclosing class
4047 // template is a member. An explicit specialization of a member
4048 // function, member class or static data member of a class
4049 // template shall be declared in the namespace of which the class
4050 // template is a member. Such a declaration may also be a
4051 // definition. If the declaration is not a definition, the
4052 // specialization may be defined later in the name- space in which
4053 // the explicit specialization was declared, or in a namespace
4054 // that encloses the one in which the explicit specialization was
4055 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004056 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004057 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004058 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004059 return true;
4060 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004061
Douglas Gregor0a407472009-10-07 17:30:37 +00004062 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
4063 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004064 << Specialized;
Douglas Gregor0a407472009-10-07 17:30:37 +00004065 return true;
4066 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004067
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004068 // C++ [temp.class.spec]p6:
4069 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004070 // in any namespace scope in which its definition may be defined (14.5.1
4071 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004072 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004073 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004074 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004075 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004076 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004077 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4078 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004079 // C++ [temp.exp.spec]p2:
4080 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004081 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004082 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004083 // An explicit specialization of a member function, member class or
4084 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004085 // namespace of which the class template is a member.
4086 //
4087 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004088 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004089 // the specialized template.
4090 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4091 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004092 bool IsCPlusPlus0xExtension
4093 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004094 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004095 S.Diag(Loc, IsCPlusPlus0xExtension
4096 ? diag::ext_template_spec_decl_out_of_scope_global
4097 : diag::err_template_spec_decl_out_of_scope_global)
4098 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004099 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004100 S.Diag(Loc, IsCPlusPlus0xExtension
4101 ? diag::ext_template_spec_decl_out_of_scope
4102 : diag::err_template_spec_decl_out_of_scope)
4103 << EntityKind << Specialized
4104 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004105
Douglas Gregor9302da62009-10-14 23:50:59 +00004106 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4107 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004108 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004109 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004110
4111 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004112 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004113 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004114 // specializations of function templates, static data members, and member
4115 // functions, so we skip the check here for those kinds of entities.
4116 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004117 // Should we refactor that check, so that it occurs later?
4118 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004119 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4120 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004121 if (isa<TranslationUnitDecl>(SpecializedContext))
4122 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4123 << EntityKind << Specialized;
4124 else if (isa<NamespaceDecl>(SpecializedContext))
4125 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4126 << EntityKind << Specialized
4127 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004128
Douglas Gregor9302da62009-10-14 23:50:59 +00004129 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004130 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004131
Douglas Gregord5cb8762009-10-07 00:13:32 +00004132 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004133
Douglas Gregor88b70942009-02-25 22:02:03 +00004134 return false;
4135}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004136
Douglas Gregorbacb9492011-01-03 21:13:47 +00004137/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4138/// that checks non-type template partial specialization arguments.
4139static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4140 NonTypeTemplateParmDecl *Param,
4141 const TemplateArgument *Args,
4142 unsigned NumArgs) {
4143 for (unsigned I = 0; I != NumArgs; ++I) {
4144 if (Args[I].getKind() == TemplateArgument::Pack) {
4145 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004146 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004147 Args[I].pack_size()))
4148 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004149
Douglas Gregore94866f2009-06-12 21:21:02 +00004150 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004151 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004152
Douglas Gregorbacb9492011-01-03 21:13:47 +00004153 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004154 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004155 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004156 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004157
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004158 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004159 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4160 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004161
4162 // Strip off any implicit casts we added as part of type checking.
4163 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4164 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004165
Douglas Gregore94866f2009-06-12 21:21:02 +00004166 // C++ [temp.class.spec]p8:
4167 // A non-type argument is non-specialized if it is the name of a
4168 // non-type parameter. All other non-type arguments are
4169 // specialized.
4170 //
4171 // Below, we check the two conditions that only apply to
4172 // specialized non-type arguments, so skip any non-specialized
4173 // arguments.
4174 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004175 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004176 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004177
Douglas Gregore94866f2009-06-12 21:21:02 +00004178 // C++ [temp.class.spec]p9:
4179 // Within the argument list of a class template partial
4180 // specialization, the following restrictions apply:
4181 // -- A partially specialized non-type argument expression
4182 // shall not involve a template parameter of the partial
4183 // specialization except when the argument expression is a
4184 // simple identifier.
4185 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004186 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004187 diag::err_dependent_non_type_arg_in_partial_spec)
4188 << ArgExpr->getSourceRange();
4189 return true;
4190 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004191
Douglas Gregore94866f2009-06-12 21:21:02 +00004192 // -- The type of a template parameter corresponding to a
4193 // specialized non-type argument shall not be dependent on a
4194 // parameter of the specialization.
4195 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004196 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004197 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4198 << Param->getType()
4199 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004200 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004201 return true;
4202 }
4203 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004204
Douglas Gregorbacb9492011-01-03 21:13:47 +00004205 return false;
4206}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004207
Douglas Gregorbacb9492011-01-03 21:13:47 +00004208/// \brief Check the non-type template arguments of a class template
4209/// partial specialization according to C++ [temp.class.spec]p9.
4210///
4211/// \param TemplateParams the template parameters of the primary class
4212/// template.
4213///
4214/// \param TemplateArg the template arguments of the class template
4215/// partial specialization.
4216///
4217/// \returns true if there was an error, false otherwise.
4218static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4219 TemplateParameterList *TemplateParams,
4220 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4221 const TemplateArgument *ArgList = TemplateArgs.data();
4222
4223 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4224 NonTypeTemplateParmDecl *Param
4225 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4226 if (!Param)
4227 continue;
4228
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004229 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004230 &ArgList[I], 1))
4231 return true;
4232 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004233
4234 return false;
4235}
4236
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004237/// \brief Retrieve the previous declaration of the given declaration.
4238static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4239 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4240 return VD->getPreviousDeclaration();
4241 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4242 return FD->getPreviousDeclaration();
4243 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4244 return TD->getPreviousDeclaration();
4245 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND))
4246 return TD->getPreviousDeclaration();
4247 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4248 return FTD->getPreviousDeclaration();
4249 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4250 return CTD->getPreviousDeclaration();
4251 return 0;
4252}
4253
John McCalld226f652010-08-21 09:40:31 +00004254DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004255Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4256 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004257 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004258 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004259 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004260 SourceLocation TemplateNameLoc,
4261 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004262 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004263 SourceLocation RAngleLoc,
4264 AttributeList *Attr,
4265 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004266 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004267
Douglas Gregorcc636682009-02-17 23:15:12 +00004268 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004269 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004270 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004271 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4272
4273 if (!ClassTemplate) {
4274 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004275 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004276 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4277 return true;
4278 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004279
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004280 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004281 bool isPartialSpecialization = false;
4282
Douglas Gregor88b70942009-02-25 22:02:03 +00004283 // Check the validity of the template headers that introduce this
4284 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004285 // FIXME: We probably shouldn't complain about these headers for
4286 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004287 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004288 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00004289 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
4290 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004291 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004292 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004293 isExplicitSpecialization,
4294 Invalid);
4295 if (Invalid)
4296 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004297
Abramo Bagnara9b934882010-06-12 08:15:14 +00004298 unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size();
4299 if (TemplateParams)
4300 --NumMatchedTemplateParamLists;
4301
Douglas Gregor05396e22009-08-25 17:23:04 +00004302 if (TemplateParams && TemplateParams->size() > 0) {
4303 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004304
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004305 if (TUK == TUK_Friend) {
4306 Diag(KWLoc, diag::err_partial_specialization_friend)
4307 << SourceRange(LAngleLoc, RAngleLoc);
4308 return true;
4309 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004310
Douglas Gregor05396e22009-08-25 17:23:04 +00004311 // C++ [temp.class.spec]p10:
4312 // The template parameter list of a specialization shall not
4313 // contain default template argument values.
4314 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4315 Decl *Param = TemplateParams->getParam(I);
4316 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4317 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004318 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004319 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004320 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004321 }
4322 } else if (NonTypeTemplateParmDecl *NTTP
4323 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4324 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004325 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004326 diag::err_default_arg_in_partial_spec)
4327 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004328 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004329 }
4330 } else {
4331 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004332 if (TTP->hasDefaultArgument()) {
4333 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004334 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004335 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004336 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004337 }
4338 }
4339 }
Douglas Gregora735b202009-10-13 14:39:41 +00004340 } else if (TemplateParams) {
4341 if (TUK == TUK_Friend)
4342 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004343 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004344 SourceRange(TemplateParams->getTemplateLoc(),
4345 TemplateParams->getRAngleLoc()))
4346 << SourceRange(LAngleLoc, RAngleLoc);
4347 else
4348 isExplicitSpecialization = true;
4349 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004350 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004351 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004352 isExplicitSpecialization = true;
4353 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004354
Douglas Gregorcc636682009-02-17 23:15:12 +00004355 // Check that the specialization uses the same tag kind as the
4356 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004357 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4358 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004359 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004360 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004361 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004362 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004363 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004364 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004365 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004366 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004367 diag::note_previous_use);
4368 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4369 }
4370
Douglas Gregor40808ce2009-03-09 23:48:35 +00004371 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004372 TemplateArgumentListInfo TemplateArgs;
4373 TemplateArgs.setLAngleLoc(LAngleLoc);
4374 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004375 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004376
Douglas Gregor925910d2011-01-03 20:35:03 +00004377 // Check for unexpanded parameter packs in any of the template arguments.
4378 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004379 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004380 UPPC_PartialSpecialization))
4381 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004382
Douglas Gregorcc636682009-02-17 23:15:12 +00004383 // Check that the template argument list is well-formed for this
4384 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004385 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004386 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4387 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004388 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004389
Douglas Gregor910f8002010-11-07 23:05:16 +00004390 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004391 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004392
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004393 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004394 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004395 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004396 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004397 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004398 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004399 return true;
4400
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004401 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004402 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004403 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004404 TemplateArgs.size())) {
4405 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4406 << ClassTemplate->getDeclName();
4407 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004408 }
4409 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004410
Douglas Gregorcc636682009-02-17 23:15:12 +00004411 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004412 ClassTemplateSpecializationDecl *PrevDecl = 0;
4413
4414 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004415 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004416 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004417 = ClassTemplate->findPartialSpecialization(Converted.data(),
4418 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004419 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004420 else
4421 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004422 = ClassTemplate->findSpecialization(Converted.data(),
4423 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004424
4425 ClassTemplateSpecializationDecl *Specialization = 0;
4426
Douglas Gregor88b70942009-02-25 22:02:03 +00004427 // Check whether we can declare a class template specialization in
4428 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004429 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004430 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4431 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004432 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004433 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004434
Douglas Gregorb88e8882009-07-30 17:40:51 +00004435 // The canonical type
4436 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004437 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004438 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004439 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004440 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004441 // arguments was referenced but not declared, or we're only
4442 // referencing this specialization as a friend, reuse that
Douglas Gregorcc636682009-02-17 23:15:12 +00004443 // declaration node as our own, updating its source location to
4444 // reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004445 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004446 Specialization->setLocation(TemplateNameLoc);
Douglas Gregorcc636682009-02-17 23:15:12 +00004447 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004448 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004449 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004450 // Build the canonical type that describes the converted template
4451 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004452 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4453 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004454 Converted.data(),
4455 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004456
4457 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004458 ClassTemplate->getInjectedClassNameSpecialization())) {
4459 // C++ [temp.class.spec]p9b3:
4460 //
4461 // -- The argument list of the specialization shall not be identical
4462 // to the implicit argument list of the primary template.
4463 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4464 << (TUK == TUK_Definition)
4465 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4466 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4467 ClassTemplate->getIdentifier(),
4468 TemplateNameLoc,
4469 Attr,
4470 TemplateParams,
4471 AS_none);
4472 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004473
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004474 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004475 ClassTemplatePartialSpecializationDecl *PrevPartial
4476 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004477 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004478 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004479 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004480 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004481 ClassTemplate->getDeclContext(),
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004482 TemplateNameLoc,
4483 TemplateParams,
4484 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004485 Converted.data(),
4486 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004487 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004488 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004489 PrevPartial,
4490 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004491 SetNestedNameSpecifier(Partial, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004492 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004493 Partial->setTemplateParameterListsInfo(Context,
4494 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004495 (TemplateParameterList**) TemplateParameterLists.release());
4496 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004497
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004498 if (!PrevPartial)
4499 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004500 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004501
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004502 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004503 // template specialization, make a note of that.
4504 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4505 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004506
Douglas Gregor031a5882009-06-13 00:26:55 +00004507 // Check that all of the template parameters of the class template
4508 // partial specialization are deducible from the template
4509 // arguments. If not, this class template partial specialization
4510 // will never be used.
4511 llvm::SmallVector<bool, 8> DeducibleParams;
4512 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004513 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004514 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004515 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004516 unsigned NumNonDeducible = 0;
4517 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4518 if (!DeducibleParams[I])
4519 ++NumNonDeducible;
4520
4521 if (NumNonDeducible) {
4522 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4523 << (NumNonDeducible > 1)
4524 << SourceRange(TemplateNameLoc, RAngleLoc);
4525 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4526 if (!DeducibleParams[I]) {
4527 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4528 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004529 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004530 diag::note_partial_spec_unused_parameter)
4531 << Param->getDeclName();
4532 else
Mike Stump1eb44332009-09-09 15:08:12 +00004533 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004534 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004535 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004536 }
4537 }
4538 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004539 } else {
4540 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004541 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004542 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004543 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004544 ClassTemplate->getDeclContext(),
4545 TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004546 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004547 Converted.data(),
4548 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004549 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004550 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004551 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004552 Specialization->setTemplateParameterListsInfo(Context,
4553 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004554 (TemplateParameterList**) TemplateParameterLists.release());
4555 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004556
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004557 if (!PrevDecl)
4558 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004559
4560 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004561 }
4562
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004563 // C++ [temp.expl.spec]p6:
4564 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004565 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004566 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004567 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004568 // use occurs; no diagnostic is required.
4569 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004570 bool Okay = false;
4571 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4572 // Is there any previous explicit specialization declaration?
4573 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4574 Okay = true;
4575 break;
4576 }
4577 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004578
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004579 if (!Okay) {
4580 SourceRange Range(TemplateNameLoc, RAngleLoc);
4581 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
4582 << Context.getTypeDeclType(Specialization) << Range;
4583
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004584 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004585 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004586 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004587 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004588 return true;
4589 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004590 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004591
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004592 // If this is not a friend, note that this is an explicit specialization.
4593 if (TUK != TUK_Friend)
4594 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004595
4596 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004597 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00004598 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004599 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00004600 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004601 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00004602 Diag(Def->getLocation(), diag::note_previous_definition);
4603 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00004604 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004605 }
4606 }
4607
John McCall7f1b9872010-12-18 03:30:47 +00004608 if (Attr)
4609 ProcessDeclAttributeList(S, Specialization, Attr);
4610
Douglas Gregorfc705b82009-02-26 22:19:44 +00004611 // Build the fully-sugared type for this class template
4612 // specialization as the user wrote in the specialization
4613 // itself. This means that we'll pretty-print the type retrieved
4614 // from the specialization's declaration the way that the user
4615 // actually wrote the specialization, rather than formatting the
4616 // name based on the "canonical" representation used to store the
4617 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00004618 TypeSourceInfo *WrittenTy
4619 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
4620 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004621 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004622 Specialization->setTypeAsWritten(WrittenTy);
Douglas Gregor7e9b57b2010-07-06 18:33:12 +00004623 if (TemplateParams)
4624 Specialization->setTemplateKeywordLoc(TemplateParams->getTemplateLoc());
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004625 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00004626 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00004627
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004628 // C++ [temp.expl.spec]p9:
4629 // A template explicit specialization is in the scope of the
4630 // namespace in which the template was defined.
4631 //
4632 // We actually implement this paragraph where we set the semantic
4633 // context (in the creation of the ClassTemplateSpecializationDecl),
4634 // but we also maintain the lexical context where the actual
4635 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00004636 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00004637
Douglas Gregorcc636682009-02-17 23:15:12 +00004638 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004639 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00004640 Specialization->startDefinition();
4641
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004642 if (TUK == TUK_Friend) {
4643 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
4644 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00004645 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004646 /*FIXME:*/KWLoc);
4647 Friend->setAccess(AS_public);
4648 CurContext->addDecl(Friend);
4649 } else {
4650 // Add the specialization into its lexical context, so that it can
4651 // be seen when iterating through the list of declarations in that
4652 // context. However, specializations are not found by name lookup.
4653 CurContext->addDecl(Specialization);
4654 }
John McCalld226f652010-08-21 09:40:31 +00004655 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00004656}
Douglas Gregord57959a2009-03-27 23:10:48 +00004657
John McCalld226f652010-08-21 09:40:31 +00004658Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00004659 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004660 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00004661 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
4662}
4663
John McCalld226f652010-08-21 09:40:31 +00004664Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00004665 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004666 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00004667 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004668 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00004669
Douglas Gregor52591bf2009-06-24 00:54:41 +00004670 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00004671 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00004672 }
Mike Stump1eb44332009-09-09 15:08:12 +00004673
Douglas Gregor52591bf2009-06-24 00:54:41 +00004674 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004675
John McCalld226f652010-08-21 09:40:31 +00004676 Decl *DP = HandleDeclarator(ParentScope, D,
4677 move(TemplateParameterLists),
4678 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00004679 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00004680 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00004681 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00004682 FunctionTemplate->getTemplatedDecl());
4683 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
4684 return ActOnStartOfFunctionDef(FnBodyScope, Function);
4685 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00004686}
4687
John McCall75042392010-02-11 01:33:53 +00004688/// \brief Strips various properties off an implicit instantiation
4689/// that has just been explicitly specialized.
4690static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00004691 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00004692
4693 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4694 FD->setInlineSpecified(false);
4695 }
4696}
4697
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004698/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00004699/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004700/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00004701/// new specialization/instantiation will have any effect.
4702///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004703/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00004704/// instantiation.
4705///
4706/// \param NewTSK the kind of the new explicit specialization or instantiation.
4707///
4708/// \param PrevDecl the previous declaration of the entity.
4709///
4710/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
4711///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004712/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00004713/// declaration was instantiated (either implicitly or explicitly).
4714///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004715/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00004716/// specialization or instantiation has no effect and should be ignored.
4717///
4718/// \returns true if there was an error that should prevent the introduction of
4719/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00004720bool
4721Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
4722 TemplateSpecializationKind NewTSK,
4723 NamedDecl *PrevDecl,
4724 TemplateSpecializationKind PrevTSK,
4725 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004726 bool &HasNoEffect) {
4727 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004728
Douglas Gregor454885e2009-10-15 15:54:05 +00004729 switch (NewTSK) {
4730 case TSK_Undeclared:
4731 case TSK_ImplicitInstantiation:
4732 assert(false && "Don't check implicit instantiations here");
4733 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004734
Douglas Gregor454885e2009-10-15 15:54:05 +00004735 case TSK_ExplicitSpecialization:
4736 switch (PrevTSK) {
4737 case TSK_Undeclared:
4738 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004739 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00004740 // explicitly specialized or has merely been mentioned without any
4741 // instantiation.
4742 return false;
4743
4744 case TSK_ImplicitInstantiation:
4745 if (PrevPointOfInstantiation.isInvalid()) {
4746 // The declaration itself has not actually been instantiated, so it is
4747 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00004748 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00004749 return false;
4750 }
4751 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004752
Douglas Gregor454885e2009-10-15 15:54:05 +00004753 case TSK_ExplicitInstantiationDeclaration:
4754 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004755 assert((PrevTSK == TSK_ImplicitInstantiation ||
4756 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00004757 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004758
Douglas Gregor454885e2009-10-15 15:54:05 +00004759 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004760 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00004761 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004762 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00004763 // implicit instantiation to take place, in every translation unit in
4764 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004765 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4766 // Is there any previous explicit specialization declaration?
4767 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
4768 return false;
4769 }
4770
Douglas Gregor0d035142009-10-27 18:42:08 +00004771 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00004772 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004773 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00004774 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004775
Douglas Gregor454885e2009-10-15 15:54:05 +00004776 return true;
4777 }
4778 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004779
Douglas Gregor454885e2009-10-15 15:54:05 +00004780 case TSK_ExplicitInstantiationDeclaration:
4781 switch (PrevTSK) {
4782 case TSK_ExplicitInstantiationDeclaration:
4783 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004784 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004785 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004786
Douglas Gregor454885e2009-10-15 15:54:05 +00004787 case TSK_Undeclared:
4788 case TSK_ImplicitInstantiation:
4789 // We're explicitly instantiating something that may have already been
4790 // implicitly instantiated; that's fine.
4791 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004792
Douglas Gregor454885e2009-10-15 15:54:05 +00004793 case TSK_ExplicitSpecialization:
4794 // C++0x [temp.explicit]p4:
4795 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004796 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00004797 // specialization for that template, the explicit instantiation has no
4798 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004799 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004800 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004801
Douglas Gregor454885e2009-10-15 15:54:05 +00004802 case TSK_ExplicitInstantiationDefinition:
4803 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004804 // If an entity is the subject of both an explicit instantiation
4805 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00004806 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004807 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00004808 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004809 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004810 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00004811 assert(PrevPointOfInstantiation.isValid() &&
4812 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004813 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004814 return false;
4815 }
4816 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004817
Douglas Gregor454885e2009-10-15 15:54:05 +00004818 case TSK_ExplicitInstantiationDefinition:
4819 switch (PrevTSK) {
4820 case TSK_Undeclared:
4821 case TSK_ImplicitInstantiation:
4822 // We're explicitly instantiating something that may have already been
4823 // implicitly instantiated; that's fine.
4824 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004825
Douglas Gregor454885e2009-10-15 15:54:05 +00004826 case TSK_ExplicitSpecialization:
4827 // C++ DR 259, C++0x [temp.explicit]p4:
4828 // For a given set of template parameters, if an explicit
4829 // instantiation of a template appears after a declaration of
4830 // an explicit specialization for that template, the explicit
4831 // instantiation has no effect.
4832 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004833 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00004834 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00004835 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00004836 if (!getLangOptions().CPlusPlus0x) {
4837 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00004838 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004839 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00004840 diag::note_previous_template_specialization);
4841 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004842 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004843 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004844
Douglas Gregor454885e2009-10-15 15:54:05 +00004845 case TSK_ExplicitInstantiationDeclaration:
4846 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004847 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00004848 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004849
Douglas Gregor454885e2009-10-15 15:54:05 +00004850 case TSK_ExplicitInstantiationDefinition:
4851 // C++0x [temp.spec]p5:
4852 // For a given template and a given set of template-arguments,
4853 // - an explicit instantiation definition shall appear at most once
4854 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00004855 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00004856 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004857 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004858 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004859 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004860 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00004861 }
4862 break;
4863 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004864
Douglas Gregor454885e2009-10-15 15:54:05 +00004865 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004866
Douglas Gregor454885e2009-10-15 15:54:05 +00004867 return false;
4868}
4869
John McCallaf2094e2010-04-08 09:05:18 +00004870/// \brief Perform semantic analysis for the given dependent function
4871/// template specialization. The only possible way to get a dependent
4872/// function template specialization is with a friend declaration,
4873/// like so:
4874///
4875/// template <class T> void foo(T);
4876/// template <class T> class A {
4877/// friend void foo<>(T);
4878/// };
4879///
4880/// There really isn't any useful analysis we can do here, so we
4881/// just store the information.
4882bool
4883Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
4884 const TemplateArgumentListInfo &ExplicitTemplateArgs,
4885 LookupResult &Previous) {
4886 // Remove anything from Previous that isn't a function template in
4887 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004888 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00004889 LookupResult::Filter F = Previous.makeFilter();
4890 while (F.hasNext()) {
4891 NamedDecl *D = F.next()->getUnderlyingDecl();
4892 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00004893 !FDLookupContext->InEnclosingNamespaceSetOf(
4894 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00004895 F.erase();
4896 }
4897 F.done();
4898
4899 // Should this be diagnosed here?
4900 if (Previous.empty()) return true;
4901
4902 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
4903 ExplicitTemplateArgs);
4904 return false;
4905}
4906
Abramo Bagnarae03db982010-05-20 15:32:11 +00004907/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004908/// specialization.
4909///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004910/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004911/// explicit function template specialization. On successful completion,
4912/// the function declaration \p FD will become a function template
4913/// specialization.
4914///
4915/// \param FD the function declaration, which will be updated to become a
4916/// function template specialization.
4917///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004918/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
4919/// if any. Note that this may be valid info even when 0 arguments are
4920/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
4921/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004922///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004923/// \param PrevDecl the set of declarations that may be specialized by
4924/// this function specialization.
4925bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004926Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
John McCalld5532b62009-11-23 01:53:49 +00004927 const TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00004928 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004929 // The set of function template specializations that could match this
4930 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00004931 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004932
Sebastian Redl7a126a42010-08-31 00:36:30 +00004933 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00004934 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
4935 I != E; ++I) {
4936 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
4937 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004938 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004939 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004940 if (!FDLookupContext->InEnclosingNamespaceSetOf(
4941 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004942 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004943
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004944 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004945 // A trailing template-argument can be left unspecified in the
4946 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004947 // provided it can be deduced from the function argument type.
4948 // Perform template argument deduction to determine whether we may be
4949 // specializing this template.
4950 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00004951 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004952 FunctionDecl *Specialization = 0;
4953 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00004954 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004955 FD->getType(),
4956 Specialization,
4957 Info)) {
4958 // FIXME: Template argument deduction failed; record why it failed, so
4959 // that we can provide nifty diagnostics.
4960 (void)TDK;
4961 continue;
4962 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004963
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004964 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00004965 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004966 }
4967 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004968
Douglas Gregorc5df30f2009-09-26 03:41:46 +00004969 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00004970 UnresolvedSetIterator Result
4971 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00004972 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004973 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00004974 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00004975 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00004976 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00004977 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00004978 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004979 return true;
John McCallc373d482010-01-27 01:50:18 +00004980
4981 // Ignore access information; it doesn't figure into redeclaration checking.
4982 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Douglas Gregorc42b6522010-04-09 21:02:29 +00004983 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004984
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004985 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004986 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00004987
4988 // If this is a friend declaration, then we're not really declaring
4989 // an explicit specialization.
4990 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004991
Douglas Gregord5cb8762009-10-07 00:13:32 +00004992 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00004993 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004994 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004995 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004996 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00004997 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004998 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004999
5000 // C++ [temp.expl.spec]p6:
5001 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005002 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005003 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005004 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005005 // use occurs; no diagnostic is required.
5006 FunctionTemplateSpecializationInfo *SpecInfo
5007 = Specialization->getTemplateSpecializationInfo();
5008 assert(SpecInfo && "Function template specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005009
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005010 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005011 if (!isFriend &&
5012 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005013 TSK_ExplicitSpecialization,
5014 Specialization,
5015 SpecInfo->getTemplateSpecializationKind(),
5016 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005017 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005018 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005019
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005020 // Mark the prior declaration as an explicit specialization, so that later
5021 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005022 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005023 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005024 MarkUnusedFileScopedDecl(Specialization);
5025 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005026
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005027 // Turn the given function declaration into a function template
5028 // specialization, with the template arguments from the previous
5029 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005030 // Take copies of (semantic and syntactic) template argument lists.
5031 const TemplateArgumentList* TemplArgs = new (Context)
5032 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5033 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5034 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005035 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005036 TemplArgs, /*InsertPos=*/0,
5037 SpecInfo->getTemplateSpecializationKind(),
5038 TemplArgsAsWritten);
5039
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005040 // The "previous declaration" for this function template specialization is
5041 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005042 Previous.clear();
5043 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005044 return false;
5045}
5046
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005047/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005048/// specialization.
5049///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005050/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005051/// explicit member function specialization. On successful completion,
5052/// the function declaration \p FD will become a member function
5053/// specialization.
5054///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005055/// \param Member the member declaration, which will be updated to become a
5056/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005057///
John McCall68263142009-11-18 22:49:29 +00005058/// \param Previous the set of declarations, one of which may be specialized
5059/// by this function specialization; the set will be modified to contain the
5060/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005061bool
John McCall68263142009-11-18 22:49:29 +00005062Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005063 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005064
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005065 // Try to find the member we are instantiating.
5066 NamedDecl *Instantiation = 0;
5067 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005068 MemberSpecializationInfo *MSInfo = 0;
5069
John McCall68263142009-11-18 22:49:29 +00005070 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005071 // Nowhere to look anyway.
5072 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005073 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5074 I != E; ++I) {
5075 NamedDecl *D = (*I)->getUnderlyingDecl();
5076 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005077 if (Context.hasSameType(Function->getType(), Method->getType())) {
5078 Instantiation = Method;
5079 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005080 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005081 break;
5082 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005083 }
5084 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005085 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005086 VarDecl *PrevVar;
5087 if (Previous.isSingleResult() &&
5088 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005089 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005090 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005091 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005092 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005093 }
5094 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005095 CXXRecordDecl *PrevRecord;
5096 if (Previous.isSingleResult() &&
5097 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5098 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005099 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005100 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005101 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005102 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005103
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005104 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005105 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005106 // specializations are always out-of-line, the caller will complain about
5107 // this mismatch later.
5108 return false;
5109 }
John McCall77e8b112010-04-13 20:37:33 +00005110
5111 // If this is a friend, just bail out here before we start turning
5112 // things into explicit specializations.
5113 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5114 // Preserve instantiation information.
5115 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5116 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5117 cast<CXXMethodDecl>(InstantiatedFrom),
5118 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5119 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5120 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5121 cast<CXXRecordDecl>(InstantiatedFrom),
5122 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5123 }
5124
5125 Previous.clear();
5126 Previous.addDecl(Instantiation);
5127 return false;
5128 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005129
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005130 // Make sure that this is a specialization of a member.
5131 if (!InstantiatedFrom) {
5132 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5133 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005134 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5135 return true;
5136 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005137
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005138 // C++ [temp.expl.spec]p6:
5139 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005140 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005141 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005142 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005143 // use occurs; no diagnostic is required.
5144 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005145
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005146 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005147 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5148 TSK_ExplicitSpecialization,
5149 Instantiation,
5150 MSInfo->getTemplateSpecializationKind(),
5151 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005152 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005153 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005154
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005155 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005156 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005157 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005158 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005159 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005160 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005161
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005162 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005163 // the original declaration to note that it is an explicit specialization
5164 // (if it was previously an implicit instantiation). This latter step
5165 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005166 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005167 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5168 if (InstantiationFunction->getTemplateSpecializationKind() ==
5169 TSK_ImplicitInstantiation) {
5170 InstantiationFunction->setTemplateSpecializationKind(
5171 TSK_ExplicitSpecialization);
5172 InstantiationFunction->setLocation(Member->getLocation());
5173 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005174
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005175 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5176 cast<CXXMethodDecl>(InstantiatedFrom),
5177 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005178 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005179 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005180 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5181 if (InstantiationVar->getTemplateSpecializationKind() ==
5182 TSK_ImplicitInstantiation) {
5183 InstantiationVar->setTemplateSpecializationKind(
5184 TSK_ExplicitSpecialization);
5185 InstantiationVar->setLocation(Member->getLocation());
5186 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005187
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005188 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5189 cast<VarDecl>(InstantiatedFrom),
5190 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005191 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005192 } else {
5193 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005194 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5195 if (InstantiationClass->getTemplateSpecializationKind() ==
5196 TSK_ImplicitInstantiation) {
5197 InstantiationClass->setTemplateSpecializationKind(
5198 TSK_ExplicitSpecialization);
5199 InstantiationClass->setLocation(Member->getLocation());
5200 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005201
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005202 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005203 cast<CXXRecordDecl>(InstantiatedFrom),
5204 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005205 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005206
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005207 // Save the caller the trouble of having to figure out which declaration
5208 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005209 Previous.clear();
5210 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005211 return false;
5212}
5213
Douglas Gregor558c0322009-10-14 23:41:34 +00005214/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005215///
5216/// \returns true if a serious error occurs, false otherwise.
5217static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005218 SourceLocation InstLoc,
5219 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005220 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5221 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005222
Douglas Gregor669eed82010-07-13 00:10:04 +00005223 if (CurContext->isRecord()) {
5224 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5225 << D;
5226 return true;
5227 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005228
Douglas Gregor558c0322009-10-14 23:41:34 +00005229 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005230 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005231 // template.
5232 //
5233 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005234 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005235 !CurContext->Encloses(OrigContext)) {
5236 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005237 S.Diag(InstLoc,
5238 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005239 diag::err_explicit_instantiation_out_of_scope
5240 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005241 << D << NS;
5242 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005243 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005244 S.getLangOptions().CPlusPlus0x?
5245 diag::err_explicit_instantiation_must_be_global
5246 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005247 << D;
5248 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005249 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005250 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005251
Douglas Gregor558c0322009-10-14 23:41:34 +00005252 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005253 // If the name declared in the explicit instantiation is an unqualified
5254 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005255 // its template is declared or, if that namespace is inline (7.3.1), any
5256 // namespace from its enclosing namespace set.
5257 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005258 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005259
5260 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005261 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005262
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005263 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005264 S.getLangOptions().CPlusPlus0x?
5265 diag::err_explicit_instantiation_unqualified_wrong_namespace
5266 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005267 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005268 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005269 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005270}
5271
5272/// \brief Determine whether the given scope specifier has a template-id in it.
5273static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5274 if (!SS.isSet())
5275 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005276
Douglas Gregor558c0322009-10-14 23:41:34 +00005277 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005278 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005279 // or a static data member of a class template specialization, the name of
5280 // the class template specialization in the qualified-id for the member
5281 // name shall be a simple-template-id.
5282 //
5283 // C++98 has the same restriction, just worded differently.
5284 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5285 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005286 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005287 if (isa<TemplateSpecializationType>(T))
5288 return true;
5289
5290 return false;
5291}
5292
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005293// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005294DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005295Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005296 SourceLocation ExternLoc,
5297 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005298 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005299 SourceLocation KWLoc,
5300 const CXXScopeSpec &SS,
5301 TemplateTy TemplateD,
5302 SourceLocation TemplateNameLoc,
5303 SourceLocation LAngleLoc,
5304 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005305 SourceLocation RAngleLoc,
5306 AttributeList *Attr) {
5307 // Find the class template we're specializing
5308 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005309 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005310 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5311
5312 // Check that the specialization uses the same tag kind as the
5313 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005314 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5315 assert(Kind != TTK_Enum &&
5316 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005317 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005318 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005319 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005320 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005321 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005322 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005323 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005324 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005325 diag::note_previous_use);
5326 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5327 }
5328
Douglas Gregor558c0322009-10-14 23:41:34 +00005329 // C++0x [temp.explicit]p2:
5330 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005331 // definition and an explicit instantiation declaration. An explicit
5332 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005333 TemplateSpecializationKind TSK
5334 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5335 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005336
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005337 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005338 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005339 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005340
5341 // Check that the template argument list is well-formed for this
5342 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005343 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005344 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5345 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005346 return true;
5347
Douglas Gregor910f8002010-11-07 23:05:16 +00005348 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005349 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005350
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005351 // Find the class template specialization declaration that
5352 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005353 void *InsertPos = 0;
5354 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005355 = ClassTemplate->findSpecialization(Converted.data(),
5356 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005357
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005358 TemplateSpecializationKind PrevDecl_TSK
5359 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5360
Douglas Gregord5cb8762009-10-07 00:13:32 +00005361 // C++0x [temp.explicit]p2:
5362 // [...] An explicit instantiation shall appear in an enclosing
5363 // namespace of its template. [...]
5364 //
5365 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005366 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5367 SS.isSet()))
5368 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005369
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005370 ClassTemplateSpecializationDecl *Specialization = 0;
5371
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005372 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005373 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005374 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005375 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005376 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005377 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005378 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005379
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005380 // Even though HasNoEffect == true means that this explicit instantiation
5381 // has no effect on semantics, we go on to put its syntax in the AST.
5382
5383 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5384 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005385 // Since the only prior class template specialization with these
5386 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005387 // declaration node as our own, updating the source location
5388 // for the template name to reflect our new declaration.
5389 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005390 Specialization = PrevDecl;
5391 Specialization->setLocation(TemplateNameLoc);
5392 PrevDecl = 0;
5393 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005394 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005395
Douglas Gregor52604ab2009-09-11 21:19:12 +00005396 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005397 // Create a new class template specialization declaration node for
5398 // this explicit specialization.
5399 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005400 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005401 ClassTemplate->getDeclContext(),
5402 TemplateNameLoc,
5403 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005404 Converted.data(),
5405 Converted.size(),
5406 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005407 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005408
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005409 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005410 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005411 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005412 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005413 }
5414
5415 // Build the fully-sugared type for this explicit instantiation as
5416 // the user wrote in the explicit instantiation itself. This means
5417 // that we'll pretty-print the type retrieved from the
5418 // specialization's declaration the way that the user actually wrote
5419 // the explicit instantiation, rather than formatting the name based
5420 // on the "canonical" representation used to store the template
5421 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005422 TypeSourceInfo *WrittenTy
5423 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5424 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005425 Context.getTypeDeclType(Specialization));
5426 Specialization->setTypeAsWritten(WrittenTy);
5427 TemplateArgsIn.release();
5428
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005429 // Set source locations for keywords.
5430 Specialization->setExternLoc(ExternLoc);
5431 Specialization->setTemplateKeywordLoc(TemplateLoc);
5432
5433 // Add the explicit instantiation into its lexical context. However,
5434 // since explicit instantiations are never found by name lookup, we
5435 // just put it into the declaration context directly.
5436 Specialization->setLexicalDeclContext(CurContext);
5437 CurContext->addDecl(Specialization);
5438
5439 // Syntax is now OK, so return if it has no other effect on semantics.
5440 if (HasNoEffect) {
5441 // Set the template specialization kind.
5442 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005443 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005444 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005445
5446 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005447 // A definition of a class template or class member template
5448 // shall be in scope at the point of the explicit instantiation of
5449 // the class template or class member template.
5450 //
5451 // This check comes when we actually try to perform the
5452 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005453 ClassTemplateSpecializationDecl *Def
5454 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005455 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005456 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005457 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005458 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005459 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005460 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5461 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005462
Douglas Gregor0d035142009-10-27 18:42:08 +00005463 // Instantiate the members of this class template specialization.
5464 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005465 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005466 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005467 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5468
5469 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5470 // TSK_ExplicitInstantiationDefinition
5471 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5472 TSK == TSK_ExplicitInstantiationDefinition)
5473 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005474
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005475 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005476 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005477
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005478 // Set the template specialization kind.
5479 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005480 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005481}
5482
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005483// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005484DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005485Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005486 SourceLocation ExternLoc,
5487 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005488 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005489 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005490 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005491 IdentifierInfo *Name,
5492 SourceLocation NameLoc,
5493 AttributeList *Attr) {
5494
Douglas Gregor402abb52009-05-28 23:31:59 +00005495 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005496 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005497 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005498 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5499 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005500 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005501 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005502 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5503
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005504 if (!TagD)
5505 return true;
5506
John McCalld226f652010-08-21 09:40:31 +00005507 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005508 if (Tag->isEnum()) {
5509 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5510 << Context.getTypeDeclType(Tag);
5511 return true;
5512 }
5513
Douglas Gregord0c87372009-05-27 17:30:49 +00005514 if (Tag->isInvalidDecl())
5515 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005516
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005517 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5518 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5519 if (!Pattern) {
5520 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5521 << Context.getTypeDeclType(Record);
5522 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5523 return true;
5524 }
5525
Douglas Gregor558c0322009-10-14 23:41:34 +00005526 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005527 // If the explicit instantiation is for a class or member class, the
5528 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005529 // simple-template-id.
5530 //
5531 // C++98 has the same restriction, just worded differently.
5532 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005533 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005534 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005535
Douglas Gregor558c0322009-10-14 23:41:34 +00005536 // C++0x [temp.explicit]p2:
5537 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005538 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005539 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005540 TemplateSpecializationKind TSK
5541 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5542 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005543
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005544 // C++0x [temp.explicit]p2:
5545 // [...] An explicit instantiation shall appear in an enclosing
5546 // namespace of its template. [...]
5547 //
5548 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005549 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005550
Douglas Gregor454885e2009-10-15 15:54:05 +00005551 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005552 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005553 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005554 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005555 PrevDecl = Record;
5556 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005557 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005558 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005559 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005560 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005561 PrevDecl,
5562 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005563 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005564 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005565 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005566 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005567 return TagD;
5568 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005569
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005570 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00005571 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005572 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005573 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005574 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005575 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005576 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00005577 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005578 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00005579 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
5580 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005581 Diag(Pattern->getLocation(), diag::note_forward_declaration)
5582 << Pattern;
5583 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00005584 } else {
5585 if (InstantiateClass(NameLoc, Record, Def,
5586 getTemplateInstantiationArgs(Record),
5587 TSK))
5588 return true;
5589
Douglas Gregor952b0172010-02-11 01:04:33 +00005590 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00005591 if (!RecordDef)
5592 return true;
5593 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005594 }
5595
Douglas Gregor0d035142009-10-27 18:42:08 +00005596 // Instantiate all of the members of the class.
5597 InstantiateClassMembers(NameLoc, RecordDef,
5598 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005599
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005600 if (TSK == TSK_ExplicitInstantiationDefinition)
5601 MarkVTableUsed(NameLoc, RecordDef, true);
5602
Mike Stump390b4cc2009-05-16 07:39:55 +00005603 // FIXME: We don't have any representation for explicit instantiations of
5604 // member classes. Such a representation is not needed for compilation, but it
5605 // should be available for clients that want to see all of the declarations in
5606 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005607 return TagD;
5608}
5609
John McCallf312b1e2010-08-26 23:41:50 +00005610DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
5611 SourceLocation ExternLoc,
5612 SourceLocation TemplateLoc,
5613 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00005614 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00005615 // TODO: check if/when DNInfo should replace Name.
5616 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
5617 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005618 if (!Name) {
5619 if (!D.isInvalidType())
5620 Diag(D.getDeclSpec().getSourceRange().getBegin(),
5621 diag::err_explicit_instantiation_requires_name)
5622 << D.getDeclSpec().getSourceRange()
5623 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005624
Douglas Gregord5a423b2009-09-25 18:43:00 +00005625 return true;
5626 }
5627
5628 // The scope passed in may not be a decl scope. Zip up the scope tree until
5629 // we find one that is.
5630 while ((S->getFlags() & Scope::DeclScope) == 0 ||
5631 (S->getFlags() & Scope::TemplateParamScope) != 0)
5632 S = S->getParent();
5633
5634 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00005635 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
5636 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005637 if (R.isNull())
5638 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005639
Douglas Gregord5a423b2009-09-25 18:43:00 +00005640 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
5641 // Cannot explicitly instantiate a typedef.
5642 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
5643 << Name;
5644 return true;
5645 }
5646
Douglas Gregor663b5a02009-10-14 20:14:33 +00005647 // C++0x [temp.explicit]p1:
5648 // [...] An explicit instantiation of a function template shall not use the
5649 // inline or constexpr specifiers.
5650 // Presumably, this also applies to member functions of class templates as
5651 // well.
5652 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005653 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00005654 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00005655 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005656
Douglas Gregor663b5a02009-10-14 20:14:33 +00005657 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005658
Douglas Gregor558c0322009-10-14 23:41:34 +00005659 // C++0x [temp.explicit]p2:
5660 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005661 // definition and an explicit instantiation declaration. An explicit
5662 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00005663 TemplateSpecializationKind TSK
5664 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5665 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005666
Abramo Bagnara25777432010-08-11 22:01:17 +00005667 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00005668 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005669
5670 if (!R->isFunctionType()) {
5671 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005672 // A [...] static data member of a class template can be explicitly
5673 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005674 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00005675 if (Previous.isAmbiguous())
5676 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005677
John McCall1bcee0a2009-12-02 08:25:40 +00005678 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005679 if (!Prev || !Prev->isStaticDataMember()) {
5680 // We expect to see a data data member here.
5681 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
5682 << Name;
5683 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5684 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00005685 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00005686 return true;
5687 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005688
Douglas Gregord5a423b2009-09-25 18:43:00 +00005689 if (!Prev->getInstantiatedFromStaticDataMember()) {
5690 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005691 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005692 diag::err_explicit_instantiation_data_member_not_instantiated)
5693 << Prev;
5694 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
5695 // FIXME: Can we provide a note showing where this was declared?
5696 return true;
5697 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005698
Douglas Gregor558c0322009-10-14 23:41:34 +00005699 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005700 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005701 // or a static data member of a class template specialization, the name of
5702 // the class template specialization in the qualified-id for the member
5703 // name shall be a simple-template-id.
5704 //
5705 // C++98 has the same restriction, just worded differently.
5706 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005707 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005708 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005709 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005710
Douglas Gregor558c0322009-10-14 23:41:34 +00005711 // Check the scope of this explicit instantiation.
5712 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005713
Douglas Gregor454885e2009-10-15 15:54:05 +00005714 // Verify that it is okay to explicitly instantiate here.
5715 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
5716 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005717 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005718 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00005719 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005720 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005721 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005722 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005723 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005724 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005725
Douglas Gregord5a423b2009-09-25 18:43:00 +00005726 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005727 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005728 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005729 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005730
Douglas Gregord5a423b2009-09-25 18:43:00 +00005731 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00005732 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005733 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005734
5735 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00005736 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00005737 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00005738 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005739 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
5740 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00005741 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
5742 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00005743 ASTTemplateArgsPtr TemplateArgsPtr(*this,
5744 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00005745 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00005746 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00005747 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00005748 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00005749 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005750
Douglas Gregord5a423b2009-09-25 18:43:00 +00005751 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005752 // A [...] function [...] can be explicitly instantiated from its template.
5753 // A member function [...] of a class template can be explicitly
5754 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005755 // template.
John McCallc373d482010-01-27 01:50:18 +00005756 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005757 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5758 P != PEnd; ++P) {
5759 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00005760 if (!HasExplicitTemplateArgs) {
5761 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
5762 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
5763 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00005764
John McCallc373d482010-01-27 01:50:18 +00005765 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00005766 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
5767 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00005768 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00005769 }
5770 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005771
Douglas Gregord5a423b2009-09-25 18:43:00 +00005772 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
5773 if (!FunTmpl)
5774 continue;
5775
John McCall5769d612010-02-08 23:07:23 +00005776 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005777 FunctionDecl *Specialization = 0;
5778 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005779 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00005780 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005781 R, Specialization, Info)) {
5782 // FIXME: Keep track of almost-matches?
5783 (void)TDK;
5784 continue;
5785 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005786
John McCallc373d482010-01-27 01:50:18 +00005787 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005788 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005789
Douglas Gregord5a423b2009-09-25 18:43:00 +00005790 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005791 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005792 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005793 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005794 PDiag(diag::err_explicit_instantiation_not_known) << Name,
5795 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
5796 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00005797
John McCallc373d482010-01-27 01:50:18 +00005798 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00005799 return true;
John McCallc373d482010-01-27 01:50:18 +00005800
5801 // Ignore access control bits, we don't need them for redeclaration checking.
5802 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005803
Douglas Gregor0a897e32009-10-15 17:21:20 +00005804 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005805 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005806 diag::err_explicit_instantiation_member_function_not_instantiated)
5807 << Specialization
5808 << (Specialization->getTemplateSpecializationKind() ==
5809 TSK_ExplicitSpecialization);
5810 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
5811 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005812 }
5813
Douglas Gregor0a897e32009-10-15 17:21:20 +00005814 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00005815 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
5816 PrevDecl = Specialization;
5817
Douglas Gregor0a897e32009-10-15 17:21:20 +00005818 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005819 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005820 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005821 PrevDecl,
5822 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00005823 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005824 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00005825 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005826
Douglas Gregor0a897e32009-10-15 17:21:20 +00005827 // FIXME: We may still want to build some representation of this
5828 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005829 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005830 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00005831 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00005832
5833 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005834
Douglas Gregor0a897e32009-10-15 17:21:20 +00005835 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005836 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005837
Douglas Gregor558c0322009-10-14 23:41:34 +00005838 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005839 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005840 // or a static data member of a class template specialization, the name of
5841 // the class template specialization in the qualified-id for the member
5842 // name shall be a simple-template-id.
5843 //
5844 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005845 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005846 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005847 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00005848 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005849 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005850 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005851 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005852
Douglas Gregor558c0322009-10-14 23:41:34 +00005853 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005854 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00005855 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005856 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00005857 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005858
Douglas Gregord5a423b2009-09-25 18:43:00 +00005859 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00005860 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005861}
5862
John McCallf312b1e2010-08-26 23:41:50 +00005863TypeResult
John McCallc4e70192009-09-11 04:59:25 +00005864Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
5865 const CXXScopeSpec &SS, IdentifierInfo *Name,
5866 SourceLocation TagLoc, SourceLocation NameLoc) {
5867 // This has to hold, because SS is expected to be defined.
5868 assert(Name && "Expected a name in a dependent tag");
5869
5870 NestedNameSpecifier *NNS
5871 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
5872 if (!NNS)
5873 return true;
5874
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005875 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00005876
Douglas Gregor48c89f42010-04-24 16:38:41 +00005877 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
5878 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005879 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00005880 return true;
5881 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005882
5883 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
John McCallb3d87482010-08-24 05:47:05 +00005884 return ParsedType::make(Context.getDependentNameType(Kwd, NNS, Name));
John McCallc4e70192009-09-11 04:59:25 +00005885}
5886
John McCallf312b1e2010-08-26 23:41:50 +00005887TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005888Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
5889 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005890 SourceLocation IdLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00005891 NestedNameSpecifier *NNS
Douglas Gregord57959a2009-03-27 23:10:48 +00005892 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
5893 if (!NNS)
5894 return true;
5895
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005896 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
5897 !getLangOptions().CPlusPlus0x)
5898 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005899 << FixItHint::CreateRemoval(TypenameLoc);
5900
Douglas Gregor107de902010-04-24 15:35:55 +00005901 QualType T = CheckTypenameType(ETK_Typename, NNS, II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00005902 TypenameLoc, SS.getRange(), IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00005903 if (T.isNull())
5904 return true;
John McCall63b43852010-04-29 23:50:39 +00005905
5906 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
5907 if (isa<DependentNameType>(T)) {
5908 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00005909 TL.setKeywordLoc(TypenameLoc);
5910 TL.setQualifierRange(SS.getRange());
5911 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00005912 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005913 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00005914 TL.setKeywordLoc(TypenameLoc);
5915 TL.setQualifierRange(SS.getRange());
5916 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00005917 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005918
John McCallb3d87482010-08-24 05:47:05 +00005919 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00005920}
5921
John McCallf312b1e2010-08-26 23:41:50 +00005922TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005923Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
5924 const CXXScopeSpec &SS, SourceLocation TemplateLoc,
John McCallb3d87482010-08-24 05:47:05 +00005925 ParsedType Ty) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005926 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
5927 !getLangOptions().CPlusPlus0x)
5928 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005929 << FixItHint::CreateRemoval(TypenameLoc);
5930
John McCall4e449832010-05-28 23:32:21 +00005931 TypeSourceInfo *InnerTSI = 0;
5932 QualType T = GetTypeFromParser(Ty, &InnerTSI);
John McCall4e449832010-05-28 23:32:21 +00005933
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005934 assert(isa<TemplateSpecializationType>(T) &&
John McCall4e449832010-05-28 23:32:21 +00005935 "Expected a template specialization type");
Douglas Gregor17343172009-04-01 00:28:59 +00005936
Douglas Gregor6946baf2009-09-02 13:05:45 +00005937 if (computeDeclContext(SS, false)) {
5938 // If we can compute a declaration context, then the "typename"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005939 // keyword was superfluous. Just build an ElaboratedType to keep
Douglas Gregor6946baf2009-09-02 13:05:45 +00005940 // track of the nested-name-specifier.
John McCall4e449832010-05-28 23:32:21 +00005941
5942 // Push the inner type, preserving its source locations if possible.
5943 TypeLocBuilder Builder;
5944 if (InnerTSI)
5945 Builder.pushFullCopy(InnerTSI->getTypeLoc());
5946 else
Douglas Gregorc21c7e92011-01-25 19:13:18 +00005947 Builder.push<TemplateSpecializationTypeLoc>(T).initialize(Context,
5948 TemplateLoc);
John McCall4e449832010-05-28 23:32:21 +00005949
Abramo Bagnara22f638a2010-08-10 13:46:45 +00005950 /* Note: NNS already embedded in template specialization type T. */
5951 T = Context.getElaboratedType(ETK_Typename, /*NNS=*/0, T);
John McCall4e449832010-05-28 23:32:21 +00005952 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
5953 TL.setKeywordLoc(TypenameLoc);
5954 TL.setQualifierRange(SS.getRange());
5955
5956 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
John McCallb3d87482010-08-24 05:47:05 +00005957 return CreateParsedType(T, TSI);
Douglas Gregor6946baf2009-09-02 13:05:45 +00005958 }
Mike Stump1eb44332009-09-09 15:08:12 +00005959
John McCall33500952010-06-11 00:33:02 +00005960 // TODO: it's really silly that we make a template specialization
5961 // type earlier only to drop it again here.
John McCallf4c73712011-01-19 06:33:43 +00005962 const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T);
John McCall33500952010-06-11 00:33:02 +00005963 DependentTemplateName *DTN =
5964 TST->getTemplateName().getAsDependentTemplateName();
5965 assert(DTN && "dependent template has non-dependent name?");
Abramo Bagnara22f638a2010-08-10 13:46:45 +00005966 assert(DTN->getQualifier()
5967 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
5968 T = Context.getDependentTemplateSpecializationType(ETK_Typename,
5969 DTN->getQualifier(),
John McCall33500952010-06-11 00:33:02 +00005970 DTN->getIdentifier(),
5971 TST->getNumArgs(),
5972 TST->getArgs());
John McCall63b43852010-04-29 23:50:39 +00005973 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
John McCall33500952010-06-11 00:33:02 +00005974 DependentTemplateSpecializationTypeLoc TL =
5975 cast<DependentTemplateSpecializationTypeLoc>(TSI->getTypeLoc());
5976 if (InnerTSI) {
5977 TemplateSpecializationTypeLoc TSTL =
5978 cast<TemplateSpecializationTypeLoc>(InnerTSI->getTypeLoc());
5979 TL.setLAngleLoc(TSTL.getLAngleLoc());
5980 TL.setRAngleLoc(TSTL.getRAngleLoc());
5981 for (unsigned I = 0, E = TST->getNumArgs(); I != E; ++I)
5982 TL.setArgLocInfo(I, TSTL.getArgLocInfo(I));
5983 } else {
Douglas Gregorc21c7e92011-01-25 19:13:18 +00005984 // FIXME: Poor source-location information here.
5985 TL.initializeLocal(Context, TemplateLoc);
John McCall33500952010-06-11 00:33:02 +00005986 }
John McCall4e449832010-05-28 23:32:21 +00005987 TL.setKeywordLoc(TypenameLoc);
5988 TL.setQualifierRange(SS.getRange());
John McCallb3d87482010-08-24 05:47:05 +00005989 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00005990}
5991
Douglas Gregord57959a2009-03-27 23:10:48 +00005992/// \brief Build the type that describes a C++ typename specifier,
5993/// e.g., "typename T::type".
5994QualType
Douglas Gregor107de902010-04-24 15:35:55 +00005995Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
5996 NestedNameSpecifier *NNS, const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00005997 SourceLocation KeywordLoc, SourceRange NNSRange,
5998 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00005999 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00006000 SS.MakeTrivial(Context, NNS, NNSRange);
Douglas Gregord57959a2009-03-27 23:10:48 +00006001
John McCall77bb1aa2010-05-01 00:40:08 +00006002 DeclContext *Ctx = computeDeclContext(SS);
6003 if (!Ctx) {
6004 // If the nested-name-specifier is dependent and couldn't be
6005 // resolved to a type, build a typename type.
6006 assert(NNS->isDependent());
6007 return Context.getDependentNameType(Keyword, NNS, &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006008 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006009
John McCall77bb1aa2010-05-01 00:40:08 +00006010 // If the nested-name-specifier refers to the current instantiation,
6011 // the "typename" keyword itself is superfluous. In C++03, the
6012 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6013 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006014 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006015
John McCall77bb1aa2010-05-01 00:40:08 +00006016 if (RequireCompleteDeclContext(SS, Ctx))
6017 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006018
6019 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006020 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006021 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006022 unsigned DiagID = 0;
6023 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006024 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006025 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006026 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006027 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006028
6029 case LookupResult::FoundUnresolvedValue: {
6030 // We found a using declaration that is a value. Most likely, the using
6031 // declaration itself is meant to have the 'typename' keyword.
6032 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(),
6033 IILoc);
6034 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6035 << Name << Ctx << FullRange;
6036 if (UnresolvedUsingValueDecl *Using
6037 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006038 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006039 Diag(Loc, diag::note_using_value_decl_missing_typename)
6040 << FixItHint::CreateInsertion(Loc, "typename ");
6041 }
6042 }
6043 // Fall through to create a dependent typename type, from which we can recover
6044 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006045
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006046 case LookupResult::NotFoundInCurrentInstantiation:
6047 // Okay, it's a member of an unknown instantiation.
Douglas Gregor107de902010-04-24 15:35:55 +00006048 return Context.getDependentNameType(Keyword, NNS, &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006049
6050 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006051 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006052 // We found a type. Build an ElaboratedType, since the
6053 // typename-specifier was just sugar.
6054 return Context.getElaboratedType(ETK_Typename, NNS,
6055 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006056 }
6057
6058 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006059 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006060 break;
6061
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006062
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006063 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006064 return QualType();
6065
Douglas Gregord57959a2009-03-27 23:10:48 +00006066 case LookupResult::FoundOverloaded:
6067 DiagID = diag::err_typename_nested_not_type;
6068 Referenced = *Result.begin();
6069 break;
6070
John McCall6e247262009-10-10 05:48:19 +00006071 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006072 return QualType();
6073 }
6074
6075 // If we get here, it's because name lookup did not find a
6076 // type. Emit an appropriate diagnostic and return an error.
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006077 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(),
6078 IILoc);
6079 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006080 if (Referenced)
6081 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6082 << Name;
6083 return QualType();
6084}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006085
6086namespace {
6087 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006088 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006089 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006090 SourceLocation Loc;
6091 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006092
Douglas Gregor4a959d82009-08-06 16:20:37 +00006093 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006094 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006095
Mike Stump1eb44332009-09-09 15:08:12 +00006096 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006097 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006098 DeclarationName Entity)
6099 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006100 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006101
6102 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006103 /// transformed.
6104 ///
6105 /// For the purposes of type reconstruction, a type has already been
6106 /// transformed if it is NULL or if it is not dependent.
6107 bool AlreadyTransformed(QualType T) {
6108 return T.isNull() || !T->isDependentType();
6109 }
Mike Stump1eb44332009-09-09 15:08:12 +00006110
6111 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006112 /// rebuilt.
6113 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006114
Douglas Gregor4a959d82009-08-06 16:20:37 +00006115 /// \brief Returns the name of the entity whose type is being rebuilt.
6116 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006117
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006118 /// \brief Sets the "base" location and entity when that
6119 /// information is known based on another transformation.
6120 void setBase(SourceLocation Loc, DeclarationName Entity) {
6121 this->Loc = Loc;
6122 this->Entity = Entity;
6123 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006124 };
6125}
6126
Douglas Gregor4a959d82009-08-06 16:20:37 +00006127/// \brief Rebuilds a type within the context of the current instantiation.
6128///
Mike Stump1eb44332009-09-09 15:08:12 +00006129/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006130/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006131/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006132/// partial specialization thereof). This routine will rebuild that type now
6133/// that we have entered the declarator's scope, which may produce different
6134/// canonical types, e.g.,
6135///
6136/// \code
6137/// template<typename T>
6138/// struct X {
6139/// typedef T* pointer;
6140/// pointer data();
6141/// };
6142///
6143/// template<typename T>
6144/// typename X<T>::pointer X<T>::data() { ... }
6145/// \endcode
6146///
Douglas Gregor4714c122010-03-31 17:34:00 +00006147/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006148/// since we do not know that we can look into X<T> when we parsed the type.
6149/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006150/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006151/// as the canonical type of T*, allowing the return types of the out-of-line
6152/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006153TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6154 SourceLocation Loc,
6155 DeclarationName Name) {
6156 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006157 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006158
Douglas Gregor4a959d82009-08-06 16:20:37 +00006159 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6160 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006161}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006162
John McCall60d7b3a2010-08-24 06:29:42 +00006163ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006164 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6165 DeclarationName());
6166 return Rebuilder.TransformExpr(E);
6167}
6168
John McCall63b43852010-04-29 23:50:39 +00006169bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
6170 if (SS.isInvalid()) return true;
John McCall31f17ec2010-04-27 00:57:59 +00006171
6172 NestedNameSpecifier *NNS = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
6173 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6174 DeclarationName());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006175 NestedNameSpecifier *Rebuilt =
John McCall31f17ec2010-04-27 00:57:59 +00006176 Rebuilder.TransformNestedNameSpecifier(NNS, SS.getRange());
John McCall63b43852010-04-29 23:50:39 +00006177 if (!Rebuilt) return true;
6178
Douglas Gregorc34348a2011-02-24 17:54:50 +00006179 SS.MakeTrivial(Context, Rebuilt, SS.getRange());
John McCall63b43852010-04-29 23:50:39 +00006180 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006181}
6182
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006183/// \brief Produces a formatted string that describes the binding of
6184/// template parameters to template arguments.
6185std::string
6186Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6187 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006188 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006189}
6190
6191std::string
6192Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6193 const TemplateArgument *Args,
6194 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006195 llvm::SmallString<128> Str;
6196 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006197
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006198 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006199 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006200
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006201 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006202 if (I >= NumArgs)
6203 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006204
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006205 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006206 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006207 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006208 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006209
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006210 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006211 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006212 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006213 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006214 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006215
Douglas Gregor87dd6972010-12-20 16:52:59 +00006216 Out << " = ";
6217 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006218 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006219
6220 Out << ']';
6221 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006222}