blob: 90b0d7e29b58e1d1037aab5f5431cb4ffc6e08e2 [file] [log] [blame]
Douglas Gregor72c3f312008-12-05 18:15:24 +00001//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Douglas Gregor99ebf652009-02-27 19:31:52 +00007//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00008//
9// This file implements semantic analysis for C++ templates.
Douglas Gregor99ebf652009-02-27 19:31:52 +000010//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +000011
John McCall2d887082010-08-25 22:03:47 +000012#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Lookup.h"
John McCall5f1e0942010-08-24 08:50:51 +000014#include "clang/Sema/Scope.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
John McCall2a7fb272010-08-25 05:32:35 +000016#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor4a959d82009-08-06 16:20:37 +000017#include "TreeTransform.h"
Douglas Gregorddc29e12009-02-06 22:42:48 +000018#include "clang/AST/ASTContext.h"
Douglas Gregor898574e2008-12-05 23:32:09 +000019#include "clang/AST/Expr.h"
Douglas Gregorcc45cb32009-02-11 19:52:55 +000020#include "clang/AST/ExprCXX.h"
John McCall92b7f702010-03-11 07:50:04 +000021#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000022#include "clang/AST/DeclTemplate.h"
John McCall4e2cbb22010-10-20 05:44:58 +000023#include "clang/AST/RecursiveASTVisitor.h"
Douglas Gregor5f3aeb62010-10-13 00:27:52 +000024#include "clang/AST/TypeVisitor.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
26#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000027#include "clang/Basic/LangOptions.h"
Douglas Gregord5a423b2009-09-25 18:43:00 +000028#include "clang/Basic/PartialDiagnostic.h"
Douglas Gregorbf4ea562009-09-15 16:23:51 +000029#include "llvm/ADT/StringExtras.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000030using namespace clang;
John McCall2a7fb272010-08-25 05:32:35 +000031using namespace sema;
Douglas Gregor72c3f312008-12-05 18:15:24 +000032
John McCall78b81052010-11-10 02:40:36 +000033// Exported for use by Parser.
34SourceRange
35clang::getTemplateParamsRange(TemplateParameterList const * const *Ps,
36 unsigned N) {
37 if (!N) return SourceRange();
38 return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc());
39}
40
Douglas Gregor2dd078a2009-09-02 22:59:36 +000041/// \brief Determine whether the declaration found is acceptable as the name
42/// of a template and, if so, return that template declaration. Otherwise,
43/// returns NULL.
John McCallad00b772010-06-16 08:42:20 +000044static NamedDecl *isAcceptableTemplateName(ASTContext &Context,
45 NamedDecl *Orig) {
46 NamedDecl *D = Orig->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregor2dd078a2009-09-02 22:59:36 +000048 if (isa<TemplateDecl>(D))
John McCallad00b772010-06-16 08:42:20 +000049 return Orig;
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor2dd078a2009-09-02 22:59:36 +000051 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
52 // C++ [temp.local]p1:
53 // Like normal (non-template) classes, class templates have an
54 // injected-class-name (Clause 9). The injected-class-name
55 // can be used with or without a template-argument-list. When
56 // it is used without a template-argument-list, it is
57 // equivalent to the injected-class-name followed by the
58 // template-parameters of the class template enclosed in
59 // <>. When it is used with a template-argument-list, it
60 // refers to the specified class template specialization,
61 // which could be the current specialization or another
62 // specialization.
63 if (Record->isInjectedClassName()) {
Douglas Gregor542b5482009-10-14 17:30:58 +000064 Record = cast<CXXRecordDecl>(Record->getDeclContext());
Douglas Gregor2dd078a2009-09-02 22:59:36 +000065 if (Record->getDescribedClassTemplate())
66 return Record->getDescribedClassTemplate();
67
68 if (ClassTemplateSpecializationDecl *Spec
69 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
70 return Spec->getSpecializedTemplate();
71 }
Mike Stump1eb44332009-09-09 15:08:12 +000072
Douglas Gregor2dd078a2009-09-02 22:59:36 +000073 return 0;
74 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Douglas Gregor2dd078a2009-09-02 22:59:36 +000076 return 0;
77}
78
John McCallf7a1a742009-11-24 19:00:30 +000079static void FilterAcceptableTemplateNames(ASTContext &C, LookupResult &R) {
Douglas Gregor01e56ae2010-04-12 20:54:26 +000080 // The set of class templates we've already seen.
81 llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates;
John McCallf7a1a742009-11-24 19:00:30 +000082 LookupResult::Filter filter = R.makeFilter();
83 while (filter.hasNext()) {
84 NamedDecl *Orig = filter.next();
John McCallad00b772010-06-16 08:42:20 +000085 NamedDecl *Repl = isAcceptableTemplateName(C, Orig);
John McCallf7a1a742009-11-24 19:00:30 +000086 if (!Repl)
87 filter.erase();
Douglas Gregor01e56ae2010-04-12 20:54:26 +000088 else if (Repl != Orig) {
89
90 // C++ [temp.local]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000091 // A lookup that finds an injected-class-name (10.2) can result in an
Douglas Gregor01e56ae2010-04-12 20:54:26 +000092 // ambiguity in certain cases (for example, if it is found in more than
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000093 // one base class). If all of the injected-class-names that are found
94 // refer to specializations of the same class template, and if the name
95 // is followed by a template-argument-list, the reference refers to the
96 // class template itself and not a specialization thereof, and is not
Douglas Gregor01e56ae2010-04-12 20:54:26 +000097 // ambiguous.
98 //
99 // FIXME: Will we eventually have to do the same for alias templates?
100 if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
101 if (!ClassTemplates.insert(ClassTmpl)) {
102 filter.erase();
103 continue;
104 }
John McCall8ba66912010-08-13 07:02:08 +0000105
106 // FIXME: we promote access to public here as a workaround to
107 // the fact that LookupResult doesn't let us remember that we
108 // found this template through a particular injected class name,
109 // which means we end up doing nasty things to the invariants.
110 // Pretending that access is public is *much* safer.
111 filter.replace(Repl, AS_public);
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000112 }
John McCallf7a1a742009-11-24 19:00:30 +0000113 }
114 filter.done();
115}
116
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000117TemplateNameKind Sema::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000118 CXXScopeSpec &SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000119 bool hasTemplateKeyword,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000120 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +0000121 ParsedType ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000122 bool EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000123 TemplateTy &TemplateResult,
124 bool &MemberOfUnknownSpecialization) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +0000125 assert(getLangOptions().CPlusPlus && "No template names in C!");
126
Douglas Gregor014e88d2009-11-03 23:16:33 +0000127 DeclarationName TName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000128 MemberOfUnknownSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000129
Douglas Gregor014e88d2009-11-03 23:16:33 +0000130 switch (Name.getKind()) {
131 case UnqualifiedId::IK_Identifier:
132 TName = DeclarationName(Name.Identifier);
133 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000134
Douglas Gregor014e88d2009-11-03 23:16:33 +0000135 case UnqualifiedId::IK_OperatorFunctionId:
136 TName = Context.DeclarationNames.getCXXOperatorName(
137 Name.OperatorFunctionId.Operator);
138 break;
139
Sean Hunte6252d12009-11-28 08:58:14 +0000140 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +0000141 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
142 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000143
Douglas Gregor014e88d2009-11-03 23:16:33 +0000144 default:
145 return TNK_Non_template;
146 }
Mike Stump1eb44332009-09-09 15:08:12 +0000147
John McCallb3d87482010-08-24 05:47:05 +0000148 QualType ObjectType = ObjectTypePtr.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000149
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000150 LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000151 LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000152 LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
153 MemberOfUnknownSpecialization);
John McCall67d22fb2010-08-28 20:17:00 +0000154 if (R.empty()) return TNK_Non_template;
155 if (R.isAmbiguous()) {
156 // Suppress diagnostics; we'll redo this lookup later.
John McCallb8592062010-08-13 02:23:42 +0000157 R.suppressDiagnostics();
John McCall67d22fb2010-08-28 20:17:00 +0000158
159 // FIXME: we might have ambiguous templates, in which case we
160 // should at least parse them properly!
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000161 return TNK_Non_template;
John McCallb8592062010-08-13 02:23:42 +0000162 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000163
John McCall0bd6feb2009-12-02 08:04:21 +0000164 TemplateName Template;
165 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000166
John McCall0bd6feb2009-12-02 08:04:21 +0000167 unsigned ResultCount = R.end() - R.begin();
168 if (ResultCount > 1) {
169 // We assume that we'll preserve the qualifier from a function
170 // template name in other ways.
171 Template = Context.getOverloadedTemplateName(R.begin(), R.end());
172 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000173
174 // We'll do this lookup again later.
175 R.suppressDiagnostics();
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000176 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000177 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
178
179 if (SS.isSet() && !SS.isInvalid()) {
180 NestedNameSpecifier *Qualifier
181 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Abramo Bagnara7c153532010-08-06 12:11:11 +0000182 Template = Context.getQualifiedTemplateName(Qualifier,
183 hasTemplateKeyword, TD);
John McCall0bd6feb2009-12-02 08:04:21 +0000184 } else {
185 Template = TemplateName(TD);
186 }
187
John McCallb8592062010-08-13 02:23:42 +0000188 if (isa<FunctionTemplateDecl>(TD)) {
John McCall0bd6feb2009-12-02 08:04:21 +0000189 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000190
191 // We'll do this lookup again later.
192 R.suppressDiagnostics();
193 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000194 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD));
195 TemplateKind = TNK_Type_template;
196 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000197 }
Mike Stump1eb44332009-09-09 15:08:12 +0000198
John McCall0bd6feb2009-12-02 08:04:21 +0000199 TemplateResult = TemplateTy::make(Template);
200 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000201}
202
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000203bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Douglas Gregor84d0a192010-01-12 21:28:44 +0000204 SourceLocation IILoc,
205 Scope *S,
206 const CXXScopeSpec *SS,
207 TemplateTy &SuggestedTemplate,
208 TemplateNameKind &SuggestedKind) {
209 // We can't recover unless there's a dependent scope specifier preceding the
210 // template name.
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000211 // FIXME: Typo correction?
Douglas Gregor84d0a192010-01-12 21:28:44 +0000212 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
213 computeDeclContext(*SS))
214 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000215
Douglas Gregor84d0a192010-01-12 21:28:44 +0000216 // The code is missing a 'template' keyword prior to the dependent template
217 // name.
218 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
219 Diag(IILoc, diag::err_template_kw_missing)
220 << Qualifier << II.getName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000221 << FixItHint::CreateInsertion(IILoc, "template ");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000222 SuggestedTemplate
Douglas Gregor84d0a192010-01-12 21:28:44 +0000223 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
224 SuggestedKind = TNK_Dependent_template_name;
225 return true;
226}
227
John McCallf7a1a742009-11-24 19:00:30 +0000228void Sema::LookupTemplateName(LookupResult &Found,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000229 Scope *S, CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +0000230 QualType ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000231 bool EnteringContext,
232 bool &MemberOfUnknownSpecialization) {
John McCallf7a1a742009-11-24 19:00:30 +0000233 // Determine where to perform name lookup
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000234 MemberOfUnknownSpecialization = false;
John McCallf7a1a742009-11-24 19:00:30 +0000235 DeclContext *LookupCtx = 0;
236 bool isDependent = false;
237 if (!ObjectType.isNull()) {
238 // This nested-name-specifier occurs in a member access expression, e.g.,
239 // x->B::f, and we are looking into the type of the object.
240 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
241 LookupCtx = computeDeclContext(ObjectType);
242 isDependent = ObjectType->isDependentType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000243 assert((isDependent || !ObjectType->isIncompleteType()) &&
John McCallf7a1a742009-11-24 19:00:30 +0000244 "Caller should have completed object type");
245 } else if (SS.isSet()) {
246 // This nested-name-specifier occurs after another nested-name-specifier,
247 // so long into the context associated with the prior nested-name-specifier.
248 LookupCtx = computeDeclContext(SS, EnteringContext);
249 isDependent = isDependentScopeSpecifier(SS);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000250
John McCallf7a1a742009-11-24 19:00:30 +0000251 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000252 if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
John McCallf7a1a742009-11-24 19:00:30 +0000253 return;
254 }
255
256 bool ObjectTypeSearchedInScope = false;
257 if (LookupCtx) {
258 // Perform "qualified" name lookup into the declaration context we
259 // computed, which is either the type of the base of a member access
260 // expression or the declaration context associated with a prior
261 // nested-name-specifier.
262 LookupQualifiedName(Found, LookupCtx);
263
264 if (!ObjectType.isNull() && Found.empty()) {
265 // C++ [basic.lookup.classref]p1:
266 // In a class member access expression (5.2.5), if the . or -> token is
267 // immediately followed by an identifier followed by a <, the
268 // identifier must be looked up to determine whether the < is the
269 // beginning of a template argument list (14.2) or a less-than operator.
270 // The identifier is first looked up in the class of the object
271 // expression. If the identifier is not found, it is then looked up in
272 // the context of the entire postfix-expression and shall name a class
273 // or function template.
John McCallf7a1a742009-11-24 19:00:30 +0000274 if (S) LookupName(Found, S);
275 ObjectTypeSearchedInScope = true;
276 }
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000277 } else if (isDependent && (!S || ObjectType.isNull())) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000278 // We cannot look into a dependent object type or nested nme
279 // specifier.
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000280 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000281 return;
282 } else {
283 // Perform unqualified name lookup in the current scope.
284 LookupName(Found, S);
285 }
286
Douglas Gregor2e933882010-01-12 17:06:20 +0000287 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000288 // If we did not find any names, attempt to correct any typos.
289 DeclarationName Name = Found.getLookupName();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000290 if (DeclarationName Corrected = CorrectTypo(Found, S, &SS, LookupCtx,
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000291 false, CTC_CXXCasts)) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000292 FilterAcceptableTemplateNames(Context, Found);
John McCallad00b772010-06-16 08:42:20 +0000293 if (!Found.empty()) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000294 if (LookupCtx)
295 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
296 << Name << LookupCtx << Found.getLookupName() << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +0000297 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000298 Found.getLookupName().getAsString());
299 else
300 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
301 << Name << Found.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000302 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000303 Found.getLookupName().getAsString());
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000304 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
305 Diag(Template->getLocation(), diag::note_previous_decl)
306 << Template->getDeclName();
John McCallad00b772010-06-16 08:42:20 +0000307 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000308 } else {
309 Found.clear();
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000310 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000311 }
312 }
313
John McCallf7a1a742009-11-24 19:00:30 +0000314 FilterAcceptableTemplateNames(Context, Found);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000315 if (Found.empty()) {
316 if (isDependent)
317 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000318 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000319 }
John McCallf7a1a742009-11-24 19:00:30 +0000320
321 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
322 // C++ [basic.lookup.classref]p1:
323 // [...] If the lookup in the class of the object expression finds a
324 // template, the name is also looked up in the context of the entire
325 // postfix-expression and [...]
326 //
327 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
328 LookupOrdinaryName);
329 LookupName(FoundOuter, S);
330 FilterAcceptableTemplateNames(Context, FoundOuter);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000331
John McCallf7a1a742009-11-24 19:00:30 +0000332 if (FoundOuter.empty()) {
333 // - if the name is not found, the name found in the class of the
334 // object expression is used, otherwise
335 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) {
336 // - if the name is found in the context of the entire
337 // postfix-expression and does not name a class template, the name
338 // found in the class of the object expression is used, otherwise
John McCallad00b772010-06-16 08:42:20 +0000339 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000340 // - if the name found is a class template, it must refer to the same
341 // entity as the one found in the class of the object expression,
342 // otherwise the program is ill-formed.
343 if (!Found.isSingleResult() ||
344 Found.getFoundDecl()->getCanonicalDecl()
345 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000346 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000347 diag::ext_nested_name_member_ref_lookup_ambiguous)
348 << Found.getLookupName()
349 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000350 Diag(Found.getRepresentativeDecl()->getLocation(),
351 diag::note_ambig_member_ref_object_type)
352 << ObjectType;
353 Diag(FoundOuter.getFoundDecl()->getLocation(),
354 diag::note_ambig_member_ref_scope);
355
356 // Recover by taking the template that we found in the object
357 // expression's type.
358 }
359 }
360 }
361}
362
John McCall2f841ba2009-12-02 03:53:29 +0000363/// ActOnDependentIdExpression - Handle a dependent id-expression that
364/// was just parsed. This is only possible with an explicit scope
365/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000366ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000367Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000368 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000369 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000370 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000371 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000372
John McCall2f841ba2009-12-02 03:53:29 +0000373 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000374 isa<CXXMethodDecl>(DC) &&
375 cast<CXXMethodDecl>(DC)->isInstance()) {
376 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000377
John McCallf7a1a742009-11-24 19:00:30 +0000378 // Since the 'this' expression is synthesized, we don't need to
379 // perform the double-lookup check.
380 NamedDecl *FirstQualifierInScope = 0;
381
John McCallaa81e162009-12-01 22:10:20 +0000382 return Owned(CXXDependentScopeMemberExpr::Create(Context,
383 /*This*/ 0, ThisType,
384 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000385 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000386 SS.getWithLocInContext(Context),
John McCallf7a1a742009-11-24 19:00:30 +0000387 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000388 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000389 TemplateArgs));
390 }
391
Abramo Bagnara25777432010-08-11 22:01:17 +0000392 return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000393}
394
John McCall60d7b3a2010-08-24 06:29:42 +0000395ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000396Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000397 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000398 const TemplateArgumentListInfo *TemplateArgs) {
399 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000400 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +0000401 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000402 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000403}
404
Douglas Gregor72c3f312008-12-05 18:15:24 +0000405/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
406/// that the template parameter 'PrevDecl' is being shadowed by a new
407/// declaration at location Loc. Returns true to indicate that this is
408/// an error, and false otherwise.
409bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000410 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000411
412 // Microsoft Visual C++ permits template parameters to be shadowed.
413 if (getLangOptions().Microsoft)
414 return false;
415
416 // C++ [temp.local]p4:
417 // A template-parameter shall not be redeclared within its
418 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000419 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000420 << cast<NamedDecl>(PrevDecl)->getDeclName();
421 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
422 return true;
423}
424
Douglas Gregor2943aed2009-03-03 04:44:36 +0000425/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000426/// the parameter D to reference the templated declaration and return a pointer
427/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000428TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
429 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
430 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000431 return Temp;
432 }
433 return 0;
434}
435
Douglas Gregorba68eca2011-01-05 17:40:24 +0000436ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
437 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000438 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000439 "Only template template arguments can be pack expansions here");
440 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
441 "Template template argument pack expansion without packs");
442 ParsedTemplateArgument Result(*this);
443 Result.EllipsisLoc = EllipsisLoc;
444 return Result;
445}
446
Douglas Gregor788cd062009-11-11 01:00:40 +0000447static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
448 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000449
Douglas Gregor788cd062009-11-11 01:00:40 +0000450 switch (Arg.getKind()) {
451 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000452 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000453 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000454 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000455 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000456 return TemplateArgumentLoc(TemplateArgument(T), DI);
457 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000458
Douglas Gregor788cd062009-11-11 01:00:40 +0000459 case ParsedTemplateArgument::NonType: {
460 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
461 return TemplateArgumentLoc(TemplateArgument(E), E);
462 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000463
Douglas Gregor788cd062009-11-11 01:00:40 +0000464 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000465 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000466 TemplateArgument TArg;
467 if (Arg.getEllipsisLoc().isValid())
468 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
469 else
470 TArg = Template;
471 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000472 Arg.getScopeSpec().getWithLocInContext(
473 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000474 Arg.getLocation(),
475 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000476 }
477 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000478
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000479 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000480 return TemplateArgumentLoc();
481}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000482
Douglas Gregor788cd062009-11-11 01:00:40 +0000483/// \brief Translates template arguments as provided by the parser
484/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000485void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
486 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000487 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000488 TemplateArgs.addArgument(translateTemplateArgument(*this,
489 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000490}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000491
Douglas Gregor72c3f312008-12-05 18:15:24 +0000492/// ActOnTypeParameter - Called when a C++ template type parameter
493/// (e.g., "typename T") has been parsed. Typename specifies whether
494/// the keyword "typename" was used to declare the type parameter
495/// (otherwise, "class" was used), and KeyLoc is the location of the
496/// "class" or "typename" keyword. ParamName is the name of the
497/// parameter (NULL indicates an unnamed template parameter) and
Douglas Gregorefed5c82010-06-16 15:23:05 +0000498/// ParamName is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000499/// If the type parameter has a default argument, it will be added
500/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000501Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
502 SourceLocation EllipsisLoc,
503 SourceLocation KeyLoc,
504 IdentifierInfo *ParamName,
505 SourceLocation ParamNameLoc,
506 unsigned Depth, unsigned Position,
507 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000508 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000509 assert(S->isTemplateParamScope() &&
510 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000511 bool Invalid = false;
512
513 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000514 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000515 LookupOrdinaryName,
516 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000517 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000518 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000519 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000520 }
521
Douglas Gregorddc29e12009-02-06 22:42:48 +0000522 SourceLocation Loc = ParamNameLoc;
523 if (!ParamName)
524 Loc = KeyLoc;
525
Douglas Gregor72c3f312008-12-05 18:15:24 +0000526 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000527 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
528 Loc, Depth, Position, ParamName, Typename,
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000529 Ellipsis);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000530 if (Invalid)
531 Param->setInvalidDecl();
532
533 if (ParamName) {
534 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000535 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000536 IdResolver.AddDecl(Param);
537 }
538
Douglas Gregor61c4d282011-01-05 15:48:55 +0000539 // C++0x [temp.param]p9:
540 // A default template-argument may be specified for any kind of
541 // template-parameter that is not a template parameter pack.
542 if (DefaultArg && Ellipsis) {
543 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
544 DefaultArg = ParsedType();
545 }
546
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000547 // Handle the default argument, if provided.
548 if (DefaultArg) {
549 TypeSourceInfo *DefaultTInfo;
550 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000551
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000552 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000553
Douglas Gregor6f526752010-12-16 08:48:57 +0000554 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000555 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000556 UPPC_DefaultArgument))
557 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000558
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000559 // Check the template argument itself.
560 if (CheckTemplateArgument(Param, DefaultTInfo)) {
561 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000562 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000563 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000564
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000565 Param->setDefaultArgument(DefaultTInfo, false);
566 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000567
John McCalld226f652010-08-21 09:40:31 +0000568 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000569}
570
Douglas Gregor2943aed2009-03-03 04:44:36 +0000571/// \brief Check that the type of a non-type template parameter is
572/// well-formed.
573///
574/// \returns the (possibly-promoted) parameter type if valid;
575/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000576QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000577Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000578 // We don't allow variably-modified types as the type of non-type template
579 // parameters.
580 if (T->isVariablyModifiedType()) {
581 Diag(Loc, diag::err_variably_modified_nontype_template_param)
582 << T;
583 return QualType();
584 }
585
Douglas Gregor2943aed2009-03-03 04:44:36 +0000586 // C++ [temp.param]p4:
587 //
588 // A non-type template-parameter shall have one of the following
589 // (optionally cv-qualified) types:
590 //
591 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000592 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000593 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000594 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000595 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000596 T->isReferenceType() ||
597 // -- pointer to member.
598 T->isMemberPointerType() ||
599 // If T is a dependent type, we can't do the check now, so we
600 // assume that it is well-formed.
601 T->isDependentType())
602 return T;
603 // C++ [temp.param]p8:
604 //
605 // A non-type template-parameter of type "array of T" or
606 // "function returning T" is adjusted to be of type "pointer to
607 // T" or "pointer to function returning T", respectively.
608 else if (T->isArrayType())
609 // FIXME: Keep the type prior to promotion?
610 return Context.getArrayDecayedType(T);
611 else if (T->isFunctionType())
612 // FIXME: Keep the type prior to promotion?
613 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000614
Douglas Gregor2943aed2009-03-03 04:44:36 +0000615 Diag(Loc, diag::err_template_nontype_parm_bad_type)
616 << T;
617
618 return QualType();
619}
620
John McCalld226f652010-08-21 09:40:31 +0000621Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
622 unsigned Depth,
623 unsigned Position,
624 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000625 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000626 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
627 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000628
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000629 assert(S->isTemplateParamScope() &&
630 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000631 bool Invalid = false;
632
633 IdentifierInfo *ParamName = D.getIdentifier();
634 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000635 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000636 LookupOrdinaryName,
637 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000638 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000639 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000640 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000641 }
642
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000643 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
644 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000645 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000646 Invalid = true;
647 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000648
Douglas Gregor10738d32010-12-23 23:51:58 +0000649 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000650 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000651 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
652 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000653 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000654 IsParameterPack, TInfo);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000655 if (Invalid)
656 Param->setInvalidDecl();
657
658 if (D.getIdentifier()) {
659 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000660 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000661 IdResolver.AddDecl(Param);
662 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000663
Douglas Gregor61c4d282011-01-05 15:48:55 +0000664 // C++0x [temp.param]p9:
665 // A default template-argument may be specified for any kind of
666 // template-parameter that is not a template parameter pack.
667 if (Default && IsParameterPack) {
668 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
669 Default = 0;
670 }
671
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000672 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000673 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000674 // Check for unexpanded parameter packs.
675 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
676 return Param;
677
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000678 TemplateArgument Converted;
679 if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) {
680 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000681 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000682 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000683
John McCall9ae2f072010-08-23 23:25:46 +0000684 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000685 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000686
John McCalld226f652010-08-21 09:40:31 +0000687 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000688}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000689
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000690/// ActOnTemplateTemplateParameter - Called when a C++ template template
691/// parameter (e.g. T in template <template <typename> class T> class array)
692/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000693Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
694 SourceLocation TmpLoc,
695 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000696 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000697 IdentifierInfo *Name,
698 SourceLocation NameLoc,
699 unsigned Depth,
700 unsigned Position,
701 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000702 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000703 assert(S->isTemplateParamScope() &&
704 "Template template parameter not in template parameter scope!");
705
706 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000707 bool IsParameterPack = EllipsisLoc.isValid();
708 // FIXME: Pack-ness is dropped
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000709 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000710 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000711 NameLoc.isInvalid()? TmpLoc : NameLoc,
712 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000713 Name, Params);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000714
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000715 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000716 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000717 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000718 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000719 IdResolver.AddDecl(Param);
720 }
721
Douglas Gregor6f526752010-12-16 08:48:57 +0000722 if (Params->size() == 0) {
723 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
724 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
725 Param->setInvalidDecl();
726 }
727
Douglas Gregor61c4d282011-01-05 15:48:55 +0000728 // C++0x [temp.param]p9:
729 // A default template-argument may be specified for any kind of
730 // template-parameter that is not a template parameter pack.
731 if (IsParameterPack && !Default.isInvalid()) {
732 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
733 Default = ParsedTemplateArgument();
734 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000735
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000736 if (!Default.isInvalid()) {
737 // Check only that we have a template template argument. We don't want to
738 // try to check well-formedness now, because our template template parameter
739 // might have dependent types in its template parameters, which we wouldn't
740 // be able to match now.
741 //
742 // If none of the template template parameter's template arguments mention
743 // other template parameters, we could actually perform more checking here.
744 // However, it isn't worth doing.
745 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
746 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
747 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
748 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000749 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000750 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000751
Douglas Gregor6f526752010-12-16 08:48:57 +0000752 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000753 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000754 DefaultArg.getArgument().getAsTemplate(),
755 UPPC_DefaultArgument))
756 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000757
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000758 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000759 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000760
John McCalld226f652010-08-21 09:40:31 +0000761 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000762}
763
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000764/// ActOnTemplateParameterList - Builds a TemplateParameterList that
765/// contains the template parameters in Params/NumParams.
766Sema::TemplateParamsTy *
767Sema::ActOnTemplateParameterList(unsigned Depth,
768 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000769 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000770 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000771 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000772 SourceLocation RAngleLoc) {
773 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000774 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000775
Douglas Gregorddc29e12009-02-06 22:42:48 +0000776 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000777 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000778 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000779}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000780
John McCallb6217662010-03-15 10:12:16 +0000781static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
782 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000783 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000784}
785
John McCallf312b1e2010-08-26 23:41:50 +0000786DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000787Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000788 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000789 IdentifierInfo *Name, SourceLocation NameLoc,
790 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000791 TemplateParameterList *TemplateParams,
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000792 AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000793 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000794 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000795 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000796 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000797
798 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000799 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000800 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000801
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000802 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
803 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000804
805 // There is no such thing as an unnamed class template.
806 if (!Name) {
807 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000808 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000809 }
810
811 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000812 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000813 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000814 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000815 if (SS.isNotEmpty() && !SS.isInvalid()) {
816 SemanticContext = computeDeclContext(SS, true);
817 if (!SemanticContext) {
818 // FIXME: Produce a reasonable diagnostic here
819 return true;
820 }
Mike Stump1eb44332009-09-09 15:08:12 +0000821
John McCall77bb1aa2010-05-01 00:40:08 +0000822 if (RequireCompleteDeclContext(SS, SemanticContext))
823 return true;
824
John McCalla24dc2e2009-11-17 02:14:36 +0000825 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000826 } else {
827 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000828 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000829 }
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Douglas Gregor57265e32010-04-12 16:00:01 +0000831 if (Previous.isAmbiguous())
832 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000833
Douglas Gregorddc29e12009-02-06 22:42:48 +0000834 NamedDecl *PrevDecl = 0;
835 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000836 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000837
Douglas Gregorddc29e12009-02-06 22:42:48 +0000838 // If there is a previous declaration with the same name, check
839 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000840 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000841 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000842
843 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000844 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000845 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000846 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000847 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
848 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000849 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000850 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
851 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
852 PrevClassTemplate
853 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
854 ->getSpecializedTemplate();
855 }
856 }
857
John McCall65c49462009-12-18 11:25:59 +0000858 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000859 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000860 // [...] When looking for a prior declaration of a class or a function
861 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000862 // function is neither a qualified name nor a template-id, scopes outside
863 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000864 if (!SS.isSet()) {
865 DeclContext *OutermostContext = CurContext;
866 while (!OutermostContext->isFileContext())
867 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000868
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000869 if (PrevDecl &&
870 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
871 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
872 SemanticContext = PrevDecl->getDeclContext();
873 } else {
874 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000875 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000876 // declaration.
877 PrevDecl = PrevClassTemplate = 0;
878 SemanticContext = OutermostContext;
879 }
John McCalle129d442009-12-17 23:21:11 +0000880 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000881
John McCalle129d442009-12-17 23:21:11 +0000882 if (CurContext->isDependentContext()) {
883 // If this is a dependent context, we don't want to link the friend
884 // class template to the template in scope, because that would perform
885 // checking of the template parameter lists that can't be performed
886 // until the outer context is instantiated.
887 PrevDecl = PrevClassTemplate = 0;
888 }
889 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
890 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000891
Douglas Gregorddc29e12009-02-06 22:42:48 +0000892 if (PrevClassTemplate) {
893 // Ensure that the template parameter lists are compatible.
894 if (!TemplateParameterListsAreEqual(TemplateParams,
895 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000896 /*Complain=*/true,
897 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000898 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000899
900 // C++ [temp.class]p4:
901 // In a redeclaration, partial specialization, explicit
902 // specialization or explicit instantiation of a class template,
903 // the class-key shall agree in kind with the original class
904 // template declaration (7.1.5.3).
905 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000906 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000907 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000908 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000909 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000910 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000911 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000912 }
913
Douglas Gregorddc29e12009-02-06 22:42:48 +0000914 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000915 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000916 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000917 Diag(NameLoc, diag::err_redefinition) << Name;
918 Diag(Def->getLocation(), diag::note_previous_definition);
919 // FIXME: Would it make sense to try to "forget" the previous
920 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000921 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000922 }
923 }
924 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
925 // Maybe we will complain about the shadowed template parameter.
926 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
927 // Just pretend that we didn't see the previous declaration.
928 PrevDecl = 0;
929 } else if (PrevDecl) {
930 // C++ [temp]p5:
931 // A class template shall not have the same name as any other
932 // template, class, function, object, enumeration, enumerator,
933 // namespace, or type in the same scope (3.3), except as specified
934 // in (14.5.4).
935 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
936 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000937 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000938 }
939
Douglas Gregord684b002009-02-10 19:49:53 +0000940 // Check the template parameter list of this declaration, possibly
941 // merging in the template parameter list from the previous class
942 // template declaration.
943 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000944 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000945 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000946 SemanticContext->isRecord() &&
947 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000948 ? TPC_ClassTemplateMember
949 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000950 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Douglas Gregor57265e32010-04-12 16:00:01 +0000952 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000953 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000954 // template out-of-line.
955 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
956 !(TUK == TUK_Friend && CurContext->isDependentContext()))
957 Diag(NameLoc, diag::err_member_def_does_not_match)
958 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000959 }
960
Mike Stump1eb44332009-09-09 15:08:12 +0000961 CXXRecordDecl *NewClass =
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000962 CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000963 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000964 PrevClassTemplate->getTemplatedDecl() : 0,
965 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000966 SetNestedNameSpecifier(NewClass, SS);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000967
968 ClassTemplateDecl *NewTemplate
969 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
970 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000971 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000972 NewClass->setDescribedClassTemplate(NewTemplate);
973
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000974 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000975 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000976 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000977 assert(T->isDependentType() && "Class template type is not dependent?");
978 (void)T;
979
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000980 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000981 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000982 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000983 PrevClassTemplate->getInstantiatedFromMemberTemplate())
984 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000985
Anders Carlsson4cbe82c2009-03-26 01:24:28 +0000986 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +0000987 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +0000988 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Douglas Gregorddc29e12009-02-06 22:42:48 +0000990 // Set the lexical context of these templates
991 NewClass->setLexicalDeclContext(CurContext);
992 NewTemplate->setLexicalDeclContext(CurContext);
993
John McCall0f434ec2009-07-31 02:45:11 +0000994 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +0000995 NewClass->startDefinition();
996
997 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000998 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000999
John McCall05b23ea2009-09-14 21:59:20 +00001000 if (TUK != TUK_Friend)
1001 PushOnScopeChains(NewTemplate, S);
1002 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001003 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001004 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001005 NewClass->setAccess(PrevClassTemplate->getAccess());
1006 }
John McCall05b23ea2009-09-14 21:59:20 +00001007
Douglas Gregord85bea22009-09-26 06:47:28 +00001008 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1009 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001010
John McCall05b23ea2009-09-14 21:59:20 +00001011 // Friend templates are visible in fairly strange ways.
1012 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001013 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001014 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1015 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1016 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001017 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001018 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001019
Douglas Gregord85bea22009-09-26 06:47:28 +00001020 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1021 NewClass->getLocation(),
1022 NewTemplate,
1023 /*FIXME:*/NewClass->getLocation());
1024 Friend->setAccess(AS_public);
1025 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001026 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001027
Douglas Gregord684b002009-02-10 19:49:53 +00001028 if (Invalid) {
1029 NewTemplate->setInvalidDecl();
1030 NewClass->setInvalidDecl();
1031 }
John McCalld226f652010-08-21 09:40:31 +00001032 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001033}
1034
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001035/// \brief Diagnose the presence of a default template argument on a
1036/// template parameter, which is ill-formed in certain contexts.
1037///
1038/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001039static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001040 Sema::TemplateParamListContext TPC,
1041 SourceLocation ParamLoc,
1042 SourceRange DefArgRange) {
1043 switch (TPC) {
1044 case Sema::TPC_ClassTemplate:
1045 return false;
1046
1047 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001048 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001049 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001050 // A default template-argument shall not be specified in a
1051 // function template declaration or a function template
1052 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001053 // If a friend function template declaration specifies a default
1054 // template-argument, that declaration shall be a definition and shall be
1055 // the only declaration of the function template in the translation unit.
1056 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001057 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001058 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001059 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001060 << DefArgRange;
1061 return false;
1062
1063 case Sema::TPC_ClassTemplateMember:
1064 // C++0x [temp.param]p9:
1065 // A default template-argument shall not be specified in the
1066 // template-parameter-lists of the definition of a member of a
1067 // class template that appears outside of the member's class.
1068 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1069 << DefArgRange;
1070 return true;
1071
1072 case Sema::TPC_FriendFunctionTemplate:
1073 // C++ [temp.param]p9:
1074 // A default template-argument shall not be specified in a
1075 // friend template declaration.
1076 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1077 << DefArgRange;
1078 return true;
1079
1080 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1081 // for friend function templates if there is only a single
1082 // declaration (and it is a definition). Strange!
1083 }
1084
1085 return false;
1086}
1087
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001088/// \brief Check for unexpanded parameter packs within the template parameters
1089/// of a template template parameter, recursively.
1090bool DiagnoseUnexpandedParameterPacks(Sema &S, TemplateTemplateParmDecl *TTP){
1091 TemplateParameterList *Params = TTP->getTemplateParameters();
1092 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1093 NamedDecl *P = Params->getParam(I);
1094 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001095 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001096 NTTP->getTypeSourceInfo(),
1097 Sema::UPPC_NonTypeTemplateParameterType))
1098 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001099
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001100 continue;
1101 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001102
1103 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001104 = dyn_cast<TemplateTemplateParmDecl>(P))
1105 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1106 return true;
1107 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001108
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001109 return false;
1110}
1111
Douglas Gregord684b002009-02-10 19:49:53 +00001112/// \brief Checks the validity of a template parameter list, possibly
1113/// considering the template parameter list from a previous
1114/// declaration.
1115///
1116/// If an "old" template parameter list is provided, it must be
1117/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1118/// template parameter list.
1119///
1120/// \param NewParams Template parameter list for a new template
1121/// declaration. This template parameter list will be updated with any
1122/// default arguments that are carried through from the previous
1123/// template parameter list.
1124///
1125/// \param OldParams If provided, template parameter list from a
1126/// previous declaration of the same template. Default template
1127/// arguments will be merged from the old template parameter list to
1128/// the new template parameter list.
1129///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001130/// \param TPC Describes the context in which we are checking the given
1131/// template parameter list.
1132///
Douglas Gregord684b002009-02-10 19:49:53 +00001133/// \returns true if an error occurred, false otherwise.
1134bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001135 TemplateParameterList *OldParams,
1136 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001137 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Douglas Gregord684b002009-02-10 19:49:53 +00001139 // C++ [temp.param]p10:
1140 // The set of default template-arguments available for use with a
1141 // template declaration or definition is obtained by merging the
1142 // default arguments from the definition (if in scope) and all
1143 // declarations in scope in the same way default function
1144 // arguments are (8.3.6).
1145 bool SawDefaultArgument = false;
1146 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001147
Anders Carlsson49d25572009-06-12 23:20:15 +00001148 bool SawParameterPack = false;
1149 SourceLocation ParameterPackLoc;
1150
Mike Stump1a35fde2009-02-11 23:03:27 +00001151 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001152 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001153 if (OldParams)
1154 OldParam = OldParams->begin();
1155
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001156 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001157 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1158 NewParamEnd = NewParams->end();
1159 NewParam != NewParamEnd; ++NewParam) {
1160 // Variables used to diagnose redundant default arguments
1161 bool RedundantDefaultArg = false;
1162 SourceLocation OldDefaultLoc;
1163 SourceLocation NewDefaultLoc;
1164
1165 // Variables used to diagnose missing default arguments
1166 bool MissingDefaultArg = false;
1167
Anders Carlsson49d25572009-06-12 23:20:15 +00001168 // C++0x [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001169 // If a template parameter of a primary class template is a template
Douglas Gregor1ed64762011-01-05 16:19:19 +00001170 // parameter pack, it shall be the last template parameter.
1171 if (SawParameterPack && TPC == TPC_ClassTemplate) {
Mike Stump1eb44332009-09-09 15:08:12 +00001172 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001173 diag::err_template_param_pack_must_be_last_template_parameter);
1174 Invalid = true;
1175 }
1176
Douglas Gregord684b002009-02-10 19:49:53 +00001177 if (TemplateTypeParmDecl *NewTypeParm
1178 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001179 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001180 if (NewTypeParm->hasDefaultArgument() &&
1181 DiagnoseDefaultTemplateArgument(*this, TPC,
1182 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001183 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001184 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001185 NewTypeParm->removeDefaultArgument();
1186
1187 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001188 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001189 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Anders Carlsson49d25572009-06-12 23:20:15 +00001191 if (NewTypeParm->isParameterPack()) {
1192 assert(!NewTypeParm->hasDefaultArgument() &&
1193 "Parameter packs can't have a default argument!");
1194 SawParameterPack = true;
1195 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001196 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001197 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001198 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1199 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1200 SawDefaultArgument = true;
1201 RedundantDefaultArg = true;
1202 PreviousDefaultArgLoc = NewDefaultLoc;
1203 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1204 // Merge the default argument from the old declaration to the
1205 // new declaration.
1206 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001207 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001208 true);
1209 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1210 } else if (NewTypeParm->hasDefaultArgument()) {
1211 SawDefaultArgument = true;
1212 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1213 } else if (SawDefaultArgument)
1214 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001215 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001216 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001217 // Check for unexpanded parameter packs.
1218 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001219 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001220 UPPC_NonTypeTemplateParameterType)) {
1221 Invalid = true;
1222 continue;
1223 }
1224
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001225 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001226 if (NewNonTypeParm->hasDefaultArgument() &&
1227 DiagnoseDefaultTemplateArgument(*this, TPC,
1228 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001229 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001230 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001231 }
1232
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001233 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001234 NonTypeTemplateParmDecl *OldNonTypeParm
1235 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001236 if (NewNonTypeParm->isParameterPack()) {
1237 assert(!NewNonTypeParm->hasDefaultArgument() &&
1238 "Parameter packs can't have a default argument!");
1239 SawParameterPack = true;
1240 ParameterPackLoc = NewNonTypeParm->getLocation();
1241 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001242 NewNonTypeParm->hasDefaultArgument()) {
1243 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1244 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1245 SawDefaultArgument = true;
1246 RedundantDefaultArg = true;
1247 PreviousDefaultArgLoc = NewDefaultLoc;
1248 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1249 // Merge the default argument from the old declaration to the
1250 // new declaration.
1251 SawDefaultArgument = true;
1252 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001253 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001254 // parameter.
1255 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001256 OldNonTypeParm->getDefaultArgument(),
1257 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001258 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1259 } else if (NewNonTypeParm->hasDefaultArgument()) {
1260 SawDefaultArgument = true;
1261 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1262 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001263 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001264 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001265 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001266 TemplateTemplateParmDecl *NewTemplateParm
1267 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001268
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001269 // Check for unexpanded parameter packs, recursively.
1270 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1271 Invalid = true;
1272 continue;
1273 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001274
1275 if (NewTemplateParm->hasDefaultArgument() &&
1276 DiagnoseDefaultTemplateArgument(*this, TPC,
1277 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001278 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001279 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001280
1281 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001282 TemplateTemplateParmDecl *OldTemplateParm
1283 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001284 if (NewTemplateParm->isParameterPack()) {
1285 assert(!NewTemplateParm->hasDefaultArgument() &&
1286 "Parameter packs can't have a default argument!");
1287 SawParameterPack = true;
1288 ParameterPackLoc = NewTemplateParm->getLocation();
1289 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001290 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001291 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1292 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001293 SawDefaultArgument = true;
1294 RedundantDefaultArg = true;
1295 PreviousDefaultArgLoc = NewDefaultLoc;
1296 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1297 // Merge the default argument from the old declaration to the
1298 // new declaration.
1299 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001300 // FIXME: We need to create a new kind of "default argument" expression
1301 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001302 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001303 OldTemplateParm->getDefaultArgument(),
1304 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001305 PreviousDefaultArgLoc
1306 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001307 } else if (NewTemplateParm->hasDefaultArgument()) {
1308 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001309 PreviousDefaultArgLoc
1310 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001311 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001312 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001313 }
1314
1315 if (RedundantDefaultArg) {
1316 // C++ [temp.param]p12:
1317 // A template-parameter shall not be given default arguments
1318 // by two different declarations in the same scope.
1319 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1320 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1321 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001322 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001323 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001324 // If a template-parameter of a class template has a default
1325 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001326 // have a default template-argument supplied or be a template parameter
1327 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001328 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001329 diag::err_template_param_default_arg_missing);
1330 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1331 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001332 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001333 }
1334
1335 // If we have an old template parameter list that we're merging
1336 // in, move on to the next parameter.
1337 if (OldParams)
1338 ++OldParam;
1339 }
1340
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001341 // We were missing some default arguments at the end of the list, so remove
1342 // all of the default arguments.
1343 if (RemoveDefaultArguments) {
1344 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1345 NewParamEnd = NewParams->end();
1346 NewParam != NewParamEnd; ++NewParam) {
1347 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1348 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001349 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001350 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1351 NTTP->removeDefaultArgument();
1352 else
1353 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1354 }
1355 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001356
Douglas Gregord684b002009-02-10 19:49:53 +00001357 return Invalid;
1358}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001359
John McCall4e2cbb22010-10-20 05:44:58 +00001360namespace {
1361
1362/// A class which looks for a use of a certain level of template
1363/// parameter.
1364struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1365 typedef RecursiveASTVisitor<DependencyChecker> super;
1366
1367 unsigned Depth;
1368 bool Match;
1369
1370 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1371 NamedDecl *ND = Params->getParam(0);
1372 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1373 Depth = PD->getDepth();
1374 } else if (NonTypeTemplateParmDecl *PD =
1375 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1376 Depth = PD->getDepth();
1377 } else {
1378 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1379 }
1380 }
1381
1382 bool Matches(unsigned ParmDepth) {
1383 if (ParmDepth >= Depth) {
1384 Match = true;
1385 return true;
1386 }
1387 return false;
1388 }
1389
1390 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1391 return !Matches(T->getDepth());
1392 }
1393
1394 bool TraverseTemplateName(TemplateName N) {
1395 if (TemplateTemplateParmDecl *PD =
1396 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1397 if (Matches(PD->getDepth())) return false;
1398 return super::TraverseTemplateName(N);
1399 }
1400
1401 bool VisitDeclRefExpr(DeclRefExpr *E) {
1402 if (NonTypeTemplateParmDecl *PD =
1403 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1404 if (PD->getDepth() == Depth) {
1405 Match = true;
1406 return false;
1407 }
1408 }
1409 return super::VisitDeclRefExpr(E);
1410 }
1411};
1412}
1413
1414/// Determines whether a template-id depends on the given parameter
1415/// list.
1416static bool
1417DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId,
1418 TemplateParameterList *Params) {
1419 DependencyChecker Checker(Params);
1420 Checker.TraverseType(QualType(TemplateId, 0));
1421 return Checker.Match;
1422}
1423
Mike Stump1eb44332009-09-09 15:08:12 +00001424/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001425/// specifier, returning the template parameter list that applies to the
1426/// name.
1427///
1428/// \param DeclStartLoc the start of the declaration that has a scope
1429/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001430///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001431/// \param SS the scope specifier that will be matched to the given template
1432/// parameter lists. This scope specifier precedes a qualified name that is
1433/// being declared.
1434///
1435/// \param ParamLists the template parameter lists, from the outermost to the
1436/// innermost template parameter lists.
1437///
1438/// \param NumParamLists the number of template parameter lists in ParamLists.
1439///
John McCall77e8b112010-04-13 20:37:33 +00001440/// \param IsFriend Whether to apply the slightly different rules for
1441/// matching template parameters to scope specifiers in friend
1442/// declarations.
1443///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001444/// \param IsExplicitSpecialization will be set true if the entity being
1445/// declared is an explicit specialization, false otherwise.
1446///
Mike Stump1eb44332009-09-09 15:08:12 +00001447/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001448/// name that is preceded by the scope specifier @p SS. This template
1449/// parameter list may be have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001450/// template) or may have no template parameters (if we're declaring a
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001451/// template specialization), or may be NULL (if we were's declaring isn't
1452/// itself a template).
1453TemplateParameterList *
1454Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
1455 const CXXScopeSpec &SS,
1456 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001457 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001458 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001459 bool &IsExplicitSpecialization,
1460 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001461 IsExplicitSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001462
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001463 // Find the template-ids that occur within the nested-name-specifier. These
1464 // template-ids will match up with the template parameter lists.
1465 llvm::SmallVector<const TemplateSpecializationType *, 4>
1466 TemplateIdsInSpecifier;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001467 llvm::SmallVector<ClassTemplateSpecializationDecl *, 4>
1468 ExplicitSpecializationsInSpecifier;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001469 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
1470 NNS; NNS = NNS->getPrefix()) {
John McCall4b2b02b2009-12-15 02:19:47 +00001471 const Type *T = NNS->getAsType();
1472 if (!T) break;
1473
1474 // C++0x [temp.expl.spec]p17:
1475 // A member or a member template may be nested within many
1476 // enclosing class templates. In an explicit specialization for
1477 // such a member, the member declaration shall be preceded by a
1478 // template<> for each enclosing class template that is
1479 // explicitly specialized.
Douglas Gregorfe331062010-02-13 05:23:25 +00001480 //
1481 // Following the existing practice of GNU and EDG, we allow a typedef of a
1482 // template specialization type.
John McCall49f4e1c2010-12-10 11:01:00 +00001483 while (const TypedefType *TT = dyn_cast<TypedefType>(T))
1484 T = TT->getDecl()->getUnderlyingType().getTypePtr();
John McCall4b2b02b2009-12-15 02:19:47 +00001485
Mike Stump1eb44332009-09-09 15:08:12 +00001486 if (const TemplateSpecializationType *SpecType
Douglas Gregorfe331062010-02-13 05:23:25 +00001487 = dyn_cast<TemplateSpecializationType>(T)) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001488 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
1489 if (!Template)
1490 continue; // FIXME: should this be an error? probably...
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Ted Kremenek6217b802009-07-29 21:53:49 +00001492 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001493 ClassTemplateSpecializationDecl *SpecDecl
1494 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
1495 // If the nested name specifier refers to an explicit specialization,
1496 // we don't need a template<> header.
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001497 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
1498 ExplicitSpecializationsInSpecifier.push_back(SpecDecl);
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001499 continue;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001500 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001501 }
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001503 TemplateIdsInSpecifier.push_back(SpecType);
1504 }
1505 }
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001507 // Reverse the list of template-ids in the scope specifier, so that we can
1508 // more easily match up the template-ids and the template parameter lists.
1509 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001511 SourceLocation FirstTemplateLoc = DeclStartLoc;
1512 if (NumParamLists)
1513 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001515 // Match the template-ids found in the specifier to the template parameter
1516 // lists.
John McCall4e2cbb22010-10-20 05:44:58 +00001517 unsigned ParamIdx = 0, TemplateIdx = 0;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001518 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
John McCall4e2cbb22010-10-20 05:44:58 +00001519 TemplateIdx != NumTemplateIds; ++TemplateIdx) {
1520 const TemplateSpecializationType *TemplateId
1521 = TemplateIdsInSpecifier[TemplateIdx];
Douglas Gregorb88e8882009-07-30 17:40:51 +00001522 bool DependentTemplateId = TemplateId->isDependentType();
John McCall4e2cbb22010-10-20 05:44:58 +00001523
1524 // In friend declarations we can have template-ids which don't
1525 // depend on the corresponding template parameter lists. But
1526 // assume that empty parameter lists are supposed to match this
1527 // template-id.
1528 if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) {
1529 if (!DependentTemplateId ||
1530 !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx]))
1531 continue;
1532 }
1533
1534 if (ParamIdx >= NumParamLists) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001535 // We have a template-id without a corresponding template parameter
1536 // list.
John McCall77e8b112010-04-13 20:37:33 +00001537
1538 // ...which is fine if this is a friend declaration.
1539 if (IsFriend) {
1540 IsExplicitSpecialization = true;
1541 break;
1542 }
1543
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001544 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +00001545 // FIXME: the location information here isn't great.
1546 Diag(SS.getRange().getBegin(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001547 diag::err_template_spec_needs_template_parameters)
John McCall4e2cbb22010-10-20 05:44:58 +00001548 << QualType(TemplateId, 0)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001549 << SS.getRange();
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001550 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001551 } else {
1552 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
1553 << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +00001554 << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001555 IsExplicitSpecialization = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001556 }
1557 return 0;
1558 }
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001560 // Check the template parameter list against its corresponding template-id.
Douglas Gregorb88e8882009-07-30 17:40:51 +00001561 if (DependentTemplateId) {
John McCall31f17ec2010-04-27 00:57:59 +00001562 TemplateParameterList *ExpectedTemplateParams = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00001563
John McCall31f17ec2010-04-27 00:57:59 +00001564 // Are there cases in (e.g.) friends where this won't match?
1565 if (const InjectedClassNameType *Injected
1566 = TemplateId->getAs<InjectedClassNameType>()) {
1567 CXXRecordDecl *Record = Injected->getDecl();
1568 if (ClassTemplatePartialSpecializationDecl *Partial =
1569 dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
1570 ExpectedTemplateParams = Partial->getTemplateParameters();
1571 else
1572 ExpectedTemplateParams = Record->getDescribedClassTemplate()
1573 ->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001574 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001575
John McCall31f17ec2010-04-27 00:57:59 +00001576 if (ExpectedTemplateParams)
John McCall4e2cbb22010-10-20 05:44:58 +00001577 TemplateParameterListsAreEqual(ParamLists[ParamIdx],
John McCall31f17ec2010-04-27 00:57:59 +00001578 ExpectedTemplateParams,
1579 true, TPL_TemplateMatch);
1580
John McCall4e2cbb22010-10-20 05:44:58 +00001581 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1582 TPC_ClassTemplateMember);
1583 } else if (ParamLists[ParamIdx]->size() > 0)
1584 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00001585 diag::err_template_param_list_matches_nontemplate)
1586 << TemplateId
John McCall4e2cbb22010-10-20 05:44:58 +00001587 << ParamLists[ParamIdx]->getSourceRange();
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001588 else
1589 IsExplicitSpecialization = true;
John McCall4e2cbb22010-10-20 05:44:58 +00001590
1591 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001592 }
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001594 // If there were at least as many template-ids as there were template
1595 // parameter lists, then there are no template parameter lists remaining for
1596 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001597 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001598 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001599
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001600 // If there were too many template parameter lists, complain about that now.
John McCall4e2cbb22010-10-20 05:44:58 +00001601 if (ParamIdx != NumParamLists - 1) {
1602 while (ParamIdx < NumParamLists - 1) {
1603 bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0;
1604 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001605 isExplicitSpecHeader? diag::warn_template_spec_extra_headers
1606 : diag::err_template_spec_extra_headers)
John McCall4e2cbb22010-10-20 05:44:58 +00001607 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1608 ParamLists[ParamIdx]->getRAngleLoc());
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001609
1610 if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) {
1611 Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(),
1612 diag::note_explicit_template_spec_does_not_need_header)
1613 << ExplicitSpecializationsInSpecifier.back();
1614 ExplicitSpecializationsInSpecifier.pop_back();
1615 }
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001616
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001617 // We have a template parameter list with no corresponding scope, which
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001618 // means that the resulting template declaration can't be instantiated
1619 // properly (we'll end up with dependent nodes when we shouldn't).
1620 if (!isExplicitSpecHeader)
1621 Invalid = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001622
John McCall4e2cbb22010-10-20 05:44:58 +00001623 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001624 }
1625 }
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001627 // Return the last template parameter list, which corresponds to the
1628 // entity being declared.
1629 return ParamLists[NumParamLists - 1];
1630}
1631
Douglas Gregor7532dc62009-03-30 22:58:21 +00001632QualType Sema::CheckTemplateIdType(TemplateName Name,
1633 SourceLocation TemplateLoc,
John McCalld5532b62009-11-23 01:53:49 +00001634 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001635 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001636 if (!Template) {
1637 // The template name does not resolve to a template, so we just
1638 // build a dependent template-id type.
John McCalld5532b62009-11-23 01:53:49 +00001639 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001640 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001641
Douglas Gregor40808ce2009-03-09 23:48:35 +00001642 // Check that the template argument list is well-formed for this
1643 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001644 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001645 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001646 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001647 return QualType();
1648
Douglas Gregor910f8002010-11-07 23:05:16 +00001649 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001650 "Converted template argument list is too short!");
1651
1652 QualType CanonType;
1653
Douglas Gregorcaddba02009-11-12 18:38:13 +00001654 if (Name.isDependent() ||
1655 TemplateSpecializationType::anyDependentTemplateArguments(
John McCalld5532b62009-11-23 01:53:49 +00001656 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001657 // This class template specialization is a dependent
1658 // type. Therefore, its canonical type is another class template
1659 // specialization type that contains all of the converted
1660 // arguments in canonical form. This ensures that, e.g., A<T> and
1661 // A<T, T> have identical types when A is declared as:
1662 //
1663 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001664 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001665 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001666 Converted.data(),
1667 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Douglas Gregor1275ae02009-07-28 23:00:59 +00001669 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001670 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001671 // In the future, we need to teach getTemplateSpecializationType to only
1672 // build the canonical type and return that to us.
1673 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001674
1675 // This might work out to be a current instantiation, in which
1676 // case the canonical type needs to be the InjectedClassNameType.
1677 //
1678 // TODO: in theory this could be a simple hashtable lookup; most
1679 // changes to CurContext don't change the set of current
1680 // instantiations.
1681 if (isa<ClassTemplateDecl>(Template)) {
1682 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1683 // If we get out to a namespace, we're done.
1684 if (Ctx->isFileContext()) break;
1685
1686 // If this isn't a record, keep looking.
1687 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1688 if (!Record) continue;
1689
1690 // Look for one of the two cases with InjectedClassNameTypes
1691 // and check whether it's the same template.
1692 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1693 !Record->getDescribedClassTemplate())
1694 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001695
John McCall31f17ec2010-04-27 00:57:59 +00001696 // Fetch the injected class name type and check whether its
1697 // injected type is equal to the type we just built.
1698 QualType ICNT = Context.getTypeDeclType(Record);
1699 QualType Injected = cast<InjectedClassNameType>(ICNT)
1700 ->getInjectedSpecializationType();
1701
1702 if (CanonType != Injected->getCanonicalTypeInternal())
1703 continue;
1704
1705 // If so, the canonical type of this TST is the injected
1706 // class name type of the record we just found.
1707 assert(ICNT.isCanonical());
1708 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001709 break;
1710 }
1711 }
Mike Stump1eb44332009-09-09 15:08:12 +00001712 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001713 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001714 // Find the class template specialization declaration that
1715 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001716 void *InsertPos = 0;
1717 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001718 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001719 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001720 if (!Decl) {
1721 // This is the first time we have referenced this class template
1722 // specialization. Create the canonical declaration and add it to
1723 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001724 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001725 ClassTemplate->getTemplatedDecl()->getTagKind(),
1726 ClassTemplate->getDeclContext(),
1727 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001728 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001729 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001730 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001731 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001732 Decl->setLexicalDeclContext(CurContext);
1733 }
1734
1735 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00001736 assert(isa<RecordType>(CanonType) &&
1737 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00001738 }
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Douglas Gregor40808ce2009-03-09 23:48:35 +00001740 // Build the fully-sugared type for this class template
1741 // specialization, which refers back to the class template
1742 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00001743 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001744}
1745
John McCallf312b1e2010-08-26 23:41:50 +00001746TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00001747Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
1748 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001749 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001750 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00001751 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00001752 if (SS.isInvalid())
1753 return true;
1754
Douglas Gregor7532dc62009-03-30 22:58:21 +00001755 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001756
Douglas Gregor40808ce2009-03-09 23:48:35 +00001757 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00001758 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00001759 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001760
Douglas Gregora88f09f2011-02-28 17:23:35 +00001761 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1762 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
1763 DTN->getQualifier(),
1764 DTN->getIdentifier(),
1765 TemplateArgs);
1766
1767 // Build type-source information.
1768 TypeLocBuilder TLB;
1769 DependentTemplateSpecializationTypeLoc SpecTL
1770 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00001771 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00001772 SpecTL.setNameLoc(TemplateLoc);
1773 SpecTL.setLAngleLoc(LAngleLoc);
1774 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00001775 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00001776 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1777 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1778 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1779 }
1780
John McCalld5532b62009-11-23 01:53:49 +00001781 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001782 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00001783
1784 if (Result.isNull())
1785 return true;
1786
Douglas Gregor059101f2011-03-02 00:47:37 +00001787 // Build type-source information.
1788 TypeLocBuilder TLB;
1789 TemplateSpecializationTypeLoc SpecTL
1790 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1791 SpecTL.setTemplateNameLoc(TemplateLoc);
1792 SpecTL.setLAngleLoc(LAngleLoc);
1793 SpecTL.setRAngleLoc(RAngleLoc);
1794 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1795 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00001796
Douglas Gregor059101f2011-03-02 00:47:37 +00001797 if (SS.isNotEmpty()) {
1798 // Create an elaborated-type-specifier containing the nested-name-specifier.
1799 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
1800 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1801 ElabTL.setKeywordLoc(SourceLocation());
1802 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1803 }
1804
1805 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00001806}
John McCallf1bbbb42009-09-04 01:14:41 +00001807
Douglas Gregor059101f2011-03-02 00:47:37 +00001808TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00001809 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00001810 SourceLocation TagLoc,
1811 CXXScopeSpec &SS,
1812 TemplateTy TemplateD,
1813 SourceLocation TemplateLoc,
1814 SourceLocation LAngleLoc,
1815 ASTTemplateArgsPtr TemplateArgsIn,
1816 SourceLocation RAngleLoc) {
1817 TemplateName Template = TemplateD.getAsVal<TemplateName>();
1818
1819 // Translate the parser's template argument list in our AST format.
1820 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
1821 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
1822
1823 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001824 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00001825 ElaboratedTypeKeyword Keyword
1826 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00001827
Douglas Gregor059101f2011-03-02 00:47:37 +00001828 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1829 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
1830 DTN->getQualifier(),
1831 DTN->getIdentifier(),
1832 TemplateArgs);
1833
1834 // Build type-source information.
1835 TypeLocBuilder TLB;
1836 DependentTemplateSpecializationTypeLoc SpecTL
1837 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
1838 SpecTL.setKeywordLoc(TagLoc);
1839 SpecTL.setNameLoc(TemplateLoc);
1840 SpecTL.setLAngleLoc(LAngleLoc);
1841 SpecTL.setRAngleLoc(RAngleLoc);
1842 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
1843 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1844 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1845 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1846 }
1847
1848 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
1849 if (Result.isNull())
1850 return TypeResult();
1851
1852 // Check the tag kind
1853 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00001854 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00001855
John McCall6b2becf2009-09-08 17:47:29 +00001856 IdentifierInfo *Id = D->getIdentifier();
1857 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00001858
John McCall6b2becf2009-09-08 17:47:29 +00001859 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1860 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00001861 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00001862 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00001863 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00001864 }
1865 }
Douglas Gregor059101f2011-03-02 00:47:37 +00001866
1867 // Provide source-location information for the template specialization.
1868 TypeLocBuilder TLB;
1869 TemplateSpecializationTypeLoc SpecTL
1870 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1871 SpecTL.setTemplateNameLoc(TemplateLoc);
1872 SpecTL.setLAngleLoc(LAngleLoc);
1873 SpecTL.setRAngleLoc(RAngleLoc);
1874 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1875 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00001876
Douglas Gregor059101f2011-03-02 00:47:37 +00001877 // Construct an elaborated type containing the nested-name-specifier (if any)
1878 // and keyword.
1879 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
1880 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1881 ElabTL.setKeywordLoc(TagLoc);
1882 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1883 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00001884}
1885
John McCall60d7b3a2010-08-24 06:29:42 +00001886ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001887 LookupResult &R,
1888 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00001889 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001890 // FIXME: Can we do any checking at this point? I guess we could check the
1891 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00001892 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001893 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001894 // foo<int> could identify a single function unambiguously
1895 // This approach does NOT work, since f<int>(1);
1896 // gets resolved prior to resorting to overload resolution
1897 // i.e., template<class T> void f(double);
1898 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00001899
1900 // These should be filtered out by our callers.
1901 assert(!R.empty() && "empty lookup results when building templateid");
1902 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
1903
John McCallc373d482010-01-27 01:50:18 +00001904 // We don't want lookup warnings at this point.
1905 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001906
John McCallf7a1a742009-11-24 19:00:30 +00001907 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001908 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00001909 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00001910 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001911 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001912 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00001913
1914 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001915}
1916
John McCallf7a1a742009-11-24 19:00:30 +00001917// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00001918ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00001919Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00001920 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00001921 const TemplateArgumentListInfo &TemplateArgs) {
1922 DeclContext *DC;
1923 if (!(DC = computeDeclContext(SS, false)) ||
1924 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00001925 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00001926 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001928 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00001929 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001930 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
1931 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00001932
John McCallf7a1a742009-11-24 19:00:30 +00001933 if (R.isAmbiguous())
1934 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001935
John McCallf7a1a742009-11-24 19:00:30 +00001936 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001937 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
1938 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001939 return ExprError();
1940 }
1941
1942 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001943 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
1944 << (NestedNameSpecifier*) SS.getScopeRep()
1945 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001946 Diag(Temp->getLocation(), diag::note_referenced_class_template);
1947 return ExprError();
1948 }
1949
1950 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001951}
1952
Douglas Gregorc45c2322009-03-31 00:43:58 +00001953/// \brief Form a dependent template name.
1954///
1955/// This action forms a dependent template name given the template
1956/// name and its (presumably dependent) scope specifier. For
1957/// example, given "MetaFun::template apply", the scope specifier \p
1958/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
1959/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001960TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001961 SourceLocation TemplateKWLoc,
1962 CXXScopeSpec &SS,
1963 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00001964 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001965 bool EnteringContext,
1966 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00001967 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
1968 !getLangOptions().CPlusPlus0x)
1969 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001970 << FixItHint::CreateRemoval(TemplateKWLoc);
1971
Douglas Gregor0707bc52010-01-19 16:01:07 +00001972 DeclContext *LookupCtx = 0;
1973 if (SS.isSet())
1974 LookupCtx = computeDeclContext(SS, EnteringContext);
1975 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00001976 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00001977 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00001978 // C++0x [temp.names]p5:
1979 // If a name prefixed by the keyword template is not the name of
1980 // a template, the program is ill-formed. [Note: the keyword
1981 // template may not be applied to non-template members of class
1982 // templates. -end note ] [ Note: as is the case with the
1983 // typename prefix, the template prefix is allowed in cases
1984 // where it is not strictly necessary; i.e., when the
1985 // nested-name-specifier or the expression on the left of the ->
1986 // or . is not dependent on a template-parameter, or the use
1987 // does not appear in the scope of a template. -end note]
1988 //
1989 // Note: C++03 was more strict here, because it banned the use of
1990 // the "template" keyword prior to a template-name that was not a
1991 // dependent name. C++ DR468 relaxed this requirement (the
1992 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00001993 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001994 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001995 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
1996 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001997 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00001998 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
1999 isa<CXXRecordDecl>(LookupCtx) &&
2000 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002001 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002002 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002003 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002004 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002005 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002006 << Name.getSourceRange()
2007 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002008 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002009 } else {
2010 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002011 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002012 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002013 }
2014
Mike Stump1eb44332009-09-09 15:08:12 +00002015 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002016 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002017
Douglas Gregor014e88d2009-11-03 23:16:33 +00002018 switch (Name.getKind()) {
2019 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002020 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002021 Name.Identifier));
2022 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002023
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002024 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002025 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002026 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002027 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002028
2029 case UnqualifiedId::IK_LiteralOperatorId:
2030 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
2031
Douglas Gregor014e88d2009-11-03 23:16:33 +00002032 default:
2033 break;
2034 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002035
2036 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002037 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002038 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002039 << Name.getSourceRange()
2040 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002041 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002042}
2043
Mike Stump1eb44332009-09-09 15:08:12 +00002044bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002045 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00002046 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002047 const TemplateArgument &Arg = AL.getArgument();
2048
Anders Carlsson436b1562009-06-13 00:33:33 +00002049 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002050 switch(Arg.getKind()) {
2051 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002052 // C++ [temp.arg.type]p1:
2053 // A template-argument for a template-parameter which is a
2054 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002055 break;
2056 case TemplateArgument::Template: {
2057 // We have a template type parameter but the template argument
2058 // is a template without any arguments.
2059 SourceRange SR = AL.getSourceRange();
2060 TemplateName Name = Arg.getAsTemplate();
2061 Diag(SR.getBegin(), diag::err_template_missing_args)
2062 << Name << SR;
2063 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2064 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002065
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002066 return true;
2067 }
2068 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002069 // We have a template type parameter but the template argument
2070 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002071 SourceRange SR = AL.getSourceRange();
2072 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002073 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002074
Anders Carlsson436b1562009-06-13 00:33:33 +00002075 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002076 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002077 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002078
John McCalla93c9342009-12-07 02:54:59 +00002079 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002080 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002081
Anders Carlsson436b1562009-06-13 00:33:33 +00002082 // Add the converted template type argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002083 Converted.push_back(
John McCall833ca992009-10-29 08:12:44 +00002084 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00002085 return false;
2086}
2087
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002088/// \brief Substitute template arguments into the default template argument for
2089/// the given template type parameter.
2090///
2091/// \param SemaRef the semantic analysis object for which we are performing
2092/// the substitution.
2093///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002094/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002095/// for.
2096///
2097/// \param TemplateLoc the location of the template name that started the
2098/// template-id we are checking.
2099///
2100/// \param RAngleLoc the location of the right angle bracket ('>') that
2101/// terminates the template-id.
2102///
2103/// \param Param the template template parameter whose default we are
2104/// substituting into.
2105///
2106/// \param Converted the list of template arguments provided for template
2107/// parameters that precede \p Param in the template parameter list.
2108///
2109/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002110static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002111SubstDefaultTemplateArgument(Sema &SemaRef,
2112 TemplateDecl *Template,
2113 SourceLocation TemplateLoc,
2114 SourceLocation RAngleLoc,
2115 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002116 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002117 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002118
2119 // If the argument type is dependent, instantiate it now based
2120 // on the previously-computed template arguments.
2121 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002122 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002123 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002124
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002125 MultiLevelTemplateArgumentList AllTemplateArgs
2126 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2127
2128 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002129 Template, Converted.data(),
2130 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002131 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002132
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002133 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2134 Param->getDefaultArgumentLoc(),
2135 Param->getDeclName());
2136 }
2137
2138 return ArgType;
2139}
2140
2141/// \brief Substitute template arguments into the default template argument for
2142/// the given non-type template parameter.
2143///
2144/// \param SemaRef the semantic analysis object for which we are performing
2145/// the substitution.
2146///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002147/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002148/// for.
2149///
2150/// \param TemplateLoc the location of the template name that started the
2151/// template-id we are checking.
2152///
2153/// \param RAngleLoc the location of the right angle bracket ('>') that
2154/// terminates the template-id.
2155///
Douglas Gregor788cd062009-11-11 01:00:40 +00002156/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002157/// substituting into.
2158///
2159/// \param Converted the list of template arguments provided for template
2160/// parameters that precede \p Param in the template parameter list.
2161///
2162/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002163static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002164SubstDefaultTemplateArgument(Sema &SemaRef,
2165 TemplateDecl *Template,
2166 SourceLocation TemplateLoc,
2167 SourceLocation RAngleLoc,
2168 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002169 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002170 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002171 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002172
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002173 MultiLevelTemplateArgumentList AllTemplateArgs
2174 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002175
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002176 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002177 Template, Converted.data(),
2178 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002179 SourceRange(TemplateLoc, RAngleLoc));
2180
2181 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2182}
2183
Douglas Gregor788cd062009-11-11 01:00:40 +00002184/// \brief Substitute template arguments into the default template argument for
2185/// the given template template parameter.
2186///
2187/// \param SemaRef the semantic analysis object for which we are performing
2188/// the substitution.
2189///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002190/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002191/// for.
2192///
2193/// \param TemplateLoc the location of the template name that started the
2194/// template-id we are checking.
2195///
2196/// \param RAngleLoc the location of the right angle bracket ('>') that
2197/// terminates the template-id.
2198///
2199/// \param Param the template template parameter whose default we are
2200/// substituting into.
2201///
2202/// \param Converted the list of template arguments provided for template
2203/// parameters that precede \p Param in the template parameter list.
2204///
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002205/// \param QualifierLoc The nested-name-specifier, with source-location
2206/// information, which will also be instantiated and updated.
2207///
Douglas Gregor788cd062009-11-11 01:00:40 +00002208/// \returns the substituted template argument, or NULL if an error occurred.
2209static TemplateName
2210SubstDefaultTemplateArgument(Sema &SemaRef,
2211 TemplateDecl *Template,
2212 SourceLocation TemplateLoc,
2213 SourceLocation RAngleLoc,
2214 TemplateTemplateParmDecl *Param,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002215 llvm::SmallVectorImpl<TemplateArgument> &Converted,
2216 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002217 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002218 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002219
Douglas Gregor788cd062009-11-11 01:00:40 +00002220 MultiLevelTemplateArgumentList AllTemplateArgs
2221 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002222
Douglas Gregor788cd062009-11-11 01:00:40 +00002223 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002224 Template, Converted.data(),
2225 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002226 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002227
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002228 // Substitute into the nested-name-specifier first,
2229 if (QualifierLoc) {
2230 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2231 AllTemplateArgs);
2232 if (!QualifierLoc)
2233 return TemplateName();
2234 }
2235
Douglas Gregor788cd062009-11-11 01:00:40 +00002236 return SemaRef.SubstTemplateName(
2237 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002238 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002239 AllTemplateArgs);
2240}
2241
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002242/// \brief If the given template parameter has a default template
2243/// argument, substitute into that default template argument and
2244/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002245TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002246Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2247 SourceLocation TemplateLoc,
2248 SourceLocation RAngleLoc,
2249 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002250 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2251 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002252 if (!TypeParm->hasDefaultArgument())
2253 return TemplateArgumentLoc();
2254
John McCalla93c9342009-12-07 02:54:59 +00002255 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002256 TemplateLoc,
2257 RAngleLoc,
2258 TypeParm,
2259 Converted);
2260 if (DI)
2261 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2262
2263 return TemplateArgumentLoc();
2264 }
2265
2266 if (NonTypeTemplateParmDecl *NonTypeParm
2267 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2268 if (!NonTypeParm->hasDefaultArgument())
2269 return TemplateArgumentLoc();
2270
John McCall60d7b3a2010-08-24 06:29:42 +00002271 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002272 TemplateLoc,
2273 RAngleLoc,
2274 NonTypeParm,
2275 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002276 if (Arg.isInvalid())
2277 return TemplateArgumentLoc();
2278
2279 Expr *ArgE = Arg.takeAs<Expr>();
2280 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2281 }
2282
2283 TemplateTemplateParmDecl *TempTempParm
2284 = cast<TemplateTemplateParmDecl>(Param);
2285 if (!TempTempParm->hasDefaultArgument())
2286 return TemplateArgumentLoc();
2287
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002288
2289 NestedNameSpecifierLoc QualifierLoc
2290 = TempTempParm->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002291 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002292 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002293 RAngleLoc,
2294 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002295 Converted,
2296 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002297 if (TName.isNull())
2298 return TemplateArgumentLoc();
2299
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002300 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002301 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002302 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2303}
2304
Douglas Gregore7526412009-11-11 19:31:23 +00002305/// \brief Check that the given template argument corresponds to the given
2306/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002307///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002308/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002309/// checked.
2310///
2311/// \param Arg The template argument.
2312///
2313/// \param Template The template in which the template argument resides.
2314///
2315/// \param TemplateLoc The location of the template name for the template
2316/// whose argument list we're matching.
2317///
2318/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2319/// the template argument list.
2320///
2321/// \param ArgumentPackIndex The index into the argument pack where this
2322/// argument will be placed. Only valid if the parameter is a parameter pack.
2323///
2324/// \param Converted The checked, converted argument will be added to the
2325/// end of this small vector.
2326///
2327/// \param CTAK Describes how we arrived at this particular template argument:
2328/// explicitly written, deduced, etc.
2329///
2330/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002331bool Sema::CheckTemplateArgument(NamedDecl *Param,
2332 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002333 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002334 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002335 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002336 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002337 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002338 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002339 // Check template type parameters.
2340 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002341 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002342
Douglas Gregord9e15302009-11-11 19:41:09 +00002343 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002344 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002345 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002346 // with the template arguments we've seen thus far. But if the
2347 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002348 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002349 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2350 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002351
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002352 if (NTTPType->isDependentType() &&
2353 !isa<TemplateTemplateParmDecl>(Template) &&
2354 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002355 // Do substitution on the type of the non-type template parameter.
2356 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002357 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002358 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002359
2360 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002361 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002362 NTTPType = SubstType(NTTPType,
2363 MultiLevelTemplateArgumentList(TemplateArgs),
2364 NTTP->getLocation(),
2365 NTTP->getDeclName());
2366 // If that worked, check the non-type template parameter type
2367 // for validity.
2368 if (!NTTPType.isNull())
2369 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2370 NTTP->getLocation());
2371 if (NTTPType.isNull())
2372 return true;
2373 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002374
Douglas Gregore7526412009-11-11 19:31:23 +00002375 switch (Arg.getArgument().getKind()) {
2376 case TemplateArgument::Null:
2377 assert(false && "Should never see a NULL template argument here");
2378 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002379
Douglas Gregore7526412009-11-11 19:31:23 +00002380 case TemplateArgument::Expression: {
2381 Expr *E = Arg.getArgument().getAsExpr();
2382 TemplateArgument Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002383 if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK))
Douglas Gregore7526412009-11-11 19:31:23 +00002384 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002385
Douglas Gregor910f8002010-11-07 23:05:16 +00002386 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002387 break;
2388 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002389
Douglas Gregore7526412009-11-11 19:31:23 +00002390 case TemplateArgument::Declaration:
2391 case TemplateArgument::Integral:
2392 // We've already checked this template argument, so just copy
2393 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002394 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002395 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002396
Douglas Gregore7526412009-11-11 19:31:23 +00002397 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002398 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002399 // We were given a template template argument. It may not be ill-formed;
2400 // see below.
2401 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002402 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2403 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002404 // We have a template argument such as \c T::template X, which we
2405 // parsed as a template template argument. However, since we now
2406 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002407 // template name into an expression.
2408
2409 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2410 Arg.getTemplateNameLoc());
2411
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002412 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002413 SS.Adopt(Arg.getTemplateQualifierLoc());
John McCallf7a1a742009-11-24 19:00:30 +00002414 Expr *E = DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002415 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002416 NameInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002417
Douglas Gregora7fc9012011-01-05 18:58:31 +00002418 // If we parsed the template argument as a pack expansion, create a
2419 // pack expansion expression.
2420 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002421 ExprResult Expansion = ActOnPackExpansion(E,
Douglas Gregora7fc9012011-01-05 18:58:31 +00002422 Arg.getTemplateEllipsisLoc());
2423 if (Expansion.isInvalid())
2424 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002425
Douglas Gregora7fc9012011-01-05 18:58:31 +00002426 E = Expansion.get();
2427 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002428
Douglas Gregore7526412009-11-11 19:31:23 +00002429 TemplateArgument Result;
2430 if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
2431 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002432
Douglas Gregor910f8002010-11-07 23:05:16 +00002433 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002434 break;
2435 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002436
Douglas Gregore7526412009-11-11 19:31:23 +00002437 // We have a template argument that actually does refer to a class
2438 // template, template alias, or template template parameter, and
2439 // therefore cannot be a non-type template argument.
2440 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2441 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002442
Douglas Gregore7526412009-11-11 19:31:23 +00002443 Diag(Param->getLocation(), diag::note_template_param_here);
2444 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002445
Douglas Gregore7526412009-11-11 19:31:23 +00002446 case TemplateArgument::Type: {
2447 // We have a non-type template parameter but the template
2448 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002449
Douglas Gregore7526412009-11-11 19:31:23 +00002450 // C++ [temp.arg]p2:
2451 // In a template-argument, an ambiguity between a type-id and
2452 // an expression is resolved to a type-id, regardless of the
2453 // form of the corresponding template-parameter.
2454 //
2455 // We warn specifically about this case, since it can be rather
2456 // confusing for users.
2457 QualType T = Arg.getArgument().getAsType();
2458 SourceRange SR = Arg.getSourceRange();
2459 if (T->isFunctionType())
2460 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2461 else
2462 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2463 Diag(Param->getLocation(), diag::note_template_param_here);
2464 return true;
2465 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002466
Douglas Gregore7526412009-11-11 19:31:23 +00002467 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002468 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002469 break;
2470 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002471
Douglas Gregore7526412009-11-11 19:31:23 +00002472 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002473 }
2474
2475
Douglas Gregore7526412009-11-11 19:31:23 +00002476 // Check template template parameters.
2477 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002478
Douglas Gregore7526412009-11-11 19:31:23 +00002479 // Substitute into the template parameter list of the template
2480 // template parameter, since previously-supplied template arguments
2481 // may appear within the template template parameter.
2482 {
2483 // Set up a template instantiation context.
2484 LocalInstantiationScope Scope(*this);
2485 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002486 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002487 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002488
2489 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002490 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002491 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002492 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002493 MultiLevelTemplateArgumentList(TemplateArgs)));
2494 if (!TempParm)
2495 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002496 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002497
Douglas Gregore7526412009-11-11 19:31:23 +00002498 switch (Arg.getArgument().getKind()) {
2499 case TemplateArgument::Null:
2500 assert(false && "Should never see a NULL template argument here");
2501 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002502
Douglas Gregore7526412009-11-11 19:31:23 +00002503 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002504 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002505 if (CheckTemplateArgument(TempParm, Arg))
2506 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002507
Douglas Gregor910f8002010-11-07 23:05:16 +00002508 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002509 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002510
Douglas Gregore7526412009-11-11 19:31:23 +00002511 case TemplateArgument::Expression:
2512 case TemplateArgument::Type:
2513 // We have a template template parameter but the template
2514 // argument does not refer to a template.
2515 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
2516 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002517
Douglas Gregore7526412009-11-11 19:31:23 +00002518 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002519 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002520 "Declaration argument with template template parameter");
2521 break;
2522 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002523 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002524 "Integral argument with template template parameter");
2525 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002526
Douglas Gregore7526412009-11-11 19:31:23 +00002527 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002528 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002529 break;
2530 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002531
Douglas Gregore7526412009-11-11 19:31:23 +00002532 return false;
2533}
2534
Douglas Gregorc15cb382009-02-09 23:23:08 +00002535/// \brief Check that the given template argument list is well-formed
2536/// for specializing the given template.
2537bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2538 SourceLocation TemplateLoc,
John McCalld5532b62009-11-23 01:53:49 +00002539 const TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002540 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002541 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002542 TemplateParameterList *Params = Template->getTemplateParameters();
2543 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002544 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002545 bool Invalid = false;
2546
John McCalld5532b62009-11-23 01:53:49 +00002547 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2548
Mike Stump1eb44332009-09-09 15:08:12 +00002549 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002550 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002551
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002552 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002553 (NumArgs < Params->getMinRequiredArguments() &&
2554 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002555 // FIXME: point at either the first arg beyond what we can handle,
2556 // or the '>', depending on whether we have too many or too few
2557 // arguments.
2558 SourceRange Range;
2559 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002560 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002561 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2562 << (NumArgs > NumParams)
2563 << (isa<ClassTemplateDecl>(Template)? 0 :
2564 isa<FunctionTemplateDecl>(Template)? 1 :
2565 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2566 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002567 Diag(Template->getLocation(), diag::note_template_decl_here)
2568 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002569 Invalid = true;
2570 }
Mike Stump1eb44332009-09-09 15:08:12 +00002571
2572 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002573 // [...] The type and form of each template-argument specified in
2574 // a template-id shall match the type and form specified for the
2575 // corresponding parameter declared by the template in its
2576 // template-parameter-list.
Douglas Gregor14be16b2010-12-20 16:57:52 +00002577 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2578 TemplateParameterList::iterator Param = Params->begin(),
2579 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002580 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002581 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002582 while (Param != ParamEnd) {
Douglas Gregor16134c62009-07-01 00:28:38 +00002583 if (ArgIdx > NumArgs && PartialTemplateArgs)
2584 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002585
Douglas Gregorf35f8282009-11-11 21:54:23 +00002586 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002587 // If we have an expanded parameter pack, make sure we don't have too
2588 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002589 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002590 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002591 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002592 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2593 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2594 << true
2595 << (isa<ClassTemplateDecl>(Template)? 0 :
2596 isa<FunctionTemplateDecl>(Template)? 1 :
2597 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2598 << Template;
2599 Diag(Template->getLocation(), diag::note_template_decl_here)
2600 << Params->getSourceRange();
2601 return true;
2602 }
2603 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002604
Douglas Gregorf35f8282009-11-11 21:54:23 +00002605 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002606 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2607 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002608 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002609 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002610
Douglas Gregor14be16b2010-12-20 16:57:52 +00002611 if ((*Param)->isTemplateParameterPack()) {
2612 // The template parameter was a template parameter pack, so take the
2613 // deduced argument and place it on the argument pack. Note that we
2614 // stay on the same template parameter so that we can deduce more
2615 // arguments.
2616 ArgumentPack.push_back(Converted.back());
2617 Converted.pop_back();
2618 } else {
2619 // Move to the next template parameter.
2620 ++Param;
2621 }
2622 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002623 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002624 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002625
2626 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002627 // arguments, just break out now and we'll fill in the argument pack below.
2628 if ((*Param)->isTemplateParameterPack())
2629 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002630
Douglas Gregorf35f8282009-11-11 21:54:23 +00002631 // We have a default template argument that we will use.
2632 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002633
Douglas Gregorf35f8282009-11-11 21:54:23 +00002634 // Retrieve the default template argument from the template
2635 // parameter. For each kind of template parameter, we substitute the
2636 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002637 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002638 // the default argument.
2639 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2640 if (!TTP->hasDefaultArgument()) {
2641 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2642 break;
2643 }
2644
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002645 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002646 Template,
2647 TemplateLoc,
2648 RAngleLoc,
2649 TTP,
2650 Converted);
2651 if (!ArgType)
2652 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002653
Douglas Gregorf35f8282009-11-11 21:54:23 +00002654 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2655 ArgType);
2656 } else if (NonTypeTemplateParmDecl *NTTP
2657 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2658 if (!NTTP->hasDefaultArgument()) {
2659 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2660 break;
2661 }
2662
John McCall60d7b3a2010-08-24 06:29:42 +00002663 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002664 TemplateLoc,
2665 RAngleLoc,
2666 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002667 Converted);
2668 if (E.isInvalid())
2669 return true;
2670
2671 Expr *Ex = E.takeAs<Expr>();
2672 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2673 } else {
2674 TemplateTemplateParmDecl *TempParm
2675 = cast<TemplateTemplateParmDecl>(*Param);
2676
2677 if (!TempParm->hasDefaultArgument()) {
2678 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2679 break;
2680 }
2681
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002682 NestedNameSpecifierLoc QualifierLoc
2683 = TempParm->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorf35f8282009-11-11 21:54:23 +00002684 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002685 TemplateLoc,
2686 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002687 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002688 Converted,
2689 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002690 if (Name.isNull())
2691 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002692
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002693 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
2694 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00002695 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002696
Douglas Gregorf35f8282009-11-11 21:54:23 +00002697 // Introduce an instantiation record that describes where we are using
2698 // the default template argument.
2699 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002700 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002701 SourceRange(TemplateLoc, RAngleLoc));
2702
Douglas Gregorf35f8282009-11-11 21:54:23 +00002703 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002704 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002705 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00002706 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002707
Douglas Gregor14be16b2010-12-20 16:57:52 +00002708 // Move to the next template parameter and argument.
2709 ++Param;
2710 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002711 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002712
Douglas Gregor14be16b2010-12-20 16:57:52 +00002713 // Form argument packs for each of the parameter packs remaining.
2714 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00002715 // If we're checking a partial list of template arguments, don't fill
2716 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002717
2718 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002719 if (PartialTemplateArgs && ArgumentPack.empty()) {
2720 Converted.push_back(TemplateArgument());
Douglas Gregor203e6a32011-01-11 23:09:57 +00002721 } else if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00002722 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00002723 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002724 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2725 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00002726 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00002727 ArgumentPack.clear();
2728 }
2729 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002730
Douglas Gregor14be16b2010-12-20 16:57:52 +00002731 ++Param;
2732 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002733
Douglas Gregorc15cb382009-02-09 23:23:08 +00002734 return Invalid;
2735}
2736
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002737namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002738 class UnnamedLocalNoLinkageFinder
2739 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002740 {
2741 Sema &S;
2742 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002743
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002744 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002745
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002746 public:
2747 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
2748
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002749 bool Visit(QualType T) {
2750 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002751 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002752
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002753#define TYPE(Class, Parent) \
2754 bool Visit##Class##Type(const Class##Type *);
2755#define ABSTRACT_TYPE(Class, Parent) \
2756 bool Visit##Class##Type(const Class##Type *) { return false; }
2757#define NON_CANONICAL_TYPE(Class, Parent) \
2758 bool Visit##Class##Type(const Class##Type *) { return false; }
2759#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002760
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002761 bool VisitTagDecl(const TagDecl *Tag);
2762 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
2763 };
2764}
2765
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002766bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002767 return false;
2768}
2769
2770bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
2771 return Visit(T->getElementType());
2772}
2773
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002774bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002775 return Visit(T->getPointeeType());
2776}
2777
2778bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002779 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002780 return Visit(T->getPointeeType());
2781}
2782
2783bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002784 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002785 return Visit(T->getPointeeType());
2786}
2787
2788bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002789 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002790 return Visit(T->getPointeeType());
2791}
2792
2793bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002794 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002795 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
2796}
2797
2798bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002799 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002800 return Visit(T->getElementType());
2801}
2802
2803bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002804 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002805 return Visit(T->getElementType());
2806}
2807
2808bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002809 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002810 return Visit(T->getElementType());
2811}
2812
2813bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002814 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002815 return Visit(T->getElementType());
2816}
2817
2818bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002819 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002820 return Visit(T->getElementType());
2821}
2822
2823bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
2824 return Visit(T->getElementType());
2825}
2826
2827bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
2828 return Visit(T->getElementType());
2829}
2830
2831bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
2832 const FunctionProtoType* T) {
2833 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002834 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002835 A != AEnd; ++A) {
2836 if (Visit(*A))
2837 return true;
2838 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002839
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002840 return Visit(T->getResultType());
2841}
2842
2843bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
2844 const FunctionNoProtoType* T) {
2845 return Visit(T->getResultType());
2846}
2847
2848bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
2849 const UnresolvedUsingType*) {
2850 return false;
2851}
2852
2853bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
2854 return false;
2855}
2856
2857bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
2858 return Visit(T->getUnderlyingType());
2859}
2860
2861bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
2862 return false;
2863}
2864
Richard Smith34b41d92011-02-20 03:19:35 +00002865bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
2866 return Visit(T->getDeducedType());
2867}
2868
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002869bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
2870 return VisitTagDecl(T->getDecl());
2871}
2872
2873bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
2874 return VisitTagDecl(T->getDecl());
2875}
2876
2877bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
2878 const TemplateTypeParmType*) {
2879 return false;
2880}
2881
Douglas Gregorc3069d62011-01-14 02:55:32 +00002882bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
2883 const SubstTemplateTypeParmPackType *) {
2884 return false;
2885}
2886
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002887bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
2888 const TemplateSpecializationType*) {
2889 return false;
2890}
2891
2892bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
2893 const InjectedClassNameType* T) {
2894 return VisitTagDecl(T->getDecl());
2895}
2896
2897bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
2898 const DependentNameType* T) {
2899 return VisitNestedNameSpecifier(T->getQualifier());
2900}
2901
2902bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
2903 const DependentTemplateSpecializationType* T) {
2904 return VisitNestedNameSpecifier(T->getQualifier());
2905}
2906
Douglas Gregor7536dd52010-12-20 02:24:11 +00002907bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
2908 const PackExpansionType* T) {
2909 return Visit(T->getPattern());
2910}
2911
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002912bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
2913 return false;
2914}
2915
2916bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
2917 const ObjCInterfaceType *) {
2918 return false;
2919}
2920
2921bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
2922 const ObjCObjectPointerType *) {
2923 return false;
2924}
2925
2926bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
2927 if (Tag->getDeclContext()->isFunctionOrMethod()) {
2928 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
2929 << S.Context.getTypeDeclType(Tag) << SR;
2930 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002931 }
2932
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002933 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) {
2934 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
2935 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
2936 return true;
2937 }
2938
2939 return false;
2940}
2941
2942bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
2943 NestedNameSpecifier *NNS) {
2944 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
2945 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002946
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002947 switch (NNS->getKind()) {
2948 case NestedNameSpecifier::Identifier:
2949 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00002950 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002951 case NestedNameSpecifier::Global:
2952 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002953
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002954 case NestedNameSpecifier::TypeSpec:
2955 case NestedNameSpecifier::TypeSpecWithTemplate:
2956 return Visit(QualType(NNS->getAsType(), 0));
2957 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00002958 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002959}
2960
2961
Douglas Gregorc15cb382009-02-09 23:23:08 +00002962/// \brief Check a template argument against its corresponding
2963/// template type parameter.
2964///
2965/// This routine implements the semantics of C++ [temp.arg.type]. It
2966/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00002967bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00002968 TypeSourceInfo *ArgInfo) {
2969 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00002970 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00002971 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00002972
2973 if (Arg->isVariablyModifiedType()) {
2974 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00002975 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00002976 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002977 }
2978
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002979 // C++03 [temp.arg.type]p2:
2980 // A local type, a type with no linkage, an unnamed type or a type
2981 // compounded from any of these types shall not be used as a
2982 // template-argument for a template type-parameter.
2983 //
2984 // C++0x allows these, and even in C++03 we allow them as an extension with
2985 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00002986 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002987 UnnamedLocalNoLinkageFinder Finder(*this, SR);
2988 (void)Finder.Visit(Context.getCanonicalType(Arg));
2989 }
2990
Douglas Gregorc15cb382009-02-09 23:23:08 +00002991 return false;
2992}
2993
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002994/// \brief Checks whether the given template argument is the address
2995/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002996static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00002997CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
2998 NonTypeTemplateParmDecl *Param,
2999 QualType ParamType,
3000 Expr *ArgIn,
3001 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003002 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003003 Expr *Arg = ArgIn;
3004 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003005
3006 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003007 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003008 Arg = Cast->getSubExpr();
3009
3010 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003011 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003012 // A template-argument for a non-type, non-template
3013 // template-parameter shall be one of: [...]
3014 //
3015 // -- the address of an object or function with external
3016 // linkage, including function templates and function
3017 // template-ids but excluding non-static class members,
3018 // expressed as & id-expression where the & is optional if
3019 // the name refers to a function or array, or if the
3020 // corresponding template-parameter is a reference; or
3021 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003022
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003023 // In C++98/03 mode, give an extension warning on any extra parentheses.
3024 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3025 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003026 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003027 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003028 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003029 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003030 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003031 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003032 }
3033
3034 Arg = Parens->getSubExpr();
3035 }
3036
Douglas Gregorb7a09262010-04-01 18:32:35 +00003037 bool AddressTaken = false;
3038 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003039 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003040 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003041 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00003042 AddressTaken = true;
3043 AddrOpLoc = UnOp->getOperatorLoc();
3044 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003045 } else
3046 DRE = dyn_cast<DeclRefExpr>(Arg);
3047
Douglas Gregorb7a09262010-04-01 18:32:35 +00003048 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003049 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3050 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003051 S.Diag(Param->getLocation(), diag::note_template_param_here);
3052 return true;
3053 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003054
3055 // Stop checking the precise nature of the argument if it is value dependent,
3056 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003057 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003058 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003059 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003060 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003061
Douglas Gregorb7a09262010-04-01 18:32:35 +00003062 if (!isa<ValueDecl>(DRE->getDecl())) {
3063 S.Diag(Arg->getSourceRange().getBegin(),
3064 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003065 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003066 S.Diag(Param->getLocation(), diag::note_template_param_here);
3067 return true;
3068 }
3069
3070 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003071
3072 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003073 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3074 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003075 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003076 S.Diag(Param->getLocation(), diag::note_template_param_here);
3077 return true;
3078 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003079
3080 // Cannot refer to non-static member functions
3081 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003082 if (!Method->isStatic()) {
3083 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003084 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003085 S.Diag(Param->getLocation(), diag::note_template_param_here);
3086 return true;
3087 }
Mike Stump1eb44332009-09-09 15:08:12 +00003088
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003089 // Functions must have external linkage.
3090 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003091 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003092 S.Diag(Arg->getSourceRange().getBegin(),
3093 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003094 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003095 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003096 << true;
3097 return true;
3098 }
3099
3100 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003101 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003102
Douglas Gregorb7a09262010-04-01 18:32:35 +00003103 // If the template parameter has pointer type, the function decays.
3104 if (ParamType->isPointerType() && !AddressTaken)
3105 ArgType = S.Context.getPointerType(Func->getType());
3106 else if (AddressTaken && ParamType->isReferenceType()) {
3107 // If we originally had an address-of operator, but the
3108 // parameter has reference type, complain and (if things look
3109 // like they will work) drop the address-of operator.
3110 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3111 ParamType.getNonReferenceType())) {
3112 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3113 << ParamType;
3114 S.Diag(Param->getLocation(), diag::note_template_param_here);
3115 return true;
3116 }
3117
3118 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3119 << ParamType
3120 << FixItHint::CreateRemoval(AddrOpLoc);
3121 S.Diag(Param->getLocation(), diag::note_template_param_here);
3122
3123 ArgType = Func->getType();
3124 }
3125 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003126 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003127 S.Diag(Arg->getSourceRange().getBegin(),
3128 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003129 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003130 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003131 << true;
3132 return true;
3133 }
3134
Douglas Gregorb7a09262010-04-01 18:32:35 +00003135 // A value of reference type is not an object.
3136 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003137 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003138 diag::err_template_arg_reference_var)
3139 << Var->getType() << Arg->getSourceRange();
3140 S.Diag(Param->getLocation(), diag::note_template_param_here);
3141 return true;
3142 }
3143
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003144 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003145 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003146
3147 // If the template parameter has pointer type, we must have taken
3148 // the address of this object.
3149 if (ParamType->isReferenceType()) {
3150 if (AddressTaken) {
3151 // If we originally had an address-of operator, but the
3152 // parameter has reference type, complain and (if things look
3153 // like they will work) drop the address-of operator.
3154 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3155 ParamType.getNonReferenceType())) {
3156 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3157 << ParamType;
3158 S.Diag(Param->getLocation(), diag::note_template_param_here);
3159 return true;
3160 }
3161
3162 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3163 << ParamType
3164 << FixItHint::CreateRemoval(AddrOpLoc);
3165 S.Diag(Param->getLocation(), diag::note_template_param_here);
3166
3167 ArgType = Var->getType();
3168 }
3169 } else if (!AddressTaken && ParamType->isPointerType()) {
3170 if (Var->getType()->isArrayType()) {
3171 // Array-to-pointer decay.
3172 ArgType = S.Context.getArrayDecayedType(Var->getType());
3173 } else {
3174 // If the template parameter has pointer type but the address of
3175 // this object was not taken, complain and (possibly) recover by
3176 // taking the address of the entity.
3177 ArgType = S.Context.getPointerType(Var->getType());
3178 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3179 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3180 << ParamType;
3181 S.Diag(Param->getLocation(), diag::note_template_param_here);
3182 return true;
3183 }
3184
3185 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3186 << ParamType
3187 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3188
3189 S.Diag(Param->getLocation(), diag::note_template_param_here);
3190 }
3191 }
3192 } else {
3193 // We found something else, but we don't know specifically what it is.
3194 S.Diag(Arg->getSourceRange().getBegin(),
3195 diag::err_template_arg_not_object_or_func)
3196 << Arg->getSourceRange();
3197 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3198 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003199 }
Mike Stump1eb44332009-09-09 15:08:12 +00003200
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003201 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003202 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003203 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003204 // For pointer-to-object types, qualification conversions are
3205 // permitted.
3206 } else {
3207 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3208 if (!ParamRef->getPointeeType()->isFunctionType()) {
3209 // C++ [temp.arg.nontype]p5b3:
3210 // For a non-type template-parameter of type reference to
3211 // object, no conversions apply. The type referred to by the
3212 // reference may be more cv-qualified than the (otherwise
3213 // identical) type of the template- argument. The
3214 // template-parameter is bound directly to the
3215 // template-argument, which shall be an lvalue.
3216
3217 // FIXME: Other qualifiers?
3218 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3219 unsigned ArgQuals = ArgType.getCVRQualifiers();
3220
3221 if ((ParamQuals | ArgQuals) != ParamQuals) {
3222 S.Diag(Arg->getSourceRange().getBegin(),
3223 diag::err_template_arg_ref_bind_ignores_quals)
3224 << ParamType << Arg->getType()
3225 << Arg->getSourceRange();
3226 S.Diag(Param->getLocation(), diag::note_template_param_here);
3227 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003228 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003229 }
3230 }
3231
3232 // At this point, the template argument refers to an object or
3233 // function with external linkage. We now need to check whether the
3234 // argument and parameter types are compatible.
3235 if (!S.Context.hasSameUnqualifiedType(ArgType,
3236 ParamType.getNonReferenceType())) {
3237 // We can't perform this conversion or binding.
3238 if (ParamType->isReferenceType())
3239 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3240 << ParamType << Arg->getType() << Arg->getSourceRange();
3241 else
3242 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3243 << Arg->getType() << ParamType << Arg->getSourceRange();
3244 S.Diag(Param->getLocation(), diag::note_template_param_here);
3245 return true;
3246 }
3247 }
3248
3249 // Create the template argument.
3250 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003251 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003252 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003253}
3254
3255/// \brief Checks whether the given template argument is a pointer to
3256/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003257bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003258 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003259 bool Invalid = false;
3260
3261 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003262 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003263 Arg = Cast->getSubExpr();
3264
3265 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003266 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003267 // A template-argument for a non-type, non-template
3268 // template-parameter shall be one of: [...]
3269 //
3270 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003271 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003272
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003273 // In C++98/03 mode, give an extension warning on any extra parentheses.
3274 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3275 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003276 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003277 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003278 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003279 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003280 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003281 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003282 }
3283
3284 Arg = Parens->getSubExpr();
3285 }
3286
Douglas Gregorcaddba02009-11-12 18:38:13 +00003287 // A pointer-to-member constant written &Class::member.
3288 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003289 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003290 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3291 if (DRE && !DRE->getQualifier())
3292 DRE = 0;
3293 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003294 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003295 // A constant of pointer-to-member type.
3296 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3297 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3298 if (VD->getType()->isMemberPointerType()) {
3299 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003300 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003301 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3302 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003303 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003304 else
3305 Converted = TemplateArgument(VD->getCanonicalDecl());
3306 return Invalid;
3307 }
3308 }
3309 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003310
Douglas Gregorcaddba02009-11-12 18:38:13 +00003311 DRE = 0;
3312 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003313
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003314 if (!DRE)
3315 return Diag(Arg->getSourceRange().getBegin(),
3316 diag::err_template_arg_not_pointer_to_member_form)
3317 << Arg->getSourceRange();
3318
3319 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3320 assert((isa<FieldDecl>(DRE->getDecl()) ||
3321 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3322 "Only non-static member pointers can make it here");
3323
3324 // Okay: this is the address of a non-static member, and therefore
3325 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003326 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003327 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003328 else
3329 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003330 return Invalid;
3331 }
3332
3333 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003334 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003335 diag::err_template_arg_not_pointer_to_member_form)
3336 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003337 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003338 diag::note_template_arg_refers_here);
3339 return true;
3340}
3341
Douglas Gregorc15cb382009-02-09 23:23:08 +00003342/// \brief Check a template argument against its corresponding
3343/// non-type template parameter.
3344///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003345/// This routine implements the semantics of C++ [temp.arg.nontype].
3346/// It returns true if an error occurred, and false otherwise. \p
3347/// InstantiatedParamType is the type of the non-type template
3348/// parameter after it has been instantiated.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003349///
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003350/// If no error was detected, Converted receives the converted template argument.
Douglas Gregorc15cb382009-02-09 23:23:08 +00003351bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
Mike Stump1eb44332009-09-09 15:08:12 +00003352 QualType InstantiatedParamType, Expr *&Arg,
Douglas Gregor02024a92010-03-28 02:42:43 +00003353 TemplateArgument &Converted,
3354 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003355 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3356
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003357 // If either the parameter has a dependent type or the argument is
3358 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003359 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3360 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003361 Converted = TemplateArgument(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003362 return false;
Douglas Gregor40808ce2009-03-09 23:48:35 +00003363 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003364
3365 // C++ [temp.arg.nontype]p5:
3366 // The following conversions are performed on each expression used
3367 // as a non-type template-argument. If a non-type
3368 // template-argument cannot be converted to the type of the
3369 // corresponding template-parameter then the program is
3370 // ill-formed.
3371 //
3372 // -- for a non-type template-parameter of integral or
3373 // enumeration type, integral promotions (4.5) and integral
3374 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003375 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003376 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003377 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003378 // C++ [temp.arg.nontype]p1:
3379 // A template-argument for a non-type, non-template
3380 // template-parameter shall be one of:
3381 //
3382 // -- an integral constant-expression of integral or enumeration
3383 // type; or
3384 // -- the name of a non-type template-parameter; or
3385 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003386 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003387 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003388 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003389 diag::err_template_arg_not_integral_or_enumeral)
3390 << ArgType << Arg->getSourceRange();
3391 Diag(Param->getLocation(), diag::note_template_param_here);
3392 return true;
3393 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003394 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003395 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3396 << ArgType << Arg->getSourceRange();
3397 return true;
3398 }
3399
Douglas Gregor02024a92010-03-28 02:42:43 +00003400 // From here on out, all we care about are the unqualified forms
3401 // of the parameter and argument types.
3402 ParamType = ParamType.getUnqualifiedType();
3403 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003404
3405 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003406 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003407 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003408 } else if (CTAK == CTAK_Deduced) {
3409 // C++ [temp.deduct.type]p17:
3410 // If, in the declaration of a function template with a non-type
3411 // template-parameter, the non-type template- parameter is used
3412 // in an expression in the function parameter-list and, if the
3413 // corresponding template-argument is deduced, the
3414 // template-argument type shall match the type of the
3415 // template-parameter exactly, except that a template-argument
3416 // deduced from an array bound may be of any integral type.
3417 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3418 << ArgType << ParamType;
3419 Diag(Param->getLocation(), diag::note_template_param_here);
John McCalldaa8e4e2010-11-15 09:13:47 +00003420 return true;
3421 } else if (ParamType->isBooleanType()) {
3422 // This is an integral-to-boolean conversion.
3423 ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003424 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3425 !ParamType->isEnumeralType()) {
3426 // This is an integral promotion or conversion.
John McCall2de56d12010-08-25 11:45:40 +00003427 ImpCastExprToType(Arg, ParamType, CK_IntegralCast);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003428 } else {
3429 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003430 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003431 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003432 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003433 Diag(Param->getLocation(), diag::note_template_param_here);
3434 return true;
3435 }
3436
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003437 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003438 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003439 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003440
3441 if (!Arg->isValueDependent()) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003442 llvm::APSInt OldValue = Value;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003443
3444 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003445 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003446 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003447 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003448 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003449 Value.setIsSigned(IntegerType->isSignedIntegerType());
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003450
3451 // Complain if an unsigned parameter received a negative value.
3452 if (IntegerType->isUnsignedIntegerType()
3453 && (OldValue.isSigned() && OldValue.isNegative())) {
3454 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3455 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3456 << Arg->getSourceRange();
3457 Diag(Param->getLocation(), diag::note_template_param_here);
3458 }
3459
3460 // Complain if we overflowed the template parameter's type.
3461 unsigned RequiredBits;
3462 if (IntegerType->isUnsignedIntegerType())
3463 RequiredBits = OldValue.getActiveBits();
3464 else if (OldValue.isUnsigned())
3465 RequiredBits = OldValue.getActiveBits() + 1;
3466 else
3467 RequiredBits = OldValue.getMinSignedBits();
3468 if (RequiredBits > AllowedBits) {
3469 Diag(Arg->getSourceRange().getBegin(),
3470 diag::warn_template_arg_too_large)
3471 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3472 << Arg->getSourceRange();
3473 Diag(Param->getLocation(), diag::note_template_param_here);
3474 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003475 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003476
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003477 // Add the value of this argument to the list of converted
3478 // arguments. We use the bitwidth and signedness of the template
3479 // parameter.
3480 if (Arg->isValueDependent()) {
3481 // The argument is value-dependent. Create a new
3482 // TemplateArgument with the converted expression.
3483 Converted = TemplateArgument(Arg);
3484 return false;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003485 }
3486
John McCall833ca992009-10-29 08:12:44 +00003487 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003488 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003489 : IntegerType);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003490 return false;
3491 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003492
John McCall6bb80172010-03-30 21:47:33 +00003493 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3494
Douglas Gregorb7a09262010-04-01 18:32:35 +00003495 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3496 // from a template argument of type std::nullptr_t to a non-type
3497 // template parameter of type pointer to object, pointer to
3498 // function, or pointer-to-member, respectively.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003499 if (ArgType->isNullPtrType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003500 (ParamType->isPointerType() || ParamType->isMemberPointerType())) {
3501 Converted = TemplateArgument((NamedDecl *)0);
3502 return false;
3503 }
3504
Douglas Gregorb86b0572009-02-11 01:18:59 +00003505 // Handle pointer-to-function, reference-to-function, and
3506 // pointer-to-member-function all in (roughly) the same way.
3507 if (// -- For a non-type template-parameter of type pointer to
3508 // function, only the function-to-pointer conversion (4.3) is
3509 // applied. If the template-argument represents a set of
3510 // overloaded functions (or a pointer to such), the matching
3511 // function is selected from the set (13.4).
3512 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003513 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003514 // -- For a non-type template-parameter of type reference to
3515 // function, no conversions apply. If the template-argument
3516 // represents a set of overloaded functions, the matching
3517 // function is selected from the set (13.4).
3518 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003519 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003520 // -- For a non-type template-parameter of type pointer to
3521 // member function, no conversions apply. If the
3522 // template-argument represents a set of overloaded member
3523 // functions, the matching member function is selected from
3524 // the set (13.4).
3525 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003526 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003527 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003528
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003529 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003530 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003531 true,
3532 FoundResult)) {
3533 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3534 return true;
3535
3536 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3537 ArgType = Arg->getType();
3538 } else
Douglas Gregor48f3bb92009-02-18 21:56:37 +00003539 return true;
Douglas Gregora35284b2009-02-11 00:19:33 +00003540 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003541
Douglas Gregorb7a09262010-04-01 18:32:35 +00003542 if (!ParamType->isMemberPointerType())
3543 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003544 ParamType,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003545 Arg, Converted);
3546
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003547 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3548 false)) {
John McCall2de56d12010-08-25 11:45:40 +00003549 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregorb7a09262010-04-01 18:32:35 +00003550 } else if (!Context.hasSameUnqualifiedType(ArgType,
3551 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003552 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003553 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003554 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003555 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003556 Diag(Param->getLocation(), diag::note_template_param_here);
3557 return true;
3558 }
Mike Stump1eb44332009-09-09 15:08:12 +00003559
Douglas Gregorb7a09262010-04-01 18:32:35 +00003560 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregora35284b2009-02-11 00:19:33 +00003561 }
3562
Chris Lattnerfe90de72009-02-20 21:37:53 +00003563 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003564 // -- for a non-type template-parameter of type pointer to
3565 // object, qualification conversions (4.4) and the
3566 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003567 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003568 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003569 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003570
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003571 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003572 ParamType,
3573 Arg, Converted);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003574 }
Mike Stump1eb44332009-09-09 15:08:12 +00003575
Ted Kremenek6217b802009-07-29 21:53:49 +00003576 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003577 // -- For a non-type template-parameter of type reference to
3578 // object, no conversions apply. The type referred to by the
3579 // reference may be more cv-qualified than the (otherwise
3580 // identical) type of the template-argument. The
3581 // template-parameter is bound directly to the
3582 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003583 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003584 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003585
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003586 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003587 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3588 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003589 true,
3590 FoundResult)) {
3591 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3592 return true;
3593
3594 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3595 ArgType = Arg->getType();
3596 } else
Douglas Gregorb7a09262010-04-01 18:32:35 +00003597 return true;
Douglas Gregorb86b0572009-02-11 01:18:59 +00003598 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003599
3600 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003601 ParamType,
3602 Arg, Converted);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003603 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003604
3605 // -- For a non-type template-parameter of type pointer to data
3606 // member, qualification conversions (4.4) are applied.
3607 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3608
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003609 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003610 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003611 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John McCall2de56d12010-08-25 11:45:40 +00003612 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregor658bbb52009-02-11 16:16:59 +00003613 } else {
3614 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003615 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003616 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003617 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003618 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00003619 return true;
Douglas Gregor658bbb52009-02-11 16:16:59 +00003620 }
3621
Douglas Gregorcaddba02009-11-12 18:38:13 +00003622 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003623}
3624
3625/// \brief Check a template argument against its corresponding
3626/// template template parameter.
3627///
3628/// This routine implements the semantics of C++ [temp.arg.template].
3629/// It returns true if an error occurred, and false otherwise.
3630bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003631 const TemplateArgumentLoc &Arg) {
3632 TemplateName Name = Arg.getArgument().getAsTemplate();
3633 TemplateDecl *Template = Name.getAsTemplateDecl();
3634 if (!Template) {
3635 // Any dependent template name is fine.
3636 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3637 return false;
3638 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003639
3640 // C++ [temp.arg.template]p1:
3641 // A template-argument for a template template-parameter shall be
3642 // the name of a class template, expressed as id-expression. Only
3643 // primary class templates are considered when matching the
3644 // template template argument with the corresponding parameter;
3645 // partial specializations are not considered even if their
3646 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003647 //
3648 // Note that we also allow template template parameters here, which
3649 // will happen when we are dealing with, e.g., class template
3650 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003651 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003652 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003653 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003654 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003655 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003656 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003657 << Template;
3658 }
3659
3660 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
3661 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003662 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003663 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00003664 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00003665}
3666
Douglas Gregor02024a92010-03-28 02:42:43 +00003667/// \brief Given a non-type template argument that refers to a
3668/// declaration and the type of its corresponding non-type template
3669/// parameter, produce an expression that properly refers to that
3670/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003671ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003672Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
3673 QualType ParamType,
3674 SourceLocation Loc) {
3675 assert(Arg.getKind() == TemplateArgument::Declaration &&
3676 "Only declaration template arguments permitted here");
3677 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
3678
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003679 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00003680 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
3681 // If the value is a class member, we might have a pointer-to-member.
3682 // Determine whether the non-type template template parameter is of
3683 // pointer-to-member type. If so, we need to build an appropriate
3684 // expression for a pointer-to-member, since a "normal" DeclRefExpr
3685 // would refer to the member itself.
3686 if (ParamType->isMemberPointerType()) {
3687 QualType ClassType
3688 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
3689 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00003690 = NestedNameSpecifier::Create(Context, 0, false,
3691 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00003692 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00003693 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00003694
3695 // The actual value-ness of this is unimportant, but for
3696 // internal consistency's sake, references to instance methods
3697 // are r-values.
3698 ExprValueKind VK = VK_LValue;
3699 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
3700 VK = VK_RValue;
3701
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003702 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00003703 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00003704 VK,
John McCallf89e55a2010-11-18 06:31:45 +00003705 Loc,
3706 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00003707 if (RefExpr.isInvalid())
3708 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003709
John McCall2de56d12010-08-25 11:45:40 +00003710 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003711
Douglas Gregorc0c83002010-04-30 21:46:38 +00003712 // We might need to perform a trailing qualification conversion, since
3713 // the element type on the parameter could be more qualified than the
3714 // element type in the expression we constructed.
3715 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003716 ParamType.getUnqualifiedType(), false)) {
Douglas Gregorc0c83002010-04-30 21:46:38 +00003717 Expr *RefE = RefExpr.takeAs<Expr>();
John McCall2de56d12010-08-25 11:45:40 +00003718 ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CK_NoOp);
Douglas Gregorc0c83002010-04-30 21:46:38 +00003719 RefExpr = Owned(RefE);
3720 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003721
Douglas Gregor02024a92010-03-28 02:42:43 +00003722 assert(!RefExpr.isInvalid() &&
3723 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00003724 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00003725 return move(RefExpr);
3726 }
3727 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003728
Douglas Gregor02024a92010-03-28 02:42:43 +00003729 QualType T = VD->getType().getNonReferenceType();
3730 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003731 // When the non-type template parameter is a pointer, take the
3732 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00003733 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003734 if (RefExpr.isInvalid())
3735 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003736
3737 if (T->isFunctionType() || T->isArrayType()) {
3738 // Decay functions and arrays.
3739 Expr *RefE = (Expr *)RefExpr.get();
3740 DefaultFunctionArrayConversion(RefE);
3741 if (RefE != RefExpr.get()) {
3742 RefExpr.release();
3743 RefExpr = Owned(RefE);
3744 }
3745
3746 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00003747 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003748
Douglas Gregorb7a09262010-04-01 18:32:35 +00003749 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00003750 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00003751 }
3752
John McCallf89e55a2010-11-18 06:31:45 +00003753 ExprValueKind VK = VK_RValue;
3754
Douglas Gregor02024a92010-03-28 02:42:43 +00003755 // If the non-type template parameter has reference type, qualify the
3756 // resulting declaration reference with the extra qualifiers on the
3757 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00003758 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
3759 VK = VK_LValue;
3760 T = Context.getQualifiedType(T,
3761 TargetRef->getPointeeType().getQualifiers());
3762 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003763
John McCallf89e55a2010-11-18 06:31:45 +00003764 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003765}
3766
3767/// \brief Construct a new expression that refers to the given
3768/// integral template argument with the given source-location
3769/// information.
3770///
3771/// This routine takes care of the mapping from an integral template
3772/// argument (which may have any integral type) to the appropriate
3773/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003774ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003775Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
3776 SourceLocation Loc) {
3777 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00003778 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00003779 QualType T = Arg.getIntegralType();
3780 if (T->isCharType() || T->isWideCharType())
3781 return Owned(new (Context) CharacterLiteral(
3782 Arg.getAsIntegral()->getZExtValue(),
3783 T->isWideCharType(),
3784 T,
3785 Loc));
3786 if (T->isBooleanType())
3787 return Owned(new (Context) CXXBoolLiteralExpr(
3788 Arg.getAsIntegral()->getBoolValue(),
3789 T,
3790 Loc));
3791
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003792 QualType BT;
3793 if (const EnumType *ET = T->getAs<EnumType>())
3794 BT = ET->getDecl()->getPromotionType();
3795 else
3796 BT = T;
3797
3798 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00003799 if (T->isEnumeralType()) {
3800 // FIXME: This is a hack. We need a better way to handle substituted
3801 // non-type template parameters.
3802 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast,
3803 E, 0,
3804 Context.getTrivialTypeSourceInfo(T, Loc),
3805 Loc, Loc);
3806 }
3807
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003808 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00003809}
3810
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003811/// \brief Match two template parameters within template parameter lists.
3812static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
3813 bool Complain,
3814 Sema::TemplateParameterListEqualKind Kind,
3815 SourceLocation TemplateArgLoc) {
3816 // Check the actual kind (type, non-type, template).
3817 if (Old->getKind() != New->getKind()) {
3818 if (Complain) {
3819 unsigned NextDiag = diag::err_template_param_different_kind;
3820 if (TemplateArgLoc.isValid()) {
3821 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3822 NextDiag = diag::note_template_param_different_kind;
3823 }
3824 S.Diag(New->getLocation(), NextDiag)
3825 << (Kind != Sema::TPL_TemplateMatch);
3826 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
3827 << (Kind != Sema::TPL_TemplateMatch);
3828 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003829
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003830 return false;
3831 }
3832
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003833 // Check that both are parameter packs are neither are parameter packs.
3834 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00003835 // template template parameter, the template template parameter can have
3836 // a parameter pack where the template template argument does not.
3837 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
3838 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3839 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003840 if (Complain) {
3841 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
3842 if (TemplateArgLoc.isValid()) {
3843 S.Diag(TemplateArgLoc,
3844 diag::err_template_arg_template_params_mismatch);
3845 NextDiag = diag::note_template_parameter_pack_non_pack;
3846 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003847
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003848 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
3849 : isa<NonTypeTemplateParmDecl>(New)? 1
3850 : 2;
3851 S.Diag(New->getLocation(), NextDiag)
3852 << ParamKind << New->isParameterPack();
3853 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
3854 << ParamKind << Old->isParameterPack();
3855 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003856
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003857 return false;
3858 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003859
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003860 // For non-type template parameters, check the type of the parameter.
3861 if (NonTypeTemplateParmDecl *OldNTTP
3862 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
3863 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003864
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003865 // If we are matching a template template argument to a template
3866 // template parameter and one of the non-type template parameter types
3867 // is dependent, then we must wait until template instantiation time
3868 // to actually compare the arguments.
3869 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3870 (OldNTTP->getType()->isDependentType() ||
3871 NewNTTP->getType()->isDependentType()))
3872 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003873
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003874 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
3875 if (Complain) {
3876 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
3877 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003878 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003879 diag::err_template_arg_template_params_mismatch);
3880 NextDiag = diag::note_template_nontype_parm_different_type;
3881 }
3882 S.Diag(NewNTTP->getLocation(), NextDiag)
3883 << NewNTTP->getType()
3884 << (Kind != Sema::TPL_TemplateMatch);
3885 S.Diag(OldNTTP->getLocation(),
3886 diag::note_template_nontype_parm_prev_declaration)
3887 << OldNTTP->getType();
3888 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003889
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003890 return false;
3891 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003892
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003893 return true;
3894 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003895
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003896 // For template template parameters, check the template parameter types.
3897 // The template parameter lists of template template
3898 // parameters must agree.
3899 if (TemplateTemplateParmDecl *OldTTP
3900 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003901 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003902 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
3903 OldTTP->getTemplateParameters(),
3904 Complain,
3905 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003906 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003907 : Kind),
3908 TemplateArgLoc);
3909 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003910
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003911 return true;
3912}
Douglas Gregor02024a92010-03-28 02:42:43 +00003913
Douglas Gregora0347822011-01-13 00:08:50 +00003914/// \brief Diagnose a known arity mismatch when comparing template argument
3915/// lists.
3916static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003917void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00003918 TemplateParameterList *New,
3919 TemplateParameterList *Old,
3920 Sema::TemplateParameterListEqualKind Kind,
3921 SourceLocation TemplateArgLoc) {
3922 unsigned NextDiag = diag::err_template_param_list_different_arity;
3923 if (TemplateArgLoc.isValid()) {
3924 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3925 NextDiag = diag::note_template_param_list_different_arity;
3926 }
3927 S.Diag(New->getTemplateLoc(), NextDiag)
3928 << (New->size() > Old->size())
3929 << (Kind != Sema::TPL_TemplateMatch)
3930 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
3931 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
3932 << (Kind != Sema::TPL_TemplateMatch)
3933 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
3934}
3935
Douglas Gregorddc29e12009-02-06 22:42:48 +00003936/// \brief Determine whether the given template parameter lists are
3937/// equivalent.
3938///
Mike Stump1eb44332009-09-09 15:08:12 +00003939/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00003940/// source code as part of a new template declaration.
3941///
3942/// \param Old The old template parameter list, typically found via
3943/// name lookup of the template declared with this template parameter
3944/// list.
3945///
3946/// \param Complain If true, this routine will produce a diagnostic if
3947/// the template parameter lists are not equivalent.
3948///
Douglas Gregorfb898e12009-11-12 16:20:59 +00003949/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00003950///
3951/// \param TemplateArgLoc If this source location is valid, then we
3952/// are actually checking the template parameter list of a template
3953/// argument (New) against the template parameter list of its
3954/// corresponding template template parameter (Old). We produce
3955/// slightly different diagnostics in this scenario.
3956///
Douglas Gregorddc29e12009-02-06 22:42:48 +00003957/// \returns True if the template parameter lists are equal, false
3958/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003959bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00003960Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
3961 TemplateParameterList *Old,
3962 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003963 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00003964 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00003965 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
3966 if (Complain)
3967 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3968 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00003969
3970 return false;
3971 }
3972
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003973 // C++0x [temp.arg.template]p3:
3974 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003975 // when each of the template parameters in the template-parameter-list of
3976 // the template-argument's corresponding class template or template alias
3977 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00003978 // template-parameter-list of P. [...]
3979 TemplateParameterList::iterator NewParm = New->begin();
3980 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00003981 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00003982 OldParmEnd = Old->end();
3983 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00003984 if (Kind != TPL_TemplateTemplateArgumentMatch ||
3985 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00003986 if (NewParm == NewParmEnd) {
3987 if (Complain)
3988 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3989 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003990
Douglas Gregora0347822011-01-13 00:08:50 +00003991 return false;
3992 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003993
Douglas Gregora0347822011-01-13 00:08:50 +00003994 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
3995 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003996 return false;
3997
Douglas Gregora0347822011-01-13 00:08:50 +00003998 ++NewParm;
3999 continue;
4000 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004001
Douglas Gregora0347822011-01-13 00:08:50 +00004002 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004003 // [...] When P's template- parameter-list contains a template parameter
4004 // pack (14.5.3), the template parameter pack will match zero or more
4005 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004006 // template-parameter-list of A with the same type and form as the
4007 // template parameter pack in P (ignoring whether those template
4008 // parameters are template parameter packs).
4009 for (; NewParm != NewParmEnd; ++NewParm) {
4010 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4011 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004012 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004013 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004014 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004015
Douglas Gregora0347822011-01-13 00:08:50 +00004016 // Make sure we exhausted all of the arguments.
4017 if (NewParm != NewParmEnd) {
4018 if (Complain)
4019 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4020 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004021
Douglas Gregora0347822011-01-13 00:08:50 +00004022 return false;
4023 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004024
Douglas Gregorddc29e12009-02-06 22:42:48 +00004025 return true;
4026}
4027
4028/// \brief Check whether a template can be declared within this scope.
4029///
4030/// If the template declaration is valid in this scope, returns
4031/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004032bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004033Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004034 // Find the nearest enclosing declaration scope.
4035 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4036 (S->getFlags() & Scope::TemplateParamScope) != 0)
4037 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004038
Douglas Gregorddc29e12009-02-06 22:42:48 +00004039 // C++ [temp]p2:
4040 // A template-declaration can appear only as a namespace scope or
4041 // class scope declaration.
4042 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004043 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4044 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004045 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004046 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004047
Eli Friedman1503f772009-07-31 01:43:05 +00004048 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004049 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004050
4051 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4052 return false;
4053
Mike Stump1eb44332009-09-09 15:08:12 +00004054 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004055 diag::err_template_outside_namespace_or_class_scope)
4056 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004057}
Douglas Gregorcc636682009-02-17 23:15:12 +00004058
Douglas Gregord5cb8762009-10-07 00:13:32 +00004059/// \brief Determine what kind of template specialization the given declaration
4060/// is.
4061static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4062 if (!D)
4063 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004064
Douglas Gregorf6b11852009-10-08 15:14:33 +00004065 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4066 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004067 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4068 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004069 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4070 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004071
Douglas Gregord5cb8762009-10-07 00:13:32 +00004072 return TSK_Undeclared;
4073}
4074
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004075/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004076/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004077///
Douglas Gregor9302da62009-10-14 23:50:59 +00004078/// This routine determines whether a template specialization can be declared
4079/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004080///
4081/// \param S the semantic analysis object for which this check is being
4082/// performed.
4083///
4084/// \param Specialized the entity being specialized or instantiated, which
4085/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004086/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004087/// member class).
4088///
4089/// \param PrevDecl the previous declaration of this entity, if any.
4090///
4091/// \param Loc the location of the explicit specialization or instantiation of
4092/// this entity.
4093///
4094/// \param IsPartialSpecialization whether this is a partial specialization of
4095/// a class template.
4096///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004097/// \returns true if there was an error that we cannot recover from, false
4098/// otherwise.
4099static bool CheckTemplateSpecializationScope(Sema &S,
4100 NamedDecl *Specialized,
4101 NamedDecl *PrevDecl,
4102 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004103 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004104 // Keep these "kind" numbers in sync with the %select statements in the
4105 // various diagnostics emitted by this routine.
4106 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004107 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004108 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004109 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004110 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004111 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004112 EntityKind = 3;
4113 else if (isa<VarDecl>(Specialized))
4114 EntityKind = 4;
4115 else if (isa<RecordDecl>(Specialized))
4116 EntityKind = 5;
4117 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004118 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4119 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004120 return true;
4121 }
4122
Douglas Gregor88b70942009-02-25 22:02:03 +00004123 // C++ [temp.expl.spec]p2:
4124 // An explicit specialization shall be declared in the namespace
4125 // of which the template is a member, or, for member templates, in
4126 // the namespace of which the enclosing class or enclosing class
4127 // template is a member. An explicit specialization of a member
4128 // function, member class or static data member of a class
4129 // template shall be declared in the namespace of which the class
4130 // template is a member. Such a declaration may also be a
4131 // definition. If the declaration is not a definition, the
4132 // specialization may be defined later in the name- space in which
4133 // the explicit specialization was declared, or in a namespace
4134 // that encloses the one in which the explicit specialization was
4135 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004136 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004137 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004138 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004139 return true;
4140 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004141
Douglas Gregor0a407472009-10-07 17:30:37 +00004142 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
4143 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004144 << Specialized;
Douglas Gregor0a407472009-10-07 17:30:37 +00004145 return true;
4146 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004147
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004148 // C++ [temp.class.spec]p6:
4149 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004150 // in any namespace scope in which its definition may be defined (14.5.1
4151 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004152 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004153 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004154 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004155 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004156 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004157 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4158 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004159 // C++ [temp.exp.spec]p2:
4160 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004161 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004162 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004163 // An explicit specialization of a member function, member class or
4164 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004165 // namespace of which the class template is a member.
4166 //
4167 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004168 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004169 // the specialized template.
4170 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4171 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004172 bool IsCPlusPlus0xExtension
4173 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004174 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004175 S.Diag(Loc, IsCPlusPlus0xExtension
4176 ? diag::ext_template_spec_decl_out_of_scope_global
4177 : diag::err_template_spec_decl_out_of_scope_global)
4178 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004179 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004180 S.Diag(Loc, IsCPlusPlus0xExtension
4181 ? diag::ext_template_spec_decl_out_of_scope
4182 : diag::err_template_spec_decl_out_of_scope)
4183 << EntityKind << Specialized
4184 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004185
Douglas Gregor9302da62009-10-14 23:50:59 +00004186 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4187 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004188 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004189 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004190
4191 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004192 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004193 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004194 // specializations of function templates, static data members, and member
4195 // functions, so we skip the check here for those kinds of entities.
4196 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004197 // Should we refactor that check, so that it occurs later?
4198 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004199 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4200 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004201 if (isa<TranslationUnitDecl>(SpecializedContext))
4202 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4203 << EntityKind << Specialized;
4204 else if (isa<NamespaceDecl>(SpecializedContext))
4205 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4206 << EntityKind << Specialized
4207 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004208
Douglas Gregor9302da62009-10-14 23:50:59 +00004209 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004210 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004211
Douglas Gregord5cb8762009-10-07 00:13:32 +00004212 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004213
Douglas Gregor88b70942009-02-25 22:02:03 +00004214 return false;
4215}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004216
Douglas Gregorbacb9492011-01-03 21:13:47 +00004217/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4218/// that checks non-type template partial specialization arguments.
4219static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4220 NonTypeTemplateParmDecl *Param,
4221 const TemplateArgument *Args,
4222 unsigned NumArgs) {
4223 for (unsigned I = 0; I != NumArgs; ++I) {
4224 if (Args[I].getKind() == TemplateArgument::Pack) {
4225 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004226 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004227 Args[I].pack_size()))
4228 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004229
Douglas Gregore94866f2009-06-12 21:21:02 +00004230 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004231 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004232
Douglas Gregorbacb9492011-01-03 21:13:47 +00004233 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004234 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004235 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004236 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004237
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004238 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004239 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4240 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004241
4242 // Strip off any implicit casts we added as part of type checking.
4243 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4244 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004245
Douglas Gregore94866f2009-06-12 21:21:02 +00004246 // C++ [temp.class.spec]p8:
4247 // A non-type argument is non-specialized if it is the name of a
4248 // non-type parameter. All other non-type arguments are
4249 // specialized.
4250 //
4251 // Below, we check the two conditions that only apply to
4252 // specialized non-type arguments, so skip any non-specialized
4253 // arguments.
4254 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004255 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004256 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004257
Douglas Gregore94866f2009-06-12 21:21:02 +00004258 // C++ [temp.class.spec]p9:
4259 // Within the argument list of a class template partial
4260 // specialization, the following restrictions apply:
4261 // -- A partially specialized non-type argument expression
4262 // shall not involve a template parameter of the partial
4263 // specialization except when the argument expression is a
4264 // simple identifier.
4265 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004266 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004267 diag::err_dependent_non_type_arg_in_partial_spec)
4268 << ArgExpr->getSourceRange();
4269 return true;
4270 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004271
Douglas Gregore94866f2009-06-12 21:21:02 +00004272 // -- The type of a template parameter corresponding to a
4273 // specialized non-type argument shall not be dependent on a
4274 // parameter of the specialization.
4275 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004276 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004277 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4278 << Param->getType()
4279 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004280 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004281 return true;
4282 }
4283 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004284
Douglas Gregorbacb9492011-01-03 21:13:47 +00004285 return false;
4286}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004287
Douglas Gregorbacb9492011-01-03 21:13:47 +00004288/// \brief Check the non-type template arguments of a class template
4289/// partial specialization according to C++ [temp.class.spec]p9.
4290///
4291/// \param TemplateParams the template parameters of the primary class
4292/// template.
4293///
4294/// \param TemplateArg the template arguments of the class template
4295/// partial specialization.
4296///
4297/// \returns true if there was an error, false otherwise.
4298static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4299 TemplateParameterList *TemplateParams,
4300 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4301 const TemplateArgument *ArgList = TemplateArgs.data();
4302
4303 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4304 NonTypeTemplateParmDecl *Param
4305 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4306 if (!Param)
4307 continue;
4308
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004309 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004310 &ArgList[I], 1))
4311 return true;
4312 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004313
4314 return false;
4315}
4316
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004317/// \brief Retrieve the previous declaration of the given declaration.
4318static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4319 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4320 return VD->getPreviousDeclaration();
4321 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4322 return FD->getPreviousDeclaration();
4323 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4324 return TD->getPreviousDeclaration();
4325 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND))
4326 return TD->getPreviousDeclaration();
4327 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4328 return FTD->getPreviousDeclaration();
4329 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4330 return CTD->getPreviousDeclaration();
4331 return 0;
4332}
4333
John McCalld226f652010-08-21 09:40:31 +00004334DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004335Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4336 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004337 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004338 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004339 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004340 SourceLocation TemplateNameLoc,
4341 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004342 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004343 SourceLocation RAngleLoc,
4344 AttributeList *Attr,
4345 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004346 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004347
Douglas Gregorcc636682009-02-17 23:15:12 +00004348 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004349 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004350 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004351 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4352
4353 if (!ClassTemplate) {
4354 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004355 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004356 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4357 return true;
4358 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004359
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004360 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004361 bool isPartialSpecialization = false;
4362
Douglas Gregor88b70942009-02-25 22:02:03 +00004363 // Check the validity of the template headers that introduce this
4364 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004365 // FIXME: We probably shouldn't complain about these headers for
4366 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004367 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004368 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00004369 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
4370 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004371 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004372 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004373 isExplicitSpecialization,
4374 Invalid);
4375 if (Invalid)
4376 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004377
Abramo Bagnara9b934882010-06-12 08:15:14 +00004378 unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size();
4379 if (TemplateParams)
4380 --NumMatchedTemplateParamLists;
4381
Douglas Gregor05396e22009-08-25 17:23:04 +00004382 if (TemplateParams && TemplateParams->size() > 0) {
4383 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004384
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004385 if (TUK == TUK_Friend) {
4386 Diag(KWLoc, diag::err_partial_specialization_friend)
4387 << SourceRange(LAngleLoc, RAngleLoc);
4388 return true;
4389 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004390
Douglas Gregor05396e22009-08-25 17:23:04 +00004391 // C++ [temp.class.spec]p10:
4392 // The template parameter list of a specialization shall not
4393 // contain default template argument values.
4394 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4395 Decl *Param = TemplateParams->getParam(I);
4396 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4397 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004398 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004399 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004400 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004401 }
4402 } else if (NonTypeTemplateParmDecl *NTTP
4403 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4404 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004405 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004406 diag::err_default_arg_in_partial_spec)
4407 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004408 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004409 }
4410 } else {
4411 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004412 if (TTP->hasDefaultArgument()) {
4413 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004414 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004415 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004416 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004417 }
4418 }
4419 }
Douglas Gregora735b202009-10-13 14:39:41 +00004420 } else if (TemplateParams) {
4421 if (TUK == TUK_Friend)
4422 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004423 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004424 SourceRange(TemplateParams->getTemplateLoc(),
4425 TemplateParams->getRAngleLoc()))
4426 << SourceRange(LAngleLoc, RAngleLoc);
4427 else
4428 isExplicitSpecialization = true;
4429 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004430 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004431 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004432 isExplicitSpecialization = true;
4433 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004434
Douglas Gregorcc636682009-02-17 23:15:12 +00004435 // Check that the specialization uses the same tag kind as the
4436 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004437 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4438 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004439 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004440 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004441 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004442 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004443 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004444 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004445 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004446 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004447 diag::note_previous_use);
4448 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4449 }
4450
Douglas Gregor40808ce2009-03-09 23:48:35 +00004451 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004452 TemplateArgumentListInfo TemplateArgs;
4453 TemplateArgs.setLAngleLoc(LAngleLoc);
4454 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004455 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004456
Douglas Gregor925910d2011-01-03 20:35:03 +00004457 // Check for unexpanded parameter packs in any of the template arguments.
4458 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004459 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004460 UPPC_PartialSpecialization))
4461 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004462
Douglas Gregorcc636682009-02-17 23:15:12 +00004463 // Check that the template argument list is well-formed for this
4464 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004465 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004466 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4467 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004468 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004469
Douglas Gregor910f8002010-11-07 23:05:16 +00004470 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004471 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004472
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004473 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004474 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004475 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004476 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004477 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004478 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004479 return true;
4480
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004481 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004482 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004483 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004484 TemplateArgs.size())) {
4485 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4486 << ClassTemplate->getDeclName();
4487 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004488 }
4489 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004490
Douglas Gregorcc636682009-02-17 23:15:12 +00004491 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004492 ClassTemplateSpecializationDecl *PrevDecl = 0;
4493
4494 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004495 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004496 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004497 = ClassTemplate->findPartialSpecialization(Converted.data(),
4498 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004499 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004500 else
4501 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004502 = ClassTemplate->findSpecialization(Converted.data(),
4503 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004504
4505 ClassTemplateSpecializationDecl *Specialization = 0;
4506
Douglas Gregor88b70942009-02-25 22:02:03 +00004507 // Check whether we can declare a class template specialization in
4508 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004509 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004510 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4511 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004512 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004513 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004514
Douglas Gregorb88e8882009-07-30 17:40:51 +00004515 // The canonical type
4516 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004517 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004518 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004519 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004520 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004521 // arguments was referenced but not declared, or we're only
4522 // referencing this specialization as a friend, reuse that
Douglas Gregorcc636682009-02-17 23:15:12 +00004523 // declaration node as our own, updating its source location to
4524 // reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004525 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004526 Specialization->setLocation(TemplateNameLoc);
Douglas Gregorcc636682009-02-17 23:15:12 +00004527 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004528 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004529 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004530 // Build the canonical type that describes the converted template
4531 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004532 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4533 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004534 Converted.data(),
4535 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004536
4537 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004538 ClassTemplate->getInjectedClassNameSpecialization())) {
4539 // C++ [temp.class.spec]p9b3:
4540 //
4541 // -- The argument list of the specialization shall not be identical
4542 // to the implicit argument list of the primary template.
4543 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4544 << (TUK == TUK_Definition)
4545 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4546 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4547 ClassTemplate->getIdentifier(),
4548 TemplateNameLoc,
4549 Attr,
4550 TemplateParams,
4551 AS_none);
4552 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004553
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004554 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004555 ClassTemplatePartialSpecializationDecl *PrevPartial
4556 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004557 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004558 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004559 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004560 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004561 ClassTemplate->getDeclContext(),
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004562 TemplateNameLoc,
4563 TemplateParams,
4564 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004565 Converted.data(),
4566 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004567 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004568 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004569 PrevPartial,
4570 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004571 SetNestedNameSpecifier(Partial, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004572 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004573 Partial->setTemplateParameterListsInfo(Context,
4574 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004575 (TemplateParameterList**) TemplateParameterLists.release());
4576 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004577
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004578 if (!PrevPartial)
4579 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004580 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004581
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004582 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004583 // template specialization, make a note of that.
4584 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4585 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004586
Douglas Gregor031a5882009-06-13 00:26:55 +00004587 // Check that all of the template parameters of the class template
4588 // partial specialization are deducible from the template
4589 // arguments. If not, this class template partial specialization
4590 // will never be used.
4591 llvm::SmallVector<bool, 8> DeducibleParams;
4592 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004593 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004594 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004595 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004596 unsigned NumNonDeducible = 0;
4597 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4598 if (!DeducibleParams[I])
4599 ++NumNonDeducible;
4600
4601 if (NumNonDeducible) {
4602 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4603 << (NumNonDeducible > 1)
4604 << SourceRange(TemplateNameLoc, RAngleLoc);
4605 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4606 if (!DeducibleParams[I]) {
4607 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4608 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004609 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004610 diag::note_partial_spec_unused_parameter)
4611 << Param->getDeclName();
4612 else
Mike Stump1eb44332009-09-09 15:08:12 +00004613 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004614 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004615 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004616 }
4617 }
4618 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004619 } else {
4620 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004621 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004622 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004623 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004624 ClassTemplate->getDeclContext(),
4625 TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004626 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004627 Converted.data(),
4628 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004629 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004630 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004631 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004632 Specialization->setTemplateParameterListsInfo(Context,
4633 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004634 (TemplateParameterList**) TemplateParameterLists.release());
4635 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004636
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004637 if (!PrevDecl)
4638 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004639
4640 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004641 }
4642
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004643 // C++ [temp.expl.spec]p6:
4644 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004645 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004646 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004647 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004648 // use occurs; no diagnostic is required.
4649 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004650 bool Okay = false;
4651 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4652 // Is there any previous explicit specialization declaration?
4653 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4654 Okay = true;
4655 break;
4656 }
4657 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004658
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004659 if (!Okay) {
4660 SourceRange Range(TemplateNameLoc, RAngleLoc);
4661 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
4662 << Context.getTypeDeclType(Specialization) << Range;
4663
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004664 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004665 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004666 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004667 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004668 return true;
4669 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004670 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004671
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004672 // If this is not a friend, note that this is an explicit specialization.
4673 if (TUK != TUK_Friend)
4674 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004675
4676 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004677 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00004678 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004679 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00004680 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004681 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00004682 Diag(Def->getLocation(), diag::note_previous_definition);
4683 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00004684 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004685 }
4686 }
4687
John McCall7f1b9872010-12-18 03:30:47 +00004688 if (Attr)
4689 ProcessDeclAttributeList(S, Specialization, Attr);
4690
Douglas Gregorfc705b82009-02-26 22:19:44 +00004691 // Build the fully-sugared type for this class template
4692 // specialization as the user wrote in the specialization
4693 // itself. This means that we'll pretty-print the type retrieved
4694 // from the specialization's declaration the way that the user
4695 // actually wrote the specialization, rather than formatting the
4696 // name based on the "canonical" representation used to store the
4697 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00004698 TypeSourceInfo *WrittenTy
4699 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
4700 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004701 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004702 Specialization->setTypeAsWritten(WrittenTy);
Douglas Gregor7e9b57b2010-07-06 18:33:12 +00004703 if (TemplateParams)
4704 Specialization->setTemplateKeywordLoc(TemplateParams->getTemplateLoc());
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004705 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00004706 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00004707
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004708 // C++ [temp.expl.spec]p9:
4709 // A template explicit specialization is in the scope of the
4710 // namespace in which the template was defined.
4711 //
4712 // We actually implement this paragraph where we set the semantic
4713 // context (in the creation of the ClassTemplateSpecializationDecl),
4714 // but we also maintain the lexical context where the actual
4715 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00004716 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00004717
Douglas Gregorcc636682009-02-17 23:15:12 +00004718 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004719 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00004720 Specialization->startDefinition();
4721
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004722 if (TUK == TUK_Friend) {
4723 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
4724 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00004725 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004726 /*FIXME:*/KWLoc);
4727 Friend->setAccess(AS_public);
4728 CurContext->addDecl(Friend);
4729 } else {
4730 // Add the specialization into its lexical context, so that it can
4731 // be seen when iterating through the list of declarations in that
4732 // context. However, specializations are not found by name lookup.
4733 CurContext->addDecl(Specialization);
4734 }
John McCalld226f652010-08-21 09:40:31 +00004735 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00004736}
Douglas Gregord57959a2009-03-27 23:10:48 +00004737
John McCalld226f652010-08-21 09:40:31 +00004738Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00004739 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004740 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00004741 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
4742}
4743
John McCalld226f652010-08-21 09:40:31 +00004744Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00004745 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004746 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00004747 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004748 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00004749
Douglas Gregor52591bf2009-06-24 00:54:41 +00004750 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00004751 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00004752 }
Mike Stump1eb44332009-09-09 15:08:12 +00004753
Douglas Gregor52591bf2009-06-24 00:54:41 +00004754 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004755
John McCalld226f652010-08-21 09:40:31 +00004756 Decl *DP = HandleDeclarator(ParentScope, D,
4757 move(TemplateParameterLists),
4758 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00004759 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00004760 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00004761 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00004762 FunctionTemplate->getTemplatedDecl());
4763 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
4764 return ActOnStartOfFunctionDef(FnBodyScope, Function);
4765 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00004766}
4767
John McCall75042392010-02-11 01:33:53 +00004768/// \brief Strips various properties off an implicit instantiation
4769/// that has just been explicitly specialized.
4770static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00004771 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00004772
4773 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4774 FD->setInlineSpecified(false);
4775 }
4776}
4777
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004778/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00004779/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004780/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00004781/// new specialization/instantiation will have any effect.
4782///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004783/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00004784/// instantiation.
4785///
4786/// \param NewTSK the kind of the new explicit specialization or instantiation.
4787///
4788/// \param PrevDecl the previous declaration of the entity.
4789///
4790/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
4791///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004792/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00004793/// declaration was instantiated (either implicitly or explicitly).
4794///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004795/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00004796/// specialization or instantiation has no effect and should be ignored.
4797///
4798/// \returns true if there was an error that should prevent the introduction of
4799/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00004800bool
4801Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
4802 TemplateSpecializationKind NewTSK,
4803 NamedDecl *PrevDecl,
4804 TemplateSpecializationKind PrevTSK,
4805 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004806 bool &HasNoEffect) {
4807 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004808
Douglas Gregor454885e2009-10-15 15:54:05 +00004809 switch (NewTSK) {
4810 case TSK_Undeclared:
4811 case TSK_ImplicitInstantiation:
4812 assert(false && "Don't check implicit instantiations here");
4813 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004814
Douglas Gregor454885e2009-10-15 15:54:05 +00004815 case TSK_ExplicitSpecialization:
4816 switch (PrevTSK) {
4817 case TSK_Undeclared:
4818 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004819 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00004820 // explicitly specialized or has merely been mentioned without any
4821 // instantiation.
4822 return false;
4823
4824 case TSK_ImplicitInstantiation:
4825 if (PrevPointOfInstantiation.isInvalid()) {
4826 // The declaration itself has not actually been instantiated, so it is
4827 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00004828 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00004829 return false;
4830 }
4831 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004832
Douglas Gregor454885e2009-10-15 15:54:05 +00004833 case TSK_ExplicitInstantiationDeclaration:
4834 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004835 assert((PrevTSK == TSK_ImplicitInstantiation ||
4836 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00004837 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004838
Douglas Gregor454885e2009-10-15 15:54:05 +00004839 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004840 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00004841 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004842 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00004843 // implicit instantiation to take place, in every translation unit in
4844 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004845 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4846 // Is there any previous explicit specialization declaration?
4847 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
4848 return false;
4849 }
4850
Douglas Gregor0d035142009-10-27 18:42:08 +00004851 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00004852 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004853 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00004854 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004855
Douglas Gregor454885e2009-10-15 15:54:05 +00004856 return true;
4857 }
4858 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004859
Douglas Gregor454885e2009-10-15 15:54:05 +00004860 case TSK_ExplicitInstantiationDeclaration:
4861 switch (PrevTSK) {
4862 case TSK_ExplicitInstantiationDeclaration:
4863 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004864 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004865 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004866
Douglas Gregor454885e2009-10-15 15:54:05 +00004867 case TSK_Undeclared:
4868 case TSK_ImplicitInstantiation:
4869 // We're explicitly instantiating something that may have already been
4870 // implicitly instantiated; that's fine.
4871 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004872
Douglas Gregor454885e2009-10-15 15:54:05 +00004873 case TSK_ExplicitSpecialization:
4874 // C++0x [temp.explicit]p4:
4875 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004876 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00004877 // specialization for that template, the explicit instantiation has no
4878 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004879 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004880 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004881
Douglas Gregor454885e2009-10-15 15:54:05 +00004882 case TSK_ExplicitInstantiationDefinition:
4883 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004884 // If an entity is the subject of both an explicit instantiation
4885 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00004886 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004887 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00004888 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004889 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004890 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00004891 assert(PrevPointOfInstantiation.isValid() &&
4892 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004893 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004894 return false;
4895 }
4896 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004897
Douglas Gregor454885e2009-10-15 15:54:05 +00004898 case TSK_ExplicitInstantiationDefinition:
4899 switch (PrevTSK) {
4900 case TSK_Undeclared:
4901 case TSK_ImplicitInstantiation:
4902 // We're explicitly instantiating something that may have already been
4903 // implicitly instantiated; that's fine.
4904 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004905
Douglas Gregor454885e2009-10-15 15:54:05 +00004906 case TSK_ExplicitSpecialization:
4907 // C++ DR 259, C++0x [temp.explicit]p4:
4908 // For a given set of template parameters, if an explicit
4909 // instantiation of a template appears after a declaration of
4910 // an explicit specialization for that template, the explicit
4911 // instantiation has no effect.
4912 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004913 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00004914 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00004915 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00004916 if (!getLangOptions().CPlusPlus0x) {
4917 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00004918 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004919 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00004920 diag::note_previous_template_specialization);
4921 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004922 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004923 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004924
Douglas Gregor454885e2009-10-15 15:54:05 +00004925 case TSK_ExplicitInstantiationDeclaration:
4926 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004927 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00004928 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004929
Douglas Gregor454885e2009-10-15 15:54:05 +00004930 case TSK_ExplicitInstantiationDefinition:
4931 // C++0x [temp.spec]p5:
4932 // For a given template and a given set of template-arguments,
4933 // - an explicit instantiation definition shall appear at most once
4934 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00004935 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00004936 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004937 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004938 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004939 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004940 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00004941 }
4942 break;
4943 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004944
Douglas Gregor454885e2009-10-15 15:54:05 +00004945 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004946
Douglas Gregor454885e2009-10-15 15:54:05 +00004947 return false;
4948}
4949
John McCallaf2094e2010-04-08 09:05:18 +00004950/// \brief Perform semantic analysis for the given dependent function
4951/// template specialization. The only possible way to get a dependent
4952/// function template specialization is with a friend declaration,
4953/// like so:
4954///
4955/// template <class T> void foo(T);
4956/// template <class T> class A {
4957/// friend void foo<>(T);
4958/// };
4959///
4960/// There really isn't any useful analysis we can do here, so we
4961/// just store the information.
4962bool
4963Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
4964 const TemplateArgumentListInfo &ExplicitTemplateArgs,
4965 LookupResult &Previous) {
4966 // Remove anything from Previous that isn't a function template in
4967 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004968 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00004969 LookupResult::Filter F = Previous.makeFilter();
4970 while (F.hasNext()) {
4971 NamedDecl *D = F.next()->getUnderlyingDecl();
4972 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00004973 !FDLookupContext->InEnclosingNamespaceSetOf(
4974 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00004975 F.erase();
4976 }
4977 F.done();
4978
4979 // Should this be diagnosed here?
4980 if (Previous.empty()) return true;
4981
4982 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
4983 ExplicitTemplateArgs);
4984 return false;
4985}
4986
Abramo Bagnarae03db982010-05-20 15:32:11 +00004987/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004988/// specialization.
4989///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004990/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004991/// explicit function template specialization. On successful completion,
4992/// the function declaration \p FD will become a function template
4993/// specialization.
4994///
4995/// \param FD the function declaration, which will be updated to become a
4996/// function template specialization.
4997///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004998/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
4999/// if any. Note that this may be valid info even when 0 arguments are
5000/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5001/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005002///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005003/// \param PrevDecl the set of declarations that may be specialized by
5004/// this function specialization.
5005bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005006Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
John McCalld5532b62009-11-23 01:53:49 +00005007 const TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005008 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005009 // The set of function template specializations that could match this
5010 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005011 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005012
Sebastian Redl7a126a42010-08-31 00:36:30 +00005013 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005014 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5015 I != E; ++I) {
5016 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5017 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005018 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005019 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005020 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5021 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005022 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005023
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005024 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005025 // A trailing template-argument can be left unspecified in the
5026 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005027 // provided it can be deduced from the function argument type.
5028 // Perform template argument deduction to determine whether we may be
5029 // specializing this template.
5030 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005031 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005032 FunctionDecl *Specialization = 0;
5033 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005034 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005035 FD->getType(),
5036 Specialization,
5037 Info)) {
5038 // FIXME: Template argument deduction failed; record why it failed, so
5039 // that we can provide nifty diagnostics.
5040 (void)TDK;
5041 continue;
5042 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005043
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005044 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005045 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005046 }
5047 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005048
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005049 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005050 UnresolvedSetIterator Result
5051 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005052 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005053 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005054 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005055 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005056 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005057 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005058 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005059 return true;
John McCallc373d482010-01-27 01:50:18 +00005060
5061 // Ignore access information; it doesn't figure into redeclaration checking.
5062 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Douglas Gregorc42b6522010-04-09 21:02:29 +00005063 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005064
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005065 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005066 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005067
5068 // If this is a friend declaration, then we're not really declaring
5069 // an explicit specialization.
5070 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005071
Douglas Gregord5cb8762009-10-07 00:13:32 +00005072 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005073 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005074 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005075 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005076 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005077 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005078 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005079
5080 // C++ [temp.expl.spec]p6:
5081 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005082 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005083 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005084 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005085 // use occurs; no diagnostic is required.
5086 FunctionTemplateSpecializationInfo *SpecInfo
5087 = Specialization->getTemplateSpecializationInfo();
5088 assert(SpecInfo && "Function template specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005089
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005090 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005091 if (!isFriend &&
5092 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005093 TSK_ExplicitSpecialization,
5094 Specialization,
5095 SpecInfo->getTemplateSpecializationKind(),
5096 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005097 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005098 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005099
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005100 // Mark the prior declaration as an explicit specialization, so that later
5101 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005102 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005103 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005104 MarkUnusedFileScopedDecl(Specialization);
5105 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005106
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005107 // Turn the given function declaration into a function template
5108 // specialization, with the template arguments from the previous
5109 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005110 // Take copies of (semantic and syntactic) template argument lists.
5111 const TemplateArgumentList* TemplArgs = new (Context)
5112 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5113 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5114 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005115 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005116 TemplArgs, /*InsertPos=*/0,
5117 SpecInfo->getTemplateSpecializationKind(),
5118 TemplArgsAsWritten);
5119
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005120 // The "previous declaration" for this function template specialization is
5121 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005122 Previous.clear();
5123 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005124 return false;
5125}
5126
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005127/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005128/// specialization.
5129///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005130/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005131/// explicit member function specialization. On successful completion,
5132/// the function declaration \p FD will become a member function
5133/// specialization.
5134///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005135/// \param Member the member declaration, which will be updated to become a
5136/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005137///
John McCall68263142009-11-18 22:49:29 +00005138/// \param Previous the set of declarations, one of which may be specialized
5139/// by this function specialization; the set will be modified to contain the
5140/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005141bool
John McCall68263142009-11-18 22:49:29 +00005142Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005143 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005144
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005145 // Try to find the member we are instantiating.
5146 NamedDecl *Instantiation = 0;
5147 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005148 MemberSpecializationInfo *MSInfo = 0;
5149
John McCall68263142009-11-18 22:49:29 +00005150 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005151 // Nowhere to look anyway.
5152 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005153 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5154 I != E; ++I) {
5155 NamedDecl *D = (*I)->getUnderlyingDecl();
5156 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005157 if (Context.hasSameType(Function->getType(), Method->getType())) {
5158 Instantiation = Method;
5159 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005160 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005161 break;
5162 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005163 }
5164 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005165 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005166 VarDecl *PrevVar;
5167 if (Previous.isSingleResult() &&
5168 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005169 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005170 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005171 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005172 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005173 }
5174 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005175 CXXRecordDecl *PrevRecord;
5176 if (Previous.isSingleResult() &&
5177 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5178 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005179 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005180 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005181 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005182 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005183
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005184 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005185 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005186 // specializations are always out-of-line, the caller will complain about
5187 // this mismatch later.
5188 return false;
5189 }
John McCall77e8b112010-04-13 20:37:33 +00005190
5191 // If this is a friend, just bail out here before we start turning
5192 // things into explicit specializations.
5193 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5194 // Preserve instantiation information.
5195 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5196 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5197 cast<CXXMethodDecl>(InstantiatedFrom),
5198 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5199 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5200 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5201 cast<CXXRecordDecl>(InstantiatedFrom),
5202 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5203 }
5204
5205 Previous.clear();
5206 Previous.addDecl(Instantiation);
5207 return false;
5208 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005209
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005210 // Make sure that this is a specialization of a member.
5211 if (!InstantiatedFrom) {
5212 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5213 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005214 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5215 return true;
5216 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005217
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005218 // C++ [temp.expl.spec]p6:
5219 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005220 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005221 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005222 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005223 // use occurs; no diagnostic is required.
5224 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005225
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005226 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005227 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5228 TSK_ExplicitSpecialization,
5229 Instantiation,
5230 MSInfo->getTemplateSpecializationKind(),
5231 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005232 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005233 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005234
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005235 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005236 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005237 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005238 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005239 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005240 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005241
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005242 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005243 // the original declaration to note that it is an explicit specialization
5244 // (if it was previously an implicit instantiation). This latter step
5245 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005246 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005247 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5248 if (InstantiationFunction->getTemplateSpecializationKind() ==
5249 TSK_ImplicitInstantiation) {
5250 InstantiationFunction->setTemplateSpecializationKind(
5251 TSK_ExplicitSpecialization);
5252 InstantiationFunction->setLocation(Member->getLocation());
5253 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005254
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005255 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5256 cast<CXXMethodDecl>(InstantiatedFrom),
5257 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005258 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005259 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005260 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5261 if (InstantiationVar->getTemplateSpecializationKind() ==
5262 TSK_ImplicitInstantiation) {
5263 InstantiationVar->setTemplateSpecializationKind(
5264 TSK_ExplicitSpecialization);
5265 InstantiationVar->setLocation(Member->getLocation());
5266 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005267
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005268 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5269 cast<VarDecl>(InstantiatedFrom),
5270 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005271 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005272 } else {
5273 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005274 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5275 if (InstantiationClass->getTemplateSpecializationKind() ==
5276 TSK_ImplicitInstantiation) {
5277 InstantiationClass->setTemplateSpecializationKind(
5278 TSK_ExplicitSpecialization);
5279 InstantiationClass->setLocation(Member->getLocation());
5280 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005281
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005282 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005283 cast<CXXRecordDecl>(InstantiatedFrom),
5284 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005285 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005286
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005287 // Save the caller the trouble of having to figure out which declaration
5288 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005289 Previous.clear();
5290 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005291 return false;
5292}
5293
Douglas Gregor558c0322009-10-14 23:41:34 +00005294/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005295///
5296/// \returns true if a serious error occurs, false otherwise.
5297static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005298 SourceLocation InstLoc,
5299 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005300 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5301 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005302
Douglas Gregor669eed82010-07-13 00:10:04 +00005303 if (CurContext->isRecord()) {
5304 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5305 << D;
5306 return true;
5307 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005308
Douglas Gregor558c0322009-10-14 23:41:34 +00005309 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005310 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005311 // template.
5312 //
5313 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005314 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005315 !CurContext->Encloses(OrigContext)) {
5316 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005317 S.Diag(InstLoc,
5318 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005319 diag::err_explicit_instantiation_out_of_scope
5320 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005321 << D << NS;
5322 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005323 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005324 S.getLangOptions().CPlusPlus0x?
5325 diag::err_explicit_instantiation_must_be_global
5326 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005327 << D;
5328 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005329 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005330 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005331
Douglas Gregor558c0322009-10-14 23:41:34 +00005332 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005333 // If the name declared in the explicit instantiation is an unqualified
5334 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005335 // its template is declared or, if that namespace is inline (7.3.1), any
5336 // namespace from its enclosing namespace set.
5337 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005338 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005339
5340 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005341 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005342
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005343 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005344 S.getLangOptions().CPlusPlus0x?
5345 diag::err_explicit_instantiation_unqualified_wrong_namespace
5346 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005347 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005348 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005349 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005350}
5351
5352/// \brief Determine whether the given scope specifier has a template-id in it.
5353static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5354 if (!SS.isSet())
5355 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005356
Douglas Gregor558c0322009-10-14 23:41:34 +00005357 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005358 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005359 // or a static data member of a class template specialization, the name of
5360 // the class template specialization in the qualified-id for the member
5361 // name shall be a simple-template-id.
5362 //
5363 // C++98 has the same restriction, just worded differently.
5364 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5365 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005366 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005367 if (isa<TemplateSpecializationType>(T))
5368 return true;
5369
5370 return false;
5371}
5372
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005373// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005374DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005375Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005376 SourceLocation ExternLoc,
5377 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005378 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005379 SourceLocation KWLoc,
5380 const CXXScopeSpec &SS,
5381 TemplateTy TemplateD,
5382 SourceLocation TemplateNameLoc,
5383 SourceLocation LAngleLoc,
5384 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005385 SourceLocation RAngleLoc,
5386 AttributeList *Attr) {
5387 // Find the class template we're specializing
5388 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005389 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005390 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5391
5392 // Check that the specialization uses the same tag kind as the
5393 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005394 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5395 assert(Kind != TTK_Enum &&
5396 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005397 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005398 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005399 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005400 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005401 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005402 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005403 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005404 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005405 diag::note_previous_use);
5406 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5407 }
5408
Douglas Gregor558c0322009-10-14 23:41:34 +00005409 // C++0x [temp.explicit]p2:
5410 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005411 // definition and an explicit instantiation declaration. An explicit
5412 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005413 TemplateSpecializationKind TSK
5414 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5415 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005416
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005417 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005418 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005419 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005420
5421 // Check that the template argument list is well-formed for this
5422 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005423 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005424 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5425 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005426 return true;
5427
Douglas Gregor910f8002010-11-07 23:05:16 +00005428 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005429 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005430
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005431 // Find the class template specialization declaration that
5432 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005433 void *InsertPos = 0;
5434 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005435 = ClassTemplate->findSpecialization(Converted.data(),
5436 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005437
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005438 TemplateSpecializationKind PrevDecl_TSK
5439 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5440
Douglas Gregord5cb8762009-10-07 00:13:32 +00005441 // C++0x [temp.explicit]p2:
5442 // [...] An explicit instantiation shall appear in an enclosing
5443 // namespace of its template. [...]
5444 //
5445 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005446 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5447 SS.isSet()))
5448 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005449
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005450 ClassTemplateSpecializationDecl *Specialization = 0;
5451
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005452 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005453 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005454 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005455 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005456 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005457 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005458 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005459
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005460 // Even though HasNoEffect == true means that this explicit instantiation
5461 // has no effect on semantics, we go on to put its syntax in the AST.
5462
5463 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5464 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005465 // Since the only prior class template specialization with these
5466 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005467 // declaration node as our own, updating the source location
5468 // for the template name to reflect our new declaration.
5469 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005470 Specialization = PrevDecl;
5471 Specialization->setLocation(TemplateNameLoc);
5472 PrevDecl = 0;
5473 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005474 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005475
Douglas Gregor52604ab2009-09-11 21:19:12 +00005476 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005477 // Create a new class template specialization declaration node for
5478 // this explicit specialization.
5479 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005480 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005481 ClassTemplate->getDeclContext(),
5482 TemplateNameLoc,
5483 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005484 Converted.data(),
5485 Converted.size(),
5486 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005487 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005488
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005489 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005490 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005491 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005492 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005493 }
5494
5495 // Build the fully-sugared type for this explicit instantiation as
5496 // the user wrote in the explicit instantiation itself. This means
5497 // that we'll pretty-print the type retrieved from the
5498 // specialization's declaration the way that the user actually wrote
5499 // the explicit instantiation, rather than formatting the name based
5500 // on the "canonical" representation used to store the template
5501 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005502 TypeSourceInfo *WrittenTy
5503 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5504 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005505 Context.getTypeDeclType(Specialization));
5506 Specialization->setTypeAsWritten(WrittenTy);
5507 TemplateArgsIn.release();
5508
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005509 // Set source locations for keywords.
5510 Specialization->setExternLoc(ExternLoc);
5511 Specialization->setTemplateKeywordLoc(TemplateLoc);
5512
5513 // Add the explicit instantiation into its lexical context. However,
5514 // since explicit instantiations are never found by name lookup, we
5515 // just put it into the declaration context directly.
5516 Specialization->setLexicalDeclContext(CurContext);
5517 CurContext->addDecl(Specialization);
5518
5519 // Syntax is now OK, so return if it has no other effect on semantics.
5520 if (HasNoEffect) {
5521 // Set the template specialization kind.
5522 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005523 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005524 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005525
5526 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005527 // A definition of a class template or class member template
5528 // shall be in scope at the point of the explicit instantiation of
5529 // the class template or class member template.
5530 //
5531 // This check comes when we actually try to perform the
5532 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005533 ClassTemplateSpecializationDecl *Def
5534 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005535 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005536 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005537 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005538 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005539 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005540 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5541 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005542
Douglas Gregor0d035142009-10-27 18:42:08 +00005543 // Instantiate the members of this class template specialization.
5544 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005545 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005546 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005547 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5548
5549 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5550 // TSK_ExplicitInstantiationDefinition
5551 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5552 TSK == TSK_ExplicitInstantiationDefinition)
5553 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005554
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005555 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005556 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005557
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005558 // Set the template specialization kind.
5559 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005560 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005561}
5562
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005563// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005564DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005565Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005566 SourceLocation ExternLoc,
5567 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005568 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005569 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005570 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005571 IdentifierInfo *Name,
5572 SourceLocation NameLoc,
5573 AttributeList *Attr) {
5574
Douglas Gregor402abb52009-05-28 23:31:59 +00005575 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005576 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005577 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005578 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5579 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005580 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005581 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005582 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5583
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005584 if (!TagD)
5585 return true;
5586
John McCalld226f652010-08-21 09:40:31 +00005587 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005588 if (Tag->isEnum()) {
5589 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5590 << Context.getTypeDeclType(Tag);
5591 return true;
5592 }
5593
Douglas Gregord0c87372009-05-27 17:30:49 +00005594 if (Tag->isInvalidDecl())
5595 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005596
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005597 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5598 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5599 if (!Pattern) {
5600 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5601 << Context.getTypeDeclType(Record);
5602 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5603 return true;
5604 }
5605
Douglas Gregor558c0322009-10-14 23:41:34 +00005606 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005607 // If the explicit instantiation is for a class or member class, the
5608 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005609 // simple-template-id.
5610 //
5611 // C++98 has the same restriction, just worded differently.
5612 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005613 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005614 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005615
Douglas Gregor558c0322009-10-14 23:41:34 +00005616 // C++0x [temp.explicit]p2:
5617 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005618 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005619 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005620 TemplateSpecializationKind TSK
5621 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5622 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005623
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005624 // C++0x [temp.explicit]p2:
5625 // [...] An explicit instantiation shall appear in an enclosing
5626 // namespace of its template. [...]
5627 //
5628 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005629 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005630
Douglas Gregor454885e2009-10-15 15:54:05 +00005631 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005632 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005633 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005634 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005635 PrevDecl = Record;
5636 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005637 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005638 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005639 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005640 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005641 PrevDecl,
5642 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005643 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005644 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005645 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005646 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005647 return TagD;
5648 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005649
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005650 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00005651 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005652 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005653 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005654 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005655 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005656 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00005657 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005658 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00005659 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
5660 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005661 Diag(Pattern->getLocation(), diag::note_forward_declaration)
5662 << Pattern;
5663 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00005664 } else {
5665 if (InstantiateClass(NameLoc, Record, Def,
5666 getTemplateInstantiationArgs(Record),
5667 TSK))
5668 return true;
5669
Douglas Gregor952b0172010-02-11 01:04:33 +00005670 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00005671 if (!RecordDef)
5672 return true;
5673 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005674 }
5675
Douglas Gregor0d035142009-10-27 18:42:08 +00005676 // Instantiate all of the members of the class.
5677 InstantiateClassMembers(NameLoc, RecordDef,
5678 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005679
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005680 if (TSK == TSK_ExplicitInstantiationDefinition)
5681 MarkVTableUsed(NameLoc, RecordDef, true);
5682
Mike Stump390b4cc2009-05-16 07:39:55 +00005683 // FIXME: We don't have any representation for explicit instantiations of
5684 // member classes. Such a representation is not needed for compilation, but it
5685 // should be available for clients that want to see all of the declarations in
5686 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005687 return TagD;
5688}
5689
John McCallf312b1e2010-08-26 23:41:50 +00005690DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
5691 SourceLocation ExternLoc,
5692 SourceLocation TemplateLoc,
5693 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00005694 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00005695 // TODO: check if/when DNInfo should replace Name.
5696 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
5697 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005698 if (!Name) {
5699 if (!D.isInvalidType())
5700 Diag(D.getDeclSpec().getSourceRange().getBegin(),
5701 diag::err_explicit_instantiation_requires_name)
5702 << D.getDeclSpec().getSourceRange()
5703 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005704
Douglas Gregord5a423b2009-09-25 18:43:00 +00005705 return true;
5706 }
5707
5708 // The scope passed in may not be a decl scope. Zip up the scope tree until
5709 // we find one that is.
5710 while ((S->getFlags() & Scope::DeclScope) == 0 ||
5711 (S->getFlags() & Scope::TemplateParamScope) != 0)
5712 S = S->getParent();
5713
5714 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00005715 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
5716 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005717 if (R.isNull())
5718 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005719
Douglas Gregord5a423b2009-09-25 18:43:00 +00005720 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
5721 // Cannot explicitly instantiate a typedef.
5722 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
5723 << Name;
5724 return true;
5725 }
5726
Douglas Gregor663b5a02009-10-14 20:14:33 +00005727 // C++0x [temp.explicit]p1:
5728 // [...] An explicit instantiation of a function template shall not use the
5729 // inline or constexpr specifiers.
5730 // Presumably, this also applies to member functions of class templates as
5731 // well.
5732 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005733 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00005734 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00005735 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005736
Douglas Gregor663b5a02009-10-14 20:14:33 +00005737 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005738
Douglas Gregor558c0322009-10-14 23:41:34 +00005739 // C++0x [temp.explicit]p2:
5740 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005741 // definition and an explicit instantiation declaration. An explicit
5742 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00005743 TemplateSpecializationKind TSK
5744 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5745 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005746
Abramo Bagnara25777432010-08-11 22:01:17 +00005747 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00005748 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005749
5750 if (!R->isFunctionType()) {
5751 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005752 // A [...] static data member of a class template can be explicitly
5753 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005754 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00005755 if (Previous.isAmbiguous())
5756 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005757
John McCall1bcee0a2009-12-02 08:25:40 +00005758 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005759 if (!Prev || !Prev->isStaticDataMember()) {
5760 // We expect to see a data data member here.
5761 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
5762 << Name;
5763 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5764 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00005765 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00005766 return true;
5767 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005768
Douglas Gregord5a423b2009-09-25 18:43:00 +00005769 if (!Prev->getInstantiatedFromStaticDataMember()) {
5770 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005771 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005772 diag::err_explicit_instantiation_data_member_not_instantiated)
5773 << Prev;
5774 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
5775 // FIXME: Can we provide a note showing where this was declared?
5776 return true;
5777 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005778
Douglas Gregor558c0322009-10-14 23:41:34 +00005779 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005780 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005781 // or a static data member of a class template specialization, the name of
5782 // the class template specialization in the qualified-id for the member
5783 // name shall be a simple-template-id.
5784 //
5785 // C++98 has the same restriction, just worded differently.
5786 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005787 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005788 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005789 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005790
Douglas Gregor558c0322009-10-14 23:41:34 +00005791 // Check the scope of this explicit instantiation.
5792 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005793
Douglas Gregor454885e2009-10-15 15:54:05 +00005794 // Verify that it is okay to explicitly instantiate here.
5795 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
5796 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005797 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005798 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00005799 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005800 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005801 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005802 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005803 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005804 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005805
Douglas Gregord5a423b2009-09-25 18:43:00 +00005806 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005807 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005808 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005809 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005810
Douglas Gregord5a423b2009-09-25 18:43:00 +00005811 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00005812 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005813 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005814
5815 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00005816 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00005817 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00005818 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005819 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
5820 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00005821 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
5822 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00005823 ASTTemplateArgsPtr TemplateArgsPtr(*this,
5824 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00005825 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00005826 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00005827 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00005828 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00005829 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005830
Douglas Gregord5a423b2009-09-25 18:43:00 +00005831 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005832 // A [...] function [...] can be explicitly instantiated from its template.
5833 // A member function [...] of a class template can be explicitly
5834 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005835 // template.
John McCallc373d482010-01-27 01:50:18 +00005836 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005837 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5838 P != PEnd; ++P) {
5839 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00005840 if (!HasExplicitTemplateArgs) {
5841 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
5842 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
5843 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00005844
John McCallc373d482010-01-27 01:50:18 +00005845 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00005846 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
5847 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00005848 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00005849 }
5850 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005851
Douglas Gregord5a423b2009-09-25 18:43:00 +00005852 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
5853 if (!FunTmpl)
5854 continue;
5855
John McCall5769d612010-02-08 23:07:23 +00005856 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005857 FunctionDecl *Specialization = 0;
5858 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005859 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00005860 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005861 R, Specialization, Info)) {
5862 // FIXME: Keep track of almost-matches?
5863 (void)TDK;
5864 continue;
5865 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005866
John McCallc373d482010-01-27 01:50:18 +00005867 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005868 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005869
Douglas Gregord5a423b2009-09-25 18:43:00 +00005870 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005871 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005872 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005873 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005874 PDiag(diag::err_explicit_instantiation_not_known) << Name,
5875 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
5876 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00005877
John McCallc373d482010-01-27 01:50:18 +00005878 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00005879 return true;
John McCallc373d482010-01-27 01:50:18 +00005880
5881 // Ignore access control bits, we don't need them for redeclaration checking.
5882 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005883
Douglas Gregor0a897e32009-10-15 17:21:20 +00005884 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005885 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005886 diag::err_explicit_instantiation_member_function_not_instantiated)
5887 << Specialization
5888 << (Specialization->getTemplateSpecializationKind() ==
5889 TSK_ExplicitSpecialization);
5890 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
5891 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005892 }
5893
Douglas Gregor0a897e32009-10-15 17:21:20 +00005894 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00005895 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
5896 PrevDecl = Specialization;
5897
Douglas Gregor0a897e32009-10-15 17:21:20 +00005898 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005899 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005900 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005901 PrevDecl,
5902 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00005903 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005904 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00005905 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005906
Douglas Gregor0a897e32009-10-15 17:21:20 +00005907 // FIXME: We may still want to build some representation of this
5908 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005909 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005910 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00005911 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00005912
5913 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005914
Douglas Gregor0a897e32009-10-15 17:21:20 +00005915 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005916 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005917
Douglas Gregor558c0322009-10-14 23:41:34 +00005918 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005919 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005920 // or a static data member of a class template specialization, the name of
5921 // the class template specialization in the qualified-id for the member
5922 // name shall be a simple-template-id.
5923 //
5924 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005925 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005926 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005927 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00005928 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005929 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005930 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005931 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005932
Douglas Gregor558c0322009-10-14 23:41:34 +00005933 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005934 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00005935 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005936 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00005937 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005938
Douglas Gregord5a423b2009-09-25 18:43:00 +00005939 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00005940 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005941}
5942
John McCallf312b1e2010-08-26 23:41:50 +00005943TypeResult
John McCallc4e70192009-09-11 04:59:25 +00005944Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
5945 const CXXScopeSpec &SS, IdentifierInfo *Name,
5946 SourceLocation TagLoc, SourceLocation NameLoc) {
5947 // This has to hold, because SS is expected to be defined.
5948 assert(Name && "Expected a name in a dependent tag");
5949
5950 NestedNameSpecifier *NNS
5951 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
5952 if (!NNS)
5953 return true;
5954
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005955 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00005956
Douglas Gregor48c89f42010-04-24 16:38:41 +00005957 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
5958 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005959 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00005960 return true;
5961 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005962
Douglas Gregor059101f2011-03-02 00:47:37 +00005963 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005964 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00005965 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
5966
5967 // Create type-source location information for this type.
5968 TypeLocBuilder TLB;
5969 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
5970 TL.setKeywordLoc(TagLoc);
5971 TL.setQualifierLoc(SS.getWithLocInContext(Context));
5972 TL.setNameLoc(NameLoc);
5973 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00005974}
5975
John McCallf312b1e2010-08-26 23:41:50 +00005976TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005977Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
5978 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005979 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00005980 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00005981 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00005982
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005983 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
5984 !getLangOptions().CPlusPlus0x)
5985 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005986 << FixItHint::CreateRemoval(TypenameLoc);
5987
Douglas Gregor2494dd02011-03-01 01:34:45 +00005988 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00005989 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
5990 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00005991 if (T.isNull())
5992 return true;
John McCall63b43852010-04-29 23:50:39 +00005993
5994 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
5995 if (isa<DependentNameType>(T)) {
5996 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00005997 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00005998 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00005999 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006000 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006001 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006002 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006003 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006004 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006005 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006006
John McCallb3d87482010-08-24 05:47:05 +00006007 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006008}
6009
John McCallf312b1e2010-08-26 23:41:50 +00006010TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006011Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6012 const CXXScopeSpec &SS,
6013 SourceLocation TemplateLoc,
6014 TemplateTy TemplateIn,
6015 SourceLocation TemplateNameLoc,
6016 SourceLocation LAngleLoc,
6017 ASTTemplateArgsPtr TemplateArgsIn,
6018 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006019 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6020 !getLangOptions().CPlusPlus0x)
6021 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006022 << FixItHint::CreateRemoval(TypenameLoc);
6023
6024 // Translate the parser's template argument list in our AST format.
6025 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6026 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6027
6028 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006029 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6030 // Construct a dependent template specialization type.
6031 assert(DTN && "dependent template has non-dependent name?");
6032 assert(DTN->getQualifier()
6033 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6034 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6035 DTN->getQualifier(),
6036 DTN->getIdentifier(),
6037 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006038
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006039 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006040 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006041 DependentTemplateSpecializationTypeLoc SpecTL
6042 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006043 SpecTL.setLAngleLoc(LAngleLoc);
6044 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006045 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006046 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006047 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006048 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6049 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006050 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006051 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006052
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006053 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6054 if (T.isNull())
6055 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006056
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006057 // Provide source-location information for the template specialization
6058 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006059 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006060 TemplateSpecializationTypeLoc SpecTL
6061 = Builder.push<TemplateSpecializationTypeLoc>(T);
6062
6063 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006064 SpecTL.setLAngleLoc(LAngleLoc);
6065 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006066 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006067 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6068 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6069
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006070 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6071 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6072 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006073 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6074
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006075 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6076 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006077}
6078
Douglas Gregora02411e2011-02-27 22:46:49 +00006079
Douglas Gregord57959a2009-03-27 23:10:48 +00006080/// \brief Build the type that describes a C++ typename specifier,
6081/// e.g., "typename T::type".
6082QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006083Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6084 SourceLocation KeywordLoc,
6085 NestedNameSpecifierLoc QualifierLoc,
6086 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006087 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006088 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006089 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006090
John McCall77bb1aa2010-05-01 00:40:08 +00006091 DeclContext *Ctx = computeDeclContext(SS);
6092 if (!Ctx) {
6093 // If the nested-name-specifier is dependent and couldn't be
6094 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006095 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6096 return Context.getDependentNameType(Keyword,
6097 QualifierLoc.getNestedNameSpecifier(),
6098 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006099 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006100
John McCall77bb1aa2010-05-01 00:40:08 +00006101 // If the nested-name-specifier refers to the current instantiation,
6102 // the "typename" keyword itself is superfluous. In C++03, the
6103 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6104 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006105 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006106
John McCall77bb1aa2010-05-01 00:40:08 +00006107 if (RequireCompleteDeclContext(SS, Ctx))
6108 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006109
6110 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006111 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006112 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006113 unsigned DiagID = 0;
6114 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006115 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006116 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006117 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006118 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006119
6120 case LookupResult::FoundUnresolvedValue: {
6121 // We found a using declaration that is a value. Most likely, the using
6122 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006123 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006124 IILoc);
6125 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6126 << Name << Ctx << FullRange;
6127 if (UnresolvedUsingValueDecl *Using
6128 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006129 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006130 Diag(Loc, diag::note_using_value_decl_missing_typename)
6131 << FixItHint::CreateInsertion(Loc, "typename ");
6132 }
6133 }
6134 // Fall through to create a dependent typename type, from which we can recover
6135 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006136
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006137 case LookupResult::NotFoundInCurrentInstantiation:
6138 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006139 return Context.getDependentNameType(Keyword,
6140 QualifierLoc.getNestedNameSpecifier(),
6141 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006142
6143 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006144 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006145 // We found a type. Build an ElaboratedType, since the
6146 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006147 return Context.getElaboratedType(ETK_Typename,
6148 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006149 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006150 }
6151
6152 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006153 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006154 break;
6155
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006156
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006157 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006158 return QualType();
6159
Douglas Gregord57959a2009-03-27 23:10:48 +00006160 case LookupResult::FoundOverloaded:
6161 DiagID = diag::err_typename_nested_not_type;
6162 Referenced = *Result.begin();
6163 break;
6164
John McCall6e247262009-10-10 05:48:19 +00006165 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006166 return QualType();
6167 }
6168
6169 // If we get here, it's because name lookup did not find a
6170 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006171 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006172 IILoc);
6173 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006174 if (Referenced)
6175 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6176 << Name;
6177 return QualType();
6178}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006179
6180namespace {
6181 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006182 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006183 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006184 SourceLocation Loc;
6185 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006186
Douglas Gregor4a959d82009-08-06 16:20:37 +00006187 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006188 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006189
Mike Stump1eb44332009-09-09 15:08:12 +00006190 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006191 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006192 DeclarationName Entity)
6193 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006194 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006195
6196 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006197 /// transformed.
6198 ///
6199 /// For the purposes of type reconstruction, a type has already been
6200 /// transformed if it is NULL or if it is not dependent.
6201 bool AlreadyTransformed(QualType T) {
6202 return T.isNull() || !T->isDependentType();
6203 }
Mike Stump1eb44332009-09-09 15:08:12 +00006204
6205 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006206 /// rebuilt.
6207 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006208
Douglas Gregor4a959d82009-08-06 16:20:37 +00006209 /// \brief Returns the name of the entity whose type is being rebuilt.
6210 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006211
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006212 /// \brief Sets the "base" location and entity when that
6213 /// information is known based on another transformation.
6214 void setBase(SourceLocation Loc, DeclarationName Entity) {
6215 this->Loc = Loc;
6216 this->Entity = Entity;
6217 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006218 };
6219}
6220
Douglas Gregor4a959d82009-08-06 16:20:37 +00006221/// \brief Rebuilds a type within the context of the current instantiation.
6222///
Mike Stump1eb44332009-09-09 15:08:12 +00006223/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006224/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006225/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006226/// partial specialization thereof). This routine will rebuild that type now
6227/// that we have entered the declarator's scope, which may produce different
6228/// canonical types, e.g.,
6229///
6230/// \code
6231/// template<typename T>
6232/// struct X {
6233/// typedef T* pointer;
6234/// pointer data();
6235/// };
6236///
6237/// template<typename T>
6238/// typename X<T>::pointer X<T>::data() { ... }
6239/// \endcode
6240///
Douglas Gregor4714c122010-03-31 17:34:00 +00006241/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006242/// since we do not know that we can look into X<T> when we parsed the type.
6243/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006244/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006245/// as the canonical type of T*, allowing the return types of the out-of-line
6246/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006247TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6248 SourceLocation Loc,
6249 DeclarationName Name) {
6250 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006251 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006252
Douglas Gregor4a959d82009-08-06 16:20:37 +00006253 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6254 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006255}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006256
John McCall60d7b3a2010-08-24 06:29:42 +00006257ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006258 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6259 DeclarationName());
6260 return Rebuilder.TransformExpr(E);
6261}
6262
John McCall63b43852010-04-29 23:50:39 +00006263bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006264 if (SS.isInvalid())
6265 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006266
Douglas Gregor7e384942011-02-25 16:07:42 +00006267 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006268 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6269 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006270 NestedNameSpecifierLoc Rebuilt
6271 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6272 if (!Rebuilt)
6273 return true;
John McCall63b43852010-04-29 23:50:39 +00006274
Douglas Gregor7e384942011-02-25 16:07:42 +00006275 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006276 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006277}
6278
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006279/// \brief Produces a formatted string that describes the binding of
6280/// template parameters to template arguments.
6281std::string
6282Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6283 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006284 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006285}
6286
6287std::string
6288Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6289 const TemplateArgument *Args,
6290 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006291 llvm::SmallString<128> Str;
6292 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006293
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006294 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006295 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006296
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006297 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006298 if (I >= NumArgs)
6299 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006300
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006301 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006302 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006303 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006304 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006305
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006306 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006307 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006308 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006309 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006310 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006311
Douglas Gregor87dd6972010-12-20 16:52:59 +00006312 Out << " = ";
6313 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006314 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006315
6316 Out << ']';
6317 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006318}