blob: 8f7b7da6f3be4dfca5686bfccd44a07a2fdae62f [file] [log] [blame]
Douglas Gregor72c3f312008-12-05 18:15:24 +00001//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Douglas Gregor99ebf652009-02-27 19:31:52 +00007//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +00008//
9// This file implements semantic analysis for C++ templates.
Douglas Gregor99ebf652009-02-27 19:31:52 +000010//===----------------------------------------------------------------------===/
Douglas Gregor72c3f312008-12-05 18:15:24 +000011
John McCall2d887082010-08-25 22:03:47 +000012#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000013#include "clang/Sema/Lookup.h"
John McCall5f1e0942010-08-24 08:50:51 +000014#include "clang/Sema/Scope.h"
John McCall7cd088e2010-08-24 07:21:54 +000015#include "clang/Sema/Template.h"
John McCall2a7fb272010-08-25 05:32:35 +000016#include "clang/Sema/TemplateDeduction.h"
Douglas Gregor4a959d82009-08-06 16:20:37 +000017#include "TreeTransform.h"
Douglas Gregorddc29e12009-02-06 22:42:48 +000018#include "clang/AST/ASTContext.h"
Douglas Gregor898574e2008-12-05 23:32:09 +000019#include "clang/AST/Expr.h"
Douglas Gregorcc45cb32009-02-11 19:52:55 +000020#include "clang/AST/ExprCXX.h"
John McCall92b7f702010-03-11 07:50:04 +000021#include "clang/AST/DeclFriend.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000022#include "clang/AST/DeclTemplate.h"
John McCall4e2cbb22010-10-20 05:44:58 +000023#include "clang/AST/RecursiveASTVisitor.h"
Douglas Gregor5f3aeb62010-10-13 00:27:52 +000024#include "clang/AST/TypeVisitor.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
26#include "clang/Sema/ParsedTemplate.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000027#include "clang/Basic/LangOptions.h"
Douglas Gregord5a423b2009-09-25 18:43:00 +000028#include "clang/Basic/PartialDiagnostic.h"
Douglas Gregorbf4ea562009-09-15 16:23:51 +000029#include "llvm/ADT/StringExtras.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000030using namespace clang;
John McCall2a7fb272010-08-25 05:32:35 +000031using namespace sema;
Douglas Gregor72c3f312008-12-05 18:15:24 +000032
John McCall78b81052010-11-10 02:40:36 +000033// Exported for use by Parser.
34SourceRange
35clang::getTemplateParamsRange(TemplateParameterList const * const *Ps,
36 unsigned N) {
37 if (!N) return SourceRange();
38 return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc());
39}
40
Douglas Gregor2dd078a2009-09-02 22:59:36 +000041/// \brief Determine whether the declaration found is acceptable as the name
42/// of a template and, if so, return that template declaration. Otherwise,
43/// returns NULL.
John McCallad00b772010-06-16 08:42:20 +000044static NamedDecl *isAcceptableTemplateName(ASTContext &Context,
45 NamedDecl *Orig) {
46 NamedDecl *D = Orig->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000047
Douglas Gregor2dd078a2009-09-02 22:59:36 +000048 if (isa<TemplateDecl>(D))
John McCallad00b772010-06-16 08:42:20 +000049 return Orig;
Mike Stump1eb44332009-09-09 15:08:12 +000050
Douglas Gregor2dd078a2009-09-02 22:59:36 +000051 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
52 // C++ [temp.local]p1:
53 // Like normal (non-template) classes, class templates have an
54 // injected-class-name (Clause 9). The injected-class-name
55 // can be used with or without a template-argument-list. When
56 // it is used without a template-argument-list, it is
57 // equivalent to the injected-class-name followed by the
58 // template-parameters of the class template enclosed in
59 // <>. When it is used with a template-argument-list, it
60 // refers to the specified class template specialization,
61 // which could be the current specialization or another
62 // specialization.
63 if (Record->isInjectedClassName()) {
Douglas Gregor542b5482009-10-14 17:30:58 +000064 Record = cast<CXXRecordDecl>(Record->getDeclContext());
Douglas Gregor2dd078a2009-09-02 22:59:36 +000065 if (Record->getDescribedClassTemplate())
66 return Record->getDescribedClassTemplate();
67
68 if (ClassTemplateSpecializationDecl *Spec
69 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
70 return Spec->getSpecializedTemplate();
71 }
Mike Stump1eb44332009-09-09 15:08:12 +000072
Douglas Gregor2dd078a2009-09-02 22:59:36 +000073 return 0;
74 }
Mike Stump1eb44332009-09-09 15:08:12 +000075
Douglas Gregor2dd078a2009-09-02 22:59:36 +000076 return 0;
77}
78
John McCallf7a1a742009-11-24 19:00:30 +000079static void FilterAcceptableTemplateNames(ASTContext &C, LookupResult &R) {
Douglas Gregor01e56ae2010-04-12 20:54:26 +000080 // The set of class templates we've already seen.
81 llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates;
John McCallf7a1a742009-11-24 19:00:30 +000082 LookupResult::Filter filter = R.makeFilter();
83 while (filter.hasNext()) {
84 NamedDecl *Orig = filter.next();
John McCallad00b772010-06-16 08:42:20 +000085 NamedDecl *Repl = isAcceptableTemplateName(C, Orig);
John McCallf7a1a742009-11-24 19:00:30 +000086 if (!Repl)
87 filter.erase();
Douglas Gregor01e56ae2010-04-12 20:54:26 +000088 else if (Repl != Orig) {
89
90 // C++ [temp.local]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000091 // A lookup that finds an injected-class-name (10.2) can result in an
Douglas Gregor01e56ae2010-04-12 20:54:26 +000092 // ambiguity in certain cases (for example, if it is found in more than
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000093 // one base class). If all of the injected-class-names that are found
94 // refer to specializations of the same class template, and if the name
95 // is followed by a template-argument-list, the reference refers to the
96 // class template itself and not a specialization thereof, and is not
Douglas Gregor01e56ae2010-04-12 20:54:26 +000097 // ambiguous.
98 //
99 // FIXME: Will we eventually have to do the same for alias templates?
100 if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
101 if (!ClassTemplates.insert(ClassTmpl)) {
102 filter.erase();
103 continue;
104 }
John McCall8ba66912010-08-13 07:02:08 +0000105
106 // FIXME: we promote access to public here as a workaround to
107 // the fact that LookupResult doesn't let us remember that we
108 // found this template through a particular injected class name,
109 // which means we end up doing nasty things to the invariants.
110 // Pretending that access is public is *much* safer.
111 filter.replace(Repl, AS_public);
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000112 }
John McCallf7a1a742009-11-24 19:00:30 +0000113 }
114 filter.done();
115}
116
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000117TemplateNameKind Sema::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000118 CXXScopeSpec &SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000119 bool hasTemplateKeyword,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000120 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +0000121 ParsedType ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000122 bool EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000123 TemplateTy &TemplateResult,
124 bool &MemberOfUnknownSpecialization) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +0000125 assert(getLangOptions().CPlusPlus && "No template names in C!");
126
Douglas Gregor014e88d2009-11-03 23:16:33 +0000127 DeclarationName TName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000128 MemberOfUnknownSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000129
Douglas Gregor014e88d2009-11-03 23:16:33 +0000130 switch (Name.getKind()) {
131 case UnqualifiedId::IK_Identifier:
132 TName = DeclarationName(Name.Identifier);
133 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000134
Douglas Gregor014e88d2009-11-03 23:16:33 +0000135 case UnqualifiedId::IK_OperatorFunctionId:
136 TName = Context.DeclarationNames.getCXXOperatorName(
137 Name.OperatorFunctionId.Operator);
138 break;
139
Sean Hunte6252d12009-11-28 08:58:14 +0000140 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +0000141 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
142 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000143
Douglas Gregor014e88d2009-11-03 23:16:33 +0000144 default:
145 return TNK_Non_template;
146 }
Mike Stump1eb44332009-09-09 15:08:12 +0000147
John McCallb3d87482010-08-24 05:47:05 +0000148 QualType ObjectType = ObjectTypePtr.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000149
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000150 LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000151 LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000152 LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
153 MemberOfUnknownSpecialization);
John McCall67d22fb2010-08-28 20:17:00 +0000154 if (R.empty()) return TNK_Non_template;
155 if (R.isAmbiguous()) {
156 // Suppress diagnostics; we'll redo this lookup later.
John McCallb8592062010-08-13 02:23:42 +0000157 R.suppressDiagnostics();
John McCall67d22fb2010-08-28 20:17:00 +0000158
159 // FIXME: we might have ambiguous templates, in which case we
160 // should at least parse them properly!
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000161 return TNK_Non_template;
John McCallb8592062010-08-13 02:23:42 +0000162 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000163
John McCall0bd6feb2009-12-02 08:04:21 +0000164 TemplateName Template;
165 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000166
John McCall0bd6feb2009-12-02 08:04:21 +0000167 unsigned ResultCount = R.end() - R.begin();
168 if (ResultCount > 1) {
169 // We assume that we'll preserve the qualifier from a function
170 // template name in other ways.
171 Template = Context.getOverloadedTemplateName(R.begin(), R.end());
172 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000173
174 // We'll do this lookup again later.
175 R.suppressDiagnostics();
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000176 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000177 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
178
179 if (SS.isSet() && !SS.isInvalid()) {
180 NestedNameSpecifier *Qualifier
181 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Abramo Bagnara7c153532010-08-06 12:11:11 +0000182 Template = Context.getQualifiedTemplateName(Qualifier,
183 hasTemplateKeyword, TD);
John McCall0bd6feb2009-12-02 08:04:21 +0000184 } else {
185 Template = TemplateName(TD);
186 }
187
John McCallb8592062010-08-13 02:23:42 +0000188 if (isa<FunctionTemplateDecl>(TD)) {
John McCall0bd6feb2009-12-02 08:04:21 +0000189 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000190
191 // We'll do this lookup again later.
192 R.suppressDiagnostics();
193 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000194 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD));
195 TemplateKind = TNK_Type_template;
196 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000197 }
Mike Stump1eb44332009-09-09 15:08:12 +0000198
John McCall0bd6feb2009-12-02 08:04:21 +0000199 TemplateResult = TemplateTy::make(Template);
200 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000201}
202
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000203bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Douglas Gregor84d0a192010-01-12 21:28:44 +0000204 SourceLocation IILoc,
205 Scope *S,
206 const CXXScopeSpec *SS,
207 TemplateTy &SuggestedTemplate,
208 TemplateNameKind &SuggestedKind) {
209 // We can't recover unless there's a dependent scope specifier preceding the
210 // template name.
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000211 // FIXME: Typo correction?
Douglas Gregor84d0a192010-01-12 21:28:44 +0000212 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
213 computeDeclContext(*SS))
214 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000215
Douglas Gregor84d0a192010-01-12 21:28:44 +0000216 // The code is missing a 'template' keyword prior to the dependent template
217 // name.
218 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
219 Diag(IILoc, diag::err_template_kw_missing)
220 << Qualifier << II.getName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000221 << FixItHint::CreateInsertion(IILoc, "template ");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000222 SuggestedTemplate
Douglas Gregor84d0a192010-01-12 21:28:44 +0000223 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
224 SuggestedKind = TNK_Dependent_template_name;
225 return true;
226}
227
John McCallf7a1a742009-11-24 19:00:30 +0000228void Sema::LookupTemplateName(LookupResult &Found,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000229 Scope *S, CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +0000230 QualType ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000231 bool EnteringContext,
232 bool &MemberOfUnknownSpecialization) {
John McCallf7a1a742009-11-24 19:00:30 +0000233 // Determine where to perform name lookup
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000234 MemberOfUnknownSpecialization = false;
John McCallf7a1a742009-11-24 19:00:30 +0000235 DeclContext *LookupCtx = 0;
236 bool isDependent = false;
237 if (!ObjectType.isNull()) {
238 // This nested-name-specifier occurs in a member access expression, e.g.,
239 // x->B::f, and we are looking into the type of the object.
240 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
241 LookupCtx = computeDeclContext(ObjectType);
242 isDependent = ObjectType->isDependentType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000243 assert((isDependent || !ObjectType->isIncompleteType()) &&
John McCallf7a1a742009-11-24 19:00:30 +0000244 "Caller should have completed object type");
245 } else if (SS.isSet()) {
246 // This nested-name-specifier occurs after another nested-name-specifier,
247 // so long into the context associated with the prior nested-name-specifier.
248 LookupCtx = computeDeclContext(SS, EnteringContext);
249 isDependent = isDependentScopeSpecifier(SS);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000250
John McCallf7a1a742009-11-24 19:00:30 +0000251 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000252 if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
John McCallf7a1a742009-11-24 19:00:30 +0000253 return;
254 }
255
256 bool ObjectTypeSearchedInScope = false;
257 if (LookupCtx) {
258 // Perform "qualified" name lookup into the declaration context we
259 // computed, which is either the type of the base of a member access
260 // expression or the declaration context associated with a prior
261 // nested-name-specifier.
262 LookupQualifiedName(Found, LookupCtx);
263
264 if (!ObjectType.isNull() && Found.empty()) {
265 // C++ [basic.lookup.classref]p1:
266 // In a class member access expression (5.2.5), if the . or -> token is
267 // immediately followed by an identifier followed by a <, the
268 // identifier must be looked up to determine whether the < is the
269 // beginning of a template argument list (14.2) or a less-than operator.
270 // The identifier is first looked up in the class of the object
271 // expression. If the identifier is not found, it is then looked up in
272 // the context of the entire postfix-expression and shall name a class
273 // or function template.
John McCallf7a1a742009-11-24 19:00:30 +0000274 if (S) LookupName(Found, S);
275 ObjectTypeSearchedInScope = true;
276 }
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000277 } else if (isDependent && (!S || ObjectType.isNull())) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000278 // We cannot look into a dependent object type or nested nme
279 // specifier.
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000280 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000281 return;
282 } else {
283 // Perform unqualified name lookup in the current scope.
284 LookupName(Found, S);
285 }
286
Douglas Gregor2e933882010-01-12 17:06:20 +0000287 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000288 // If we did not find any names, attempt to correct any typos.
289 DeclarationName Name = Found.getLookupName();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000290 if (DeclarationName Corrected = CorrectTypo(Found, S, &SS, LookupCtx,
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000291 false, CTC_CXXCasts)) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000292 FilterAcceptableTemplateNames(Context, Found);
John McCallad00b772010-06-16 08:42:20 +0000293 if (!Found.empty()) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000294 if (LookupCtx)
295 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
296 << Name << LookupCtx << Found.getLookupName() << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +0000297 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000298 Found.getLookupName().getAsString());
299 else
300 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
301 << Name << Found.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000302 << FixItHint::CreateReplacement(Found.getNameLoc(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000303 Found.getLookupName().getAsString());
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000304 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
305 Diag(Template->getLocation(), diag::note_previous_decl)
306 << Template->getDeclName();
John McCallad00b772010-06-16 08:42:20 +0000307 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000308 } else {
309 Found.clear();
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000310 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000311 }
312 }
313
John McCallf7a1a742009-11-24 19:00:30 +0000314 FilterAcceptableTemplateNames(Context, Found);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000315 if (Found.empty()) {
316 if (isDependent)
317 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000318 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000319 }
John McCallf7a1a742009-11-24 19:00:30 +0000320
321 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
322 // C++ [basic.lookup.classref]p1:
323 // [...] If the lookup in the class of the object expression finds a
324 // template, the name is also looked up in the context of the entire
325 // postfix-expression and [...]
326 //
327 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
328 LookupOrdinaryName);
329 LookupName(FoundOuter, S);
330 FilterAcceptableTemplateNames(Context, FoundOuter);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000331
John McCallf7a1a742009-11-24 19:00:30 +0000332 if (FoundOuter.empty()) {
333 // - if the name is not found, the name found in the class of the
334 // object expression is used, otherwise
335 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>()) {
336 // - if the name is found in the context of the entire
337 // postfix-expression and does not name a class template, the name
338 // found in the class of the object expression is used, otherwise
John McCallad00b772010-06-16 08:42:20 +0000339 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000340 // - if the name found is a class template, it must refer to the same
341 // entity as the one found in the class of the object expression,
342 // otherwise the program is ill-formed.
343 if (!Found.isSingleResult() ||
344 Found.getFoundDecl()->getCanonicalDecl()
345 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000346 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000347 diag::ext_nested_name_member_ref_lookup_ambiguous)
348 << Found.getLookupName()
349 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000350 Diag(Found.getRepresentativeDecl()->getLocation(),
351 diag::note_ambig_member_ref_object_type)
352 << ObjectType;
353 Diag(FoundOuter.getFoundDecl()->getLocation(),
354 diag::note_ambig_member_ref_scope);
355
356 // Recover by taking the template that we found in the object
357 // expression's type.
358 }
359 }
360 }
361}
362
John McCall2f841ba2009-12-02 03:53:29 +0000363/// ActOnDependentIdExpression - Handle a dependent id-expression that
364/// was just parsed. This is only possible with an explicit scope
365/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000366ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000367Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000368 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000369 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000370 const TemplateArgumentListInfo *TemplateArgs) {
371 NestedNameSpecifier *Qualifier
372 = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
John McCallea1471e2010-05-20 01:18:31 +0000373
374 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000375
John McCall2f841ba2009-12-02 03:53:29 +0000376 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000377 isa<CXXMethodDecl>(DC) &&
378 cast<CXXMethodDecl>(DC)->isInstance()) {
379 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000380
John McCallf7a1a742009-11-24 19:00:30 +0000381 // Since the 'this' expression is synthesized, we don't need to
382 // perform the double-lookup check.
383 NamedDecl *FirstQualifierInScope = 0;
384
John McCallaa81e162009-12-01 22:10:20 +0000385 return Owned(CXXDependentScopeMemberExpr::Create(Context,
386 /*This*/ 0, ThisType,
387 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000388 /*Op*/ SourceLocation(),
389 Qualifier, SS.getRange(),
390 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000391 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000392 TemplateArgs));
393 }
394
Abramo Bagnara25777432010-08-11 22:01:17 +0000395 return BuildDependentDeclRefExpr(SS, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000396}
397
John McCall60d7b3a2010-08-24 06:29:42 +0000398ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000399Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +0000400 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000401 const TemplateArgumentListInfo *TemplateArgs) {
402 return Owned(DependentScopeDeclRefExpr::Create(Context,
403 static_cast<NestedNameSpecifier*>(SS.getScopeRep()),
404 SS.getRange(),
Abramo Bagnara25777432010-08-11 22:01:17 +0000405 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000406 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000407}
408
Douglas Gregor72c3f312008-12-05 18:15:24 +0000409/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
410/// that the template parameter 'PrevDecl' is being shadowed by a new
411/// declaration at location Loc. Returns true to indicate that this is
412/// an error, and false otherwise.
413bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000414 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000415
416 // Microsoft Visual C++ permits template parameters to be shadowed.
417 if (getLangOptions().Microsoft)
418 return false;
419
420 // C++ [temp.local]p4:
421 // A template-parameter shall not be redeclared within its
422 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000423 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000424 << cast<NamedDecl>(PrevDecl)->getDeclName();
425 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
426 return true;
427}
428
Douglas Gregor2943aed2009-03-03 04:44:36 +0000429/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000430/// the parameter D to reference the templated declaration and return a pointer
431/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000432TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
433 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
434 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000435 return Temp;
436 }
437 return 0;
438}
439
Douglas Gregorba68eca2011-01-05 17:40:24 +0000440ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
441 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000442 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000443 "Only template template arguments can be pack expansions here");
444 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
445 "Template template argument pack expansion without packs");
446 ParsedTemplateArgument Result(*this);
447 Result.EllipsisLoc = EllipsisLoc;
448 return Result;
449}
450
Douglas Gregor788cd062009-11-11 01:00:40 +0000451static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
452 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000453
Douglas Gregor788cd062009-11-11 01:00:40 +0000454 switch (Arg.getKind()) {
455 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000456 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000457 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000458 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000459 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000460 return TemplateArgumentLoc(TemplateArgument(T), DI);
461 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000462
Douglas Gregor788cd062009-11-11 01:00:40 +0000463 case ParsedTemplateArgument::NonType: {
464 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
465 return TemplateArgumentLoc(TemplateArgument(E), E);
466 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000467
Douglas Gregor788cd062009-11-11 01:00:40 +0000468 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000469 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000470 TemplateArgument TArg;
471 if (Arg.getEllipsisLoc().isValid())
472 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
473 else
474 TArg = Template;
475 return TemplateArgumentLoc(TArg,
Douglas Gregor788cd062009-11-11 01:00:40 +0000476 Arg.getScopeSpec().getRange(),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000477 Arg.getLocation(),
478 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000479 }
480 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000481
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000482 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000483 return TemplateArgumentLoc();
484}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000485
Douglas Gregor788cd062009-11-11 01:00:40 +0000486/// \brief Translates template arguments as provided by the parser
487/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000488void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
489 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000490 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000491 TemplateArgs.addArgument(translateTemplateArgument(*this,
492 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000493}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000494
Douglas Gregor72c3f312008-12-05 18:15:24 +0000495/// ActOnTypeParameter - Called when a C++ template type parameter
496/// (e.g., "typename T") has been parsed. Typename specifies whether
497/// the keyword "typename" was used to declare the type parameter
498/// (otherwise, "class" was used), and KeyLoc is the location of the
499/// "class" or "typename" keyword. ParamName is the name of the
500/// parameter (NULL indicates an unnamed template parameter) and
Douglas Gregorefed5c82010-06-16 15:23:05 +0000501/// ParamName is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000502/// If the type parameter has a default argument, it will be added
503/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000504Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
505 SourceLocation EllipsisLoc,
506 SourceLocation KeyLoc,
507 IdentifierInfo *ParamName,
508 SourceLocation ParamNameLoc,
509 unsigned Depth, unsigned Position,
510 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000511 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000512 assert(S->isTemplateParamScope() &&
513 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000514 bool Invalid = false;
515
516 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000517 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000518 LookupOrdinaryName,
519 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000520 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000521 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000522 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000523 }
524
Douglas Gregorddc29e12009-02-06 22:42:48 +0000525 SourceLocation Loc = ParamNameLoc;
526 if (!ParamName)
527 Loc = KeyLoc;
528
Douglas Gregor72c3f312008-12-05 18:15:24 +0000529 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000530 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
531 Loc, Depth, Position, ParamName, Typename,
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000532 Ellipsis);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000533 if (Invalid)
534 Param->setInvalidDecl();
535
536 if (ParamName) {
537 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000538 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000539 IdResolver.AddDecl(Param);
540 }
541
Douglas Gregor61c4d282011-01-05 15:48:55 +0000542 // C++0x [temp.param]p9:
543 // A default template-argument may be specified for any kind of
544 // template-parameter that is not a template parameter pack.
545 if (DefaultArg && Ellipsis) {
546 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
547 DefaultArg = ParsedType();
548 }
549
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000550 // Handle the default argument, if provided.
551 if (DefaultArg) {
552 TypeSourceInfo *DefaultTInfo;
553 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000554
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000555 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000556
Douglas Gregor6f526752010-12-16 08:48:57 +0000557 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000558 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000559 UPPC_DefaultArgument))
560 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000561
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000562 // Check the template argument itself.
563 if (CheckTemplateArgument(Param, DefaultTInfo)) {
564 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000565 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000566 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000567
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000568 Param->setDefaultArgument(DefaultTInfo, false);
569 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000570
John McCalld226f652010-08-21 09:40:31 +0000571 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000572}
573
Douglas Gregor2943aed2009-03-03 04:44:36 +0000574/// \brief Check that the type of a non-type template parameter is
575/// well-formed.
576///
577/// \returns the (possibly-promoted) parameter type if valid;
578/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000579QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000580Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000581 // We don't allow variably-modified types as the type of non-type template
582 // parameters.
583 if (T->isVariablyModifiedType()) {
584 Diag(Loc, diag::err_variably_modified_nontype_template_param)
585 << T;
586 return QualType();
587 }
588
Douglas Gregor2943aed2009-03-03 04:44:36 +0000589 // C++ [temp.param]p4:
590 //
591 // A non-type template-parameter shall have one of the following
592 // (optionally cv-qualified) types:
593 //
594 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000595 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000596 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000597 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000598 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000599 T->isReferenceType() ||
600 // -- pointer to member.
601 T->isMemberPointerType() ||
602 // If T is a dependent type, we can't do the check now, so we
603 // assume that it is well-formed.
604 T->isDependentType())
605 return T;
606 // C++ [temp.param]p8:
607 //
608 // A non-type template-parameter of type "array of T" or
609 // "function returning T" is adjusted to be of type "pointer to
610 // T" or "pointer to function returning T", respectively.
611 else if (T->isArrayType())
612 // FIXME: Keep the type prior to promotion?
613 return Context.getArrayDecayedType(T);
614 else if (T->isFunctionType())
615 // FIXME: Keep the type prior to promotion?
616 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000617
Douglas Gregor2943aed2009-03-03 04:44:36 +0000618 Diag(Loc, diag::err_template_nontype_parm_bad_type)
619 << T;
620
621 return QualType();
622}
623
John McCalld226f652010-08-21 09:40:31 +0000624Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
625 unsigned Depth,
626 unsigned Position,
627 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000628 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000629 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
630 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000631
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000632 assert(S->isTemplateParamScope() &&
633 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000634 bool Invalid = false;
635
636 IdentifierInfo *ParamName = D.getIdentifier();
637 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000638 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000639 LookupOrdinaryName,
640 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000641 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000642 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000643 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000644 }
645
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000646 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
647 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000648 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000649 Invalid = true;
650 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000651
Douglas Gregor10738d32010-12-23 23:51:58 +0000652 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000653 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000654 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
655 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000656 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000657 IsParameterPack, TInfo);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000658 if (Invalid)
659 Param->setInvalidDecl();
660
661 if (D.getIdentifier()) {
662 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000663 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000664 IdResolver.AddDecl(Param);
665 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000666
Douglas Gregor61c4d282011-01-05 15:48:55 +0000667 // C++0x [temp.param]p9:
668 // A default template-argument may be specified for any kind of
669 // template-parameter that is not a template parameter pack.
670 if (Default && IsParameterPack) {
671 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
672 Default = 0;
673 }
674
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000675 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000676 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000677 // Check for unexpanded parameter packs.
678 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
679 return Param;
680
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000681 TemplateArgument Converted;
682 if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) {
683 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000684 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000685 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000686
John McCall9ae2f072010-08-23 23:25:46 +0000687 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000688 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000689
John McCalld226f652010-08-21 09:40:31 +0000690 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000691}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000692
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000693/// ActOnTemplateTemplateParameter - Called when a C++ template template
694/// parameter (e.g. T in template <template <typename> class T> class array)
695/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000696Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
697 SourceLocation TmpLoc,
698 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000699 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000700 IdentifierInfo *Name,
701 SourceLocation NameLoc,
702 unsigned Depth,
703 unsigned Position,
704 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000705 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000706 assert(S->isTemplateParamScope() &&
707 "Template template parameter not in template parameter scope!");
708
709 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000710 bool IsParameterPack = EllipsisLoc.isValid();
711 // FIXME: Pack-ness is dropped
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000712 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000713 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000714 NameLoc.isInvalid()? TmpLoc : NameLoc,
715 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000716 Name, Params);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000717
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000718 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000719 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000720 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000721 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000722 IdResolver.AddDecl(Param);
723 }
724
Douglas Gregor6f526752010-12-16 08:48:57 +0000725 if (Params->size() == 0) {
726 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
727 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
728 Param->setInvalidDecl();
729 }
730
Douglas Gregor61c4d282011-01-05 15:48:55 +0000731 // C++0x [temp.param]p9:
732 // A default template-argument may be specified for any kind of
733 // template-parameter that is not a template parameter pack.
734 if (IsParameterPack && !Default.isInvalid()) {
735 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
736 Default = ParsedTemplateArgument();
737 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000738
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000739 if (!Default.isInvalid()) {
740 // Check only that we have a template template argument. We don't want to
741 // try to check well-formedness now, because our template template parameter
742 // might have dependent types in its template parameters, which we wouldn't
743 // be able to match now.
744 //
745 // If none of the template template parameter's template arguments mention
746 // other template parameters, we could actually perform more checking here.
747 // However, it isn't worth doing.
748 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
749 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
750 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
751 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000752 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000753 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000754
Douglas Gregor6f526752010-12-16 08:48:57 +0000755 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000756 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000757 DefaultArg.getArgument().getAsTemplate(),
758 UPPC_DefaultArgument))
759 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000760
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000761 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000762 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000763
John McCalld226f652010-08-21 09:40:31 +0000764 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000765}
766
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000767/// ActOnTemplateParameterList - Builds a TemplateParameterList that
768/// contains the template parameters in Params/NumParams.
769Sema::TemplateParamsTy *
770Sema::ActOnTemplateParameterList(unsigned Depth,
771 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000772 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000773 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000774 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000775 SourceLocation RAngleLoc) {
776 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000777 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000778
Douglas Gregorddc29e12009-02-06 22:42:48 +0000779 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000780 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000781 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000782}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000783
John McCallb6217662010-03-15 10:12:16 +0000784static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
785 if (SS.isSet())
786 T->setQualifierInfo(static_cast<NestedNameSpecifier*>(SS.getScopeRep()),
787 SS.getRange());
788}
789
John McCallf312b1e2010-08-26 23:41:50 +0000790DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000791Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000792 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000793 IdentifierInfo *Name, SourceLocation NameLoc,
794 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000795 TemplateParameterList *TemplateParams,
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000796 AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000797 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000798 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000799 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000800 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000801
802 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000803 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000804 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000805
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000806 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
807 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000808
809 // There is no such thing as an unnamed class template.
810 if (!Name) {
811 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000812 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000813 }
814
815 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000816 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000817 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000818 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000819 if (SS.isNotEmpty() && !SS.isInvalid()) {
820 SemanticContext = computeDeclContext(SS, true);
821 if (!SemanticContext) {
822 // FIXME: Produce a reasonable diagnostic here
823 return true;
824 }
Mike Stump1eb44332009-09-09 15:08:12 +0000825
John McCall77bb1aa2010-05-01 00:40:08 +0000826 if (RequireCompleteDeclContext(SS, SemanticContext))
827 return true;
828
John McCalla24dc2e2009-11-17 02:14:36 +0000829 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000830 } else {
831 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000832 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000833 }
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Douglas Gregor57265e32010-04-12 16:00:01 +0000835 if (Previous.isAmbiguous())
836 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000837
Douglas Gregorddc29e12009-02-06 22:42:48 +0000838 NamedDecl *PrevDecl = 0;
839 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000840 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000841
Douglas Gregorddc29e12009-02-06 22:42:48 +0000842 // If there is a previous declaration with the same name, check
843 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000844 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000845 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000846
847 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000848 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000849 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000850 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000851 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
852 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000853 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000854 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
855 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
856 PrevClassTemplate
857 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
858 ->getSpecializedTemplate();
859 }
860 }
861
John McCall65c49462009-12-18 11:25:59 +0000862 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000863 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000864 // [...] When looking for a prior declaration of a class or a function
865 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000866 // function is neither a qualified name nor a template-id, scopes outside
867 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000868 if (!SS.isSet()) {
869 DeclContext *OutermostContext = CurContext;
870 while (!OutermostContext->isFileContext())
871 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000872
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000873 if (PrevDecl &&
874 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
875 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
876 SemanticContext = PrevDecl->getDeclContext();
877 } else {
878 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000879 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000880 // declaration.
881 PrevDecl = PrevClassTemplate = 0;
882 SemanticContext = OutermostContext;
883 }
John McCalle129d442009-12-17 23:21:11 +0000884 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000885
John McCalle129d442009-12-17 23:21:11 +0000886 if (CurContext->isDependentContext()) {
887 // If this is a dependent context, we don't want to link the friend
888 // class template to the template in scope, because that would perform
889 // checking of the template parameter lists that can't be performed
890 // until the outer context is instantiated.
891 PrevDecl = PrevClassTemplate = 0;
892 }
893 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
894 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000895
Douglas Gregorddc29e12009-02-06 22:42:48 +0000896 if (PrevClassTemplate) {
897 // Ensure that the template parameter lists are compatible.
898 if (!TemplateParameterListsAreEqual(TemplateParams,
899 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000900 /*Complain=*/true,
901 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000902 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000903
904 // C++ [temp.class]p4:
905 // In a redeclaration, partial specialization, explicit
906 // specialization or explicit instantiation of a class template,
907 // the class-key shall agree in kind with the original class
908 // template declaration (7.1.5.3).
909 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000910 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000911 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000912 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000913 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000914 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000915 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000916 }
917
Douglas Gregorddc29e12009-02-06 22:42:48 +0000918 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000919 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000920 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000921 Diag(NameLoc, diag::err_redefinition) << Name;
922 Diag(Def->getLocation(), diag::note_previous_definition);
923 // FIXME: Would it make sense to try to "forget" the previous
924 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000925 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000926 }
927 }
928 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
929 // Maybe we will complain about the shadowed template parameter.
930 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
931 // Just pretend that we didn't see the previous declaration.
932 PrevDecl = 0;
933 } else if (PrevDecl) {
934 // C++ [temp]p5:
935 // A class template shall not have the same name as any other
936 // template, class, function, object, enumeration, enumerator,
937 // namespace, or type in the same scope (3.3), except as specified
938 // in (14.5.4).
939 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
940 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000941 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000942 }
943
Douglas Gregord684b002009-02-10 19:49:53 +0000944 // Check the template parameter list of this declaration, possibly
945 // merging in the template parameter list from the previous class
946 // template declaration.
947 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000948 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000949 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000950 SemanticContext->isRecord() &&
951 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000952 ? TPC_ClassTemplateMember
953 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000954 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Douglas Gregor57265e32010-04-12 16:00:01 +0000956 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000957 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000958 // template out-of-line.
959 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
960 !(TUK == TUK_Friend && CurContext->isDependentContext()))
961 Diag(NameLoc, diag::err_member_def_does_not_match)
962 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000963 }
964
Mike Stump1eb44332009-09-09 15:08:12 +0000965 CXXRecordDecl *NewClass =
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000966 CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000967 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000968 PrevClassTemplate->getTemplatedDecl() : 0,
969 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000970 SetNestedNameSpecifier(NewClass, SS);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000971
972 ClassTemplateDecl *NewTemplate
973 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
974 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000975 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000976 NewClass->setDescribedClassTemplate(NewTemplate);
977
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000978 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000979 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000980 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000981 assert(T->isDependentType() && "Class template type is not dependent?");
982 (void)T;
983
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000984 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000985 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000986 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000987 PrevClassTemplate->getInstantiatedFromMemberTemplate())
988 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000989
Anders Carlsson4cbe82c2009-03-26 01:24:28 +0000990 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +0000991 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +0000992 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Douglas Gregorddc29e12009-02-06 22:42:48 +0000994 // Set the lexical context of these templates
995 NewClass->setLexicalDeclContext(CurContext);
996 NewTemplate->setLexicalDeclContext(CurContext);
997
John McCall0f434ec2009-07-31 02:45:11 +0000998 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +0000999 NewClass->startDefinition();
1000
1001 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001002 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001003
John McCall05b23ea2009-09-14 21:59:20 +00001004 if (TUK != TUK_Friend)
1005 PushOnScopeChains(NewTemplate, S);
1006 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001007 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001008 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001009 NewClass->setAccess(PrevClassTemplate->getAccess());
1010 }
John McCall05b23ea2009-09-14 21:59:20 +00001011
Douglas Gregord85bea22009-09-26 06:47:28 +00001012 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1013 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001014
John McCall05b23ea2009-09-14 21:59:20 +00001015 // Friend templates are visible in fairly strange ways.
1016 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001017 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001018 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1019 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1020 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001021 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001022 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001023
Douglas Gregord85bea22009-09-26 06:47:28 +00001024 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1025 NewClass->getLocation(),
1026 NewTemplate,
1027 /*FIXME:*/NewClass->getLocation());
1028 Friend->setAccess(AS_public);
1029 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001030 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001031
Douglas Gregord684b002009-02-10 19:49:53 +00001032 if (Invalid) {
1033 NewTemplate->setInvalidDecl();
1034 NewClass->setInvalidDecl();
1035 }
John McCalld226f652010-08-21 09:40:31 +00001036 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001037}
1038
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001039/// \brief Diagnose the presence of a default template argument on a
1040/// template parameter, which is ill-formed in certain contexts.
1041///
1042/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001043static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001044 Sema::TemplateParamListContext TPC,
1045 SourceLocation ParamLoc,
1046 SourceRange DefArgRange) {
1047 switch (TPC) {
1048 case Sema::TPC_ClassTemplate:
1049 return false;
1050
1051 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001052 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001053 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001054 // A default template-argument shall not be specified in a
1055 // function template declaration or a function template
1056 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001057 // If a friend function template declaration specifies a default
1058 // template-argument, that declaration shall be a definition and shall be
1059 // the only declaration of the function template in the translation unit.
1060 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001061 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001062 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001063 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001064 << DefArgRange;
1065 return false;
1066
1067 case Sema::TPC_ClassTemplateMember:
1068 // C++0x [temp.param]p9:
1069 // A default template-argument shall not be specified in the
1070 // template-parameter-lists of the definition of a member of a
1071 // class template that appears outside of the member's class.
1072 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1073 << DefArgRange;
1074 return true;
1075
1076 case Sema::TPC_FriendFunctionTemplate:
1077 // C++ [temp.param]p9:
1078 // A default template-argument shall not be specified in a
1079 // friend template declaration.
1080 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1081 << DefArgRange;
1082 return true;
1083
1084 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1085 // for friend function templates if there is only a single
1086 // declaration (and it is a definition). Strange!
1087 }
1088
1089 return false;
1090}
1091
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001092/// \brief Check for unexpanded parameter packs within the template parameters
1093/// of a template template parameter, recursively.
1094bool DiagnoseUnexpandedParameterPacks(Sema &S, TemplateTemplateParmDecl *TTP){
1095 TemplateParameterList *Params = TTP->getTemplateParameters();
1096 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1097 NamedDecl *P = Params->getParam(I);
1098 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001099 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001100 NTTP->getTypeSourceInfo(),
1101 Sema::UPPC_NonTypeTemplateParameterType))
1102 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001103
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001104 continue;
1105 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001106
1107 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001108 = dyn_cast<TemplateTemplateParmDecl>(P))
1109 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1110 return true;
1111 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001112
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001113 return false;
1114}
1115
Douglas Gregord684b002009-02-10 19:49:53 +00001116/// \brief Checks the validity of a template parameter list, possibly
1117/// considering the template parameter list from a previous
1118/// declaration.
1119///
1120/// If an "old" template parameter list is provided, it must be
1121/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1122/// template parameter list.
1123///
1124/// \param NewParams Template parameter list for a new template
1125/// declaration. This template parameter list will be updated with any
1126/// default arguments that are carried through from the previous
1127/// template parameter list.
1128///
1129/// \param OldParams If provided, template parameter list from a
1130/// previous declaration of the same template. Default template
1131/// arguments will be merged from the old template parameter list to
1132/// the new template parameter list.
1133///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001134/// \param TPC Describes the context in which we are checking the given
1135/// template parameter list.
1136///
Douglas Gregord684b002009-02-10 19:49:53 +00001137/// \returns true if an error occurred, false otherwise.
1138bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001139 TemplateParameterList *OldParams,
1140 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001141 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Douglas Gregord684b002009-02-10 19:49:53 +00001143 // C++ [temp.param]p10:
1144 // The set of default template-arguments available for use with a
1145 // template declaration or definition is obtained by merging the
1146 // default arguments from the definition (if in scope) and all
1147 // declarations in scope in the same way default function
1148 // arguments are (8.3.6).
1149 bool SawDefaultArgument = false;
1150 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001151
Anders Carlsson49d25572009-06-12 23:20:15 +00001152 bool SawParameterPack = false;
1153 SourceLocation ParameterPackLoc;
1154
Mike Stump1a35fde2009-02-11 23:03:27 +00001155 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001156 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001157 if (OldParams)
1158 OldParam = OldParams->begin();
1159
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001160 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001161 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1162 NewParamEnd = NewParams->end();
1163 NewParam != NewParamEnd; ++NewParam) {
1164 // Variables used to diagnose redundant default arguments
1165 bool RedundantDefaultArg = false;
1166 SourceLocation OldDefaultLoc;
1167 SourceLocation NewDefaultLoc;
1168
1169 // Variables used to diagnose missing default arguments
1170 bool MissingDefaultArg = false;
1171
Anders Carlsson49d25572009-06-12 23:20:15 +00001172 // C++0x [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001173 // If a template parameter of a primary class template is a template
Douglas Gregor1ed64762011-01-05 16:19:19 +00001174 // parameter pack, it shall be the last template parameter.
1175 if (SawParameterPack && TPC == TPC_ClassTemplate) {
Mike Stump1eb44332009-09-09 15:08:12 +00001176 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001177 diag::err_template_param_pack_must_be_last_template_parameter);
1178 Invalid = true;
1179 }
1180
Douglas Gregord684b002009-02-10 19:49:53 +00001181 if (TemplateTypeParmDecl *NewTypeParm
1182 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001183 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001184 if (NewTypeParm->hasDefaultArgument() &&
1185 DiagnoseDefaultTemplateArgument(*this, TPC,
1186 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001187 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001188 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001189 NewTypeParm->removeDefaultArgument();
1190
1191 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001192 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001193 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Anders Carlsson49d25572009-06-12 23:20:15 +00001195 if (NewTypeParm->isParameterPack()) {
1196 assert(!NewTypeParm->hasDefaultArgument() &&
1197 "Parameter packs can't have a default argument!");
1198 SawParameterPack = true;
1199 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001200 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001201 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001202 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1203 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1204 SawDefaultArgument = true;
1205 RedundantDefaultArg = true;
1206 PreviousDefaultArgLoc = NewDefaultLoc;
1207 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1208 // Merge the default argument from the old declaration to the
1209 // new declaration.
1210 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001211 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001212 true);
1213 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1214 } else if (NewTypeParm->hasDefaultArgument()) {
1215 SawDefaultArgument = true;
1216 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1217 } else if (SawDefaultArgument)
1218 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001219 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001220 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001221 // Check for unexpanded parameter packs.
1222 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001223 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001224 UPPC_NonTypeTemplateParameterType)) {
1225 Invalid = true;
1226 continue;
1227 }
1228
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001229 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001230 if (NewNonTypeParm->hasDefaultArgument() &&
1231 DiagnoseDefaultTemplateArgument(*this, TPC,
1232 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001233 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001234 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001235 }
1236
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001237 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001238 NonTypeTemplateParmDecl *OldNonTypeParm
1239 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001240 if (NewNonTypeParm->isParameterPack()) {
1241 assert(!NewNonTypeParm->hasDefaultArgument() &&
1242 "Parameter packs can't have a default argument!");
1243 SawParameterPack = true;
1244 ParameterPackLoc = NewNonTypeParm->getLocation();
1245 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001246 NewNonTypeParm->hasDefaultArgument()) {
1247 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1248 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1249 SawDefaultArgument = true;
1250 RedundantDefaultArg = true;
1251 PreviousDefaultArgLoc = NewDefaultLoc;
1252 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1253 // Merge the default argument from the old declaration to the
1254 // new declaration.
1255 SawDefaultArgument = true;
1256 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001257 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001258 // parameter.
1259 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001260 OldNonTypeParm->getDefaultArgument(),
1261 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001262 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1263 } else if (NewNonTypeParm->hasDefaultArgument()) {
1264 SawDefaultArgument = true;
1265 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1266 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001267 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001268 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001269 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001270 TemplateTemplateParmDecl *NewTemplateParm
1271 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001272
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001273 // Check for unexpanded parameter packs, recursively.
1274 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1275 Invalid = true;
1276 continue;
1277 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001278
1279 if (NewTemplateParm->hasDefaultArgument() &&
1280 DiagnoseDefaultTemplateArgument(*this, TPC,
1281 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001282 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001283 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001284
1285 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001286 TemplateTemplateParmDecl *OldTemplateParm
1287 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001288 if (NewTemplateParm->isParameterPack()) {
1289 assert(!NewTemplateParm->hasDefaultArgument() &&
1290 "Parameter packs can't have a default argument!");
1291 SawParameterPack = true;
1292 ParameterPackLoc = NewTemplateParm->getLocation();
1293 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001294 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001295 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1296 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001297 SawDefaultArgument = true;
1298 RedundantDefaultArg = true;
1299 PreviousDefaultArgLoc = NewDefaultLoc;
1300 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1301 // Merge the default argument from the old declaration to the
1302 // new declaration.
1303 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001304 // FIXME: We need to create a new kind of "default argument" expression
1305 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001306 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001307 OldTemplateParm->getDefaultArgument(),
1308 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001309 PreviousDefaultArgLoc
1310 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001311 } else if (NewTemplateParm->hasDefaultArgument()) {
1312 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001313 PreviousDefaultArgLoc
1314 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001315 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001316 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001317 }
1318
1319 if (RedundantDefaultArg) {
1320 // C++ [temp.param]p12:
1321 // A template-parameter shall not be given default arguments
1322 // by two different declarations in the same scope.
1323 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1324 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1325 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001326 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001327 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001328 // If a template-parameter of a class template has a default
1329 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001330 // have a default template-argument supplied or be a template parameter
1331 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001332 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001333 diag::err_template_param_default_arg_missing);
1334 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1335 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001336 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001337 }
1338
1339 // If we have an old template parameter list that we're merging
1340 // in, move on to the next parameter.
1341 if (OldParams)
1342 ++OldParam;
1343 }
1344
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001345 // We were missing some default arguments at the end of the list, so remove
1346 // all of the default arguments.
1347 if (RemoveDefaultArguments) {
1348 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1349 NewParamEnd = NewParams->end();
1350 NewParam != NewParamEnd; ++NewParam) {
1351 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1352 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001353 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001354 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1355 NTTP->removeDefaultArgument();
1356 else
1357 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1358 }
1359 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001360
Douglas Gregord684b002009-02-10 19:49:53 +00001361 return Invalid;
1362}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001363
John McCall4e2cbb22010-10-20 05:44:58 +00001364namespace {
1365
1366/// A class which looks for a use of a certain level of template
1367/// parameter.
1368struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1369 typedef RecursiveASTVisitor<DependencyChecker> super;
1370
1371 unsigned Depth;
1372 bool Match;
1373
1374 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1375 NamedDecl *ND = Params->getParam(0);
1376 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1377 Depth = PD->getDepth();
1378 } else if (NonTypeTemplateParmDecl *PD =
1379 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1380 Depth = PD->getDepth();
1381 } else {
1382 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1383 }
1384 }
1385
1386 bool Matches(unsigned ParmDepth) {
1387 if (ParmDepth >= Depth) {
1388 Match = true;
1389 return true;
1390 }
1391 return false;
1392 }
1393
1394 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1395 return !Matches(T->getDepth());
1396 }
1397
1398 bool TraverseTemplateName(TemplateName N) {
1399 if (TemplateTemplateParmDecl *PD =
1400 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1401 if (Matches(PD->getDepth())) return false;
1402 return super::TraverseTemplateName(N);
1403 }
1404
1405 bool VisitDeclRefExpr(DeclRefExpr *E) {
1406 if (NonTypeTemplateParmDecl *PD =
1407 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1408 if (PD->getDepth() == Depth) {
1409 Match = true;
1410 return false;
1411 }
1412 }
1413 return super::VisitDeclRefExpr(E);
1414 }
1415};
1416}
1417
1418/// Determines whether a template-id depends on the given parameter
1419/// list.
1420static bool
1421DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId,
1422 TemplateParameterList *Params) {
1423 DependencyChecker Checker(Params);
1424 Checker.TraverseType(QualType(TemplateId, 0));
1425 return Checker.Match;
1426}
1427
Mike Stump1eb44332009-09-09 15:08:12 +00001428/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001429/// specifier, returning the template parameter list that applies to the
1430/// name.
1431///
1432/// \param DeclStartLoc the start of the declaration that has a scope
1433/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001434///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001435/// \param SS the scope specifier that will be matched to the given template
1436/// parameter lists. This scope specifier precedes a qualified name that is
1437/// being declared.
1438///
1439/// \param ParamLists the template parameter lists, from the outermost to the
1440/// innermost template parameter lists.
1441///
1442/// \param NumParamLists the number of template parameter lists in ParamLists.
1443///
John McCall77e8b112010-04-13 20:37:33 +00001444/// \param IsFriend Whether to apply the slightly different rules for
1445/// matching template parameters to scope specifiers in friend
1446/// declarations.
1447///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001448/// \param IsExplicitSpecialization will be set true if the entity being
1449/// declared is an explicit specialization, false otherwise.
1450///
Mike Stump1eb44332009-09-09 15:08:12 +00001451/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001452/// name that is preceded by the scope specifier @p SS. This template
1453/// parameter list may be have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001454/// template) or may have no template parameters (if we're declaring a
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001455/// template specialization), or may be NULL (if we were's declaring isn't
1456/// itself a template).
1457TemplateParameterList *
1458Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
1459 const CXXScopeSpec &SS,
1460 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001461 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001462 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001463 bool &IsExplicitSpecialization,
1464 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001465 IsExplicitSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001466
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001467 // Find the template-ids that occur within the nested-name-specifier. These
1468 // template-ids will match up with the template parameter lists.
1469 llvm::SmallVector<const TemplateSpecializationType *, 4>
1470 TemplateIdsInSpecifier;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001471 llvm::SmallVector<ClassTemplateSpecializationDecl *, 4>
1472 ExplicitSpecializationsInSpecifier;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001473 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
1474 NNS; NNS = NNS->getPrefix()) {
John McCall4b2b02b2009-12-15 02:19:47 +00001475 const Type *T = NNS->getAsType();
1476 if (!T) break;
1477
1478 // C++0x [temp.expl.spec]p17:
1479 // A member or a member template may be nested within many
1480 // enclosing class templates. In an explicit specialization for
1481 // such a member, the member declaration shall be preceded by a
1482 // template<> for each enclosing class template that is
1483 // explicitly specialized.
Douglas Gregorfe331062010-02-13 05:23:25 +00001484 //
1485 // Following the existing practice of GNU and EDG, we allow a typedef of a
1486 // template specialization type.
John McCall49f4e1c2010-12-10 11:01:00 +00001487 while (const TypedefType *TT = dyn_cast<TypedefType>(T))
1488 T = TT->getDecl()->getUnderlyingType().getTypePtr();
John McCall4b2b02b2009-12-15 02:19:47 +00001489
Mike Stump1eb44332009-09-09 15:08:12 +00001490 if (const TemplateSpecializationType *SpecType
Douglas Gregorfe331062010-02-13 05:23:25 +00001491 = dyn_cast<TemplateSpecializationType>(T)) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001492 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
1493 if (!Template)
1494 continue; // FIXME: should this be an error? probably...
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Ted Kremenek6217b802009-07-29 21:53:49 +00001496 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001497 ClassTemplateSpecializationDecl *SpecDecl
1498 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
1499 // If the nested name specifier refers to an explicit specialization,
1500 // we don't need a template<> header.
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001501 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
1502 ExplicitSpecializationsInSpecifier.push_back(SpecDecl);
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001503 continue;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001504 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001505 }
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001507 TemplateIdsInSpecifier.push_back(SpecType);
1508 }
1509 }
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001511 // Reverse the list of template-ids in the scope specifier, so that we can
1512 // more easily match up the template-ids and the template parameter lists.
1513 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001515 SourceLocation FirstTemplateLoc = DeclStartLoc;
1516 if (NumParamLists)
1517 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001519 // Match the template-ids found in the specifier to the template parameter
1520 // lists.
John McCall4e2cbb22010-10-20 05:44:58 +00001521 unsigned ParamIdx = 0, TemplateIdx = 0;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001522 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
John McCall4e2cbb22010-10-20 05:44:58 +00001523 TemplateIdx != NumTemplateIds; ++TemplateIdx) {
1524 const TemplateSpecializationType *TemplateId
1525 = TemplateIdsInSpecifier[TemplateIdx];
Douglas Gregorb88e8882009-07-30 17:40:51 +00001526 bool DependentTemplateId = TemplateId->isDependentType();
John McCall4e2cbb22010-10-20 05:44:58 +00001527
1528 // In friend declarations we can have template-ids which don't
1529 // depend on the corresponding template parameter lists. But
1530 // assume that empty parameter lists are supposed to match this
1531 // template-id.
1532 if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) {
1533 if (!DependentTemplateId ||
1534 !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx]))
1535 continue;
1536 }
1537
1538 if (ParamIdx >= NumParamLists) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001539 // We have a template-id without a corresponding template parameter
1540 // list.
John McCall77e8b112010-04-13 20:37:33 +00001541
1542 // ...which is fine if this is a friend declaration.
1543 if (IsFriend) {
1544 IsExplicitSpecialization = true;
1545 break;
1546 }
1547
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001548 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +00001549 // FIXME: the location information here isn't great.
1550 Diag(SS.getRange().getBegin(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001551 diag::err_template_spec_needs_template_parameters)
John McCall4e2cbb22010-10-20 05:44:58 +00001552 << QualType(TemplateId, 0)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001553 << SS.getRange();
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001554 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001555 } else {
1556 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
1557 << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +00001558 << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001559 IsExplicitSpecialization = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001560 }
1561 return 0;
1562 }
Mike Stump1eb44332009-09-09 15:08:12 +00001563
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001564 // Check the template parameter list against its corresponding template-id.
Douglas Gregorb88e8882009-07-30 17:40:51 +00001565 if (DependentTemplateId) {
John McCall31f17ec2010-04-27 00:57:59 +00001566 TemplateParameterList *ExpectedTemplateParams = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00001567
John McCall31f17ec2010-04-27 00:57:59 +00001568 // Are there cases in (e.g.) friends where this won't match?
1569 if (const InjectedClassNameType *Injected
1570 = TemplateId->getAs<InjectedClassNameType>()) {
1571 CXXRecordDecl *Record = Injected->getDecl();
1572 if (ClassTemplatePartialSpecializationDecl *Partial =
1573 dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
1574 ExpectedTemplateParams = Partial->getTemplateParameters();
1575 else
1576 ExpectedTemplateParams = Record->getDescribedClassTemplate()
1577 ->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001578 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001579
John McCall31f17ec2010-04-27 00:57:59 +00001580 if (ExpectedTemplateParams)
John McCall4e2cbb22010-10-20 05:44:58 +00001581 TemplateParameterListsAreEqual(ParamLists[ParamIdx],
John McCall31f17ec2010-04-27 00:57:59 +00001582 ExpectedTemplateParams,
1583 true, TPL_TemplateMatch);
1584
John McCall4e2cbb22010-10-20 05:44:58 +00001585 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1586 TPC_ClassTemplateMember);
1587 } else if (ParamLists[ParamIdx]->size() > 0)
1588 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00001589 diag::err_template_param_list_matches_nontemplate)
1590 << TemplateId
John McCall4e2cbb22010-10-20 05:44:58 +00001591 << ParamLists[ParamIdx]->getSourceRange();
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001592 else
1593 IsExplicitSpecialization = true;
John McCall4e2cbb22010-10-20 05:44:58 +00001594
1595 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001596 }
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001598 // If there were at least as many template-ids as there were template
1599 // parameter lists, then there are no template parameter lists remaining for
1600 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001601 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001602 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001604 // If there were too many template parameter lists, complain about that now.
John McCall4e2cbb22010-10-20 05:44:58 +00001605 if (ParamIdx != NumParamLists - 1) {
1606 while (ParamIdx < NumParamLists - 1) {
1607 bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0;
1608 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001609 isExplicitSpecHeader? diag::warn_template_spec_extra_headers
1610 : diag::err_template_spec_extra_headers)
John McCall4e2cbb22010-10-20 05:44:58 +00001611 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1612 ParamLists[ParamIdx]->getRAngleLoc());
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001613
1614 if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) {
1615 Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(),
1616 diag::note_explicit_template_spec_does_not_need_header)
1617 << ExplicitSpecializationsInSpecifier.back();
1618 ExplicitSpecializationsInSpecifier.pop_back();
1619 }
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001620
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001621 // We have a template parameter list with no corresponding scope, which
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001622 // means that the resulting template declaration can't be instantiated
1623 // properly (we'll end up with dependent nodes when we shouldn't).
1624 if (!isExplicitSpecHeader)
1625 Invalid = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001626
John McCall4e2cbb22010-10-20 05:44:58 +00001627 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001628 }
1629 }
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001631 // Return the last template parameter list, which corresponds to the
1632 // entity being declared.
1633 return ParamLists[NumParamLists - 1];
1634}
1635
Douglas Gregor7532dc62009-03-30 22:58:21 +00001636QualType Sema::CheckTemplateIdType(TemplateName Name,
1637 SourceLocation TemplateLoc,
John McCalld5532b62009-11-23 01:53:49 +00001638 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001639 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001640 if (!Template) {
1641 // The template name does not resolve to a template, so we just
1642 // build a dependent template-id type.
John McCalld5532b62009-11-23 01:53:49 +00001643 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001644 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001645
Douglas Gregor40808ce2009-03-09 23:48:35 +00001646 // Check that the template argument list is well-formed for this
1647 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001648 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001649 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001650 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001651 return QualType();
1652
Douglas Gregor910f8002010-11-07 23:05:16 +00001653 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001654 "Converted template argument list is too short!");
1655
1656 QualType CanonType;
1657
Douglas Gregorcaddba02009-11-12 18:38:13 +00001658 if (Name.isDependent() ||
1659 TemplateSpecializationType::anyDependentTemplateArguments(
John McCalld5532b62009-11-23 01:53:49 +00001660 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001661 // This class template specialization is a dependent
1662 // type. Therefore, its canonical type is another class template
1663 // specialization type that contains all of the converted
1664 // arguments in canonical form. This ensures that, e.g., A<T> and
1665 // A<T, T> have identical types when A is declared as:
1666 //
1667 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001668 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001669 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001670 Converted.data(),
1671 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Douglas Gregor1275ae02009-07-28 23:00:59 +00001673 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001674 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001675 // In the future, we need to teach getTemplateSpecializationType to only
1676 // build the canonical type and return that to us.
1677 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001678
1679 // This might work out to be a current instantiation, in which
1680 // case the canonical type needs to be the InjectedClassNameType.
1681 //
1682 // TODO: in theory this could be a simple hashtable lookup; most
1683 // changes to CurContext don't change the set of current
1684 // instantiations.
1685 if (isa<ClassTemplateDecl>(Template)) {
1686 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1687 // If we get out to a namespace, we're done.
1688 if (Ctx->isFileContext()) break;
1689
1690 // If this isn't a record, keep looking.
1691 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1692 if (!Record) continue;
1693
1694 // Look for one of the two cases with InjectedClassNameTypes
1695 // and check whether it's the same template.
1696 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1697 !Record->getDescribedClassTemplate())
1698 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001699
John McCall31f17ec2010-04-27 00:57:59 +00001700 // Fetch the injected class name type and check whether its
1701 // injected type is equal to the type we just built.
1702 QualType ICNT = Context.getTypeDeclType(Record);
1703 QualType Injected = cast<InjectedClassNameType>(ICNT)
1704 ->getInjectedSpecializationType();
1705
1706 if (CanonType != Injected->getCanonicalTypeInternal())
1707 continue;
1708
1709 // If so, the canonical type of this TST is the injected
1710 // class name type of the record we just found.
1711 assert(ICNT.isCanonical());
1712 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001713 break;
1714 }
1715 }
Mike Stump1eb44332009-09-09 15:08:12 +00001716 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001717 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001718 // Find the class template specialization declaration that
1719 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001720 void *InsertPos = 0;
1721 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001722 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001723 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001724 if (!Decl) {
1725 // This is the first time we have referenced this class template
1726 // specialization. Create the canonical declaration and add it to
1727 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001728 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001729 ClassTemplate->getTemplatedDecl()->getTagKind(),
1730 ClassTemplate->getDeclContext(),
1731 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001732 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001733 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001734 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001735 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001736 Decl->setLexicalDeclContext(CurContext);
1737 }
1738
1739 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00001740 assert(isa<RecordType>(CanonType) &&
1741 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00001742 }
Mike Stump1eb44332009-09-09 15:08:12 +00001743
Douglas Gregor40808ce2009-03-09 23:48:35 +00001744 // Build the fully-sugared type for this class template
1745 // specialization, which refers back to the class template
1746 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00001747 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001748}
1749
John McCallf312b1e2010-08-26 23:41:50 +00001750TypeResult
Douglas Gregor7532dc62009-03-30 22:58:21 +00001751Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001752 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001753 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00001754 SourceLocation RAngleLoc) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001755 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001756
Douglas Gregor40808ce2009-03-09 23:48:35 +00001757 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00001758 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00001759 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001760
John McCalld5532b62009-11-23 01:53:49 +00001761 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001762 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00001763
1764 if (Result.isNull())
1765 return true;
1766
John McCalla93c9342009-12-07 02:54:59 +00001767 TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result);
John McCall833ca992009-10-29 08:12:44 +00001768 TemplateSpecializationTypeLoc TL
1769 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1770 TL.setTemplateNameLoc(TemplateLoc);
1771 TL.setLAngleLoc(LAngleLoc);
1772 TL.setRAngleLoc(RAngleLoc);
1773 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1774 TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
1775
John McCallb3d87482010-08-24 05:47:05 +00001776 return CreateParsedType(Result, DI);
John McCall6b2becf2009-09-08 17:47:29 +00001777}
John McCallf1bbbb42009-09-04 01:14:41 +00001778
Craig Silverstein45ab4b52010-11-18 08:32:02 +00001779TypeResult Sema::ActOnTagTemplateIdType(CXXScopeSpec &SS,
1780 TypeResult TypeResult,
John McCallf312b1e2010-08-26 23:41:50 +00001781 TagUseKind TUK,
1782 TypeSpecifierType TagSpec,
1783 SourceLocation TagLoc) {
John McCall6b2becf2009-09-08 17:47:29 +00001784 if (TypeResult.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00001785 return ::TypeResult();
John McCallf1bbbb42009-09-04 01:14:41 +00001786
John McCalla93c9342009-12-07 02:54:59 +00001787 TypeSourceInfo *DI;
John McCall833ca992009-10-29 08:12:44 +00001788 QualType Type = GetTypeFromParser(TypeResult.get(), &DI);
John McCallf1bbbb42009-09-04 01:14:41 +00001789
John McCall6b2becf2009-09-08 17:47:29 +00001790 // Verify the tag specifier.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001791 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Mike Stump1eb44332009-09-09 15:08:12 +00001792
John McCall6b2becf2009-09-08 17:47:29 +00001793 if (const RecordType *RT = Type->getAs<RecordType>()) {
1794 RecordDecl *D = RT->getDecl();
1795
1796 IdentifierInfo *Id = D->getIdentifier();
1797 assert(Id && "templated class must have an identifier");
1798
1799 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1800 Diag(TagLoc, diag::err_use_with_wrong_tag)
John McCallc4e70192009-09-11 04:59:25 +00001801 << Type
Douglas Gregor849b2432010-03-31 17:46:05 +00001802 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00001803 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00001804 }
1805 }
1806
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001807 ElaboratedTypeKeyword Keyword
1808 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
1809 QualType ElabType = Context.getElaboratedType(Keyword, /*NNS=*/0, Type);
John McCall6b2becf2009-09-08 17:47:29 +00001810
Craig Silverstein45ab4b52010-11-18 08:32:02 +00001811 TypeSourceInfo *ElabDI = Context.CreateTypeSourceInfo(ElabType);
1812 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(ElabDI->getTypeLoc());
1813 TL.setKeywordLoc(TagLoc);
1814 TL.setQualifierRange(SS.getRange());
1815 TL.getNamedTypeLoc().initializeFullCopy(DI->getTypeLoc());
1816 return CreateParsedType(ElabType, ElabDI);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001817}
1818
John McCall60d7b3a2010-08-24 06:29:42 +00001819ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +00001820 LookupResult &R,
1821 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00001822 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001823 // FIXME: Can we do any checking at this point? I guess we could check the
1824 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00001825 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001826 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001827 // foo<int> could identify a single function unambiguously
1828 // This approach does NOT work, since f<int>(1);
1829 // gets resolved prior to resorting to overload resolution
1830 // i.e., template<class T> void f(double);
1831 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00001832
1833 // These should be filtered out by our callers.
1834 assert(!R.empty() && "empty lookup results when building templateid");
1835 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
1836
1837 NestedNameSpecifier *Qualifier = 0;
1838 SourceRange QualifierRange;
1839 if (SS.isSet()) {
1840 Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
1841 QualifierRange = SS.getRange();
Douglas Gregora9e29aa2009-10-22 07:19:14 +00001842 }
John McCallc373d482010-01-27 01:50:18 +00001843
1844 // We don't want lookup warnings at this point.
1845 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001846
John McCallf7a1a742009-11-24 19:00:30 +00001847 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001848 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
John McCallf7a1a742009-11-24 19:00:30 +00001849 Qualifier, QualifierRange,
Abramo Bagnara25777432010-08-11 22:01:17 +00001850 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001851 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001852 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00001853
1854 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001855}
1856
John McCallf7a1a742009-11-24 19:00:30 +00001857// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00001858ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00001859Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00001860 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00001861 const TemplateArgumentListInfo &TemplateArgs) {
1862 DeclContext *DC;
1863 if (!(DC = computeDeclContext(SS, false)) ||
1864 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00001865 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00001866 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001868 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00001869 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001870 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
1871 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00001872
John McCallf7a1a742009-11-24 19:00:30 +00001873 if (R.isAmbiguous())
1874 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001875
John McCallf7a1a742009-11-24 19:00:30 +00001876 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001877 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
1878 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001879 return ExprError();
1880 }
1881
1882 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001883 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
1884 << (NestedNameSpecifier*) SS.getScopeRep()
1885 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001886 Diag(Temp->getLocation(), diag::note_referenced_class_template);
1887 return ExprError();
1888 }
1889
1890 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001891}
1892
Douglas Gregorc45c2322009-03-31 00:43:58 +00001893/// \brief Form a dependent template name.
1894///
1895/// This action forms a dependent template name given the template
1896/// name and its (presumably dependent) scope specifier. For
1897/// example, given "MetaFun::template apply", the scope specifier \p
1898/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
1899/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001900TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001901 SourceLocation TemplateKWLoc,
1902 CXXScopeSpec &SS,
1903 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00001904 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001905 bool EnteringContext,
1906 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00001907 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
1908 !getLangOptions().CPlusPlus0x)
1909 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001910 << FixItHint::CreateRemoval(TemplateKWLoc);
1911
Douglas Gregor0707bc52010-01-19 16:01:07 +00001912 DeclContext *LookupCtx = 0;
1913 if (SS.isSet())
1914 LookupCtx = computeDeclContext(SS, EnteringContext);
1915 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00001916 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00001917 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00001918 // C++0x [temp.names]p5:
1919 // If a name prefixed by the keyword template is not the name of
1920 // a template, the program is ill-formed. [Note: the keyword
1921 // template may not be applied to non-template members of class
1922 // templates. -end note ] [ Note: as is the case with the
1923 // typename prefix, the template prefix is allowed in cases
1924 // where it is not strictly necessary; i.e., when the
1925 // nested-name-specifier or the expression on the left of the ->
1926 // or . is not dependent on a template-parameter, or the use
1927 // does not appear in the scope of a template. -end note]
1928 //
1929 // Note: C++03 was more strict here, because it banned the use of
1930 // the "template" keyword prior to a template-name that was not a
1931 // dependent name. C++ DR468 relaxed this requirement (the
1932 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00001933 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001934 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001935 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
1936 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001937 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00001938 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
1939 isa<CXXRecordDecl>(LookupCtx) &&
1940 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00001941 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00001942 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001943 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00001944 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00001945 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00001946 << Name.getSourceRange()
1947 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00001948 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00001949 } else {
1950 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00001951 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00001952 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00001953 }
1954
Mike Stump1eb44332009-09-09 15:08:12 +00001955 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001956 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001957
Douglas Gregor014e88d2009-11-03 23:16:33 +00001958 switch (Name.getKind()) {
1959 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001960 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001961 Name.Identifier));
1962 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001963
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001964 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00001965 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001966 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00001967 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00001968
1969 case UnqualifiedId::IK_LiteralOperatorId:
1970 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
1971
Douglas Gregor014e88d2009-11-03 23:16:33 +00001972 default:
1973 break;
1974 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001975
1976 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00001977 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00001978 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00001979 << Name.getSourceRange()
1980 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00001981 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00001982}
1983
Mike Stump1eb44332009-09-09 15:08:12 +00001984bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00001985 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00001986 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00001987 const TemplateArgument &Arg = AL.getArgument();
1988
Anders Carlsson436b1562009-06-13 00:33:33 +00001989 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00001990 switch(Arg.getKind()) {
1991 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00001992 // C++ [temp.arg.type]p1:
1993 // A template-argument for a template-parameter which is a
1994 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00001995 break;
1996 case TemplateArgument::Template: {
1997 // We have a template type parameter but the template argument
1998 // is a template without any arguments.
1999 SourceRange SR = AL.getSourceRange();
2000 TemplateName Name = Arg.getAsTemplate();
2001 Diag(SR.getBegin(), diag::err_template_missing_args)
2002 << Name << SR;
2003 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2004 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002005
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002006 return true;
2007 }
2008 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002009 // We have a template type parameter but the template argument
2010 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002011 SourceRange SR = AL.getSourceRange();
2012 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002013 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002014
Anders Carlsson436b1562009-06-13 00:33:33 +00002015 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002016 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002017 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002018
John McCalla93c9342009-12-07 02:54:59 +00002019 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002020 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002021
Anders Carlsson436b1562009-06-13 00:33:33 +00002022 // Add the converted template type argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002023 Converted.push_back(
John McCall833ca992009-10-29 08:12:44 +00002024 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00002025 return false;
2026}
2027
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002028/// \brief Substitute template arguments into the default template argument for
2029/// the given template type parameter.
2030///
2031/// \param SemaRef the semantic analysis object for which we are performing
2032/// the substitution.
2033///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002034/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002035/// for.
2036///
2037/// \param TemplateLoc the location of the template name that started the
2038/// template-id we are checking.
2039///
2040/// \param RAngleLoc the location of the right angle bracket ('>') that
2041/// terminates the template-id.
2042///
2043/// \param Param the template template parameter whose default we are
2044/// substituting into.
2045///
2046/// \param Converted the list of template arguments provided for template
2047/// parameters that precede \p Param in the template parameter list.
2048///
2049/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002050static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002051SubstDefaultTemplateArgument(Sema &SemaRef,
2052 TemplateDecl *Template,
2053 SourceLocation TemplateLoc,
2054 SourceLocation RAngleLoc,
2055 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002056 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002057 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002058
2059 // If the argument type is dependent, instantiate it now based
2060 // on the previously-computed template arguments.
2061 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002062 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002063 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002064
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002065 MultiLevelTemplateArgumentList AllTemplateArgs
2066 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2067
2068 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002069 Template, Converted.data(),
2070 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002071 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002072
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002073 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2074 Param->getDefaultArgumentLoc(),
2075 Param->getDeclName());
2076 }
2077
2078 return ArgType;
2079}
2080
2081/// \brief Substitute template arguments into the default template argument for
2082/// the given non-type template parameter.
2083///
2084/// \param SemaRef the semantic analysis object for which we are performing
2085/// the substitution.
2086///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002087/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002088/// for.
2089///
2090/// \param TemplateLoc the location of the template name that started the
2091/// template-id we are checking.
2092///
2093/// \param RAngleLoc the location of the right angle bracket ('>') that
2094/// terminates the template-id.
2095///
Douglas Gregor788cd062009-11-11 01:00:40 +00002096/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002097/// substituting into.
2098///
2099/// \param Converted the list of template arguments provided for template
2100/// parameters that precede \p Param in the template parameter list.
2101///
2102/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002103static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002104SubstDefaultTemplateArgument(Sema &SemaRef,
2105 TemplateDecl *Template,
2106 SourceLocation TemplateLoc,
2107 SourceLocation RAngleLoc,
2108 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002109 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002110 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002111 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002112
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002113 MultiLevelTemplateArgumentList AllTemplateArgs
2114 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002115
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002116 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002117 Template, Converted.data(),
2118 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002119 SourceRange(TemplateLoc, RAngleLoc));
2120
2121 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2122}
2123
Douglas Gregor788cd062009-11-11 01:00:40 +00002124/// \brief Substitute template arguments into the default template argument for
2125/// the given template template parameter.
2126///
2127/// \param SemaRef the semantic analysis object for which we are performing
2128/// the substitution.
2129///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002130/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002131/// for.
2132///
2133/// \param TemplateLoc the location of the template name that started the
2134/// template-id we are checking.
2135///
2136/// \param RAngleLoc the location of the right angle bracket ('>') that
2137/// terminates the template-id.
2138///
2139/// \param Param the template template parameter whose default we are
2140/// substituting into.
2141///
2142/// \param Converted the list of template arguments provided for template
2143/// parameters that precede \p Param in the template parameter list.
2144///
2145/// \returns the substituted template argument, or NULL if an error occurred.
2146static TemplateName
2147SubstDefaultTemplateArgument(Sema &SemaRef,
2148 TemplateDecl *Template,
2149 SourceLocation TemplateLoc,
2150 SourceLocation RAngleLoc,
2151 TemplateTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002152 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002153 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002154 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002155
Douglas Gregor788cd062009-11-11 01:00:40 +00002156 MultiLevelTemplateArgumentList AllTemplateArgs
2157 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002158
Douglas Gregor788cd062009-11-11 01:00:40 +00002159 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002160 Template, Converted.data(),
2161 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002162 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002163
Douglas Gregor788cd062009-11-11 01:00:40 +00002164 return SemaRef.SubstTemplateName(
2165 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002166 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002167 AllTemplateArgs);
2168}
2169
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002170/// \brief If the given template parameter has a default template
2171/// argument, substitute into that default template argument and
2172/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002173TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002174Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2175 SourceLocation TemplateLoc,
2176 SourceLocation RAngleLoc,
2177 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002178 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2179 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002180 if (!TypeParm->hasDefaultArgument())
2181 return TemplateArgumentLoc();
2182
John McCalla93c9342009-12-07 02:54:59 +00002183 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002184 TemplateLoc,
2185 RAngleLoc,
2186 TypeParm,
2187 Converted);
2188 if (DI)
2189 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2190
2191 return TemplateArgumentLoc();
2192 }
2193
2194 if (NonTypeTemplateParmDecl *NonTypeParm
2195 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2196 if (!NonTypeParm->hasDefaultArgument())
2197 return TemplateArgumentLoc();
2198
John McCall60d7b3a2010-08-24 06:29:42 +00002199 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002200 TemplateLoc,
2201 RAngleLoc,
2202 NonTypeParm,
2203 Converted);
2204 if (Arg.isInvalid())
2205 return TemplateArgumentLoc();
2206
2207 Expr *ArgE = Arg.takeAs<Expr>();
2208 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2209 }
2210
2211 TemplateTemplateParmDecl *TempTempParm
2212 = cast<TemplateTemplateParmDecl>(Param);
2213 if (!TempTempParm->hasDefaultArgument())
2214 return TemplateArgumentLoc();
2215
2216 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002217 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002218 RAngleLoc,
2219 TempTempParm,
2220 Converted);
2221 if (TName.isNull())
2222 return TemplateArgumentLoc();
2223
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002224 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002225 TempTempParm->getDefaultArgument().getTemplateQualifierRange(),
2226 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2227}
2228
Douglas Gregore7526412009-11-11 19:31:23 +00002229/// \brief Check that the given template argument corresponds to the given
2230/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002231///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002232/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002233/// checked.
2234///
2235/// \param Arg The template argument.
2236///
2237/// \param Template The template in which the template argument resides.
2238///
2239/// \param TemplateLoc The location of the template name for the template
2240/// whose argument list we're matching.
2241///
2242/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2243/// the template argument list.
2244///
2245/// \param ArgumentPackIndex The index into the argument pack where this
2246/// argument will be placed. Only valid if the parameter is a parameter pack.
2247///
2248/// \param Converted The checked, converted argument will be added to the
2249/// end of this small vector.
2250///
2251/// \param CTAK Describes how we arrived at this particular template argument:
2252/// explicitly written, deduced, etc.
2253///
2254/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002255bool Sema::CheckTemplateArgument(NamedDecl *Param,
2256 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002257 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002258 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002259 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002260 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002261 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002262 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002263 // Check template type parameters.
2264 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002265 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002266
Douglas Gregord9e15302009-11-11 19:41:09 +00002267 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002268 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002269 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002270 // with the template arguments we've seen thus far. But if the
2271 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002272 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002273 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2274 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002275
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002276 if (NTTPType->isDependentType() &&
2277 !isa<TemplateTemplateParmDecl>(Template) &&
2278 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002279 // Do substitution on the type of the non-type template parameter.
2280 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002281 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002282 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002283
2284 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002285 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002286 NTTPType = SubstType(NTTPType,
2287 MultiLevelTemplateArgumentList(TemplateArgs),
2288 NTTP->getLocation(),
2289 NTTP->getDeclName());
2290 // If that worked, check the non-type template parameter type
2291 // for validity.
2292 if (!NTTPType.isNull())
2293 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2294 NTTP->getLocation());
2295 if (NTTPType.isNull())
2296 return true;
2297 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002298
Douglas Gregore7526412009-11-11 19:31:23 +00002299 switch (Arg.getArgument().getKind()) {
2300 case TemplateArgument::Null:
2301 assert(false && "Should never see a NULL template argument here");
2302 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002303
Douglas Gregore7526412009-11-11 19:31:23 +00002304 case TemplateArgument::Expression: {
2305 Expr *E = Arg.getArgument().getAsExpr();
2306 TemplateArgument Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002307 if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK))
Douglas Gregore7526412009-11-11 19:31:23 +00002308 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002309
Douglas Gregor910f8002010-11-07 23:05:16 +00002310 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002311 break;
2312 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002313
Douglas Gregore7526412009-11-11 19:31:23 +00002314 case TemplateArgument::Declaration:
2315 case TemplateArgument::Integral:
2316 // We've already checked this template argument, so just copy
2317 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002318 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002319 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002320
Douglas Gregore7526412009-11-11 19:31:23 +00002321 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002322 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002323 // We were given a template template argument. It may not be ill-formed;
2324 // see below.
2325 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002326 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2327 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002328 // We have a template argument such as \c T::template X, which we
2329 // parsed as a template template argument. However, since we now
2330 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002331 // template name into an expression.
2332
2333 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2334 Arg.getTemplateNameLoc());
2335
John McCallf7a1a742009-11-24 19:00:30 +00002336 Expr *E = DependentScopeDeclRefExpr::Create(Context,
2337 DTN->getQualifier(),
Douglas Gregore7526412009-11-11 19:31:23 +00002338 Arg.getTemplateQualifierRange(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002339 NameInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002340
Douglas Gregora7fc9012011-01-05 18:58:31 +00002341 // If we parsed the template argument as a pack expansion, create a
2342 // pack expansion expression.
2343 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002344 ExprResult Expansion = ActOnPackExpansion(E,
Douglas Gregora7fc9012011-01-05 18:58:31 +00002345 Arg.getTemplateEllipsisLoc());
2346 if (Expansion.isInvalid())
2347 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002348
Douglas Gregora7fc9012011-01-05 18:58:31 +00002349 E = Expansion.get();
2350 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002351
Douglas Gregore7526412009-11-11 19:31:23 +00002352 TemplateArgument Result;
2353 if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
2354 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002355
Douglas Gregor910f8002010-11-07 23:05:16 +00002356 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002357 break;
2358 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002359
Douglas Gregore7526412009-11-11 19:31:23 +00002360 // We have a template argument that actually does refer to a class
2361 // template, template alias, or template template parameter, and
2362 // therefore cannot be a non-type template argument.
2363 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2364 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002365
Douglas Gregore7526412009-11-11 19:31:23 +00002366 Diag(Param->getLocation(), diag::note_template_param_here);
2367 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002368
Douglas Gregore7526412009-11-11 19:31:23 +00002369 case TemplateArgument::Type: {
2370 // We have a non-type template parameter but the template
2371 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002372
Douglas Gregore7526412009-11-11 19:31:23 +00002373 // C++ [temp.arg]p2:
2374 // In a template-argument, an ambiguity between a type-id and
2375 // an expression is resolved to a type-id, regardless of the
2376 // form of the corresponding template-parameter.
2377 //
2378 // We warn specifically about this case, since it can be rather
2379 // confusing for users.
2380 QualType T = Arg.getArgument().getAsType();
2381 SourceRange SR = Arg.getSourceRange();
2382 if (T->isFunctionType())
2383 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2384 else
2385 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2386 Diag(Param->getLocation(), diag::note_template_param_here);
2387 return true;
2388 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002389
Douglas Gregore7526412009-11-11 19:31:23 +00002390 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002391 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002392 break;
2393 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002394
Douglas Gregore7526412009-11-11 19:31:23 +00002395 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002396 }
2397
2398
Douglas Gregore7526412009-11-11 19:31:23 +00002399 // Check template template parameters.
2400 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002401
Douglas Gregore7526412009-11-11 19:31:23 +00002402 // Substitute into the template parameter list of the template
2403 // template parameter, since previously-supplied template arguments
2404 // may appear within the template template parameter.
2405 {
2406 // Set up a template instantiation context.
2407 LocalInstantiationScope Scope(*this);
2408 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002409 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002410 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002411
2412 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002413 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002414 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002415 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002416 MultiLevelTemplateArgumentList(TemplateArgs)));
2417 if (!TempParm)
2418 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002419 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002420
Douglas Gregore7526412009-11-11 19:31:23 +00002421 switch (Arg.getArgument().getKind()) {
2422 case TemplateArgument::Null:
2423 assert(false && "Should never see a NULL template argument here");
2424 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002425
Douglas Gregore7526412009-11-11 19:31:23 +00002426 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002427 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002428 if (CheckTemplateArgument(TempParm, Arg))
2429 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002430
Douglas Gregor910f8002010-11-07 23:05:16 +00002431 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002432 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002433
Douglas Gregore7526412009-11-11 19:31:23 +00002434 case TemplateArgument::Expression:
2435 case TemplateArgument::Type:
2436 // We have a template template parameter but the template
2437 // argument does not refer to a template.
2438 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
2439 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002440
Douglas Gregore7526412009-11-11 19:31:23 +00002441 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002442 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002443 "Declaration argument with template template parameter");
2444 break;
2445 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002446 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002447 "Integral argument with template template parameter");
2448 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002449
Douglas Gregore7526412009-11-11 19:31:23 +00002450 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002451 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002452 break;
2453 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002454
Douglas Gregore7526412009-11-11 19:31:23 +00002455 return false;
2456}
2457
Douglas Gregorc15cb382009-02-09 23:23:08 +00002458/// \brief Check that the given template argument list is well-formed
2459/// for specializing the given template.
2460bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2461 SourceLocation TemplateLoc,
John McCalld5532b62009-11-23 01:53:49 +00002462 const TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002463 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002464 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002465 TemplateParameterList *Params = Template->getTemplateParameters();
2466 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002467 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002468 bool Invalid = false;
2469
John McCalld5532b62009-11-23 01:53:49 +00002470 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2471
Mike Stump1eb44332009-09-09 15:08:12 +00002472 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002473 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002474
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002475 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002476 (NumArgs < Params->getMinRequiredArguments() &&
2477 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002478 // FIXME: point at either the first arg beyond what we can handle,
2479 // or the '>', depending on whether we have too many or too few
2480 // arguments.
2481 SourceRange Range;
2482 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002483 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002484 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2485 << (NumArgs > NumParams)
2486 << (isa<ClassTemplateDecl>(Template)? 0 :
2487 isa<FunctionTemplateDecl>(Template)? 1 :
2488 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2489 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002490 Diag(Template->getLocation(), diag::note_template_decl_here)
2491 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002492 Invalid = true;
2493 }
Mike Stump1eb44332009-09-09 15:08:12 +00002494
2495 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002496 // [...] The type and form of each template-argument specified in
2497 // a template-id shall match the type and form specified for the
2498 // corresponding parameter declared by the template in its
2499 // template-parameter-list.
Douglas Gregor14be16b2010-12-20 16:57:52 +00002500 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2501 TemplateParameterList::iterator Param = Params->begin(),
2502 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002503 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002504 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002505 while (Param != ParamEnd) {
Douglas Gregor16134c62009-07-01 00:28:38 +00002506 if (ArgIdx > NumArgs && PartialTemplateArgs)
2507 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002508
Douglas Gregorf35f8282009-11-11 21:54:23 +00002509 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002510 // If we have an expanded parameter pack, make sure we don't have too
2511 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002512 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002513 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002514 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002515 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2516 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2517 << true
2518 << (isa<ClassTemplateDecl>(Template)? 0 :
2519 isa<FunctionTemplateDecl>(Template)? 1 :
2520 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2521 << Template;
2522 Diag(Template->getLocation(), diag::note_template_decl_here)
2523 << Params->getSourceRange();
2524 return true;
2525 }
2526 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002527
Douglas Gregorf35f8282009-11-11 21:54:23 +00002528 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002529 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2530 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002531 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002532 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002533
Douglas Gregor14be16b2010-12-20 16:57:52 +00002534 if ((*Param)->isTemplateParameterPack()) {
2535 // The template parameter was a template parameter pack, so take the
2536 // deduced argument and place it on the argument pack. Note that we
2537 // stay on the same template parameter so that we can deduce more
2538 // arguments.
2539 ArgumentPack.push_back(Converted.back());
2540 Converted.pop_back();
2541 } else {
2542 // Move to the next template parameter.
2543 ++Param;
2544 }
2545 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002546 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002547 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002548
2549 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002550 // arguments, just break out now and we'll fill in the argument pack below.
2551 if ((*Param)->isTemplateParameterPack())
2552 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002553
Douglas Gregorf35f8282009-11-11 21:54:23 +00002554 // We have a default template argument that we will use.
2555 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002556
Douglas Gregorf35f8282009-11-11 21:54:23 +00002557 // Retrieve the default template argument from the template
2558 // parameter. For each kind of template parameter, we substitute the
2559 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002560 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002561 // the default argument.
2562 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2563 if (!TTP->hasDefaultArgument()) {
2564 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2565 break;
2566 }
2567
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002568 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002569 Template,
2570 TemplateLoc,
2571 RAngleLoc,
2572 TTP,
2573 Converted);
2574 if (!ArgType)
2575 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002576
Douglas Gregorf35f8282009-11-11 21:54:23 +00002577 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2578 ArgType);
2579 } else if (NonTypeTemplateParmDecl *NTTP
2580 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2581 if (!NTTP->hasDefaultArgument()) {
2582 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2583 break;
2584 }
2585
John McCall60d7b3a2010-08-24 06:29:42 +00002586 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002587 TemplateLoc,
2588 RAngleLoc,
2589 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002590 Converted);
2591 if (E.isInvalid())
2592 return true;
2593
2594 Expr *Ex = E.takeAs<Expr>();
2595 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2596 } else {
2597 TemplateTemplateParmDecl *TempParm
2598 = cast<TemplateTemplateParmDecl>(*Param);
2599
2600 if (!TempParm->hasDefaultArgument()) {
2601 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2602 break;
2603 }
2604
2605 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002606 TemplateLoc,
2607 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002608 TempParm,
2609 Converted);
2610 if (Name.isNull())
2611 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002612
2613 Arg = TemplateArgumentLoc(TemplateArgument(Name),
Douglas Gregorf35f8282009-11-11 21:54:23 +00002614 TempParm->getDefaultArgument().getTemplateQualifierRange(),
2615 TempParm->getDefaultArgument().getTemplateNameLoc());
2616 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002617
Douglas Gregorf35f8282009-11-11 21:54:23 +00002618 // Introduce an instantiation record that describes where we are using
2619 // the default template argument.
2620 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002621 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002622 SourceRange(TemplateLoc, RAngleLoc));
2623
Douglas Gregorf35f8282009-11-11 21:54:23 +00002624 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002625 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002626 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00002627 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002628
Douglas Gregor14be16b2010-12-20 16:57:52 +00002629 // Move to the next template parameter and argument.
2630 ++Param;
2631 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002632 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002633
Douglas Gregor14be16b2010-12-20 16:57:52 +00002634 // Form argument packs for each of the parameter packs remaining.
2635 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00002636 // If we're checking a partial list of template arguments, don't fill
2637 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002638
2639 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002640 if (PartialTemplateArgs && ArgumentPack.empty()) {
2641 Converted.push_back(TemplateArgument());
Douglas Gregor203e6a32011-01-11 23:09:57 +00002642 } else if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00002643 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00002644 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002645 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2646 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00002647 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00002648 ArgumentPack.clear();
2649 }
2650 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002651
Douglas Gregor14be16b2010-12-20 16:57:52 +00002652 ++Param;
2653 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002654
Douglas Gregorc15cb382009-02-09 23:23:08 +00002655 return Invalid;
2656}
2657
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002658namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002659 class UnnamedLocalNoLinkageFinder
2660 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002661 {
2662 Sema &S;
2663 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002664
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002665 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002666
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002667 public:
2668 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
2669
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002670 bool Visit(QualType T) {
2671 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002672 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002673
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002674#define TYPE(Class, Parent) \
2675 bool Visit##Class##Type(const Class##Type *);
2676#define ABSTRACT_TYPE(Class, Parent) \
2677 bool Visit##Class##Type(const Class##Type *) { return false; }
2678#define NON_CANONICAL_TYPE(Class, Parent) \
2679 bool Visit##Class##Type(const Class##Type *) { return false; }
2680#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002681
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002682 bool VisitTagDecl(const TagDecl *Tag);
2683 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
2684 };
2685}
2686
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002687bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002688 return false;
2689}
2690
2691bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
2692 return Visit(T->getElementType());
2693}
2694
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002695bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002696 return Visit(T->getPointeeType());
2697}
2698
2699bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002700 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002701 return Visit(T->getPointeeType());
2702}
2703
2704bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002705 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002706 return Visit(T->getPointeeType());
2707}
2708
2709bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002710 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002711 return Visit(T->getPointeeType());
2712}
2713
2714bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002715 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002716 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
2717}
2718
2719bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002720 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002721 return Visit(T->getElementType());
2722}
2723
2724bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002725 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002726 return Visit(T->getElementType());
2727}
2728
2729bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002730 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002731 return Visit(T->getElementType());
2732}
2733
2734bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002735 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002736 return Visit(T->getElementType());
2737}
2738
2739bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002740 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002741 return Visit(T->getElementType());
2742}
2743
2744bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
2745 return Visit(T->getElementType());
2746}
2747
2748bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
2749 return Visit(T->getElementType());
2750}
2751
2752bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
2753 const FunctionProtoType* T) {
2754 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002755 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002756 A != AEnd; ++A) {
2757 if (Visit(*A))
2758 return true;
2759 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002760
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002761 return Visit(T->getResultType());
2762}
2763
2764bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
2765 const FunctionNoProtoType* T) {
2766 return Visit(T->getResultType());
2767}
2768
2769bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
2770 const UnresolvedUsingType*) {
2771 return false;
2772}
2773
2774bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
2775 return false;
2776}
2777
2778bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
2779 return Visit(T->getUnderlyingType());
2780}
2781
2782bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
2783 return false;
2784}
2785
Richard Smith34b41d92011-02-20 03:19:35 +00002786bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
2787 return Visit(T->getDeducedType());
2788}
2789
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002790bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
2791 return VisitTagDecl(T->getDecl());
2792}
2793
2794bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
2795 return VisitTagDecl(T->getDecl());
2796}
2797
2798bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
2799 const TemplateTypeParmType*) {
2800 return false;
2801}
2802
Douglas Gregorc3069d62011-01-14 02:55:32 +00002803bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
2804 const SubstTemplateTypeParmPackType *) {
2805 return false;
2806}
2807
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002808bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
2809 const TemplateSpecializationType*) {
2810 return false;
2811}
2812
2813bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
2814 const InjectedClassNameType* T) {
2815 return VisitTagDecl(T->getDecl());
2816}
2817
2818bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
2819 const DependentNameType* T) {
2820 return VisitNestedNameSpecifier(T->getQualifier());
2821}
2822
2823bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
2824 const DependentTemplateSpecializationType* T) {
2825 return VisitNestedNameSpecifier(T->getQualifier());
2826}
2827
Douglas Gregor7536dd52010-12-20 02:24:11 +00002828bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
2829 const PackExpansionType* T) {
2830 return Visit(T->getPattern());
2831}
2832
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002833bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
2834 return false;
2835}
2836
2837bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
2838 const ObjCInterfaceType *) {
2839 return false;
2840}
2841
2842bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
2843 const ObjCObjectPointerType *) {
2844 return false;
2845}
2846
2847bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
2848 if (Tag->getDeclContext()->isFunctionOrMethod()) {
2849 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
2850 << S.Context.getTypeDeclType(Tag) << SR;
2851 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002852 }
2853
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002854 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) {
2855 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
2856 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
2857 return true;
2858 }
2859
2860 return false;
2861}
2862
2863bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
2864 NestedNameSpecifier *NNS) {
2865 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
2866 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002867
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002868 switch (NNS->getKind()) {
2869 case NestedNameSpecifier::Identifier:
2870 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00002871 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002872 case NestedNameSpecifier::Global:
2873 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002874
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002875 case NestedNameSpecifier::TypeSpec:
2876 case NestedNameSpecifier::TypeSpecWithTemplate:
2877 return Visit(QualType(NNS->getAsType(), 0));
2878 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00002879 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002880}
2881
2882
Douglas Gregorc15cb382009-02-09 23:23:08 +00002883/// \brief Check a template argument against its corresponding
2884/// template type parameter.
2885///
2886/// This routine implements the semantics of C++ [temp.arg.type]. It
2887/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00002888bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00002889 TypeSourceInfo *ArgInfo) {
2890 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00002891 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00002892 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00002893
2894 if (Arg->isVariablyModifiedType()) {
2895 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00002896 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00002897 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002898 }
2899
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002900 // C++03 [temp.arg.type]p2:
2901 // A local type, a type with no linkage, an unnamed type or a type
2902 // compounded from any of these types shall not be used as a
2903 // template-argument for a template type-parameter.
2904 //
2905 // C++0x allows these, and even in C++03 we allow them as an extension with
2906 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00002907 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002908 UnnamedLocalNoLinkageFinder Finder(*this, SR);
2909 (void)Finder.Visit(Context.getCanonicalType(Arg));
2910 }
2911
Douglas Gregorc15cb382009-02-09 23:23:08 +00002912 return false;
2913}
2914
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002915/// \brief Checks whether the given template argument is the address
2916/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002917static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00002918CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
2919 NonTypeTemplateParmDecl *Param,
2920 QualType ParamType,
2921 Expr *ArgIn,
2922 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002923 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00002924 Expr *Arg = ArgIn;
2925 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002926
2927 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00002928 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002929 Arg = Cast->getSubExpr();
2930
2931 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00002932 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002933 // A template-argument for a non-type, non-template
2934 // template-parameter shall be one of: [...]
2935 //
2936 // -- the address of an object or function with external
2937 // linkage, including function templates and function
2938 // template-ids but excluding non-static class members,
2939 // expressed as & id-expression where the & is optional if
2940 // the name refers to a function or array, or if the
2941 // corresponding template-parameter is a reference; or
2942 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002943
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002944 // In C++98/03 mode, give an extension warning on any extra parentheses.
2945 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
2946 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002947 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002948 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00002949 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002950 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002951 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002952 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002953 }
2954
2955 Arg = Parens->getSubExpr();
2956 }
2957
Douglas Gregorb7a09262010-04-01 18:32:35 +00002958 bool AddressTaken = false;
2959 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002960 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00002961 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002962 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00002963 AddressTaken = true;
2964 AddrOpLoc = UnOp->getOperatorLoc();
2965 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002966 } else
2967 DRE = dyn_cast<DeclRefExpr>(Arg);
2968
Douglas Gregorb7a09262010-04-01 18:32:35 +00002969 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00002970 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
2971 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00002972 S.Diag(Param->getLocation(), diag::note_template_param_here);
2973 return true;
2974 }
Chandler Carruth038cc392010-01-31 10:01:20 +00002975
2976 // Stop checking the precise nature of the argument if it is value dependent,
2977 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00002978 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00002979 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00002980 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00002981 }
Chandler Carruth038cc392010-01-31 10:01:20 +00002982
Douglas Gregorb7a09262010-04-01 18:32:35 +00002983 if (!isa<ValueDecl>(DRE->getDecl())) {
2984 S.Diag(Arg->getSourceRange().getBegin(),
2985 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002986 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00002987 S.Diag(Param->getLocation(), diag::note_template_param_here);
2988 return true;
2989 }
2990
2991 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002992
2993 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00002994 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
2995 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002996 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00002997 S.Diag(Param->getLocation(), diag::note_template_param_here);
2998 return true;
2999 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003000
3001 // Cannot refer to non-static member functions
3002 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003003 if (!Method->isStatic()) {
3004 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003005 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003006 S.Diag(Param->getLocation(), diag::note_template_param_here);
3007 return true;
3008 }
Mike Stump1eb44332009-09-09 15:08:12 +00003009
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003010 // Functions must have external linkage.
3011 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003012 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003013 S.Diag(Arg->getSourceRange().getBegin(),
3014 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003015 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003016 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003017 << true;
3018 return true;
3019 }
3020
3021 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003022 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003023
Douglas Gregorb7a09262010-04-01 18:32:35 +00003024 // If the template parameter has pointer type, the function decays.
3025 if (ParamType->isPointerType() && !AddressTaken)
3026 ArgType = S.Context.getPointerType(Func->getType());
3027 else if (AddressTaken && ParamType->isReferenceType()) {
3028 // If we originally had an address-of operator, but the
3029 // parameter has reference type, complain and (if things look
3030 // like they will work) drop the address-of operator.
3031 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3032 ParamType.getNonReferenceType())) {
3033 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3034 << ParamType;
3035 S.Diag(Param->getLocation(), diag::note_template_param_here);
3036 return true;
3037 }
3038
3039 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3040 << ParamType
3041 << FixItHint::CreateRemoval(AddrOpLoc);
3042 S.Diag(Param->getLocation(), diag::note_template_param_here);
3043
3044 ArgType = Func->getType();
3045 }
3046 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003047 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003048 S.Diag(Arg->getSourceRange().getBegin(),
3049 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003050 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003051 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003052 << true;
3053 return true;
3054 }
3055
Douglas Gregorb7a09262010-04-01 18:32:35 +00003056 // A value of reference type is not an object.
3057 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003058 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003059 diag::err_template_arg_reference_var)
3060 << Var->getType() << Arg->getSourceRange();
3061 S.Diag(Param->getLocation(), diag::note_template_param_here);
3062 return true;
3063 }
3064
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003065 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003066 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003067
3068 // If the template parameter has pointer type, we must have taken
3069 // the address of this object.
3070 if (ParamType->isReferenceType()) {
3071 if (AddressTaken) {
3072 // If we originally had an address-of operator, but the
3073 // parameter has reference type, complain and (if things look
3074 // like they will work) drop the address-of operator.
3075 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3076 ParamType.getNonReferenceType())) {
3077 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3078 << ParamType;
3079 S.Diag(Param->getLocation(), diag::note_template_param_here);
3080 return true;
3081 }
3082
3083 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3084 << ParamType
3085 << FixItHint::CreateRemoval(AddrOpLoc);
3086 S.Diag(Param->getLocation(), diag::note_template_param_here);
3087
3088 ArgType = Var->getType();
3089 }
3090 } else if (!AddressTaken && ParamType->isPointerType()) {
3091 if (Var->getType()->isArrayType()) {
3092 // Array-to-pointer decay.
3093 ArgType = S.Context.getArrayDecayedType(Var->getType());
3094 } else {
3095 // If the template parameter has pointer type but the address of
3096 // this object was not taken, complain and (possibly) recover by
3097 // taking the address of the entity.
3098 ArgType = S.Context.getPointerType(Var->getType());
3099 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3100 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3101 << ParamType;
3102 S.Diag(Param->getLocation(), diag::note_template_param_here);
3103 return true;
3104 }
3105
3106 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3107 << ParamType
3108 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3109
3110 S.Diag(Param->getLocation(), diag::note_template_param_here);
3111 }
3112 }
3113 } else {
3114 // We found something else, but we don't know specifically what it is.
3115 S.Diag(Arg->getSourceRange().getBegin(),
3116 diag::err_template_arg_not_object_or_func)
3117 << Arg->getSourceRange();
3118 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3119 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003120 }
Mike Stump1eb44332009-09-09 15:08:12 +00003121
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003122 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003123 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003124 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003125 // For pointer-to-object types, qualification conversions are
3126 // permitted.
3127 } else {
3128 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3129 if (!ParamRef->getPointeeType()->isFunctionType()) {
3130 // C++ [temp.arg.nontype]p5b3:
3131 // For a non-type template-parameter of type reference to
3132 // object, no conversions apply. The type referred to by the
3133 // reference may be more cv-qualified than the (otherwise
3134 // identical) type of the template- argument. The
3135 // template-parameter is bound directly to the
3136 // template-argument, which shall be an lvalue.
3137
3138 // FIXME: Other qualifiers?
3139 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3140 unsigned ArgQuals = ArgType.getCVRQualifiers();
3141
3142 if ((ParamQuals | ArgQuals) != ParamQuals) {
3143 S.Diag(Arg->getSourceRange().getBegin(),
3144 diag::err_template_arg_ref_bind_ignores_quals)
3145 << ParamType << Arg->getType()
3146 << Arg->getSourceRange();
3147 S.Diag(Param->getLocation(), diag::note_template_param_here);
3148 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003149 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003150 }
3151 }
3152
3153 // At this point, the template argument refers to an object or
3154 // function with external linkage. We now need to check whether the
3155 // argument and parameter types are compatible.
3156 if (!S.Context.hasSameUnqualifiedType(ArgType,
3157 ParamType.getNonReferenceType())) {
3158 // We can't perform this conversion or binding.
3159 if (ParamType->isReferenceType())
3160 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3161 << ParamType << Arg->getType() << Arg->getSourceRange();
3162 else
3163 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3164 << Arg->getType() << ParamType << Arg->getSourceRange();
3165 S.Diag(Param->getLocation(), diag::note_template_param_here);
3166 return true;
3167 }
3168 }
3169
3170 // Create the template argument.
3171 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003172 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003173 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003174}
3175
3176/// \brief Checks whether the given template argument is a pointer to
3177/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003178bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003179 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003180 bool Invalid = false;
3181
3182 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003183 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003184 Arg = Cast->getSubExpr();
3185
3186 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003187 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003188 // A template-argument for a non-type, non-template
3189 // template-parameter shall be one of: [...]
3190 //
3191 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003192 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003193
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003194 // In C++98/03 mode, give an extension warning on any extra parentheses.
3195 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3196 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003197 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003198 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003199 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003200 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003201 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003202 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003203 }
3204
3205 Arg = Parens->getSubExpr();
3206 }
3207
Douglas Gregorcaddba02009-11-12 18:38:13 +00003208 // A pointer-to-member constant written &Class::member.
3209 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003210 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003211 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3212 if (DRE && !DRE->getQualifier())
3213 DRE = 0;
3214 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003215 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003216 // A constant of pointer-to-member type.
3217 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3218 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3219 if (VD->getType()->isMemberPointerType()) {
3220 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003221 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003222 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3223 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003224 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003225 else
3226 Converted = TemplateArgument(VD->getCanonicalDecl());
3227 return Invalid;
3228 }
3229 }
3230 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003231
Douglas Gregorcaddba02009-11-12 18:38:13 +00003232 DRE = 0;
3233 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003234
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003235 if (!DRE)
3236 return Diag(Arg->getSourceRange().getBegin(),
3237 diag::err_template_arg_not_pointer_to_member_form)
3238 << Arg->getSourceRange();
3239
3240 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3241 assert((isa<FieldDecl>(DRE->getDecl()) ||
3242 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3243 "Only non-static member pointers can make it here");
3244
3245 // Okay: this is the address of a non-static member, and therefore
3246 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003247 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003248 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003249 else
3250 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003251 return Invalid;
3252 }
3253
3254 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003255 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003256 diag::err_template_arg_not_pointer_to_member_form)
3257 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003258 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003259 diag::note_template_arg_refers_here);
3260 return true;
3261}
3262
Douglas Gregorc15cb382009-02-09 23:23:08 +00003263/// \brief Check a template argument against its corresponding
3264/// non-type template parameter.
3265///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003266/// This routine implements the semantics of C++ [temp.arg.nontype].
3267/// It returns true if an error occurred, and false otherwise. \p
3268/// InstantiatedParamType is the type of the non-type template
3269/// parameter after it has been instantiated.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003270///
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003271/// If no error was detected, Converted receives the converted template argument.
Douglas Gregorc15cb382009-02-09 23:23:08 +00003272bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
Mike Stump1eb44332009-09-09 15:08:12 +00003273 QualType InstantiatedParamType, Expr *&Arg,
Douglas Gregor02024a92010-03-28 02:42:43 +00003274 TemplateArgument &Converted,
3275 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003276 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3277
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003278 // If either the parameter has a dependent type or the argument is
3279 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003280 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3281 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003282 Converted = TemplateArgument(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003283 return false;
Douglas Gregor40808ce2009-03-09 23:48:35 +00003284 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003285
3286 // C++ [temp.arg.nontype]p5:
3287 // The following conversions are performed on each expression used
3288 // as a non-type template-argument. If a non-type
3289 // template-argument cannot be converted to the type of the
3290 // corresponding template-parameter then the program is
3291 // ill-formed.
3292 //
3293 // -- for a non-type template-parameter of integral or
3294 // enumeration type, integral promotions (4.5) and integral
3295 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003296 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003297 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003298 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003299 // C++ [temp.arg.nontype]p1:
3300 // A template-argument for a non-type, non-template
3301 // template-parameter shall be one of:
3302 //
3303 // -- an integral constant-expression of integral or enumeration
3304 // type; or
3305 // -- the name of a non-type template-parameter; or
3306 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003307 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003308 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003309 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003310 diag::err_template_arg_not_integral_or_enumeral)
3311 << ArgType << Arg->getSourceRange();
3312 Diag(Param->getLocation(), diag::note_template_param_here);
3313 return true;
3314 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003315 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003316 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3317 << ArgType << Arg->getSourceRange();
3318 return true;
3319 }
3320
Douglas Gregor02024a92010-03-28 02:42:43 +00003321 // From here on out, all we care about are the unqualified forms
3322 // of the parameter and argument types.
3323 ParamType = ParamType.getUnqualifiedType();
3324 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003325
3326 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003327 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003328 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003329 } else if (CTAK == CTAK_Deduced) {
3330 // C++ [temp.deduct.type]p17:
3331 // If, in the declaration of a function template with a non-type
3332 // template-parameter, the non-type template- parameter is used
3333 // in an expression in the function parameter-list and, if the
3334 // corresponding template-argument is deduced, the
3335 // template-argument type shall match the type of the
3336 // template-parameter exactly, except that a template-argument
3337 // deduced from an array bound may be of any integral type.
3338 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3339 << ArgType << ParamType;
3340 Diag(Param->getLocation(), diag::note_template_param_here);
John McCalldaa8e4e2010-11-15 09:13:47 +00003341 return true;
3342 } else if (ParamType->isBooleanType()) {
3343 // This is an integral-to-boolean conversion.
3344 ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003345 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3346 !ParamType->isEnumeralType()) {
3347 // This is an integral promotion or conversion.
John McCall2de56d12010-08-25 11:45:40 +00003348 ImpCastExprToType(Arg, ParamType, CK_IntegralCast);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003349 } else {
3350 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003351 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003352 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003353 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003354 Diag(Param->getLocation(), diag::note_template_param_here);
3355 return true;
3356 }
3357
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003358 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003359 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003360 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003361
3362 if (!Arg->isValueDependent()) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003363 llvm::APSInt OldValue = Value;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003364
3365 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003366 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003367 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003368 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003369 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003370 Value.setIsSigned(IntegerType->isSignedIntegerType());
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003371
3372 // Complain if an unsigned parameter received a negative value.
3373 if (IntegerType->isUnsignedIntegerType()
3374 && (OldValue.isSigned() && OldValue.isNegative())) {
3375 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3376 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3377 << Arg->getSourceRange();
3378 Diag(Param->getLocation(), diag::note_template_param_here);
3379 }
3380
3381 // Complain if we overflowed the template parameter's type.
3382 unsigned RequiredBits;
3383 if (IntegerType->isUnsignedIntegerType())
3384 RequiredBits = OldValue.getActiveBits();
3385 else if (OldValue.isUnsigned())
3386 RequiredBits = OldValue.getActiveBits() + 1;
3387 else
3388 RequiredBits = OldValue.getMinSignedBits();
3389 if (RequiredBits > AllowedBits) {
3390 Diag(Arg->getSourceRange().getBegin(),
3391 diag::warn_template_arg_too_large)
3392 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3393 << Arg->getSourceRange();
3394 Diag(Param->getLocation(), diag::note_template_param_here);
3395 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003396 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003397
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003398 // Add the value of this argument to the list of converted
3399 // arguments. We use the bitwidth and signedness of the template
3400 // parameter.
3401 if (Arg->isValueDependent()) {
3402 // The argument is value-dependent. Create a new
3403 // TemplateArgument with the converted expression.
3404 Converted = TemplateArgument(Arg);
3405 return false;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003406 }
3407
John McCall833ca992009-10-29 08:12:44 +00003408 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003409 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003410 : IntegerType);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003411 return false;
3412 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003413
John McCall6bb80172010-03-30 21:47:33 +00003414 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3415
Douglas Gregorb7a09262010-04-01 18:32:35 +00003416 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3417 // from a template argument of type std::nullptr_t to a non-type
3418 // template parameter of type pointer to object, pointer to
3419 // function, or pointer-to-member, respectively.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003420 if (ArgType->isNullPtrType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003421 (ParamType->isPointerType() || ParamType->isMemberPointerType())) {
3422 Converted = TemplateArgument((NamedDecl *)0);
3423 return false;
3424 }
3425
Douglas Gregorb86b0572009-02-11 01:18:59 +00003426 // Handle pointer-to-function, reference-to-function, and
3427 // pointer-to-member-function all in (roughly) the same way.
3428 if (// -- For a non-type template-parameter of type pointer to
3429 // function, only the function-to-pointer conversion (4.3) is
3430 // applied. If the template-argument represents a set of
3431 // overloaded functions (or a pointer to such), the matching
3432 // function is selected from the set (13.4).
3433 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003434 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003435 // -- For a non-type template-parameter of type reference to
3436 // function, no conversions apply. If the template-argument
3437 // represents a set of overloaded functions, the matching
3438 // function is selected from the set (13.4).
3439 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003440 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003441 // -- For a non-type template-parameter of type pointer to
3442 // member function, no conversions apply. If the
3443 // template-argument represents a set of overloaded member
3444 // functions, the matching member function is selected from
3445 // the set (13.4).
3446 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003447 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003448 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003449
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003450 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003451 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003452 true,
3453 FoundResult)) {
3454 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3455 return true;
3456
3457 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3458 ArgType = Arg->getType();
3459 } else
Douglas Gregor48f3bb92009-02-18 21:56:37 +00003460 return true;
Douglas Gregora35284b2009-02-11 00:19:33 +00003461 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003462
Douglas Gregorb7a09262010-04-01 18:32:35 +00003463 if (!ParamType->isMemberPointerType())
3464 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003465 ParamType,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003466 Arg, Converted);
3467
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003468 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3469 false)) {
John McCall2de56d12010-08-25 11:45:40 +00003470 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregorb7a09262010-04-01 18:32:35 +00003471 } else if (!Context.hasSameUnqualifiedType(ArgType,
3472 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003473 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003474 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003475 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003476 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003477 Diag(Param->getLocation(), diag::note_template_param_here);
3478 return true;
3479 }
Mike Stump1eb44332009-09-09 15:08:12 +00003480
Douglas Gregorb7a09262010-04-01 18:32:35 +00003481 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregora35284b2009-02-11 00:19:33 +00003482 }
3483
Chris Lattnerfe90de72009-02-20 21:37:53 +00003484 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003485 // -- for a non-type template-parameter of type pointer to
3486 // object, qualification conversions (4.4) and the
3487 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003488 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003489 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003490 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003491
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003492 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003493 ParamType,
3494 Arg, Converted);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003495 }
Mike Stump1eb44332009-09-09 15:08:12 +00003496
Ted Kremenek6217b802009-07-29 21:53:49 +00003497 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003498 // -- For a non-type template-parameter of type reference to
3499 // object, no conversions apply. The type referred to by the
3500 // reference may be more cv-qualified than the (otherwise
3501 // identical) type of the template-argument. The
3502 // template-parameter is bound directly to the
3503 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003504 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003505 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003506
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003507 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003508 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3509 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003510 true,
3511 FoundResult)) {
3512 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3513 return true;
3514
3515 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3516 ArgType = Arg->getType();
3517 } else
Douglas Gregorb7a09262010-04-01 18:32:35 +00003518 return true;
Douglas Gregorb86b0572009-02-11 01:18:59 +00003519 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003520
3521 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003522 ParamType,
3523 Arg, Converted);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003524 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003525
3526 // -- For a non-type template-parameter of type pointer to data
3527 // member, qualification conversions (4.4) are applied.
3528 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3529
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003530 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003531 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003532 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John McCall2de56d12010-08-25 11:45:40 +00003533 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregor658bbb52009-02-11 16:16:59 +00003534 } else {
3535 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003536 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003537 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003538 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003539 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00003540 return true;
Douglas Gregor658bbb52009-02-11 16:16:59 +00003541 }
3542
Douglas Gregorcaddba02009-11-12 18:38:13 +00003543 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003544}
3545
3546/// \brief Check a template argument against its corresponding
3547/// template template parameter.
3548///
3549/// This routine implements the semantics of C++ [temp.arg.template].
3550/// It returns true if an error occurred, and false otherwise.
3551bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003552 const TemplateArgumentLoc &Arg) {
3553 TemplateName Name = Arg.getArgument().getAsTemplate();
3554 TemplateDecl *Template = Name.getAsTemplateDecl();
3555 if (!Template) {
3556 // Any dependent template name is fine.
3557 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3558 return false;
3559 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003560
3561 // C++ [temp.arg.template]p1:
3562 // A template-argument for a template template-parameter shall be
3563 // the name of a class template, expressed as id-expression. Only
3564 // primary class templates are considered when matching the
3565 // template template argument with the corresponding parameter;
3566 // partial specializations are not considered even if their
3567 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003568 //
3569 // Note that we also allow template template parameters here, which
3570 // will happen when we are dealing with, e.g., class template
3571 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003572 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003573 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003574 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003575 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003576 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003577 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003578 << Template;
3579 }
3580
3581 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
3582 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003583 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003584 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00003585 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00003586}
3587
Douglas Gregor02024a92010-03-28 02:42:43 +00003588/// \brief Given a non-type template argument that refers to a
3589/// declaration and the type of its corresponding non-type template
3590/// parameter, produce an expression that properly refers to that
3591/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003592ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003593Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
3594 QualType ParamType,
3595 SourceLocation Loc) {
3596 assert(Arg.getKind() == TemplateArgument::Declaration &&
3597 "Only declaration template arguments permitted here");
3598 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
3599
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003600 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00003601 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
3602 // If the value is a class member, we might have a pointer-to-member.
3603 // Determine whether the non-type template template parameter is of
3604 // pointer-to-member type. If so, we need to build an appropriate
3605 // expression for a pointer-to-member, since a "normal" DeclRefExpr
3606 // would refer to the member itself.
3607 if (ParamType->isMemberPointerType()) {
3608 QualType ClassType
3609 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
3610 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00003611 = NestedNameSpecifier::Create(Context, 0, false,
3612 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00003613 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00003614 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00003615
3616 // The actual value-ness of this is unimportant, but for
3617 // internal consistency's sake, references to instance methods
3618 // are r-values.
3619 ExprValueKind VK = VK_LValue;
3620 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
3621 VK = VK_RValue;
3622
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003623 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00003624 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00003625 VK,
John McCallf89e55a2010-11-18 06:31:45 +00003626 Loc,
3627 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00003628 if (RefExpr.isInvalid())
3629 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003630
John McCall2de56d12010-08-25 11:45:40 +00003631 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003632
Douglas Gregorc0c83002010-04-30 21:46:38 +00003633 // We might need to perform a trailing qualification conversion, since
3634 // the element type on the parameter could be more qualified than the
3635 // element type in the expression we constructed.
3636 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003637 ParamType.getUnqualifiedType(), false)) {
Douglas Gregorc0c83002010-04-30 21:46:38 +00003638 Expr *RefE = RefExpr.takeAs<Expr>();
John McCall2de56d12010-08-25 11:45:40 +00003639 ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CK_NoOp);
Douglas Gregorc0c83002010-04-30 21:46:38 +00003640 RefExpr = Owned(RefE);
3641 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003642
Douglas Gregor02024a92010-03-28 02:42:43 +00003643 assert(!RefExpr.isInvalid() &&
3644 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00003645 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00003646 return move(RefExpr);
3647 }
3648 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003649
Douglas Gregor02024a92010-03-28 02:42:43 +00003650 QualType T = VD->getType().getNonReferenceType();
3651 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003652 // When the non-type template parameter is a pointer, take the
3653 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00003654 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003655 if (RefExpr.isInvalid())
3656 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003657
3658 if (T->isFunctionType() || T->isArrayType()) {
3659 // Decay functions and arrays.
3660 Expr *RefE = (Expr *)RefExpr.get();
3661 DefaultFunctionArrayConversion(RefE);
3662 if (RefE != RefExpr.get()) {
3663 RefExpr.release();
3664 RefExpr = Owned(RefE);
3665 }
3666
3667 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00003668 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003669
Douglas Gregorb7a09262010-04-01 18:32:35 +00003670 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00003671 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00003672 }
3673
John McCallf89e55a2010-11-18 06:31:45 +00003674 ExprValueKind VK = VK_RValue;
3675
Douglas Gregor02024a92010-03-28 02:42:43 +00003676 // If the non-type template parameter has reference type, qualify the
3677 // resulting declaration reference with the extra qualifiers on the
3678 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00003679 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
3680 VK = VK_LValue;
3681 T = Context.getQualifiedType(T,
3682 TargetRef->getPointeeType().getQualifiers());
3683 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003684
John McCallf89e55a2010-11-18 06:31:45 +00003685 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003686}
3687
3688/// \brief Construct a new expression that refers to the given
3689/// integral template argument with the given source-location
3690/// information.
3691///
3692/// This routine takes care of the mapping from an integral template
3693/// argument (which may have any integral type) to the appropriate
3694/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003695ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003696Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
3697 SourceLocation Loc) {
3698 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00003699 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00003700 QualType T = Arg.getIntegralType();
3701 if (T->isCharType() || T->isWideCharType())
3702 return Owned(new (Context) CharacterLiteral(
3703 Arg.getAsIntegral()->getZExtValue(),
3704 T->isWideCharType(),
3705 T,
3706 Loc));
3707 if (T->isBooleanType())
3708 return Owned(new (Context) CXXBoolLiteralExpr(
3709 Arg.getAsIntegral()->getBoolValue(),
3710 T,
3711 Loc));
3712
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003713 QualType BT;
3714 if (const EnumType *ET = T->getAs<EnumType>())
3715 BT = ET->getDecl()->getPromotionType();
3716 else
3717 BT = T;
3718
3719 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00003720 if (T->isEnumeralType()) {
3721 // FIXME: This is a hack. We need a better way to handle substituted
3722 // non-type template parameters.
3723 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast,
3724 E, 0,
3725 Context.getTrivialTypeSourceInfo(T, Loc),
3726 Loc, Loc);
3727 }
3728
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003729 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00003730}
3731
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003732/// \brief Match two template parameters within template parameter lists.
3733static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
3734 bool Complain,
3735 Sema::TemplateParameterListEqualKind Kind,
3736 SourceLocation TemplateArgLoc) {
3737 // Check the actual kind (type, non-type, template).
3738 if (Old->getKind() != New->getKind()) {
3739 if (Complain) {
3740 unsigned NextDiag = diag::err_template_param_different_kind;
3741 if (TemplateArgLoc.isValid()) {
3742 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3743 NextDiag = diag::note_template_param_different_kind;
3744 }
3745 S.Diag(New->getLocation(), NextDiag)
3746 << (Kind != Sema::TPL_TemplateMatch);
3747 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
3748 << (Kind != Sema::TPL_TemplateMatch);
3749 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003750
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003751 return false;
3752 }
3753
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003754 // Check that both are parameter packs are neither are parameter packs.
3755 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00003756 // template template parameter, the template template parameter can have
3757 // a parameter pack where the template template argument does not.
3758 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
3759 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3760 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003761 if (Complain) {
3762 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
3763 if (TemplateArgLoc.isValid()) {
3764 S.Diag(TemplateArgLoc,
3765 diag::err_template_arg_template_params_mismatch);
3766 NextDiag = diag::note_template_parameter_pack_non_pack;
3767 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003768
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003769 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
3770 : isa<NonTypeTemplateParmDecl>(New)? 1
3771 : 2;
3772 S.Diag(New->getLocation(), NextDiag)
3773 << ParamKind << New->isParameterPack();
3774 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
3775 << ParamKind << Old->isParameterPack();
3776 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003777
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003778 return false;
3779 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003780
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003781 // For non-type template parameters, check the type of the parameter.
3782 if (NonTypeTemplateParmDecl *OldNTTP
3783 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
3784 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003785
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003786 // If we are matching a template template argument to a template
3787 // template parameter and one of the non-type template parameter types
3788 // is dependent, then we must wait until template instantiation time
3789 // to actually compare the arguments.
3790 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3791 (OldNTTP->getType()->isDependentType() ||
3792 NewNTTP->getType()->isDependentType()))
3793 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003794
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003795 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
3796 if (Complain) {
3797 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
3798 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003799 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003800 diag::err_template_arg_template_params_mismatch);
3801 NextDiag = diag::note_template_nontype_parm_different_type;
3802 }
3803 S.Diag(NewNTTP->getLocation(), NextDiag)
3804 << NewNTTP->getType()
3805 << (Kind != Sema::TPL_TemplateMatch);
3806 S.Diag(OldNTTP->getLocation(),
3807 diag::note_template_nontype_parm_prev_declaration)
3808 << OldNTTP->getType();
3809 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003810
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003811 return false;
3812 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003813
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003814 return true;
3815 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003816
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003817 // For template template parameters, check the template parameter types.
3818 // The template parameter lists of template template
3819 // parameters must agree.
3820 if (TemplateTemplateParmDecl *OldTTP
3821 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003822 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003823 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
3824 OldTTP->getTemplateParameters(),
3825 Complain,
3826 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003827 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003828 : Kind),
3829 TemplateArgLoc);
3830 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003831
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003832 return true;
3833}
Douglas Gregor02024a92010-03-28 02:42:43 +00003834
Douglas Gregora0347822011-01-13 00:08:50 +00003835/// \brief Diagnose a known arity mismatch when comparing template argument
3836/// lists.
3837static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003838void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00003839 TemplateParameterList *New,
3840 TemplateParameterList *Old,
3841 Sema::TemplateParameterListEqualKind Kind,
3842 SourceLocation TemplateArgLoc) {
3843 unsigned NextDiag = diag::err_template_param_list_different_arity;
3844 if (TemplateArgLoc.isValid()) {
3845 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3846 NextDiag = diag::note_template_param_list_different_arity;
3847 }
3848 S.Diag(New->getTemplateLoc(), NextDiag)
3849 << (New->size() > Old->size())
3850 << (Kind != Sema::TPL_TemplateMatch)
3851 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
3852 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
3853 << (Kind != Sema::TPL_TemplateMatch)
3854 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
3855}
3856
Douglas Gregorddc29e12009-02-06 22:42:48 +00003857/// \brief Determine whether the given template parameter lists are
3858/// equivalent.
3859///
Mike Stump1eb44332009-09-09 15:08:12 +00003860/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00003861/// source code as part of a new template declaration.
3862///
3863/// \param Old The old template parameter list, typically found via
3864/// name lookup of the template declared with this template parameter
3865/// list.
3866///
3867/// \param Complain If true, this routine will produce a diagnostic if
3868/// the template parameter lists are not equivalent.
3869///
Douglas Gregorfb898e12009-11-12 16:20:59 +00003870/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00003871///
3872/// \param TemplateArgLoc If this source location is valid, then we
3873/// are actually checking the template parameter list of a template
3874/// argument (New) against the template parameter list of its
3875/// corresponding template template parameter (Old). We produce
3876/// slightly different diagnostics in this scenario.
3877///
Douglas Gregorddc29e12009-02-06 22:42:48 +00003878/// \returns True if the template parameter lists are equal, false
3879/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003880bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00003881Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
3882 TemplateParameterList *Old,
3883 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003884 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00003885 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00003886 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
3887 if (Complain)
3888 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3889 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00003890
3891 return false;
3892 }
3893
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003894 // C++0x [temp.arg.template]p3:
3895 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003896 // when each of the template parameters in the template-parameter-list of
3897 // the template-argument's corresponding class template or template alias
3898 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00003899 // template-parameter-list of P. [...]
3900 TemplateParameterList::iterator NewParm = New->begin();
3901 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00003902 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00003903 OldParmEnd = Old->end();
3904 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00003905 if (Kind != TPL_TemplateTemplateArgumentMatch ||
3906 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00003907 if (NewParm == NewParmEnd) {
3908 if (Complain)
3909 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3910 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003911
Douglas Gregora0347822011-01-13 00:08:50 +00003912 return false;
3913 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003914
Douglas Gregora0347822011-01-13 00:08:50 +00003915 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
3916 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003917 return false;
3918
Douglas Gregora0347822011-01-13 00:08:50 +00003919 ++NewParm;
3920 continue;
3921 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003922
Douglas Gregora0347822011-01-13 00:08:50 +00003923 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003924 // [...] When P's template- parameter-list contains a template parameter
3925 // pack (14.5.3), the template parameter pack will match zero or more
3926 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00003927 // template-parameter-list of A with the same type and form as the
3928 // template parameter pack in P (ignoring whether those template
3929 // parameters are template parameter packs).
3930 for (; NewParm != NewParmEnd; ++NewParm) {
3931 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
3932 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003933 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00003934 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00003935 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003936
Douglas Gregora0347822011-01-13 00:08:50 +00003937 // Make sure we exhausted all of the arguments.
3938 if (NewParm != NewParmEnd) {
3939 if (Complain)
3940 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3941 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003942
Douglas Gregora0347822011-01-13 00:08:50 +00003943 return false;
3944 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003945
Douglas Gregorddc29e12009-02-06 22:42:48 +00003946 return true;
3947}
3948
3949/// \brief Check whether a template can be declared within this scope.
3950///
3951/// If the template declaration is valid in this scope, returns
3952/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00003953bool
Douglas Gregor05396e22009-08-25 17:23:04 +00003954Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00003955 // Find the nearest enclosing declaration scope.
3956 while ((S->getFlags() & Scope::DeclScope) == 0 ||
3957 (S->getFlags() & Scope::TemplateParamScope) != 0)
3958 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00003959
Douglas Gregorddc29e12009-02-06 22:42:48 +00003960 // C++ [temp]p2:
3961 // A template-declaration can appear only as a namespace scope or
3962 // class scope declaration.
3963 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00003964 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
3965 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00003966 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00003967 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003968
Eli Friedman1503f772009-07-31 01:43:05 +00003969 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00003970 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00003971
3972 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
3973 return false;
3974
Mike Stump1eb44332009-09-09 15:08:12 +00003975 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00003976 diag::err_template_outside_namespace_or_class_scope)
3977 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00003978}
Douglas Gregorcc636682009-02-17 23:15:12 +00003979
Douglas Gregord5cb8762009-10-07 00:13:32 +00003980/// \brief Determine what kind of template specialization the given declaration
3981/// is.
3982static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
3983 if (!D)
3984 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003985
Douglas Gregorf6b11852009-10-08 15:14:33 +00003986 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
3987 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00003988 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
3989 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00003990 if (VarDecl *Var = dyn_cast<VarDecl>(D))
3991 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003992
Douglas Gregord5cb8762009-10-07 00:13:32 +00003993 return TSK_Undeclared;
3994}
3995
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003996/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00003997/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00003998///
Douglas Gregor9302da62009-10-14 23:50:59 +00003999/// This routine determines whether a template specialization can be declared
4000/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004001///
4002/// \param S the semantic analysis object for which this check is being
4003/// performed.
4004///
4005/// \param Specialized the entity being specialized or instantiated, which
4006/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004007/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004008/// member class).
4009///
4010/// \param PrevDecl the previous declaration of this entity, if any.
4011///
4012/// \param Loc the location of the explicit specialization or instantiation of
4013/// this entity.
4014///
4015/// \param IsPartialSpecialization whether this is a partial specialization of
4016/// a class template.
4017///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004018/// \returns true if there was an error that we cannot recover from, false
4019/// otherwise.
4020static bool CheckTemplateSpecializationScope(Sema &S,
4021 NamedDecl *Specialized,
4022 NamedDecl *PrevDecl,
4023 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004024 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004025 // Keep these "kind" numbers in sync with the %select statements in the
4026 // various diagnostics emitted by this routine.
4027 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004028 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004029 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004030 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004031 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004032 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004033 EntityKind = 3;
4034 else if (isa<VarDecl>(Specialized))
4035 EntityKind = 4;
4036 else if (isa<RecordDecl>(Specialized))
4037 EntityKind = 5;
4038 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004039 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4040 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004041 return true;
4042 }
4043
Douglas Gregor88b70942009-02-25 22:02:03 +00004044 // C++ [temp.expl.spec]p2:
4045 // An explicit specialization shall be declared in the namespace
4046 // of which the template is a member, or, for member templates, in
4047 // the namespace of which the enclosing class or enclosing class
4048 // template is a member. An explicit specialization of a member
4049 // function, member class or static data member of a class
4050 // template shall be declared in the namespace of which the class
4051 // template is a member. Such a declaration may also be a
4052 // definition. If the declaration is not a definition, the
4053 // specialization may be defined later in the name- space in which
4054 // the explicit specialization was declared, or in a namespace
4055 // that encloses the one in which the explicit specialization was
4056 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004057 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004058 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004059 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004060 return true;
4061 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004062
Douglas Gregor0a407472009-10-07 17:30:37 +00004063 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
4064 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004065 << Specialized;
Douglas Gregor0a407472009-10-07 17:30:37 +00004066 return true;
4067 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004068
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004069 // C++ [temp.class.spec]p6:
4070 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004071 // in any namespace scope in which its definition may be defined (14.5.1
4072 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004073 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004074 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004075 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004076 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004077 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004078 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4079 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004080 // C++ [temp.exp.spec]p2:
4081 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004082 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004083 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004084 // An explicit specialization of a member function, member class or
4085 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004086 // namespace of which the class template is a member.
4087 //
4088 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004089 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004090 // the specialized template.
4091 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4092 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004093 bool IsCPlusPlus0xExtension
4094 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004095 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004096 S.Diag(Loc, IsCPlusPlus0xExtension
4097 ? diag::ext_template_spec_decl_out_of_scope_global
4098 : diag::err_template_spec_decl_out_of_scope_global)
4099 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004100 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004101 S.Diag(Loc, IsCPlusPlus0xExtension
4102 ? diag::ext_template_spec_decl_out_of_scope
4103 : diag::err_template_spec_decl_out_of_scope)
4104 << EntityKind << Specialized
4105 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004106
Douglas Gregor9302da62009-10-14 23:50:59 +00004107 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4108 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004109 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004110 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004111
4112 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004113 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004114 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004115 // specializations of function templates, static data members, and member
4116 // functions, so we skip the check here for those kinds of entities.
4117 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004118 // Should we refactor that check, so that it occurs later?
4119 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004120 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4121 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004122 if (isa<TranslationUnitDecl>(SpecializedContext))
4123 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4124 << EntityKind << Specialized;
4125 else if (isa<NamespaceDecl>(SpecializedContext))
4126 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4127 << EntityKind << Specialized
4128 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004129
Douglas Gregor9302da62009-10-14 23:50:59 +00004130 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004131 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004132
Douglas Gregord5cb8762009-10-07 00:13:32 +00004133 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004134
Douglas Gregor88b70942009-02-25 22:02:03 +00004135 return false;
4136}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004137
Douglas Gregorbacb9492011-01-03 21:13:47 +00004138/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4139/// that checks non-type template partial specialization arguments.
4140static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4141 NonTypeTemplateParmDecl *Param,
4142 const TemplateArgument *Args,
4143 unsigned NumArgs) {
4144 for (unsigned I = 0; I != NumArgs; ++I) {
4145 if (Args[I].getKind() == TemplateArgument::Pack) {
4146 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004147 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004148 Args[I].pack_size()))
4149 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004150
Douglas Gregore94866f2009-06-12 21:21:02 +00004151 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004152 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004153
Douglas Gregorbacb9492011-01-03 21:13:47 +00004154 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004155 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004156 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004157 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004158
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004159 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004160 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4161 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004162
4163 // Strip off any implicit casts we added as part of type checking.
4164 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4165 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004166
Douglas Gregore94866f2009-06-12 21:21:02 +00004167 // C++ [temp.class.spec]p8:
4168 // A non-type argument is non-specialized if it is the name of a
4169 // non-type parameter. All other non-type arguments are
4170 // specialized.
4171 //
4172 // Below, we check the two conditions that only apply to
4173 // specialized non-type arguments, so skip any non-specialized
4174 // arguments.
4175 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004176 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004177 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004178
Douglas Gregore94866f2009-06-12 21:21:02 +00004179 // C++ [temp.class.spec]p9:
4180 // Within the argument list of a class template partial
4181 // specialization, the following restrictions apply:
4182 // -- A partially specialized non-type argument expression
4183 // shall not involve a template parameter of the partial
4184 // specialization except when the argument expression is a
4185 // simple identifier.
4186 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004187 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004188 diag::err_dependent_non_type_arg_in_partial_spec)
4189 << ArgExpr->getSourceRange();
4190 return true;
4191 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004192
Douglas Gregore94866f2009-06-12 21:21:02 +00004193 // -- The type of a template parameter corresponding to a
4194 // specialized non-type argument shall not be dependent on a
4195 // parameter of the specialization.
4196 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004197 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004198 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4199 << Param->getType()
4200 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004201 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004202 return true;
4203 }
4204 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004205
Douglas Gregorbacb9492011-01-03 21:13:47 +00004206 return false;
4207}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004208
Douglas Gregorbacb9492011-01-03 21:13:47 +00004209/// \brief Check the non-type template arguments of a class template
4210/// partial specialization according to C++ [temp.class.spec]p9.
4211///
4212/// \param TemplateParams the template parameters of the primary class
4213/// template.
4214///
4215/// \param TemplateArg the template arguments of the class template
4216/// partial specialization.
4217///
4218/// \returns true if there was an error, false otherwise.
4219static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4220 TemplateParameterList *TemplateParams,
4221 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4222 const TemplateArgument *ArgList = TemplateArgs.data();
4223
4224 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4225 NonTypeTemplateParmDecl *Param
4226 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4227 if (!Param)
4228 continue;
4229
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004230 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004231 &ArgList[I], 1))
4232 return true;
4233 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004234
4235 return false;
4236}
4237
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004238/// \brief Retrieve the previous declaration of the given declaration.
4239static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4240 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4241 return VD->getPreviousDeclaration();
4242 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4243 return FD->getPreviousDeclaration();
4244 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4245 return TD->getPreviousDeclaration();
4246 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND))
4247 return TD->getPreviousDeclaration();
4248 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4249 return FTD->getPreviousDeclaration();
4250 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4251 return CTD->getPreviousDeclaration();
4252 return 0;
4253}
4254
John McCalld226f652010-08-21 09:40:31 +00004255DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004256Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4257 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004258 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004259 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004260 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004261 SourceLocation TemplateNameLoc,
4262 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004263 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004264 SourceLocation RAngleLoc,
4265 AttributeList *Attr,
4266 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004267 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004268
Douglas Gregorcc636682009-02-17 23:15:12 +00004269 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004270 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004271 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004272 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4273
4274 if (!ClassTemplate) {
4275 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004276 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004277 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4278 return true;
4279 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004280
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004281 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004282 bool isPartialSpecialization = false;
4283
Douglas Gregor88b70942009-02-25 22:02:03 +00004284 // Check the validity of the template headers that introduce this
4285 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004286 // FIXME: We probably shouldn't complain about these headers for
4287 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004288 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004289 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00004290 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
4291 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004292 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004293 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004294 isExplicitSpecialization,
4295 Invalid);
4296 if (Invalid)
4297 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004298
Abramo Bagnara9b934882010-06-12 08:15:14 +00004299 unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size();
4300 if (TemplateParams)
4301 --NumMatchedTemplateParamLists;
4302
Douglas Gregor05396e22009-08-25 17:23:04 +00004303 if (TemplateParams && TemplateParams->size() > 0) {
4304 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004305
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004306 if (TUK == TUK_Friend) {
4307 Diag(KWLoc, diag::err_partial_specialization_friend)
4308 << SourceRange(LAngleLoc, RAngleLoc);
4309 return true;
4310 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004311
Douglas Gregor05396e22009-08-25 17:23:04 +00004312 // C++ [temp.class.spec]p10:
4313 // The template parameter list of a specialization shall not
4314 // contain default template argument values.
4315 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4316 Decl *Param = TemplateParams->getParam(I);
4317 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4318 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004319 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004320 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004321 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004322 }
4323 } else if (NonTypeTemplateParmDecl *NTTP
4324 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4325 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004326 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004327 diag::err_default_arg_in_partial_spec)
4328 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004329 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004330 }
4331 } else {
4332 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004333 if (TTP->hasDefaultArgument()) {
4334 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004335 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004336 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004337 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004338 }
4339 }
4340 }
Douglas Gregora735b202009-10-13 14:39:41 +00004341 } else if (TemplateParams) {
4342 if (TUK == TUK_Friend)
4343 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004344 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004345 SourceRange(TemplateParams->getTemplateLoc(),
4346 TemplateParams->getRAngleLoc()))
4347 << SourceRange(LAngleLoc, RAngleLoc);
4348 else
4349 isExplicitSpecialization = true;
4350 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004351 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004352 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004353 isExplicitSpecialization = true;
4354 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004355
Douglas Gregorcc636682009-02-17 23:15:12 +00004356 // Check that the specialization uses the same tag kind as the
4357 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004358 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4359 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004360 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004361 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004362 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004363 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004364 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004365 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004366 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004367 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004368 diag::note_previous_use);
4369 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4370 }
4371
Douglas Gregor40808ce2009-03-09 23:48:35 +00004372 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004373 TemplateArgumentListInfo TemplateArgs;
4374 TemplateArgs.setLAngleLoc(LAngleLoc);
4375 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004376 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004377
Douglas Gregor925910d2011-01-03 20:35:03 +00004378 // Check for unexpanded parameter packs in any of the template arguments.
4379 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004380 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004381 UPPC_PartialSpecialization))
4382 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004383
Douglas Gregorcc636682009-02-17 23:15:12 +00004384 // Check that the template argument list is well-formed for this
4385 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004386 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004387 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4388 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004389 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004390
Douglas Gregor910f8002010-11-07 23:05:16 +00004391 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004392 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004393
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004394 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004395 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004396 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004397 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004398 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004399 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004400 return true;
4401
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004402 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004403 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004404 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004405 TemplateArgs.size())) {
4406 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4407 << ClassTemplate->getDeclName();
4408 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004409 }
4410 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004411
Douglas Gregorcc636682009-02-17 23:15:12 +00004412 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004413 ClassTemplateSpecializationDecl *PrevDecl = 0;
4414
4415 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004416 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004417 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004418 = ClassTemplate->findPartialSpecialization(Converted.data(),
4419 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004420 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004421 else
4422 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004423 = ClassTemplate->findSpecialization(Converted.data(),
4424 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004425
4426 ClassTemplateSpecializationDecl *Specialization = 0;
4427
Douglas Gregor88b70942009-02-25 22:02:03 +00004428 // Check whether we can declare a class template specialization in
4429 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004430 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004431 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4432 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004433 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004434 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004435
Douglas Gregorb88e8882009-07-30 17:40:51 +00004436 // The canonical type
4437 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004438 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004439 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004440 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004441 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004442 // arguments was referenced but not declared, or we're only
4443 // referencing this specialization as a friend, reuse that
Douglas Gregorcc636682009-02-17 23:15:12 +00004444 // declaration node as our own, updating its source location to
4445 // reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004446 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004447 Specialization->setLocation(TemplateNameLoc);
Douglas Gregorcc636682009-02-17 23:15:12 +00004448 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004449 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004450 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004451 // Build the canonical type that describes the converted template
4452 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004453 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4454 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004455 Converted.data(),
4456 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004457
4458 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004459 ClassTemplate->getInjectedClassNameSpecialization())) {
4460 // C++ [temp.class.spec]p9b3:
4461 //
4462 // -- The argument list of the specialization shall not be identical
4463 // to the implicit argument list of the primary template.
4464 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4465 << (TUK == TUK_Definition)
4466 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4467 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4468 ClassTemplate->getIdentifier(),
4469 TemplateNameLoc,
4470 Attr,
4471 TemplateParams,
4472 AS_none);
4473 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004474
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004475 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004476 ClassTemplatePartialSpecializationDecl *PrevPartial
4477 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004478 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004479 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004480 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004481 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004482 ClassTemplate->getDeclContext(),
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004483 TemplateNameLoc,
4484 TemplateParams,
4485 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004486 Converted.data(),
4487 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004488 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004489 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004490 PrevPartial,
4491 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004492 SetNestedNameSpecifier(Partial, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004493 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004494 Partial->setTemplateParameterListsInfo(Context,
4495 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004496 (TemplateParameterList**) TemplateParameterLists.release());
4497 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004498
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004499 if (!PrevPartial)
4500 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004501 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004502
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004503 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004504 // template specialization, make a note of that.
4505 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4506 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004507
Douglas Gregor031a5882009-06-13 00:26:55 +00004508 // Check that all of the template parameters of the class template
4509 // partial specialization are deducible from the template
4510 // arguments. If not, this class template partial specialization
4511 // will never be used.
4512 llvm::SmallVector<bool, 8> DeducibleParams;
4513 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004514 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004515 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004516 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004517 unsigned NumNonDeducible = 0;
4518 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4519 if (!DeducibleParams[I])
4520 ++NumNonDeducible;
4521
4522 if (NumNonDeducible) {
4523 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4524 << (NumNonDeducible > 1)
4525 << SourceRange(TemplateNameLoc, RAngleLoc);
4526 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4527 if (!DeducibleParams[I]) {
4528 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4529 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004530 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004531 diag::note_partial_spec_unused_parameter)
4532 << Param->getDeclName();
4533 else
Mike Stump1eb44332009-09-09 15:08:12 +00004534 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004535 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004536 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004537 }
4538 }
4539 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004540 } else {
4541 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004542 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004543 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004544 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004545 ClassTemplate->getDeclContext(),
4546 TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004547 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004548 Converted.data(),
4549 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004550 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004551 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004552 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004553 Specialization->setTemplateParameterListsInfo(Context,
4554 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004555 (TemplateParameterList**) TemplateParameterLists.release());
4556 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004557
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004558 if (!PrevDecl)
4559 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004560
4561 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004562 }
4563
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004564 // C++ [temp.expl.spec]p6:
4565 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004566 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004567 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004568 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004569 // use occurs; no diagnostic is required.
4570 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004571 bool Okay = false;
4572 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4573 // Is there any previous explicit specialization declaration?
4574 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4575 Okay = true;
4576 break;
4577 }
4578 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004579
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004580 if (!Okay) {
4581 SourceRange Range(TemplateNameLoc, RAngleLoc);
4582 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
4583 << Context.getTypeDeclType(Specialization) << Range;
4584
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004585 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004586 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004587 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004588 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004589 return true;
4590 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004591 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004592
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004593 // If this is not a friend, note that this is an explicit specialization.
4594 if (TUK != TUK_Friend)
4595 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004596
4597 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004598 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00004599 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004600 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00004601 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004602 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00004603 Diag(Def->getLocation(), diag::note_previous_definition);
4604 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00004605 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004606 }
4607 }
4608
John McCall7f1b9872010-12-18 03:30:47 +00004609 if (Attr)
4610 ProcessDeclAttributeList(S, Specialization, Attr);
4611
Douglas Gregorfc705b82009-02-26 22:19:44 +00004612 // Build the fully-sugared type for this class template
4613 // specialization as the user wrote in the specialization
4614 // itself. This means that we'll pretty-print the type retrieved
4615 // from the specialization's declaration the way that the user
4616 // actually wrote the specialization, rather than formatting the
4617 // name based on the "canonical" representation used to store the
4618 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00004619 TypeSourceInfo *WrittenTy
4620 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
4621 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004622 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004623 Specialization->setTypeAsWritten(WrittenTy);
Douglas Gregor7e9b57b2010-07-06 18:33:12 +00004624 if (TemplateParams)
4625 Specialization->setTemplateKeywordLoc(TemplateParams->getTemplateLoc());
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004626 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00004627 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00004628
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004629 // C++ [temp.expl.spec]p9:
4630 // A template explicit specialization is in the scope of the
4631 // namespace in which the template was defined.
4632 //
4633 // We actually implement this paragraph where we set the semantic
4634 // context (in the creation of the ClassTemplateSpecializationDecl),
4635 // but we also maintain the lexical context where the actual
4636 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00004637 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00004638
Douglas Gregorcc636682009-02-17 23:15:12 +00004639 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004640 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00004641 Specialization->startDefinition();
4642
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004643 if (TUK == TUK_Friend) {
4644 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
4645 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00004646 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004647 /*FIXME:*/KWLoc);
4648 Friend->setAccess(AS_public);
4649 CurContext->addDecl(Friend);
4650 } else {
4651 // Add the specialization into its lexical context, so that it can
4652 // be seen when iterating through the list of declarations in that
4653 // context. However, specializations are not found by name lookup.
4654 CurContext->addDecl(Specialization);
4655 }
John McCalld226f652010-08-21 09:40:31 +00004656 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00004657}
Douglas Gregord57959a2009-03-27 23:10:48 +00004658
John McCalld226f652010-08-21 09:40:31 +00004659Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00004660 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004661 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00004662 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
4663}
4664
John McCalld226f652010-08-21 09:40:31 +00004665Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00004666 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004667 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00004668 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004669 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00004670
Douglas Gregor52591bf2009-06-24 00:54:41 +00004671 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00004672 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00004673 }
Mike Stump1eb44332009-09-09 15:08:12 +00004674
Douglas Gregor52591bf2009-06-24 00:54:41 +00004675 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004676
John McCalld226f652010-08-21 09:40:31 +00004677 Decl *DP = HandleDeclarator(ParentScope, D,
4678 move(TemplateParameterLists),
4679 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00004680 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00004681 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00004682 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00004683 FunctionTemplate->getTemplatedDecl());
4684 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
4685 return ActOnStartOfFunctionDef(FnBodyScope, Function);
4686 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00004687}
4688
John McCall75042392010-02-11 01:33:53 +00004689/// \brief Strips various properties off an implicit instantiation
4690/// that has just been explicitly specialized.
4691static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00004692 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00004693
4694 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4695 FD->setInlineSpecified(false);
4696 }
4697}
4698
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004699/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00004700/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004701/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00004702/// new specialization/instantiation will have any effect.
4703///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004704/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00004705/// instantiation.
4706///
4707/// \param NewTSK the kind of the new explicit specialization or instantiation.
4708///
4709/// \param PrevDecl the previous declaration of the entity.
4710///
4711/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
4712///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004713/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00004714/// declaration was instantiated (either implicitly or explicitly).
4715///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004716/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00004717/// specialization or instantiation has no effect and should be ignored.
4718///
4719/// \returns true if there was an error that should prevent the introduction of
4720/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00004721bool
4722Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
4723 TemplateSpecializationKind NewTSK,
4724 NamedDecl *PrevDecl,
4725 TemplateSpecializationKind PrevTSK,
4726 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004727 bool &HasNoEffect) {
4728 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004729
Douglas Gregor454885e2009-10-15 15:54:05 +00004730 switch (NewTSK) {
4731 case TSK_Undeclared:
4732 case TSK_ImplicitInstantiation:
4733 assert(false && "Don't check implicit instantiations here");
4734 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004735
Douglas Gregor454885e2009-10-15 15:54:05 +00004736 case TSK_ExplicitSpecialization:
4737 switch (PrevTSK) {
4738 case TSK_Undeclared:
4739 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004740 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00004741 // explicitly specialized or has merely been mentioned without any
4742 // instantiation.
4743 return false;
4744
4745 case TSK_ImplicitInstantiation:
4746 if (PrevPointOfInstantiation.isInvalid()) {
4747 // The declaration itself has not actually been instantiated, so it is
4748 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00004749 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00004750 return false;
4751 }
4752 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004753
Douglas Gregor454885e2009-10-15 15:54:05 +00004754 case TSK_ExplicitInstantiationDeclaration:
4755 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004756 assert((PrevTSK == TSK_ImplicitInstantiation ||
4757 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00004758 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004759
Douglas Gregor454885e2009-10-15 15:54:05 +00004760 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004761 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00004762 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004763 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00004764 // implicit instantiation to take place, in every translation unit in
4765 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004766 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4767 // Is there any previous explicit specialization declaration?
4768 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
4769 return false;
4770 }
4771
Douglas Gregor0d035142009-10-27 18:42:08 +00004772 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00004773 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004774 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00004775 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004776
Douglas Gregor454885e2009-10-15 15:54:05 +00004777 return true;
4778 }
4779 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004780
Douglas Gregor454885e2009-10-15 15:54:05 +00004781 case TSK_ExplicitInstantiationDeclaration:
4782 switch (PrevTSK) {
4783 case TSK_ExplicitInstantiationDeclaration:
4784 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004785 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004786 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004787
Douglas Gregor454885e2009-10-15 15:54:05 +00004788 case TSK_Undeclared:
4789 case TSK_ImplicitInstantiation:
4790 // We're explicitly instantiating something that may have already been
4791 // implicitly instantiated; that's fine.
4792 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004793
Douglas Gregor454885e2009-10-15 15:54:05 +00004794 case TSK_ExplicitSpecialization:
4795 // C++0x [temp.explicit]p4:
4796 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004797 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00004798 // specialization for that template, the explicit instantiation has no
4799 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004800 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004801 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004802
Douglas Gregor454885e2009-10-15 15:54:05 +00004803 case TSK_ExplicitInstantiationDefinition:
4804 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004805 // If an entity is the subject of both an explicit instantiation
4806 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00004807 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004808 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00004809 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004810 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004811 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00004812 assert(PrevPointOfInstantiation.isValid() &&
4813 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004814 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004815 return false;
4816 }
4817 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004818
Douglas Gregor454885e2009-10-15 15:54:05 +00004819 case TSK_ExplicitInstantiationDefinition:
4820 switch (PrevTSK) {
4821 case TSK_Undeclared:
4822 case TSK_ImplicitInstantiation:
4823 // We're explicitly instantiating something that may have already been
4824 // implicitly instantiated; that's fine.
4825 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004826
Douglas Gregor454885e2009-10-15 15:54:05 +00004827 case TSK_ExplicitSpecialization:
4828 // C++ DR 259, C++0x [temp.explicit]p4:
4829 // For a given set of template parameters, if an explicit
4830 // instantiation of a template appears after a declaration of
4831 // an explicit specialization for that template, the explicit
4832 // instantiation has no effect.
4833 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004834 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00004835 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00004836 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00004837 if (!getLangOptions().CPlusPlus0x) {
4838 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00004839 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004840 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00004841 diag::note_previous_template_specialization);
4842 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004843 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004844 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004845
Douglas Gregor454885e2009-10-15 15:54:05 +00004846 case TSK_ExplicitInstantiationDeclaration:
4847 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004848 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00004849 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004850
Douglas Gregor454885e2009-10-15 15:54:05 +00004851 case TSK_ExplicitInstantiationDefinition:
4852 // C++0x [temp.spec]p5:
4853 // For a given template and a given set of template-arguments,
4854 // - an explicit instantiation definition shall appear at most once
4855 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00004856 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00004857 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004858 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004859 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004860 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004861 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00004862 }
4863 break;
4864 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004865
Douglas Gregor454885e2009-10-15 15:54:05 +00004866 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004867
Douglas Gregor454885e2009-10-15 15:54:05 +00004868 return false;
4869}
4870
John McCallaf2094e2010-04-08 09:05:18 +00004871/// \brief Perform semantic analysis for the given dependent function
4872/// template specialization. The only possible way to get a dependent
4873/// function template specialization is with a friend declaration,
4874/// like so:
4875///
4876/// template <class T> void foo(T);
4877/// template <class T> class A {
4878/// friend void foo<>(T);
4879/// };
4880///
4881/// There really isn't any useful analysis we can do here, so we
4882/// just store the information.
4883bool
4884Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
4885 const TemplateArgumentListInfo &ExplicitTemplateArgs,
4886 LookupResult &Previous) {
4887 // Remove anything from Previous that isn't a function template in
4888 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004889 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00004890 LookupResult::Filter F = Previous.makeFilter();
4891 while (F.hasNext()) {
4892 NamedDecl *D = F.next()->getUnderlyingDecl();
4893 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00004894 !FDLookupContext->InEnclosingNamespaceSetOf(
4895 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00004896 F.erase();
4897 }
4898 F.done();
4899
4900 // Should this be diagnosed here?
4901 if (Previous.empty()) return true;
4902
4903 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
4904 ExplicitTemplateArgs);
4905 return false;
4906}
4907
Abramo Bagnarae03db982010-05-20 15:32:11 +00004908/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004909/// specialization.
4910///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004911/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004912/// explicit function template specialization. On successful completion,
4913/// the function declaration \p FD will become a function template
4914/// specialization.
4915///
4916/// \param FD the function declaration, which will be updated to become a
4917/// function template specialization.
4918///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004919/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
4920/// if any. Note that this may be valid info even when 0 arguments are
4921/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
4922/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004923///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004924/// \param PrevDecl the set of declarations that may be specialized by
4925/// this function specialization.
4926bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004927Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
John McCalld5532b62009-11-23 01:53:49 +00004928 const TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00004929 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004930 // The set of function template specializations that could match this
4931 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00004932 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004933
Sebastian Redl7a126a42010-08-31 00:36:30 +00004934 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00004935 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
4936 I != E; ++I) {
4937 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
4938 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004939 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004940 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004941 if (!FDLookupContext->InEnclosingNamespaceSetOf(
4942 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004943 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004944
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004945 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004946 // A trailing template-argument can be left unspecified in the
4947 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004948 // provided it can be deduced from the function argument type.
4949 // Perform template argument deduction to determine whether we may be
4950 // specializing this template.
4951 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00004952 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004953 FunctionDecl *Specialization = 0;
4954 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00004955 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004956 FD->getType(),
4957 Specialization,
4958 Info)) {
4959 // FIXME: Template argument deduction failed; record why it failed, so
4960 // that we can provide nifty diagnostics.
4961 (void)TDK;
4962 continue;
4963 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004964
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004965 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00004966 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004967 }
4968 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004969
Douglas Gregorc5df30f2009-09-26 03:41:46 +00004970 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00004971 UnresolvedSetIterator Result
4972 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00004973 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004974 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00004975 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00004976 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00004977 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00004978 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00004979 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004980 return true;
John McCallc373d482010-01-27 01:50:18 +00004981
4982 // Ignore access information; it doesn't figure into redeclaration checking.
4983 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Douglas Gregorc42b6522010-04-09 21:02:29 +00004984 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004985
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004986 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004987 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00004988
4989 // If this is a friend declaration, then we're not really declaring
4990 // an explicit specialization.
4991 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004992
Douglas Gregord5cb8762009-10-07 00:13:32 +00004993 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00004994 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004995 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004996 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004997 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00004998 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004999 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005000
5001 // C++ [temp.expl.spec]p6:
5002 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005003 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005004 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005005 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005006 // use occurs; no diagnostic is required.
5007 FunctionTemplateSpecializationInfo *SpecInfo
5008 = Specialization->getTemplateSpecializationInfo();
5009 assert(SpecInfo && "Function template specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005010
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005011 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005012 if (!isFriend &&
5013 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005014 TSK_ExplicitSpecialization,
5015 Specialization,
5016 SpecInfo->getTemplateSpecializationKind(),
5017 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005018 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005019 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005020
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005021 // Mark the prior declaration as an explicit specialization, so that later
5022 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005023 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005024 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005025 MarkUnusedFileScopedDecl(Specialization);
5026 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005027
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005028 // Turn the given function declaration into a function template
5029 // specialization, with the template arguments from the previous
5030 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005031 // Take copies of (semantic and syntactic) template argument lists.
5032 const TemplateArgumentList* TemplArgs = new (Context)
5033 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5034 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5035 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005036 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005037 TemplArgs, /*InsertPos=*/0,
5038 SpecInfo->getTemplateSpecializationKind(),
5039 TemplArgsAsWritten);
5040
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005041 // The "previous declaration" for this function template specialization is
5042 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005043 Previous.clear();
5044 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005045 return false;
5046}
5047
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005048/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005049/// specialization.
5050///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005051/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005052/// explicit member function specialization. On successful completion,
5053/// the function declaration \p FD will become a member function
5054/// specialization.
5055///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005056/// \param Member the member declaration, which will be updated to become a
5057/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005058///
John McCall68263142009-11-18 22:49:29 +00005059/// \param Previous the set of declarations, one of which may be specialized
5060/// by this function specialization; the set will be modified to contain the
5061/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005062bool
John McCall68263142009-11-18 22:49:29 +00005063Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005064 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005065
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005066 // Try to find the member we are instantiating.
5067 NamedDecl *Instantiation = 0;
5068 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005069 MemberSpecializationInfo *MSInfo = 0;
5070
John McCall68263142009-11-18 22:49:29 +00005071 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005072 // Nowhere to look anyway.
5073 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005074 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5075 I != E; ++I) {
5076 NamedDecl *D = (*I)->getUnderlyingDecl();
5077 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005078 if (Context.hasSameType(Function->getType(), Method->getType())) {
5079 Instantiation = Method;
5080 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005081 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005082 break;
5083 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005084 }
5085 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005086 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005087 VarDecl *PrevVar;
5088 if (Previous.isSingleResult() &&
5089 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005090 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005091 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005092 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005093 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005094 }
5095 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005096 CXXRecordDecl *PrevRecord;
5097 if (Previous.isSingleResult() &&
5098 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5099 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005100 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005101 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005102 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005103 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005104
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005105 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005106 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005107 // specializations are always out-of-line, the caller will complain about
5108 // this mismatch later.
5109 return false;
5110 }
John McCall77e8b112010-04-13 20:37:33 +00005111
5112 // If this is a friend, just bail out here before we start turning
5113 // things into explicit specializations.
5114 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5115 // Preserve instantiation information.
5116 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5117 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5118 cast<CXXMethodDecl>(InstantiatedFrom),
5119 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5120 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5121 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5122 cast<CXXRecordDecl>(InstantiatedFrom),
5123 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5124 }
5125
5126 Previous.clear();
5127 Previous.addDecl(Instantiation);
5128 return false;
5129 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005130
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005131 // Make sure that this is a specialization of a member.
5132 if (!InstantiatedFrom) {
5133 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5134 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005135 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5136 return true;
5137 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005138
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005139 // C++ [temp.expl.spec]p6:
5140 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005141 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005142 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005143 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005144 // use occurs; no diagnostic is required.
5145 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005146
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005147 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005148 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5149 TSK_ExplicitSpecialization,
5150 Instantiation,
5151 MSInfo->getTemplateSpecializationKind(),
5152 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005153 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005154 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005155
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005156 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005157 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005158 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005159 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005160 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005161 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005162
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005163 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005164 // the original declaration to note that it is an explicit specialization
5165 // (if it was previously an implicit instantiation). This latter step
5166 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005167 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005168 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5169 if (InstantiationFunction->getTemplateSpecializationKind() ==
5170 TSK_ImplicitInstantiation) {
5171 InstantiationFunction->setTemplateSpecializationKind(
5172 TSK_ExplicitSpecialization);
5173 InstantiationFunction->setLocation(Member->getLocation());
5174 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005175
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005176 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5177 cast<CXXMethodDecl>(InstantiatedFrom),
5178 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005179 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005180 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005181 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5182 if (InstantiationVar->getTemplateSpecializationKind() ==
5183 TSK_ImplicitInstantiation) {
5184 InstantiationVar->setTemplateSpecializationKind(
5185 TSK_ExplicitSpecialization);
5186 InstantiationVar->setLocation(Member->getLocation());
5187 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005188
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005189 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5190 cast<VarDecl>(InstantiatedFrom),
5191 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005192 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005193 } else {
5194 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005195 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5196 if (InstantiationClass->getTemplateSpecializationKind() ==
5197 TSK_ImplicitInstantiation) {
5198 InstantiationClass->setTemplateSpecializationKind(
5199 TSK_ExplicitSpecialization);
5200 InstantiationClass->setLocation(Member->getLocation());
5201 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005202
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005203 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005204 cast<CXXRecordDecl>(InstantiatedFrom),
5205 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005206 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005207
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005208 // Save the caller the trouble of having to figure out which declaration
5209 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005210 Previous.clear();
5211 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005212 return false;
5213}
5214
Douglas Gregor558c0322009-10-14 23:41:34 +00005215/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005216///
5217/// \returns true if a serious error occurs, false otherwise.
5218static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005219 SourceLocation InstLoc,
5220 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005221 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5222 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005223
Douglas Gregor669eed82010-07-13 00:10:04 +00005224 if (CurContext->isRecord()) {
5225 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5226 << D;
5227 return true;
5228 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005229
Douglas Gregor558c0322009-10-14 23:41:34 +00005230 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005231 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005232 // template.
5233 //
5234 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005235 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005236 !CurContext->Encloses(OrigContext)) {
5237 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005238 S.Diag(InstLoc,
5239 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005240 diag::err_explicit_instantiation_out_of_scope
5241 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005242 << D << NS;
5243 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005244 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005245 S.getLangOptions().CPlusPlus0x?
5246 diag::err_explicit_instantiation_must_be_global
5247 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005248 << D;
5249 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005250 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005251 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005252
Douglas Gregor558c0322009-10-14 23:41:34 +00005253 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005254 // If the name declared in the explicit instantiation is an unqualified
5255 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005256 // its template is declared or, if that namespace is inline (7.3.1), any
5257 // namespace from its enclosing namespace set.
5258 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005259 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005260
5261 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005262 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005263
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005264 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005265 S.getLangOptions().CPlusPlus0x?
5266 diag::err_explicit_instantiation_unqualified_wrong_namespace
5267 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005268 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005269 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005270 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005271}
5272
5273/// \brief Determine whether the given scope specifier has a template-id in it.
5274static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5275 if (!SS.isSet())
5276 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005277
Douglas Gregor558c0322009-10-14 23:41:34 +00005278 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005279 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005280 // or a static data member of a class template specialization, the name of
5281 // the class template specialization in the qualified-id for the member
5282 // name shall be a simple-template-id.
5283 //
5284 // C++98 has the same restriction, just worded differently.
5285 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5286 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005287 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005288 if (isa<TemplateSpecializationType>(T))
5289 return true;
5290
5291 return false;
5292}
5293
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005294// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005295DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005296Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005297 SourceLocation ExternLoc,
5298 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005299 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005300 SourceLocation KWLoc,
5301 const CXXScopeSpec &SS,
5302 TemplateTy TemplateD,
5303 SourceLocation TemplateNameLoc,
5304 SourceLocation LAngleLoc,
5305 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005306 SourceLocation RAngleLoc,
5307 AttributeList *Attr) {
5308 // Find the class template we're specializing
5309 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005310 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005311 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5312
5313 // Check that the specialization uses the same tag kind as the
5314 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005315 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5316 assert(Kind != TTK_Enum &&
5317 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005318 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005319 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005320 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005321 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005322 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005323 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005324 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005325 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005326 diag::note_previous_use);
5327 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5328 }
5329
Douglas Gregor558c0322009-10-14 23:41:34 +00005330 // C++0x [temp.explicit]p2:
5331 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005332 // definition and an explicit instantiation declaration. An explicit
5333 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005334 TemplateSpecializationKind TSK
5335 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5336 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005337
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005338 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005339 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005340 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005341
5342 // Check that the template argument list is well-formed for this
5343 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005344 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005345 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5346 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005347 return true;
5348
Douglas Gregor910f8002010-11-07 23:05:16 +00005349 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005350 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005351
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005352 // Find the class template specialization declaration that
5353 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005354 void *InsertPos = 0;
5355 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005356 = ClassTemplate->findSpecialization(Converted.data(),
5357 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005358
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005359 TemplateSpecializationKind PrevDecl_TSK
5360 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5361
Douglas Gregord5cb8762009-10-07 00:13:32 +00005362 // C++0x [temp.explicit]p2:
5363 // [...] An explicit instantiation shall appear in an enclosing
5364 // namespace of its template. [...]
5365 //
5366 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005367 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5368 SS.isSet()))
5369 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005370
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005371 ClassTemplateSpecializationDecl *Specialization = 0;
5372
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005373 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005374 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005375 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005376 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005377 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005378 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005379 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005380
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005381 // Even though HasNoEffect == true means that this explicit instantiation
5382 // has no effect on semantics, we go on to put its syntax in the AST.
5383
5384 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5385 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005386 // Since the only prior class template specialization with these
5387 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005388 // declaration node as our own, updating the source location
5389 // for the template name to reflect our new declaration.
5390 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005391 Specialization = PrevDecl;
5392 Specialization->setLocation(TemplateNameLoc);
5393 PrevDecl = 0;
5394 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005395 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005396
Douglas Gregor52604ab2009-09-11 21:19:12 +00005397 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005398 // Create a new class template specialization declaration node for
5399 // this explicit specialization.
5400 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005401 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005402 ClassTemplate->getDeclContext(),
5403 TemplateNameLoc,
5404 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005405 Converted.data(),
5406 Converted.size(),
5407 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005408 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005409
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005410 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005411 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005412 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005413 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005414 }
5415
5416 // Build the fully-sugared type for this explicit instantiation as
5417 // the user wrote in the explicit instantiation itself. This means
5418 // that we'll pretty-print the type retrieved from the
5419 // specialization's declaration the way that the user actually wrote
5420 // the explicit instantiation, rather than formatting the name based
5421 // on the "canonical" representation used to store the template
5422 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005423 TypeSourceInfo *WrittenTy
5424 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5425 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005426 Context.getTypeDeclType(Specialization));
5427 Specialization->setTypeAsWritten(WrittenTy);
5428 TemplateArgsIn.release();
5429
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005430 // Set source locations for keywords.
5431 Specialization->setExternLoc(ExternLoc);
5432 Specialization->setTemplateKeywordLoc(TemplateLoc);
5433
5434 // Add the explicit instantiation into its lexical context. However,
5435 // since explicit instantiations are never found by name lookup, we
5436 // just put it into the declaration context directly.
5437 Specialization->setLexicalDeclContext(CurContext);
5438 CurContext->addDecl(Specialization);
5439
5440 // Syntax is now OK, so return if it has no other effect on semantics.
5441 if (HasNoEffect) {
5442 // Set the template specialization kind.
5443 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005444 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005445 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005446
5447 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005448 // A definition of a class template or class member template
5449 // shall be in scope at the point of the explicit instantiation of
5450 // the class template or class member template.
5451 //
5452 // This check comes when we actually try to perform the
5453 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005454 ClassTemplateSpecializationDecl *Def
5455 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005456 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005457 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005458 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005459 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005460 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005461 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5462 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005463
Douglas Gregor0d035142009-10-27 18:42:08 +00005464 // Instantiate the members of this class template specialization.
5465 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005466 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005467 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005468 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5469
5470 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5471 // TSK_ExplicitInstantiationDefinition
5472 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5473 TSK == TSK_ExplicitInstantiationDefinition)
5474 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005475
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005476 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005477 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005478
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005479 // Set the template specialization kind.
5480 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005481 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005482}
5483
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005484// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005485DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005486Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005487 SourceLocation ExternLoc,
5488 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005489 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005490 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005491 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005492 IdentifierInfo *Name,
5493 SourceLocation NameLoc,
5494 AttributeList *Attr) {
5495
Douglas Gregor402abb52009-05-28 23:31:59 +00005496 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005497 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005498 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005499 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5500 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005501 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005502 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005503 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5504
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005505 if (!TagD)
5506 return true;
5507
John McCalld226f652010-08-21 09:40:31 +00005508 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005509 if (Tag->isEnum()) {
5510 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5511 << Context.getTypeDeclType(Tag);
5512 return true;
5513 }
5514
Douglas Gregord0c87372009-05-27 17:30:49 +00005515 if (Tag->isInvalidDecl())
5516 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005517
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005518 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5519 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5520 if (!Pattern) {
5521 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5522 << Context.getTypeDeclType(Record);
5523 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5524 return true;
5525 }
5526
Douglas Gregor558c0322009-10-14 23:41:34 +00005527 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005528 // If the explicit instantiation is for a class or member class, the
5529 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005530 // simple-template-id.
5531 //
5532 // C++98 has the same restriction, just worded differently.
5533 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005534 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005535 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005536
Douglas Gregor558c0322009-10-14 23:41:34 +00005537 // C++0x [temp.explicit]p2:
5538 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005539 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005540 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005541 TemplateSpecializationKind TSK
5542 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5543 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005544
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005545 // C++0x [temp.explicit]p2:
5546 // [...] An explicit instantiation shall appear in an enclosing
5547 // namespace of its template. [...]
5548 //
5549 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005550 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005551
Douglas Gregor454885e2009-10-15 15:54:05 +00005552 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005553 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005554 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005555 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005556 PrevDecl = Record;
5557 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005558 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005559 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005560 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005561 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005562 PrevDecl,
5563 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005564 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005565 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005566 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005567 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005568 return TagD;
5569 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005570
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005571 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00005572 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005573 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005574 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005575 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005576 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005577 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00005578 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005579 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00005580 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
5581 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005582 Diag(Pattern->getLocation(), diag::note_forward_declaration)
5583 << Pattern;
5584 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00005585 } else {
5586 if (InstantiateClass(NameLoc, Record, Def,
5587 getTemplateInstantiationArgs(Record),
5588 TSK))
5589 return true;
5590
Douglas Gregor952b0172010-02-11 01:04:33 +00005591 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00005592 if (!RecordDef)
5593 return true;
5594 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005595 }
5596
Douglas Gregor0d035142009-10-27 18:42:08 +00005597 // Instantiate all of the members of the class.
5598 InstantiateClassMembers(NameLoc, RecordDef,
5599 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005600
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005601 if (TSK == TSK_ExplicitInstantiationDefinition)
5602 MarkVTableUsed(NameLoc, RecordDef, true);
5603
Mike Stump390b4cc2009-05-16 07:39:55 +00005604 // FIXME: We don't have any representation for explicit instantiations of
5605 // member classes. Such a representation is not needed for compilation, but it
5606 // should be available for clients that want to see all of the declarations in
5607 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005608 return TagD;
5609}
5610
John McCallf312b1e2010-08-26 23:41:50 +00005611DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
5612 SourceLocation ExternLoc,
5613 SourceLocation TemplateLoc,
5614 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00005615 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00005616 // TODO: check if/when DNInfo should replace Name.
5617 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
5618 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005619 if (!Name) {
5620 if (!D.isInvalidType())
5621 Diag(D.getDeclSpec().getSourceRange().getBegin(),
5622 diag::err_explicit_instantiation_requires_name)
5623 << D.getDeclSpec().getSourceRange()
5624 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005625
Douglas Gregord5a423b2009-09-25 18:43:00 +00005626 return true;
5627 }
5628
5629 // The scope passed in may not be a decl scope. Zip up the scope tree until
5630 // we find one that is.
5631 while ((S->getFlags() & Scope::DeclScope) == 0 ||
5632 (S->getFlags() & Scope::TemplateParamScope) != 0)
5633 S = S->getParent();
5634
5635 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00005636 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
5637 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005638 if (R.isNull())
5639 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005640
Douglas Gregord5a423b2009-09-25 18:43:00 +00005641 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
5642 // Cannot explicitly instantiate a typedef.
5643 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
5644 << Name;
5645 return true;
5646 }
5647
Douglas Gregor663b5a02009-10-14 20:14:33 +00005648 // C++0x [temp.explicit]p1:
5649 // [...] An explicit instantiation of a function template shall not use the
5650 // inline or constexpr specifiers.
5651 // Presumably, this also applies to member functions of class templates as
5652 // well.
5653 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005654 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00005655 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00005656 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005657
Douglas Gregor663b5a02009-10-14 20:14:33 +00005658 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005659
Douglas Gregor558c0322009-10-14 23:41:34 +00005660 // C++0x [temp.explicit]p2:
5661 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005662 // definition and an explicit instantiation declaration. An explicit
5663 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00005664 TemplateSpecializationKind TSK
5665 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5666 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005667
Abramo Bagnara25777432010-08-11 22:01:17 +00005668 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00005669 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005670
5671 if (!R->isFunctionType()) {
5672 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005673 // A [...] static data member of a class template can be explicitly
5674 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005675 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00005676 if (Previous.isAmbiguous())
5677 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005678
John McCall1bcee0a2009-12-02 08:25:40 +00005679 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005680 if (!Prev || !Prev->isStaticDataMember()) {
5681 // We expect to see a data data member here.
5682 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
5683 << Name;
5684 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5685 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00005686 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00005687 return true;
5688 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005689
Douglas Gregord5a423b2009-09-25 18:43:00 +00005690 if (!Prev->getInstantiatedFromStaticDataMember()) {
5691 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005692 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005693 diag::err_explicit_instantiation_data_member_not_instantiated)
5694 << Prev;
5695 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
5696 // FIXME: Can we provide a note showing where this was declared?
5697 return true;
5698 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005699
Douglas Gregor558c0322009-10-14 23:41:34 +00005700 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005701 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005702 // or a static data member of a class template specialization, the name of
5703 // the class template specialization in the qualified-id for the member
5704 // name shall be a simple-template-id.
5705 //
5706 // C++98 has the same restriction, just worded differently.
5707 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005708 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005709 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005710 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005711
Douglas Gregor558c0322009-10-14 23:41:34 +00005712 // Check the scope of this explicit instantiation.
5713 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005714
Douglas Gregor454885e2009-10-15 15:54:05 +00005715 // Verify that it is okay to explicitly instantiate here.
5716 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
5717 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005718 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005719 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00005720 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005721 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005722 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005723 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005724 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005725 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005726
Douglas Gregord5a423b2009-09-25 18:43:00 +00005727 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005728 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005729 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005730 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005731
Douglas Gregord5a423b2009-09-25 18:43:00 +00005732 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00005733 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005734 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005735
5736 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00005737 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00005738 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00005739 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005740 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
5741 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00005742 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
5743 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00005744 ASTTemplateArgsPtr TemplateArgsPtr(*this,
5745 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00005746 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00005747 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00005748 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00005749 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00005750 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005751
Douglas Gregord5a423b2009-09-25 18:43:00 +00005752 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005753 // A [...] function [...] can be explicitly instantiated from its template.
5754 // A member function [...] of a class template can be explicitly
5755 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005756 // template.
John McCallc373d482010-01-27 01:50:18 +00005757 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005758 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5759 P != PEnd; ++P) {
5760 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00005761 if (!HasExplicitTemplateArgs) {
5762 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
5763 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
5764 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00005765
John McCallc373d482010-01-27 01:50:18 +00005766 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00005767 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
5768 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00005769 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00005770 }
5771 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005772
Douglas Gregord5a423b2009-09-25 18:43:00 +00005773 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
5774 if (!FunTmpl)
5775 continue;
5776
John McCall5769d612010-02-08 23:07:23 +00005777 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005778 FunctionDecl *Specialization = 0;
5779 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005780 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00005781 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005782 R, Specialization, Info)) {
5783 // FIXME: Keep track of almost-matches?
5784 (void)TDK;
5785 continue;
5786 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005787
John McCallc373d482010-01-27 01:50:18 +00005788 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005789 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005790
Douglas Gregord5a423b2009-09-25 18:43:00 +00005791 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005792 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005793 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005794 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005795 PDiag(diag::err_explicit_instantiation_not_known) << Name,
5796 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
5797 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00005798
John McCallc373d482010-01-27 01:50:18 +00005799 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00005800 return true;
John McCallc373d482010-01-27 01:50:18 +00005801
5802 // Ignore access control bits, we don't need them for redeclaration checking.
5803 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005804
Douglas Gregor0a897e32009-10-15 17:21:20 +00005805 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005806 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005807 diag::err_explicit_instantiation_member_function_not_instantiated)
5808 << Specialization
5809 << (Specialization->getTemplateSpecializationKind() ==
5810 TSK_ExplicitSpecialization);
5811 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
5812 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005813 }
5814
Douglas Gregor0a897e32009-10-15 17:21:20 +00005815 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00005816 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
5817 PrevDecl = Specialization;
5818
Douglas Gregor0a897e32009-10-15 17:21:20 +00005819 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005820 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005821 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005822 PrevDecl,
5823 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00005824 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005825 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00005826 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005827
Douglas Gregor0a897e32009-10-15 17:21:20 +00005828 // FIXME: We may still want to build some representation of this
5829 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005830 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005831 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00005832 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00005833
5834 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005835
Douglas Gregor0a897e32009-10-15 17:21:20 +00005836 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005837 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005838
Douglas Gregor558c0322009-10-14 23:41:34 +00005839 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005840 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005841 // or a static data member of a class template specialization, the name of
5842 // the class template specialization in the qualified-id for the member
5843 // name shall be a simple-template-id.
5844 //
5845 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005846 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005847 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005848 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00005849 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005850 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005851 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005852 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005853
Douglas Gregor558c0322009-10-14 23:41:34 +00005854 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005855 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00005856 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005857 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00005858 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005859
Douglas Gregord5a423b2009-09-25 18:43:00 +00005860 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00005861 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005862}
5863
John McCallf312b1e2010-08-26 23:41:50 +00005864TypeResult
John McCallc4e70192009-09-11 04:59:25 +00005865Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
5866 const CXXScopeSpec &SS, IdentifierInfo *Name,
5867 SourceLocation TagLoc, SourceLocation NameLoc) {
5868 // This has to hold, because SS is expected to be defined.
5869 assert(Name && "Expected a name in a dependent tag");
5870
5871 NestedNameSpecifier *NNS
5872 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
5873 if (!NNS)
5874 return true;
5875
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005876 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00005877
Douglas Gregor48c89f42010-04-24 16:38:41 +00005878 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
5879 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005880 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00005881 return true;
5882 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005883
5884 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
John McCallb3d87482010-08-24 05:47:05 +00005885 return ParsedType::make(Context.getDependentNameType(Kwd, NNS, Name));
John McCallc4e70192009-09-11 04:59:25 +00005886}
5887
John McCallf312b1e2010-08-26 23:41:50 +00005888TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005889Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
5890 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005891 SourceLocation IdLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00005892 NestedNameSpecifier *NNS
Douglas Gregord57959a2009-03-27 23:10:48 +00005893 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
5894 if (!NNS)
5895 return true;
5896
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005897 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
5898 !getLangOptions().CPlusPlus0x)
5899 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005900 << FixItHint::CreateRemoval(TypenameLoc);
5901
Douglas Gregor107de902010-04-24 15:35:55 +00005902 QualType T = CheckTypenameType(ETK_Typename, NNS, II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00005903 TypenameLoc, SS.getRange(), IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00005904 if (T.isNull())
5905 return true;
John McCall63b43852010-04-29 23:50:39 +00005906
5907 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
5908 if (isa<DependentNameType>(T)) {
5909 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00005910 TL.setKeywordLoc(TypenameLoc);
5911 TL.setQualifierRange(SS.getRange());
5912 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00005913 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005914 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00005915 TL.setKeywordLoc(TypenameLoc);
5916 TL.setQualifierRange(SS.getRange());
5917 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00005918 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005919
John McCallb3d87482010-08-24 05:47:05 +00005920 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00005921}
5922
John McCallf312b1e2010-08-26 23:41:50 +00005923TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005924Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
5925 const CXXScopeSpec &SS, SourceLocation TemplateLoc,
John McCallb3d87482010-08-24 05:47:05 +00005926 ParsedType Ty) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005927 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
5928 !getLangOptions().CPlusPlus0x)
5929 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005930 << FixItHint::CreateRemoval(TypenameLoc);
5931
John McCall4e449832010-05-28 23:32:21 +00005932 TypeSourceInfo *InnerTSI = 0;
5933 QualType T = GetTypeFromParser(Ty, &InnerTSI);
John McCall4e449832010-05-28 23:32:21 +00005934
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005935 assert(isa<TemplateSpecializationType>(T) &&
John McCall4e449832010-05-28 23:32:21 +00005936 "Expected a template specialization type");
Douglas Gregor17343172009-04-01 00:28:59 +00005937
Douglas Gregor6946baf2009-09-02 13:05:45 +00005938 if (computeDeclContext(SS, false)) {
5939 // If we can compute a declaration context, then the "typename"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005940 // keyword was superfluous. Just build an ElaboratedType to keep
Douglas Gregor6946baf2009-09-02 13:05:45 +00005941 // track of the nested-name-specifier.
John McCall4e449832010-05-28 23:32:21 +00005942
5943 // Push the inner type, preserving its source locations if possible.
5944 TypeLocBuilder Builder;
5945 if (InnerTSI)
5946 Builder.pushFullCopy(InnerTSI->getTypeLoc());
5947 else
Douglas Gregorc21c7e92011-01-25 19:13:18 +00005948 Builder.push<TemplateSpecializationTypeLoc>(T).initialize(Context,
5949 TemplateLoc);
John McCall4e449832010-05-28 23:32:21 +00005950
Abramo Bagnara22f638a2010-08-10 13:46:45 +00005951 /* Note: NNS already embedded in template specialization type T. */
5952 T = Context.getElaboratedType(ETK_Typename, /*NNS=*/0, T);
John McCall4e449832010-05-28 23:32:21 +00005953 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
5954 TL.setKeywordLoc(TypenameLoc);
5955 TL.setQualifierRange(SS.getRange());
5956
5957 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
John McCallb3d87482010-08-24 05:47:05 +00005958 return CreateParsedType(T, TSI);
Douglas Gregor6946baf2009-09-02 13:05:45 +00005959 }
Mike Stump1eb44332009-09-09 15:08:12 +00005960
John McCall33500952010-06-11 00:33:02 +00005961 // TODO: it's really silly that we make a template specialization
5962 // type earlier only to drop it again here.
John McCallf4c73712011-01-19 06:33:43 +00005963 const TemplateSpecializationType *TST = cast<TemplateSpecializationType>(T);
John McCall33500952010-06-11 00:33:02 +00005964 DependentTemplateName *DTN =
5965 TST->getTemplateName().getAsDependentTemplateName();
5966 assert(DTN && "dependent template has non-dependent name?");
Abramo Bagnara22f638a2010-08-10 13:46:45 +00005967 assert(DTN->getQualifier()
5968 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
5969 T = Context.getDependentTemplateSpecializationType(ETK_Typename,
5970 DTN->getQualifier(),
John McCall33500952010-06-11 00:33:02 +00005971 DTN->getIdentifier(),
5972 TST->getNumArgs(),
5973 TST->getArgs());
John McCall63b43852010-04-29 23:50:39 +00005974 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
John McCall33500952010-06-11 00:33:02 +00005975 DependentTemplateSpecializationTypeLoc TL =
5976 cast<DependentTemplateSpecializationTypeLoc>(TSI->getTypeLoc());
5977 if (InnerTSI) {
5978 TemplateSpecializationTypeLoc TSTL =
5979 cast<TemplateSpecializationTypeLoc>(InnerTSI->getTypeLoc());
5980 TL.setLAngleLoc(TSTL.getLAngleLoc());
5981 TL.setRAngleLoc(TSTL.getRAngleLoc());
5982 for (unsigned I = 0, E = TST->getNumArgs(); I != E; ++I)
5983 TL.setArgLocInfo(I, TSTL.getArgLocInfo(I));
5984 } else {
Douglas Gregorc21c7e92011-01-25 19:13:18 +00005985 // FIXME: Poor source-location information here.
5986 TL.initializeLocal(Context, TemplateLoc);
John McCall33500952010-06-11 00:33:02 +00005987 }
John McCall4e449832010-05-28 23:32:21 +00005988 TL.setKeywordLoc(TypenameLoc);
5989 TL.setQualifierRange(SS.getRange());
John McCallb3d87482010-08-24 05:47:05 +00005990 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00005991}
5992
Douglas Gregord57959a2009-03-27 23:10:48 +00005993/// \brief Build the type that describes a C++ typename specifier,
5994/// e.g., "typename T::type".
5995QualType
Douglas Gregor107de902010-04-24 15:35:55 +00005996Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
5997 NestedNameSpecifier *NNS, const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00005998 SourceLocation KeywordLoc, SourceRange NNSRange,
5999 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006000 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00006001 SS.MakeTrivial(Context, NNS, NNSRange);
Douglas Gregord57959a2009-03-27 23:10:48 +00006002
John McCall77bb1aa2010-05-01 00:40:08 +00006003 DeclContext *Ctx = computeDeclContext(SS);
6004 if (!Ctx) {
6005 // If the nested-name-specifier is dependent and couldn't be
6006 // resolved to a type, build a typename type.
6007 assert(NNS->isDependent());
6008 return Context.getDependentNameType(Keyword, NNS, &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006009 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006010
John McCall77bb1aa2010-05-01 00:40:08 +00006011 // If the nested-name-specifier refers to the current instantiation,
6012 // the "typename" keyword itself is superfluous. In C++03, the
6013 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6014 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006015 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006016
John McCall77bb1aa2010-05-01 00:40:08 +00006017 if (RequireCompleteDeclContext(SS, Ctx))
6018 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006019
6020 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006021 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006022 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006023 unsigned DiagID = 0;
6024 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006025 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006026 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006027 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006028 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006029
6030 case LookupResult::FoundUnresolvedValue: {
6031 // We found a using declaration that is a value. Most likely, the using
6032 // declaration itself is meant to have the 'typename' keyword.
6033 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(),
6034 IILoc);
6035 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6036 << Name << Ctx << FullRange;
6037 if (UnresolvedUsingValueDecl *Using
6038 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006039 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006040 Diag(Loc, diag::note_using_value_decl_missing_typename)
6041 << FixItHint::CreateInsertion(Loc, "typename ");
6042 }
6043 }
6044 // Fall through to create a dependent typename type, from which we can recover
6045 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006046
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006047 case LookupResult::NotFoundInCurrentInstantiation:
6048 // Okay, it's a member of an unknown instantiation.
Douglas Gregor107de902010-04-24 15:35:55 +00006049 return Context.getDependentNameType(Keyword, NNS, &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006050
6051 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006052 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006053 // We found a type. Build an ElaboratedType, since the
6054 // typename-specifier was just sugar.
6055 return Context.getElaboratedType(ETK_Typename, NNS,
6056 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006057 }
6058
6059 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006060 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006061 break;
6062
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006063
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006064 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006065 return QualType();
6066
Douglas Gregord57959a2009-03-27 23:10:48 +00006067 case LookupResult::FoundOverloaded:
6068 DiagID = diag::err_typename_nested_not_type;
6069 Referenced = *Result.begin();
6070 break;
6071
John McCall6e247262009-10-10 05:48:19 +00006072 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006073 return QualType();
6074 }
6075
6076 // If we get here, it's because name lookup did not find a
6077 // type. Emit an appropriate diagnostic and return an error.
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006078 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(),
6079 IILoc);
6080 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006081 if (Referenced)
6082 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6083 << Name;
6084 return QualType();
6085}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006086
6087namespace {
6088 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006089 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006090 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006091 SourceLocation Loc;
6092 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006093
Douglas Gregor4a959d82009-08-06 16:20:37 +00006094 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006095 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006096
Mike Stump1eb44332009-09-09 15:08:12 +00006097 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006098 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006099 DeclarationName Entity)
6100 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006101 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006102
6103 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006104 /// transformed.
6105 ///
6106 /// For the purposes of type reconstruction, a type has already been
6107 /// transformed if it is NULL or if it is not dependent.
6108 bool AlreadyTransformed(QualType T) {
6109 return T.isNull() || !T->isDependentType();
6110 }
Mike Stump1eb44332009-09-09 15:08:12 +00006111
6112 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006113 /// rebuilt.
6114 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006115
Douglas Gregor4a959d82009-08-06 16:20:37 +00006116 /// \brief Returns the name of the entity whose type is being rebuilt.
6117 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006118
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006119 /// \brief Sets the "base" location and entity when that
6120 /// information is known based on another transformation.
6121 void setBase(SourceLocation Loc, DeclarationName Entity) {
6122 this->Loc = Loc;
6123 this->Entity = Entity;
6124 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006125 };
6126}
6127
Douglas Gregor4a959d82009-08-06 16:20:37 +00006128/// \brief Rebuilds a type within the context of the current instantiation.
6129///
Mike Stump1eb44332009-09-09 15:08:12 +00006130/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006131/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006132/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006133/// partial specialization thereof). This routine will rebuild that type now
6134/// that we have entered the declarator's scope, which may produce different
6135/// canonical types, e.g.,
6136///
6137/// \code
6138/// template<typename T>
6139/// struct X {
6140/// typedef T* pointer;
6141/// pointer data();
6142/// };
6143///
6144/// template<typename T>
6145/// typename X<T>::pointer X<T>::data() { ... }
6146/// \endcode
6147///
Douglas Gregor4714c122010-03-31 17:34:00 +00006148/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006149/// since we do not know that we can look into X<T> when we parsed the type.
6150/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006151/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006152/// as the canonical type of T*, allowing the return types of the out-of-line
6153/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006154TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6155 SourceLocation Loc,
6156 DeclarationName Name) {
6157 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006158 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006159
Douglas Gregor4a959d82009-08-06 16:20:37 +00006160 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6161 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006162}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006163
John McCall60d7b3a2010-08-24 06:29:42 +00006164ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006165 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6166 DeclarationName());
6167 return Rebuilder.TransformExpr(E);
6168}
6169
John McCall63b43852010-04-29 23:50:39 +00006170bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
6171 if (SS.isInvalid()) return true;
John McCall31f17ec2010-04-27 00:57:59 +00006172
6173 NestedNameSpecifier *NNS = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
6174 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6175 DeclarationName());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006176 NestedNameSpecifier *Rebuilt =
John McCall31f17ec2010-04-27 00:57:59 +00006177 Rebuilder.TransformNestedNameSpecifier(NNS, SS.getRange());
John McCall63b43852010-04-29 23:50:39 +00006178 if (!Rebuilt) return true;
6179
Douglas Gregorc34348a2011-02-24 17:54:50 +00006180 SS.MakeTrivial(Context, Rebuilt, SS.getRange());
John McCall63b43852010-04-29 23:50:39 +00006181 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006182}
6183
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006184/// \brief Produces a formatted string that describes the binding of
6185/// template parameters to template arguments.
6186std::string
6187Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6188 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006189 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006190}
6191
6192std::string
6193Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6194 const TemplateArgument *Args,
6195 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006196 llvm::SmallString<128> Str;
6197 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006198
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006199 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006200 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006201
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006202 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006203 if (I >= NumArgs)
6204 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006205
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006206 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006207 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006208 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006209 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006210
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006211 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006212 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006213 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006214 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006215 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006216
Douglas Gregor87dd6972010-12-20 16:52:59 +00006217 Out << " = ";
6218 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006219 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006220
6221 Out << ']';
6222 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006223}