blob: cbfd7b09394843dd9f23edf982edf0eceeb4ffc3 [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,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000403 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +0000404 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000405 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000406}
407
Douglas Gregor72c3f312008-12-05 18:15:24 +0000408/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
409/// that the template parameter 'PrevDecl' is being shadowed by a new
410/// declaration at location Loc. Returns true to indicate that this is
411/// an error, and false otherwise.
412bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000413 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000414
415 // Microsoft Visual C++ permits template parameters to be shadowed.
416 if (getLangOptions().Microsoft)
417 return false;
418
419 // C++ [temp.local]p4:
420 // A template-parameter shall not be redeclared within its
421 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000422 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000423 << cast<NamedDecl>(PrevDecl)->getDeclName();
424 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
425 return true;
426}
427
Douglas Gregor2943aed2009-03-03 04:44:36 +0000428/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000429/// the parameter D to reference the templated declaration and return a pointer
430/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000431TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
432 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
433 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000434 return Temp;
435 }
436 return 0;
437}
438
Douglas Gregorba68eca2011-01-05 17:40:24 +0000439ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
440 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000441 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000442 "Only template template arguments can be pack expansions here");
443 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
444 "Template template argument pack expansion without packs");
445 ParsedTemplateArgument Result(*this);
446 Result.EllipsisLoc = EllipsisLoc;
447 return Result;
448}
449
Douglas Gregor788cd062009-11-11 01:00:40 +0000450static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
451 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000452
Douglas Gregor788cd062009-11-11 01:00:40 +0000453 switch (Arg.getKind()) {
454 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000455 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000456 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000457 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000458 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000459 return TemplateArgumentLoc(TemplateArgument(T), DI);
460 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000461
Douglas Gregor788cd062009-11-11 01:00:40 +0000462 case ParsedTemplateArgument::NonType: {
463 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
464 return TemplateArgumentLoc(TemplateArgument(E), E);
465 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000466
Douglas Gregor788cd062009-11-11 01:00:40 +0000467 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000468 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000469 TemplateArgument TArg;
470 if (Arg.getEllipsisLoc().isValid())
471 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
472 else
473 TArg = Template;
474 return TemplateArgumentLoc(TArg,
Douglas Gregor788cd062009-11-11 01:00:40 +0000475 Arg.getScopeSpec().getRange(),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000476 Arg.getLocation(),
477 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000478 }
479 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000480
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000481 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000482 return TemplateArgumentLoc();
483}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000484
Douglas Gregor788cd062009-11-11 01:00:40 +0000485/// \brief Translates template arguments as provided by the parser
486/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000487void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
488 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000489 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000490 TemplateArgs.addArgument(translateTemplateArgument(*this,
491 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000492}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000493
Douglas Gregor72c3f312008-12-05 18:15:24 +0000494/// ActOnTypeParameter - Called when a C++ template type parameter
495/// (e.g., "typename T") has been parsed. Typename specifies whether
496/// the keyword "typename" was used to declare the type parameter
497/// (otherwise, "class" was used), and KeyLoc is the location of the
498/// "class" or "typename" keyword. ParamName is the name of the
499/// parameter (NULL indicates an unnamed template parameter) and
Douglas Gregorefed5c82010-06-16 15:23:05 +0000500/// ParamName is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000501/// If the type parameter has a default argument, it will be added
502/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000503Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
504 SourceLocation EllipsisLoc,
505 SourceLocation KeyLoc,
506 IdentifierInfo *ParamName,
507 SourceLocation ParamNameLoc,
508 unsigned Depth, unsigned Position,
509 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000510 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000511 assert(S->isTemplateParamScope() &&
512 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000513 bool Invalid = false;
514
515 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000516 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000517 LookupOrdinaryName,
518 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000519 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000520 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000521 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000522 }
523
Douglas Gregorddc29e12009-02-06 22:42:48 +0000524 SourceLocation Loc = ParamNameLoc;
525 if (!ParamName)
526 Loc = KeyLoc;
527
Douglas Gregor72c3f312008-12-05 18:15:24 +0000528 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000529 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
530 Loc, Depth, Position, ParamName, Typename,
Anders Carlsson6d845ae2009-06-12 22:23:22 +0000531 Ellipsis);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000532 if (Invalid)
533 Param->setInvalidDecl();
534
535 if (ParamName) {
536 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000537 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000538 IdResolver.AddDecl(Param);
539 }
540
Douglas Gregor61c4d282011-01-05 15:48:55 +0000541 // C++0x [temp.param]p9:
542 // A default template-argument may be specified for any kind of
543 // template-parameter that is not a template parameter pack.
544 if (DefaultArg && Ellipsis) {
545 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
546 DefaultArg = ParsedType();
547 }
548
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000549 // Handle the default argument, if provided.
550 if (DefaultArg) {
551 TypeSourceInfo *DefaultTInfo;
552 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000553
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000554 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000555
Douglas Gregor6f526752010-12-16 08:48:57 +0000556 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000557 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000558 UPPC_DefaultArgument))
559 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000560
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000561 // Check the template argument itself.
562 if (CheckTemplateArgument(Param, DefaultTInfo)) {
563 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000564 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000565 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000566
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000567 Param->setDefaultArgument(DefaultTInfo, false);
568 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000569
John McCalld226f652010-08-21 09:40:31 +0000570 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000571}
572
Douglas Gregor2943aed2009-03-03 04:44:36 +0000573/// \brief Check that the type of a non-type template parameter is
574/// well-formed.
575///
576/// \returns the (possibly-promoted) parameter type if valid;
577/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000578QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000579Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000580 // We don't allow variably-modified types as the type of non-type template
581 // parameters.
582 if (T->isVariablyModifiedType()) {
583 Diag(Loc, diag::err_variably_modified_nontype_template_param)
584 << T;
585 return QualType();
586 }
587
Douglas Gregor2943aed2009-03-03 04:44:36 +0000588 // C++ [temp.param]p4:
589 //
590 // A non-type template-parameter shall have one of the following
591 // (optionally cv-qualified) types:
592 //
593 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000594 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000595 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000596 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000597 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000598 T->isReferenceType() ||
599 // -- pointer to member.
600 T->isMemberPointerType() ||
601 // If T is a dependent type, we can't do the check now, so we
602 // assume that it is well-formed.
603 T->isDependentType())
604 return T;
605 // C++ [temp.param]p8:
606 //
607 // A non-type template-parameter of type "array of T" or
608 // "function returning T" is adjusted to be of type "pointer to
609 // T" or "pointer to function returning T", respectively.
610 else if (T->isArrayType())
611 // FIXME: Keep the type prior to promotion?
612 return Context.getArrayDecayedType(T);
613 else if (T->isFunctionType())
614 // FIXME: Keep the type prior to promotion?
615 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000616
Douglas Gregor2943aed2009-03-03 04:44:36 +0000617 Diag(Loc, diag::err_template_nontype_parm_bad_type)
618 << T;
619
620 return QualType();
621}
622
John McCalld226f652010-08-21 09:40:31 +0000623Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
624 unsigned Depth,
625 unsigned Position,
626 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000627 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000628 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
629 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000630
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000631 assert(S->isTemplateParamScope() &&
632 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000633 bool Invalid = false;
634
635 IdentifierInfo *ParamName = D.getIdentifier();
636 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000637 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000638 LookupOrdinaryName,
639 ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000640 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000641 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000642 PrevDecl);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000643 }
644
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000645 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
646 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000647 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000648 Invalid = true;
649 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000650
Douglas Gregor10738d32010-12-23 23:51:58 +0000651 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000652 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000653 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
654 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000655 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000656 IsParameterPack, TInfo);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000657 if (Invalid)
658 Param->setInvalidDecl();
659
660 if (D.getIdentifier()) {
661 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000662 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000663 IdResolver.AddDecl(Param);
664 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000665
Douglas Gregor61c4d282011-01-05 15:48:55 +0000666 // C++0x [temp.param]p9:
667 // A default template-argument may be specified for any kind of
668 // template-parameter that is not a template parameter pack.
669 if (Default && IsParameterPack) {
670 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
671 Default = 0;
672 }
673
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000674 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000675 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000676 // Check for unexpanded parameter packs.
677 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
678 return Param;
679
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000680 TemplateArgument Converted;
681 if (CheckTemplateArgument(Param, Param->getType(), Default, Converted)) {
682 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000683 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000684 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000685
John McCall9ae2f072010-08-23 23:25:46 +0000686 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000687 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000688
John McCalld226f652010-08-21 09:40:31 +0000689 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000690}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000691
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000692/// ActOnTemplateTemplateParameter - Called when a C++ template template
693/// parameter (e.g. T in template <template <typename> class T> class array)
694/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000695Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
696 SourceLocation TmpLoc,
697 TemplateParamsTy *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000698 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000699 IdentifierInfo *Name,
700 SourceLocation NameLoc,
701 unsigned Depth,
702 unsigned Position,
703 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000704 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000705 assert(S->isTemplateParamScope() &&
706 "Template template parameter not in template parameter scope!");
707
708 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000709 bool IsParameterPack = EllipsisLoc.isValid();
710 // FIXME: Pack-ness is dropped
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000711 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000712 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000713 NameLoc.isInvalid()? TmpLoc : NameLoc,
714 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000715 Name, Params);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000716
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000717 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000718 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000719 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000720 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000721 IdResolver.AddDecl(Param);
722 }
723
Douglas Gregor6f526752010-12-16 08:48:57 +0000724 if (Params->size() == 0) {
725 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
726 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
727 Param->setInvalidDecl();
728 }
729
Douglas Gregor61c4d282011-01-05 15:48:55 +0000730 // C++0x [temp.param]p9:
731 // A default template-argument may be specified for any kind of
732 // template-parameter that is not a template parameter pack.
733 if (IsParameterPack && !Default.isInvalid()) {
734 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
735 Default = ParsedTemplateArgument();
736 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000737
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000738 if (!Default.isInvalid()) {
739 // Check only that we have a template template argument. We don't want to
740 // try to check well-formedness now, because our template template parameter
741 // might have dependent types in its template parameters, which we wouldn't
742 // be able to match now.
743 //
744 // If none of the template template parameter's template arguments mention
745 // other template parameters, we could actually perform more checking here.
746 // However, it isn't worth doing.
747 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
748 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
749 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
750 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000751 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000752 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000753
Douglas Gregor6f526752010-12-16 08:48:57 +0000754 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000755 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000756 DefaultArg.getArgument().getAsTemplate(),
757 UPPC_DefaultArgument))
758 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000759
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000760 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000761 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000762
John McCalld226f652010-08-21 09:40:31 +0000763 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000764}
765
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000766/// ActOnTemplateParameterList - Builds a TemplateParameterList that
767/// contains the template parameters in Params/NumParams.
768Sema::TemplateParamsTy *
769Sema::ActOnTemplateParameterList(unsigned Depth,
770 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000771 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000772 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000773 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000774 SourceLocation RAngleLoc) {
775 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000776 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000777
Douglas Gregorddc29e12009-02-06 22:42:48 +0000778 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000779 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000780 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000781}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000782
John McCallb6217662010-03-15 10:12:16 +0000783static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
784 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000785 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000786}
787
John McCallf312b1e2010-08-26 23:41:50 +0000788DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000789Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000790 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000791 IdentifierInfo *Name, SourceLocation NameLoc,
792 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000793 TemplateParameterList *TemplateParams,
Anders Carlsson5aeccdb2009-03-26 00:52:18 +0000794 AccessSpecifier AS) {
Mike Stump1eb44332009-09-09 15:08:12 +0000795 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000796 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000797 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000798 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000799
800 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000801 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000802 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000803
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000804 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
805 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000806
807 // There is no such thing as an unnamed class template.
808 if (!Name) {
809 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000810 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000811 }
812
813 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000814 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000815 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000816 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000817 if (SS.isNotEmpty() && !SS.isInvalid()) {
818 SemanticContext = computeDeclContext(SS, true);
819 if (!SemanticContext) {
820 // FIXME: Produce a reasonable diagnostic here
821 return true;
822 }
Mike Stump1eb44332009-09-09 15:08:12 +0000823
John McCall77bb1aa2010-05-01 00:40:08 +0000824 if (RequireCompleteDeclContext(SS, SemanticContext))
825 return true;
826
John McCalla24dc2e2009-11-17 02:14:36 +0000827 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000828 } else {
829 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000830 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000831 }
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Douglas Gregor57265e32010-04-12 16:00:01 +0000833 if (Previous.isAmbiguous())
834 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000835
Douglas Gregorddc29e12009-02-06 22:42:48 +0000836 NamedDecl *PrevDecl = 0;
837 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000838 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000839
Douglas Gregorddc29e12009-02-06 22:42:48 +0000840 // If there is a previous declaration with the same name, check
841 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000842 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000843 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000844
845 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000846 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000847 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000848 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000849 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
850 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000851 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000852 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
853 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
854 PrevClassTemplate
855 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
856 ->getSpecializedTemplate();
857 }
858 }
859
John McCall65c49462009-12-18 11:25:59 +0000860 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000861 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000862 // [...] When looking for a prior declaration of a class or a function
863 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000864 // function is neither a qualified name nor a template-id, scopes outside
865 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000866 if (!SS.isSet()) {
867 DeclContext *OutermostContext = CurContext;
868 while (!OutermostContext->isFileContext())
869 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000870
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000871 if (PrevDecl &&
872 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
873 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
874 SemanticContext = PrevDecl->getDeclContext();
875 } else {
876 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000877 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000878 // declaration.
879 PrevDecl = PrevClassTemplate = 0;
880 SemanticContext = OutermostContext;
881 }
John McCalle129d442009-12-17 23:21:11 +0000882 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000883
John McCalle129d442009-12-17 23:21:11 +0000884 if (CurContext->isDependentContext()) {
885 // If this is a dependent context, we don't want to link the friend
886 // class template to the template in scope, because that would perform
887 // checking of the template parameter lists that can't be performed
888 // until the outer context is instantiated.
889 PrevDecl = PrevClassTemplate = 0;
890 }
891 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
892 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000893
Douglas Gregorddc29e12009-02-06 22:42:48 +0000894 if (PrevClassTemplate) {
895 // Ensure that the template parameter lists are compatible.
896 if (!TemplateParameterListsAreEqual(TemplateParams,
897 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000898 /*Complain=*/true,
899 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000900 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000901
902 // C++ [temp.class]p4:
903 // In a redeclaration, partial specialization, explicit
904 // specialization or explicit instantiation of a class template,
905 // the class-key shall agree in kind with the original class
906 // template declaration (7.1.5.3).
907 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Douglas Gregor501c5ce2009-05-14 16:41:31 +0000908 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000909 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000910 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000911 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000912 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000913 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000914 }
915
Douglas Gregorddc29e12009-02-06 22:42:48 +0000916 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000917 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000918 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000919 Diag(NameLoc, diag::err_redefinition) << Name;
920 Diag(Def->getLocation(), diag::note_previous_definition);
921 // FIXME: Would it make sense to try to "forget" the previous
922 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000923 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000924 }
925 }
926 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
927 // Maybe we will complain about the shadowed template parameter.
928 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
929 // Just pretend that we didn't see the previous declaration.
930 PrevDecl = 0;
931 } else if (PrevDecl) {
932 // C++ [temp]p5:
933 // A class template shall not have the same name as any other
934 // template, class, function, object, enumeration, enumerator,
935 // namespace, or type in the same scope (3.3), except as specified
936 // in (14.5.4).
937 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
938 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000939 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000940 }
941
Douglas Gregord684b002009-02-10 19:49:53 +0000942 // Check the template parameter list of this declaration, possibly
943 // merging in the template parameter list from the previous class
944 // template declaration.
945 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000946 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000947 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000948 SemanticContext->isRecord() &&
949 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000950 ? TPC_ClassTemplateMember
951 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +0000952 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Douglas Gregor57265e32010-04-12 16:00:01 +0000954 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000955 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +0000956 // template out-of-line.
957 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
958 !(TUK == TUK_Friend && CurContext->isDependentContext()))
959 Diag(NameLoc, diag::err_member_def_does_not_match)
960 << Name << SemanticContext << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000961 }
962
Mike Stump1eb44332009-09-09 15:08:12 +0000963 CXXRecordDecl *NewClass =
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000964 CXXRecordDecl::Create(Context, Kind, SemanticContext, NameLoc, Name, KWLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000965 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000966 PrevClassTemplate->getTemplatedDecl() : 0,
967 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +0000968 SetNestedNameSpecifier(NewClass, SS);
Douglas Gregorddc29e12009-02-06 22:42:48 +0000969
970 ClassTemplateDecl *NewTemplate
971 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
972 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +0000973 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +0000974 NewClass->setDescribedClassTemplate(NewTemplate);
975
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000976 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +0000977 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +0000978 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +0000979 assert(T->isDependentType() && "Class template type is not dependent?");
980 (void)T;
981
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000982 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000983 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000984 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +0000985 PrevClassTemplate->getInstantiatedFromMemberTemplate())
986 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000987
Anders Carlsson4cbe82c2009-03-26 01:24:28 +0000988 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +0000989 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +0000990 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Douglas Gregorddc29e12009-02-06 22:42:48 +0000992 // Set the lexical context of these templates
993 NewClass->setLexicalDeclContext(CurContext);
994 NewTemplate->setLexicalDeclContext(CurContext);
995
John McCall0f434ec2009-07-31 02:45:11 +0000996 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +0000997 NewClass->startDefinition();
998
999 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001000 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001001
John McCall05b23ea2009-09-14 21:59:20 +00001002 if (TUK != TUK_Friend)
1003 PushOnScopeChains(NewTemplate, S);
1004 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001005 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001006 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001007 NewClass->setAccess(PrevClassTemplate->getAccess());
1008 }
John McCall05b23ea2009-09-14 21:59:20 +00001009
Douglas Gregord85bea22009-09-26 06:47:28 +00001010 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1011 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001012
John McCall05b23ea2009-09-14 21:59:20 +00001013 // Friend templates are visible in fairly strange ways.
1014 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001015 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001016 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1017 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1018 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001019 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001020 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001021
Douglas Gregord85bea22009-09-26 06:47:28 +00001022 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1023 NewClass->getLocation(),
1024 NewTemplate,
1025 /*FIXME:*/NewClass->getLocation());
1026 Friend->setAccess(AS_public);
1027 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001028 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001029
Douglas Gregord684b002009-02-10 19:49:53 +00001030 if (Invalid) {
1031 NewTemplate->setInvalidDecl();
1032 NewClass->setInvalidDecl();
1033 }
John McCalld226f652010-08-21 09:40:31 +00001034 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001035}
1036
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001037/// \brief Diagnose the presence of a default template argument on a
1038/// template parameter, which is ill-formed in certain contexts.
1039///
1040/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001041static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001042 Sema::TemplateParamListContext TPC,
1043 SourceLocation ParamLoc,
1044 SourceRange DefArgRange) {
1045 switch (TPC) {
1046 case Sema::TPC_ClassTemplate:
1047 return false;
1048
1049 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001050 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001051 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001052 // A default template-argument shall not be specified in a
1053 // function template declaration or a function template
1054 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001055 // If a friend function template declaration specifies a default
1056 // template-argument, that declaration shall be a definition and shall be
1057 // the only declaration of the function template in the translation unit.
1058 // (C++98/03 doesn't have this wording; see DR226).
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001059 if (!S.getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001060 S.Diag(ParamLoc,
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001061 diag::ext_template_parameter_default_in_function_template)
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001062 << DefArgRange;
1063 return false;
1064
1065 case Sema::TPC_ClassTemplateMember:
1066 // C++0x [temp.param]p9:
1067 // A default template-argument shall not be specified in the
1068 // template-parameter-lists of the definition of a member of a
1069 // class template that appears outside of the member's class.
1070 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1071 << DefArgRange;
1072 return true;
1073
1074 case Sema::TPC_FriendFunctionTemplate:
1075 // C++ [temp.param]p9:
1076 // A default template-argument shall not be specified in a
1077 // friend template declaration.
1078 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1079 << DefArgRange;
1080 return true;
1081
1082 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1083 // for friend function templates if there is only a single
1084 // declaration (and it is a definition). Strange!
1085 }
1086
1087 return false;
1088}
1089
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001090/// \brief Check for unexpanded parameter packs within the template parameters
1091/// of a template template parameter, recursively.
1092bool DiagnoseUnexpandedParameterPacks(Sema &S, TemplateTemplateParmDecl *TTP){
1093 TemplateParameterList *Params = TTP->getTemplateParameters();
1094 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1095 NamedDecl *P = Params->getParam(I);
1096 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001097 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001098 NTTP->getTypeSourceInfo(),
1099 Sema::UPPC_NonTypeTemplateParameterType))
1100 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001101
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001102 continue;
1103 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001104
1105 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001106 = dyn_cast<TemplateTemplateParmDecl>(P))
1107 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1108 return true;
1109 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001110
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001111 return false;
1112}
1113
Douglas Gregord684b002009-02-10 19:49:53 +00001114/// \brief Checks the validity of a template parameter list, possibly
1115/// considering the template parameter list from a previous
1116/// declaration.
1117///
1118/// If an "old" template parameter list is provided, it must be
1119/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1120/// template parameter list.
1121///
1122/// \param NewParams Template parameter list for a new template
1123/// declaration. This template parameter list will be updated with any
1124/// default arguments that are carried through from the previous
1125/// template parameter list.
1126///
1127/// \param OldParams If provided, template parameter list from a
1128/// previous declaration of the same template. Default template
1129/// arguments will be merged from the old template parameter list to
1130/// the new template parameter list.
1131///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001132/// \param TPC Describes the context in which we are checking the given
1133/// template parameter list.
1134///
Douglas Gregord684b002009-02-10 19:49:53 +00001135/// \returns true if an error occurred, false otherwise.
1136bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001137 TemplateParameterList *OldParams,
1138 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001139 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Douglas Gregord684b002009-02-10 19:49:53 +00001141 // C++ [temp.param]p10:
1142 // The set of default template-arguments available for use with a
1143 // template declaration or definition is obtained by merging the
1144 // default arguments from the definition (if in scope) and all
1145 // declarations in scope in the same way default function
1146 // arguments are (8.3.6).
1147 bool SawDefaultArgument = false;
1148 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001149
Anders Carlsson49d25572009-06-12 23:20:15 +00001150 bool SawParameterPack = false;
1151 SourceLocation ParameterPackLoc;
1152
Mike Stump1a35fde2009-02-11 23:03:27 +00001153 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001154 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001155 if (OldParams)
1156 OldParam = OldParams->begin();
1157
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001158 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001159 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1160 NewParamEnd = NewParams->end();
1161 NewParam != NewParamEnd; ++NewParam) {
1162 // Variables used to diagnose redundant default arguments
1163 bool RedundantDefaultArg = false;
1164 SourceLocation OldDefaultLoc;
1165 SourceLocation NewDefaultLoc;
1166
1167 // Variables used to diagnose missing default arguments
1168 bool MissingDefaultArg = false;
1169
Anders Carlsson49d25572009-06-12 23:20:15 +00001170 // C++0x [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001171 // If a template parameter of a primary class template is a template
Douglas Gregor1ed64762011-01-05 16:19:19 +00001172 // parameter pack, it shall be the last template parameter.
1173 if (SawParameterPack && TPC == TPC_ClassTemplate) {
Mike Stump1eb44332009-09-09 15:08:12 +00001174 Diag(ParameterPackLoc,
Anders Carlsson49d25572009-06-12 23:20:15 +00001175 diag::err_template_param_pack_must_be_last_template_parameter);
1176 Invalid = true;
1177 }
1178
Douglas Gregord684b002009-02-10 19:49:53 +00001179 if (TemplateTypeParmDecl *NewTypeParm
1180 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001181 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001182 if (NewTypeParm->hasDefaultArgument() &&
1183 DiagnoseDefaultTemplateArgument(*this, TPC,
1184 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001185 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001186 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001187 NewTypeParm->removeDefaultArgument();
1188
1189 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001190 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001191 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Anders Carlsson49d25572009-06-12 23:20:15 +00001193 if (NewTypeParm->isParameterPack()) {
1194 assert(!NewTypeParm->hasDefaultArgument() &&
1195 "Parameter packs can't have a default argument!");
1196 SawParameterPack = true;
1197 ParameterPackLoc = NewTypeParm->getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001198 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001199 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001200 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1201 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1202 SawDefaultArgument = true;
1203 RedundantDefaultArg = true;
1204 PreviousDefaultArgLoc = NewDefaultLoc;
1205 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1206 // Merge the default argument from the old declaration to the
1207 // new declaration.
1208 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001209 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001210 true);
1211 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1212 } else if (NewTypeParm->hasDefaultArgument()) {
1213 SawDefaultArgument = true;
1214 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1215 } else if (SawDefaultArgument)
1216 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001217 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001218 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001219 // Check for unexpanded parameter packs.
1220 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001221 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001222 UPPC_NonTypeTemplateParameterType)) {
1223 Invalid = true;
1224 continue;
1225 }
1226
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001227 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001228 if (NewNonTypeParm->hasDefaultArgument() &&
1229 DiagnoseDefaultTemplateArgument(*this, TPC,
1230 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001231 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001232 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001233 }
1234
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001235 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001236 NonTypeTemplateParmDecl *OldNonTypeParm
1237 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001238 if (NewNonTypeParm->isParameterPack()) {
1239 assert(!NewNonTypeParm->hasDefaultArgument() &&
1240 "Parameter packs can't have a default argument!");
1241 SawParameterPack = true;
1242 ParameterPackLoc = NewNonTypeParm->getLocation();
1243 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001244 NewNonTypeParm->hasDefaultArgument()) {
1245 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1246 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1247 SawDefaultArgument = true;
1248 RedundantDefaultArg = true;
1249 PreviousDefaultArgLoc = NewDefaultLoc;
1250 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1251 // Merge the default argument from the old declaration to the
1252 // new declaration.
1253 SawDefaultArgument = true;
1254 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001255 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001256 // parameter.
1257 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001258 OldNonTypeParm->getDefaultArgument(),
1259 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001260 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1261 } else if (NewNonTypeParm->hasDefaultArgument()) {
1262 SawDefaultArgument = true;
1263 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1264 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001265 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001266 } else {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001267 // Check the presence of a default argument here.
Douglas Gregord684b002009-02-10 19:49:53 +00001268 TemplateTemplateParmDecl *NewTemplateParm
1269 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001270
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001271 // Check for unexpanded parameter packs, recursively.
1272 if (DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
1273 Invalid = true;
1274 continue;
1275 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001276
1277 if (NewTemplateParm->hasDefaultArgument() &&
1278 DiagnoseDefaultTemplateArgument(*this, TPC,
1279 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001280 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001281 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001282
1283 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001284 TemplateTemplateParmDecl *OldTemplateParm
1285 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001286 if (NewTemplateParm->isParameterPack()) {
1287 assert(!NewTemplateParm->hasDefaultArgument() &&
1288 "Parameter packs can't have a default argument!");
1289 SawParameterPack = true;
1290 ParameterPackLoc = NewTemplateParm->getLocation();
1291 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001292 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001293 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1294 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001295 SawDefaultArgument = true;
1296 RedundantDefaultArg = true;
1297 PreviousDefaultArgLoc = NewDefaultLoc;
1298 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1299 // Merge the default argument from the old declaration to the
1300 // new declaration.
1301 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001302 // FIXME: We need to create a new kind of "default argument" expression
1303 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001304 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001305 OldTemplateParm->getDefaultArgument(),
1306 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001307 PreviousDefaultArgLoc
1308 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001309 } else if (NewTemplateParm->hasDefaultArgument()) {
1310 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001311 PreviousDefaultArgLoc
1312 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001313 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001314 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001315 }
1316
1317 if (RedundantDefaultArg) {
1318 // C++ [temp.param]p12:
1319 // A template-parameter shall not be given default arguments
1320 // by two different declarations in the same scope.
1321 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1322 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1323 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001324 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001325 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001326 // If a template-parameter of a class template has a default
1327 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001328 // have a default template-argument supplied or be a template parameter
1329 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001330 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001331 diag::err_template_param_default_arg_missing);
1332 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1333 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001334 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001335 }
1336
1337 // If we have an old template parameter list that we're merging
1338 // in, move on to the next parameter.
1339 if (OldParams)
1340 ++OldParam;
1341 }
1342
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001343 // We were missing some default arguments at the end of the list, so remove
1344 // all of the default arguments.
1345 if (RemoveDefaultArguments) {
1346 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1347 NewParamEnd = NewParams->end();
1348 NewParam != NewParamEnd; ++NewParam) {
1349 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1350 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001351 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001352 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1353 NTTP->removeDefaultArgument();
1354 else
1355 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1356 }
1357 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001358
Douglas Gregord684b002009-02-10 19:49:53 +00001359 return Invalid;
1360}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001361
John McCall4e2cbb22010-10-20 05:44:58 +00001362namespace {
1363
1364/// A class which looks for a use of a certain level of template
1365/// parameter.
1366struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1367 typedef RecursiveASTVisitor<DependencyChecker> super;
1368
1369 unsigned Depth;
1370 bool Match;
1371
1372 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1373 NamedDecl *ND = Params->getParam(0);
1374 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1375 Depth = PD->getDepth();
1376 } else if (NonTypeTemplateParmDecl *PD =
1377 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1378 Depth = PD->getDepth();
1379 } else {
1380 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1381 }
1382 }
1383
1384 bool Matches(unsigned ParmDepth) {
1385 if (ParmDepth >= Depth) {
1386 Match = true;
1387 return true;
1388 }
1389 return false;
1390 }
1391
1392 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1393 return !Matches(T->getDepth());
1394 }
1395
1396 bool TraverseTemplateName(TemplateName N) {
1397 if (TemplateTemplateParmDecl *PD =
1398 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1399 if (Matches(PD->getDepth())) return false;
1400 return super::TraverseTemplateName(N);
1401 }
1402
1403 bool VisitDeclRefExpr(DeclRefExpr *E) {
1404 if (NonTypeTemplateParmDecl *PD =
1405 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1406 if (PD->getDepth() == Depth) {
1407 Match = true;
1408 return false;
1409 }
1410 }
1411 return super::VisitDeclRefExpr(E);
1412 }
1413};
1414}
1415
1416/// Determines whether a template-id depends on the given parameter
1417/// list.
1418static bool
1419DependsOnTemplateParameters(const TemplateSpecializationType *TemplateId,
1420 TemplateParameterList *Params) {
1421 DependencyChecker Checker(Params);
1422 Checker.TraverseType(QualType(TemplateId, 0));
1423 return Checker.Match;
1424}
1425
Mike Stump1eb44332009-09-09 15:08:12 +00001426/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001427/// specifier, returning the template parameter list that applies to the
1428/// name.
1429///
1430/// \param DeclStartLoc the start of the declaration that has a scope
1431/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001432///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001433/// \param SS the scope specifier that will be matched to the given template
1434/// parameter lists. This scope specifier precedes a qualified name that is
1435/// being declared.
1436///
1437/// \param ParamLists the template parameter lists, from the outermost to the
1438/// innermost template parameter lists.
1439///
1440/// \param NumParamLists the number of template parameter lists in ParamLists.
1441///
John McCall77e8b112010-04-13 20:37:33 +00001442/// \param IsFriend Whether to apply the slightly different rules for
1443/// matching template parameters to scope specifiers in friend
1444/// declarations.
1445///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001446/// \param IsExplicitSpecialization will be set true if the entity being
1447/// declared is an explicit specialization, false otherwise.
1448///
Mike Stump1eb44332009-09-09 15:08:12 +00001449/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001450/// name that is preceded by the scope specifier @p SS. This template
1451/// parameter list may be have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001452/// template) or may have no template parameters (if we're declaring a
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001453/// template specialization), or may be NULL (if we were's declaring isn't
1454/// itself a template).
1455TemplateParameterList *
1456Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
1457 const CXXScopeSpec &SS,
1458 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001459 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001460 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001461 bool &IsExplicitSpecialization,
1462 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001463 IsExplicitSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001464
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001465 // Find the template-ids that occur within the nested-name-specifier. These
1466 // template-ids will match up with the template parameter lists.
1467 llvm::SmallVector<const TemplateSpecializationType *, 4>
1468 TemplateIdsInSpecifier;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001469 llvm::SmallVector<ClassTemplateSpecializationDecl *, 4>
1470 ExplicitSpecializationsInSpecifier;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001471 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
1472 NNS; NNS = NNS->getPrefix()) {
John McCall4b2b02b2009-12-15 02:19:47 +00001473 const Type *T = NNS->getAsType();
1474 if (!T) break;
1475
1476 // C++0x [temp.expl.spec]p17:
1477 // A member or a member template may be nested within many
1478 // enclosing class templates. In an explicit specialization for
1479 // such a member, the member declaration shall be preceded by a
1480 // template<> for each enclosing class template that is
1481 // explicitly specialized.
Douglas Gregorfe331062010-02-13 05:23:25 +00001482 //
1483 // Following the existing practice of GNU and EDG, we allow a typedef of a
1484 // template specialization type.
John McCall49f4e1c2010-12-10 11:01:00 +00001485 while (const TypedefType *TT = dyn_cast<TypedefType>(T))
1486 T = TT->getDecl()->getUnderlyingType().getTypePtr();
John McCall4b2b02b2009-12-15 02:19:47 +00001487
Mike Stump1eb44332009-09-09 15:08:12 +00001488 if (const TemplateSpecializationType *SpecType
Douglas Gregorfe331062010-02-13 05:23:25 +00001489 = dyn_cast<TemplateSpecializationType>(T)) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001490 TemplateDecl *Template = SpecType->getTemplateName().getAsTemplateDecl();
1491 if (!Template)
1492 continue; // FIXME: should this be an error? probably...
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Ted Kremenek6217b802009-07-29 21:53:49 +00001494 if (const RecordType *Record = SpecType->getAs<RecordType>()) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001495 ClassTemplateSpecializationDecl *SpecDecl
1496 = cast<ClassTemplateSpecializationDecl>(Record->getDecl());
1497 // If the nested name specifier refers to an explicit specialization,
1498 // we don't need a template<> header.
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001499 if (SpecDecl->getSpecializationKind() == TSK_ExplicitSpecialization) {
1500 ExplicitSpecializationsInSpecifier.push_back(SpecDecl);
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001501 continue;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001502 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001503 }
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001505 TemplateIdsInSpecifier.push_back(SpecType);
1506 }
1507 }
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001509 // Reverse the list of template-ids in the scope specifier, so that we can
1510 // more easily match up the template-ids and the template parameter lists.
1511 std::reverse(TemplateIdsInSpecifier.begin(), TemplateIdsInSpecifier.end());
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001513 SourceLocation FirstTemplateLoc = DeclStartLoc;
1514 if (NumParamLists)
1515 FirstTemplateLoc = ParamLists[0]->getTemplateLoc();
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001517 // Match the template-ids found in the specifier to the template parameter
1518 // lists.
John McCall4e2cbb22010-10-20 05:44:58 +00001519 unsigned ParamIdx = 0, TemplateIdx = 0;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001520 for (unsigned NumTemplateIds = TemplateIdsInSpecifier.size();
John McCall4e2cbb22010-10-20 05:44:58 +00001521 TemplateIdx != NumTemplateIds; ++TemplateIdx) {
1522 const TemplateSpecializationType *TemplateId
1523 = TemplateIdsInSpecifier[TemplateIdx];
Douglas Gregorb88e8882009-07-30 17:40:51 +00001524 bool DependentTemplateId = TemplateId->isDependentType();
John McCall4e2cbb22010-10-20 05:44:58 +00001525
1526 // In friend declarations we can have template-ids which don't
1527 // depend on the corresponding template parameter lists. But
1528 // assume that empty parameter lists are supposed to match this
1529 // template-id.
1530 if (IsFriend && ParamIdx < NumParamLists && ParamLists[ParamIdx]->size()) {
1531 if (!DependentTemplateId ||
1532 !DependsOnTemplateParameters(TemplateId, ParamLists[ParamIdx]))
1533 continue;
1534 }
1535
1536 if (ParamIdx >= NumParamLists) {
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001537 // We have a template-id without a corresponding template parameter
1538 // list.
John McCall77e8b112010-04-13 20:37:33 +00001539
1540 // ...which is fine if this is a friend declaration.
1541 if (IsFriend) {
1542 IsExplicitSpecialization = true;
1543 break;
1544 }
1545
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001546 if (DependentTemplateId) {
Mike Stump1eb44332009-09-09 15:08:12 +00001547 // FIXME: the location information here isn't great.
1548 Diag(SS.getRange().getBegin(),
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001549 diag::err_template_spec_needs_template_parameters)
John McCall4e2cbb22010-10-20 05:44:58 +00001550 << QualType(TemplateId, 0)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001551 << SS.getRange();
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001552 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001553 } else {
1554 Diag(SS.getRange().getBegin(), diag::err_template_spec_needs_header)
1555 << SS.getRange()
Douglas Gregor849b2432010-03-31 17:46:05 +00001556 << FixItHint::CreateInsertion(FirstTemplateLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001557 IsExplicitSpecialization = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001558 }
1559 return 0;
1560 }
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001562 // Check the template parameter list against its corresponding template-id.
Douglas Gregorb88e8882009-07-30 17:40:51 +00001563 if (DependentTemplateId) {
John McCall31f17ec2010-04-27 00:57:59 +00001564 TemplateParameterList *ExpectedTemplateParams = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00001565
John McCall31f17ec2010-04-27 00:57:59 +00001566 // Are there cases in (e.g.) friends where this won't match?
1567 if (const InjectedClassNameType *Injected
1568 = TemplateId->getAs<InjectedClassNameType>()) {
1569 CXXRecordDecl *Record = Injected->getDecl();
1570 if (ClassTemplatePartialSpecializationDecl *Partial =
1571 dyn_cast<ClassTemplatePartialSpecializationDecl>(Record))
1572 ExpectedTemplateParams = Partial->getTemplateParameters();
1573 else
1574 ExpectedTemplateParams = Record->getDescribedClassTemplate()
1575 ->getTemplateParameters();
Mike Stump1eb44332009-09-09 15:08:12 +00001576 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001577
John McCall31f17ec2010-04-27 00:57:59 +00001578 if (ExpectedTemplateParams)
John McCall4e2cbb22010-10-20 05:44:58 +00001579 TemplateParameterListsAreEqual(ParamLists[ParamIdx],
John McCall31f17ec2010-04-27 00:57:59 +00001580 ExpectedTemplateParams,
1581 true, TPL_TemplateMatch);
1582
John McCall4e2cbb22010-10-20 05:44:58 +00001583 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1584 TPC_ClassTemplateMember);
1585 } else if (ParamLists[ParamIdx]->size() > 0)
1586 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregorb88e8882009-07-30 17:40:51 +00001587 diag::err_template_param_list_matches_nontemplate)
1588 << TemplateId
John McCall4e2cbb22010-10-20 05:44:58 +00001589 << ParamLists[ParamIdx]->getSourceRange();
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001590 else
1591 IsExplicitSpecialization = true;
John McCall4e2cbb22010-10-20 05:44:58 +00001592
1593 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001594 }
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001596 // If there were at least as many template-ids as there were template
1597 // parameter lists, then there are no template parameter lists remaining for
1598 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001599 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001600 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001601
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001602 // If there were too many template parameter lists, complain about that now.
John McCall4e2cbb22010-10-20 05:44:58 +00001603 if (ParamIdx != NumParamLists - 1) {
1604 while (ParamIdx < NumParamLists - 1) {
1605 bool isExplicitSpecHeader = ParamLists[ParamIdx]->size() == 0;
1606 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001607 isExplicitSpecHeader? diag::warn_template_spec_extra_headers
1608 : diag::err_template_spec_extra_headers)
John McCall4e2cbb22010-10-20 05:44:58 +00001609 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1610 ParamLists[ParamIdx]->getRAngleLoc());
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001611
1612 if (isExplicitSpecHeader && !ExplicitSpecializationsInSpecifier.empty()) {
1613 Diag(ExplicitSpecializationsInSpecifier.back()->getLocation(),
1614 diag::note_explicit_template_spec_does_not_need_header)
1615 << ExplicitSpecializationsInSpecifier.back();
1616 ExplicitSpecializationsInSpecifier.pop_back();
1617 }
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001618
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001619 // We have a template parameter list with no corresponding scope, which
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001620 // means that the resulting template declaration can't be instantiated
1621 // properly (we'll end up with dependent nodes when we shouldn't).
1622 if (!isExplicitSpecHeader)
1623 Invalid = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001624
John McCall4e2cbb22010-10-20 05:44:58 +00001625 ++ParamIdx;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001626 }
1627 }
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001629 // Return the last template parameter list, which corresponds to the
1630 // entity being declared.
1631 return ParamLists[NumParamLists - 1];
1632}
1633
Douglas Gregor7532dc62009-03-30 22:58:21 +00001634QualType Sema::CheckTemplateIdType(TemplateName Name,
1635 SourceLocation TemplateLoc,
John McCalld5532b62009-11-23 01:53:49 +00001636 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001637 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001638 if (!Template) {
1639 // The template name does not resolve to a template, so we just
1640 // build a dependent template-id type.
John McCalld5532b62009-11-23 01:53:49 +00001641 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Douglas Gregorc45c2322009-03-31 00:43:58 +00001642 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001643
Douglas Gregor40808ce2009-03-09 23:48:35 +00001644 // Check that the template argument list is well-formed for this
1645 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00001646 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001647 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001648 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001649 return QualType();
1650
Douglas Gregor910f8002010-11-07 23:05:16 +00001651 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001652 "Converted template argument list is too short!");
1653
1654 QualType CanonType;
1655
Douglas Gregorcaddba02009-11-12 18:38:13 +00001656 if (Name.isDependent() ||
1657 TemplateSpecializationType::anyDependentTemplateArguments(
John McCalld5532b62009-11-23 01:53:49 +00001658 TemplateArgs)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001659 // This class template specialization is a dependent
1660 // type. Therefore, its canonical type is another class template
1661 // specialization type that contains all of the converted
1662 // arguments in canonical form. This ensures that, e.g., A<T> and
1663 // A<T, T> have identical types when A is declared as:
1664 //
1665 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001666 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001667 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001668 Converted.data(),
1669 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001670
Douglas Gregor1275ae02009-07-28 23:00:59 +00001671 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001672 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001673 // In the future, we need to teach getTemplateSpecializationType to only
1674 // build the canonical type and return that to us.
1675 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001676
1677 // This might work out to be a current instantiation, in which
1678 // case the canonical type needs to be the InjectedClassNameType.
1679 //
1680 // TODO: in theory this could be a simple hashtable lookup; most
1681 // changes to CurContext don't change the set of current
1682 // instantiations.
1683 if (isa<ClassTemplateDecl>(Template)) {
1684 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1685 // If we get out to a namespace, we're done.
1686 if (Ctx->isFileContext()) break;
1687
1688 // If this isn't a record, keep looking.
1689 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1690 if (!Record) continue;
1691
1692 // Look for one of the two cases with InjectedClassNameTypes
1693 // and check whether it's the same template.
1694 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1695 !Record->getDescribedClassTemplate())
1696 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001697
John McCall31f17ec2010-04-27 00:57:59 +00001698 // Fetch the injected class name type and check whether its
1699 // injected type is equal to the type we just built.
1700 QualType ICNT = Context.getTypeDeclType(Record);
1701 QualType Injected = cast<InjectedClassNameType>(ICNT)
1702 ->getInjectedSpecializationType();
1703
1704 if (CanonType != Injected->getCanonicalTypeInternal())
1705 continue;
1706
1707 // If so, the canonical type of this TST is the injected
1708 // class name type of the record we just found.
1709 assert(ICNT.isCanonical());
1710 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00001711 break;
1712 }
1713 }
Mike Stump1eb44332009-09-09 15:08:12 +00001714 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00001715 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001716 // Find the class template specialization declaration that
1717 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00001718 void *InsertPos = 0;
1719 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001720 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001721 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001722 if (!Decl) {
1723 // This is the first time we have referenced this class template
1724 // specialization. Create the canonical declaration and add it to
1725 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00001726 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00001727 ClassTemplate->getTemplatedDecl()->getTagKind(),
1728 ClassTemplate->getDeclContext(),
1729 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001730 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001731 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00001732 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00001733 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001734 Decl->setLexicalDeclContext(CurContext);
1735 }
1736
1737 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00001738 assert(isa<RecordType>(CanonType) &&
1739 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00001740 }
Mike Stump1eb44332009-09-09 15:08:12 +00001741
Douglas Gregor40808ce2009-03-09 23:48:35 +00001742 // Build the fully-sugared type for this class template
1743 // specialization, which refers back to the class template
1744 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00001745 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001746}
1747
John McCallf312b1e2010-08-26 23:41:50 +00001748TypeResult
Douglas Gregor7532dc62009-03-30 22:58:21 +00001749Sema::ActOnTemplateIdType(TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001750 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00001751 ASTTemplateArgsPtr TemplateArgsIn,
John McCall6b2becf2009-09-08 17:47:29 +00001752 SourceLocation RAngleLoc) {
Douglas Gregor7532dc62009-03-30 22:58:21 +00001753 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00001754
Douglas Gregor40808ce2009-03-09 23:48:35 +00001755 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00001756 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00001757 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00001758
Douglas Gregora88f09f2011-02-28 17:23:35 +00001759 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
1760 QualType T = Context.getDependentTemplateSpecializationType(ETK_None,
1761 DTN->getQualifier(),
1762 DTN->getIdentifier(),
1763 TemplateArgs);
1764
1765 // Build type-source information.
1766 TypeLocBuilder TLB;
1767 DependentTemplateSpecializationTypeLoc SpecTL
1768 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
1769 SpecTL.setKeywordLoc(SourceLocation()); // FIXME: 'template' location
1770 SpecTL.setNameLoc(TemplateLoc);
1771 SpecTL.setLAngleLoc(LAngleLoc);
1772 SpecTL.setRAngleLoc(RAngleLoc);
1773 SpecTL.setQualifierRange(TemplateLoc); // FIXME: nested-name-specifier loc
1774 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
1775 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
1776 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
1777 }
1778
John McCalld5532b62009-11-23 01:53:49 +00001779 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001780 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00001781
1782 if (Result.isNull())
1783 return true;
1784
John McCalla93c9342009-12-07 02:54:59 +00001785 TypeSourceInfo *DI = Context.CreateTypeSourceInfo(Result);
John McCall833ca992009-10-29 08:12:44 +00001786 TemplateSpecializationTypeLoc TL
1787 = cast<TemplateSpecializationTypeLoc>(DI->getTypeLoc());
1788 TL.setTemplateNameLoc(TemplateLoc);
1789 TL.setLAngleLoc(LAngleLoc);
1790 TL.setRAngleLoc(RAngleLoc);
1791 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
1792 TL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
1793
John McCallb3d87482010-08-24 05:47:05 +00001794 return CreateParsedType(Result, DI);
John McCall6b2becf2009-09-08 17:47:29 +00001795}
John McCallf1bbbb42009-09-04 01:14:41 +00001796
Craig Silverstein45ab4b52010-11-18 08:32:02 +00001797TypeResult Sema::ActOnTagTemplateIdType(CXXScopeSpec &SS,
1798 TypeResult TypeResult,
John McCallf312b1e2010-08-26 23:41:50 +00001799 TagUseKind TUK,
1800 TypeSpecifierType TagSpec,
1801 SourceLocation TagLoc) {
John McCall6b2becf2009-09-08 17:47:29 +00001802 if (TypeResult.isInvalid())
John McCallf312b1e2010-08-26 23:41:50 +00001803 return ::TypeResult();
John McCallf1bbbb42009-09-04 01:14:41 +00001804
John McCalla93c9342009-12-07 02:54:59 +00001805 TypeSourceInfo *DI;
John McCall833ca992009-10-29 08:12:44 +00001806 QualType Type = GetTypeFromParser(TypeResult.get(), &DI);
John McCallf1bbbb42009-09-04 01:14:41 +00001807
John McCall6b2becf2009-09-08 17:47:29 +00001808 // Verify the tag specifier.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001809 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Mike Stump1eb44332009-09-09 15:08:12 +00001810
John McCall6b2becf2009-09-08 17:47:29 +00001811 if (const RecordType *RT = Type->getAs<RecordType>()) {
1812 RecordDecl *D = RT->getDecl();
1813
1814 IdentifierInfo *Id = D->getIdentifier();
1815 assert(Id && "templated class must have an identifier");
1816
1817 if (!isAcceptableTagRedeclaration(D, TagKind, TagLoc, *Id)) {
1818 Diag(TagLoc, diag::err_use_with_wrong_tag)
John McCallc4e70192009-09-11 04:59:25 +00001819 << Type
Douglas Gregor849b2432010-03-31 17:46:05 +00001820 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00001821 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00001822 }
1823 }
1824
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001825 ElaboratedTypeKeyword Keyword
1826 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
1827 QualType ElabType = Context.getElaboratedType(Keyword, /*NNS=*/0, Type);
John McCall6b2becf2009-09-08 17:47:29 +00001828
Craig Silverstein45ab4b52010-11-18 08:32:02 +00001829 TypeSourceInfo *ElabDI = Context.CreateTypeSourceInfo(ElabType);
1830 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(ElabDI->getTypeLoc());
1831 TL.setKeywordLoc(TagLoc);
1832 TL.setQualifierRange(SS.getRange());
1833 TL.getNamedTypeLoc().initializeFullCopy(DI->getTypeLoc());
1834 return CreateParsedType(ElabType, ElabDI);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001835}
1836
John McCall60d7b3a2010-08-24 06:29:42 +00001837ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +00001838 LookupResult &R,
1839 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00001840 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001841 // FIXME: Can we do any checking at this point? I guess we could check the
1842 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00001843 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001844 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00001845 // foo<int> could identify a single function unambiguously
1846 // This approach does NOT work, since f<int>(1);
1847 // gets resolved prior to resorting to overload resolution
1848 // i.e., template<class T> void f(double);
1849 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00001850
1851 // These should be filtered out by our callers.
1852 assert(!R.empty() && "empty lookup results when building templateid");
1853 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
1854
1855 NestedNameSpecifier *Qualifier = 0;
1856 SourceRange QualifierRange;
1857 if (SS.isSet()) {
1858 Qualifier = static_cast<NestedNameSpecifier*>(SS.getScopeRep());
1859 QualifierRange = SS.getRange();
Douglas Gregora9e29aa2009-10-22 07:19:14 +00001860 }
John McCallc373d482010-01-27 01:50:18 +00001861
1862 // We don't want lookup warnings at this point.
1863 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001864
John McCallf7a1a742009-11-24 19:00:30 +00001865 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001866 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
John McCallf7a1a742009-11-24 19:00:30 +00001867 Qualifier, QualifierRange,
Abramo Bagnara25777432010-08-11 22:01:17 +00001868 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001869 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00001870 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00001871
1872 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001873}
1874
John McCallf7a1a742009-11-24 19:00:30 +00001875// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00001876ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00001877Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnara25777432010-08-11 22:01:17 +00001878 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00001879 const TemplateArgumentListInfo &TemplateArgs) {
1880 DeclContext *DC;
1881 if (!(DC = computeDeclContext(SS, false)) ||
1882 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00001883 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara25777432010-08-11 22:01:17 +00001884 return BuildDependentDeclRefExpr(SS, NameInfo, &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00001885
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001886 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00001887 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001888 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
1889 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00001890
John McCallf7a1a742009-11-24 19:00:30 +00001891 if (R.isAmbiguous())
1892 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001893
John McCallf7a1a742009-11-24 19:00:30 +00001894 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001895 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
1896 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001897 return ExprError();
1898 }
1899
1900 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001901 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
1902 << (NestedNameSpecifier*) SS.getScopeRep()
1903 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00001904 Diag(Temp->getLocation(), diag::note_referenced_class_template);
1905 return ExprError();
1906 }
1907
1908 return BuildTemplateIdExpr(SS, R, /* ADL */ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00001909}
1910
Douglas Gregorc45c2322009-03-31 00:43:58 +00001911/// \brief Form a dependent template name.
1912///
1913/// This action forms a dependent template name given the template
1914/// name and its (presumably dependent) scope specifier. For
1915/// example, given "MetaFun::template apply", the scope specifier \p
1916/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
1917/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001918TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001919 SourceLocation TemplateKWLoc,
1920 CXXScopeSpec &SS,
1921 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00001922 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001923 bool EnteringContext,
1924 TemplateTy &Result) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00001925 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent() &&
1926 !getLangOptions().CPlusPlus0x)
1927 Diag(TemplateKWLoc, diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001928 << FixItHint::CreateRemoval(TemplateKWLoc);
1929
Douglas Gregor0707bc52010-01-19 16:01:07 +00001930 DeclContext *LookupCtx = 0;
1931 if (SS.isSet())
1932 LookupCtx = computeDeclContext(SS, EnteringContext);
1933 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00001934 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00001935 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00001936 // C++0x [temp.names]p5:
1937 // If a name prefixed by the keyword template is not the name of
1938 // a template, the program is ill-formed. [Note: the keyword
1939 // template may not be applied to non-template members of class
1940 // templates. -end note ] [ Note: as is the case with the
1941 // typename prefix, the template prefix is allowed in cases
1942 // where it is not strictly necessary; i.e., when the
1943 // nested-name-specifier or the expression on the left of the ->
1944 // or . is not dependent on a template-parameter, or the use
1945 // does not appear in the scope of a template. -end note]
1946 //
1947 // Note: C++03 was more strict here, because it banned the use of
1948 // the "template" keyword prior to a template-name that was not a
1949 // dependent name. C++ DR468 relaxed this requirement (the
1950 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00001951 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001952 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00001953 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
1954 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00001955 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00001956 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
1957 isa<CXXRecordDecl>(LookupCtx) &&
1958 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases()) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00001959 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00001960 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001961 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00001962 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00001963 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00001964 << Name.getSourceRange()
1965 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00001966 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00001967 } else {
1968 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00001969 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00001970 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00001971 }
1972
Mike Stump1eb44332009-09-09 15:08:12 +00001973 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001974 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001975
Douglas Gregor014e88d2009-11-03 23:16:33 +00001976 switch (Name.getKind()) {
1977 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001978 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00001979 Name.Identifier));
1980 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001981
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001982 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00001983 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00001984 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00001985 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00001986
1987 case UnqualifiedId::IK_LiteralOperatorId:
1988 assert(false && "We don't support these; Parse shouldn't have allowed propagation");
1989
Douglas Gregor014e88d2009-11-03 23:16:33 +00001990 default:
1991 break;
1992 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001993
1994 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00001995 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00001996 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00001997 << Name.getSourceRange()
1998 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00001999 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002000}
2001
Mike Stump1eb44332009-09-09 15:08:12 +00002002bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002003 const TemplateArgumentLoc &AL,
Douglas Gregor910f8002010-11-07 23:05:16 +00002004 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002005 const TemplateArgument &Arg = AL.getArgument();
2006
Anders Carlsson436b1562009-06-13 00:33:33 +00002007 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002008 switch(Arg.getKind()) {
2009 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002010 // C++ [temp.arg.type]p1:
2011 // A template-argument for a template-parameter which is a
2012 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002013 break;
2014 case TemplateArgument::Template: {
2015 // We have a template type parameter but the template argument
2016 // is a template without any arguments.
2017 SourceRange SR = AL.getSourceRange();
2018 TemplateName Name = Arg.getAsTemplate();
2019 Diag(SR.getBegin(), diag::err_template_missing_args)
2020 << Name << SR;
2021 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2022 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002023
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002024 return true;
2025 }
2026 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002027 // We have a template type parameter but the template argument
2028 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002029 SourceRange SR = AL.getSourceRange();
2030 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002031 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002032
Anders Carlsson436b1562009-06-13 00:33:33 +00002033 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002034 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002035 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002036
John McCalla93c9342009-12-07 02:54:59 +00002037 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002038 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002039
Anders Carlsson436b1562009-06-13 00:33:33 +00002040 // Add the converted template type argument.
Douglas Gregor910f8002010-11-07 23:05:16 +00002041 Converted.push_back(
John McCall833ca992009-10-29 08:12:44 +00002042 TemplateArgument(Context.getCanonicalType(Arg.getAsType())));
Anders Carlsson436b1562009-06-13 00:33:33 +00002043 return false;
2044}
2045
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002046/// \brief Substitute template arguments into the default template argument for
2047/// the given template type parameter.
2048///
2049/// \param SemaRef the semantic analysis object for which we are performing
2050/// the substitution.
2051///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002052/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002053/// for.
2054///
2055/// \param TemplateLoc the location of the template name that started the
2056/// template-id we are checking.
2057///
2058/// \param RAngleLoc the location of the right angle bracket ('>') that
2059/// terminates the template-id.
2060///
2061/// \param Param the template template parameter whose default we are
2062/// substituting into.
2063///
2064/// \param Converted the list of template arguments provided for template
2065/// parameters that precede \p Param in the template parameter list.
2066///
2067/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002068static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002069SubstDefaultTemplateArgument(Sema &SemaRef,
2070 TemplateDecl *Template,
2071 SourceLocation TemplateLoc,
2072 SourceLocation RAngleLoc,
2073 TemplateTypeParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002074 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002075 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002076
2077 // If the argument type is dependent, instantiate it now based
2078 // on the previously-computed template arguments.
2079 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002080 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002081 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002082
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002083 MultiLevelTemplateArgumentList AllTemplateArgs
2084 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2085
2086 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002087 Template, Converted.data(),
2088 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002089 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002090
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002091 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2092 Param->getDefaultArgumentLoc(),
2093 Param->getDeclName());
2094 }
2095
2096 return ArgType;
2097}
2098
2099/// \brief Substitute template arguments into the default template argument for
2100/// the given non-type template parameter.
2101///
2102/// \param SemaRef the semantic analysis object for which we are performing
2103/// the substitution.
2104///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002105/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002106/// for.
2107///
2108/// \param TemplateLoc the location of the template name that started the
2109/// template-id we are checking.
2110///
2111/// \param RAngleLoc the location of the right angle bracket ('>') that
2112/// terminates the template-id.
2113///
Douglas Gregor788cd062009-11-11 01:00:40 +00002114/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002115/// substituting into.
2116///
2117/// \param Converted the list of template arguments provided for template
2118/// parameters that precede \p Param in the template parameter list.
2119///
2120/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002121static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002122SubstDefaultTemplateArgument(Sema &SemaRef,
2123 TemplateDecl *Template,
2124 SourceLocation TemplateLoc,
2125 SourceLocation RAngleLoc,
2126 NonTypeTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002127 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002128 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002129 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002130
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002131 MultiLevelTemplateArgumentList AllTemplateArgs
2132 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002133
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002134 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002135 Template, Converted.data(),
2136 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002137 SourceRange(TemplateLoc, RAngleLoc));
2138
2139 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2140}
2141
Douglas Gregor788cd062009-11-11 01:00:40 +00002142/// \brief Substitute template arguments into the default template argument for
2143/// the given template template parameter.
2144///
2145/// \param SemaRef the semantic analysis object for which we are performing
2146/// the substitution.
2147///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002148/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002149/// for.
2150///
2151/// \param TemplateLoc the location of the template name that started the
2152/// template-id we are checking.
2153///
2154/// \param RAngleLoc the location of the right angle bracket ('>') that
2155/// terminates the template-id.
2156///
2157/// \param Param the template template parameter whose default we are
2158/// substituting into.
2159///
2160/// \param Converted the list of template arguments provided for template
2161/// parameters that precede \p Param in the template parameter list.
2162///
2163/// \returns the substituted template argument, or NULL if an error occurred.
2164static TemplateName
2165SubstDefaultTemplateArgument(Sema &SemaRef,
2166 TemplateDecl *Template,
2167 SourceLocation TemplateLoc,
2168 SourceLocation RAngleLoc,
2169 TemplateTemplateParmDecl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002170 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002171 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002172 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002173
Douglas Gregor788cd062009-11-11 01:00:40 +00002174 MultiLevelTemplateArgumentList AllTemplateArgs
2175 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002176
Douglas Gregor788cd062009-11-11 01:00:40 +00002177 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002178 Template, Converted.data(),
2179 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002180 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002181
Douglas Gregor788cd062009-11-11 01:00:40 +00002182 return SemaRef.SubstTemplateName(
2183 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002184 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002185 AllTemplateArgs);
2186}
2187
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002188/// \brief If the given template parameter has a default template
2189/// argument, substitute into that default template argument and
2190/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002191TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002192Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2193 SourceLocation TemplateLoc,
2194 SourceLocation RAngleLoc,
2195 Decl *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002196 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
2197 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002198 if (!TypeParm->hasDefaultArgument())
2199 return TemplateArgumentLoc();
2200
John McCalla93c9342009-12-07 02:54:59 +00002201 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002202 TemplateLoc,
2203 RAngleLoc,
2204 TypeParm,
2205 Converted);
2206 if (DI)
2207 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2208
2209 return TemplateArgumentLoc();
2210 }
2211
2212 if (NonTypeTemplateParmDecl *NonTypeParm
2213 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2214 if (!NonTypeParm->hasDefaultArgument())
2215 return TemplateArgumentLoc();
2216
John McCall60d7b3a2010-08-24 06:29:42 +00002217 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002218 TemplateLoc,
2219 RAngleLoc,
2220 NonTypeParm,
2221 Converted);
2222 if (Arg.isInvalid())
2223 return TemplateArgumentLoc();
2224
2225 Expr *ArgE = Arg.takeAs<Expr>();
2226 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2227 }
2228
2229 TemplateTemplateParmDecl *TempTempParm
2230 = cast<TemplateTemplateParmDecl>(Param);
2231 if (!TempTempParm->hasDefaultArgument())
2232 return TemplateArgumentLoc();
2233
2234 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002235 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002236 RAngleLoc,
2237 TempTempParm,
2238 Converted);
2239 if (TName.isNull())
2240 return TemplateArgumentLoc();
2241
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002242 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002243 TempTempParm->getDefaultArgument().getTemplateQualifierRange(),
2244 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2245}
2246
Douglas Gregore7526412009-11-11 19:31:23 +00002247/// \brief Check that the given template argument corresponds to the given
2248/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002249///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002250/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002251/// checked.
2252///
2253/// \param Arg The template argument.
2254///
2255/// \param Template The template in which the template argument resides.
2256///
2257/// \param TemplateLoc The location of the template name for the template
2258/// whose argument list we're matching.
2259///
2260/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2261/// the template argument list.
2262///
2263/// \param ArgumentPackIndex The index into the argument pack where this
2264/// argument will be placed. Only valid if the parameter is a parameter pack.
2265///
2266/// \param Converted The checked, converted argument will be added to the
2267/// end of this small vector.
2268///
2269/// \param CTAK Describes how we arrived at this particular template argument:
2270/// explicitly written, deduced, etc.
2271///
2272/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002273bool Sema::CheckTemplateArgument(NamedDecl *Param,
2274 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002275 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002276 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002277 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002278 unsigned ArgumentPackIndex,
Douglas Gregor910f8002010-11-07 23:05:16 +00002279 llvm::SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002280 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002281 // Check template type parameters.
2282 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002283 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002284
Douglas Gregord9e15302009-11-11 19:41:09 +00002285 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002286 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002287 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002288 // with the template arguments we've seen thus far. But if the
2289 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002290 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002291 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2292 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002293
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002294 if (NTTPType->isDependentType() &&
2295 !isa<TemplateTemplateParmDecl>(Template) &&
2296 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002297 // Do substitution on the type of the non-type template parameter.
2298 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002299 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002300 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002301
2302 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002303 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002304 NTTPType = SubstType(NTTPType,
2305 MultiLevelTemplateArgumentList(TemplateArgs),
2306 NTTP->getLocation(),
2307 NTTP->getDeclName());
2308 // If that worked, check the non-type template parameter type
2309 // for validity.
2310 if (!NTTPType.isNull())
2311 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2312 NTTP->getLocation());
2313 if (NTTPType.isNull())
2314 return true;
2315 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002316
Douglas Gregore7526412009-11-11 19:31:23 +00002317 switch (Arg.getArgument().getKind()) {
2318 case TemplateArgument::Null:
2319 assert(false && "Should never see a NULL template argument here");
2320 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002321
Douglas Gregore7526412009-11-11 19:31:23 +00002322 case TemplateArgument::Expression: {
2323 Expr *E = Arg.getArgument().getAsExpr();
2324 TemplateArgument Result;
Douglas Gregor02024a92010-03-28 02:42:43 +00002325 if (CheckTemplateArgument(NTTP, NTTPType, E, Result, CTAK))
Douglas Gregore7526412009-11-11 19:31:23 +00002326 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002327
Douglas Gregor910f8002010-11-07 23:05:16 +00002328 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002329 break;
2330 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002331
Douglas Gregore7526412009-11-11 19:31:23 +00002332 case TemplateArgument::Declaration:
2333 case TemplateArgument::Integral:
2334 // We've already checked this template argument, so just copy
2335 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002336 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002337 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002338
Douglas Gregore7526412009-11-11 19:31:23 +00002339 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002340 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002341 // We were given a template template argument. It may not be ill-formed;
2342 // see below.
2343 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002344 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2345 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002346 // We have a template argument such as \c T::template X, which we
2347 // parsed as a template template argument. However, since we now
2348 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002349 // template name into an expression.
2350
2351 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2352 Arg.getTemplateNameLoc());
2353
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002354 // FIXME: TemplateArgumentLoc should store a NestedNameSpecifierLoc
2355 // for the template name.
2356 CXXScopeSpec SS;
2357 SS.MakeTrivial(Context, DTN->getQualifier(),
2358 Arg.getTemplateQualifierRange());
John McCallf7a1a742009-11-24 19:00:30 +00002359 Expr *E = DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002360 SS.getWithLocInContext(Context),
Abramo Bagnara25777432010-08-11 22:01:17 +00002361 NameInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002362
Douglas Gregora7fc9012011-01-05 18:58:31 +00002363 // If we parsed the template argument as a pack expansion, create a
2364 // pack expansion expression.
2365 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002366 ExprResult Expansion = ActOnPackExpansion(E,
Douglas Gregora7fc9012011-01-05 18:58:31 +00002367 Arg.getTemplateEllipsisLoc());
2368 if (Expansion.isInvalid())
2369 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002370
Douglas Gregora7fc9012011-01-05 18:58:31 +00002371 E = Expansion.get();
2372 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002373
Douglas Gregore7526412009-11-11 19:31:23 +00002374 TemplateArgument Result;
2375 if (CheckTemplateArgument(NTTP, NTTPType, E, Result))
2376 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002377
Douglas Gregor910f8002010-11-07 23:05:16 +00002378 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002379 break;
2380 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002381
Douglas Gregore7526412009-11-11 19:31:23 +00002382 // We have a template argument that actually does refer to a class
2383 // template, template alias, or template template parameter, and
2384 // therefore cannot be a non-type template argument.
2385 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2386 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002387
Douglas Gregore7526412009-11-11 19:31:23 +00002388 Diag(Param->getLocation(), diag::note_template_param_here);
2389 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002390
Douglas Gregore7526412009-11-11 19:31:23 +00002391 case TemplateArgument::Type: {
2392 // We have a non-type template parameter but the template
2393 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002394
Douglas Gregore7526412009-11-11 19:31:23 +00002395 // C++ [temp.arg]p2:
2396 // In a template-argument, an ambiguity between a type-id and
2397 // an expression is resolved to a type-id, regardless of the
2398 // form of the corresponding template-parameter.
2399 //
2400 // We warn specifically about this case, since it can be rather
2401 // confusing for users.
2402 QualType T = Arg.getArgument().getAsType();
2403 SourceRange SR = Arg.getSourceRange();
2404 if (T->isFunctionType())
2405 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2406 else
2407 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2408 Diag(Param->getLocation(), diag::note_template_param_here);
2409 return true;
2410 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002411
Douglas Gregore7526412009-11-11 19:31:23 +00002412 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002413 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002414 break;
2415 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002416
Douglas Gregore7526412009-11-11 19:31:23 +00002417 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002418 }
2419
2420
Douglas Gregore7526412009-11-11 19:31:23 +00002421 // Check template template parameters.
2422 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002423
Douglas Gregore7526412009-11-11 19:31:23 +00002424 // Substitute into the template parameter list of the template
2425 // template parameter, since previously-supplied template arguments
2426 // may appear within the template template parameter.
2427 {
2428 // Set up a template instantiation context.
2429 LocalInstantiationScope Scope(*this);
2430 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002431 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002432 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002433
2434 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002435 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002436 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002437 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002438 MultiLevelTemplateArgumentList(TemplateArgs)));
2439 if (!TempParm)
2440 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002441 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002442
Douglas Gregore7526412009-11-11 19:31:23 +00002443 switch (Arg.getArgument().getKind()) {
2444 case TemplateArgument::Null:
2445 assert(false && "Should never see a NULL template argument here");
2446 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002447
Douglas Gregore7526412009-11-11 19:31:23 +00002448 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002449 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002450 if (CheckTemplateArgument(TempParm, Arg))
2451 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002452
Douglas Gregor910f8002010-11-07 23:05:16 +00002453 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002454 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002455
Douglas Gregore7526412009-11-11 19:31:23 +00002456 case TemplateArgument::Expression:
2457 case TemplateArgument::Type:
2458 // We have a template template parameter but the template
2459 // argument does not refer to a template.
2460 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template);
2461 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002462
Douglas Gregore7526412009-11-11 19:31:23 +00002463 case TemplateArgument::Declaration:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002464 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002465 "Declaration argument with template template parameter");
2466 break;
2467 case TemplateArgument::Integral:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002468 llvm_unreachable(
Douglas Gregore7526412009-11-11 19:31:23 +00002469 "Integral argument with template template parameter");
2470 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002471
Douglas Gregore7526412009-11-11 19:31:23 +00002472 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002473 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002474 break;
2475 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002476
Douglas Gregore7526412009-11-11 19:31:23 +00002477 return false;
2478}
2479
Douglas Gregorc15cb382009-02-09 23:23:08 +00002480/// \brief Check that the given template argument list is well-formed
2481/// for specializing the given template.
2482bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2483 SourceLocation TemplateLoc,
John McCalld5532b62009-11-23 01:53:49 +00002484 const TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002485 bool PartialTemplateArgs,
Douglas Gregor910f8002010-11-07 23:05:16 +00002486 llvm::SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002487 TemplateParameterList *Params = Template->getTemplateParameters();
2488 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002489 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002490 bool Invalid = false;
2491
John McCalld5532b62009-11-23 01:53:49 +00002492 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2493
Mike Stump1eb44332009-09-09 15:08:12 +00002494 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002495 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002496
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002497 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002498 (NumArgs < Params->getMinRequiredArguments() &&
2499 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002500 // FIXME: point at either the first arg beyond what we can handle,
2501 // or the '>', depending on whether we have too many or too few
2502 // arguments.
2503 SourceRange Range;
2504 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002505 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002506 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2507 << (NumArgs > NumParams)
2508 << (isa<ClassTemplateDecl>(Template)? 0 :
2509 isa<FunctionTemplateDecl>(Template)? 1 :
2510 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2511 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002512 Diag(Template->getLocation(), diag::note_template_decl_here)
2513 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002514 Invalid = true;
2515 }
Mike Stump1eb44332009-09-09 15:08:12 +00002516
2517 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002518 // [...] The type and form of each template-argument specified in
2519 // a template-id shall match the type and form specified for the
2520 // corresponding parameter declared by the template in its
2521 // template-parameter-list.
Douglas Gregor14be16b2010-12-20 16:57:52 +00002522 llvm::SmallVector<TemplateArgument, 2> ArgumentPack;
2523 TemplateParameterList::iterator Param = Params->begin(),
2524 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002525 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002526 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002527 while (Param != ParamEnd) {
Douglas Gregor16134c62009-07-01 00:28:38 +00002528 if (ArgIdx > NumArgs && PartialTemplateArgs)
2529 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002530
Douglas Gregorf35f8282009-11-11 21:54:23 +00002531 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002532 // If we have an expanded parameter pack, make sure we don't have too
2533 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002534 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002535 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002536 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002537 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2538 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2539 << true
2540 << (isa<ClassTemplateDecl>(Template)? 0 :
2541 isa<FunctionTemplateDecl>(Template)? 1 :
2542 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2543 << Template;
2544 Diag(Template->getLocation(), diag::note_template_decl_here)
2545 << Params->getSourceRange();
2546 return true;
2547 }
2548 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002549
Douglas Gregorf35f8282009-11-11 21:54:23 +00002550 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002551 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2552 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002553 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002554 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002555
Douglas Gregor14be16b2010-12-20 16:57:52 +00002556 if ((*Param)->isTemplateParameterPack()) {
2557 // The template parameter was a template parameter pack, so take the
2558 // deduced argument and place it on the argument pack. Note that we
2559 // stay on the same template parameter so that we can deduce more
2560 // arguments.
2561 ArgumentPack.push_back(Converted.back());
2562 Converted.pop_back();
2563 } else {
2564 // Move to the next template parameter.
2565 ++Param;
2566 }
2567 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002568 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002569 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002570
2571 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002572 // arguments, just break out now and we'll fill in the argument pack below.
2573 if ((*Param)->isTemplateParameterPack())
2574 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002575
Douglas Gregorf35f8282009-11-11 21:54:23 +00002576 // We have a default template argument that we will use.
2577 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002578
Douglas Gregorf35f8282009-11-11 21:54:23 +00002579 // Retrieve the default template argument from the template
2580 // parameter. For each kind of template parameter, we substitute the
2581 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002582 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002583 // the default argument.
2584 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2585 if (!TTP->hasDefaultArgument()) {
2586 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2587 break;
2588 }
2589
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002590 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002591 Template,
2592 TemplateLoc,
2593 RAngleLoc,
2594 TTP,
2595 Converted);
2596 if (!ArgType)
2597 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002598
Douglas Gregorf35f8282009-11-11 21:54:23 +00002599 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2600 ArgType);
2601 } else if (NonTypeTemplateParmDecl *NTTP
2602 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2603 if (!NTTP->hasDefaultArgument()) {
2604 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2605 break;
2606 }
2607
John McCall60d7b3a2010-08-24 06:29:42 +00002608 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002609 TemplateLoc,
2610 RAngleLoc,
2611 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002612 Converted);
2613 if (E.isInvalid())
2614 return true;
2615
2616 Expr *Ex = E.takeAs<Expr>();
2617 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
2618 } else {
2619 TemplateTemplateParmDecl *TempParm
2620 = cast<TemplateTemplateParmDecl>(*Param);
2621
2622 if (!TempParm->hasDefaultArgument()) {
2623 assert((Invalid || PartialTemplateArgs) && "Missing default argument");
2624 break;
2625 }
2626
2627 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002628 TemplateLoc,
2629 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002630 TempParm,
2631 Converted);
2632 if (Name.isNull())
2633 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002634
2635 Arg = TemplateArgumentLoc(TemplateArgument(Name),
Douglas Gregorf35f8282009-11-11 21:54:23 +00002636 TempParm->getDefaultArgument().getTemplateQualifierRange(),
2637 TempParm->getDefaultArgument().getTemplateNameLoc());
2638 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002639
Douglas Gregorf35f8282009-11-11 21:54:23 +00002640 // Introduce an instantiation record that describes where we are using
2641 // the default template argument.
2642 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00002643 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002644 SourceRange(TemplateLoc, RAngleLoc));
2645
Douglas Gregorf35f8282009-11-11 21:54:23 +00002646 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00002647 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002648 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00002649 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002650
Douglas Gregor14be16b2010-12-20 16:57:52 +00002651 // Move to the next template parameter and argument.
2652 ++Param;
2653 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002654 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002655
Douglas Gregor14be16b2010-12-20 16:57:52 +00002656 // Form argument packs for each of the parameter packs remaining.
2657 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00002658 // If we're checking a partial list of template arguments, don't fill
2659 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002660
2661 if ((*Param)->isTemplateParameterPack()) {
Douglas Gregord3731192011-01-10 07:32:04 +00002662 if (PartialTemplateArgs && ArgumentPack.empty()) {
2663 Converted.push_back(TemplateArgument());
Douglas Gregor203e6a32011-01-11 23:09:57 +00002664 } else if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00002665 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00002666 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002667 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2668 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00002669 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00002670 ArgumentPack.clear();
2671 }
2672 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002673
Douglas Gregor14be16b2010-12-20 16:57:52 +00002674 ++Param;
2675 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002676
Douglas Gregorc15cb382009-02-09 23:23:08 +00002677 return Invalid;
2678}
2679
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002680namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002681 class UnnamedLocalNoLinkageFinder
2682 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002683 {
2684 Sema &S;
2685 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002686
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002687 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002688
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002689 public:
2690 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
2691
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002692 bool Visit(QualType T) {
2693 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002694 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002695
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002696#define TYPE(Class, Parent) \
2697 bool Visit##Class##Type(const Class##Type *);
2698#define ABSTRACT_TYPE(Class, Parent) \
2699 bool Visit##Class##Type(const Class##Type *) { return false; }
2700#define NON_CANONICAL_TYPE(Class, Parent) \
2701 bool Visit##Class##Type(const Class##Type *) { return false; }
2702#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002703
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002704 bool VisitTagDecl(const TagDecl *Tag);
2705 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
2706 };
2707}
2708
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002709bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002710 return false;
2711}
2712
2713bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
2714 return Visit(T->getElementType());
2715}
2716
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002717bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002718 return Visit(T->getPointeeType());
2719}
2720
2721bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002722 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002723 return Visit(T->getPointeeType());
2724}
2725
2726bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002727 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002728 return Visit(T->getPointeeType());
2729}
2730
2731bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002732 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002733 return Visit(T->getPointeeType());
2734}
2735
2736bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002737 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002738 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
2739}
2740
2741bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002742 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002743 return Visit(T->getElementType());
2744}
2745
2746bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002747 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002748 return Visit(T->getElementType());
2749}
2750
2751bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002752 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002753 return Visit(T->getElementType());
2754}
2755
2756bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002757 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002758 return Visit(T->getElementType());
2759}
2760
2761bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002762 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002763 return Visit(T->getElementType());
2764}
2765
2766bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
2767 return Visit(T->getElementType());
2768}
2769
2770bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
2771 return Visit(T->getElementType());
2772}
2773
2774bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
2775 const FunctionProtoType* T) {
2776 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002777 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002778 A != AEnd; ++A) {
2779 if (Visit(*A))
2780 return true;
2781 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002782
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002783 return Visit(T->getResultType());
2784}
2785
2786bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
2787 const FunctionNoProtoType* T) {
2788 return Visit(T->getResultType());
2789}
2790
2791bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
2792 const UnresolvedUsingType*) {
2793 return false;
2794}
2795
2796bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
2797 return false;
2798}
2799
2800bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
2801 return Visit(T->getUnderlyingType());
2802}
2803
2804bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
2805 return false;
2806}
2807
Richard Smith34b41d92011-02-20 03:19:35 +00002808bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
2809 return Visit(T->getDeducedType());
2810}
2811
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002812bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
2813 return VisitTagDecl(T->getDecl());
2814}
2815
2816bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
2817 return VisitTagDecl(T->getDecl());
2818}
2819
2820bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
2821 const TemplateTypeParmType*) {
2822 return false;
2823}
2824
Douglas Gregorc3069d62011-01-14 02:55:32 +00002825bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
2826 const SubstTemplateTypeParmPackType *) {
2827 return false;
2828}
2829
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002830bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
2831 const TemplateSpecializationType*) {
2832 return false;
2833}
2834
2835bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
2836 const InjectedClassNameType* T) {
2837 return VisitTagDecl(T->getDecl());
2838}
2839
2840bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
2841 const DependentNameType* T) {
2842 return VisitNestedNameSpecifier(T->getQualifier());
2843}
2844
2845bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
2846 const DependentTemplateSpecializationType* T) {
2847 return VisitNestedNameSpecifier(T->getQualifier());
2848}
2849
Douglas Gregor7536dd52010-12-20 02:24:11 +00002850bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
2851 const PackExpansionType* T) {
2852 return Visit(T->getPattern());
2853}
2854
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002855bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
2856 return false;
2857}
2858
2859bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
2860 const ObjCInterfaceType *) {
2861 return false;
2862}
2863
2864bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
2865 const ObjCObjectPointerType *) {
2866 return false;
2867}
2868
2869bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
2870 if (Tag->getDeclContext()->isFunctionOrMethod()) {
2871 S.Diag(SR.getBegin(), diag::ext_template_arg_local_type)
2872 << S.Context.getTypeDeclType(Tag) << SR;
2873 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002874 }
2875
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002876 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl()) {
2877 S.Diag(SR.getBegin(), diag::ext_template_arg_unnamed_type) << SR;
2878 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
2879 return true;
2880 }
2881
2882 return false;
2883}
2884
2885bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
2886 NestedNameSpecifier *NNS) {
2887 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
2888 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002889
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002890 switch (NNS->getKind()) {
2891 case NestedNameSpecifier::Identifier:
2892 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00002893 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002894 case NestedNameSpecifier::Global:
2895 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002896
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002897 case NestedNameSpecifier::TypeSpec:
2898 case NestedNameSpecifier::TypeSpecWithTemplate:
2899 return Visit(QualType(NNS->getAsType(), 0));
2900 }
Fariborz Jahanian7b1ec6c2010-10-13 16:19:16 +00002901 return false;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002902}
2903
2904
Douglas Gregorc15cb382009-02-09 23:23:08 +00002905/// \brief Check a template argument against its corresponding
2906/// template type parameter.
2907///
2908/// This routine implements the semantics of C++ [temp.arg.type]. It
2909/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00002910bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00002911 TypeSourceInfo *ArgInfo) {
2912 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00002913 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00002914 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00002915
2916 if (Arg->isVariablyModifiedType()) {
2917 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00002918 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00002919 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00002920 }
2921
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002922 // C++03 [temp.arg.type]p2:
2923 // A local type, a type with no linkage, an unnamed type or a type
2924 // compounded from any of these types shall not be used as a
2925 // template-argument for a template type-parameter.
2926 //
2927 // C++0x allows these, and even in C++03 we allow them as an extension with
2928 // a warning.
Douglas Gregordb4d4bb2010-10-13 18:05:20 +00002929 if (!LangOpts.CPlusPlus0x && Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00002930 UnnamedLocalNoLinkageFinder Finder(*this, SR);
2931 (void)Finder.Visit(Context.getCanonicalType(Arg));
2932 }
2933
Douglas Gregorc15cb382009-02-09 23:23:08 +00002934 return false;
2935}
2936
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002937/// \brief Checks whether the given template argument is the address
2938/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002939static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00002940CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
2941 NonTypeTemplateParmDecl *Param,
2942 QualType ParamType,
2943 Expr *ArgIn,
2944 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002945 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00002946 Expr *Arg = ArgIn;
2947 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002948
2949 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00002950 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002951 Arg = Cast->getSubExpr();
2952
2953 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00002954 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002955 // A template-argument for a non-type, non-template
2956 // template-parameter shall be one of: [...]
2957 //
2958 // -- the address of an object or function with external
2959 // linkage, including function templates and function
2960 // template-ids but excluding non-static class members,
2961 // expressed as & id-expression where the & is optional if
2962 // the name refers to a function or array, or if the
2963 // corresponding template-parameter is a reference; or
2964 DeclRefExpr *DRE = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002965
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002966 // In C++98/03 mode, give an extension warning on any extra parentheses.
2967 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
2968 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002969 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002970 if (!Invalid && !ExtraParens && !S.getLangOptions().CPlusPlus0x) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00002971 S.Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002972 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002973 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00002974 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002975 }
2976
2977 Arg = Parens->getSubExpr();
2978 }
2979
Douglas Gregorb7a09262010-04-01 18:32:35 +00002980 bool AddressTaken = false;
2981 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002982 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00002983 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002984 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
Douglas Gregorb7a09262010-04-01 18:32:35 +00002985 AddressTaken = true;
2986 AddrOpLoc = UnOp->getOperatorLoc();
2987 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00002988 } else
2989 DRE = dyn_cast<DeclRefExpr>(Arg);
2990
Douglas Gregorb7a09262010-04-01 18:32:35 +00002991 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00002992 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
2993 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00002994 S.Diag(Param->getLocation(), diag::note_template_param_here);
2995 return true;
2996 }
Chandler Carruth038cc392010-01-31 10:01:20 +00002997
2998 // Stop checking the precise nature of the argument if it is value dependent,
2999 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003000 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003001 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003002 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003003 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003004
Douglas Gregorb7a09262010-04-01 18:32:35 +00003005 if (!isa<ValueDecl>(DRE->getDecl())) {
3006 S.Diag(Arg->getSourceRange().getBegin(),
3007 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003008 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003009 S.Diag(Param->getLocation(), diag::note_template_param_here);
3010 return true;
3011 }
3012
3013 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003014
3015 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003016 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3017 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003018 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003019 S.Diag(Param->getLocation(), diag::note_template_param_here);
3020 return true;
3021 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003022
3023 // Cannot refer to non-static member functions
3024 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003025 if (!Method->isStatic()) {
3026 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003027 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003028 S.Diag(Param->getLocation(), diag::note_template_param_here);
3029 return true;
3030 }
Mike Stump1eb44332009-09-09 15:08:12 +00003031
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003032 // Functions must have external linkage.
3033 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003034 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003035 S.Diag(Arg->getSourceRange().getBegin(),
3036 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003037 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003038 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003039 << true;
3040 return true;
3041 }
3042
3043 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003044 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003045
Douglas Gregorb7a09262010-04-01 18:32:35 +00003046 // If the template parameter has pointer type, the function decays.
3047 if (ParamType->isPointerType() && !AddressTaken)
3048 ArgType = S.Context.getPointerType(Func->getType());
3049 else if (AddressTaken && ParamType->isReferenceType()) {
3050 // If we originally had an address-of operator, but the
3051 // parameter has reference type, complain and (if things look
3052 // like they will work) drop the address-of operator.
3053 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3054 ParamType.getNonReferenceType())) {
3055 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3056 << ParamType;
3057 S.Diag(Param->getLocation(), diag::note_template_param_here);
3058 return true;
3059 }
3060
3061 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3062 << ParamType
3063 << FixItHint::CreateRemoval(AddrOpLoc);
3064 S.Diag(Param->getLocation(), diag::note_template_param_here);
3065
3066 ArgType = Func->getType();
3067 }
3068 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003069 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003070 S.Diag(Arg->getSourceRange().getBegin(),
3071 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003072 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003073 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003074 << true;
3075 return true;
3076 }
3077
Douglas Gregorb7a09262010-04-01 18:32:35 +00003078 // A value of reference type is not an object.
3079 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003080 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003081 diag::err_template_arg_reference_var)
3082 << Var->getType() << Arg->getSourceRange();
3083 S.Diag(Param->getLocation(), diag::note_template_param_here);
3084 return true;
3085 }
3086
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003087 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003088 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003089
3090 // If the template parameter has pointer type, we must have taken
3091 // the address of this object.
3092 if (ParamType->isReferenceType()) {
3093 if (AddressTaken) {
3094 // If we originally had an address-of operator, but the
3095 // parameter has reference type, complain and (if things look
3096 // like they will work) drop the address-of operator.
3097 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3098 ParamType.getNonReferenceType())) {
3099 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3100 << ParamType;
3101 S.Diag(Param->getLocation(), diag::note_template_param_here);
3102 return true;
3103 }
3104
3105 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3106 << ParamType
3107 << FixItHint::CreateRemoval(AddrOpLoc);
3108 S.Diag(Param->getLocation(), diag::note_template_param_here);
3109
3110 ArgType = Var->getType();
3111 }
3112 } else if (!AddressTaken && ParamType->isPointerType()) {
3113 if (Var->getType()->isArrayType()) {
3114 // Array-to-pointer decay.
3115 ArgType = S.Context.getArrayDecayedType(Var->getType());
3116 } else {
3117 // If the template parameter has pointer type but the address of
3118 // this object was not taken, complain and (possibly) recover by
3119 // taking the address of the entity.
3120 ArgType = S.Context.getPointerType(Var->getType());
3121 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3122 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3123 << ParamType;
3124 S.Diag(Param->getLocation(), diag::note_template_param_here);
3125 return true;
3126 }
3127
3128 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3129 << ParamType
3130 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3131
3132 S.Diag(Param->getLocation(), diag::note_template_param_here);
3133 }
3134 }
3135 } else {
3136 // We found something else, but we don't know specifically what it is.
3137 S.Diag(Arg->getSourceRange().getBegin(),
3138 diag::err_template_arg_not_object_or_func)
3139 << Arg->getSourceRange();
3140 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3141 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003142 }
Mike Stump1eb44332009-09-09 15:08:12 +00003143
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003144 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003145 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003146 S.IsQualificationConversion(ArgType, ParamType, false)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003147 // For pointer-to-object types, qualification conversions are
3148 // permitted.
3149 } else {
3150 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3151 if (!ParamRef->getPointeeType()->isFunctionType()) {
3152 // C++ [temp.arg.nontype]p5b3:
3153 // For a non-type template-parameter of type reference to
3154 // object, no conversions apply. The type referred to by the
3155 // reference may be more cv-qualified than the (otherwise
3156 // identical) type of the template- argument. The
3157 // template-parameter is bound directly to the
3158 // template-argument, which shall be an lvalue.
3159
3160 // FIXME: Other qualifiers?
3161 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3162 unsigned ArgQuals = ArgType.getCVRQualifiers();
3163
3164 if ((ParamQuals | ArgQuals) != ParamQuals) {
3165 S.Diag(Arg->getSourceRange().getBegin(),
3166 diag::err_template_arg_ref_bind_ignores_quals)
3167 << ParamType << Arg->getType()
3168 << Arg->getSourceRange();
3169 S.Diag(Param->getLocation(), diag::note_template_param_here);
3170 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003171 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003172 }
3173 }
3174
3175 // At this point, the template argument refers to an object or
3176 // function with external linkage. We now need to check whether the
3177 // argument and parameter types are compatible.
3178 if (!S.Context.hasSameUnqualifiedType(ArgType,
3179 ParamType.getNonReferenceType())) {
3180 // We can't perform this conversion or binding.
3181 if (ParamType->isReferenceType())
3182 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
3183 << ParamType << Arg->getType() << Arg->getSourceRange();
3184 else
3185 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
3186 << Arg->getType() << ParamType << Arg->getSourceRange();
3187 S.Diag(Param->getLocation(), diag::note_template_param_here);
3188 return true;
3189 }
3190 }
3191
3192 // Create the template argument.
3193 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003194 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003195 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003196}
3197
3198/// \brief Checks whether the given template argument is a pointer to
3199/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003200bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003201 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003202 bool Invalid = false;
3203
3204 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003205 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003206 Arg = Cast->getSubExpr();
3207
3208 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003209 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003210 // A template-argument for a non-type, non-template
3211 // template-parameter shall be one of: [...]
3212 //
3213 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003214 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003215
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003216 // In C++98/03 mode, give an extension warning on any extra parentheses.
3217 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3218 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003219 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003220 if (!Invalid && !ExtraParens && !getLangOptions().CPlusPlus0x) {
Mike Stump1eb44332009-09-09 15:08:12 +00003221 Diag(Arg->getSourceRange().getBegin(),
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003222 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003223 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003224 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003225 }
3226
3227 Arg = Parens->getSubExpr();
3228 }
3229
Douglas Gregorcaddba02009-11-12 18:38:13 +00003230 // A pointer-to-member constant written &Class::member.
3231 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003232 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003233 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3234 if (DRE && !DRE->getQualifier())
3235 DRE = 0;
3236 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003237 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003238 // A constant of pointer-to-member type.
3239 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3240 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3241 if (VD->getType()->isMemberPointerType()) {
3242 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003243 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003244 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3245 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003246 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003247 else
3248 Converted = TemplateArgument(VD->getCanonicalDecl());
3249 return Invalid;
3250 }
3251 }
3252 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003253
Douglas Gregorcaddba02009-11-12 18:38:13 +00003254 DRE = 0;
3255 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003256
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003257 if (!DRE)
3258 return Diag(Arg->getSourceRange().getBegin(),
3259 diag::err_template_arg_not_pointer_to_member_form)
3260 << Arg->getSourceRange();
3261
3262 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3263 assert((isa<FieldDecl>(DRE->getDecl()) ||
3264 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3265 "Only non-static member pointers can make it here");
3266
3267 // Okay: this is the address of a non-static member, and therefore
3268 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003269 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003270 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003271 else
3272 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003273 return Invalid;
3274 }
3275
3276 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003277 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003278 diag::err_template_arg_not_pointer_to_member_form)
3279 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003280 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003281 diag::note_template_arg_refers_here);
3282 return true;
3283}
3284
Douglas Gregorc15cb382009-02-09 23:23:08 +00003285/// \brief Check a template argument against its corresponding
3286/// non-type template parameter.
3287///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003288/// This routine implements the semantics of C++ [temp.arg.nontype].
3289/// It returns true if an error occurred, and false otherwise. \p
3290/// InstantiatedParamType is the type of the non-type template
3291/// parameter after it has been instantiated.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003292///
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003293/// If no error was detected, Converted receives the converted template argument.
Douglas Gregorc15cb382009-02-09 23:23:08 +00003294bool Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
Mike Stump1eb44332009-09-09 15:08:12 +00003295 QualType InstantiatedParamType, Expr *&Arg,
Douglas Gregor02024a92010-03-28 02:42:43 +00003296 TemplateArgument &Converted,
3297 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003298 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3299
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003300 // If either the parameter has a dependent type or the argument is
3301 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003302 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3303 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003304 Converted = TemplateArgument(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003305 return false;
Douglas Gregor40808ce2009-03-09 23:48:35 +00003306 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003307
3308 // C++ [temp.arg.nontype]p5:
3309 // The following conversions are performed on each expression used
3310 // as a non-type template-argument. If a non-type
3311 // template-argument cannot be converted to the type of the
3312 // corresponding template-parameter then the program is
3313 // ill-formed.
3314 //
3315 // -- for a non-type template-parameter of integral or
3316 // enumeration type, integral promotions (4.5) and integral
3317 // conversions (4.7) are applied.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003318 QualType ParamType = InstantiatedParamType;
Douglas Gregora35284b2009-02-11 00:19:33 +00003319 QualType ArgType = Arg->getType();
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003320 if (ParamType->isIntegralOrEnumerationType()) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003321 // C++ [temp.arg.nontype]p1:
3322 // A template-argument for a non-type, non-template
3323 // template-parameter shall be one of:
3324 //
3325 // -- an integral constant-expression of integral or enumeration
3326 // type; or
3327 // -- the name of a non-type template-parameter; or
3328 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003329 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003330 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003331 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003332 diag::err_template_arg_not_integral_or_enumeral)
3333 << ArgType << Arg->getSourceRange();
3334 Diag(Param->getLocation(), diag::note_template_param_here);
3335 return true;
3336 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003337 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003338 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3339 << ArgType << Arg->getSourceRange();
3340 return true;
3341 }
3342
Douglas Gregor02024a92010-03-28 02:42:43 +00003343 // From here on out, all we care about are the unqualified forms
3344 // of the parameter and argument types.
3345 ParamType = ParamType.getUnqualifiedType();
3346 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003347
3348 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003349 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003350 // Okay: no conversion necessary
Douglas Gregor02024a92010-03-28 02:42:43 +00003351 } else if (CTAK == CTAK_Deduced) {
3352 // C++ [temp.deduct.type]p17:
3353 // If, in the declaration of a function template with a non-type
3354 // template-parameter, the non-type template- parameter is used
3355 // in an expression in the function parameter-list and, if the
3356 // corresponding template-argument is deduced, the
3357 // template-argument type shall match the type of the
3358 // template-parameter exactly, except that a template-argument
3359 // deduced from an array bound may be of any integral type.
3360 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3361 << ArgType << ParamType;
3362 Diag(Param->getLocation(), diag::note_template_param_here);
John McCalldaa8e4e2010-11-15 09:13:47 +00003363 return true;
3364 } else if (ParamType->isBooleanType()) {
3365 // This is an integral-to-boolean conversion.
3366 ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003367 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3368 !ParamType->isEnumeralType()) {
3369 // This is an integral promotion or conversion.
John McCall2de56d12010-08-25 11:45:40 +00003370 ImpCastExprToType(Arg, ParamType, CK_IntegralCast);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003371 } else {
3372 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003373 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003374 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003375 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003376 Diag(Param->getLocation(), diag::note_template_param_here);
3377 return true;
3378 }
3379
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003380 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003381 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003382 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003383
3384 if (!Arg->isValueDependent()) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003385 llvm::APSInt OldValue = Value;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003386
3387 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003388 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003389 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003390 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003391 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003392 Value.setIsSigned(IntegerType->isSignedIntegerType());
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003393
3394 // Complain if an unsigned parameter received a negative value.
3395 if (IntegerType->isUnsignedIntegerType()
3396 && (OldValue.isSigned() && OldValue.isNegative())) {
3397 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3398 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3399 << Arg->getSourceRange();
3400 Diag(Param->getLocation(), diag::note_template_param_here);
3401 }
3402
3403 // Complain if we overflowed the template parameter's type.
3404 unsigned RequiredBits;
3405 if (IntegerType->isUnsignedIntegerType())
3406 RequiredBits = OldValue.getActiveBits();
3407 else if (OldValue.isUnsigned())
3408 RequiredBits = OldValue.getActiveBits() + 1;
3409 else
3410 RequiredBits = OldValue.getMinSignedBits();
3411 if (RequiredBits > AllowedBits) {
3412 Diag(Arg->getSourceRange().getBegin(),
3413 diag::warn_template_arg_too_large)
3414 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3415 << Arg->getSourceRange();
3416 Diag(Param->getLocation(), diag::note_template_param_here);
3417 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003418 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003419
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003420 // Add the value of this argument to the list of converted
3421 // arguments. We use the bitwidth and signedness of the template
3422 // parameter.
3423 if (Arg->isValueDependent()) {
3424 // The argument is value-dependent. Create a new
3425 // TemplateArgument with the converted expression.
3426 Converted = TemplateArgument(Arg);
3427 return false;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003428 }
3429
John McCall833ca992009-10-29 08:12:44 +00003430 Converted = TemplateArgument(Value,
Mike Stump1eb44332009-09-09 15:08:12 +00003431 ParamType->isEnumeralType() ? ParamType
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003432 : IntegerType);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003433 return false;
3434 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003435
John McCall6bb80172010-03-30 21:47:33 +00003436 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3437
Douglas Gregorb7a09262010-04-01 18:32:35 +00003438 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3439 // from a template argument of type std::nullptr_t to a non-type
3440 // template parameter of type pointer to object, pointer to
3441 // function, or pointer-to-member, respectively.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003442 if (ArgType->isNullPtrType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003443 (ParamType->isPointerType() || ParamType->isMemberPointerType())) {
3444 Converted = TemplateArgument((NamedDecl *)0);
3445 return false;
3446 }
3447
Douglas Gregorb86b0572009-02-11 01:18:59 +00003448 // Handle pointer-to-function, reference-to-function, and
3449 // pointer-to-member-function all in (roughly) the same way.
3450 if (// -- For a non-type template-parameter of type pointer to
3451 // function, only the function-to-pointer conversion (4.3) is
3452 // applied. If the template-argument represents a set of
3453 // overloaded functions (or a pointer to such), the matching
3454 // function is selected from the set (13.4).
3455 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003456 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003457 // -- For a non-type template-parameter of type reference to
3458 // function, no conversions apply. If the template-argument
3459 // represents a set of overloaded functions, the matching
3460 // function is selected from the set (13.4).
3461 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003462 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003463 // -- For a non-type template-parameter of type pointer to
3464 // member function, no conversions apply. If the
3465 // template-argument represents a set of overloaded member
3466 // functions, the matching member function is selected from
3467 // the set (13.4).
3468 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003469 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003470 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003471
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003472 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003473 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003474 true,
3475 FoundResult)) {
3476 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3477 return true;
3478
3479 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3480 ArgType = Arg->getType();
3481 } else
Douglas Gregor48f3bb92009-02-18 21:56:37 +00003482 return true;
Douglas Gregora35284b2009-02-11 00:19:33 +00003483 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003484
Douglas Gregorb7a09262010-04-01 18:32:35 +00003485 if (!ParamType->isMemberPointerType())
3486 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003487 ParamType,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003488 Arg, Converted);
3489
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003490 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
3491 false)) {
John McCall2de56d12010-08-25 11:45:40 +00003492 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregorb7a09262010-04-01 18:32:35 +00003493 } else if (!Context.hasSameUnqualifiedType(ArgType,
3494 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00003495 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003496 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00003497 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003498 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00003499 Diag(Param->getLocation(), diag::note_template_param_here);
3500 return true;
3501 }
Mike Stump1eb44332009-09-09 15:08:12 +00003502
Douglas Gregorb7a09262010-04-01 18:32:35 +00003503 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregora35284b2009-02-11 00:19:33 +00003504 }
3505
Chris Lattnerfe90de72009-02-20 21:37:53 +00003506 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003507 // -- for a non-type template-parameter of type pointer to
3508 // object, qualification conversions (4.4) and the
3509 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00003510 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00003511 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003512 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003513
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003514 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003515 ParamType,
3516 Arg, Converted);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003517 }
Mike Stump1eb44332009-09-09 15:08:12 +00003518
Ted Kremenek6217b802009-07-29 21:53:49 +00003519 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00003520 // -- For a non-type template-parameter of type reference to
3521 // object, no conversions apply. The type referred to by the
3522 // reference may be more cv-qualified than the (otherwise
3523 // identical) type of the template-argument. The
3524 // template-parameter is bound directly to the
3525 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00003526 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00003527 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00003528
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003529 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003530 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
3531 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003532 true,
3533 FoundResult)) {
3534 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
3535 return true;
3536
3537 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3538 ArgType = Arg->getType();
3539 } else
Douglas Gregorb7a09262010-04-01 18:32:35 +00003540 return true;
Douglas Gregorb86b0572009-02-11 01:18:59 +00003541 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003542
3543 return CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
Douglas Gregorb7a09262010-04-01 18:32:35 +00003544 ParamType,
3545 Arg, Converted);
Douglas Gregorb86b0572009-02-11 01:18:59 +00003546 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00003547
3548 // -- For a non-type template-parameter of type pointer to data
3549 // member, qualification conversions (4.4) are applied.
3550 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
3551
Douglas Gregor8e6563b2009-02-11 18:22:40 +00003552 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00003553 // Types match exactly: nothing more to do here.
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003554 } else if (IsQualificationConversion(ArgType, ParamType, false)) {
John McCall2de56d12010-08-25 11:45:40 +00003555 ImpCastExprToType(Arg, ParamType, CK_NoOp, CastCategory(Arg));
Douglas Gregor658bbb52009-02-11 16:16:59 +00003556 } else {
3557 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003558 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00003559 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003560 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00003561 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00003562 return true;
Douglas Gregor658bbb52009-02-11 16:16:59 +00003563 }
3564
Douglas Gregorcaddba02009-11-12 18:38:13 +00003565 return CheckTemplateArgumentPointerToMember(Arg, Converted);
Douglas Gregorc15cb382009-02-09 23:23:08 +00003566}
3567
3568/// \brief Check a template argument against its corresponding
3569/// template template parameter.
3570///
3571/// This routine implements the semantics of C++ [temp.arg.template].
3572/// It returns true if an error occurred, and false otherwise.
3573bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00003574 const TemplateArgumentLoc &Arg) {
3575 TemplateName Name = Arg.getArgument().getAsTemplate();
3576 TemplateDecl *Template = Name.getAsTemplateDecl();
3577 if (!Template) {
3578 // Any dependent template name is fine.
3579 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
3580 return false;
3581 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00003582
3583 // C++ [temp.arg.template]p1:
3584 // A template-argument for a template template-parameter shall be
3585 // the name of a class template, expressed as id-expression. Only
3586 // primary class templates are considered when matching the
3587 // template template argument with the corresponding parameter;
3588 // partial specializations are not considered even if their
3589 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003590 //
3591 // Note that we also allow template template parameters here, which
3592 // will happen when we are dealing with, e.g., class template
3593 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00003594 if (!isa<ClassTemplateDecl>(Template) &&
Douglas Gregorba1ecb52009-06-12 19:43:02 +00003595 !isa<TemplateTemplateParmDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00003596 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00003597 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00003598 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00003599 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00003600 << Template;
3601 }
3602
3603 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
3604 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003605 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003606 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00003607 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00003608}
3609
Douglas Gregor02024a92010-03-28 02:42:43 +00003610/// \brief Given a non-type template argument that refers to a
3611/// declaration and the type of its corresponding non-type template
3612/// parameter, produce an expression that properly refers to that
3613/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003614ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003615Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
3616 QualType ParamType,
3617 SourceLocation Loc) {
3618 assert(Arg.getKind() == TemplateArgument::Declaration &&
3619 "Only declaration template arguments permitted here");
3620 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
3621
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003622 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00003623 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
3624 // If the value is a class member, we might have a pointer-to-member.
3625 // Determine whether the non-type template template parameter is of
3626 // pointer-to-member type. If so, we need to build an appropriate
3627 // expression for a pointer-to-member, since a "normal" DeclRefExpr
3628 // would refer to the member itself.
3629 if (ParamType->isMemberPointerType()) {
3630 QualType ClassType
3631 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
3632 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00003633 = NestedNameSpecifier::Create(Context, 0, false,
3634 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00003635 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00003636 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00003637
3638 // The actual value-ness of this is unimportant, but for
3639 // internal consistency's sake, references to instance methods
3640 // are r-values.
3641 ExprValueKind VK = VK_LValue;
3642 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
3643 VK = VK_RValue;
3644
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003645 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00003646 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00003647 VK,
John McCallf89e55a2010-11-18 06:31:45 +00003648 Loc,
3649 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00003650 if (RefExpr.isInvalid())
3651 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003652
John McCall2de56d12010-08-25 11:45:40 +00003653 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003654
Douglas Gregorc0c83002010-04-30 21:46:38 +00003655 // We might need to perform a trailing qualification conversion, since
3656 // the element type on the parameter could be more qualified than the
3657 // element type in the expression we constructed.
3658 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
Douglas Gregor14d0aee2011-01-27 00:58:17 +00003659 ParamType.getUnqualifiedType(), false)) {
Douglas Gregorc0c83002010-04-30 21:46:38 +00003660 Expr *RefE = RefExpr.takeAs<Expr>();
John McCall2de56d12010-08-25 11:45:40 +00003661 ImpCastExprToType(RefE, ParamType.getUnqualifiedType(), CK_NoOp);
Douglas Gregorc0c83002010-04-30 21:46:38 +00003662 RefExpr = Owned(RefE);
3663 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003664
Douglas Gregor02024a92010-03-28 02:42:43 +00003665 assert(!RefExpr.isInvalid() &&
3666 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00003667 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00003668 return move(RefExpr);
3669 }
3670 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003671
Douglas Gregor02024a92010-03-28 02:42:43 +00003672 QualType T = VD->getType().getNonReferenceType();
3673 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003674 // When the non-type template parameter is a pointer, take the
3675 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00003676 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003677 if (RefExpr.isInvalid())
3678 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003679
3680 if (T->isFunctionType() || T->isArrayType()) {
3681 // Decay functions and arrays.
3682 Expr *RefE = (Expr *)RefExpr.get();
3683 DefaultFunctionArrayConversion(RefE);
3684 if (RefE != RefExpr.get()) {
3685 RefExpr.release();
3686 RefExpr = Owned(RefE);
3687 }
3688
3689 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00003690 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003691
Douglas Gregorb7a09262010-04-01 18:32:35 +00003692 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00003693 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00003694 }
3695
John McCallf89e55a2010-11-18 06:31:45 +00003696 ExprValueKind VK = VK_RValue;
3697
Douglas Gregor02024a92010-03-28 02:42:43 +00003698 // If the non-type template parameter has reference type, qualify the
3699 // resulting declaration reference with the extra qualifiers on the
3700 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00003701 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
3702 VK = VK_LValue;
3703 T = Context.getQualifiedType(T,
3704 TargetRef->getPointeeType().getQualifiers());
3705 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003706
John McCallf89e55a2010-11-18 06:31:45 +00003707 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00003708}
3709
3710/// \brief Construct a new expression that refers to the given
3711/// integral template argument with the given source-location
3712/// information.
3713///
3714/// This routine takes care of the mapping from an integral template
3715/// argument (which may have any integral type) to the appropriate
3716/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003717ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00003718Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
3719 SourceLocation Loc) {
3720 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00003721 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00003722 QualType T = Arg.getIntegralType();
3723 if (T->isCharType() || T->isWideCharType())
3724 return Owned(new (Context) CharacterLiteral(
3725 Arg.getAsIntegral()->getZExtValue(),
3726 T->isWideCharType(),
3727 T,
3728 Loc));
3729 if (T->isBooleanType())
3730 return Owned(new (Context) CXXBoolLiteralExpr(
3731 Arg.getAsIntegral()->getBoolValue(),
3732 T,
3733 Loc));
3734
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003735 QualType BT;
3736 if (const EnumType *ET = T->getAs<EnumType>())
3737 BT = ET->getDecl()->getPromotionType();
3738 else
3739 BT = T;
3740
3741 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
Douglas Gregor8f5667d2011-02-18 02:12:44 +00003742 if (T->isEnumeralType()) {
3743 // FIXME: This is a hack. We need a better way to handle substituted
3744 // non-type template parameters.
3745 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast,
3746 E, 0,
3747 Context.getTrivialTypeSourceInfo(T, Loc),
3748 Loc, Loc);
3749 }
3750
Peter Collingbournefb7b3632010-12-15 15:06:14 +00003751 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00003752}
3753
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003754/// \brief Match two template parameters within template parameter lists.
3755static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
3756 bool Complain,
3757 Sema::TemplateParameterListEqualKind Kind,
3758 SourceLocation TemplateArgLoc) {
3759 // Check the actual kind (type, non-type, template).
3760 if (Old->getKind() != New->getKind()) {
3761 if (Complain) {
3762 unsigned NextDiag = diag::err_template_param_different_kind;
3763 if (TemplateArgLoc.isValid()) {
3764 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3765 NextDiag = diag::note_template_param_different_kind;
3766 }
3767 S.Diag(New->getLocation(), NextDiag)
3768 << (Kind != Sema::TPL_TemplateMatch);
3769 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
3770 << (Kind != Sema::TPL_TemplateMatch);
3771 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003772
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003773 return false;
3774 }
3775
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003776 // Check that both are parameter packs are neither are parameter packs.
3777 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00003778 // template template parameter, the template template parameter can have
3779 // a parameter pack where the template template argument does not.
3780 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
3781 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3782 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003783 if (Complain) {
3784 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
3785 if (TemplateArgLoc.isValid()) {
3786 S.Diag(TemplateArgLoc,
3787 diag::err_template_arg_template_params_mismatch);
3788 NextDiag = diag::note_template_parameter_pack_non_pack;
3789 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003790
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003791 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
3792 : isa<NonTypeTemplateParmDecl>(New)? 1
3793 : 2;
3794 S.Diag(New->getLocation(), NextDiag)
3795 << ParamKind << New->isParameterPack();
3796 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
3797 << ParamKind << Old->isParameterPack();
3798 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003799
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003800 return false;
3801 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003802
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003803 // For non-type template parameters, check the type of the parameter.
3804 if (NonTypeTemplateParmDecl *OldNTTP
3805 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
3806 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003807
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003808 // If we are matching a template template argument to a template
3809 // template parameter and one of the non-type template parameter types
3810 // is dependent, then we must wait until template instantiation time
3811 // to actually compare the arguments.
3812 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
3813 (OldNTTP->getType()->isDependentType() ||
3814 NewNTTP->getType()->isDependentType()))
3815 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003816
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003817 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
3818 if (Complain) {
3819 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
3820 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003821 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003822 diag::err_template_arg_template_params_mismatch);
3823 NextDiag = diag::note_template_nontype_parm_different_type;
3824 }
3825 S.Diag(NewNTTP->getLocation(), NextDiag)
3826 << NewNTTP->getType()
3827 << (Kind != Sema::TPL_TemplateMatch);
3828 S.Diag(OldNTTP->getLocation(),
3829 diag::note_template_nontype_parm_prev_declaration)
3830 << OldNTTP->getType();
3831 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003832
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003833 return false;
3834 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003835
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003836 return true;
3837 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003838
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003839 // For template template parameters, check the template parameter types.
3840 // The template parameter lists of template template
3841 // parameters must agree.
3842 if (TemplateTemplateParmDecl *OldTTP
3843 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003844 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003845 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
3846 OldTTP->getTemplateParameters(),
3847 Complain,
3848 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003849 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003850 : Kind),
3851 TemplateArgLoc);
3852 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003853
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003854 return true;
3855}
Douglas Gregor02024a92010-03-28 02:42:43 +00003856
Douglas Gregora0347822011-01-13 00:08:50 +00003857/// \brief Diagnose a known arity mismatch when comparing template argument
3858/// lists.
3859static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003860void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00003861 TemplateParameterList *New,
3862 TemplateParameterList *Old,
3863 Sema::TemplateParameterListEqualKind Kind,
3864 SourceLocation TemplateArgLoc) {
3865 unsigned NextDiag = diag::err_template_param_list_different_arity;
3866 if (TemplateArgLoc.isValid()) {
3867 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
3868 NextDiag = diag::note_template_param_list_different_arity;
3869 }
3870 S.Diag(New->getTemplateLoc(), NextDiag)
3871 << (New->size() > Old->size())
3872 << (Kind != Sema::TPL_TemplateMatch)
3873 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
3874 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
3875 << (Kind != Sema::TPL_TemplateMatch)
3876 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
3877}
3878
Douglas Gregorddc29e12009-02-06 22:42:48 +00003879/// \brief Determine whether the given template parameter lists are
3880/// equivalent.
3881///
Mike Stump1eb44332009-09-09 15:08:12 +00003882/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00003883/// source code as part of a new template declaration.
3884///
3885/// \param Old The old template parameter list, typically found via
3886/// name lookup of the template declared with this template parameter
3887/// list.
3888///
3889/// \param Complain If true, this routine will produce a diagnostic if
3890/// the template parameter lists are not equivalent.
3891///
Douglas Gregorfb898e12009-11-12 16:20:59 +00003892/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00003893///
3894/// \param TemplateArgLoc If this source location is valid, then we
3895/// are actually checking the template parameter list of a template
3896/// argument (New) against the template parameter list of its
3897/// corresponding template template parameter (Old). We produce
3898/// slightly different diagnostics in this scenario.
3899///
Douglas Gregorddc29e12009-02-06 22:42:48 +00003900/// \returns True if the template parameter lists are equal, false
3901/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003902bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00003903Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
3904 TemplateParameterList *Old,
3905 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00003906 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00003907 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00003908 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
3909 if (Complain)
3910 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3911 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00003912
3913 return false;
3914 }
3915
Douglas Gregorab7ddf02011-01-12 23:45:44 +00003916 // C++0x [temp.arg.template]p3:
3917 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003918 // when each of the template parameters in the template-parameter-list of
3919 // the template-argument's corresponding class template or template alias
3920 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00003921 // template-parameter-list of P. [...]
3922 TemplateParameterList::iterator NewParm = New->begin();
3923 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00003924 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00003925 OldParmEnd = Old->end();
3926 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00003927 if (Kind != TPL_TemplateTemplateArgumentMatch ||
3928 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00003929 if (NewParm == NewParmEnd) {
3930 if (Complain)
3931 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3932 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003933
Douglas Gregora0347822011-01-13 00:08:50 +00003934 return false;
3935 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003936
Douglas Gregora0347822011-01-13 00:08:50 +00003937 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
3938 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003939 return false;
3940
Douglas Gregora0347822011-01-13 00:08:50 +00003941 ++NewParm;
3942 continue;
3943 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003944
Douglas Gregora0347822011-01-13 00:08:50 +00003945 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00003946 // [...] When P's template- parameter-list contains a template parameter
3947 // pack (14.5.3), the template parameter pack will match zero or more
3948 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00003949 // template-parameter-list of A with the same type and form as the
3950 // template parameter pack in P (ignoring whether those template
3951 // parameters are template parameter packs).
3952 for (; NewParm != NewParmEnd; ++NewParm) {
3953 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
3954 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003955 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00003956 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00003957 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003958
Douglas Gregora0347822011-01-13 00:08:50 +00003959 // Make sure we exhausted all of the arguments.
3960 if (NewParm != NewParmEnd) {
3961 if (Complain)
3962 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
3963 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003964
Douglas Gregora0347822011-01-13 00:08:50 +00003965 return false;
3966 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003967
Douglas Gregorddc29e12009-02-06 22:42:48 +00003968 return true;
3969}
3970
3971/// \brief Check whether a template can be declared within this scope.
3972///
3973/// If the template declaration is valid in this scope, returns
3974/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00003975bool
Douglas Gregor05396e22009-08-25 17:23:04 +00003976Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorddc29e12009-02-06 22:42:48 +00003977 // Find the nearest enclosing declaration scope.
3978 while ((S->getFlags() & Scope::DeclScope) == 0 ||
3979 (S->getFlags() & Scope::TemplateParamScope) != 0)
3980 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00003981
Douglas Gregorddc29e12009-02-06 22:42:48 +00003982 // C++ [temp]p2:
3983 // A template-declaration can appear only as a namespace scope or
3984 // class scope declaration.
3985 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00003986 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
3987 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00003988 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00003989 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003990
Eli Friedman1503f772009-07-31 01:43:05 +00003991 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00003992 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00003993
3994 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
3995 return false;
3996
Mike Stump1eb44332009-09-09 15:08:12 +00003997 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00003998 diag::err_template_outside_namespace_or_class_scope)
3999 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004000}
Douglas Gregorcc636682009-02-17 23:15:12 +00004001
Douglas Gregord5cb8762009-10-07 00:13:32 +00004002/// \brief Determine what kind of template specialization the given declaration
4003/// is.
4004static TemplateSpecializationKind getTemplateSpecializationKind(NamedDecl *D) {
4005 if (!D)
4006 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004007
Douglas Gregorf6b11852009-10-08 15:14:33 +00004008 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4009 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004010 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4011 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004012 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4013 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004014
Douglas Gregord5cb8762009-10-07 00:13:32 +00004015 return TSK_Undeclared;
4016}
4017
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004018/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004019/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004020///
Douglas Gregor9302da62009-10-14 23:50:59 +00004021/// This routine determines whether a template specialization can be declared
4022/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004023///
4024/// \param S the semantic analysis object for which this check is being
4025/// performed.
4026///
4027/// \param Specialized the entity being specialized or instantiated, which
4028/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004029/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004030/// member class).
4031///
4032/// \param PrevDecl the previous declaration of this entity, if any.
4033///
4034/// \param Loc the location of the explicit specialization or instantiation of
4035/// this entity.
4036///
4037/// \param IsPartialSpecialization whether this is a partial specialization of
4038/// a class template.
4039///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004040/// \returns true if there was an error that we cannot recover from, false
4041/// otherwise.
4042static bool CheckTemplateSpecializationScope(Sema &S,
4043 NamedDecl *Specialized,
4044 NamedDecl *PrevDecl,
4045 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004046 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004047 // Keep these "kind" numbers in sync with the %select statements in the
4048 // various diagnostics emitted by this routine.
4049 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004050 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004051 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004052 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004053 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004054 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004055 EntityKind = 3;
4056 else if (isa<VarDecl>(Specialized))
4057 EntityKind = 4;
4058 else if (isa<RecordDecl>(Specialized))
4059 EntityKind = 5;
4060 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004061 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4062 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004063 return true;
4064 }
4065
Douglas Gregor88b70942009-02-25 22:02:03 +00004066 // C++ [temp.expl.spec]p2:
4067 // An explicit specialization shall be declared in the namespace
4068 // of which the template is a member, or, for member templates, in
4069 // the namespace of which the enclosing class or enclosing class
4070 // template is a member. An explicit specialization of a member
4071 // function, member class or static data member of a class
4072 // template shall be declared in the namespace of which the class
4073 // template is a member. Such a declaration may also be a
4074 // definition. If the declaration is not a definition, the
4075 // specialization may be defined later in the name- space in which
4076 // the explicit specialization was declared, or in a namespace
4077 // that encloses the one in which the explicit specialization was
4078 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004079 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004080 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004081 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004082 return true;
4083 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004084
Douglas Gregor0a407472009-10-07 17:30:37 +00004085 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
4086 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004087 << Specialized;
Douglas Gregor0a407472009-10-07 17:30:37 +00004088 return true;
4089 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004090
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004091 // C++ [temp.class.spec]p6:
4092 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004093 // in any namespace scope in which its definition may be defined (14.5.1
4094 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004095 bool ComplainedAboutScope = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004096 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004097 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004098 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004099 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004100 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4101 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004102 // C++ [temp.exp.spec]p2:
4103 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004104 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004105 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004106 // An explicit specialization of a member function, member class or
4107 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004108 // namespace of which the class template is a member.
4109 //
4110 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004111 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004112 // the specialized template.
4113 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext) &&
4114 !(S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext))) {
Douglas Gregora4d5de52010-09-12 05:24:55 +00004115 bool IsCPlusPlus0xExtension
4116 = !S.getLangOptions().CPlusPlus0x && DC->Encloses(SpecializedContext);
Douglas Gregor9302da62009-10-14 23:50:59 +00004117 if (isa<TranslationUnitDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004118 S.Diag(Loc, IsCPlusPlus0xExtension
4119 ? diag::ext_template_spec_decl_out_of_scope_global
4120 : diag::err_template_spec_decl_out_of_scope_global)
4121 << EntityKind << Specialized;
Douglas Gregor9302da62009-10-14 23:50:59 +00004122 else if (isa<NamespaceDecl>(SpecializedContext))
Douglas Gregora4d5de52010-09-12 05:24:55 +00004123 S.Diag(Loc, IsCPlusPlus0xExtension
4124 ? diag::ext_template_spec_decl_out_of_scope
4125 : diag::err_template_spec_decl_out_of_scope)
4126 << EntityKind << Specialized
4127 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004128
Douglas Gregor9302da62009-10-14 23:50:59 +00004129 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
4130 ComplainedAboutScope = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004131 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004132 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004133
4134 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004135 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004136 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004137 // specializations of function templates, static data members, and member
4138 // functions, so we skip the check here for those kinds of entities.
4139 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004140 // Should we refactor that check, so that it occurs later?
4141 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004142 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4143 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004144 if (isa<TranslationUnitDecl>(SpecializedContext))
4145 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4146 << EntityKind << Specialized;
4147 else if (isa<NamespaceDecl>(SpecializedContext))
4148 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4149 << EntityKind << Specialized
4150 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004151
Douglas Gregor9302da62009-10-14 23:50:59 +00004152 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004153 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004154
Douglas Gregord5cb8762009-10-07 00:13:32 +00004155 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004156
Douglas Gregor88b70942009-02-25 22:02:03 +00004157 return false;
4158}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004159
Douglas Gregorbacb9492011-01-03 21:13:47 +00004160/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4161/// that checks non-type template partial specialization arguments.
4162static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4163 NonTypeTemplateParmDecl *Param,
4164 const TemplateArgument *Args,
4165 unsigned NumArgs) {
4166 for (unsigned I = 0; I != NumArgs; ++I) {
4167 if (Args[I].getKind() == TemplateArgument::Pack) {
4168 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004169 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004170 Args[I].pack_size()))
4171 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004172
Douglas Gregore94866f2009-06-12 21:21:02 +00004173 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004174 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004175
Douglas Gregorbacb9492011-01-03 21:13:47 +00004176 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004177 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004178 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004179 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004180
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004181 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004182 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4183 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004184
4185 // Strip off any implicit casts we added as part of type checking.
4186 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4187 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004188
Douglas Gregore94866f2009-06-12 21:21:02 +00004189 // C++ [temp.class.spec]p8:
4190 // A non-type argument is non-specialized if it is the name of a
4191 // non-type parameter. All other non-type arguments are
4192 // specialized.
4193 //
4194 // Below, we check the two conditions that only apply to
4195 // specialized non-type arguments, so skip any non-specialized
4196 // arguments.
4197 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004198 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004199 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004200
Douglas Gregore94866f2009-06-12 21:21:02 +00004201 // C++ [temp.class.spec]p9:
4202 // Within the argument list of a class template partial
4203 // specialization, the following restrictions apply:
4204 // -- A partially specialized non-type argument expression
4205 // shall not involve a template parameter of the partial
4206 // specialization except when the argument expression is a
4207 // simple identifier.
4208 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004209 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004210 diag::err_dependent_non_type_arg_in_partial_spec)
4211 << ArgExpr->getSourceRange();
4212 return true;
4213 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004214
Douglas Gregore94866f2009-06-12 21:21:02 +00004215 // -- The type of a template parameter corresponding to a
4216 // specialized non-type argument shall not be dependent on a
4217 // parameter of the specialization.
4218 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004219 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004220 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4221 << Param->getType()
4222 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004223 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004224 return true;
4225 }
4226 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004227
Douglas Gregorbacb9492011-01-03 21:13:47 +00004228 return false;
4229}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004230
Douglas Gregorbacb9492011-01-03 21:13:47 +00004231/// \brief Check the non-type template arguments of a class template
4232/// partial specialization according to C++ [temp.class.spec]p9.
4233///
4234/// \param TemplateParams the template parameters of the primary class
4235/// template.
4236///
4237/// \param TemplateArg the template arguments of the class template
4238/// partial specialization.
4239///
4240/// \returns true if there was an error, false otherwise.
4241static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4242 TemplateParameterList *TemplateParams,
4243 llvm::SmallVectorImpl<TemplateArgument> &TemplateArgs) {
4244 const TemplateArgument *ArgList = TemplateArgs.data();
4245
4246 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4247 NonTypeTemplateParmDecl *Param
4248 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4249 if (!Param)
4250 continue;
4251
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004252 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004253 &ArgList[I], 1))
4254 return true;
4255 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004256
4257 return false;
4258}
4259
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004260/// \brief Retrieve the previous declaration of the given declaration.
4261static NamedDecl *getPreviousDecl(NamedDecl *ND) {
4262 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
4263 return VD->getPreviousDeclaration();
4264 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
4265 return FD->getPreviousDeclaration();
4266 if (TagDecl *TD = dyn_cast<TagDecl>(ND))
4267 return TD->getPreviousDeclaration();
4268 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND))
4269 return TD->getPreviousDeclaration();
4270 if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
4271 return FTD->getPreviousDeclaration();
4272 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
4273 return CTD->getPreviousDeclaration();
4274 return 0;
4275}
4276
John McCalld226f652010-08-21 09:40:31 +00004277DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004278Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4279 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004280 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004281 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004282 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004283 SourceLocation TemplateNameLoc,
4284 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004285 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004286 SourceLocation RAngleLoc,
4287 AttributeList *Attr,
4288 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004289 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004290
Douglas Gregorcc636682009-02-17 23:15:12 +00004291 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004292 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004293 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004294 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4295
4296 if (!ClassTemplate) {
4297 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004298 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004299 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4300 return true;
4301 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004302
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004303 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004304 bool isPartialSpecialization = false;
4305
Douglas Gregor88b70942009-02-25 22:02:03 +00004306 // Check the validity of the template headers that introduce this
4307 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004308 // FIXME: We probably shouldn't complain about these headers for
4309 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004310 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004311 TemplateParameterList *TemplateParams
Mike Stump1eb44332009-09-09 15:08:12 +00004312 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc, SS,
4313 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004314 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004315 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004316 isExplicitSpecialization,
4317 Invalid);
4318 if (Invalid)
4319 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004320
Abramo Bagnara9b934882010-06-12 08:15:14 +00004321 unsigned NumMatchedTemplateParamLists = TemplateParameterLists.size();
4322 if (TemplateParams)
4323 --NumMatchedTemplateParamLists;
4324
Douglas Gregor05396e22009-08-25 17:23:04 +00004325 if (TemplateParams && TemplateParams->size() > 0) {
4326 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004327
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004328 if (TUK == TUK_Friend) {
4329 Diag(KWLoc, diag::err_partial_specialization_friend)
4330 << SourceRange(LAngleLoc, RAngleLoc);
4331 return true;
4332 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004333
Douglas Gregor05396e22009-08-25 17:23:04 +00004334 // C++ [temp.class.spec]p10:
4335 // The template parameter list of a specialization shall not
4336 // contain default template argument values.
4337 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4338 Decl *Param = TemplateParams->getParam(I);
4339 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4340 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004341 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004342 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004343 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004344 }
4345 } else if (NonTypeTemplateParmDecl *NTTP
4346 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4347 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004348 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004349 diag::err_default_arg_in_partial_spec)
4350 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004351 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004352 }
4353 } else {
4354 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004355 if (TTP->hasDefaultArgument()) {
4356 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004357 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004358 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004359 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004360 }
4361 }
4362 }
Douglas Gregora735b202009-10-13 14:39:41 +00004363 } else if (TemplateParams) {
4364 if (TUK == TUK_Friend)
4365 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004366 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004367 SourceRange(TemplateParams->getTemplateLoc(),
4368 TemplateParams->getRAngleLoc()))
4369 << SourceRange(LAngleLoc, RAngleLoc);
4370 else
4371 isExplicitSpecialization = true;
4372 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004373 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004374 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004375 isExplicitSpecialization = true;
4376 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004377
Douglas Gregorcc636682009-02-17 23:15:12 +00004378 // Check that the specialization uses the same tag kind as the
4379 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004380 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4381 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004382 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00004383 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004384 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004385 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004386 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004387 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004388 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004389 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004390 diag::note_previous_use);
4391 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4392 }
4393
Douglas Gregor40808ce2009-03-09 23:48:35 +00004394 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004395 TemplateArgumentListInfo TemplateArgs;
4396 TemplateArgs.setLAngleLoc(LAngleLoc);
4397 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004398 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004399
Douglas Gregor925910d2011-01-03 20:35:03 +00004400 // Check for unexpanded parameter packs in any of the template arguments.
4401 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004402 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004403 UPPC_PartialSpecialization))
4404 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004405
Douglas Gregorcc636682009-02-17 23:15:12 +00004406 // Check that the template argument list is well-formed for this
4407 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00004408 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004409 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4410 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004411 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004412
Douglas Gregor910f8002010-11-07 23:05:16 +00004413 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004414 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004415
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004416 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004417 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004418 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004419 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004420 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004421 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004422 return true;
4423
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004424 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004425 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004426 TemplateArgs.getArgumentArray(),
Douglas Gregorde090962010-02-09 00:37:32 +00004427 TemplateArgs.size())) {
4428 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4429 << ClassTemplate->getDeclName();
4430 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004431 }
4432 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004433
Douglas Gregorcc636682009-02-17 23:15:12 +00004434 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004435 ClassTemplateSpecializationDecl *PrevDecl = 0;
4436
4437 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004438 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004439 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004440 = ClassTemplate->findPartialSpecialization(Converted.data(),
4441 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004442 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004443 else
4444 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004445 = ClassTemplate->findSpecialization(Converted.data(),
4446 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004447
4448 ClassTemplateSpecializationDecl *Specialization = 0;
4449
Douglas Gregor88b70942009-02-25 22:02:03 +00004450 // Check whether we can declare a class template specialization in
4451 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004452 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004453 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
4454 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004455 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004456 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004457
Douglas Gregorb88e8882009-07-30 17:40:51 +00004458 // The canonical type
4459 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004460 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004461 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00004462 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004463 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004464 // arguments was referenced but not declared, or we're only
4465 // referencing this specialization as a friend, reuse that
Douglas Gregorcc636682009-02-17 23:15:12 +00004466 // declaration node as our own, updating its source location to
4467 // reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004468 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004469 Specialization->setLocation(TemplateNameLoc);
Douglas Gregorcc636682009-02-17 23:15:12 +00004470 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00004471 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004472 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00004473 // Build the canonical type that describes the converted template
4474 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00004475 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
4476 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004477 Converted.data(),
4478 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004479
4480 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00004481 ClassTemplate->getInjectedClassNameSpecialization())) {
4482 // C++ [temp.class.spec]p9b3:
4483 //
4484 // -- The argument list of the specialization shall not be identical
4485 // to the implicit argument list of the primary template.
4486 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
4487 << (TUK == TUK_Definition)
4488 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
4489 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
4490 ClassTemplate->getIdentifier(),
4491 TemplateNameLoc,
4492 Attr,
4493 TemplateParams,
4494 AS_none);
4495 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00004496
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004497 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004498 ClassTemplatePartialSpecializationDecl *PrevPartial
4499 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004500 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004501 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00004502 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00004503 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004504 ClassTemplate->getDeclContext(),
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00004505 TemplateNameLoc,
4506 TemplateParams,
4507 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004508 Converted.data(),
4509 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00004510 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00004511 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00004512 PrevPartial,
4513 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00004514 SetNestedNameSpecifier(Partial, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004515 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004516 Partial->setTemplateParameterListsInfo(Context,
4517 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004518 (TemplateParameterList**) TemplateParameterLists.release());
4519 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004520
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004521 if (!PrevPartial)
4522 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004523 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00004524
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004525 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00004526 // template specialization, make a note of that.
4527 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
4528 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004529
Douglas Gregor031a5882009-06-13 00:26:55 +00004530 // Check that all of the template parameters of the class template
4531 // partial specialization are deducible from the template
4532 // arguments. If not, this class template partial specialization
4533 // will never be used.
4534 llvm::SmallVector<bool, 8> DeducibleParams;
4535 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004536 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00004537 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00004538 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00004539 unsigned NumNonDeducible = 0;
4540 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
4541 if (!DeducibleParams[I])
4542 ++NumNonDeducible;
4543
4544 if (NumNonDeducible) {
4545 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
4546 << (NumNonDeducible > 1)
4547 << SourceRange(TemplateNameLoc, RAngleLoc);
4548 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
4549 if (!DeducibleParams[I]) {
4550 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
4551 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00004552 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004553 diag::note_partial_spec_unused_parameter)
4554 << Param->getDeclName();
4555 else
Mike Stump1eb44332009-09-09 15:08:12 +00004556 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00004557 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00004558 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00004559 }
4560 }
4561 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004562 } else {
4563 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004564 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00004565 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00004566 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00004567 ClassTemplate->getDeclContext(),
4568 TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00004569 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00004570 Converted.data(),
4571 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004572 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00004573 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor98c2e622010-07-28 23:59:57 +00004574 if (NumMatchedTemplateParamLists > 0 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00004575 Specialization->setTemplateParameterListsInfo(Context,
4576 NumMatchedTemplateParamLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00004577 (TemplateParameterList**) TemplateParameterLists.release());
4578 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004579
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004580 if (!PrevDecl)
4581 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00004582
4583 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004584 }
4585
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004586 // C++ [temp.expl.spec]p6:
4587 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004588 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004589 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004590 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004591 // use occurs; no diagnostic is required.
4592 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004593 bool Okay = false;
4594 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4595 // Is there any previous explicit specialization declaration?
4596 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
4597 Okay = true;
4598 break;
4599 }
4600 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004601
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004602 if (!Okay) {
4603 SourceRange Range(TemplateNameLoc, RAngleLoc);
4604 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
4605 << Context.getTypeDeclType(Specialization) << Range;
4606
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004607 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004608 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004609 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004610 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004611 return true;
4612 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00004613 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004614
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004615 // If this is not a friend, note that this is an explicit specialization.
4616 if (TUK != TUK_Friend)
4617 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00004618
4619 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004620 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00004621 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00004622 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00004623 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004624 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00004625 Diag(Def->getLocation(), diag::note_previous_definition);
4626 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00004627 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004628 }
4629 }
4630
John McCall7f1b9872010-12-18 03:30:47 +00004631 if (Attr)
4632 ProcessDeclAttributeList(S, Specialization, Attr);
4633
Douglas Gregorfc705b82009-02-26 22:19:44 +00004634 // Build the fully-sugared type for this class template
4635 // specialization as the user wrote in the specialization
4636 // itself. This means that we'll pretty-print the type retrieved
4637 // from the specialization's declaration the way that the user
4638 // actually wrote the specialization, rather than formatting the
4639 // name based on the "canonical" representation used to store the
4640 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00004641 TypeSourceInfo *WrittenTy
4642 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
4643 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004644 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004645 Specialization->setTypeAsWritten(WrittenTy);
Douglas Gregor7e9b57b2010-07-06 18:33:12 +00004646 if (TemplateParams)
4647 Specialization->setTemplateKeywordLoc(TemplateParams->getTemplateLoc());
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004648 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00004649 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00004650
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00004651 // C++ [temp.expl.spec]p9:
4652 // A template explicit specialization is in the scope of the
4653 // namespace in which the template was defined.
4654 //
4655 // We actually implement this paragraph where we set the semantic
4656 // context (in the creation of the ClassTemplateSpecializationDecl),
4657 // but we also maintain the lexical context where the actual
4658 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00004659 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00004660
Douglas Gregorcc636682009-02-17 23:15:12 +00004661 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00004662 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00004663 Specialization->startDefinition();
4664
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004665 if (TUK == TUK_Friend) {
4666 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
4667 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00004668 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004669 /*FIXME:*/KWLoc);
4670 Friend->setAccess(AS_public);
4671 CurContext->addDecl(Friend);
4672 } else {
4673 // Add the specialization into its lexical context, so that it can
4674 // be seen when iterating through the list of declarations in that
4675 // context. However, specializations are not found by name lookup.
4676 CurContext->addDecl(Specialization);
4677 }
John McCalld226f652010-08-21 09:40:31 +00004678 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00004679}
Douglas Gregord57959a2009-03-27 23:10:48 +00004680
John McCalld226f652010-08-21 09:40:31 +00004681Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00004682 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004683 Declarator &D) {
Douglas Gregore542c862009-06-23 23:11:28 +00004684 return HandleDeclarator(S, D, move(TemplateParameterLists), false);
4685}
4686
John McCalld226f652010-08-21 09:40:31 +00004687Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00004688 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00004689 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00004690 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00004691 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00004692
Douglas Gregor52591bf2009-06-24 00:54:41 +00004693 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00004694 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00004695 }
Mike Stump1eb44332009-09-09 15:08:12 +00004696
Douglas Gregor52591bf2009-06-24 00:54:41 +00004697 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004698
John McCalld226f652010-08-21 09:40:31 +00004699 Decl *DP = HandleDeclarator(ParentScope, D,
4700 move(TemplateParameterLists),
4701 /*IsFunctionDefinition=*/true);
Mike Stump1eb44332009-09-09 15:08:12 +00004702 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00004703 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00004704 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00004705 FunctionTemplate->getTemplatedDecl());
4706 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
4707 return ActOnStartOfFunctionDef(FnBodyScope, Function);
4708 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00004709}
4710
John McCall75042392010-02-11 01:33:53 +00004711/// \brief Strips various properties off an implicit instantiation
4712/// that has just been explicitly specialized.
4713static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00004714 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00004715
4716 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
4717 FD->setInlineSpecified(false);
4718 }
4719}
4720
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004721/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00004722/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004723/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00004724/// new specialization/instantiation will have any effect.
4725///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004726/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00004727/// instantiation.
4728///
4729/// \param NewTSK the kind of the new explicit specialization or instantiation.
4730///
4731/// \param PrevDecl the previous declaration of the entity.
4732///
4733/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
4734///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004735/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00004736/// declaration was instantiated (either implicitly or explicitly).
4737///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004738/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00004739/// specialization or instantiation has no effect and should be ignored.
4740///
4741/// \returns true if there was an error that should prevent the introduction of
4742/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00004743bool
4744Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
4745 TemplateSpecializationKind NewTSK,
4746 NamedDecl *PrevDecl,
4747 TemplateSpecializationKind PrevTSK,
4748 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004749 bool &HasNoEffect) {
4750 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004751
Douglas Gregor454885e2009-10-15 15:54:05 +00004752 switch (NewTSK) {
4753 case TSK_Undeclared:
4754 case TSK_ImplicitInstantiation:
4755 assert(false && "Don't check implicit instantiations here");
4756 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004757
Douglas Gregor454885e2009-10-15 15:54:05 +00004758 case TSK_ExplicitSpecialization:
4759 switch (PrevTSK) {
4760 case TSK_Undeclared:
4761 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004762 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00004763 // explicitly specialized or has merely been mentioned without any
4764 // instantiation.
4765 return false;
4766
4767 case TSK_ImplicitInstantiation:
4768 if (PrevPointOfInstantiation.isInvalid()) {
4769 // The declaration itself has not actually been instantiated, so it is
4770 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00004771 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00004772 return false;
4773 }
4774 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004775
Douglas Gregor454885e2009-10-15 15:54:05 +00004776 case TSK_ExplicitInstantiationDeclaration:
4777 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004778 assert((PrevTSK == TSK_ImplicitInstantiation ||
4779 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00004780 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004781
Douglas Gregor454885e2009-10-15 15:54:05 +00004782 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004783 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00004784 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004785 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00004786 // implicit instantiation to take place, in every translation unit in
4787 // which such a use occurs; no diagnostic is required.
Douglas Gregordc0a11c2010-02-26 06:03:23 +00004788 for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
4789 // Is there any previous explicit specialization declaration?
4790 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
4791 return false;
4792 }
4793
Douglas Gregor0d035142009-10-27 18:42:08 +00004794 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00004795 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004796 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00004797 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004798
Douglas Gregor454885e2009-10-15 15:54:05 +00004799 return true;
4800 }
4801 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004802
Douglas Gregor454885e2009-10-15 15:54:05 +00004803 case TSK_ExplicitInstantiationDeclaration:
4804 switch (PrevTSK) {
4805 case TSK_ExplicitInstantiationDeclaration:
4806 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004807 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004808 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004809
Douglas Gregor454885e2009-10-15 15:54:05 +00004810 case TSK_Undeclared:
4811 case TSK_ImplicitInstantiation:
4812 // We're explicitly instantiating something that may have already been
4813 // implicitly instantiated; that's fine.
4814 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004815
Douglas Gregor454885e2009-10-15 15:54:05 +00004816 case TSK_ExplicitSpecialization:
4817 // C++0x [temp.explicit]p4:
4818 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004819 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00004820 // specialization for that template, the explicit instantiation has no
4821 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004822 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004823 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004824
Douglas Gregor454885e2009-10-15 15:54:05 +00004825 case TSK_ExplicitInstantiationDefinition:
4826 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004827 // If an entity is the subject of both an explicit instantiation
4828 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00004829 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004830 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00004831 diag::err_explicit_instantiation_declaration_after_definition);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004832 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004833 diag::note_explicit_instantiation_definition_here);
Douglas Gregor454885e2009-10-15 15:54:05 +00004834 assert(PrevPointOfInstantiation.isValid() &&
4835 "Explicit instantiation without point of instantiation?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004836 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004837 return false;
4838 }
4839 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004840
Douglas Gregor454885e2009-10-15 15:54:05 +00004841 case TSK_ExplicitInstantiationDefinition:
4842 switch (PrevTSK) {
4843 case TSK_Undeclared:
4844 case TSK_ImplicitInstantiation:
4845 // We're explicitly instantiating something that may have already been
4846 // implicitly instantiated; that's fine.
4847 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004848
Douglas Gregor454885e2009-10-15 15:54:05 +00004849 case TSK_ExplicitSpecialization:
4850 // C++ DR 259, C++0x [temp.explicit]p4:
4851 // For a given set of template parameters, if an explicit
4852 // instantiation of a template appears after a declaration of
4853 // an explicit specialization for that template, the explicit
4854 // instantiation has no effect.
4855 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004856 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00004857 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00004858 // has been explicitly specialized.
Douglas Gregor0d035142009-10-27 18:42:08 +00004859 if (!getLangOptions().CPlusPlus0x) {
4860 Diag(NewLoc, diag::ext_explicit_instantiation_after_specialization)
Douglas Gregor454885e2009-10-15 15:54:05 +00004861 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00004862 Diag(PrevDecl->getLocation(),
Douglas Gregor454885e2009-10-15 15:54:05 +00004863 diag::note_previous_template_specialization);
4864 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004865 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00004866 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004867
Douglas Gregor454885e2009-10-15 15:54:05 +00004868 case TSK_ExplicitInstantiationDeclaration:
4869 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004870 // were previously asked to suppress instantiations. That's fine.
Douglas Gregor454885e2009-10-15 15:54:05 +00004871 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004872
Douglas Gregor454885e2009-10-15 15:54:05 +00004873 case TSK_ExplicitInstantiationDefinition:
4874 // C++0x [temp.spec]p5:
4875 // For a given template and a given set of template-arguments,
4876 // - an explicit instantiation definition shall appear at most once
4877 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00004878 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00004879 << PrevDecl;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004880 Diag(PrevPointOfInstantiation,
Douglas Gregor0d035142009-10-27 18:42:08 +00004881 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00004882 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004883 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00004884 }
4885 break;
4886 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004887
Douglas Gregor454885e2009-10-15 15:54:05 +00004888 assert(false && "Missing specialization/instantiation case?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004889
Douglas Gregor454885e2009-10-15 15:54:05 +00004890 return false;
4891}
4892
John McCallaf2094e2010-04-08 09:05:18 +00004893/// \brief Perform semantic analysis for the given dependent function
4894/// template specialization. The only possible way to get a dependent
4895/// function template specialization is with a friend declaration,
4896/// like so:
4897///
4898/// template <class T> void foo(T);
4899/// template <class T> class A {
4900/// friend void foo<>(T);
4901/// };
4902///
4903/// There really isn't any useful analysis we can do here, so we
4904/// just store the information.
4905bool
4906Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
4907 const TemplateArgumentListInfo &ExplicitTemplateArgs,
4908 LookupResult &Previous) {
4909 // Remove anything from Previous that isn't a function template in
4910 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004911 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00004912 LookupResult::Filter F = Previous.makeFilter();
4913 while (F.hasNext()) {
4914 NamedDecl *D = F.next()->getUnderlyingDecl();
4915 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00004916 !FDLookupContext->InEnclosingNamespaceSetOf(
4917 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00004918 F.erase();
4919 }
4920 F.done();
4921
4922 // Should this be diagnosed here?
4923 if (Previous.empty()) return true;
4924
4925 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
4926 ExplicitTemplateArgs);
4927 return false;
4928}
4929
Abramo Bagnarae03db982010-05-20 15:32:11 +00004930/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004931/// specialization.
4932///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004933/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004934/// explicit function template specialization. On successful completion,
4935/// the function declaration \p FD will become a function template
4936/// specialization.
4937///
4938/// \param FD the function declaration, which will be updated to become a
4939/// function template specialization.
4940///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004941/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
4942/// if any. Note that this may be valid info even when 0 arguments are
4943/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
4944/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004945///
Abramo Bagnarae03db982010-05-20 15:32:11 +00004946/// \param PrevDecl the set of declarations that may be specialized by
4947/// this function specialization.
4948bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004949Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
John McCalld5532b62009-11-23 01:53:49 +00004950 const TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00004951 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004952 // The set of function template specializations that could match this
4953 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00004954 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004955
Sebastian Redl7a126a42010-08-31 00:36:30 +00004956 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00004957 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
4958 I != E; ++I) {
4959 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
4960 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004961 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004962 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004963 if (!FDLookupContext->InEnclosingNamespaceSetOf(
4964 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004965 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004966
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004967 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004968 // A trailing template-argument can be left unspecified in the
4969 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004970 // provided it can be deduced from the function argument type.
4971 // Perform template argument deduction to determine whether we may be
4972 // specializing this template.
4973 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00004974 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004975 FunctionDecl *Specialization = 0;
4976 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00004977 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004978 FD->getType(),
4979 Specialization,
4980 Info)) {
4981 // FIXME: Template argument deduction failed; record why it failed, so
4982 // that we can provide nifty diagnostics.
4983 (void)TDK;
4984 continue;
4985 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004986
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004987 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00004988 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00004989 }
4990 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004991
Douglas Gregorc5df30f2009-09-26 03:41:46 +00004992 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00004993 UnresolvedSetIterator Result
4994 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00004995 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004996 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00004997 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00004998 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00004999 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005000 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005001 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005002 return true;
John McCallc373d482010-01-27 01:50:18 +00005003
5004 // Ignore access information; it doesn't figure into redeclaration checking.
5005 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Douglas Gregorc42b6522010-04-09 21:02:29 +00005006 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005007
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005008 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005009 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005010
5011 // If this is a friend declaration, then we're not really declaring
5012 // an explicit specialization.
5013 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005014
Douglas Gregord5cb8762009-10-07 00:13:32 +00005015 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005016 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005017 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005018 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005019 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005020 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005021 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005022
5023 // C++ [temp.expl.spec]p6:
5024 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005025 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005026 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005027 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005028 // use occurs; no diagnostic is required.
5029 FunctionTemplateSpecializationInfo *SpecInfo
5030 = Specialization->getTemplateSpecializationInfo();
5031 assert(SpecInfo && "Function template specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005032
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005033 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005034 if (!isFriend &&
5035 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005036 TSK_ExplicitSpecialization,
5037 Specialization,
5038 SpecInfo->getTemplateSpecializationKind(),
5039 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005040 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005041 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005042
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005043 // Mark the prior declaration as an explicit specialization, so that later
5044 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005045 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005046 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005047 MarkUnusedFileScopedDecl(Specialization);
5048 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005049
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005050 // Turn the given function declaration into a function template
5051 // specialization, with the template arguments from the previous
5052 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005053 // Take copies of (semantic and syntactic) template argument lists.
5054 const TemplateArgumentList* TemplArgs = new (Context)
5055 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
5056 const TemplateArgumentListInfo* TemplArgsAsWritten = ExplicitTemplateArgs
5057 ? new (Context) TemplateArgumentListInfo(*ExplicitTemplateArgs) : 0;
Douglas Gregor838db382010-02-11 01:19:42 +00005058 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005059 TemplArgs, /*InsertPos=*/0,
5060 SpecInfo->getTemplateSpecializationKind(),
5061 TemplArgsAsWritten);
5062
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005063 // The "previous declaration" for this function template specialization is
5064 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005065 Previous.clear();
5066 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005067 return false;
5068}
5069
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005070/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005071/// specialization.
5072///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005073/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005074/// explicit member function specialization. On successful completion,
5075/// the function declaration \p FD will become a member function
5076/// specialization.
5077///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005078/// \param Member the member declaration, which will be updated to become a
5079/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005080///
John McCall68263142009-11-18 22:49:29 +00005081/// \param Previous the set of declarations, one of which may be specialized
5082/// by this function specialization; the set will be modified to contain the
5083/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005084bool
John McCall68263142009-11-18 22:49:29 +00005085Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005086 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005087
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005088 // Try to find the member we are instantiating.
5089 NamedDecl *Instantiation = 0;
5090 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005091 MemberSpecializationInfo *MSInfo = 0;
5092
John McCall68263142009-11-18 22:49:29 +00005093 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005094 // Nowhere to look anyway.
5095 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005096 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5097 I != E; ++I) {
5098 NamedDecl *D = (*I)->getUnderlyingDecl();
5099 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005100 if (Context.hasSameType(Function->getType(), Method->getType())) {
5101 Instantiation = Method;
5102 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005103 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005104 break;
5105 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005106 }
5107 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005108 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005109 VarDecl *PrevVar;
5110 if (Previous.isSingleResult() &&
5111 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005112 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005113 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005114 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005115 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005116 }
5117 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005118 CXXRecordDecl *PrevRecord;
5119 if (Previous.isSingleResult() &&
5120 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5121 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005122 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005123 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005124 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005125 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005126
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005127 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005128 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005129 // specializations are always out-of-line, the caller will complain about
5130 // this mismatch later.
5131 return false;
5132 }
John McCall77e8b112010-04-13 20:37:33 +00005133
5134 // If this is a friend, just bail out here before we start turning
5135 // things into explicit specializations.
5136 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5137 // Preserve instantiation information.
5138 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5139 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5140 cast<CXXMethodDecl>(InstantiatedFrom),
5141 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5142 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5143 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5144 cast<CXXRecordDecl>(InstantiatedFrom),
5145 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5146 }
5147
5148 Previous.clear();
5149 Previous.addDecl(Instantiation);
5150 return false;
5151 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005152
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005153 // Make sure that this is a specialization of a member.
5154 if (!InstantiatedFrom) {
5155 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5156 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005157 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5158 return true;
5159 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005160
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005161 // C++ [temp.expl.spec]p6:
5162 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005163 // explicitly specialized then that spe- cialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005164 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005165 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005166 // use occurs; no diagnostic is required.
5167 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005168
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005169 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005170 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5171 TSK_ExplicitSpecialization,
5172 Instantiation,
5173 MSInfo->getTemplateSpecializationKind(),
5174 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005175 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005176 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005177
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005178 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005179 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005180 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005181 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005182 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005183 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005184
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005185 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005186 // the original declaration to note that it is an explicit specialization
5187 // (if it was previously an implicit instantiation). This latter step
5188 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005189 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005190 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5191 if (InstantiationFunction->getTemplateSpecializationKind() ==
5192 TSK_ImplicitInstantiation) {
5193 InstantiationFunction->setTemplateSpecializationKind(
5194 TSK_ExplicitSpecialization);
5195 InstantiationFunction->setLocation(Member->getLocation());
5196 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005197
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005198 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5199 cast<CXXMethodDecl>(InstantiatedFrom),
5200 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005201 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005202 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005203 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5204 if (InstantiationVar->getTemplateSpecializationKind() ==
5205 TSK_ImplicitInstantiation) {
5206 InstantiationVar->setTemplateSpecializationKind(
5207 TSK_ExplicitSpecialization);
5208 InstantiationVar->setLocation(Member->getLocation());
5209 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005210
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005211 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5212 cast<VarDecl>(InstantiatedFrom),
5213 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005214 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005215 } else {
5216 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005217 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5218 if (InstantiationClass->getTemplateSpecializationKind() ==
5219 TSK_ImplicitInstantiation) {
5220 InstantiationClass->setTemplateSpecializationKind(
5221 TSK_ExplicitSpecialization);
5222 InstantiationClass->setLocation(Member->getLocation());
5223 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005224
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005225 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005226 cast<CXXRecordDecl>(InstantiatedFrom),
5227 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005228 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005229
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005230 // Save the caller the trouble of having to figure out which declaration
5231 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005232 Previous.clear();
5233 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005234 return false;
5235}
5236
Douglas Gregor558c0322009-10-14 23:41:34 +00005237/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005238///
5239/// \returns true if a serious error occurs, false otherwise.
5240static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005241 SourceLocation InstLoc,
5242 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005243 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5244 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005245
Douglas Gregor669eed82010-07-13 00:10:04 +00005246 if (CurContext->isRecord()) {
5247 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5248 << D;
5249 return true;
5250 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005251
Douglas Gregor558c0322009-10-14 23:41:34 +00005252 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005253 // An explicit instantiation shall appear in an enclosing namespace of its
Douglas Gregor558c0322009-10-14 23:41:34 +00005254 // template.
5255 //
5256 // This is DR275, which we do not retroactively apply to C++98/03.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005257 if (S.getLangOptions().CPlusPlus0x &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00005258 !CurContext->Encloses(OrigContext)) {
5259 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005260 S.Diag(InstLoc,
5261 S.getLangOptions().CPlusPlus0x?
Douglas Gregor2166beb2010-05-11 17:39:34 +00005262 diag::err_explicit_instantiation_out_of_scope
5263 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005264 << D << NS;
5265 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005266 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005267 S.getLangOptions().CPlusPlus0x?
5268 diag::err_explicit_instantiation_must_be_global
5269 : diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005270 << D;
5271 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005272 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005273 }
Sebastian Redl7a126a42010-08-31 00:36:30 +00005274
Douglas Gregor558c0322009-10-14 23:41:34 +00005275 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005276 // If the name declared in the explicit instantiation is an unqualified
5277 // name, the explicit instantiation shall appear in the namespace where
Douglas Gregor558c0322009-10-14 23:41:34 +00005278 // its template is declared or, if that namespace is inline (7.3.1), any
5279 // namespace from its enclosing namespace set.
5280 if (WasQualifiedName)
Douglas Gregor669eed82010-07-13 00:10:04 +00005281 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005282
5283 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
Douglas Gregor669eed82010-07-13 00:10:04 +00005284 return false;
Sebastian Redl7a126a42010-08-31 00:36:30 +00005285
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005286 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005287 S.getLangOptions().CPlusPlus0x?
5288 diag::err_explicit_instantiation_unqualified_wrong_namespace
5289 : diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
Sebastian Redl7a126a42010-08-31 00:36:30 +00005290 << D << OrigContext;
Douglas Gregor558c0322009-10-14 23:41:34 +00005291 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005292 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005293}
5294
5295/// \brief Determine whether the given scope specifier has a template-id in it.
5296static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5297 if (!SS.isSet())
5298 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005299
Douglas Gregor558c0322009-10-14 23:41:34 +00005300 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005301 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005302 // or a static data member of a class template specialization, the name of
5303 // the class template specialization in the qualified-id for the member
5304 // name shall be a simple-template-id.
5305 //
5306 // C++98 has the same restriction, just worded differently.
5307 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5308 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005309 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005310 if (isa<TemplateSpecializationType>(T))
5311 return true;
5312
5313 return false;
5314}
5315
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005316// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005317DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005318Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005319 SourceLocation ExternLoc,
5320 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005321 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005322 SourceLocation KWLoc,
5323 const CXXScopeSpec &SS,
5324 TemplateTy TemplateD,
5325 SourceLocation TemplateNameLoc,
5326 SourceLocation LAngleLoc,
5327 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005328 SourceLocation RAngleLoc,
5329 AttributeList *Attr) {
5330 // Find the class template we're specializing
5331 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005332 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005333 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5334
5335 // Check that the specialization uses the same tag kind as the
5336 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005337 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5338 assert(Kind != TTK_Enum &&
5339 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005340 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Mike Stump1eb44332009-09-09 15:08:12 +00005341 Kind, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005342 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005343 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005344 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005345 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005346 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005347 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005348 diag::note_previous_use);
5349 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5350 }
5351
Douglas Gregor558c0322009-10-14 23:41:34 +00005352 // C++0x [temp.explicit]p2:
5353 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005354 // definition and an explicit instantiation declaration. An explicit
5355 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005356 TemplateSpecializationKind TSK
5357 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5358 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005359
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005360 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005361 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005362 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005363
5364 // Check that the template argument list is well-formed for this
5365 // template.
Douglas Gregor910f8002010-11-07 23:05:16 +00005366 llvm::SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005367 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5368 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005369 return true;
5370
Douglas Gregor910f8002010-11-07 23:05:16 +00005371 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005372 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005373
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005374 // Find the class template specialization declaration that
5375 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005376 void *InsertPos = 0;
5377 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005378 = ClassTemplate->findSpecialization(Converted.data(),
5379 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005380
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005381 TemplateSpecializationKind PrevDecl_TSK
5382 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5383
Douglas Gregord5cb8762009-10-07 00:13:32 +00005384 // C++0x [temp.explicit]p2:
5385 // [...] An explicit instantiation shall appear in an enclosing
5386 // namespace of its template. [...]
5387 //
5388 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005389 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5390 SS.isSet()))
5391 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005392
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005393 ClassTemplateSpecializationDecl *Specialization = 0;
5394
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005395 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005396 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005397 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005398 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005399 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005400 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005401 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005402
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005403 // Even though HasNoEffect == true means that this explicit instantiation
5404 // has no effect on semantics, we go on to put its syntax in the AST.
5405
5406 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5407 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005408 // Since the only prior class template specialization with these
5409 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005410 // declaration node as our own, updating the source location
5411 // for the template name to reflect our new declaration.
5412 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00005413 Specialization = PrevDecl;
5414 Specialization->setLocation(TemplateNameLoc);
5415 PrevDecl = 0;
5416 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005417 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005418
Douglas Gregor52604ab2009-09-11 21:19:12 +00005419 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005420 // Create a new class template specialization declaration node for
5421 // this explicit specialization.
5422 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005423 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005424 ClassTemplate->getDeclContext(),
5425 TemplateNameLoc,
5426 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005427 Converted.data(),
5428 Converted.size(),
5429 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005430 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005431
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005432 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005433 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005434 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005435 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005436 }
5437
5438 // Build the fully-sugared type for this explicit instantiation as
5439 // the user wrote in the explicit instantiation itself. This means
5440 // that we'll pretty-print the type retrieved from the
5441 // specialization's declaration the way that the user actually wrote
5442 // the explicit instantiation, rather than formatting the name based
5443 // on the "canonical" representation used to store the template
5444 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005445 TypeSourceInfo *WrittenTy
5446 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5447 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005448 Context.getTypeDeclType(Specialization));
5449 Specialization->setTypeAsWritten(WrittenTy);
5450 TemplateArgsIn.release();
5451
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005452 // Set source locations for keywords.
5453 Specialization->setExternLoc(ExternLoc);
5454 Specialization->setTemplateKeywordLoc(TemplateLoc);
5455
5456 // Add the explicit instantiation into its lexical context. However,
5457 // since explicit instantiations are never found by name lookup, we
5458 // just put it into the declaration context directly.
5459 Specialization->setLexicalDeclContext(CurContext);
5460 CurContext->addDecl(Specialization);
5461
5462 // Syntax is now OK, so return if it has no other effect on semantics.
5463 if (HasNoEffect) {
5464 // Set the template specialization kind.
5465 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005466 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00005467 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005468
5469 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005470 // A definition of a class template or class member template
5471 // shall be in scope at the point of the explicit instantiation of
5472 // the class template or class member template.
5473 //
5474 // This check comes when we actually try to perform the
5475 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005476 ClassTemplateSpecializationDecl *Def
5477 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005478 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005479 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00005480 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005481 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005482 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005483 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
5484 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005485
Douglas Gregor0d035142009-10-27 18:42:08 +00005486 // Instantiate the members of this class template specialization.
5487 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00005488 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005489 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00005490 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
5491
5492 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
5493 // TSK_ExplicitInstantiationDefinition
5494 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
5495 TSK == TSK_ExplicitInstantiationDefinition)
5496 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005497
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005498 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00005499 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005500
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005501 // Set the template specialization kind.
5502 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00005503 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005504}
5505
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005506// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00005507DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005508Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005509 SourceLocation ExternLoc,
5510 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005511 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005512 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00005513 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005514 IdentifierInfo *Name,
5515 SourceLocation NameLoc,
5516 AttributeList *Attr) {
5517
Douglas Gregor402abb52009-05-28 23:31:59 +00005518 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00005519 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00005520 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00005521 KWLoc, SS, Name, NameLoc, Attr, AS_none,
5522 MultiTemplateParamsArg(*this, 0, 0),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00005523 Owned, IsDependent, false, false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00005524 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00005525 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
5526
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005527 if (!TagD)
5528 return true;
5529
John McCalld226f652010-08-21 09:40:31 +00005530 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005531 if (Tag->isEnum()) {
5532 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
5533 << Context.getTypeDeclType(Tag);
5534 return true;
5535 }
5536
Douglas Gregord0c87372009-05-27 17:30:49 +00005537 if (Tag->isInvalidDecl())
5538 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005539
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005540 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
5541 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
5542 if (!Pattern) {
5543 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
5544 << Context.getTypeDeclType(Record);
5545 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
5546 return true;
5547 }
5548
Douglas Gregor558c0322009-10-14 23:41:34 +00005549 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005550 // If the explicit instantiation is for a class or member class, the
5551 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00005552 // simple-template-id.
5553 //
5554 // C++98 has the same restriction, just worded differently.
5555 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00005556 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005557 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005558
Douglas Gregor558c0322009-10-14 23:41:34 +00005559 // C++0x [temp.explicit]p2:
5560 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005561 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00005562 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00005563 TemplateSpecializationKind TSK
5564 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5565 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005566
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005567 // C++0x [temp.explicit]p2:
5568 // [...] An explicit instantiation shall appear in an enclosing
5569 // namespace of its template. [...]
5570 //
5571 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00005572 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005573
Douglas Gregor454885e2009-10-15 15:54:05 +00005574 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005575 CXXRecordDecl *PrevDecl
Douglas Gregor583f33b2009-10-15 18:07:02 +00005576 = cast_or_null<CXXRecordDecl>(Record->getPreviousDeclaration());
Douglas Gregor952b0172010-02-11 01:04:33 +00005577 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00005578 PrevDecl = Record;
5579 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00005580 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005581 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005582 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005583 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00005584 PrevDecl,
5585 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005586 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005587 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005588 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005589 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00005590 return TagD;
5591 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005592
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005593 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00005594 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005595 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005596 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005597 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005598 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005599 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00005600 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005601 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00005602 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
5603 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00005604 Diag(Pattern->getLocation(), diag::note_forward_declaration)
5605 << Pattern;
5606 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00005607 } else {
5608 if (InstantiateClass(NameLoc, Record, Def,
5609 getTemplateInstantiationArgs(Record),
5610 TSK))
5611 return true;
5612
Douglas Gregor952b0172010-02-11 01:04:33 +00005613 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00005614 if (!RecordDef)
5615 return true;
5616 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005617 }
5618
Douglas Gregor0d035142009-10-27 18:42:08 +00005619 // Instantiate all of the members of the class.
5620 InstantiateClassMembers(NameLoc, RecordDef,
5621 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005622
Douglas Gregor6fb745b2010-05-13 16:44:06 +00005623 if (TSK == TSK_ExplicitInstantiationDefinition)
5624 MarkVTableUsed(NameLoc, RecordDef, true);
5625
Mike Stump390b4cc2009-05-16 07:39:55 +00005626 // FIXME: We don't have any representation for explicit instantiations of
5627 // member classes. Such a representation is not needed for compilation, but it
5628 // should be available for clients that want to see all of the declarations in
5629 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005630 return TagD;
5631}
5632
John McCallf312b1e2010-08-26 23:41:50 +00005633DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
5634 SourceLocation ExternLoc,
5635 SourceLocation TemplateLoc,
5636 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00005637 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00005638 // TODO: check if/when DNInfo should replace Name.
5639 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
5640 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005641 if (!Name) {
5642 if (!D.isInvalidType())
5643 Diag(D.getDeclSpec().getSourceRange().getBegin(),
5644 diag::err_explicit_instantiation_requires_name)
5645 << D.getDeclSpec().getSourceRange()
5646 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005647
Douglas Gregord5a423b2009-09-25 18:43:00 +00005648 return true;
5649 }
5650
5651 // The scope passed in may not be a decl scope. Zip up the scope tree until
5652 // we find one that is.
5653 while ((S->getFlags() & Scope::DeclScope) == 0 ||
5654 (S->getFlags() & Scope::TemplateParamScope) != 0)
5655 S = S->getParent();
5656
5657 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00005658 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
5659 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005660 if (R.isNull())
5661 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005662
Douglas Gregord5a423b2009-09-25 18:43:00 +00005663 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
5664 // Cannot explicitly instantiate a typedef.
5665 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
5666 << Name;
5667 return true;
5668 }
5669
Douglas Gregor663b5a02009-10-14 20:14:33 +00005670 // C++0x [temp.explicit]p1:
5671 // [...] An explicit instantiation of a function template shall not use the
5672 // inline or constexpr specifiers.
5673 // Presumably, this also applies to member functions of class templates as
5674 // well.
5675 if (D.getDeclSpec().isInlineSpecified() && getLangOptions().CPlusPlus0x)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005676 Diag(D.getDeclSpec().getInlineSpecLoc(),
Douglas Gregor663b5a02009-10-14 20:14:33 +00005677 diag::err_explicit_instantiation_inline)
Douglas Gregor849b2432010-03-31 17:46:05 +00005678 <<FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005679
Douglas Gregor663b5a02009-10-14 20:14:33 +00005680 // FIXME: check for constexpr specifier.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005681
Douglas Gregor558c0322009-10-14 23:41:34 +00005682 // C++0x [temp.explicit]p2:
5683 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005684 // definition and an explicit instantiation declaration. An explicit
5685 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00005686 TemplateSpecializationKind TSK
5687 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5688 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005689
Abramo Bagnara25777432010-08-11 22:01:17 +00005690 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00005691 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005692
5693 if (!R->isFunctionType()) {
5694 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005695 // A [...] static data member of a class template can be explicitly
5696 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005697 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00005698 if (Previous.isAmbiguous())
5699 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005700
John McCall1bcee0a2009-12-02 08:25:40 +00005701 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00005702 if (!Prev || !Prev->isStaticDataMember()) {
5703 // We expect to see a data data member here.
5704 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
5705 << Name;
5706 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5707 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00005708 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00005709 return true;
5710 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005711
Douglas Gregord5a423b2009-09-25 18:43:00 +00005712 if (!Prev->getInstantiatedFromStaticDataMember()) {
5713 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005714 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005715 diag::err_explicit_instantiation_data_member_not_instantiated)
5716 << Prev;
5717 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
5718 // FIXME: Can we provide a note showing where this was declared?
5719 return true;
5720 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005721
Douglas Gregor558c0322009-10-14 23:41:34 +00005722 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005723 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005724 // or a static data member of a class template specialization, the name of
5725 // the class template specialization in the qualified-id for the member
5726 // name shall be a simple-template-id.
5727 //
5728 // C++98 has the same restriction, just worded differently.
5729 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005730 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005731 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005732 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005733
Douglas Gregor558c0322009-10-14 23:41:34 +00005734 // Check the scope of this explicit instantiation.
5735 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005736
Douglas Gregor454885e2009-10-15 15:54:05 +00005737 // Verify that it is okay to explicitly instantiate here.
5738 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
5739 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005740 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005741 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00005742 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005743 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005744 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00005745 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005746 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005747 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005748
Douglas Gregord5a423b2009-09-25 18:43:00 +00005749 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005750 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005751 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005752 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005753
Douglas Gregord5a423b2009-09-25 18:43:00 +00005754 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00005755 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005756 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005757
5758 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00005759 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00005760 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00005761 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005762 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
5763 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00005764 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
5765 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00005766 ASTTemplateArgsPtr TemplateArgsPtr(*this,
5767 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00005768 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00005769 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00005770 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00005771 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00005772 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005773
Douglas Gregord5a423b2009-09-25 18:43:00 +00005774 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005775 // A [...] function [...] can be explicitly instantiated from its template.
5776 // A member function [...] of a class template can be explicitly
5777 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00005778 // template.
John McCallc373d482010-01-27 01:50:18 +00005779 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005780 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
5781 P != PEnd; ++P) {
5782 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00005783 if (!HasExplicitTemplateArgs) {
5784 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
5785 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
5786 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00005787
John McCallc373d482010-01-27 01:50:18 +00005788 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00005789 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
5790 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00005791 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00005792 }
5793 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005794
Douglas Gregord5a423b2009-09-25 18:43:00 +00005795 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
5796 if (!FunTmpl)
5797 continue;
5798
John McCall5769d612010-02-08 23:07:23 +00005799 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005800 FunctionDecl *Specialization = 0;
5801 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005802 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00005803 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005804 R, Specialization, Info)) {
5805 // FIXME: Keep track of almost-matches?
5806 (void)TDK;
5807 continue;
5808 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005809
John McCallc373d482010-01-27 01:50:18 +00005810 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00005811 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005812
Douglas Gregord5a423b2009-09-25 18:43:00 +00005813 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005814 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005815 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005816 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005817 PDiag(diag::err_explicit_instantiation_not_known) << Name,
5818 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
5819 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00005820
John McCallc373d482010-01-27 01:50:18 +00005821 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00005822 return true;
John McCallc373d482010-01-27 01:50:18 +00005823
5824 // Ignore access control bits, we don't need them for redeclaration checking.
5825 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005826
Douglas Gregor0a897e32009-10-15 17:21:20 +00005827 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005828 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00005829 diag::err_explicit_instantiation_member_function_not_instantiated)
5830 << Specialization
5831 << (Specialization->getTemplateSpecializationKind() ==
5832 TSK_ExplicitSpecialization);
5833 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
5834 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005835 }
5836
Douglas Gregor0a897e32009-10-15 17:21:20 +00005837 FunctionDecl *PrevDecl = Specialization->getPreviousDeclaration();
Douglas Gregor583f33b2009-10-15 18:07:02 +00005838 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
5839 PrevDecl = Specialization;
5840
Douglas Gregor0a897e32009-10-15 17:21:20 +00005841 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005842 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00005843 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005844 PrevDecl,
5845 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00005846 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005847 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00005848 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005849
Douglas Gregor0a897e32009-10-15 17:21:20 +00005850 // FIXME: We may still want to build some representation of this
5851 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005852 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00005853 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00005854 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00005855
5856 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005857
Douglas Gregor0a897e32009-10-15 17:21:20 +00005858 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00005859 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005860
Douglas Gregor558c0322009-10-14 23:41:34 +00005861 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005862 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005863 // or a static data member of a class template specialization, the name of
5864 // the class template specialization in the qualified-id for the member
5865 // name shall be a simple-template-id.
5866 //
5867 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00005868 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00005869 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005870 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00005871 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005872 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00005873 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00005874 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005875
Douglas Gregor558c0322009-10-14 23:41:34 +00005876 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005877 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00005878 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005879 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00005880 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005881
Douglas Gregord5a423b2009-09-25 18:43:00 +00005882 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00005883 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00005884}
5885
John McCallf312b1e2010-08-26 23:41:50 +00005886TypeResult
John McCallc4e70192009-09-11 04:59:25 +00005887Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
5888 const CXXScopeSpec &SS, IdentifierInfo *Name,
5889 SourceLocation TagLoc, SourceLocation NameLoc) {
5890 // This has to hold, because SS is expected to be defined.
5891 assert(Name && "Expected a name in a dependent tag");
5892
5893 NestedNameSpecifier *NNS
5894 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
5895 if (!NNS)
5896 return true;
5897
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005898 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00005899
Douglas Gregor48c89f42010-04-24 16:38:41 +00005900 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
5901 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005902 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00005903 return true;
5904 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005905
5906 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
John McCallb3d87482010-08-24 05:47:05 +00005907 return ParsedType::make(Context.getDependentNameType(Kwd, NNS, Name));
John McCallc4e70192009-09-11 04:59:25 +00005908}
5909
John McCallf312b1e2010-08-26 23:41:50 +00005910TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005911Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
5912 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005913 SourceLocation IdLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00005914 NestedNameSpecifier *NNS
Douglas Gregord57959a2009-03-27 23:10:48 +00005915 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
5916 if (!NNS)
5917 return true;
5918
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005919 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
5920 !getLangOptions().CPlusPlus0x)
5921 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005922 << FixItHint::CreateRemoval(TypenameLoc);
5923
Douglas Gregor107de902010-04-24 15:35:55 +00005924 QualType T = CheckTypenameType(ETK_Typename, NNS, II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00005925 TypenameLoc, SS.getRange(), IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00005926 if (T.isNull())
5927 return true;
John McCall63b43852010-04-29 23:50:39 +00005928
5929 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
5930 if (isa<DependentNameType>(T)) {
5931 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00005932 TL.setKeywordLoc(TypenameLoc);
5933 TL.setQualifierRange(SS.getRange());
5934 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00005935 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005936 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00005937 TL.setKeywordLoc(TypenameLoc);
5938 TL.setQualifierRange(SS.getRange());
5939 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00005940 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005941
John McCallb3d87482010-08-24 05:47:05 +00005942 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00005943}
5944
John McCallf312b1e2010-08-26 23:41:50 +00005945TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00005946Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
5947 const CXXScopeSpec &SS,
5948 SourceLocation TemplateLoc,
5949 TemplateTy TemplateIn,
5950 SourceLocation TemplateNameLoc,
5951 SourceLocation LAngleLoc,
5952 ASTTemplateArgsPtr TemplateArgsIn,
5953 SourceLocation RAngleLoc) {
Douglas Gregor1a15dae2010-06-16 22:31:08 +00005954 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent() &&
5955 !getLangOptions().CPlusPlus0x)
5956 Diag(TypenameLoc, diag::ext_typename_outside_of_template)
Douglas Gregora02411e2011-02-27 22:46:49 +00005957 << FixItHint::CreateRemoval(TypenameLoc);
5958
5959 // Translate the parser's template argument list in our AST format.
5960 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
5961 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
5962
5963 TemplateName Template = TemplateIn.get();
5964
Douglas Gregor6946baf2009-09-02 13:05:45 +00005965 if (computeDeclContext(SS, false)) {
5966 // If we can compute a declaration context, then the "typename"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005967 // keyword was superfluous. Just build an ElaboratedType to keep
Douglas Gregor6946baf2009-09-02 13:05:45 +00005968 // track of the nested-name-specifier.
Douglas Gregora02411e2011-02-27 22:46:49 +00005969
5970 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
5971 if (T.isNull())
5972 return true;
5973
5974 // Provide source-location information for the template specialization
5975 // type.
John McCall4e449832010-05-28 23:32:21 +00005976 TypeLocBuilder Builder;
Douglas Gregora02411e2011-02-27 22:46:49 +00005977 TemplateSpecializationTypeLoc SpecTL
5978 = Builder.push<TemplateSpecializationTypeLoc>(T);
5979
5980 // FIXME: No place to set the location of the 'template' keyword!
5981 SpecTL.setLAngleLoc(LAngleLoc);
5982 SpecTL.setRAngleLoc(RAngleLoc);
5983 SpecTL.setTemplateNameLoc(TemplateNameLoc);
5984 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
5985 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
5986
5987 // FIXME: This is a hack. We really want to push the nested-name-specifier
5988 // into TemplateSpecializationType.
5989
Abramo Bagnara22f638a2010-08-10 13:46:45 +00005990 /* Note: NNS already embedded in template specialization type T. */
5991 T = Context.getElaboratedType(ETK_Typename, /*NNS=*/0, T);
John McCall4e449832010-05-28 23:32:21 +00005992 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
5993 TL.setKeywordLoc(TypenameLoc);
5994 TL.setQualifierRange(SS.getRange());
Douglas Gregora02411e2011-02-27 22:46:49 +00005995
John McCall4e449832010-05-28 23:32:21 +00005996 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
John McCallb3d87482010-08-24 05:47:05 +00005997 return CreateParsedType(T, TSI);
Douglas Gregor6946baf2009-09-02 13:05:45 +00005998 }
Douglas Gregora02411e2011-02-27 22:46:49 +00005999
6000 // Construct a dependent template specialization type.
6001 DependentTemplateName *DTN = Template.getAsDependentTemplateName();
John McCall33500952010-06-11 00:33:02 +00006002 assert(DTN && "dependent template has non-dependent name?");
Abramo Bagnara22f638a2010-08-10 13:46:45 +00006003 assert(DTN->getQualifier()
6004 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
Douglas Gregora02411e2011-02-27 22:46:49 +00006005 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6006 DTN->getQualifier(),
6007 DTN->getIdentifier(),
6008 TemplateArgs);
6009
6010 // Create source-location information for this type.
6011 TypeLocBuilder Builder;
6012 DependentTemplateSpecializationTypeLoc SpecTL
6013 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
6014 SpecTL.setLAngleLoc(LAngleLoc);
6015 SpecTL.setRAngleLoc(RAngleLoc);
6016 SpecTL.setKeywordLoc(TypenameLoc);
6017 SpecTL.setNameLoc(TemplateNameLoc);
6018 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6019 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6020
6021 // FIXME: Nested-name-specifier source locations.
6022 SpecTL.setQualifierRange(SS.getRange());
6023 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor17343172009-04-01 00:28:59 +00006024}
6025
Douglas Gregora02411e2011-02-27 22:46:49 +00006026
Douglas Gregord57959a2009-03-27 23:10:48 +00006027/// \brief Build the type that describes a C++ typename specifier,
6028/// e.g., "typename T::type".
6029QualType
Douglas Gregor107de902010-04-24 15:35:55 +00006030Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6031 NestedNameSpecifier *NNS, const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006032 SourceLocation KeywordLoc, SourceRange NNSRange,
6033 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006034 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00006035 SS.MakeTrivial(Context, NNS, NNSRange);
Douglas Gregord57959a2009-03-27 23:10:48 +00006036
John McCall77bb1aa2010-05-01 00:40:08 +00006037 DeclContext *Ctx = computeDeclContext(SS);
6038 if (!Ctx) {
6039 // If the nested-name-specifier is dependent and couldn't be
6040 // resolved to a type, build a typename type.
6041 assert(NNS->isDependent());
6042 return Context.getDependentNameType(Keyword, NNS, &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006043 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006044
John McCall77bb1aa2010-05-01 00:40:08 +00006045 // If the nested-name-specifier refers to the current instantiation,
6046 // the "typename" keyword itself is superfluous. In C++03, the
6047 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6048 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006049 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006050
John McCall77bb1aa2010-05-01 00:40:08 +00006051 if (RequireCompleteDeclContext(SS, Ctx))
6052 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006053
6054 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006055 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006056 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006057 unsigned DiagID = 0;
6058 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006059 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006060 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006061 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006062 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006063
6064 case LookupResult::FoundUnresolvedValue: {
6065 // We found a using declaration that is a value. Most likely, the using
6066 // declaration itself is meant to have the 'typename' keyword.
6067 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(),
6068 IILoc);
6069 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6070 << Name << Ctx << FullRange;
6071 if (UnresolvedUsingValueDecl *Using
6072 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006073 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006074 Diag(Loc, diag::note_using_value_decl_missing_typename)
6075 << FixItHint::CreateInsertion(Loc, "typename ");
6076 }
6077 }
6078 // Fall through to create a dependent typename type, from which we can recover
6079 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006080
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006081 case LookupResult::NotFoundInCurrentInstantiation:
6082 // Okay, it's a member of an unknown instantiation.
Douglas Gregor107de902010-04-24 15:35:55 +00006083 return Context.getDependentNameType(Keyword, NNS, &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006084
6085 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006086 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006087 // We found a type. Build an ElaboratedType, since the
6088 // typename-specifier was just sugar.
6089 return Context.getElaboratedType(ETK_Typename, NNS,
6090 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006091 }
6092
6093 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006094 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006095 break;
6096
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006097
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00006098 llvm_unreachable("unresolved using decl in non-dependent context");
John McCall7ba107a2009-11-18 02:36:19 +00006099 return QualType();
6100
Douglas Gregord57959a2009-03-27 23:10:48 +00006101 case LookupResult::FoundOverloaded:
6102 DiagID = diag::err_typename_nested_not_type;
6103 Referenced = *Result.begin();
6104 break;
6105
John McCall6e247262009-10-10 05:48:19 +00006106 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006107 return QualType();
6108 }
6109
6110 // If we get here, it's because name lookup did not find a
6111 // type. Emit an appropriate diagnostic and return an error.
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006112 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : NNSRange.getBegin(),
6113 IILoc);
6114 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006115 if (Referenced)
6116 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6117 << Name;
6118 return QualType();
6119}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006120
6121namespace {
6122 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006123 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006124 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006125 SourceLocation Loc;
6126 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006127
Douglas Gregor4a959d82009-08-06 16:20:37 +00006128 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006129 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006130
Mike Stump1eb44332009-09-09 15:08:12 +00006131 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006132 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006133 DeclarationName Entity)
6134 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006135 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006136
6137 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006138 /// transformed.
6139 ///
6140 /// For the purposes of type reconstruction, a type has already been
6141 /// transformed if it is NULL or if it is not dependent.
6142 bool AlreadyTransformed(QualType T) {
6143 return T.isNull() || !T->isDependentType();
6144 }
Mike Stump1eb44332009-09-09 15:08:12 +00006145
6146 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006147 /// rebuilt.
6148 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006149
Douglas Gregor4a959d82009-08-06 16:20:37 +00006150 /// \brief Returns the name of the entity whose type is being rebuilt.
6151 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006152
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006153 /// \brief Sets the "base" location and entity when that
6154 /// information is known based on another transformation.
6155 void setBase(SourceLocation Loc, DeclarationName Entity) {
6156 this->Loc = Loc;
6157 this->Entity = Entity;
6158 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006159 };
6160}
6161
Douglas Gregor4a959d82009-08-06 16:20:37 +00006162/// \brief Rebuilds a type within the context of the current instantiation.
6163///
Mike Stump1eb44332009-09-09 15:08:12 +00006164/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006165/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006166/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006167/// partial specialization thereof). This routine will rebuild that type now
6168/// that we have entered the declarator's scope, which may produce different
6169/// canonical types, e.g.,
6170///
6171/// \code
6172/// template<typename T>
6173/// struct X {
6174/// typedef T* pointer;
6175/// pointer data();
6176/// };
6177///
6178/// template<typename T>
6179/// typename X<T>::pointer X<T>::data() { ... }
6180/// \endcode
6181///
Douglas Gregor4714c122010-03-31 17:34:00 +00006182/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006183/// since we do not know that we can look into X<T> when we parsed the type.
6184/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006185/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006186/// as the canonical type of T*, allowing the return types of the out-of-line
6187/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006188TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6189 SourceLocation Loc,
6190 DeclarationName Name) {
6191 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006192 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006193
Douglas Gregor4a959d82009-08-06 16:20:37 +00006194 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6195 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006196}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006197
John McCall60d7b3a2010-08-24 06:29:42 +00006198ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006199 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6200 DeclarationName());
6201 return Rebuilder.TransformExpr(E);
6202}
6203
John McCall63b43852010-04-29 23:50:39 +00006204bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006205 if (SS.isInvalid())
6206 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006207
Douglas Gregor7e384942011-02-25 16:07:42 +00006208 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006209 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6210 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006211 NestedNameSpecifierLoc Rebuilt
6212 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6213 if (!Rebuilt)
6214 return true;
John McCall63b43852010-04-29 23:50:39 +00006215
Douglas Gregor7e384942011-02-25 16:07:42 +00006216 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006217 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006218}
6219
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006220/// \brief Produces a formatted string that describes the binding of
6221/// template parameters to template arguments.
6222std::string
6223Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6224 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006225 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006226}
6227
6228std::string
6229Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6230 const TemplateArgument *Args,
6231 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006232 llvm::SmallString<128> Str;
6233 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006234
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006235 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006236 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006237
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006238 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006239 if (I >= NumArgs)
6240 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006241
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006242 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006243 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006244 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006245 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006246
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006247 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006248 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006249 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006250 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006251 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006252
Douglas Gregor87dd6972010-12-20 16:52:59 +00006253 Out << " = ";
6254 Args[I].print(Context.PrintingPolicy, Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006255 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006256
6257 Out << ']';
6258 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006259}