blob: 23329a3ab6c04b1dc9fe692135c49e78eb75e571 [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(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000528 KeyLoc, Loc, Depth, Position, ParamName,
529 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000530 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000531 if (Invalid)
532 Param->setInvalidDecl();
533
534 if (ParamName) {
535 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000536 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000537 IdResolver.AddDecl(Param);
538 }
539
Douglas Gregor61c4d282011-01-05 15:48:55 +0000540 // C++0x [temp.param]p9:
541 // A default template-argument may be specified for any kind of
542 // template-parameter that is not a template parameter pack.
543 if (DefaultArg && Ellipsis) {
544 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
545 DefaultArg = ParsedType();
546 }
547
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000548 // Handle the default argument, if provided.
549 if (DefaultArg) {
550 TypeSourceInfo *DefaultTInfo;
551 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000552
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000553 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000554
Douglas Gregor6f526752010-12-16 08:48:57 +0000555 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000556 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000557 UPPC_DefaultArgument))
558 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000559
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000560 // Check the template argument itself.
561 if (CheckTemplateArgument(Param, DefaultTInfo)) {
562 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000563 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000564 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000565
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000566 Param->setDefaultArgument(DefaultTInfo, false);
567 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000568
John McCalld226f652010-08-21 09:40:31 +0000569 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000570}
571
Douglas Gregor2943aed2009-03-03 04:44:36 +0000572/// \brief Check that the type of a non-type template parameter is
573/// well-formed.
574///
575/// \returns the (possibly-promoted) parameter type if valid;
576/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000577QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000578Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000579 // We don't allow variably-modified types as the type of non-type template
580 // parameters.
581 if (T->isVariablyModifiedType()) {
582 Diag(Loc, diag::err_variably_modified_nontype_template_param)
583 << T;
584 return QualType();
585 }
586
Douglas Gregor2943aed2009-03-03 04:44:36 +0000587 // C++ [temp.param]p4:
588 //
589 // A non-type template-parameter shall have one of the following
590 // (optionally cv-qualified) types:
591 //
592 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000593 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000594 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000595 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000596 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000597 T->isReferenceType() ||
598 // -- pointer to member.
599 T->isMemberPointerType() ||
600 // If T is a dependent type, we can't do the check now, so we
601 // assume that it is well-formed.
602 T->isDependentType())
603 return T;
604 // C++ [temp.param]p8:
605 //
606 // A non-type template-parameter of type "array of T" or
607 // "function returning T" is adjusted to be of type "pointer to
608 // T" or "pointer to function returning T", respectively.
609 else if (T->isArrayType())
610 // FIXME: Keep the type prior to promotion?
611 return Context.getArrayDecayedType(T);
612 else if (T->isFunctionType())
613 // FIXME: Keep the type prior to promotion?
614 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000615
Douglas Gregor2943aed2009-03-03 04:44:36 +0000616 Diag(Loc, diag::err_template_nontype_parm_bad_type)
617 << T;
618
619 return QualType();
620}
621
John McCalld226f652010-08-21 09:40:31 +0000622Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
623 unsigned Depth,
624 unsigned Position,
625 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000626 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000627 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
628 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000629
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000630 assert(S->isTemplateParamScope() &&
631 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000632 bool Invalid = false;
633
634 IdentifierInfo *ParamName = D.getIdentifier();
635 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000636 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000637 LookupOrdinaryName,
638 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000639 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000640 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000641 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000642 }
643
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000644 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
645 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000646 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000647 Invalid = true;
648 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000649
Douglas Gregor10738d32010-12-23 23:51:58 +0000650 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000651 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000652 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000653 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000654 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000655 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000656 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000657 Param->setAccess(AS_public);
658
Douglas Gregor72c3f312008-12-05 18:15:24 +0000659 if (Invalid)
660 Param->setInvalidDecl();
661
662 if (D.getIdentifier()) {
663 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000664 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000665 IdResolver.AddDecl(Param);
666 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000667
Douglas Gregor61c4d282011-01-05 15:48:55 +0000668 // C++0x [temp.param]p9:
669 // A default template-argument may be specified for any kind of
670 // template-parameter that is not a template parameter pack.
671 if (Default && IsParameterPack) {
672 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
673 Default = 0;
674 }
675
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000676 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000677 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000678 // Check for unexpanded parameter packs.
679 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
680 return Param;
681
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000682 TemplateArgument Converted;
683 if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) {
684 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000685 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000686 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000687
John McCall9ae2f072010-08-23 23:25:46 +0000688 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000689 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000690
John McCalld226f652010-08-21 09:40:31 +0000691 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000692}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000693
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000694/// ActOnTemplateTemplateParameter - Called when a C++ template template
695/// parameter (e.g. T in template <template <typename> class T> class array)
696/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000697Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
698 SourceLocation TmpLoc,
699 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000700 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000701 IdentifierInfo *Name,
702 SourceLocation NameLoc,
703 unsigned Depth,
704 unsigned Position,
705 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000706 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000707 assert(S->isTemplateParamScope() &&
708 "Template template parameter not in template parameter scope!");
709
710 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000711 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000712 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000713 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000714 NameLoc.isInvalid()? TmpLoc : NameLoc,
715 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000716 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000717 Param->setAccess(AS_public);
718
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000719 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000720 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000721 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000722 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000723 IdResolver.AddDecl(Param);
724 }
725
Douglas Gregor6f526752010-12-16 08:48:57 +0000726 if (Params->size() == 0) {
727 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
728 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
729 Param->setInvalidDecl();
730 }
731
Douglas Gregor61c4d282011-01-05 15:48:55 +0000732 // C++0x [temp.param]p9:
733 // A default template-argument may be specified for any kind of
734 // template-parameter that is not a template parameter pack.
735 if (IsParameterPack && !Default.isInvalid()) {
736 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
737 Default = ParsedTemplateArgument();
738 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000739
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000740 if (!Default.isInvalid()) {
741 // Check only that we have a template template argument. We don't want to
742 // try to check well-formedness now, because our template template parameter
743 // might have dependent types in its template parameters, which we wouldn't
744 // be able to match now.
745 //
746 // If none of the template template parameter's template arguments mention
747 // other template parameters, we could actually perform more checking here.
748 // However, it isn't worth doing.
749 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
750 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
751 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
752 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000753 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000754 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000755
Douglas Gregor6f526752010-12-16 08:48:57 +0000756 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000757 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000758 DefaultArg.getArgument().getAsTemplate(),
759 UPPC_DefaultArgument))
760 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000761
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000762 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000763 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000764
John McCalld226f652010-08-21 09:40:31 +0000765 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000766}
767
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000768/// ActOnTemplateParameterList - Builds a TemplateParameterList that
769/// contains the template parameters in Params/NumParams.
770Sema::TemplateParamsTy *
771Sema::ActOnTemplateParameterList(unsigned Depth,
772 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000773 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000774 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000775 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000776 SourceLocation RAngleLoc) {
777 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000778 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000779
Douglas Gregorddc29e12009-02-06 22:42:48 +0000780 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000781 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000782 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000783}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000784
John McCallb6217662010-03-15 10:12:16 +0000785static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
786 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000787 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000788}
789
John McCallf312b1e2010-08-26 23:41:50 +0000790DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000791Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000792 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000793 IdentifierInfo *Name, SourceLocation NameLoc,
794 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000795 TemplateParameterList *TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000796 AccessSpecifier AS,
797 unsigned NumOuterTemplateParamLists,
798 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000799 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000800 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000801 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000802 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000803
804 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000805 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000806 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000807
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000808 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
809 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000810
811 // There is no such thing as an unnamed class template.
812 if (!Name) {
813 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000814 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000815 }
816
817 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000818 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000819 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000820 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000821 if (SS.isNotEmpty() && !SS.isInvalid()) {
822 SemanticContext = computeDeclContext(SS, true);
823 if (!SemanticContext) {
824 // FIXME: Produce a reasonable diagnostic here
825 return true;
826 }
Mike Stump1eb44332009-09-09 15:08:12 +0000827
John McCall77bb1aa2010-05-01 00:40:08 +0000828 if (RequireCompleteDeclContext(SS, SemanticContext))
829 return true;
830
John McCalla24dc2e2009-11-17 02:14:36 +0000831 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000832 } else {
833 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000834 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000835 }
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Douglas Gregor57265e32010-04-12 16:00:01 +0000837 if (Previous.isAmbiguous())
838 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000839
Douglas Gregorddc29e12009-02-06 22:42:48 +0000840 NamedDecl *PrevDecl = 0;
841 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000842 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000843
Douglas Gregorddc29e12009-02-06 22:42:48 +0000844 // If there is a previous declaration with the same name, check
845 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000846 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000847 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000848
849 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000850 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000851 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000852 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000853 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
854 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000855 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000856 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
857 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
858 PrevClassTemplate
859 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
860 ->getSpecializedTemplate();
861 }
862 }
863
John McCall65c49462009-12-18 11:25:59 +0000864 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000865 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000866 // [...] When looking for a prior declaration of a class or a function
867 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000868 // function is neither a qualified name nor a template-id, scopes outside
869 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000870 if (!SS.isSet()) {
871 DeclContext *OutermostContext = CurContext;
872 while (!OutermostContext->isFileContext())
873 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000874
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000875 if (PrevDecl &&
876 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
877 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
878 SemanticContext = PrevDecl->getDeclContext();
879 } else {
880 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000881 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000882 // declaration.
883 PrevDecl = PrevClassTemplate = 0;
884 SemanticContext = OutermostContext;
885 }
John McCalle129d442009-12-17 23:21:11 +0000886 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000887
John McCalle129d442009-12-17 23:21:11 +0000888 if (CurContext->isDependentContext()) {
889 // If this is a dependent context, we don't want to link the friend
890 // class template to the template in scope, because that would perform
891 // checking of the template parameter lists that can't be performed
892 // until the outer context is instantiated.
893 PrevDecl = PrevClassTemplate = 0;
894 }
895 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
896 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000897
Douglas Gregorddc29e12009-02-06 22:42:48 +0000898 if (PrevClassTemplate) {
899 // Ensure that the template parameter lists are compatible.
900 if (!TemplateParameterListsAreEqual(TemplateParams,
901 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000902 /*Complain=*/true,
903 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000904 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000905
906 // C++ [temp.class]p4:
907 // In a redeclaration, partial specialization, explicit
908 // specialization or explicit instantiation of a class template,
909 // the class-key shall agree in kind with the original class
910 // template declaration (7.1.5.3).
911 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000912 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000913 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000914 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000915 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000916 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000917 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000918 }
919
Douglas Gregorddc29e12009-02-06 22:42:48 +0000920 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000921 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000922 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000923 Diag(NameLoc, diag::err_redefinition) << Name;
924 Diag(Def->getLocation(), diag::note_previous_definition);
925 // FIXME: Would it make sense to try to "forget" the previous
926 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000927 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000928 }
929 }
930 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
931 // Maybe we will complain about the shadowed template parameter.
932 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
933 // Just pretend that we didn't see the previous declaration.
934 PrevDecl = 0;
935 } else if (PrevDecl) {
936 // C++ [temp]p5:
937 // A class template shall not have the same name as any other
938 // template, class, function, object, enumeration, enumerator,
939 // namespace, or type in the same scope (3.3), except as specified
940 // in (14.5.4).
941 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
942 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000943 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000944 }
945
Douglas Gregord684b002009-02-10 19:49:53 +0000946 // Check the template parameter list of this declaration, possibly
947 // merging in the template parameter list from the previous class
948 // template declaration.
949 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000950 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000951 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000952 SemanticContext->isRecord() &&
953 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000954 ? TPC_ClassTemplateMember
955 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000956 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Douglas Gregor57265e32010-04-12 16:00:01 +0000958 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000959 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000960 // template out-of-line.
961 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
962 !(TUK == TUK_Friend && CurContext->isDependentContext()))
963 Diag(NameLoc, diag::err_member_def_does_not_match)
964 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000965 }
966
Mike Stump1eb44332009-09-09 15:08:12 +0000967 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +0000968 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +0000969 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000970 PrevClassTemplate->getTemplatedDecl() : 0,
971 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000972 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000973 if (NumOuterTemplateParamLists > 0)
974 NewClass->setTemplateParameterListsInfo(Context,
975 NumOuterTemplateParamLists,
976 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000977
978 ClassTemplateDecl *NewTemplate
979 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
980 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000981 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000982 NewClass->setDescribedClassTemplate(NewTemplate);
983
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000984 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000985 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000986 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000987 assert(T->isDependentType() && "Class template type is not dependent?");
988 (void)T;
989
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000990 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000991 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000992 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000993 PrevClassTemplate->getInstantiatedFromMemberTemplate())
994 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000995
Anders Carlsson4cbe82c2009-03-26 01:24:28 +0000996 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +0000997 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +0000998 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Douglas Gregorddc29e12009-02-06 22:42:48 +00001000 // Set the lexical context of these templates
1001 NewClass->setLexicalDeclContext(CurContext);
1002 NewTemplate->setLexicalDeclContext(CurContext);
1003
John McCall0f434ec2009-07-31 02:45:11 +00001004 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001005 NewClass->startDefinition();
1006
1007 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001008 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001009
John McCall05b23ea2009-09-14 21:59:20 +00001010 if (TUK != TUK_Friend)
1011 PushOnScopeChains(NewTemplate, S);
1012 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001013 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001014 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001015 NewClass->setAccess(PrevClassTemplate->getAccess());
1016 }
John McCall05b23ea2009-09-14 21:59:20 +00001017
Douglas Gregord85bea22009-09-26 06:47:28 +00001018 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1019 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001020
John McCall05b23ea2009-09-14 21:59:20 +00001021 // Friend templates are visible in fairly strange ways.
1022 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001023 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001024 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1025 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1026 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001027 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001028 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001029
Douglas Gregord85bea22009-09-26 06:47:28 +00001030 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1031 NewClass->getLocation(),
1032 NewTemplate,
1033 /*FIXME:*/NewClass->getLocation());
1034 Friend->setAccess(AS_public);
1035 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001036 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001037
Douglas Gregord684b002009-02-10 19:49:53 +00001038 if (Invalid) {
1039 NewTemplate->setInvalidDecl();
1040 NewClass->setInvalidDecl();
1041 }
John McCalld226f652010-08-21 09:40:31 +00001042 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001043}
1044
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001045/// \brief Diagnose the presence of a default template argument on a
1046/// template parameter, which is ill-formed in certain contexts.
1047///
1048/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001049static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001050 Sema::TemplateParamListContext TPC,
1051 SourceLocation ParamLoc,
1052 SourceRange DefArgRange) {
1053 switch (TPC) {
1054 case Sema::TPC_ClassTemplate:
1055 return false;
1056
1057 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001058 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001059 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001060 // A default template-argument shall not be specified in a
1061 // function template declaration or a function template
1062 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001063 // If a friend function template declaration specifies a default
1064 // template-argument, that declaration shall be a definition and shall be
1065 // the only declaration of the function template in the translation unit.
1066 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001067 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001068 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001069 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001070 << DefArgRange;
1071 return false;
1072
1073 case Sema::TPC_ClassTemplateMember:
1074 // C++0x [temp.param]p9:
1075 // A default template-argument shall not be specified in the
1076 // template-parameter-lists of the definition of a member of a
1077 // class template that appears outside of the member's class.
1078 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1079 << DefArgRange;
1080 return true;
1081
1082 case Sema::TPC_FriendFunctionTemplate:
1083 // C++ [temp.param]p9:
1084 // A default template-argument shall not be specified in a
1085 // friend template declaration.
1086 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1087 << DefArgRange;
1088 return true;
1089
1090 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1091 // for friend function templates if there is only a single
1092 // declaration (and it is a definition). Strange!
1093 }
1094
1095 return false;
1096}
1097
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001098/// \brief Check for unexpanded parameter packs within the template parameters
1099/// of a template template parameter, recursively.
1100bool DiagnoseUnexpandedParameterPacks(Sema &S, TemplateTemplateParmDecl *TTP){
1101 TemplateParameterList *Params = TTP->getTemplateParameters();
1102 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1103 NamedDecl *P = Params->getParam(I);
1104 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001105 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001106 NTTP->getTypeSourceInfo(),
1107 Sema::UPPC_NonTypeTemplateParameterType))
1108 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001109
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001110 continue;
1111 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001112
1113 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001114 = dyn_cast<TemplateTemplateParmDecl>(P))
1115 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1116 return true;
1117 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001118
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001119 return false;
1120}
1121
Douglas Gregord684b002009-02-10 19:49:53 +00001122/// \brief Checks the validity of a template parameter list, possibly
1123/// considering the template parameter list from a previous
1124/// declaration.
1125///
1126/// If an "old" template parameter list is provided, it must be
1127/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1128/// template parameter list.
1129///
1130/// \param NewParams Template parameter list for a new template
1131/// declaration. This template parameter list will be updated with any
1132/// default arguments that are carried through from the previous
1133/// template parameter list.
1134///
1135/// \param OldParams If provided, template parameter list from a
1136/// previous declaration of the same template. Default template
1137/// arguments will be merged from the old template parameter list to
1138/// the new template parameter list.
1139///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001140/// \param TPC Describes the context in which we are checking the given
1141/// template parameter list.
1142///
Douglas Gregord684b002009-02-10 19:49:53 +00001143/// \returns true if an error occurred, false otherwise.
1144bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001145 TemplateParameterList *OldParams,
1146 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001147 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Douglas Gregord684b002009-02-10 19:49:53 +00001149 // C++ [temp.param]p10:
1150 // The set of default template-arguments available for use with a
1151 // template declaration or definition is obtained by merging the
1152 // default arguments from the definition (if in scope) and all
1153 // declarations in scope in the same way default function
1154 // arguments are (8.3.6).
1155 bool SawDefaultArgument = false;
1156 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001157
Anders Carlsson49d25572009-06-12 23:20:15 +00001158 bool SawParameterPack = false;
1159 SourceLocation ParameterPackLoc;
1160
Mike Stump1a35fde2009-02-11 23:03:27 +00001161 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001162 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001163 if (OldParams)
1164 OldParam = OldParams->begin();
1165
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001166 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001167 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1168 NewParamEnd = NewParams->end();
1169 NewParam != NewParamEnd; ++NewParam) {
1170 // Variables used to diagnose redundant default arguments
1171 bool RedundantDefaultArg = false;
1172 SourceLocation OldDefaultLoc;
1173 SourceLocation NewDefaultLoc;
1174
1175 // Variables used to diagnose missing default arguments
1176 bool MissingDefaultArg = false;
1177
Anders Carlsson49d25572009-06-12 23:20:15 +00001178 // C++0x [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001179 // If a template parameter of a primary class template is a template
Douglas Gregor1ed64762011-01-05 16:19:19 +00001180 // parameter pack, it shall be the last template parameter.
1181 if (SawParameterPack && TPC == TPC_ClassTemplate) {
Mike Stump1eb44332009-09-09 15:08:12 +00001182 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001183 diag::err_template_param_pack_must_be_last_template_parameter);
1184 Invalid = true;
1185 }
1186
Douglas Gregord684b002009-02-10 19:49:53 +00001187 if (TemplateTypeParmDecl *NewTypeParm
1188 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001189 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001190 if (NewTypeParm->hasDefaultArgument() &&
1191 DiagnoseDefaultTemplateArgument(*this, TPC,
1192 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001193 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001194 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001195 NewTypeParm->removeDefaultArgument();
1196
1197 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001198 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001199 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Anders Carlsson49d25572009-06-12 23:20:15 +00001201 if (NewTypeParm->isParameterPack()) {
1202 assert(!NewTypeParm->hasDefaultArgument() &&
1203 "Parameter packs can't have a default argument!");
1204 SawParameterPack = true;
1205 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001206 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001207 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001208 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1209 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1210 SawDefaultArgument = true;
1211 RedundantDefaultArg = true;
1212 PreviousDefaultArgLoc = NewDefaultLoc;
1213 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1214 // Merge the default argument from the old declaration to the
1215 // new declaration.
1216 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001217 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001218 true);
1219 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1220 } else if (NewTypeParm->hasDefaultArgument()) {
1221 SawDefaultArgument = true;
1222 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1223 } else if (SawDefaultArgument)
1224 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001225 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001226 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001227 // Check for unexpanded parameter packs.
1228 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001229 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001230 UPPC_NonTypeTemplateParameterType)) {
1231 Invalid = true;
1232 continue;
1233 }
1234
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001235 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001236 if (NewNonTypeParm->hasDefaultArgument() &&
1237 DiagnoseDefaultTemplateArgument(*this, TPC,
1238 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001239 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001240 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001241 }
1242
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001243 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001244 NonTypeTemplateParmDecl *OldNonTypeParm
1245 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001246 if (NewNonTypeParm->isParameterPack()) {
1247 assert(!NewNonTypeParm->hasDefaultArgument() &&
1248 "Parameter packs can't have a default argument!");
1249 SawParameterPack = true;
1250 ParameterPackLoc = NewNonTypeParm->getLocation();
1251 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001252 NewNonTypeParm->hasDefaultArgument()) {
1253 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1254 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1255 SawDefaultArgument = true;
1256 RedundantDefaultArg = true;
1257 PreviousDefaultArgLoc = NewDefaultLoc;
1258 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1259 // Merge the default argument from the old declaration to the
1260 // new declaration.
1261 SawDefaultArgument = true;
1262 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001263 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001264 // parameter.
1265 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001266 OldNonTypeParm->getDefaultArgument(),
1267 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001268 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1269 } else if (NewNonTypeParm->hasDefaultArgument()) {
1270 SawDefaultArgument = true;
1271 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1272 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001273 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001274 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001275 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001276 TemplateTemplateParmDecl *NewTemplateParm
1277 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001278
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001279 // Check for unexpanded parameter packs, recursively.
1280 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1281 Invalid = true;
1282 continue;
1283 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001284
1285 if (NewTemplateParm->hasDefaultArgument() &&
1286 DiagnoseDefaultTemplateArgument(*this, TPC,
1287 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001288 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001289 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001290
1291 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001292 TemplateTemplateParmDecl *OldTemplateParm
1293 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001294 if (NewTemplateParm->isParameterPack()) {
1295 assert(!NewTemplateParm->hasDefaultArgument() &&
1296 "Parameter packs can't have a default argument!");
1297 SawParameterPack = true;
1298 ParameterPackLoc = NewTemplateParm->getLocation();
1299 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001300 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001301 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1302 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001303 SawDefaultArgument = true;
1304 RedundantDefaultArg = true;
1305 PreviousDefaultArgLoc = NewDefaultLoc;
1306 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1307 // Merge the default argument from the old declaration to the
1308 // new declaration.
1309 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001310 // FIXME: We need to create a new kind of "default argument" expression
1311 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001312 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001313 OldTemplateParm->getDefaultArgument(),
1314 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001315 PreviousDefaultArgLoc
1316 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001317 } else if (NewTemplateParm->hasDefaultArgument()) {
1318 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001319 PreviousDefaultArgLoc
1320 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001321 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001322 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001323 }
1324
1325 if (RedundantDefaultArg) {
1326 // C++ [temp.param]p12:
1327 // A template-parameter shall not be given default arguments
1328 // by two different declarations in the same scope.
1329 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1330 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1331 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001332 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001333 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001334 // If a template-parameter of a class template has a default
1335 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001336 // have a default template-argument supplied or be a template parameter
1337 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001338 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001339 diag::err_template_param_default_arg_missing);
1340 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1341 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001342 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001343 }
1344
1345 // If we have an old template parameter list that we're merging
1346 // in, move on to the next parameter.
1347 if (OldParams)
1348 ++OldParam;
1349 }
1350
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001351 // We were missing some default arguments at the end of the list, so remove
1352 // all of the default arguments.
1353 if (RemoveDefaultArguments) {
1354 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1355 NewParamEnd = NewParams->end();
1356 NewParam != NewParamEnd; ++NewParam) {
1357 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1358 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001359 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001360 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1361 NTTP->removeDefaultArgument();
1362 else
1363 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1364 }
1365 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001366
Douglas Gregord684b002009-02-10 19:49:53 +00001367 return Invalid;
1368}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001369
John McCall4e2cbb22010-10-20 05:44:58 +00001370namespace {
1371
1372/// A class which looks for a use of a certain level of template
1373/// parameter.
1374struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1375 typedef RecursiveASTVisitor<DependencyChecker> super;
1376
1377 unsigned Depth;
1378 bool Match;
1379
1380 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1381 NamedDecl *ND = Params->getParam(0);
1382 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1383 Depth = PD->getDepth();
1384 } else if (NonTypeTemplateParmDecl *PD =
1385 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1386 Depth = PD->getDepth();
1387 } else {
1388 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1389 }
1390 }
1391
1392 bool Matches(unsigned ParmDepth) {
1393 if (ParmDepth >= Depth) {
1394 Match = true;
1395 return true;
1396 }
1397 return false;
1398 }
1399
1400 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1401 return !Matches(T->getDepth());
1402 }
1403
1404 bool TraverseTemplateName(TemplateName N) {
1405 if (TemplateTemplateParmDecl *PD =
1406 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1407 if (Matches(PD->getDepth())) return false;
1408 return super::TraverseTemplateName(N);
1409 }
1410
1411 bool VisitDeclRefExpr(DeclRefExpr *E) {
1412 if (NonTypeTemplateParmDecl *PD =
1413 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1414 if (PD->getDepth() == Depth) {
1415 Match = true;
1416 return false;
1417 }
1418 }
1419 return super::VisitDeclRefExpr(E);
1420 }
1421};
1422}
1423
1424/// Determines whether a template-id depends on the given parameter
1425/// list.
1426static bool
1427DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId,
1428 TemplateParameterList *Params) {
1429 DependencyChecker Checker(Params);
1430 Checker.TraverseType(QualType(TemplateId, 0));
1431 return Checker.Match;
1432}
1433
Mike Stump1eb44332009-09-09 15:08:12 +00001434/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001435/// specifier, returning the template parameter list that applies to the
1436/// name.
1437///
1438/// \param DeclStartLoc the start of the declaration that has a scope
1439/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001440///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001441/// \param SS the scope specifier that will be matched to the given template
1442/// parameter lists. This scope specifier precedes a qualified name that is
1443/// being declared.
1444///
1445/// \param ParamLists the template parameter lists, from the outermost to the
1446/// innermost template parameter lists.
1447///
1448/// \param NumParamLists the number of template parameter lists in ParamLists.
1449///
John McCall77e8b112010-04-13 20:37:33 +00001450/// \param IsFriend Whether to apply the slightly different rules for
1451/// matching template parameters to scope specifiers in friend
1452/// declarations.
1453///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001454/// \param IsExplicitSpecialization will be set true if the entity being
1455/// declared is an explicit specialization, false otherwise.
1456///
Mike Stump1eb44332009-09-09 15:08:12 +00001457/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001458/// name that is preceded by the scope specifier @p SS. This template
1459/// parameter list may be have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001460/// template) or may have no template parameters (if we're declaring a
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001461/// template specialization), or may be NULL (if we were's declaring isn't
1462/// itself a template).
1463TemplateParameterList *
1464Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
1465 const CXXScopeSpec &SS,
1466 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001467 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001468 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001469 bool &IsExplicitSpecialization,
1470 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001471 IsExplicitSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001472
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001473 // Find the template-ids that occur within the nested-name-specifier. These
1474 // template-ids will match up with the template parameter lists.
1475 llvm::SmallVector<const TemplateSpecializationType *, 4>
1476 TemplateIdsInSpecifier;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001477 llvm::SmallVector<ClassTemplateSpecializationDecl *, 4>
1478 ExplicitSpecializationsInSpecifier;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001479 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
1480 NNS; NNS = NNS->getPrefix()) {
John McCall4b2b02b2009-12-15 02:19:47 +00001481 const Type *T = NNS->getAsType();
1482 if (!T) break;
1483
1484 // C++0x [temp.expl.spec]p17:
1485 // A member or a member template may be nested within many
1486 // enclosing class templates. In an explicit specialization for
1487 // such a member, the member declaration shall be preceded by a
1488 // template<> for each enclosing class template that is
1489 // explicitly specialized.
Douglas Gregorfe331062010-02-13 05:23:25 +00001490 //
1491 // Following the existing practice of GNU and EDG, we allow a typedef of a
1492 // template specialization type.
John McCall49f4e1c2010-12-10 11:01:00 +00001493 while (const TypedefType *TT = dyn_cast<TypedefType>(T))
1494 T = TT->getDecl()->getUnderlyingType().getTypePtr();
John McCall4b2b02b2009-12-15 02:19:47 +00001495
Mike Stump1eb44332009-09-09 15:08:12 +00001496 if (const TemplateSpecializationType *SpecType
Douglas Gregorfe331062010-02-13 05:23:25 +00001497 = dyn_cast<TemplateSpecializationType>(T)) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001498 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
1499 if (!Template)
1500 continue; // FIXME: should this be an error? probably...
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Ted Kremenek6217b802009-07-29 21:53:49 +00001502 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001503 ClassTemplateSpecializationDecl *SpecDecl
1504 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
1505 // If the nested name specifier refers to an explicit specialization,
1506 // we don't need a template<> header.
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001507 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
1508 ExplicitSpecializationsInSpecifier.push_back(SpecDecl);
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001509 continue;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001510 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001511 }
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001513 TemplateIdsInSpecifier.push_back(SpecType);
1514 }
1515 }
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001517 // Reverse the list of template-ids in the scope specifier, so that we can
1518 // more easily match up the template-ids and the template parameter lists.
1519 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001520
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001521 SourceLocation FirstTemplateLoc = DeclStartLoc;
1522 if (NumParamLists)
1523 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001525 // Match the template-ids found in the specifier to the template parameter
1526 // lists.
John McCall4e2cbb22010-10-20 05:44:58 +00001527 unsigned ParamIdx = 0, TemplateIdx = 0;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001528 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
John McCall4e2cbb22010-10-20 05:44:58 +00001529 TemplateIdx != NumTemplateIds; ++TemplateIdx) {
1530 const TemplateSpecializationType *TemplateId
1531 = TemplateIdsInSpecifier[TemplateIdx];
Douglas Gregorb88e8882009-07-30 17:40:51 +00001532 bool DependentTemplateId = TemplateId->isDependentType();
John McCall4e2cbb22010-10-20 05:44:58 +00001533
1534 // In friend declarations we can have template-ids which don't
1535 // depend on the corresponding template parameter lists. But
1536 // assume that empty parameter lists are supposed to match this
1537 // template-id.
1538 if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) {
1539 if (!DependentTemplateId ||
1540 !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx]))
1541 continue;
1542 }
1543
1544 if (ParamIdx >= NumParamLists) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001545 // We have a template-id without a corresponding template parameter
1546 // list.
John McCall77e8b112010-04-13 20:37:33 +00001547
1548 // ...which is fine if this is a friend declaration.
1549 if (IsFriend) {
1550 IsExplicitSpecialization = true;
1551 break;
1552 }
1553
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001554 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +00001555 // FIXME: the location information here isn't great.
1556 Diag(SS.getRange().getBegin(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001557 diag::err_template_spec_needs_template_parameters)
John McCall4e2cbb22010-10-20 05:44:58 +00001558 << QualType(TemplateId, 0)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001559 << SS.getRange();
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001560 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001561 } else {
1562 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
1563 << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +00001564 << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001565 IsExplicitSpecialization = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001566 }
1567 return 0;
1568 }
Mike Stump1eb44332009-09-09 15:08:12 +00001569
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001570 // Check the template parameter list against its corresponding template-id.
Douglas Gregorb88e8882009-07-30 17:40:51 +00001571 if (DependentTemplateId) {
John McCall31f17ec2010-04-27 00:57:59 +00001572 TemplateParameterList *ExpectedTemplateParams = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00001573
John McCall31f17ec2010-04-27 00:57:59 +00001574 // Are there cases in (e.g.) friends where this won't match?
1575 if (const InjectedClassNameType *Injected
1576 = TemplateId->getAs<InjectedClassNameType>()) {
1577 CXXRecordDecl *Record = Injected->getDecl();
1578 if (ClassTemplatePartialSpecializationDecl *Partial =
1579 dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
1580 ExpectedTemplateParams = Partial->getTemplateParameters();
1581 else
1582 ExpectedTemplateParams = Record->getDescribedClassTemplate()
1583 ->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001584 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001585
John McCall31f17ec2010-04-27 00:57:59 +00001586 if (ExpectedTemplateParams)
John McCall4e2cbb22010-10-20 05:44:58 +00001587 TemplateParameterListsAreEqual(ParamLists[ParamIdx],
John McCall31f17ec2010-04-27 00:57:59 +00001588 ExpectedTemplateParams,
1589 true, TPL_TemplateMatch);
1590
John McCall4e2cbb22010-10-20 05:44:58 +00001591 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1592 TPC_ClassTemplateMember);
1593 } else if (ParamLists[ParamIdx]->size() > 0)
1594 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00001595 diag::err_template_param_list_matches_nontemplate)
1596 << TemplateId
John McCall4e2cbb22010-10-20 05:44:58 +00001597 << ParamLists[ParamIdx]->getSourceRange();
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001598 else
1599 IsExplicitSpecialization = true;
John McCall4e2cbb22010-10-20 05:44:58 +00001600
1601 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001602 }
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001604 // If there were at least as many template-ids as there were template
1605 // parameter lists, then there are no template parameter lists remaining for
1606 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001607 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001608 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001609
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001610 // If there were too many template parameter lists, complain about that now.
John McCall4e2cbb22010-10-20 05:44:58 +00001611 if (ParamIdx != NumParamLists - 1) {
1612 while (ParamIdx < NumParamLists - 1) {
1613 bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0;
1614 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001615 isExplicitSpecHeader? diag::warn_template_spec_extra_headers
1616 : diag::err_template_spec_extra_headers)
John McCall4e2cbb22010-10-20 05:44:58 +00001617 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1618 ParamLists[ParamIdx]->getRAngleLoc());
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001619
1620 if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) {
1621 Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(),
1622 diag::note_explicit_template_spec_does_not_need_header)
1623 << ExplicitSpecializationsInSpecifier.back();
1624 ExplicitSpecializationsInSpecifier.pop_back();
1625 }
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001626
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001627 // We have a template parameter list with no corresponding scope, which
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001628 // means that the resulting template declaration can't be instantiated
1629 // properly (we'll end up with dependent nodes when we shouldn't).
1630 if (!isExplicitSpecHeader)
1631 Invalid = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001632
John McCall4e2cbb22010-10-20 05:44:58 +00001633 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001634 }
1635 }
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001637 // Return the last template parameter list, which corresponds to the
1638 // entity being declared.
1639 return ParamLists[NumParamLists - 1];
1640}
1641
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001642void Sema::NoteAllFoundTemplates(TemplateName Name) {
1643 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1644 Diag(Template->getLocation(), diag::note_template_declared_here)
1645 << (isa<FunctionTemplateDecl>(Template)? 0
1646 : isa<ClassTemplateDecl>(Template)? 1
1647 : 2)
1648 << Template->getDeclName();
1649 return;
1650 }
1651
1652 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1653 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1654 IEnd = OST->end();
1655 I != IEnd; ++I)
1656 Diag((*I)->getLocation(), diag::note_template_declared_here)
1657 << 0 << (*I)->getDeclName();
1658
1659 return;
1660 }
1661}
1662
1663
Douglas Gregor7532dc62009-03-30 22:58:21 +00001664QualType Sema::CheckTemplateIdType(TemplateName Name,
1665 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001666 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001667 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001668 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1669 // We might have a substituted template template parameter pack. If so,
1670 // build a template specialization type for it.
1671 if (Name.getAsSubstTemplateTemplateParmPack())
1672 return Context.getTemplateSpecializationType(Name, TemplateArgs);
1673
1674 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1675 << Name;
1676 NoteAllFoundTemplates(Name);
1677 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001678 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001679
Douglas Gregor40808ce2009-03-09 23:48:35 +00001680 // Check that the template argument list is well-formed for this
1681 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001682 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001683 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001684 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001685 return QualType();
1686
Douglas Gregor910f8002010-11-07 23:05:16 +00001687 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001688 "Converted template argument list is too short!");
1689
1690 QualType CanonType;
1691
Douglas Gregorcaddba02009-11-12 18:38:13 +00001692 if (Name.isDependent() ||
1693 TemplateSpecializationType::anyDependentTemplateArguments(
John McCalld5532b62009-11-23 01:53:49 +00001694 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001695 // This class template specialization is a dependent
1696 // type. Therefore, its canonical type is another class template
1697 // specialization type that contains all of the converted
1698 // arguments in canonical form. This ensures that, e.g., A<T> and
1699 // A<T, T> have identical types when A is declared as:
1700 //
1701 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001702 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001703 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001704 Converted.data(),
1705 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001706
Douglas Gregor1275ae02009-07-28 23:00:59 +00001707 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001708 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001709 // In the future, we need to teach getTemplateSpecializationType to only
1710 // build the canonical type and return that to us.
1711 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001712
1713 // This might work out to be a current instantiation, in which
1714 // case the canonical type needs to be the InjectedClassNameType.
1715 //
1716 // TODO: in theory this could be a simple hashtable lookup; most
1717 // changes to CurContext don't change the set of current
1718 // instantiations.
1719 if (isa<ClassTemplateDecl>(Template)) {
1720 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1721 // If we get out to a namespace, we're done.
1722 if (Ctx->isFileContext()) break;
1723
1724 // If this isn't a record, keep looking.
1725 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1726 if (!Record) continue;
1727
1728 // Look for one of the two cases with InjectedClassNameTypes
1729 // and check whether it's the same template.
1730 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1731 !Record->getDescribedClassTemplate())
1732 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001733
John McCall31f17ec2010-04-27 00:57:59 +00001734 // Fetch the injected class name type and check whether its
1735 // injected type is equal to the type we just built.
1736 QualType ICNT = Context.getTypeDeclType(Record);
1737 QualType Injected = cast<InjectedClassNameType>(ICNT)
1738 ->getInjectedSpecializationType();
1739
1740 if (CanonType != Injected->getCanonicalTypeInternal())
1741 continue;
1742
1743 // If so, the canonical type of this TST is the injected
1744 // class name type of the record we just found.
1745 assert(ICNT.isCanonical());
1746 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001747 break;
1748 }
1749 }
Mike Stump1eb44332009-09-09 15:08:12 +00001750 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001751 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001752 // Find the class template specialization declaration that
1753 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001754 void *InsertPos = 0;
1755 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001756 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001757 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001758 if (!Decl) {
1759 // This is the first time we have referenced this class template
1760 // specialization. Create the canonical declaration and add it to
1761 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001762 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001763 ClassTemplate->getTemplatedDecl()->getTagKind(),
1764 ClassTemplate->getDeclContext(),
1765 ClassTemplate->getLocation(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001766 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001767 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001768 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001769 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001770 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001771 Decl->setLexicalDeclContext(CurContext);
1772 }
1773
1774 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00001775 assert(isa<RecordType>(CanonType) &&
1776 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00001777 }
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Douglas Gregor40808ce2009-03-09 23:48:35 +00001779 // Build the fully-sugared type for this class template
1780 // specialization, which refers back to the class template
1781 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00001782 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001783}
1784
John McCallf312b1e2010-08-26 23:41:50 +00001785TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00001786Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
1787 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001788 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001789 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00001790 SourceLocation RAngleLoc) {
Douglas Gregor059101f2011-03-02 00:47:37 +00001791 if (SS.isInvalid())
1792 return true;
1793
Douglas Gregor7532dc62009-03-30 22:58:21 +00001794 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001795
Douglas Gregor40808ce2009-03-09 23:48:35 +00001796 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00001797 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00001798 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001799
Douglas Gregora88f09f2011-02-28 17:23:35 +00001800 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1801 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
1802 DTN->getQualifier(),
1803 DTN->getIdentifier(),
1804 TemplateArgs);
1805
1806 // Build type-source information.
1807 TypeLocBuilder TLB;
1808 DependentTemplateSpecializationTypeLoc SpecTL
1809 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00001810 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00001811 SpecTL.setNameLoc(TemplateLoc);
1812 SpecTL.setLAngleLoc(LAngleLoc);
1813 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00001814 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00001815 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1816 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1817 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1818 }
1819
John McCalld5532b62009-11-23 01:53:49 +00001820 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001821 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00001822
1823 if (Result.isNull())
1824 return true;
1825
Douglas Gregor059101f2011-03-02 00:47:37 +00001826 // Build type-source information.
1827 TypeLocBuilder TLB;
1828 TemplateSpecializationTypeLoc SpecTL
1829 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1830 SpecTL.setTemplateNameLoc(TemplateLoc);
1831 SpecTL.setLAngleLoc(LAngleLoc);
1832 SpecTL.setRAngleLoc(RAngleLoc);
1833 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1834 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00001835
Douglas Gregor059101f2011-03-02 00:47:37 +00001836 if (SS.isNotEmpty()) {
1837 // Create an elaborated-type-specifier containing the nested-name-specifier.
1838 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
1839 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1840 ElabTL.setKeywordLoc(SourceLocation());
1841 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1842 }
1843
1844 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00001845}
John McCallf1bbbb42009-09-04 01:14:41 +00001846
Douglas Gregor059101f2011-03-02 00:47:37 +00001847TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00001848 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00001849 SourceLocation TagLoc,
1850 CXXScopeSpec &SS,
1851 TemplateTy TemplateD,
1852 SourceLocation TemplateLoc,
1853 SourceLocation LAngleLoc,
1854 ASTTemplateArgsPtr TemplateArgsIn,
1855 SourceLocation RAngleLoc) {
1856 TemplateName Template = TemplateD.getAsVal<TemplateName>();
1857
1858 // Translate the parser's template argument list in our AST format.
1859 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
1860 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
1861
1862 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001863 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00001864 ElaboratedTypeKeyword Keyword
1865 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00001866
Douglas Gregor059101f2011-03-02 00:47:37 +00001867 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1868 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
1869 DTN->getQualifier(),
1870 DTN->getIdentifier(),
1871 TemplateArgs);
1872
1873 // Build type-source information.
1874 TypeLocBuilder TLB;
1875 DependentTemplateSpecializationTypeLoc SpecTL
1876 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
1877 SpecTL.setKeywordLoc(TagLoc);
1878 SpecTL.setNameLoc(TemplateLoc);
1879 SpecTL.setLAngleLoc(LAngleLoc);
1880 SpecTL.setRAngleLoc(RAngleLoc);
1881 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
1882 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1883 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1884 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1885 }
1886
1887 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
1888 if (Result.isNull())
1889 return TypeResult();
1890
1891 // Check the tag kind
1892 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00001893 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00001894
John McCall6b2becf2009-09-08 17:47:29 +00001895 IdentifierInfo *Id = D->getIdentifier();
1896 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00001897
John McCall6b2becf2009-09-08 17:47:29 +00001898 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1899 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00001900 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00001901 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00001902 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00001903 }
1904 }
Douglas Gregor059101f2011-03-02 00:47:37 +00001905
1906 // Provide source-location information for the template specialization.
1907 TypeLocBuilder TLB;
1908 TemplateSpecializationTypeLoc SpecTL
1909 = TLB.push<TemplateSpecializationTypeLoc>(Result);
1910 SpecTL.setTemplateNameLoc(TemplateLoc);
1911 SpecTL.setLAngleLoc(LAngleLoc);
1912 SpecTL.setRAngleLoc(RAngleLoc);
1913 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
1914 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00001915
Douglas Gregor059101f2011-03-02 00:47:37 +00001916 // Construct an elaborated type containing the nested-name-specifier (if any)
1917 // and keyword.
1918 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
1919 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
1920 ElabTL.setKeywordLoc(TagLoc);
1921 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
1922 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00001923}
1924
John McCall60d7b3a2010-08-24 06:29:42 +00001925ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Douglas Gregor4c9be892011-02-28 20:01:57 +00001926 LookupResult &R,
1927 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00001928 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001929 // FIXME: Can we do any checking at this point? I guess we could check the
1930 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00001931 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001932 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001933 // foo<int> could identify a single function unambiguously
1934 // This approach does NOT work, since f<int>(1);
1935 // gets resolved prior to resorting to overload resolution
1936 // i.e., template<class T> void f(double);
1937 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00001938
1939 // These should be filtered out by our callers.
1940 assert(!R.empty() && "empty lookup results when building templateid");
1941 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
1942
John McCallc373d482010-01-27 01:50:18 +00001943 // We don't want lookup warnings at this point.
1944 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001945
John McCallf7a1a742009-11-24 19:00:30 +00001946 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001947 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00001948 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00001949 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001950 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001951 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00001952
1953 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001954}
1955
John McCallf7a1a742009-11-24 19:00:30 +00001956// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00001957ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00001958Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00001959 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00001960 const TemplateArgumentListInfo &TemplateArgs) {
1961 DeclContext *DC;
1962 if (!(DC = computeDeclContext(SS, false)) ||
1963 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00001964 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00001965 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001966
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001967 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00001968 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001969 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
1970 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00001971
John McCallf7a1a742009-11-24 19:00:30 +00001972 if (R.isAmbiguous())
1973 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001974
John McCallf7a1a742009-11-24 19:00:30 +00001975 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001976 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
1977 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001978 return ExprError();
1979 }
1980
1981 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001982 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
1983 << (NestedNameSpecifier*) SS.getScopeRep()
1984 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001985 Diag(Temp->getLocation(), diag::note_referenced_class_template);
1986 return ExprError();
1987 }
1988
1989 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001990}
1991
Douglas Gregorc45c2322009-03-31 00:43:58 +00001992/// \brief Form a dependent template name.
1993///
1994/// This action forms a dependent template name given the template
1995/// name and its (presumably dependent) scope specifier. For
1996/// example, given "MetaFun::template apply", the scope specifier \p
1997/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
1998/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001999TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002000 SourceLocation TemplateKWLoc,
2001 CXXScopeSpec &SS,
2002 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002003 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002004 bool EnteringContext,
2005 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00002006 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
2007 !getLangOptions().CPlusPlus0x)
2008 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002009 << FixItHint::CreateRemoval(TemplateKWLoc);
2010
Douglas Gregor0707bc52010-01-19 16:01:07 +00002011 DeclContext *LookupCtx = 0;
2012 if (SS.isSet())
2013 LookupCtx = computeDeclContext(SS, EnteringContext);
2014 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002015 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002016 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002017 // C++0x [temp.names]p5:
2018 // If a name prefixed by the keyword template is not the name of
2019 // a template, the program is ill-formed. [Note: the keyword
2020 // template may not be applied to non-template members of class
2021 // templates. -end note ] [ Note: as is the case with the
2022 // typename prefix, the template prefix is allowed in cases
2023 // where it is not strictly necessary; i.e., when the
2024 // nested-name-specifier or the expression on the left of the ->
2025 // or . is not dependent on a template-parameter, or the use
2026 // does not appear in the scope of a template. -end note]
2027 //
2028 // Note: C++03 was more strict here, because it banned the use of
2029 // the "template" keyword prior to a template-name that was not a
2030 // dependent name. C++ DR468 relaxed this requirement (the
2031 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002032 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002033 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002034 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2035 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002036 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002037 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2038 isa<CXXRecordDecl>(LookupCtx) &&
2039 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002040 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002041 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002042 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002043 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002044 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002045 << Name.getSourceRange()
2046 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002047 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002048 } else {
2049 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002050 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002051 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002052 }
2053
Mike Stump1eb44332009-09-09 15:08:12 +00002054 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002055 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002056
Douglas Gregor014e88d2009-11-03 23:16:33 +00002057 switch (Name.getKind()) {
2058 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002059 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002060 Name.Identifier));
2061 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002062
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002063 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002064 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002065 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002066 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002067
2068 case UnqualifiedId::IK_LiteralOperatorId:
2069 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
2070
Douglas Gregor014e88d2009-11-03 23:16:33 +00002071 default:
2072 break;
2073 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002074
2075 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002076 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002077 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002078 << Name.getSourceRange()
2079 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002080 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002081}
2082
Mike Stump1eb44332009-09-09 15:08:12 +00002083bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002084 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00002085 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002086 const TemplateArgument &Arg = AL.getArgument();
2087
Anders Carlsson436b1562009-06-13 00:33:33 +00002088 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002089 switch(Arg.getKind()) {
2090 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002091 // C++ [temp.arg.type]p1:
2092 // A template-argument for a template-parameter which is a
2093 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002094 break;
2095 case TemplateArgument::Template: {
2096 // We have a template type parameter but the template argument
2097 // is a template without any arguments.
2098 SourceRange SR = AL.getSourceRange();
2099 TemplateName Name = Arg.getAsTemplate();
2100 Diag(SR.getBegin(), diag::err_template_missing_args)
2101 << Name << SR;
2102 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2103 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002104
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002105 return true;
2106 }
2107 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002108 // We have a template type parameter but the template argument
2109 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002110 SourceRange SR = AL.getSourceRange();
2111 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002112 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002113
Anders Carlsson436b1562009-06-13 00:33:33 +00002114 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002115 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002116 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002117
John McCalla93c9342009-12-07 02:54:59 +00002118 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002119 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002120
Anders Carlsson436b1562009-06-13 00:33:33 +00002121 // Add the converted template type argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002122 Converted.push_back(
John McCall833ca992009-10-29 08:12:44 +00002123 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00002124 return false;
2125}
2126
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002127/// \brief Substitute template arguments into the default template argument for
2128/// the given template type parameter.
2129///
2130/// \param SemaRef the semantic analysis object for which we are performing
2131/// the substitution.
2132///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002133/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002134/// for.
2135///
2136/// \param TemplateLoc the location of the template name that started the
2137/// template-id we are checking.
2138///
2139/// \param RAngleLoc the location of the right angle bracket ('>') that
2140/// terminates the template-id.
2141///
2142/// \param Param the template template parameter whose default we are
2143/// substituting into.
2144///
2145/// \param Converted the list of template arguments provided for template
2146/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002147/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002148static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002149SubstDefaultTemplateArgument(Sema &SemaRef,
2150 TemplateDecl *Template,
2151 SourceLocation TemplateLoc,
2152 SourceLocation RAngleLoc,
2153 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002154 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002155 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002156
2157 // If the argument type is dependent, instantiate it now based
2158 // on the previously-computed template arguments.
2159 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002160 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002161 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002162
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002163 MultiLevelTemplateArgumentList AllTemplateArgs
2164 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2165
2166 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002167 Template, Converted.data(),
2168 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002169 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002170
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002171 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2172 Param->getDefaultArgumentLoc(),
2173 Param->getDeclName());
2174 }
2175
2176 return ArgType;
2177}
2178
2179/// \brief Substitute template arguments into the default template argument for
2180/// the given non-type template parameter.
2181///
2182/// \param SemaRef the semantic analysis object for which we are performing
2183/// the substitution.
2184///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002185/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002186/// for.
2187///
2188/// \param TemplateLoc the location of the template name that started the
2189/// template-id we are checking.
2190///
2191/// \param RAngleLoc the location of the right angle bracket ('>') that
2192/// terminates the template-id.
2193///
Douglas Gregor788cd062009-11-11 01:00:40 +00002194/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002195/// substituting into.
2196///
2197/// \param Converted the list of template arguments provided for template
2198/// parameters that precede \p Param in the template parameter list.
2199///
2200/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002201static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002202SubstDefaultTemplateArgument(Sema &SemaRef,
2203 TemplateDecl *Template,
2204 SourceLocation TemplateLoc,
2205 SourceLocation RAngleLoc,
2206 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002207 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002208 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002209 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002210
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002211 MultiLevelTemplateArgumentList AllTemplateArgs
2212 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002213
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002214 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002215 Template, Converted.data(),
2216 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002217 SourceRange(TemplateLoc, RAngleLoc));
2218
2219 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2220}
2221
Douglas Gregor788cd062009-11-11 01:00:40 +00002222/// \brief Substitute template arguments into the default template argument for
2223/// the given template template parameter.
2224///
2225/// \param SemaRef the semantic analysis object for which we are performing
2226/// the substitution.
2227///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002228/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002229/// for.
2230///
2231/// \param TemplateLoc the location of the template name that started the
2232/// template-id we are checking.
2233///
2234/// \param RAngleLoc the location of the right angle bracket ('>') that
2235/// terminates the template-id.
2236///
2237/// \param Param the template template parameter whose default we are
2238/// substituting into.
2239///
2240/// \param Converted the list of template arguments provided for template
2241/// parameters that precede \p Param in the template parameter list.
2242///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002243/// \param QualifierLoc Will be set to the nested-name-specifier (with
2244/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002245///
Douglas Gregor788cd062009-11-11 01:00:40 +00002246/// \returns the substituted template argument, or NULL if an error occurred.
2247static TemplateName
2248SubstDefaultTemplateArgument(Sema &SemaRef,
2249 TemplateDecl *Template,
2250 SourceLocation TemplateLoc,
2251 SourceLocation RAngleLoc,
2252 TemplateTemplateParmDecl *Param,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002253 llvm::SmallVectorImpl<TemplateArgument> &Converted,
2254 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002255 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002256 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002257
Douglas Gregor788cd062009-11-11 01:00:40 +00002258 MultiLevelTemplateArgumentList AllTemplateArgs
2259 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002260
Douglas Gregor788cd062009-11-11 01:00:40 +00002261 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002262 Template, Converted.data(),
2263 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002264 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002265
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002266 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002267 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002268 if (QualifierLoc) {
2269 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2270 AllTemplateArgs);
2271 if (!QualifierLoc)
2272 return TemplateName();
2273 }
2274
Douglas Gregor1d752d72011-03-02 18:46:51 +00002275 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002276 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002277 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002278 AllTemplateArgs);
2279}
2280
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002281/// \brief If the given template parameter has a default template
2282/// argument, substitute into that default template argument and
2283/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002284TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002285Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2286 SourceLocation TemplateLoc,
2287 SourceLocation RAngleLoc,
2288 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002289 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2290 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002291 if (!TypeParm->hasDefaultArgument())
2292 return TemplateArgumentLoc();
2293
John McCalla93c9342009-12-07 02:54:59 +00002294 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002295 TemplateLoc,
2296 RAngleLoc,
2297 TypeParm,
2298 Converted);
2299 if (DI)
2300 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2301
2302 return TemplateArgumentLoc();
2303 }
2304
2305 if (NonTypeTemplateParmDecl *NonTypeParm
2306 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2307 if (!NonTypeParm->hasDefaultArgument())
2308 return TemplateArgumentLoc();
2309
John McCall60d7b3a2010-08-24 06:29:42 +00002310 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002311 TemplateLoc,
2312 RAngleLoc,
2313 NonTypeParm,
2314 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002315 if (Arg.isInvalid())
2316 return TemplateArgumentLoc();
2317
2318 Expr *ArgE = Arg.takeAs<Expr>();
2319 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2320 }
2321
2322 TemplateTemplateParmDecl *TempTempParm
2323 = cast<TemplateTemplateParmDecl>(Param);
2324 if (!TempTempParm->hasDefaultArgument())
2325 return TemplateArgumentLoc();
2326
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002327
Douglas Gregor1d752d72011-03-02 18:46:51 +00002328 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002329 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002330 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002331 RAngleLoc,
2332 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002333 Converted,
2334 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002335 if (TName.isNull())
2336 return TemplateArgumentLoc();
2337
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002338 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002339 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002340 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2341}
2342
Douglas Gregore7526412009-11-11 19:31:23 +00002343/// \brief Check that the given template argument corresponds to the given
2344/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002345///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002346/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002347/// checked.
2348///
2349/// \param Arg The template argument.
2350///
2351/// \param Template The template in which the template argument resides.
2352///
2353/// \param TemplateLoc The location of the template name for the template
2354/// whose argument list we're matching.
2355///
2356/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2357/// the template argument list.
2358///
2359/// \param ArgumentPackIndex The index into the argument pack where this
2360/// argument will be placed. Only valid if the parameter is a parameter pack.
2361///
2362/// \param Converted The checked, converted argument will be added to the
2363/// end of this small vector.
2364///
2365/// \param CTAK Describes how we arrived at this particular template argument:
2366/// explicitly written, deduced, etc.
2367///
2368/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002369bool Sema::CheckTemplateArgument(NamedDecl *Param,
2370 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002371 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002372 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002373 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002374 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002375 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002376 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002377 // Check template type parameters.
2378 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002379 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002380
Douglas Gregord9e15302009-11-11 19:41:09 +00002381 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002382 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002383 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002384 // with the template arguments we've seen thus far. But if the
2385 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002386 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002387 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2388 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002389
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002390 if (NTTPType->isDependentType() &&
2391 !isa<TemplateTemplateParmDecl>(Template) &&
2392 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002393 // Do substitution on the type of the non-type template parameter.
2394 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002395 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002396 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002397
2398 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002399 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002400 NTTPType = SubstType(NTTPType,
2401 MultiLevelTemplateArgumentList(TemplateArgs),
2402 NTTP->getLocation(),
2403 NTTP->getDeclName());
2404 // If that worked, check the non-type template parameter type
2405 // for validity.
2406 if (!NTTPType.isNull())
2407 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2408 NTTP->getLocation());
2409 if (NTTPType.isNull())
2410 return true;
2411 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002412
Douglas Gregore7526412009-11-11 19:31:23 +00002413 switch (Arg.getArgument().getKind()) {
2414 case TemplateArgument::Null:
2415 assert(false && "Should never see a NULL template argument here");
2416 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002417
Douglas Gregore7526412009-11-11 19:31:23 +00002418 case TemplateArgument::Expression: {
2419 Expr *E = Arg.getArgument().getAsExpr();
2420 TemplateArgument Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002421 if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK))
Douglas Gregore7526412009-11-11 19:31:23 +00002422 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002423
Douglas Gregor910f8002010-11-07 23:05:16 +00002424 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002425 break;
2426 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002427
Douglas Gregore7526412009-11-11 19:31:23 +00002428 case TemplateArgument::Declaration:
2429 case TemplateArgument::Integral:
2430 // We've already checked this template argument, so just copy
2431 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002432 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002433 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002434
Douglas Gregore7526412009-11-11 19:31:23 +00002435 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002436 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002437 // We were given a template template argument. It may not be ill-formed;
2438 // see below.
2439 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002440 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2441 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002442 // We have a template argument such as \c T::template X, which we
2443 // parsed as a template template argument. However, since we now
2444 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002445 // template name into an expression.
2446
2447 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2448 Arg.getTemplateNameLoc());
2449
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002450 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002451 SS.Adopt(Arg.getTemplateQualifierLoc());
John McCallf7a1a742009-11-24 19:00:30 +00002452 Expr *E = DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002453 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002454 NameInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002455
Douglas Gregora7fc9012011-01-05 18:58:31 +00002456 // If we parsed the template argument as a pack expansion, create a
2457 // pack expansion expression.
2458 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002459 ExprResult Expansion = ActOnPackExpansion(E,
Douglas Gregora7fc9012011-01-05 18:58:31 +00002460 Arg.getTemplateEllipsisLoc());
2461 if (Expansion.isInvalid())
2462 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002463
Douglas Gregora7fc9012011-01-05 18:58:31 +00002464 E = Expansion.get();
2465 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002466
Douglas Gregore7526412009-11-11 19:31:23 +00002467 TemplateArgument Result;
2468 if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
2469 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002470
Douglas Gregor910f8002010-11-07 23:05:16 +00002471 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002472 break;
2473 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002474
Douglas Gregore7526412009-11-11 19:31:23 +00002475 // We have a template argument that actually does refer to a class
2476 // template, template alias, or template template parameter, and
2477 // therefore cannot be a non-type template argument.
2478 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2479 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002480
Douglas Gregore7526412009-11-11 19:31:23 +00002481 Diag(Param->getLocation(), diag::note_template_param_here);
2482 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002483
Douglas Gregore7526412009-11-11 19:31:23 +00002484 case TemplateArgument::Type: {
2485 // We have a non-type template parameter but the template
2486 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002487
Douglas Gregore7526412009-11-11 19:31:23 +00002488 // C++ [temp.arg]p2:
2489 // In a template-argument, an ambiguity between a type-id and
2490 // an expression is resolved to a type-id, regardless of the
2491 // form of the corresponding template-parameter.
2492 //
2493 // We warn specifically about this case, since it can be rather
2494 // confusing for users.
2495 QualType T = Arg.getArgument().getAsType();
2496 SourceRange SR = Arg.getSourceRange();
2497 if (T->isFunctionType())
2498 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2499 else
2500 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2501 Diag(Param->getLocation(), diag::note_template_param_here);
2502 return true;
2503 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002504
Douglas Gregore7526412009-11-11 19:31:23 +00002505 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002506 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002507 break;
2508 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002509
Douglas Gregore7526412009-11-11 19:31:23 +00002510 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002511 }
2512
2513
Douglas Gregore7526412009-11-11 19:31:23 +00002514 // Check template template parameters.
2515 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002516
Douglas Gregore7526412009-11-11 19:31:23 +00002517 // Substitute into the template parameter list of the template
2518 // template parameter, since previously-supplied template arguments
2519 // may appear within the template template parameter.
2520 {
2521 // Set up a template instantiation context.
2522 LocalInstantiationScope Scope(*this);
2523 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002524 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002525 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002526
2527 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002528 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002529 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002530 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002531 MultiLevelTemplateArgumentList(TemplateArgs)));
2532 if (!TempParm)
2533 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002534 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002535
Douglas Gregore7526412009-11-11 19:31:23 +00002536 switch (Arg.getArgument().getKind()) {
2537 case TemplateArgument::Null:
2538 assert(false && "Should never see a NULL template argument here");
2539 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002540
Douglas Gregore7526412009-11-11 19:31:23 +00002541 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002542 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002543 if (CheckTemplateArgument(TempParm, Arg))
2544 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002545
Douglas Gregor910f8002010-11-07 23:05:16 +00002546 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002547 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002548
Douglas Gregore7526412009-11-11 19:31:23 +00002549 case TemplateArgument::Expression:
2550 case TemplateArgument::Type:
2551 // We have a template template parameter but the template
2552 // argument does not refer to a template.
2553 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
2554 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002555
Douglas Gregore7526412009-11-11 19:31:23 +00002556 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002557 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002558 "Declaration argument with template template parameter");
2559 break;
2560 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002561 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002562 "Integral argument with template template parameter");
2563 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002564
Douglas Gregore7526412009-11-11 19:31:23 +00002565 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002566 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002567 break;
2568 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002569
Douglas Gregore7526412009-11-11 19:31:23 +00002570 return false;
2571}
2572
Douglas Gregorc15cb382009-02-09 23:23:08 +00002573/// \brief Check that the given template argument list is well-formed
2574/// for specializing the given template.
2575bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2576 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002577 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002578 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002579 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002580 TemplateParameterList *Params = Template->getTemplateParameters();
2581 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002582 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002583 bool Invalid = false;
2584
John McCalld5532b62009-11-23 01:53:49 +00002585 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2586
Mike Stump1eb44332009-09-09 15:08:12 +00002587 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002588 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002589
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002590 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002591 (NumArgs < Params->getMinRequiredArguments() &&
2592 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002593 // FIXME: point at either the first arg beyond what we can handle,
2594 // or the '>', depending on whether we have too many or too few
2595 // arguments.
2596 SourceRange Range;
2597 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002598 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002599 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2600 << (NumArgs > NumParams)
2601 << (isa<ClassTemplateDecl>(Template)? 0 :
2602 isa<FunctionTemplateDecl>(Template)? 1 :
2603 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2604 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002605 Diag(Template->getLocation(), diag::note_template_decl_here)
2606 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002607 Invalid = true;
2608 }
Mike Stump1eb44332009-09-09 15:08:12 +00002609
2610 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002611 // [...] The type and form of each template-argument specified in
2612 // a template-id shall match the type and form specified for the
2613 // corresponding parameter declared by the template in its
2614 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002615 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002616 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2617 TemplateParameterList::iterator Param = Params->begin(),
2618 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002619 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002620 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002621 while (Param != ParamEnd) {
Douglas Gregor16134c62009-07-01 00:28:38 +00002622 if (ArgIdx > NumArgs && PartialTemplateArgs)
2623 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002624
Douglas Gregorf35f8282009-11-11 21:54:23 +00002625 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002626 // If we have an expanded parameter pack, make sure we don't have too
2627 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002628 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002629 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002630 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002631 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2632 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2633 << true
2634 << (isa<ClassTemplateDecl>(Template)? 0 :
2635 isa<FunctionTemplateDecl>(Template)? 1 :
2636 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2637 << Template;
2638 Diag(Template->getLocation(), diag::note_template_decl_here)
2639 << Params->getSourceRange();
2640 return true;
2641 }
2642 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002643
Douglas Gregorf35f8282009-11-11 21:54:23 +00002644 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002645 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2646 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002647 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002648 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002649
Douglas Gregor14be16b2010-12-20 16:57:52 +00002650 if ((*Param)->isTemplateParameterPack()) {
2651 // The template parameter was a template parameter pack, so take the
2652 // deduced argument and place it on the argument pack. Note that we
2653 // stay on the same template parameter so that we can deduce more
2654 // arguments.
2655 ArgumentPack.push_back(Converted.back());
2656 Converted.pop_back();
2657 } else {
2658 // Move to the next template parameter.
2659 ++Param;
2660 }
2661 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002662 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002663 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002664
2665 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002666 // arguments, just break out now and we'll fill in the argument pack below.
2667 if ((*Param)->isTemplateParameterPack())
2668 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002669
Douglas Gregorf35f8282009-11-11 21:54:23 +00002670 // We have a default template argument that we will use.
2671 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002672
Douglas Gregorf35f8282009-11-11 21:54:23 +00002673 // Retrieve the default template argument from the template
2674 // parameter. For each kind of template parameter, we substitute the
2675 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002676 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002677 // the default argument.
2678 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2679 if (!TTP->hasDefaultArgument()) {
2680 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2681 break;
2682 }
2683
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002684 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002685 Template,
2686 TemplateLoc,
2687 RAngleLoc,
2688 TTP,
2689 Converted);
2690 if (!ArgType)
2691 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002692
Douglas Gregorf35f8282009-11-11 21:54:23 +00002693 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2694 ArgType);
2695 } else if (NonTypeTemplateParmDecl *NTTP
2696 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2697 if (!NTTP->hasDefaultArgument()) {
2698 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2699 break;
2700 }
2701
John McCall60d7b3a2010-08-24 06:29:42 +00002702 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002703 TemplateLoc,
2704 RAngleLoc,
2705 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002706 Converted);
2707 if (E.isInvalid())
2708 return true;
2709
2710 Expr *Ex = E.takeAs<Expr>();
2711 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2712 } else {
2713 TemplateTemplateParmDecl *TempParm
2714 = cast<TemplateTemplateParmDecl>(*Param);
2715
2716 if (!TempParm->hasDefaultArgument()) {
2717 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2718 break;
2719 }
2720
Douglas Gregor1d752d72011-03-02 18:46:51 +00002721 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002722 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002723 TemplateLoc,
2724 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002725 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002726 Converted,
2727 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00002728 if (Name.isNull())
2729 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002730
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002731 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
2732 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00002733 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002734
Douglas Gregorf35f8282009-11-11 21:54:23 +00002735 // Introduce an instantiation record that describes where we are using
2736 // the default template argument.
2737 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002738 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002739 SourceRange(TemplateLoc, RAngleLoc));
2740
Douglas Gregorf35f8282009-11-11 21:54:23 +00002741 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002742 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002743 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00002744 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002745
Douglas Gregor67714232011-03-03 02:41:12 +00002746 // Core issue 150 (assumed resolution): if this is a template template
2747 // parameter, keep track of the default template arguments from the
2748 // template definition.
2749 if (isTemplateTemplateParameter)
2750 TemplateArgs.addArgument(Arg);
2751
Douglas Gregor14be16b2010-12-20 16:57:52 +00002752 // Move to the next template parameter and argument.
2753 ++Param;
2754 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002755 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002756
Douglas Gregor14be16b2010-12-20 16:57:52 +00002757 // Form argument packs for each of the parameter packs remaining.
2758 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00002759 // If we're checking a partial list of template arguments, don't fill
2760 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002761
2762 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002763 if (PartialTemplateArgs && ArgumentPack.empty()) {
2764 Converted.push_back(TemplateArgument());
Douglas Gregor203e6a32011-01-11 23:09:57 +00002765 } else if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00002766 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00002767 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002768 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2769 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00002770 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00002771 ArgumentPack.clear();
2772 }
2773 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002774
Douglas Gregor14be16b2010-12-20 16:57:52 +00002775 ++Param;
2776 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002777
Douglas Gregorc15cb382009-02-09 23:23:08 +00002778 return Invalid;
2779}
2780
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002781namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002782 class UnnamedLocalNoLinkageFinder
2783 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002784 {
2785 Sema &S;
2786 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002787
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002788 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002789
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002790 public:
2791 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
2792
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002793 bool Visit(QualType T) {
2794 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002795 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002796
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002797#define TYPE(Class, Parent) \
2798 bool Visit##Class##Type(const Class##Type *);
2799#define ABSTRACT_TYPE(Class, Parent) \
2800 bool Visit##Class##Type(const Class##Type *) { return false; }
2801#define NON_CANONICAL_TYPE(Class, Parent) \
2802 bool Visit##Class##Type(const Class##Type *) { return false; }
2803#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002804
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002805 bool VisitTagDecl(const TagDecl *Tag);
2806 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
2807 };
2808}
2809
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002810bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002811 return false;
2812}
2813
2814bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
2815 return Visit(T->getElementType());
2816}
2817
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002818bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002819 return Visit(T->getPointeeType());
2820}
2821
2822bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002823 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002824 return Visit(T->getPointeeType());
2825}
2826
2827bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002828 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002829 return Visit(T->getPointeeType());
2830}
2831
2832bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002833 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002834 return Visit(T->getPointeeType());
2835}
2836
2837bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002838 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002839 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
2840}
2841
2842bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002843 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002844 return Visit(T->getElementType());
2845}
2846
2847bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002848 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002849 return Visit(T->getElementType());
2850}
2851
2852bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002853 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002854 return Visit(T->getElementType());
2855}
2856
2857bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002858 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002859 return Visit(T->getElementType());
2860}
2861
2862bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002863 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002864 return Visit(T->getElementType());
2865}
2866
2867bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
2868 return Visit(T->getElementType());
2869}
2870
2871bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
2872 return Visit(T->getElementType());
2873}
2874
2875bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
2876 const FunctionProtoType* T) {
2877 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002878 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002879 A != AEnd; ++A) {
2880 if (Visit(*A))
2881 return true;
2882 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002883
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002884 return Visit(T->getResultType());
2885}
2886
2887bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
2888 const FunctionNoProtoType* T) {
2889 return Visit(T->getResultType());
2890}
2891
2892bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
2893 const UnresolvedUsingType*) {
2894 return false;
2895}
2896
2897bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
2898 return false;
2899}
2900
2901bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
2902 return Visit(T->getUnderlyingType());
2903}
2904
2905bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
2906 return false;
2907}
2908
Richard Smith34b41d92011-02-20 03:19:35 +00002909bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
2910 return Visit(T->getDeducedType());
2911}
2912
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002913bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
2914 return VisitTagDecl(T->getDecl());
2915}
2916
2917bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
2918 return VisitTagDecl(T->getDecl());
2919}
2920
2921bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
2922 const TemplateTypeParmType*) {
2923 return false;
2924}
2925
Douglas Gregorc3069d62011-01-14 02:55:32 +00002926bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
2927 const SubstTemplateTypeParmPackType *) {
2928 return false;
2929}
2930
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002931bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
2932 const TemplateSpecializationType*) {
2933 return false;
2934}
2935
2936bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
2937 const InjectedClassNameType* T) {
2938 return VisitTagDecl(T->getDecl());
2939}
2940
2941bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
2942 const DependentNameType* T) {
2943 return VisitNestedNameSpecifier(T->getQualifier());
2944}
2945
2946bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
2947 const DependentTemplateSpecializationType* T) {
2948 return VisitNestedNameSpecifier(T->getQualifier());
2949}
2950
Douglas Gregor7536dd52010-12-20 02:24:11 +00002951bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
2952 const PackExpansionType* T) {
2953 return Visit(T->getPattern());
2954}
2955
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002956bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
2957 return false;
2958}
2959
2960bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
2961 const ObjCInterfaceType *) {
2962 return false;
2963}
2964
2965bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
2966 const ObjCObjectPointerType *) {
2967 return false;
2968}
2969
2970bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
2971 if (Tag->getDeclContext()->isFunctionOrMethod()) {
2972 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
2973 << S.Context.getTypeDeclType(Tag) << SR;
2974 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002975 }
2976
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002977 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) {
2978 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
2979 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
2980 return true;
2981 }
2982
2983 return false;
2984}
2985
2986bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
2987 NestedNameSpecifier *NNS) {
2988 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
2989 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002990
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002991 switch (NNS->getKind()) {
2992 case NestedNameSpecifier::Identifier:
2993 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00002994 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002995 case NestedNameSpecifier::Global:
2996 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002997
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002998 case NestedNameSpecifier::TypeSpec:
2999 case NestedNameSpecifier::TypeSpecWithTemplate:
3000 return Visit(QualType(NNS->getAsType(), 0));
3001 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00003002 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003003}
3004
3005
Douglas Gregorc15cb382009-02-09 23:23:08 +00003006/// \brief Check a template argument against its corresponding
3007/// template type parameter.
3008///
3009/// This routine implements the semantics of C++ [temp.arg.type]. It
3010/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003011bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003012 TypeSourceInfo *ArgInfo) {
3013 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003014 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003015 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003016
3017 if (Arg->isVariablyModifiedType()) {
3018 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003019 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003020 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003021 }
3022
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003023 // C++03 [temp.arg.type]p2:
3024 // A local type, a type with no linkage, an unnamed type or a type
3025 // compounded from any of these types shall not be used as a
3026 // template-argument for a template type-parameter.
3027 //
3028 // C++0x allows these, and even in C++03 we allow them as an extension with
3029 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00003030 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003031 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3032 (void)Finder.Visit(Context.getCanonicalType(Arg));
3033 }
3034
Douglas Gregorc15cb382009-02-09 23:23:08 +00003035 return false;
3036}
3037
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003038/// \brief Checks whether the given template argument is the address
3039/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003040static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003041CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3042 NonTypeTemplateParmDecl *Param,
3043 QualType ParamType,
3044 Expr *ArgIn,
3045 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003046 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003047 Expr *Arg = ArgIn;
3048 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003049
3050 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003051 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003052 Arg = Cast->getSubExpr();
3053
3054 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003055 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003056 // A template-argument for a non-type, non-template
3057 // template-parameter shall be one of: [...]
3058 //
3059 // -- the address of an object or function with external
3060 // linkage, including function templates and function
3061 // template-ids but excluding non-static class members,
3062 // expressed as & id-expression where the & is optional if
3063 // the name refers to a function or array, or if the
3064 // corresponding template-parameter is a reference; or
3065 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003066
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003067 // In C++98/03 mode, give an extension warning on any extra parentheses.
3068 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3069 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003070 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003071 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003072 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003073 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003074 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003075 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003076 }
3077
3078 Arg = Parens->getSubExpr();
3079 }
3080
Douglas Gregorb7a09262010-04-01 18:32:35 +00003081 bool AddressTaken = false;
3082 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003083 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003084 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003085 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00003086 AddressTaken = true;
3087 AddrOpLoc = UnOp->getOperatorLoc();
3088 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003089 } else
3090 DRE = dyn_cast<DeclRefExpr>(Arg);
3091
Douglas Gregorb7a09262010-04-01 18:32:35 +00003092 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003093 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3094 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003095 S.Diag(Param->getLocation(), diag::note_template_param_here);
3096 return true;
3097 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003098
3099 // Stop checking the precise nature of the argument if it is value dependent,
3100 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003101 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003102 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003103 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003104 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003105
Douglas Gregorb7a09262010-04-01 18:32:35 +00003106 if (!isa<ValueDecl>(DRE->getDecl())) {
3107 S.Diag(Arg->getSourceRange().getBegin(),
3108 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003109 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003110 S.Diag(Param->getLocation(), diag::note_template_param_here);
3111 return true;
3112 }
3113
3114 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003115
3116 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003117 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3118 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003119 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003120 S.Diag(Param->getLocation(), diag::note_template_param_here);
3121 return true;
3122 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003123
3124 // Cannot refer to non-static member functions
3125 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003126 if (!Method->isStatic()) {
3127 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003128 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003129 S.Diag(Param->getLocation(), diag::note_template_param_here);
3130 return true;
3131 }
Mike Stump1eb44332009-09-09 15:08:12 +00003132
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003133 // Functions must have external linkage.
3134 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003135 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003136 S.Diag(Arg->getSourceRange().getBegin(),
3137 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003138 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003139 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003140 << true;
3141 return true;
3142 }
3143
3144 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003145 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003146
Douglas Gregorb7a09262010-04-01 18:32:35 +00003147 // If the template parameter has pointer type, the function decays.
3148 if (ParamType->isPointerType() && !AddressTaken)
3149 ArgType = S.Context.getPointerType(Func->getType());
3150 else if (AddressTaken && ParamType->isReferenceType()) {
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(Func->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 = Func->getType();
3168 }
3169 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003170 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003171 S.Diag(Arg->getSourceRange().getBegin(),
3172 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003173 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003174 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003175 << true;
3176 return true;
3177 }
3178
Douglas Gregorb7a09262010-04-01 18:32:35 +00003179 // A value of reference type is not an object.
3180 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003181 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003182 diag::err_template_arg_reference_var)
3183 << Var->getType() << Arg->getSourceRange();
3184 S.Diag(Param->getLocation(), diag::note_template_param_here);
3185 return true;
3186 }
3187
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003188 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003189 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003190
3191 // If the template parameter has pointer type, we must have taken
3192 // the address of this object.
3193 if (ParamType->isReferenceType()) {
3194 if (AddressTaken) {
3195 // If we originally had an address-of operator, but the
3196 // parameter has reference type, complain and (if things look
3197 // like they will work) drop the address-of operator.
3198 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3199 ParamType.getNonReferenceType())) {
3200 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3201 << ParamType;
3202 S.Diag(Param->getLocation(), diag::note_template_param_here);
3203 return true;
3204 }
3205
3206 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3207 << ParamType
3208 << FixItHint::CreateRemoval(AddrOpLoc);
3209 S.Diag(Param->getLocation(), diag::note_template_param_here);
3210
3211 ArgType = Var->getType();
3212 }
3213 } else if (!AddressTaken && ParamType->isPointerType()) {
3214 if (Var->getType()->isArrayType()) {
3215 // Array-to-pointer decay.
3216 ArgType = S.Context.getArrayDecayedType(Var->getType());
3217 } else {
3218 // If the template parameter has pointer type but the address of
3219 // this object was not taken, complain and (possibly) recover by
3220 // taking the address of the entity.
3221 ArgType = S.Context.getPointerType(Var->getType());
3222 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3223 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3224 << ParamType;
3225 S.Diag(Param->getLocation(), diag::note_template_param_here);
3226 return true;
3227 }
3228
3229 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3230 << ParamType
3231 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3232
3233 S.Diag(Param->getLocation(), diag::note_template_param_here);
3234 }
3235 }
3236 } else {
3237 // We found something else, but we don't know specifically what it is.
3238 S.Diag(Arg->getSourceRange().getBegin(),
3239 diag::err_template_arg_not_object_or_func)
3240 << Arg->getSourceRange();
3241 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3242 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003243 }
Mike Stump1eb44332009-09-09 15:08:12 +00003244
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003245 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003246 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003247 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003248 // For pointer-to-object types, qualification conversions are
3249 // permitted.
3250 } else {
3251 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3252 if (!ParamRef->getPointeeType()->isFunctionType()) {
3253 // C++ [temp.arg.nontype]p5b3:
3254 // For a non-type template-parameter of type reference to
3255 // object, no conversions apply. The type referred to by the
3256 // reference may be more cv-qualified than the (otherwise
3257 // identical) type of the template- argument. The
3258 // template-parameter is bound directly to the
3259 // template-argument, which shall be an lvalue.
3260
3261 // FIXME: Other qualifiers?
3262 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3263 unsigned ArgQuals = ArgType.getCVRQualifiers();
3264
3265 if ((ParamQuals | ArgQuals) != ParamQuals) {
3266 S.Diag(Arg->getSourceRange().getBegin(),
3267 diag::err_template_arg_ref_bind_ignores_quals)
3268 << ParamType << Arg->getType()
3269 << Arg->getSourceRange();
3270 S.Diag(Param->getLocation(), diag::note_template_param_here);
3271 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003272 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003273 }
3274 }
3275
3276 // At this point, the template argument refers to an object or
3277 // function with external linkage. We now need to check whether the
3278 // argument and parameter types are compatible.
3279 if (!S.Context.hasSameUnqualifiedType(ArgType,
3280 ParamType.getNonReferenceType())) {
3281 // We can't perform this conversion or binding.
3282 if (ParamType->isReferenceType())
3283 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3284 << ParamType << Arg->getType() << Arg->getSourceRange();
3285 else
3286 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3287 << Arg->getType() << ParamType << Arg->getSourceRange();
3288 S.Diag(Param->getLocation(), diag::note_template_param_here);
3289 return true;
3290 }
3291 }
3292
3293 // Create the template argument.
3294 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003295 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003296 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003297}
3298
3299/// \brief Checks whether the given template argument is a pointer to
3300/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003301bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003302 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003303 bool Invalid = false;
3304
3305 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003306 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003307 Arg = Cast->getSubExpr();
3308
3309 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003310 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003311 // A template-argument for a non-type, non-template
3312 // template-parameter shall be one of: [...]
3313 //
3314 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003315 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003316
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003317 // In C++98/03 mode, give an extension warning on any extra parentheses.
3318 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3319 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003320 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003321 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003322 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003323 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003324 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003325 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003326 }
3327
3328 Arg = Parens->getSubExpr();
3329 }
3330
Douglas Gregorcaddba02009-11-12 18:38:13 +00003331 // A pointer-to-member constant written &Class::member.
3332 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003333 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003334 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3335 if (DRE && !DRE->getQualifier())
3336 DRE = 0;
3337 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003338 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003339 // A constant of pointer-to-member type.
3340 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3341 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3342 if (VD->getType()->isMemberPointerType()) {
3343 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003344 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003345 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3346 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003347 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003348 else
3349 Converted = TemplateArgument(VD->getCanonicalDecl());
3350 return Invalid;
3351 }
3352 }
3353 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003354
Douglas Gregorcaddba02009-11-12 18:38:13 +00003355 DRE = 0;
3356 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003357
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003358 if (!DRE)
3359 return Diag(Arg->getSourceRange().getBegin(),
3360 diag::err_template_arg_not_pointer_to_member_form)
3361 << Arg->getSourceRange();
3362
3363 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3364 assert((isa<FieldDecl>(DRE->getDecl()) ||
3365 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3366 "Only non-static member pointers can make it here");
3367
3368 // Okay: this is the address of a non-static member, and therefore
3369 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003370 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003371 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003372 else
3373 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003374 return Invalid;
3375 }
3376
3377 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003378 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003379 diag::err_template_arg_not_pointer_to_member_form)
3380 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003381 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003382 diag::note_template_arg_refers_here);
3383 return true;
3384}
3385
Douglas Gregorc15cb382009-02-09 23:23:08 +00003386/// \brief Check a template argument against its corresponding
3387/// non-type template parameter.
3388///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003389/// This routine implements the semantics of C++ [temp.arg.nontype].
3390/// It returns true if an error occurred, and false otherwise. \p
3391/// InstantiatedParamType is the type of the non-type template
3392/// parameter after it has been instantiated.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003393///
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003394/// If no error was detected, Converted receives the converted template argument.
Douglas Gregorc15cb382009-02-09 23:23:08 +00003395bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
Mike Stump1eb44332009-09-09 15:08:12 +00003396 QualType InstantiatedParamType, Expr *&Arg,
Douglas Gregor02024a92010-03-28 02:42:43 +00003397 TemplateArgument &Converted,
3398 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003399 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3400
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003401 // If either the parameter has a dependent type or the argument is
3402 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003403 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3404 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003405 Converted = TemplateArgument(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003406 return false;
Douglas Gregor40808ce2009-03-09 23:48:35 +00003407 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003408
3409 // C++ [temp.arg.nontype]p5:
3410 // The following conversions are performed on each expression used
3411 // as a non-type template-argument. If a non-type
3412 // template-argument cannot be converted to the type of the
3413 // corresponding template-parameter then the program is
3414 // ill-formed.
3415 //
3416 // -- for a non-type template-parameter of integral or
3417 // enumeration type, integral promotions (4.5) and integral
3418 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003419 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003420 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003421 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003422 // C++ [temp.arg.nontype]p1:
3423 // A template-argument for a non-type, non-template
3424 // template-parameter shall be one of:
3425 //
3426 // -- an integral constant-expression of integral or enumeration
3427 // type; or
3428 // -- the name of a non-type template-parameter; or
3429 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003430 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003431 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003432 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003433 diag::err_template_arg_not_integral_or_enumeral)
3434 << ArgType << Arg->getSourceRange();
3435 Diag(Param->getLocation(), diag::note_template_param_here);
3436 return true;
3437 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003438 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003439 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3440 << ArgType << Arg->getSourceRange();
3441 return true;
3442 }
3443
Douglas Gregor02024a92010-03-28 02:42:43 +00003444 // From here on out, all we care about are the unqualified forms
3445 // of the parameter and argument types.
3446 ParamType = ParamType.getUnqualifiedType();
3447 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003448
3449 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003450 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003451 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003452 } else if (CTAK == CTAK_Deduced) {
3453 // C++ [temp.deduct.type]p17:
3454 // If, in the declaration of a function template with a non-type
3455 // template-parameter, the non-type template- parameter is used
3456 // in an expression in the function parameter-list and, if the
3457 // corresponding template-argument is deduced, the
3458 // template-argument type shall match the type of the
3459 // template-parameter exactly, except that a template-argument
3460 // deduced from an array bound may be of any integral type.
3461 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3462 << ArgType << ParamType;
3463 Diag(Param->getLocation(), diag::note_template_param_here);
John McCalldaa8e4e2010-11-15 09:13:47 +00003464 return true;
3465 } else if (ParamType->isBooleanType()) {
3466 // This is an integral-to-boolean conversion.
3467 ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003468 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3469 !ParamType->isEnumeralType()) {
3470 // This is an integral promotion or conversion.
John McCall2de56d12010-08-25 11:45:40 +00003471 ImpCastExprToType(Arg, ParamType, CK_IntegralCast);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003472 } else {
3473 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003474 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003475 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003476 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003477 Diag(Param->getLocation(), diag::note_template_param_here);
3478 return true;
3479 }
3480
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003481 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003482 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003483 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003484
3485 if (!Arg->isValueDependent()) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003486 llvm::APSInt OldValue = Value;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003487
3488 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003489 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003490 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003491 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003492 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003493 Value.setIsSigned(IntegerType->isSignedIntegerType());
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003494
3495 // Complain if an unsigned parameter received a negative value.
3496 if (IntegerType->isUnsignedIntegerType()
3497 && (OldValue.isSigned() && OldValue.isNegative())) {
3498 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3499 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3500 << Arg->getSourceRange();
3501 Diag(Param->getLocation(), diag::note_template_param_here);
3502 }
3503
3504 // Complain if we overflowed the template parameter's type.
3505 unsigned RequiredBits;
3506 if (IntegerType->isUnsignedIntegerType())
3507 RequiredBits = OldValue.getActiveBits();
3508 else if (OldValue.isUnsigned())
3509 RequiredBits = OldValue.getActiveBits() + 1;
3510 else
3511 RequiredBits = OldValue.getMinSignedBits();
3512 if (RequiredBits > AllowedBits) {
3513 Diag(Arg->getSourceRange().getBegin(),
3514 diag::warn_template_arg_too_large)
3515 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3516 << Arg->getSourceRange();
3517 Diag(Param->getLocation(), diag::note_template_param_here);
3518 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003519 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003520
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003521 // Add the value of this argument to the list of converted
3522 // arguments. We use the bitwidth and signedness of the template
3523 // parameter.
3524 if (Arg->isValueDependent()) {
3525 // The argument is value-dependent. Create a new
3526 // TemplateArgument with the converted expression.
3527 Converted = TemplateArgument(Arg);
3528 return false;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003529 }
3530
John McCall833ca992009-10-29 08:12:44 +00003531 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003532 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003533 : IntegerType);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003534 return false;
3535 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003536
John McCall6bb80172010-03-30 21:47:33 +00003537 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3538
Douglas Gregorb7a09262010-04-01 18:32:35 +00003539 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3540 // from a template argument of type std::nullptr_t to a non-type
3541 // template parameter of type pointer to object, pointer to
3542 // function, or pointer-to-member, respectively.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003543 if (ArgType->isNullPtrType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003544 (ParamType->isPointerType() || ParamType->isMemberPointerType())) {
3545 Converted = TemplateArgument((NamedDecl *)0);
3546 return false;
3547 }
3548
Douglas Gregorb86b0572009-02-11 01:18:59 +00003549 // Handle pointer-to-function, reference-to-function, and
3550 // pointer-to-member-function all in (roughly) the same way.
3551 if (// -- For a non-type template-parameter of type pointer to
3552 // function, only the function-to-pointer conversion (4.3) is
3553 // applied. If the template-argument represents a set of
3554 // overloaded functions (or a pointer to such), the matching
3555 // function is selected from the set (13.4).
3556 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003557 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003558 // -- For a non-type template-parameter of type reference to
3559 // function, no conversions apply. If the template-argument
3560 // represents a set of overloaded functions, the matching
3561 // function is selected from the set (13.4).
3562 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003563 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003564 // -- For a non-type template-parameter of type pointer to
3565 // member function, no conversions apply. If the
3566 // template-argument represents a set of overloaded member
3567 // functions, the matching member function is selected from
3568 // the set (13.4).
3569 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003570 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003571 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003572
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003573 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003574 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003575 true,
3576 FoundResult)) {
3577 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3578 return true;
3579
3580 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3581 ArgType = Arg->getType();
3582 } else
Douglas Gregor48f3bb92009-02-18 21:56:37 +00003583 return true;
Douglas Gregora35284b2009-02-11 00:19:33 +00003584 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003585
Douglas Gregorb7a09262010-04-01 18:32:35 +00003586 if (!ParamType->isMemberPointerType())
3587 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003588 ParamType,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003589 Arg, Converted);
3590
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003591 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3592 false)) {
John McCall2de56d12010-08-25 11:45:40 +00003593 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregorb7a09262010-04-01 18:32:35 +00003594 } else if (!Context.hasSameUnqualifiedType(ArgType,
3595 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003596 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003597 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003598 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003599 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003600 Diag(Param->getLocation(), diag::note_template_param_here);
3601 return true;
3602 }
Mike Stump1eb44332009-09-09 15:08:12 +00003603
Douglas Gregorb7a09262010-04-01 18:32:35 +00003604 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregora35284b2009-02-11 00:19:33 +00003605 }
3606
Chris Lattnerfe90de72009-02-20 21:37:53 +00003607 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003608 // -- for a non-type template-parameter of type pointer to
3609 // object, qualification conversions (4.4) and the
3610 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003611 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003612 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003613 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003614
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003615 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003616 ParamType,
3617 Arg, Converted);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003618 }
Mike Stump1eb44332009-09-09 15:08:12 +00003619
Ted Kremenek6217b802009-07-29 21:53:49 +00003620 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003621 // -- For a non-type template-parameter of type reference to
3622 // object, no conversions apply. The type referred to by the
3623 // reference may be more cv-qualified than the (otherwise
3624 // identical) type of the template-argument. The
3625 // template-parameter is bound directly to the
3626 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003627 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003628 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003629
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003630 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003631 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3632 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003633 true,
3634 FoundResult)) {
3635 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3636 return true;
3637
3638 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3639 ArgType = Arg->getType();
3640 } else
Douglas Gregorb7a09262010-04-01 18:32:35 +00003641 return true;
Douglas Gregorb86b0572009-02-11 01:18:59 +00003642 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003643
3644 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003645 ParamType,
3646 Arg, Converted);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003647 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003648
3649 // -- For a non-type template-parameter of type pointer to data
3650 // member, qualification conversions (4.4) are applied.
3651 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3652
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003653 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003654 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003655 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John McCall2de56d12010-08-25 11:45:40 +00003656 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregor658bbb52009-02-11 16:16:59 +00003657 } else {
3658 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003659 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003660 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003661 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003662 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00003663 return true;
Douglas Gregor658bbb52009-02-11 16:16:59 +00003664 }
3665
Douglas Gregorcaddba02009-11-12 18:38:13 +00003666 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003667}
3668
3669/// \brief Check a template argument against its corresponding
3670/// template template parameter.
3671///
3672/// This routine implements the semantics of C++ [temp.arg.template].
3673/// It returns true if an error occurred, and false otherwise.
3674bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003675 const TemplateArgumentLoc &Arg) {
3676 TemplateName Name = Arg.getArgument().getAsTemplate();
3677 TemplateDecl *Template = Name.getAsTemplateDecl();
3678 if (!Template) {
3679 // Any dependent template name is fine.
3680 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3681 return false;
3682 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003683
3684 // C++ [temp.arg.template]p1:
3685 // A template-argument for a template template-parameter shall be
3686 // the name of a class template, expressed as id-expression. Only
3687 // primary class templates are considered when matching the
3688 // template template argument with the corresponding parameter;
3689 // partial specializations are not considered even if their
3690 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003691 //
3692 // Note that we also allow template template parameters here, which
3693 // will happen when we are dealing with, e.g., class template
3694 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003695 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003696 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003697 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003698 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003699 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003700 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003701 << Template;
3702 }
3703
3704 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
3705 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003706 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003707 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00003708 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00003709}
3710
Douglas Gregor02024a92010-03-28 02:42:43 +00003711/// \brief Given a non-type template argument that refers to a
3712/// declaration and the type of its corresponding non-type template
3713/// parameter, produce an expression that properly refers to that
3714/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003715ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003716Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
3717 QualType ParamType,
3718 SourceLocation Loc) {
3719 assert(Arg.getKind() == TemplateArgument::Declaration &&
3720 "Only declaration template arguments permitted here");
3721 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
3722
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003723 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00003724 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
3725 // If the value is a class member, we might have a pointer-to-member.
3726 // Determine whether the non-type template template parameter is of
3727 // pointer-to-member type. If so, we need to build an appropriate
3728 // expression for a pointer-to-member, since a "normal" DeclRefExpr
3729 // would refer to the member itself.
3730 if (ParamType->isMemberPointerType()) {
3731 QualType ClassType
3732 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
3733 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00003734 = NestedNameSpecifier::Create(Context, 0, false,
3735 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00003736 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00003737 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00003738
3739 // The actual value-ness of this is unimportant, but for
3740 // internal consistency's sake, references to instance methods
3741 // are r-values.
3742 ExprValueKind VK = VK_LValue;
3743 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
3744 VK = VK_RValue;
3745
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003746 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00003747 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00003748 VK,
John McCallf89e55a2010-11-18 06:31:45 +00003749 Loc,
3750 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00003751 if (RefExpr.isInvalid())
3752 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003753
John McCall2de56d12010-08-25 11:45:40 +00003754 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003755
Douglas Gregorc0c83002010-04-30 21:46:38 +00003756 // We might need to perform a trailing qualification conversion, since
3757 // the element type on the parameter could be more qualified than the
3758 // element type in the expression we constructed.
3759 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003760 ParamType.getUnqualifiedType(), false)) {
Douglas Gregorc0c83002010-04-30 21:46:38 +00003761 Expr *RefE = RefExpr.takeAs<Expr>();
John McCall2de56d12010-08-25 11:45:40 +00003762 ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CK_NoOp);
Douglas Gregorc0c83002010-04-30 21:46:38 +00003763 RefExpr = Owned(RefE);
3764 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003765
Douglas Gregor02024a92010-03-28 02:42:43 +00003766 assert(!RefExpr.isInvalid() &&
3767 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00003768 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00003769 return move(RefExpr);
3770 }
3771 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003772
Douglas Gregor02024a92010-03-28 02:42:43 +00003773 QualType T = VD->getType().getNonReferenceType();
3774 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003775 // When the non-type template parameter is a pointer, take the
3776 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00003777 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003778 if (RefExpr.isInvalid())
3779 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003780
3781 if (T->isFunctionType() || T->isArrayType()) {
3782 // Decay functions and arrays.
3783 Expr *RefE = (Expr *)RefExpr.get();
3784 DefaultFunctionArrayConversion(RefE);
3785 if (RefE != RefExpr.get()) {
3786 RefExpr.release();
3787 RefExpr = Owned(RefE);
3788 }
3789
3790 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00003791 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003792
Douglas Gregorb7a09262010-04-01 18:32:35 +00003793 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00003794 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00003795 }
3796
John McCallf89e55a2010-11-18 06:31:45 +00003797 ExprValueKind VK = VK_RValue;
3798
Douglas Gregor02024a92010-03-28 02:42:43 +00003799 // If the non-type template parameter has reference type, qualify the
3800 // resulting declaration reference with the extra qualifiers on the
3801 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00003802 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
3803 VK = VK_LValue;
3804 T = Context.getQualifiedType(T,
3805 TargetRef->getPointeeType().getQualifiers());
3806 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003807
John McCallf89e55a2010-11-18 06:31:45 +00003808 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003809}
3810
3811/// \brief Construct a new expression that refers to the given
3812/// integral template argument with the given source-location
3813/// information.
3814///
3815/// This routine takes care of the mapping from an integral template
3816/// argument (which may have any integral type) to the appropriate
3817/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003818ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003819Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
3820 SourceLocation Loc) {
3821 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00003822 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00003823 QualType T = Arg.getIntegralType();
3824 if (T->isCharType() || T->isWideCharType())
3825 return Owned(new (Context) CharacterLiteral(
3826 Arg.getAsIntegral()->getZExtValue(),
3827 T->isWideCharType(),
3828 T,
3829 Loc));
3830 if (T->isBooleanType())
3831 return Owned(new (Context) CXXBoolLiteralExpr(
3832 Arg.getAsIntegral()->getBoolValue(),
3833 T,
3834 Loc));
3835
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003836 QualType BT;
3837 if (const EnumType *ET = T->getAs<EnumType>())
3838 BT = ET->getDecl()->getPromotionType();
3839 else
3840 BT = T;
3841
3842 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00003843 if (T->isEnumeralType()) {
3844 // FIXME: This is a hack. We need a better way to handle substituted
3845 // non-type template parameters.
3846 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast,
3847 E, 0,
3848 Context.getTrivialTypeSourceInfo(T, Loc),
3849 Loc, Loc);
3850 }
3851
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003852 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00003853}
3854
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003855/// \brief Match two template parameters within template parameter lists.
3856static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
3857 bool Complain,
3858 Sema::TemplateParameterListEqualKind Kind,
3859 SourceLocation TemplateArgLoc) {
3860 // Check the actual kind (type, non-type, template).
3861 if (Old->getKind() != New->getKind()) {
3862 if (Complain) {
3863 unsigned NextDiag = diag::err_template_param_different_kind;
3864 if (TemplateArgLoc.isValid()) {
3865 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3866 NextDiag = diag::note_template_param_different_kind;
3867 }
3868 S.Diag(New->getLocation(), NextDiag)
3869 << (Kind != Sema::TPL_TemplateMatch);
3870 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
3871 << (Kind != Sema::TPL_TemplateMatch);
3872 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003873
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003874 return false;
3875 }
3876
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003877 // Check that both are parameter packs are neither are parameter packs.
3878 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00003879 // template template parameter, the template template parameter can have
3880 // a parameter pack where the template template argument does not.
3881 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
3882 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3883 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003884 if (Complain) {
3885 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
3886 if (TemplateArgLoc.isValid()) {
3887 S.Diag(TemplateArgLoc,
3888 diag::err_template_arg_template_params_mismatch);
3889 NextDiag = diag::note_template_parameter_pack_non_pack;
3890 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003891
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003892 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
3893 : isa<NonTypeTemplateParmDecl>(New)? 1
3894 : 2;
3895 S.Diag(New->getLocation(), NextDiag)
3896 << ParamKind << New->isParameterPack();
3897 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
3898 << ParamKind << Old->isParameterPack();
3899 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003900
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003901 return false;
3902 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003903
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003904 // For non-type template parameters, check the type of the parameter.
3905 if (NonTypeTemplateParmDecl *OldNTTP
3906 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
3907 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003908
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003909 // If we are matching a template template argument to a template
3910 // template parameter and one of the non-type template parameter types
3911 // is dependent, then we must wait until template instantiation time
3912 // to actually compare the arguments.
3913 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3914 (OldNTTP->getType()->isDependentType() ||
3915 NewNTTP->getType()->isDependentType()))
3916 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003917
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003918 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
3919 if (Complain) {
3920 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
3921 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003922 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003923 diag::err_template_arg_template_params_mismatch);
3924 NextDiag = diag::note_template_nontype_parm_different_type;
3925 }
3926 S.Diag(NewNTTP->getLocation(), NextDiag)
3927 << NewNTTP->getType()
3928 << (Kind != Sema::TPL_TemplateMatch);
3929 S.Diag(OldNTTP->getLocation(),
3930 diag::note_template_nontype_parm_prev_declaration)
3931 << OldNTTP->getType();
3932 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003933
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003934 return false;
3935 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003936
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003937 return true;
3938 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003939
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003940 // For template template parameters, check the template parameter types.
3941 // The template parameter lists of template template
3942 // parameters must agree.
3943 if (TemplateTemplateParmDecl *OldTTP
3944 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003945 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003946 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
3947 OldTTP->getTemplateParameters(),
3948 Complain,
3949 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003950 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003951 : Kind),
3952 TemplateArgLoc);
3953 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003954
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003955 return true;
3956}
Douglas Gregor02024a92010-03-28 02:42:43 +00003957
Douglas Gregora0347822011-01-13 00:08:50 +00003958/// \brief Diagnose a known arity mismatch when comparing template argument
3959/// lists.
3960static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003961void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00003962 TemplateParameterList *New,
3963 TemplateParameterList *Old,
3964 Sema::TemplateParameterListEqualKind Kind,
3965 SourceLocation TemplateArgLoc) {
3966 unsigned NextDiag = diag::err_template_param_list_different_arity;
3967 if (TemplateArgLoc.isValid()) {
3968 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3969 NextDiag = diag::note_template_param_list_different_arity;
3970 }
3971 S.Diag(New->getTemplateLoc(), NextDiag)
3972 << (New->size() > Old->size())
3973 << (Kind != Sema::TPL_TemplateMatch)
3974 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
3975 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
3976 << (Kind != Sema::TPL_TemplateMatch)
3977 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
3978}
3979
Douglas Gregorddc29e12009-02-06 22:42:48 +00003980/// \brief Determine whether the given template parameter lists are
3981/// equivalent.
3982///
Mike Stump1eb44332009-09-09 15:08:12 +00003983/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00003984/// source code as part of a new template declaration.
3985///
3986/// \param Old The old template parameter list, typically found via
3987/// name lookup of the template declared with this template parameter
3988/// list.
3989///
3990/// \param Complain If true, this routine will produce a diagnostic if
3991/// the template parameter lists are not equivalent.
3992///
Douglas Gregorfb898e12009-11-12 16:20:59 +00003993/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00003994///
3995/// \param TemplateArgLoc If this source location is valid, then we
3996/// are actually checking the template parameter list of a template
3997/// argument (New) against the template parameter list of its
3998/// corresponding template template parameter (Old). We produce
3999/// slightly different diagnostics in this scenario.
4000///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004001/// \returns True if the template parameter lists are equal, false
4002/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004003bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004004Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4005 TemplateParameterList *Old,
4006 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004007 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004008 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004009 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4010 if (Complain)
4011 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4012 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004013
4014 return false;
4015 }
4016
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004017 // C++0x [temp.arg.template]p3:
4018 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004019 // when each of the template parameters in the template-parameter-list of
4020 // the template-argument's corresponding class template or template alias
4021 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004022 // template-parameter-list of P. [...]
4023 TemplateParameterList::iterator NewParm = New->begin();
4024 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004025 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004026 OldParmEnd = Old->end();
4027 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004028 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4029 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004030 if (NewParm == NewParmEnd) {
4031 if (Complain)
4032 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4033 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004034
Douglas Gregora0347822011-01-13 00:08:50 +00004035 return false;
4036 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004037
Douglas Gregora0347822011-01-13 00:08:50 +00004038 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4039 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004040 return false;
4041
Douglas Gregora0347822011-01-13 00:08:50 +00004042 ++NewParm;
4043 continue;
4044 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004045
Douglas Gregora0347822011-01-13 00:08:50 +00004046 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004047 // [...] When P's template- parameter-list contains a template parameter
4048 // pack (14.5.3), the template parameter pack will match zero or more
4049 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004050 // template-parameter-list of A with the same type and form as the
4051 // template parameter pack in P (ignoring whether those template
4052 // parameters are template parameter packs).
4053 for (; NewParm != NewParmEnd; ++NewParm) {
4054 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4055 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004056 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004057 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004058 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004059
Douglas Gregora0347822011-01-13 00:08:50 +00004060 // Make sure we exhausted all of the arguments.
4061 if (NewParm != NewParmEnd) {
4062 if (Complain)
4063 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4064 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004065
Douglas Gregora0347822011-01-13 00:08:50 +00004066 return false;
4067 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004068
Douglas Gregorddc29e12009-02-06 22:42:48 +00004069 return true;
4070}
4071
4072/// \brief Check whether a template can be declared within this scope.
4073///
4074/// If the template declaration is valid in this scope, returns
4075/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004076bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004077Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00004078 // Find the nearest enclosing declaration scope.
4079 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4080 (S->getFlags() & Scope::TemplateParamScope) != 0)
4081 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004082
Douglas Gregorddc29e12009-02-06 22:42:48 +00004083 // C++ [temp]p2:
4084 // A template-declaration can appear only as a namespace scope or
4085 // class scope declaration.
4086 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004087 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4088 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004089 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004090 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004091
Eli Friedman1503f772009-07-31 01:43:05 +00004092 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004093 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004094
4095 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4096 return false;
4097
Mike Stump1eb44332009-09-09 15:08:12 +00004098 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004099 diag::err_template_outside_namespace_or_class_scope)
4100 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004101}
Douglas Gregorcc636682009-02-17 23:15:12 +00004102
Douglas Gregord5cb8762009-10-07 00:13:32 +00004103/// \brief Determine what kind of template specialization the given declaration
4104/// is.
4105static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4106 if (!D)
4107 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004108
Douglas Gregorf6b11852009-10-08 15:14:33 +00004109 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4110 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004111 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4112 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004113 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4114 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004115
Douglas Gregord5cb8762009-10-07 00:13:32 +00004116 return TSK_Undeclared;
4117}
4118
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004119/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004120/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004121///
Douglas Gregor9302da62009-10-14 23:50:59 +00004122/// This routine determines whether a template specialization can be declared
4123/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004124///
4125/// \param S the semantic analysis object for which this check is being
4126/// performed.
4127///
4128/// \param Specialized the entity being specialized or instantiated, which
4129/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004130/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004131/// member class).
4132///
4133/// \param PrevDecl the previous declaration of this entity, if any.
4134///
4135/// \param Loc the location of the explicit specialization or instantiation of
4136/// this entity.
4137///
4138/// \param IsPartialSpecialization whether this is a partial specialization of
4139/// a class template.
4140///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004141/// \returns true if there was an error that we cannot recover from, false
4142/// otherwise.
4143static bool CheckTemplateSpecializationScope(Sema &S,
4144 NamedDecl *Specialized,
4145 NamedDecl *PrevDecl,
4146 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004147 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004148 // Keep these "kind" numbers in sync with the %select statements in the
4149 // various diagnostics emitted by this routine.
4150 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004151 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004152 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004153 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004154 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004155 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004156 EntityKind = 3;
4157 else if (isa<VarDecl>(Specialized))
4158 EntityKind = 4;
4159 else if (isa<RecordDecl>(Specialized))
4160 EntityKind = 5;
4161 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004162 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4163 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004164 return true;
4165 }
4166
Douglas Gregor88b70942009-02-25 22:02:03 +00004167 // C++ [temp.expl.spec]p2:
4168 // An explicit specialization shall be declared in the namespace
4169 // of which the template is a member, or, for member templates, in
4170 // the namespace of which the enclosing class or enclosing class
4171 // template is a member. An explicit specialization of a member
4172 // function, member class or static data member of a class
4173 // template shall be declared in the namespace of which the class
4174 // template is a member. Such a declaration may also be a
4175 // definition. If the declaration is not a definition, the
4176 // specialization may be defined later in the name- space in which
4177 // the explicit specialization was declared, or in a namespace
4178 // that encloses the one in which the explicit specialization was
4179 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004180 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004181 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004182 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004183 return true;
4184 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004185
Douglas Gregor0a407472009-10-07 17:30:37 +00004186 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
4187 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004188 << Specialized;
Douglas Gregor0a407472009-10-07 17:30:37 +00004189 return true;
4190 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004191
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004192 // C++ [temp.class.spec]p6:
4193 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004194 // in any namespace scope in which its definition may be defined (14.5.1
4195 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004196 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004197 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004198 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004199 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004200 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004201 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4202 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004203 // C++ [temp.exp.spec]p2:
4204 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004205 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004206 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004207 // An explicit specialization of a member function, member class or
4208 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004209 // namespace of which the class template is a member.
4210 //
4211 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004212 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004213 // the specialized template.
4214 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4215 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004216 bool IsCPlusPlus0xExtension
4217 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004218 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004219 S.Diag(Loc, IsCPlusPlus0xExtension
4220 ? diag::ext_template_spec_decl_out_of_scope_global
4221 : diag::err_template_spec_decl_out_of_scope_global)
4222 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004223 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004224 S.Diag(Loc, IsCPlusPlus0xExtension
4225 ? diag::ext_template_spec_decl_out_of_scope
4226 : diag::err_template_spec_decl_out_of_scope)
4227 << EntityKind << Specialized
4228 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004229
Douglas Gregor9302da62009-10-14 23:50:59 +00004230 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4231 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004232 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004233 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004234
4235 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004236 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004237 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004238 // specializations of function templates, static data members, and member
4239 // functions, so we skip the check here for those kinds of entities.
4240 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004241 // Should we refactor that check, so that it occurs later?
4242 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004243 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4244 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004245 if (isa<TranslationUnitDecl>(SpecializedContext))
4246 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4247 << EntityKind << Specialized;
4248 else if (isa<NamespaceDecl>(SpecializedContext))
4249 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4250 << EntityKind << Specialized
4251 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004252
Douglas Gregor9302da62009-10-14 23:50:59 +00004253 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004254 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004255
Douglas Gregord5cb8762009-10-07 00:13:32 +00004256 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004257
Douglas Gregor88b70942009-02-25 22:02:03 +00004258 return false;
4259}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004260
Douglas Gregorbacb9492011-01-03 21:13:47 +00004261/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4262/// that checks non-type template partial specialization arguments.
4263static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4264 NonTypeTemplateParmDecl *Param,
4265 const TemplateArgument *Args,
4266 unsigned NumArgs) {
4267 for (unsigned I = 0; I != NumArgs; ++I) {
4268 if (Args[I].getKind() == TemplateArgument::Pack) {
4269 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004270 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004271 Args[I].pack_size()))
4272 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004273
Douglas Gregore94866f2009-06-12 21:21:02 +00004274 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004275 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004276
Douglas Gregorbacb9492011-01-03 21:13:47 +00004277 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004278 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004279 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004280 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004281
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004282 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004283 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4284 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004285
4286 // Strip off any implicit casts we added as part of type checking.
4287 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4288 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004289
Douglas Gregore94866f2009-06-12 21:21:02 +00004290 // C++ [temp.class.spec]p8:
4291 // A non-type argument is non-specialized if it is the name of a
4292 // non-type parameter. All other non-type arguments are
4293 // specialized.
4294 //
4295 // Below, we check the two conditions that only apply to
4296 // specialized non-type arguments, so skip any non-specialized
4297 // arguments.
4298 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004299 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004300 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004301
Douglas Gregore94866f2009-06-12 21:21:02 +00004302 // C++ [temp.class.spec]p9:
4303 // Within the argument list of a class template partial
4304 // specialization, the following restrictions apply:
4305 // -- A partially specialized non-type argument expression
4306 // shall not involve a template parameter of the partial
4307 // specialization except when the argument expression is a
4308 // simple identifier.
4309 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004310 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004311 diag::err_dependent_non_type_arg_in_partial_spec)
4312 << ArgExpr->getSourceRange();
4313 return true;
4314 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004315
Douglas Gregore94866f2009-06-12 21:21:02 +00004316 // -- The type of a template parameter corresponding to a
4317 // specialized non-type argument shall not be dependent on a
4318 // parameter of the specialization.
4319 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004320 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004321 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4322 << Param->getType()
4323 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004324 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004325 return true;
4326 }
4327 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004328
Douglas Gregorbacb9492011-01-03 21:13:47 +00004329 return false;
4330}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004331
Douglas Gregorbacb9492011-01-03 21:13:47 +00004332/// \brief Check the non-type template arguments of a class template
4333/// partial specialization according to C++ [temp.class.spec]p9.
4334///
4335/// \param TemplateParams the template parameters of the primary class
4336/// template.
4337///
4338/// \param TemplateArg the template arguments of the class template
4339/// partial specialization.
4340///
4341/// \returns true if there was an error, false otherwise.
4342static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4343 TemplateParameterList *TemplateParams,
4344 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4345 const TemplateArgument *ArgList = TemplateArgs.data();
4346
4347 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4348 NonTypeTemplateParmDecl *Param
4349 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4350 if (!Param)
4351 continue;
4352
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004353 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004354 &ArgList[I], 1))
4355 return true;
4356 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004357
4358 return false;
4359}
4360
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004361/// \brief Retrieve the previous declaration of the given declaration.
4362static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4363 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4364 return VD->getPreviousDeclaration();
4365 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4366 return FD->getPreviousDeclaration();
4367 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4368 return TD->getPreviousDeclaration();
4369 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND))
4370 return TD->getPreviousDeclaration();
4371 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4372 return FTD->getPreviousDeclaration();
4373 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4374 return CTD->getPreviousDeclaration();
4375 return 0;
4376}
4377
John McCalld226f652010-08-21 09:40:31 +00004378DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004379Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4380 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004381 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004382 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004383 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004384 SourceLocation TemplateNameLoc,
4385 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004386 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004387 SourceLocation RAngleLoc,
4388 AttributeList *Attr,
4389 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004390 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004391
Douglas Gregorcc636682009-02-17 23:15:12 +00004392 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004393 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004394 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004395 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4396
4397 if (!ClassTemplate) {
4398 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004399 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004400 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4401 return true;
4402 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004403
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004404 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004405 bool isPartialSpecialization = false;
4406
Douglas Gregor88b70942009-02-25 22:02:03 +00004407 // Check the validity of the template headers that introduce this
4408 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004409 // FIXME: We probably shouldn't complain about these headers for
4410 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004411 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004412 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00004413 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
4414 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004415 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004416 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004417 isExplicitSpecialization,
4418 Invalid);
4419 if (Invalid)
4420 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004421
Abramo Bagnara9b934882010-06-12 08:15:14 +00004422 unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size();
4423 if (TemplateParams)
4424 --NumMatchedTemplateParamLists;
4425
Douglas Gregor05396e22009-08-25 17:23:04 +00004426 if (TemplateParams && TemplateParams->size() > 0) {
4427 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004428
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004429 if (TUK == TUK_Friend) {
4430 Diag(KWLoc, diag::err_partial_specialization_friend)
4431 << SourceRange(LAngleLoc, RAngleLoc);
4432 return true;
4433 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004434
Douglas Gregor05396e22009-08-25 17:23:04 +00004435 // C++ [temp.class.spec]p10:
4436 // The template parameter list of a specialization shall not
4437 // contain default template argument values.
4438 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4439 Decl *Param = TemplateParams->getParam(I);
4440 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4441 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004442 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004443 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004444 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004445 }
4446 } else if (NonTypeTemplateParmDecl *NTTP
4447 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4448 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004449 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004450 diag::err_default_arg_in_partial_spec)
4451 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004452 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004453 }
4454 } else {
4455 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004456 if (TTP->hasDefaultArgument()) {
4457 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004458 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004459 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004460 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004461 }
4462 }
4463 }
Douglas Gregora735b202009-10-13 14:39:41 +00004464 } else if (TemplateParams) {
4465 if (TUK == TUK_Friend)
4466 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004467 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004468 SourceRange(TemplateParams->getTemplateLoc(),
4469 TemplateParams->getRAngleLoc()))
4470 << SourceRange(LAngleLoc, RAngleLoc);
4471 else
4472 isExplicitSpecialization = true;
4473 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004474 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004475 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004476 isExplicitSpecialization = true;
4477 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004478
Douglas Gregorcc636682009-02-17 23:15:12 +00004479 // Check that the specialization uses the same tag kind as the
4480 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004481 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4482 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004483 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004484 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004485 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004486 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004487 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004488 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004489 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004490 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004491 diag::note_previous_use);
4492 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4493 }
4494
Douglas Gregor40808ce2009-03-09 23:48:35 +00004495 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004496 TemplateArgumentListInfo TemplateArgs;
4497 TemplateArgs.setLAngleLoc(LAngleLoc);
4498 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004499 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004500
Douglas Gregor925910d2011-01-03 20:35:03 +00004501 // Check for unexpanded parameter packs in any of the template arguments.
4502 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004503 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004504 UPPC_PartialSpecialization))
4505 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004506
Douglas Gregorcc636682009-02-17 23:15:12 +00004507 // Check that the template argument list is well-formed for this
4508 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004509 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004510 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4511 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004512 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004513
Douglas Gregor910f8002010-11-07 23:05:16 +00004514 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004515 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004516
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004517 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004518 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004519 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004520 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004521 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004522 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004523 return true;
4524
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004525 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004526 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004527 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004528 TemplateArgs.size())) {
4529 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4530 << ClassTemplate->getDeclName();
4531 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004532 }
4533 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004534
Douglas Gregorcc636682009-02-17 23:15:12 +00004535 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004536 ClassTemplateSpecializationDecl *PrevDecl = 0;
4537
4538 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004539 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004540 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004541 = ClassTemplate->findPartialSpecialization(Converted.data(),
4542 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004543 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004544 else
4545 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004546 = ClassTemplate->findSpecialization(Converted.data(),
4547 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004548
4549 ClassTemplateSpecializationDecl *Specialization = 0;
4550
Douglas Gregor88b70942009-02-25 22:02:03 +00004551 // Check whether we can declare a class template specialization in
4552 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004553 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004554 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4555 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004556 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004557 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004558
Douglas Gregorb88e8882009-07-30 17:40:51 +00004559 // The canonical type
4560 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004561 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004562 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004563 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004564 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004565 // arguments was referenced but not declared, or we're only
4566 // referencing this specialization as a friend, reuse that
Douglas Gregorcc636682009-02-17 23:15:12 +00004567 // declaration node as our own, updating its source location to
4568 // reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004569 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004570 Specialization->setLocation(TemplateNameLoc);
Douglas Gregorcc636682009-02-17 23:15:12 +00004571 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004572 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004573 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004574 // Build the canonical type that describes the converted template
4575 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004576 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4577 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004578 Converted.data(),
4579 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004580
4581 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004582 ClassTemplate->getInjectedClassNameSpecialization())) {
4583 // C++ [temp.class.spec]p9b3:
4584 //
4585 // -- The argument list of the specialization shall not be identical
4586 // to the implicit argument list of the primary template.
4587 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4588 << (TUK == TUK_Definition)
4589 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4590 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4591 ClassTemplate->getIdentifier(),
4592 TemplateNameLoc,
4593 Attr,
4594 TemplateParams,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00004595 AS_none,
4596 NumMatchedTemplateParamLists,
4597 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00004598 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004599
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004600 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004601 ClassTemplatePartialSpecializationDecl *PrevPartial
4602 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004603 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004604 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004605 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004606 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004607 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004608 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004609 TemplateParams,
4610 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004611 Converted.data(),
4612 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004613 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004614 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004615 PrevPartial,
4616 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004617 SetNestedNameSpecifier(Partial, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004618 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004619 Partial->setTemplateParameterListsInfo(Context,
4620 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004621 (TemplateParameterList**) TemplateParameterLists.release());
4622 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004623
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004624 if (!PrevPartial)
4625 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004626 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004627
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004628 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004629 // template specialization, make a note of that.
4630 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4631 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004632
Douglas Gregor031a5882009-06-13 00:26:55 +00004633 // Check that all of the template parameters of the class template
4634 // partial specialization are deducible from the template
4635 // arguments. If not, this class template partial specialization
4636 // will never be used.
4637 llvm::SmallVector<bool, 8> DeducibleParams;
4638 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004639 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004640 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004641 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004642 unsigned NumNonDeducible = 0;
4643 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4644 if (!DeducibleParams[I])
4645 ++NumNonDeducible;
4646
4647 if (NumNonDeducible) {
4648 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4649 << (NumNonDeducible > 1)
4650 << SourceRange(TemplateNameLoc, RAngleLoc);
4651 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4652 if (!DeducibleParams[I]) {
4653 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4654 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004655 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004656 diag::note_partial_spec_unused_parameter)
4657 << Param->getDeclName();
4658 else
Mike Stump1eb44332009-09-09 15:08:12 +00004659 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004660 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004661 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004662 }
4663 }
4664 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004665 } else {
4666 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004667 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004668 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004669 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004670 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004671 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004672 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004673 Converted.data(),
4674 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004675 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004676 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004677 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004678 Specialization->setTemplateParameterListsInfo(Context,
4679 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004680 (TemplateParameterList**) TemplateParameterLists.release());
4681 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004682
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004683 if (!PrevDecl)
4684 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004685
4686 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004687 }
4688
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004689 // C++ [temp.expl.spec]p6:
4690 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004691 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004692 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004693 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004694 // use occurs; no diagnostic is required.
4695 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004696 bool Okay = false;
4697 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4698 // Is there any previous explicit specialization declaration?
4699 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4700 Okay = true;
4701 break;
4702 }
4703 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004704
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004705 if (!Okay) {
4706 SourceRange Range(TemplateNameLoc, RAngleLoc);
4707 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
4708 << Context.getTypeDeclType(Specialization) << Range;
4709
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004710 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004711 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004712 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004713 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004714 return true;
4715 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004716 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004717
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004718 // If this is not a friend, note that this is an explicit specialization.
4719 if (TUK != TUK_Friend)
4720 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004721
4722 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004723 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00004724 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004725 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00004726 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004727 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00004728 Diag(Def->getLocation(), diag::note_previous_definition);
4729 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00004730 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004731 }
4732 }
4733
John McCall7f1b9872010-12-18 03:30:47 +00004734 if (Attr)
4735 ProcessDeclAttributeList(S, Specialization, Attr);
4736
Douglas Gregorfc705b82009-02-26 22:19:44 +00004737 // Build the fully-sugared type for this class template
4738 // specialization as the user wrote in the specialization
4739 // itself. This means that we'll pretty-print the type retrieved
4740 // from the specialization's declaration the way that the user
4741 // actually wrote the specialization, rather than formatting the
4742 // name based on the "canonical" representation used to store the
4743 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00004744 TypeSourceInfo *WrittenTy
4745 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
4746 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004747 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004748 Specialization->setTypeAsWritten(WrittenTy);
Douglas Gregor7e9b57b2010-07-06 18:33:12 +00004749 if (TemplateParams)
4750 Specialization->setTemplateKeywordLoc(TemplateParams->getTemplateLoc());
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004751 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00004752 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00004753
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004754 // C++ [temp.expl.spec]p9:
4755 // A template explicit specialization is in the scope of the
4756 // namespace in which the template was defined.
4757 //
4758 // We actually implement this paragraph where we set the semantic
4759 // context (in the creation of the ClassTemplateSpecializationDecl),
4760 // but we also maintain the lexical context where the actual
4761 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00004762 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00004763
Douglas Gregorcc636682009-02-17 23:15:12 +00004764 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004765 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00004766 Specialization->startDefinition();
4767
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004768 if (TUK == TUK_Friend) {
4769 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
4770 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00004771 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004772 /*FIXME:*/KWLoc);
4773 Friend->setAccess(AS_public);
4774 CurContext->addDecl(Friend);
4775 } else {
4776 // Add the specialization into its lexical context, so that it can
4777 // be seen when iterating through the list of declarations in that
4778 // context. However, specializations are not found by name lookup.
4779 CurContext->addDecl(Specialization);
4780 }
John McCalld226f652010-08-21 09:40:31 +00004781 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00004782}
Douglas Gregord57959a2009-03-27 23:10:48 +00004783
John McCalld226f652010-08-21 09:40:31 +00004784Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00004785 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004786 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00004787 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
4788}
4789
John McCalld226f652010-08-21 09:40:31 +00004790Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00004791 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004792 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00004793 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004794 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00004795
Douglas Gregor52591bf2009-06-24 00:54:41 +00004796 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00004797 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00004798 }
Mike Stump1eb44332009-09-09 15:08:12 +00004799
Douglas Gregor52591bf2009-06-24 00:54:41 +00004800 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004801
John McCalld226f652010-08-21 09:40:31 +00004802 Decl *DP = HandleDeclarator(ParentScope, D,
4803 move(TemplateParameterLists),
4804 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00004805 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00004806 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00004807 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00004808 FunctionTemplate->getTemplatedDecl());
4809 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
4810 return ActOnStartOfFunctionDef(FnBodyScope, Function);
4811 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00004812}
4813
John McCall75042392010-02-11 01:33:53 +00004814/// \brief Strips various properties off an implicit instantiation
4815/// that has just been explicitly specialized.
4816static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00004817 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00004818
4819 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4820 FD->setInlineSpecified(false);
4821 }
4822}
4823
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004824/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00004825/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004826/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00004827/// new specialization/instantiation will have any effect.
4828///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004829/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00004830/// instantiation.
4831///
4832/// \param NewTSK the kind of the new explicit specialization or instantiation.
4833///
4834/// \param PrevDecl the previous declaration of the entity.
4835///
4836/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
4837///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004838/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00004839/// declaration was instantiated (either implicitly or explicitly).
4840///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004841/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00004842/// specialization or instantiation has no effect and should be ignored.
4843///
4844/// \returns true if there was an error that should prevent the introduction of
4845/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00004846bool
4847Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
4848 TemplateSpecializationKind NewTSK,
4849 NamedDecl *PrevDecl,
4850 TemplateSpecializationKind PrevTSK,
4851 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004852 bool &HasNoEffect) {
4853 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004854
Douglas Gregor454885e2009-10-15 15:54:05 +00004855 switch (NewTSK) {
4856 case TSK_Undeclared:
4857 case TSK_ImplicitInstantiation:
4858 assert(false && "Don't check implicit instantiations here");
4859 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004860
Douglas Gregor454885e2009-10-15 15:54:05 +00004861 case TSK_ExplicitSpecialization:
4862 switch (PrevTSK) {
4863 case TSK_Undeclared:
4864 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004865 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00004866 // explicitly specialized or has merely been mentioned without any
4867 // instantiation.
4868 return false;
4869
4870 case TSK_ImplicitInstantiation:
4871 if (PrevPointOfInstantiation.isInvalid()) {
4872 // The declaration itself has not actually been instantiated, so it is
4873 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00004874 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00004875 return false;
4876 }
4877 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004878
Douglas Gregor454885e2009-10-15 15:54:05 +00004879 case TSK_ExplicitInstantiationDeclaration:
4880 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004881 assert((PrevTSK == TSK_ImplicitInstantiation ||
4882 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00004883 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004884
Douglas Gregor454885e2009-10-15 15:54:05 +00004885 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004886 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00004887 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004888 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00004889 // implicit instantiation to take place, in every translation unit in
4890 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004891 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4892 // Is there any previous explicit specialization declaration?
4893 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
4894 return false;
4895 }
4896
Douglas Gregor0d035142009-10-27 18:42:08 +00004897 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00004898 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004899 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00004900 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004901
Douglas Gregor454885e2009-10-15 15:54:05 +00004902 return true;
4903 }
4904 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004905
Douglas Gregor454885e2009-10-15 15:54:05 +00004906 case TSK_ExplicitInstantiationDeclaration:
4907 switch (PrevTSK) {
4908 case TSK_ExplicitInstantiationDeclaration:
4909 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004910 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004911 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004912
Douglas Gregor454885e2009-10-15 15:54:05 +00004913 case TSK_Undeclared:
4914 case TSK_ImplicitInstantiation:
4915 // We're explicitly instantiating something that may have already been
4916 // implicitly instantiated; that's fine.
4917 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004918
Douglas Gregor454885e2009-10-15 15:54:05 +00004919 case TSK_ExplicitSpecialization:
4920 // C++0x [temp.explicit]p4:
4921 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004922 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00004923 // specialization for that template, the explicit instantiation has no
4924 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004925 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004926 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004927
Douglas Gregor454885e2009-10-15 15:54:05 +00004928 case TSK_ExplicitInstantiationDefinition:
4929 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004930 // If an entity is the subject of both an explicit instantiation
4931 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00004932 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004933 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00004934 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004935 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004936 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00004937 assert(PrevPointOfInstantiation.isValid() &&
4938 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004939 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004940 return false;
4941 }
4942 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004943
Douglas Gregor454885e2009-10-15 15:54:05 +00004944 case TSK_ExplicitInstantiationDefinition:
4945 switch (PrevTSK) {
4946 case TSK_Undeclared:
4947 case TSK_ImplicitInstantiation:
4948 // We're explicitly instantiating something that may have already been
4949 // implicitly instantiated; that's fine.
4950 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004951
Douglas Gregor454885e2009-10-15 15:54:05 +00004952 case TSK_ExplicitSpecialization:
4953 // C++ DR 259, C++0x [temp.explicit]p4:
4954 // For a given set of template parameters, if an explicit
4955 // instantiation of a template appears after a declaration of
4956 // an explicit specialization for that template, the explicit
4957 // instantiation has no effect.
4958 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004959 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00004960 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00004961 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00004962 if (!getLangOptions().CPlusPlus0x) {
4963 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00004964 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004965 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00004966 diag::note_previous_template_specialization);
4967 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004968 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004969 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004970
Douglas Gregor454885e2009-10-15 15:54:05 +00004971 case TSK_ExplicitInstantiationDeclaration:
4972 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004973 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00004974 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004975
Douglas Gregor454885e2009-10-15 15:54:05 +00004976 case TSK_ExplicitInstantiationDefinition:
4977 // C++0x [temp.spec]p5:
4978 // For a given template and a given set of template-arguments,
4979 // - an explicit instantiation definition shall appear at most once
4980 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00004981 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00004982 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004983 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004984 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004985 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004986 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00004987 }
4988 break;
4989 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004990
Douglas Gregor454885e2009-10-15 15:54:05 +00004991 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004992
Douglas Gregor454885e2009-10-15 15:54:05 +00004993 return false;
4994}
4995
John McCallaf2094e2010-04-08 09:05:18 +00004996/// \brief Perform semantic analysis for the given dependent function
4997/// template specialization. The only possible way to get a dependent
4998/// function template specialization is with a friend declaration,
4999/// like so:
5000///
5001/// template <class T> void foo(T);
5002/// template <class T> class A {
5003/// friend void foo<>(T);
5004/// };
5005///
5006/// There really isn't any useful analysis we can do here, so we
5007/// just store the information.
5008bool
5009Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5010 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5011 LookupResult &Previous) {
5012 // Remove anything from Previous that isn't a function template in
5013 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005014 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005015 LookupResult::Filter F = Previous.makeFilter();
5016 while (F.hasNext()) {
5017 NamedDecl *D = F.next()->getUnderlyingDecl();
5018 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005019 !FDLookupContext->InEnclosingNamespaceSetOf(
5020 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005021 F.erase();
5022 }
5023 F.done();
5024
5025 // Should this be diagnosed here?
5026 if (Previous.empty()) return true;
5027
5028 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5029 ExplicitTemplateArgs);
5030 return false;
5031}
5032
Abramo Bagnarae03db982010-05-20 15:32:11 +00005033/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005034/// specialization.
5035///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005036/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005037/// explicit function template specialization. On successful completion,
5038/// the function declaration \p FD will become a function template
5039/// specialization.
5040///
5041/// \param FD the function declaration, which will be updated to become a
5042/// function template specialization.
5043///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005044/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5045/// if any. Note that this may be valid info even when 0 arguments are
5046/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5047/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005048///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005049/// \param PrevDecl the set of declarations that may be specialized by
5050/// this function specialization.
5051bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005052Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005053 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005054 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005055 // The set of function template specializations that could match this
5056 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005057 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005058
Sebastian Redl7a126a42010-08-31 00:36:30 +00005059 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005060 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5061 I != E; ++I) {
5062 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5063 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005064 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005065 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005066 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5067 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005068 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005069
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005070 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005071 // A trailing template-argument can be left unspecified in the
5072 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005073 // provided it can be deduced from the function argument type.
5074 // Perform template argument deduction to determine whether we may be
5075 // specializing this template.
5076 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005077 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005078 FunctionDecl *Specialization = 0;
5079 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005080 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005081 FD->getType(),
5082 Specialization,
5083 Info)) {
5084 // FIXME: Template argument deduction failed; record why it failed, so
5085 // that we can provide nifty diagnostics.
5086 (void)TDK;
5087 continue;
5088 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005089
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005090 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005091 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005092 }
5093 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005094
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005095 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005096 UnresolvedSetIterator Result
5097 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005098 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005099 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005100 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005101 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005102 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005103 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005104 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005105 return true;
John McCallc373d482010-01-27 01:50:18 +00005106
5107 // Ignore access information; it doesn't figure into redeclaration checking.
5108 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005109
5110 FunctionTemplateSpecializationInfo *SpecInfo
5111 = Specialization->getTemplateSpecializationInfo();
5112 assert(SpecInfo && "Function template specialization info missing?");
5113 {
5114 // Note: do not overwrite location info if previous template
5115 // specialization kind was explicit.
5116 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5117 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5118 Specialization->setLocation(FD->getLocation());
5119 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005120
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005121 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005122 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005123
5124 // If this is a friend declaration, then we're not really declaring
5125 // an explicit specialization.
5126 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005127
Douglas Gregord5cb8762009-10-07 00:13:32 +00005128 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005129 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005130 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005131 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005132 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005133 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005134 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005135
5136 // C++ [temp.expl.spec]p6:
5137 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005138 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005139 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005140 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005141 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005142 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005143 if (!isFriend &&
5144 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005145 TSK_ExplicitSpecialization,
5146 Specialization,
5147 SpecInfo->getTemplateSpecializationKind(),
5148 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005149 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005150 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005151
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005152 // Mark the prior declaration as an explicit specialization, so that later
5153 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005154 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005155 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005156 MarkUnusedFileScopedDecl(Specialization);
5157 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005158
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005159 // Turn the given function declaration into a function template
5160 // specialization, with the template arguments from the previous
5161 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005162 // Take copies of (semantic and syntactic) template argument lists.
5163 const TemplateArgumentList* TemplArgs = new (Context)
5164 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5165 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5166 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005167 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005168 TemplArgs, /*InsertPos=*/0,
5169 SpecInfo->getTemplateSpecializationKind(),
5170 TemplArgsAsWritten);
5171
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005172 // The "previous declaration" for this function template specialization is
5173 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005174 Previous.clear();
5175 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005176 return false;
5177}
5178
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005179/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005180/// specialization.
5181///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005182/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005183/// explicit member function specialization. On successful completion,
5184/// the function declaration \p FD will become a member function
5185/// specialization.
5186///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005187/// \param Member the member declaration, which will be updated to become a
5188/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005189///
John McCall68263142009-11-18 22:49:29 +00005190/// \param Previous the set of declarations, one of which may be specialized
5191/// by this function specialization; the set will be modified to contain the
5192/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005193bool
John McCall68263142009-11-18 22:49:29 +00005194Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005195 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005196
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005197 // Try to find the member we are instantiating.
5198 NamedDecl *Instantiation = 0;
5199 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005200 MemberSpecializationInfo *MSInfo = 0;
5201
John McCall68263142009-11-18 22:49:29 +00005202 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005203 // Nowhere to look anyway.
5204 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005205 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5206 I != E; ++I) {
5207 NamedDecl *D = (*I)->getUnderlyingDecl();
5208 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005209 if (Context.hasSameType(Function->getType(), Method->getType())) {
5210 Instantiation = Method;
5211 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005212 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005213 break;
5214 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005215 }
5216 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005217 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005218 VarDecl *PrevVar;
5219 if (Previous.isSingleResult() &&
5220 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005221 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005222 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005223 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005224 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005225 }
5226 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005227 CXXRecordDecl *PrevRecord;
5228 if (Previous.isSingleResult() &&
5229 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5230 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005231 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005232 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005233 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005234 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005235
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005236 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005237 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005238 // specializations are always out-of-line, the caller will complain about
5239 // this mismatch later.
5240 return false;
5241 }
John McCall77e8b112010-04-13 20:37:33 +00005242
5243 // If this is a friend, just bail out here before we start turning
5244 // things into explicit specializations.
5245 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5246 // Preserve instantiation information.
5247 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5248 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5249 cast<CXXMethodDecl>(InstantiatedFrom),
5250 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5251 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5252 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5253 cast<CXXRecordDecl>(InstantiatedFrom),
5254 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5255 }
5256
5257 Previous.clear();
5258 Previous.addDecl(Instantiation);
5259 return false;
5260 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005261
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005262 // Make sure that this is a specialization of a member.
5263 if (!InstantiatedFrom) {
5264 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5265 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005266 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5267 return true;
5268 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005269
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005270 // C++ [temp.expl.spec]p6:
5271 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005272 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005273 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005274 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005275 // use occurs; no diagnostic is required.
5276 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005277
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005278 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005279 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5280 TSK_ExplicitSpecialization,
5281 Instantiation,
5282 MSInfo->getTemplateSpecializationKind(),
5283 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005284 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005285 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005286
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005287 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005288 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005289 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005290 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005291 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005292 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005293
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005294 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005295 // the original declaration to note that it is an explicit specialization
5296 // (if it was previously an implicit instantiation). This latter step
5297 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005298 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005299 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5300 if (InstantiationFunction->getTemplateSpecializationKind() ==
5301 TSK_ImplicitInstantiation) {
5302 InstantiationFunction->setTemplateSpecializationKind(
5303 TSK_ExplicitSpecialization);
5304 InstantiationFunction->setLocation(Member->getLocation());
5305 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005306
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005307 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5308 cast<CXXMethodDecl>(InstantiatedFrom),
5309 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005310 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005311 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005312 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5313 if (InstantiationVar->getTemplateSpecializationKind() ==
5314 TSK_ImplicitInstantiation) {
5315 InstantiationVar->setTemplateSpecializationKind(
5316 TSK_ExplicitSpecialization);
5317 InstantiationVar->setLocation(Member->getLocation());
5318 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005319
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005320 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5321 cast<VarDecl>(InstantiatedFrom),
5322 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005323 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005324 } else {
5325 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005326 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5327 if (InstantiationClass->getTemplateSpecializationKind() ==
5328 TSK_ImplicitInstantiation) {
5329 InstantiationClass->setTemplateSpecializationKind(
5330 TSK_ExplicitSpecialization);
5331 InstantiationClass->setLocation(Member->getLocation());
5332 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005333
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005334 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005335 cast<CXXRecordDecl>(InstantiatedFrom),
5336 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005337 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005338
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005339 // Save the caller the trouble of having to figure out which declaration
5340 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005341 Previous.clear();
5342 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005343 return false;
5344}
5345
Douglas Gregor558c0322009-10-14 23:41:34 +00005346/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005347///
5348/// \returns true if a serious error occurs, false otherwise.
5349static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005350 SourceLocation InstLoc,
5351 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005352 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5353 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005354
Douglas Gregor669eed82010-07-13 00:10:04 +00005355 if (CurContext->isRecord()) {
5356 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5357 << D;
5358 return true;
5359 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005360
Douglas Gregor558c0322009-10-14 23:41:34 +00005361 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005362 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005363 // template.
5364 //
5365 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005366 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005367 !CurContext->Encloses(OrigContext)) {
5368 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005369 S.Diag(InstLoc,
5370 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005371 diag::err_explicit_instantiation_out_of_scope
5372 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005373 << D << NS;
5374 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005375 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005376 S.getLangOptions().CPlusPlus0x?
5377 diag::err_explicit_instantiation_must_be_global
5378 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005379 << D;
5380 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005381 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005382 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005383
Douglas Gregor558c0322009-10-14 23:41:34 +00005384 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005385 // If the name declared in the explicit instantiation is an unqualified
5386 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005387 // its template is declared or, if that namespace is inline (7.3.1), any
5388 // namespace from its enclosing namespace set.
5389 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005390 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005391
5392 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005393 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005394
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005395 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005396 S.getLangOptions().CPlusPlus0x?
5397 diag::err_explicit_instantiation_unqualified_wrong_namespace
5398 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005399 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005400 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005401 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005402}
5403
5404/// \brief Determine whether the given scope specifier has a template-id in it.
5405static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5406 if (!SS.isSet())
5407 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005408
Douglas Gregor558c0322009-10-14 23:41:34 +00005409 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005410 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005411 // or a static data member of a class template specialization, the name of
5412 // the class template specialization in the qualified-id for the member
5413 // name shall be a simple-template-id.
5414 //
5415 // C++98 has the same restriction, just worded differently.
5416 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5417 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005418 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005419 if (isa<TemplateSpecializationType>(T))
5420 return true;
5421
5422 return false;
5423}
5424
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005425// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005426DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005427Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005428 SourceLocation ExternLoc,
5429 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005430 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005431 SourceLocation KWLoc,
5432 const CXXScopeSpec &SS,
5433 TemplateTy TemplateD,
5434 SourceLocation TemplateNameLoc,
5435 SourceLocation LAngleLoc,
5436 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005437 SourceLocation RAngleLoc,
5438 AttributeList *Attr) {
5439 // Find the class template we're specializing
5440 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005441 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005442 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5443
5444 // Check that the specialization uses the same tag kind as the
5445 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005446 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5447 assert(Kind != TTK_Enum &&
5448 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005449 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005450 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005451 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005452 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005453 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005454 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005455 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005456 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005457 diag::note_previous_use);
5458 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5459 }
5460
Douglas Gregor558c0322009-10-14 23:41:34 +00005461 // C++0x [temp.explicit]p2:
5462 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005463 // definition and an explicit instantiation declaration. An explicit
5464 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005465 TemplateSpecializationKind TSK
5466 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5467 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005468
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005469 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005470 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005471 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005472
5473 // Check that the template argument list is well-formed for this
5474 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005475 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005476 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5477 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005478 return true;
5479
Douglas Gregor910f8002010-11-07 23:05:16 +00005480 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005481 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005482
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005483 // Find the class template specialization declaration that
5484 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005485 void *InsertPos = 0;
5486 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005487 = ClassTemplate->findSpecialization(Converted.data(),
5488 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005489
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005490 TemplateSpecializationKind PrevDecl_TSK
5491 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5492
Douglas Gregord5cb8762009-10-07 00:13:32 +00005493 // C++0x [temp.explicit]p2:
5494 // [...] An explicit instantiation shall appear in an enclosing
5495 // namespace of its template. [...]
5496 //
5497 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005498 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5499 SS.isSet()))
5500 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005501
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005502 ClassTemplateSpecializationDecl *Specialization = 0;
5503
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005504 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005505 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005506 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005507 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005508 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005509 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005510 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005511
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005512 // Even though HasNoEffect == true means that this explicit instantiation
5513 // has no effect on semantics, we go on to put its syntax in the AST.
5514
5515 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5516 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005517 // Since the only prior class template specialization with these
5518 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005519 // declaration node as our own, updating the source location
5520 // for the template name to reflect our new declaration.
5521 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005522 Specialization = PrevDecl;
5523 Specialization->setLocation(TemplateNameLoc);
5524 PrevDecl = 0;
5525 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005526 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005527
Douglas Gregor52604ab2009-09-11 21:19:12 +00005528 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005529 // Create a new class template specialization declaration node for
5530 // this explicit specialization.
5531 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005532 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005533 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005534 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005535 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005536 Converted.data(),
5537 Converted.size(),
5538 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005539 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005540
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005541 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005542 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005543 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005544 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005545 }
5546
5547 // Build the fully-sugared type for this explicit instantiation as
5548 // the user wrote in the explicit instantiation itself. This means
5549 // that we'll pretty-print the type retrieved from the
5550 // specialization's declaration the way that the user actually wrote
5551 // the explicit instantiation, rather than formatting the name based
5552 // on the "canonical" representation used to store the template
5553 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005554 TypeSourceInfo *WrittenTy
5555 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5556 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005557 Context.getTypeDeclType(Specialization));
5558 Specialization->setTypeAsWritten(WrittenTy);
5559 TemplateArgsIn.release();
5560
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005561 // Set source locations for keywords.
5562 Specialization->setExternLoc(ExternLoc);
5563 Specialization->setTemplateKeywordLoc(TemplateLoc);
5564
5565 // Add the explicit instantiation into its lexical context. However,
5566 // since explicit instantiations are never found by name lookup, we
5567 // just put it into the declaration context directly.
5568 Specialization->setLexicalDeclContext(CurContext);
5569 CurContext->addDecl(Specialization);
5570
5571 // Syntax is now OK, so return if it has no other effect on semantics.
5572 if (HasNoEffect) {
5573 // Set the template specialization kind.
5574 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005575 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005576 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005577
5578 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005579 // A definition of a class template or class member template
5580 // shall be in scope at the point of the explicit instantiation of
5581 // the class template or class member template.
5582 //
5583 // This check comes when we actually try to perform the
5584 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005585 ClassTemplateSpecializationDecl *Def
5586 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005587 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005588 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005589 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005590 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005591 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005592 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5593 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005594
Douglas Gregor0d035142009-10-27 18:42:08 +00005595 // Instantiate the members of this class template specialization.
5596 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005597 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005598 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005599 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5600
5601 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5602 // TSK_ExplicitInstantiationDefinition
5603 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5604 TSK == TSK_ExplicitInstantiationDefinition)
5605 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005606
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005607 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005608 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005609
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005610 // Set the template specialization kind.
5611 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005612 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005613}
5614
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005615// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005616DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005617Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005618 SourceLocation ExternLoc,
5619 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005620 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005621 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005622 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005623 IdentifierInfo *Name,
5624 SourceLocation NameLoc,
5625 AttributeList *Attr) {
5626
Douglas Gregor402abb52009-05-28 23:31:59 +00005627 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005628 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005629 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005630 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5631 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005632 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005633 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005634 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5635
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005636 if (!TagD)
5637 return true;
5638
John McCalld226f652010-08-21 09:40:31 +00005639 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005640 if (Tag->isEnum()) {
5641 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5642 << Context.getTypeDeclType(Tag);
5643 return true;
5644 }
5645
Douglas Gregord0c87372009-05-27 17:30:49 +00005646 if (Tag->isInvalidDecl())
5647 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005648
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005649 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5650 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5651 if (!Pattern) {
5652 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5653 << Context.getTypeDeclType(Record);
5654 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5655 return true;
5656 }
5657
Douglas Gregor558c0322009-10-14 23:41:34 +00005658 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005659 // If the explicit instantiation is for a class or member class, the
5660 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005661 // simple-template-id.
5662 //
5663 // C++98 has the same restriction, just worded differently.
5664 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005665 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005666 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005667
Douglas Gregor558c0322009-10-14 23:41:34 +00005668 // C++0x [temp.explicit]p2:
5669 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005670 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005671 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005672 TemplateSpecializationKind TSK
5673 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5674 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005675
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005676 // C++0x [temp.explicit]p2:
5677 // [...] An explicit instantiation shall appear in an enclosing
5678 // namespace of its template. [...]
5679 //
5680 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005681 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005682
Douglas Gregor454885e2009-10-15 15:54:05 +00005683 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005684 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005685 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005686 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005687 PrevDecl = Record;
5688 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005689 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005690 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005691 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005692 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005693 PrevDecl,
5694 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005695 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005696 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005697 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005698 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005699 return TagD;
5700 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005701
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005702 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00005703 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005704 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005705 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005706 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005707 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005708 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00005709 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005710 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00005711 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
5712 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005713 Diag(Pattern->getLocation(), diag::note_forward_declaration)
5714 << Pattern;
5715 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00005716 } else {
5717 if (InstantiateClass(NameLoc, Record, Def,
5718 getTemplateInstantiationArgs(Record),
5719 TSK))
5720 return true;
5721
Douglas Gregor952b0172010-02-11 01:04:33 +00005722 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00005723 if (!RecordDef)
5724 return true;
5725 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005726 }
5727
Douglas Gregor0d035142009-10-27 18:42:08 +00005728 // Instantiate all of the members of the class.
5729 InstantiateClassMembers(NameLoc, RecordDef,
5730 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005731
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005732 if (TSK == TSK_ExplicitInstantiationDefinition)
5733 MarkVTableUsed(NameLoc, RecordDef, true);
5734
Mike Stump390b4cc2009-05-16 07:39:55 +00005735 // FIXME: We don't have any representation for explicit instantiations of
5736 // member classes. Such a representation is not needed for compilation, but it
5737 // should be available for clients that want to see all of the declarations in
5738 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005739 return TagD;
5740}
5741
John McCallf312b1e2010-08-26 23:41:50 +00005742DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
5743 SourceLocation ExternLoc,
5744 SourceLocation TemplateLoc,
5745 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00005746 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00005747 // TODO: check if/when DNInfo should replace Name.
5748 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
5749 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005750 if (!Name) {
5751 if (!D.isInvalidType())
5752 Diag(D.getDeclSpec().getSourceRange().getBegin(),
5753 diag::err_explicit_instantiation_requires_name)
5754 << D.getDeclSpec().getSourceRange()
5755 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005756
Douglas Gregord5a423b2009-09-25 18:43:00 +00005757 return true;
5758 }
5759
5760 // The scope passed in may not be a decl scope. Zip up the scope tree until
5761 // we find one that is.
5762 while ((S->getFlags() & Scope::DeclScope) == 0 ||
5763 (S->getFlags() & Scope::TemplateParamScope) != 0)
5764 S = S->getParent();
5765
5766 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00005767 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
5768 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005769 if (R.isNull())
5770 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005771
Douglas Gregord5a423b2009-09-25 18:43:00 +00005772 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
5773 // Cannot explicitly instantiate a typedef.
5774 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
5775 << Name;
5776 return true;
5777 }
5778
Douglas Gregor663b5a02009-10-14 20:14:33 +00005779 // C++0x [temp.explicit]p1:
5780 // [...] An explicit instantiation of a function template shall not use the
5781 // inline or constexpr specifiers.
5782 // Presumably, this also applies to member functions of class templates as
5783 // well.
5784 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005785 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00005786 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00005787 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005788
Douglas Gregor663b5a02009-10-14 20:14:33 +00005789 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005790
Douglas Gregor558c0322009-10-14 23:41:34 +00005791 // C++0x [temp.explicit]p2:
5792 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005793 // definition and an explicit instantiation declaration. An explicit
5794 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00005795 TemplateSpecializationKind TSK
5796 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5797 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005798
Abramo Bagnara25777432010-08-11 22:01:17 +00005799 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00005800 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005801
5802 if (!R->isFunctionType()) {
5803 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005804 // A [...] static data member of a class template can be explicitly
5805 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005806 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00005807 if (Previous.isAmbiguous())
5808 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005809
John McCall1bcee0a2009-12-02 08:25:40 +00005810 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005811 if (!Prev || !Prev->isStaticDataMember()) {
5812 // We expect to see a data data member here.
5813 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
5814 << Name;
5815 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5816 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00005817 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00005818 return true;
5819 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005820
Douglas Gregord5a423b2009-09-25 18:43:00 +00005821 if (!Prev->getInstantiatedFromStaticDataMember()) {
5822 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005823 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005824 diag::err_explicit_instantiation_data_member_not_instantiated)
5825 << Prev;
5826 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
5827 // FIXME: Can we provide a note showing where this was declared?
5828 return true;
5829 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005830
Douglas Gregor558c0322009-10-14 23:41:34 +00005831 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005832 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005833 // or a static data member of a class template specialization, the name of
5834 // the class template specialization in the qualified-id for the member
5835 // name shall be a simple-template-id.
5836 //
5837 // C++98 has the same restriction, just worded differently.
5838 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005839 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005840 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005841 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005842
Douglas Gregor558c0322009-10-14 23:41:34 +00005843 // Check the scope of this explicit instantiation.
5844 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005845
Douglas Gregor454885e2009-10-15 15:54:05 +00005846 // Verify that it is okay to explicitly instantiate here.
5847 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
5848 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005849 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005850 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00005851 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005852 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005853 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005854 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005855 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005856 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005857
Douglas Gregord5a423b2009-09-25 18:43:00 +00005858 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005859 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005860 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005861 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005862
Douglas Gregord5a423b2009-09-25 18:43:00 +00005863 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00005864 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005865 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005866
5867 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00005868 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00005869 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00005870 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005871 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
5872 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00005873 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
5874 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00005875 ASTTemplateArgsPtr TemplateArgsPtr(*this,
5876 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00005877 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00005878 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00005879 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00005880 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00005881 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005882
Douglas Gregord5a423b2009-09-25 18:43:00 +00005883 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005884 // A [...] function [...] can be explicitly instantiated from its template.
5885 // A member function [...] of a class template can be explicitly
5886 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005887 // template.
John McCallc373d482010-01-27 01:50:18 +00005888 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005889 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5890 P != PEnd; ++P) {
5891 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00005892 if (!HasExplicitTemplateArgs) {
5893 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
5894 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
5895 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00005896
John McCallc373d482010-01-27 01:50:18 +00005897 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00005898 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
5899 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00005900 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00005901 }
5902 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005903
Douglas Gregord5a423b2009-09-25 18:43:00 +00005904 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
5905 if (!FunTmpl)
5906 continue;
5907
John McCall5769d612010-02-08 23:07:23 +00005908 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005909 FunctionDecl *Specialization = 0;
5910 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005911 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00005912 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005913 R, Specialization, Info)) {
5914 // FIXME: Keep track of almost-matches?
5915 (void)TDK;
5916 continue;
5917 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005918
John McCallc373d482010-01-27 01:50:18 +00005919 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005920 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005921
Douglas Gregord5a423b2009-09-25 18:43:00 +00005922 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005923 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005924 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005925 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005926 PDiag(diag::err_explicit_instantiation_not_known) << Name,
5927 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
5928 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00005929
John McCallc373d482010-01-27 01:50:18 +00005930 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00005931 return true;
John McCallc373d482010-01-27 01:50:18 +00005932
5933 // Ignore access control bits, we don't need them for redeclaration checking.
5934 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005935
Douglas Gregor0a897e32009-10-15 17:21:20 +00005936 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005937 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005938 diag::err_explicit_instantiation_member_function_not_instantiated)
5939 << Specialization
5940 << (Specialization->getTemplateSpecializationKind() ==
5941 TSK_ExplicitSpecialization);
5942 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
5943 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005944 }
5945
Douglas Gregor0a897e32009-10-15 17:21:20 +00005946 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00005947 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
5948 PrevDecl = Specialization;
5949
Douglas Gregor0a897e32009-10-15 17:21:20 +00005950 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005951 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005952 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005953 PrevDecl,
5954 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00005955 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005956 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00005957 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005958
Douglas Gregor0a897e32009-10-15 17:21:20 +00005959 // FIXME: We may still want to build some representation of this
5960 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005961 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005962 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00005963 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00005964
5965 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005966
Douglas Gregor0a897e32009-10-15 17:21:20 +00005967 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005968 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005969
Douglas Gregor558c0322009-10-14 23:41:34 +00005970 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005971 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005972 // or a static data member of a class template specialization, the name of
5973 // the class template specialization in the qualified-id for the member
5974 // name shall be a simple-template-id.
5975 //
5976 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005977 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005978 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005979 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00005980 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005981 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005982 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005983 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005984
Douglas Gregor558c0322009-10-14 23:41:34 +00005985 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005986 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00005987 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005988 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00005989 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005990
Douglas Gregord5a423b2009-09-25 18:43:00 +00005991 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00005992 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005993}
5994
John McCallf312b1e2010-08-26 23:41:50 +00005995TypeResult
John McCallc4e70192009-09-11 04:59:25 +00005996Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
5997 const CXXScopeSpec &SS, IdentifierInfo *Name,
5998 SourceLocation TagLoc, SourceLocation NameLoc) {
5999 // This has to hold, because SS is expected to be defined.
6000 assert(Name && "Expected a name in a dependent tag");
6001
6002 NestedNameSpecifier *NNS
6003 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6004 if (!NNS)
6005 return true;
6006
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006007 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006008
Douglas Gregor48c89f42010-04-24 16:38:41 +00006009 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6010 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006011 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006012 return true;
6013 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006014
Douglas Gregor059101f2011-03-02 00:47:37 +00006015 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006016 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006017 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6018
6019 // Create type-source location information for this type.
6020 TypeLocBuilder TLB;
6021 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6022 TL.setKeywordLoc(TagLoc);
6023 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6024 TL.setNameLoc(NameLoc);
6025 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006026}
6027
John McCallf312b1e2010-08-26 23:41:50 +00006028TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006029Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6030 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006031 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006032 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006033 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006034
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006035 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6036 !getLangOptions().CPlusPlus0x)
6037 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006038 << FixItHint::CreateRemoval(TypenameLoc);
6039
Douglas Gregor2494dd02011-03-01 01:34:45 +00006040 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006041 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6042 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006043 if (T.isNull())
6044 return true;
John McCall63b43852010-04-29 23:50:39 +00006045
6046 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6047 if (isa<DependentNameType>(T)) {
6048 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006049 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006050 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006051 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006052 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006053 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006054 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006055 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006056 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006057 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006058
John McCallb3d87482010-08-24 05:47:05 +00006059 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006060}
6061
John McCallf312b1e2010-08-26 23:41:50 +00006062TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006063Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6064 const CXXScopeSpec &SS,
6065 SourceLocation TemplateLoc,
6066 TemplateTy TemplateIn,
6067 SourceLocation TemplateNameLoc,
6068 SourceLocation LAngleLoc,
6069 ASTTemplateArgsPtr TemplateArgsIn,
6070 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006071 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
6072 !getLangOptions().CPlusPlus0x)
6073 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00006074 << FixItHint::CreateRemoval(TypenameLoc);
6075
6076 // Translate the parser's template argument list in our AST format.
6077 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6078 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6079
6080 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006081 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6082 // Construct a dependent template specialization type.
6083 assert(DTN && "dependent template has non-dependent name?");
6084 assert(DTN->getQualifier()
6085 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6086 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6087 DTN->getQualifier(),
6088 DTN->getIdentifier(),
6089 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006090
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006091 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006092 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006093 DependentTemplateSpecializationTypeLoc SpecTL
6094 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006095 SpecTL.setLAngleLoc(LAngleLoc);
6096 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006097 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006098 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006099 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006100 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6101 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006102 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006103 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006104
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006105 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6106 if (T.isNull())
6107 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006108
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006109 // Provide source-location information for the template specialization
6110 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006111 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006112 TemplateSpecializationTypeLoc SpecTL
6113 = Builder.push<TemplateSpecializationTypeLoc>(T);
6114
6115 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006116 SpecTL.setLAngleLoc(LAngleLoc);
6117 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006118 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006119 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6120 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6121
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006122 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6123 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6124 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006125 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6126
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006127 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6128 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006129}
6130
Douglas Gregora02411e2011-02-27 22:46:49 +00006131
Douglas Gregord57959a2009-03-27 23:10:48 +00006132/// \brief Build the type that describes a C++ typename specifier,
6133/// e.g., "typename T::type".
6134QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006135Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6136 SourceLocation KeywordLoc,
6137 NestedNameSpecifierLoc QualifierLoc,
6138 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006139 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006140 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006141 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006142
John McCall77bb1aa2010-05-01 00:40:08 +00006143 DeclContext *Ctx = computeDeclContext(SS);
6144 if (!Ctx) {
6145 // If the nested-name-specifier is dependent and couldn't be
6146 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006147 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6148 return Context.getDependentNameType(Keyword,
6149 QualifierLoc.getNestedNameSpecifier(),
6150 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006151 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006152
John McCall77bb1aa2010-05-01 00:40:08 +00006153 // If the nested-name-specifier refers to the current instantiation,
6154 // the "typename" keyword itself is superfluous. In C++03, the
6155 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6156 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006157 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006158
John McCall77bb1aa2010-05-01 00:40:08 +00006159 if (RequireCompleteDeclContext(SS, Ctx))
6160 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006161
6162 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006163 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006164 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006165 unsigned DiagID = 0;
6166 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006167 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006168 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006169 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006170 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006171
6172 case LookupResult::FoundUnresolvedValue: {
6173 // We found a using declaration that is a value. Most likely, the using
6174 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006175 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006176 IILoc);
6177 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6178 << Name << Ctx << FullRange;
6179 if (UnresolvedUsingValueDecl *Using
6180 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006181 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006182 Diag(Loc, diag::note_using_value_decl_missing_typename)
6183 << FixItHint::CreateInsertion(Loc, "typename ");
6184 }
6185 }
6186 // Fall through to create a dependent typename type, from which we can recover
6187 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006188
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006189 case LookupResult::NotFoundInCurrentInstantiation:
6190 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006191 return Context.getDependentNameType(Keyword,
6192 QualifierLoc.getNestedNameSpecifier(),
6193 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006194
6195 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006196 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006197 // We found a type. Build an ElaboratedType, since the
6198 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006199 return Context.getElaboratedType(ETK_Typename,
6200 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006201 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006202 }
6203
6204 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006205 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006206 break;
6207
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006208
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006209 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006210 return QualType();
6211
Douglas Gregord57959a2009-03-27 23:10:48 +00006212 case LookupResult::FoundOverloaded:
6213 DiagID = diag::err_typename_nested_not_type;
6214 Referenced = *Result.begin();
6215 break;
6216
John McCall6e247262009-10-10 05:48:19 +00006217 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006218 return QualType();
6219 }
6220
6221 // If we get here, it's because name lookup did not find a
6222 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006223 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006224 IILoc);
6225 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006226 if (Referenced)
6227 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6228 << Name;
6229 return QualType();
6230}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006231
6232namespace {
6233 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006234 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006235 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006236 SourceLocation Loc;
6237 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006238
Douglas Gregor4a959d82009-08-06 16:20:37 +00006239 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006240 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006241
Mike Stump1eb44332009-09-09 15:08:12 +00006242 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006243 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006244 DeclarationName Entity)
6245 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006246 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006247
6248 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006249 /// transformed.
6250 ///
6251 /// For the purposes of type reconstruction, a type has already been
6252 /// transformed if it is NULL or if it is not dependent.
6253 bool AlreadyTransformed(QualType T) {
6254 return T.isNull() || !T->isDependentType();
6255 }
Mike Stump1eb44332009-09-09 15:08:12 +00006256
6257 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006258 /// rebuilt.
6259 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006260
Douglas Gregor4a959d82009-08-06 16:20:37 +00006261 /// \brief Returns the name of the entity whose type is being rebuilt.
6262 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006263
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006264 /// \brief Sets the "base" location and entity when that
6265 /// information is known based on another transformation.
6266 void setBase(SourceLocation Loc, DeclarationName Entity) {
6267 this->Loc = Loc;
6268 this->Entity = Entity;
6269 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006270 };
6271}
6272
Douglas Gregor4a959d82009-08-06 16:20:37 +00006273/// \brief Rebuilds a type within the context of the current instantiation.
6274///
Mike Stump1eb44332009-09-09 15:08:12 +00006275/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006276/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006277/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006278/// partial specialization thereof). This routine will rebuild that type now
6279/// that we have entered the declarator's scope, which may produce different
6280/// canonical types, e.g.,
6281///
6282/// \code
6283/// template<typename T>
6284/// struct X {
6285/// typedef T* pointer;
6286/// pointer data();
6287/// };
6288///
6289/// template<typename T>
6290/// typename X<T>::pointer X<T>::data() { ... }
6291/// \endcode
6292///
Douglas Gregor4714c122010-03-31 17:34:00 +00006293/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006294/// since we do not know that we can look into X<T> when we parsed the type.
6295/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006296/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006297/// as the canonical type of T*, allowing the return types of the out-of-line
6298/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006299TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6300 SourceLocation Loc,
6301 DeclarationName Name) {
6302 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006303 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006304
Douglas Gregor4a959d82009-08-06 16:20:37 +00006305 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6306 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006307}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006308
John McCall60d7b3a2010-08-24 06:29:42 +00006309ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006310 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6311 DeclarationName());
6312 return Rebuilder.TransformExpr(E);
6313}
6314
John McCall63b43852010-04-29 23:50:39 +00006315bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006316 if (SS.isInvalid())
6317 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006318
Douglas Gregor7e384942011-02-25 16:07:42 +00006319 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006320 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6321 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006322 NestedNameSpecifierLoc Rebuilt
6323 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6324 if (!Rebuilt)
6325 return true;
John McCall63b43852010-04-29 23:50:39 +00006326
Douglas Gregor7e384942011-02-25 16:07:42 +00006327 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006328 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006329}
6330
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006331/// \brief Produces a formatted string that describes the binding of
6332/// template parameters to template arguments.
6333std::string
6334Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6335 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006336 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006337}
6338
6339std::string
6340Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6341 const TemplateArgument *Args,
6342 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006343 llvm::SmallString<128> Str;
6344 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006345
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006346 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006347 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006348
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006349 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006350 if (I >= NumArgs)
6351 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006352
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006353 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006354 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006355 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006356 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006357
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006358 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006359 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006360 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006361 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006362 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006363
Douglas Gregor87dd6972010-12-20 16:52:59 +00006364 Out << " = ";
6365 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006366 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006367
6368 Out << ']';
6369 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006370}