blob: 5b7e4a72b7b0308c48dba900ba721a75c72e5e12 [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
Douglas Gregor312eadb2011-04-24 05:37:28 +000079void Sema::FilterAcceptableTemplateNames(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();
Douglas Gregor312eadb2011-04-24 05:37:28 +000085 NamedDecl *Repl = isAcceptableTemplateName(Context, 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
Richard Smith3e4c6c42011-05-05 21:57:07 +000095 // is used as a template-name, the reference refers to the class
96 // template itself and not a specialization thereof, and is not
Douglas Gregor01e56ae2010-04-12 20:54:26 +000097 // ambiguous.
Douglas Gregor01e56ae2010-04-12 20:54:26 +000098 if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
99 if (!ClassTemplates.insert(ClassTmpl)) {
100 filter.erase();
101 continue;
102 }
John McCall8ba66912010-08-13 07:02:08 +0000103
104 // FIXME: we promote access to public here as a workaround to
105 // the fact that LookupResult doesn't let us remember that we
106 // found this template through a particular injected class name,
107 // which means we end up doing nasty things to the invariants.
108 // Pretending that access is public is *much* safer.
109 filter.replace(Repl, AS_public);
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000110 }
John McCallf7a1a742009-11-24 19:00:30 +0000111 }
112 filter.done();
113}
114
Douglas Gregor312eadb2011-04-24 05:37:28 +0000115bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R) {
116 for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I)
117 if (isAcceptableTemplateName(Context, *I))
118 return true;
119
Douglas Gregor3b887352011-04-27 04:48:22 +0000120 return false;
Douglas Gregor312eadb2011-04-24 05:37:28 +0000121}
122
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000123TemplateNameKind Sema::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000124 CXXScopeSpec &SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000125 bool hasTemplateKeyword,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000126 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +0000127 ParsedType ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000128 bool EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000129 TemplateTy &TemplateResult,
130 bool &MemberOfUnknownSpecialization) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +0000131 assert(getLangOptions().CPlusPlus && "No template names in C!");
132
Douglas Gregor014e88d2009-11-03 23:16:33 +0000133 DeclarationName TName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000134 MemberOfUnknownSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000135
Douglas Gregor014e88d2009-11-03 23:16:33 +0000136 switch (Name.getKind()) {
137 case UnqualifiedId::IK_Identifier:
138 TName = DeclarationName(Name.Identifier);
139 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000140
Douglas Gregor014e88d2009-11-03 23:16:33 +0000141 case UnqualifiedId::IK_OperatorFunctionId:
142 TName = Context.DeclarationNames.getCXXOperatorName(
143 Name.OperatorFunctionId.Operator);
144 break;
145
Sean Hunte6252d12009-11-28 08:58:14 +0000146 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +0000147 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
148 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000149
Douglas Gregor014e88d2009-11-03 23:16:33 +0000150 default:
151 return TNK_Non_template;
152 }
Mike Stump1eb44332009-09-09 15:08:12 +0000153
John McCallb3d87482010-08-24 05:47:05 +0000154 QualType ObjectType = ObjectTypePtr.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000155
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000156 LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000157 LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000158 LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
159 MemberOfUnknownSpecialization);
John McCall67d22fb2010-08-28 20:17:00 +0000160 if (R.empty()) return TNK_Non_template;
161 if (R.isAmbiguous()) {
162 // Suppress diagnostics; we'll redo this lookup later.
John McCallb8592062010-08-13 02:23:42 +0000163 R.suppressDiagnostics();
John McCall67d22fb2010-08-28 20:17:00 +0000164
165 // FIXME: we might have ambiguous templates, in which case we
166 // should at least parse them properly!
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000167 return TNK_Non_template;
John McCallb8592062010-08-13 02:23:42 +0000168 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000169
John McCall0bd6feb2009-12-02 08:04:21 +0000170 TemplateName Template;
171 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000172
John McCall0bd6feb2009-12-02 08:04:21 +0000173 unsigned ResultCount = R.end() - R.begin();
174 if (ResultCount > 1) {
175 // We assume that we'll preserve the qualifier from a function
176 // template name in other ways.
177 Template = Context.getOverloadedTemplateName(R.begin(), R.end());
178 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000179
180 // We'll do this lookup again later.
181 R.suppressDiagnostics();
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000182 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000183 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
184
185 if (SS.isSet() && !SS.isInvalid()) {
186 NestedNameSpecifier *Qualifier
187 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Abramo Bagnara7c153532010-08-06 12:11:11 +0000188 Template = Context.getQualifiedTemplateName(Qualifier,
189 hasTemplateKeyword, TD);
John McCall0bd6feb2009-12-02 08:04:21 +0000190 } else {
191 Template = TemplateName(TD);
192 }
193
John McCallb8592062010-08-13 02:23:42 +0000194 if (isa<FunctionTemplateDecl>(TD)) {
John McCall0bd6feb2009-12-02 08:04:21 +0000195 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000196
197 // We'll do this lookup again later.
198 R.suppressDiagnostics();
199 } else {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000200 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) ||
201 isa<TypeAliasTemplateDecl>(TD));
John McCall0bd6feb2009-12-02 08:04:21 +0000202 TemplateKind = TNK_Type_template;
203 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205
John McCall0bd6feb2009-12-02 08:04:21 +0000206 TemplateResult = TemplateTy::make(Template);
207 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000208}
209
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000210bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Douglas Gregor84d0a192010-01-12 21:28:44 +0000211 SourceLocation IILoc,
212 Scope *S,
213 const CXXScopeSpec *SS,
214 TemplateTy &SuggestedTemplate,
215 TemplateNameKind &SuggestedKind) {
216 // We can't recover unless there's a dependent scope specifier preceding the
217 // template name.
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000218 // FIXME: Typo correction?
Douglas Gregor84d0a192010-01-12 21:28:44 +0000219 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
220 computeDeclContext(*SS))
221 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000222
Douglas Gregor84d0a192010-01-12 21:28:44 +0000223 // The code is missing a 'template' keyword prior to the dependent template
224 // name.
225 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
226 Diag(IILoc, diag::err_template_kw_missing)
227 << Qualifier << II.getName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000228 << FixItHint::CreateInsertion(IILoc, "template ");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000229 SuggestedTemplate
Douglas Gregor84d0a192010-01-12 21:28:44 +0000230 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
231 SuggestedKind = TNK_Dependent_template_name;
232 return true;
233}
234
John McCallf7a1a742009-11-24 19:00:30 +0000235void Sema::LookupTemplateName(LookupResult &Found,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000236 Scope *S, CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +0000237 QualType ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000238 bool EnteringContext,
239 bool &MemberOfUnknownSpecialization) {
John McCallf7a1a742009-11-24 19:00:30 +0000240 // Determine where to perform name lookup
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000241 MemberOfUnknownSpecialization = false;
John McCallf7a1a742009-11-24 19:00:30 +0000242 DeclContext *LookupCtx = 0;
243 bool isDependent = false;
244 if (!ObjectType.isNull()) {
245 // This nested-name-specifier occurs in a member access expression, e.g.,
246 // x->B::f, and we are looking into the type of the object.
247 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
248 LookupCtx = computeDeclContext(ObjectType);
249 isDependent = ObjectType->isDependentType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000250 assert((isDependent || !ObjectType->isIncompleteType()) &&
John McCallf7a1a742009-11-24 19:00:30 +0000251 "Caller should have completed object type");
Douglas Gregor1d7049a2012-01-12 16:11:24 +0000252
253 // Template names cannot appear inside an Objective-C class or object type.
254 if (ObjectType->isObjCObjectOrInterfaceType()) {
255 Found.clear();
256 return;
257 }
John McCallf7a1a742009-11-24 19:00:30 +0000258 } else if (SS.isSet()) {
259 // This nested-name-specifier occurs after another nested-name-specifier,
260 // so long into the context associated with the prior nested-name-specifier.
261 LookupCtx = computeDeclContext(SS, EnteringContext);
262 isDependent = isDependentScopeSpecifier(SS);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000263
John McCallf7a1a742009-11-24 19:00:30 +0000264 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000265 if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
John McCallf7a1a742009-11-24 19:00:30 +0000266 return;
267 }
268
269 bool ObjectTypeSearchedInScope = false;
270 if (LookupCtx) {
271 // Perform "qualified" name lookup into the declaration context we
272 // computed, which is either the type of the base of a member access
273 // expression or the declaration context associated with a prior
274 // nested-name-specifier.
275 LookupQualifiedName(Found, LookupCtx);
276
277 if (!ObjectType.isNull() && Found.empty()) {
278 // C++ [basic.lookup.classref]p1:
279 // In a class member access expression (5.2.5), if the . or -> token is
280 // immediately followed by an identifier followed by a <, the
281 // identifier must be looked up to determine whether the < is the
282 // beginning of a template argument list (14.2) or a less-than operator.
283 // The identifier is first looked up in the class of the object
284 // expression. If the identifier is not found, it is then looked up in
285 // the context of the entire postfix-expression and shall name a class
286 // or function template.
John McCallf7a1a742009-11-24 19:00:30 +0000287 if (S) LookupName(Found, S);
288 ObjectTypeSearchedInScope = true;
289 }
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000290 } else if (isDependent && (!S || ObjectType.isNull())) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000291 // We cannot look into a dependent object type or nested nme
292 // specifier.
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000293 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000294 return;
295 } else {
296 // Perform unqualified name lookup in the current scope.
297 LookupName(Found, S);
298 }
299
Douglas Gregor2e933882010-01-12 17:06:20 +0000300 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000301 // If we did not find any names, attempt to correct any typos.
302 DeclarationName Name = Found.getLookupName();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000303 Found.clear();
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000304 // Simple filter callback that, for keywords, only accepts the C++ *_cast
305 CorrectionCandidateCallback FilterCCC;
306 FilterCCC.WantTypeSpecifiers = false;
307 FilterCCC.WantExpressionKeywords = false;
308 FilterCCC.WantRemainingKeywords = false;
309 FilterCCC.WantCXXNamedCasts = true;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000310 if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(),
311 Found.getLookupKind(), S, &SS,
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000312 &FilterCCC, LookupCtx)) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000313 Found.setLookupName(Corrected.getCorrection());
314 if (Corrected.getCorrectionDecl())
315 Found.addDecl(Corrected.getCorrectionDecl());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000316 FilterAcceptableTemplateNames(Found);
John McCallad00b772010-06-16 08:42:20 +0000317 if (!Found.empty()) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000318 std::string CorrectedStr(Corrected.getAsString(getLangOptions()));
319 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions()));
Douglas Gregorbfea2392009-12-31 08:11:17 +0000320 if (LookupCtx)
321 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000322 << Name << LookupCtx << CorrectedQuotedStr << SS.getRange()
323 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000324 else
325 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000326 << Name << CorrectedQuotedStr
327 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000328 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
329 Diag(Template->getLocation(), diag::note_previous_decl)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000330 << CorrectedQuotedStr;
John McCallad00b772010-06-16 08:42:20 +0000331 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000332 } else {
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000333 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000334 }
335 }
336
Douglas Gregor312eadb2011-04-24 05:37:28 +0000337 FilterAcceptableTemplateNames(Found);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000338 if (Found.empty()) {
339 if (isDependent)
340 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000341 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000342 }
John McCallf7a1a742009-11-24 19:00:30 +0000343
344 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
345 // C++ [basic.lookup.classref]p1:
346 // [...] If the lookup in the class of the object expression finds a
347 // template, the name is also looked up in the context of the entire
348 // postfix-expression and [...]
349 //
350 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
351 LookupOrdinaryName);
352 LookupName(FoundOuter, S);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000353 FilterAcceptableTemplateNames(FoundOuter);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000354
John McCallf7a1a742009-11-24 19:00:30 +0000355 if (FoundOuter.empty()) {
356 // - if the name is not found, the name found in the class of the
357 // object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000358 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() ||
359 FoundOuter.isAmbiguous()) {
John McCallf7a1a742009-11-24 19:00:30 +0000360 // - if the name is found in the context of the entire
361 // postfix-expression and does not name a class template, the name
362 // found in the class of the object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000363 FoundOuter.clear();
John McCallad00b772010-06-16 08:42:20 +0000364 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000365 // - if the name found is a class template, it must refer to the same
366 // entity as the one found in the class of the object expression,
367 // otherwise the program is ill-formed.
368 if (!Found.isSingleResult() ||
369 Found.getFoundDecl()->getCanonicalDecl()
370 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000371 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000372 diag::ext_nested_name_member_ref_lookup_ambiguous)
373 << Found.getLookupName()
374 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000375 Diag(Found.getRepresentativeDecl()->getLocation(),
376 diag::note_ambig_member_ref_object_type)
377 << ObjectType;
378 Diag(FoundOuter.getFoundDecl()->getLocation(),
379 diag::note_ambig_member_ref_scope);
380
381 // Recover by taking the template that we found in the object
382 // expression's type.
383 }
384 }
385 }
386}
387
John McCall2f841ba2009-12-02 03:53:29 +0000388/// ActOnDependentIdExpression - Handle a dependent id-expression that
389/// was just parsed. This is only possible with an explicit scope
390/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000391ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000392Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000393 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000394 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000395 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000396 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000397 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000398
John McCall2f841ba2009-12-02 03:53:29 +0000399 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000400 isa<CXXMethodDecl>(DC) &&
401 cast<CXXMethodDecl>(DC)->isInstance()) {
402 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000403
John McCallf7a1a742009-11-24 19:00:30 +0000404 // Since the 'this' expression is synthesized, we don't need to
405 // perform the double-lookup check.
406 NamedDecl *FirstQualifierInScope = 0;
407
John McCallaa81e162009-12-01 22:10:20 +0000408 return Owned(CXXDependentScopeMemberExpr::Create(Context,
409 /*This*/ 0, ThisType,
410 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000411 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000412 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000413 TemplateKWLoc,
John McCallf7a1a742009-11-24 19:00:30 +0000414 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000415 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000416 TemplateArgs));
417 }
418
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000419 return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000420}
421
John McCall60d7b3a2010-08-24 06:29:42 +0000422ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000423Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000424 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000425 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000426 const TemplateArgumentListInfo *TemplateArgs) {
427 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000428 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000429 TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000430 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000431 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000432}
433
Douglas Gregor72c3f312008-12-05 18:15:24 +0000434/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
435/// that the template parameter 'PrevDecl' is being shadowed by a new
436/// declaration at location Loc. Returns true to indicate that this is
437/// an error, and false otherwise.
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000438void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000439 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000440
441 // Microsoft Visual C++ permits template parameters to be shadowed.
Francois Pichet62ec1f22011-09-17 17:15:52 +0000442 if (getLangOptions().MicrosoftExt)
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000443 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000444
445 // C++ [temp.local]p4:
446 // A template-parameter shall not be redeclared within its
447 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000448 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000449 << cast<NamedDecl>(PrevDecl)->getDeclName();
450 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000451 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000452}
453
Douglas Gregor2943aed2009-03-03 04:44:36 +0000454/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000455/// the parameter D to reference the templated declaration and return a pointer
456/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000457TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
458 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
459 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000460 return Temp;
461 }
462 return 0;
463}
464
Douglas Gregorba68eca2011-01-05 17:40:24 +0000465ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
466 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000467 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000468 "Only template template arguments can be pack expansions here");
469 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
470 "Template template argument pack expansion without packs");
471 ParsedTemplateArgument Result(*this);
472 Result.EllipsisLoc = EllipsisLoc;
473 return Result;
474}
475
Douglas Gregor788cd062009-11-11 01:00:40 +0000476static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
477 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000478
Douglas Gregor788cd062009-11-11 01:00:40 +0000479 switch (Arg.getKind()) {
480 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000481 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000482 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000483 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000484 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000485 return TemplateArgumentLoc(TemplateArgument(T), DI);
486 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000487
Douglas Gregor788cd062009-11-11 01:00:40 +0000488 case ParsedTemplateArgument::NonType: {
489 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
490 return TemplateArgumentLoc(TemplateArgument(E), E);
491 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000492
Douglas Gregor788cd062009-11-11 01:00:40 +0000493 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000494 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000495 TemplateArgument TArg;
496 if (Arg.getEllipsisLoc().isValid())
497 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
498 else
499 TArg = Template;
500 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000501 Arg.getScopeSpec().getWithLocInContext(
502 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000503 Arg.getLocation(),
504 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000505 }
506 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000507
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000508 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000509}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000510
Douglas Gregor788cd062009-11-11 01:00:40 +0000511/// \brief Translates template arguments as provided by the parser
512/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000513void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
514 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000515 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000516 TemplateArgs.addArgument(translateTemplateArgument(*this,
517 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000518}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000519
Douglas Gregor72c3f312008-12-05 18:15:24 +0000520/// ActOnTypeParameter - Called when a C++ template type parameter
521/// (e.g., "typename T") has been parsed. Typename specifies whether
522/// the keyword "typename" was used to declare the type parameter
523/// (otherwise, "class" was used), and KeyLoc is the location of the
524/// "class" or "typename" keyword. ParamName is the name of the
525/// parameter (NULL indicates an unnamed template parameter) and
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000526/// ParamNameLoc is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000527/// If the type parameter has a default argument, it will be added
528/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000529Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
530 SourceLocation EllipsisLoc,
531 SourceLocation KeyLoc,
532 IdentifierInfo *ParamName,
533 SourceLocation ParamNameLoc,
534 unsigned Depth, unsigned Position,
535 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000536 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000537 assert(S->isTemplateParamScope() &&
538 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000539 bool Invalid = false;
540
541 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000542 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000543 LookupOrdinaryName,
544 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000545 if (PrevDecl && PrevDecl->isTemplateParameter()) {
546 DiagnoseTemplateParameterShadow(ParamNameLoc, PrevDecl);
547 PrevDecl = 0;
548 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000549 }
550
Douglas Gregorddc29e12009-02-06 22:42:48 +0000551 SourceLocation Loc = ParamNameLoc;
552 if (!ParamName)
553 Loc = KeyLoc;
554
Douglas Gregor72c3f312008-12-05 18:15:24 +0000555 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000556 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000557 KeyLoc, Loc, Depth, Position, ParamName,
558 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000559 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000560 if (Invalid)
561 Param->setInvalidDecl();
562
563 if (ParamName) {
564 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000565 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000566 IdResolver.AddDecl(Param);
567 }
568
Douglas Gregor61c4d282011-01-05 15:48:55 +0000569 // C++0x [temp.param]p9:
570 // A default template-argument may be specified for any kind of
571 // template-parameter that is not a template parameter pack.
572 if (DefaultArg && Ellipsis) {
573 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
574 DefaultArg = ParsedType();
575 }
576
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000577 // Handle the default argument, if provided.
578 if (DefaultArg) {
579 TypeSourceInfo *DefaultTInfo;
580 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000581
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000582 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000583
Douglas Gregor6f526752010-12-16 08:48:57 +0000584 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000585 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000586 UPPC_DefaultArgument))
587 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000588
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000589 // Check the template argument itself.
590 if (CheckTemplateArgument(Param, DefaultTInfo)) {
591 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000592 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000593 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000594
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000595 Param->setDefaultArgument(DefaultTInfo, false);
596 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000597
John McCalld226f652010-08-21 09:40:31 +0000598 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000599}
600
Douglas Gregor2943aed2009-03-03 04:44:36 +0000601/// \brief Check that the type of a non-type template parameter is
602/// well-formed.
603///
604/// \returns the (possibly-promoted) parameter type if valid;
605/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000606QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000607Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000608 // We don't allow variably-modified types as the type of non-type template
609 // parameters.
610 if (T->isVariablyModifiedType()) {
611 Diag(Loc, diag::err_variably_modified_nontype_template_param)
612 << T;
613 return QualType();
614 }
615
Douglas Gregor2943aed2009-03-03 04:44:36 +0000616 // C++ [temp.param]p4:
617 //
618 // A non-type template-parameter shall have one of the following
619 // (optionally cv-qualified) types:
620 //
621 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000622 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000623 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000624 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000625 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000626 T->isReferenceType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000627 // -- pointer to member,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000628 T->isMemberPointerType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000629 // -- std::nullptr_t.
630 T->isNullPtrType() ||
Douglas Gregor2943aed2009-03-03 04:44:36 +0000631 // If T is a dependent type, we can't do the check now, so we
632 // assume that it is well-formed.
633 T->isDependentType())
634 return T;
635 // C++ [temp.param]p8:
636 //
637 // A non-type template-parameter of type "array of T" or
638 // "function returning T" is adjusted to be of type "pointer to
639 // T" or "pointer to function returning T", respectively.
640 else if (T->isArrayType())
641 // FIXME: Keep the type prior to promotion?
642 return Context.getArrayDecayedType(T);
643 else if (T->isFunctionType())
644 // FIXME: Keep the type prior to promotion?
645 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000646
Douglas Gregor2943aed2009-03-03 04:44:36 +0000647 Diag(Loc, diag::err_template_nontype_parm_bad_type)
648 << T;
649
650 return QualType();
651}
652
John McCalld226f652010-08-21 09:40:31 +0000653Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
654 unsigned Depth,
655 unsigned Position,
656 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000657 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000658 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
659 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000660
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000661 assert(S->isTemplateParamScope() &&
662 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000663 bool Invalid = false;
664
665 IdentifierInfo *ParamName = D.getIdentifier();
666 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000667 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000668 LookupOrdinaryName,
669 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000670 if (PrevDecl && PrevDecl->isTemplateParameter()) {
671 DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl);
672 PrevDecl = 0;
673 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000674 }
675
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000676 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
677 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000678 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000679 Invalid = true;
680 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000681
Douglas Gregor10738d32010-12-23 23:51:58 +0000682 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000683 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000684 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000685 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000686 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000687 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000688 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000689 Param->setAccess(AS_public);
690
Douglas Gregor72c3f312008-12-05 18:15:24 +0000691 if (Invalid)
692 Param->setInvalidDecl();
693
694 if (D.getIdentifier()) {
695 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000696 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000697 IdResolver.AddDecl(Param);
698 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000699
Douglas Gregor61c4d282011-01-05 15:48:55 +0000700 // C++0x [temp.param]p9:
701 // A default template-argument may be specified for any kind of
702 // template-parameter that is not a template parameter pack.
703 if (Default && IsParameterPack) {
704 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
705 Default = 0;
706 }
707
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000708 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000709 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000710 // Check for unexpanded parameter packs.
711 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
712 return Param;
713
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000714 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000715 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
716 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000717 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000718 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000719 }
John Wiegley429bb272011-04-08 18:41:53 +0000720 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000721
John McCall9ae2f072010-08-23 23:25:46 +0000722 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000723 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000724
John McCalld226f652010-08-21 09:40:31 +0000725 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000726}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000727
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000728/// ActOnTemplateTemplateParameter - Called when a C++ template template
729/// parameter (e.g. T in template <template <typename> class T> class array)
730/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000731Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
732 SourceLocation TmpLoc,
Richard Trieu90ab75b2011-09-09 03:18:59 +0000733 TemplateParameterList *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000734 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000735 IdentifierInfo *Name,
736 SourceLocation NameLoc,
737 unsigned Depth,
738 unsigned Position,
739 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000740 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000741 assert(S->isTemplateParamScope() &&
742 "Template template parameter not in template parameter scope!");
743
744 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000745 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000746 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000747 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000748 NameLoc.isInvalid()? TmpLoc : NameLoc,
749 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000750 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000751 Param->setAccess(AS_public);
752
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000753 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000754 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000755 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000756 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000757 IdResolver.AddDecl(Param);
758 }
759
Douglas Gregor6f526752010-12-16 08:48:57 +0000760 if (Params->size() == 0) {
761 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
762 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
763 Param->setInvalidDecl();
764 }
765
Douglas Gregor61c4d282011-01-05 15:48:55 +0000766 // C++0x [temp.param]p9:
767 // A default template-argument may be specified for any kind of
768 // template-parameter that is not a template parameter pack.
769 if (IsParameterPack && !Default.isInvalid()) {
770 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
771 Default = ParsedTemplateArgument();
772 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000773
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000774 if (!Default.isInvalid()) {
775 // Check only that we have a template template argument. We don't want to
776 // try to check well-formedness now, because our template template parameter
777 // might have dependent types in its template parameters, which we wouldn't
778 // be able to match now.
779 //
780 // If none of the template template parameter's template arguments mention
781 // other template parameters, we could actually perform more checking here.
782 // However, it isn't worth doing.
783 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
784 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
785 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
786 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000787 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000788 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000789
Douglas Gregor6f526752010-12-16 08:48:57 +0000790 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000791 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000792 DefaultArg.getArgument().getAsTemplate(),
793 UPPC_DefaultArgument))
794 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000795
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000796 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000797 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000798
John McCalld226f652010-08-21 09:40:31 +0000799 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000800}
801
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000802/// ActOnTemplateParameterList - Builds a TemplateParameterList that
803/// contains the template parameters in Params/NumParams.
Richard Trieu90ab75b2011-09-09 03:18:59 +0000804TemplateParameterList *
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000805Sema::ActOnTemplateParameterList(unsigned Depth,
806 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000807 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000808 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000809 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000810 SourceLocation RAngleLoc) {
811 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000812 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000813
Douglas Gregorddc29e12009-02-06 22:42:48 +0000814 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000815 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000816 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000817}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000818
John McCallb6217662010-03-15 10:12:16 +0000819static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
820 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000821 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000822}
823
John McCallf312b1e2010-08-26 23:41:50 +0000824DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000825Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000826 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000827 IdentifierInfo *Name, SourceLocation NameLoc,
828 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000829 TemplateParameterList *TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +0000830 AccessSpecifier AS, SourceLocation ModulePrivateLoc,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000831 unsigned NumOuterTemplateParamLists,
832 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000833 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000834 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000835 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000836 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000837
838 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000839 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000840 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000841
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000842 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
843 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000844
845 // There is no such thing as an unnamed class template.
846 if (!Name) {
847 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000848 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000849 }
850
851 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000852 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000853 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000854 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000855 if (SS.isNotEmpty() && !SS.isInvalid()) {
856 SemanticContext = computeDeclContext(SS, true);
857 if (!SemanticContext) {
858 // FIXME: Produce a reasonable diagnostic here
859 return true;
860 }
Mike Stump1eb44332009-09-09 15:08:12 +0000861
John McCall77bb1aa2010-05-01 00:40:08 +0000862 if (RequireCompleteDeclContext(SS, SemanticContext))
863 return true;
864
Douglas Gregor20606502011-10-14 15:31:12 +0000865 // If we're adding a template to a dependent context, we may need to
866 // rebuilding some of the types used within the template parameter list,
867 // now that we know what the current instantiation is.
868 if (SemanticContext->isDependentContext()) {
869 ContextRAII SavedContext(*this, SemanticContext);
870 if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams))
871 Invalid = true;
872 }
873
John McCalla24dc2e2009-11-17 02:14:36 +0000874 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000875 } else {
876 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000877 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000878 }
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Douglas Gregor57265e32010-04-12 16:00:01 +0000880 if (Previous.isAmbiguous())
881 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000882
Douglas Gregorddc29e12009-02-06 22:42:48 +0000883 NamedDecl *PrevDecl = 0;
884 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000885 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000886
Douglas Gregorddc29e12009-02-06 22:42:48 +0000887 // If there is a previous declaration with the same name, check
888 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000889 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000890 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000891
892 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000893 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000894 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000895 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000896 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
897 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000898 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000899 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
900 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
901 PrevClassTemplate
902 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
903 ->getSpecializedTemplate();
904 }
905 }
906
John McCall65c49462009-12-18 11:25:59 +0000907 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000908 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000909 // [...] When looking for a prior declaration of a class or a function
910 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000911 // function is neither a qualified name nor a template-id, scopes outside
912 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000913 if (!SS.isSet()) {
914 DeclContext *OutermostContext = CurContext;
915 while (!OutermostContext->isFileContext())
916 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000917
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000918 if (PrevDecl &&
919 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
920 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
921 SemanticContext = PrevDecl->getDeclContext();
922 } else {
923 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000924 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000925 // declaration.
926 PrevDecl = PrevClassTemplate = 0;
927 SemanticContext = OutermostContext;
928 }
John McCalle129d442009-12-17 23:21:11 +0000929 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000930
John McCalle129d442009-12-17 23:21:11 +0000931 if (CurContext->isDependentContext()) {
932 // If this is a dependent context, we don't want to link the friend
933 // class template to the template in scope, because that would perform
934 // checking of the template parameter lists that can't be performed
935 // until the outer context is instantiated.
936 PrevDecl = PrevClassTemplate = 0;
937 }
938 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
939 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000940
Douglas Gregorddc29e12009-02-06 22:42:48 +0000941 if (PrevClassTemplate) {
942 // Ensure that the template parameter lists are compatible.
943 if (!TemplateParameterListsAreEqual(TemplateParams,
944 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000945 /*Complain=*/true,
946 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000947 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000948
949 // C++ [temp.class]p4:
950 // In a redeclaration, partial specialization, explicit
951 // specialization or explicit instantiation of a class template,
952 // the class-key shall agree in kind with the original class
953 // template declaration (7.1.5.3).
954 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Richard Trieubbf34c02011-06-10 03:11:26 +0000955 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind,
956 TUK == TUK_Definition, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000957 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000958 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000959 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000960 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000961 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000962 }
963
Douglas Gregorddc29e12009-02-06 22:42:48 +0000964 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000965 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000966 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000967 Diag(NameLoc, diag::err_redefinition) << Name;
968 Diag(Def->getLocation(), diag::note_previous_definition);
969 // FIXME: Would it make sense to try to "forget" the previous
970 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000971 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000972 }
Douglas Gregor6311d2b2011-09-09 18:32:39 +0000973 }
Douglas Gregorddc29e12009-02-06 22:42:48 +0000974 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
975 // Maybe we will complain about the shadowed template parameter.
976 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
977 // Just pretend that we didn't see the previous declaration.
978 PrevDecl = 0;
979 } else if (PrevDecl) {
980 // C++ [temp]p5:
981 // A class template shall not have the same name as any other
982 // template, class, function, object, enumeration, enumerator,
983 // namespace, or type in the same scope (3.3), except as specified
984 // in (14.5.4).
985 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
986 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000987 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000988 }
989
Douglas Gregord684b002009-02-10 19:49:53 +0000990 // Check the template parameter list of this declaration, possibly
991 // merging in the template parameter list from the previous class
992 // template declaration.
993 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000994 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000995 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000996 SemanticContext->isRecord() &&
997 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000998 ? TPC_ClassTemplateMember
999 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +00001000 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Douglas Gregor57265e32010-04-12 16:00:01 +00001002 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001003 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +00001004 // template out-of-line.
1005 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
Douglas Gregorea9f54a2011-11-01 21:35:16 +00001006 !(TUK == TUK_Friend && CurContext->isDependentContext())) {
Douglas Gregor57265e32010-04-12 16:00:01 +00001007 Diag(NameLoc, diag::err_member_def_does_not_match)
1008 << Name << SemanticContext << SS.getRange();
Douglas Gregorea9f54a2011-11-01 21:35:16 +00001009 Invalid = true;
1010 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001011 }
1012
Mike Stump1eb44332009-09-09 15:08:12 +00001013 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001014 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +00001015 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001016 PrevClassTemplate->getTemplatedDecl() : 0,
1017 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +00001018 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00001019 if (NumOuterTemplateParamLists > 0)
1020 NewClass->setTemplateParameterListsInfo(Context,
1021 NumOuterTemplateParamLists,
1022 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001023
1024 ClassTemplateDecl *NewTemplate
1025 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
1026 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001027 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +00001028 NewClass->setDescribedClassTemplate(NewTemplate);
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001029
Douglas Gregor2ccd89c2011-12-20 18:11:52 +00001030 if (ModulePrivateLoc.isValid())
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001031 NewTemplate->setModulePrivate();
Douglas Gregor8d267c52011-09-09 02:06:17 +00001032
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001033 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +00001034 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +00001035 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001036 assert(T->isDependentType() && "Class template type is not dependent?");
1037 (void)T;
1038
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001039 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001040 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001041 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001042 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1043 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001044
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001045 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001046 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001047 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Douglas Gregorddc29e12009-02-06 22:42:48 +00001049 // Set the lexical context of these templates
1050 NewClass->setLexicalDeclContext(CurContext);
1051 NewTemplate->setLexicalDeclContext(CurContext);
1052
John McCall0f434ec2009-07-31 02:45:11 +00001053 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001054 NewClass->startDefinition();
1055
1056 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001057 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001058
John McCall05b23ea2009-09-14 21:59:20 +00001059 if (TUK != TUK_Friend)
1060 PushOnScopeChains(NewTemplate, S);
1061 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001062 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001063 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001064 NewClass->setAccess(PrevClassTemplate->getAccess());
1065 }
John McCall05b23ea2009-09-14 21:59:20 +00001066
Douglas Gregord85bea22009-09-26 06:47:28 +00001067 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1068 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001069
John McCall05b23ea2009-09-14 21:59:20 +00001070 // Friend templates are visible in fairly strange ways.
1071 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001072 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001073 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1074 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1075 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001076 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001077 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001078
Douglas Gregord85bea22009-09-26 06:47:28 +00001079 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1080 NewClass->getLocation(),
1081 NewTemplate,
1082 /*FIXME:*/NewClass->getLocation());
1083 Friend->setAccess(AS_public);
1084 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001085 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001086
Douglas Gregord684b002009-02-10 19:49:53 +00001087 if (Invalid) {
1088 NewTemplate->setInvalidDecl();
1089 NewClass->setInvalidDecl();
1090 }
John McCalld226f652010-08-21 09:40:31 +00001091 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001092}
1093
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001094/// \brief Diagnose the presence of a default template argument on a
1095/// template parameter, which is ill-formed in certain contexts.
1096///
1097/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001098static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001099 Sema::TemplateParamListContext TPC,
1100 SourceLocation ParamLoc,
1101 SourceRange DefArgRange) {
1102 switch (TPC) {
1103 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001104 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001105 return false;
1106
1107 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001108 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001109 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001110 // A default template-argument shall not be specified in a
1111 // function template declaration or a function template
1112 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001113 // If a friend function template declaration specifies a default
1114 // template-argument, that declaration shall be a definition and shall be
1115 // the only declaration of the function template in the translation unit.
1116 // (C++98/03 doesn't have this wording; see DR226).
Richard Smithebaf0e62011-10-18 20:49:44 +00001117 S.Diag(ParamLoc, S.getLangOptions().CPlusPlus0x ?
1118 diag::warn_cxx98_compat_template_parameter_default_in_function_template
1119 : diag::ext_template_parameter_default_in_function_template)
1120 << DefArgRange;
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001121 return false;
1122
1123 case Sema::TPC_ClassTemplateMember:
1124 // C++0x [temp.param]p9:
1125 // A default template-argument shall not be specified in the
1126 // template-parameter-lists of the definition of a member of a
1127 // class template that appears outside of the member's class.
1128 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1129 << DefArgRange;
1130 return true;
1131
1132 case Sema::TPC_FriendFunctionTemplate:
1133 // C++ [temp.param]p9:
1134 // A default template-argument shall not be specified in a
1135 // friend template declaration.
1136 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1137 << DefArgRange;
1138 return true;
1139
1140 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1141 // for friend function templates if there is only a single
1142 // declaration (and it is a definition). Strange!
1143 }
1144
David Blaikie7530c032012-01-17 06:56:22 +00001145 llvm_unreachable("Invalid TemplateParamListContext!");
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001146}
1147
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001148/// \brief Check for unexpanded parameter packs within the template parameters
1149/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001150static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1151 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001152 TemplateParameterList *Params = TTP->getTemplateParameters();
1153 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1154 NamedDecl *P = Params->getParam(I);
1155 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001156 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001157 NTTP->getTypeSourceInfo(),
1158 Sema::UPPC_NonTypeTemplateParameterType))
1159 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001160
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001161 continue;
1162 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001163
1164 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001165 = dyn_cast<TemplateTemplateParmDecl>(P))
1166 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1167 return true;
1168 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001169
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001170 return false;
1171}
1172
Douglas Gregord684b002009-02-10 19:49:53 +00001173/// \brief Checks the validity of a template parameter list, possibly
1174/// considering the template parameter list from a previous
1175/// declaration.
1176///
1177/// If an "old" template parameter list is provided, it must be
1178/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1179/// template parameter list.
1180///
1181/// \param NewParams Template parameter list for a new template
1182/// declaration. This template parameter list will be updated with any
1183/// default arguments that are carried through from the previous
1184/// template parameter list.
1185///
1186/// \param OldParams If provided, template parameter list from a
1187/// previous declaration of the same template. Default template
1188/// arguments will be merged from the old template parameter list to
1189/// the new template parameter list.
1190///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001191/// \param TPC Describes the context in which we are checking the given
1192/// template parameter list.
1193///
Douglas Gregord684b002009-02-10 19:49:53 +00001194/// \returns true if an error occurred, false otherwise.
1195bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001196 TemplateParameterList *OldParams,
1197 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001198 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Douglas Gregord684b002009-02-10 19:49:53 +00001200 // C++ [temp.param]p10:
1201 // The set of default template-arguments available for use with a
1202 // template declaration or definition is obtained by merging the
1203 // default arguments from the definition (if in scope) and all
1204 // declarations in scope in the same way default function
1205 // arguments are (8.3.6).
1206 bool SawDefaultArgument = false;
1207 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001208
Mike Stump1a35fde2009-02-11 23:03:27 +00001209 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001210 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001211 if (OldParams)
1212 OldParam = OldParams->begin();
1213
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001214 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001215 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1216 NewParamEnd = NewParams->end();
1217 NewParam != NewParamEnd; ++NewParam) {
1218 // Variables used to diagnose redundant default arguments
1219 bool RedundantDefaultArg = false;
1220 SourceLocation OldDefaultLoc;
1221 SourceLocation NewDefaultLoc;
1222
David Blaikie1368e582011-10-19 05:19:50 +00001223 // Variable used to diagnose missing default arguments
Douglas Gregord684b002009-02-10 19:49:53 +00001224 bool MissingDefaultArg = false;
1225
David Blaikie1368e582011-10-19 05:19:50 +00001226 // Variable used to diagnose non-final parameter packs
1227 bool SawParameterPack = false;
Anders Carlsson49d25572009-06-12 23:20:15 +00001228
Douglas Gregord684b002009-02-10 19:49:53 +00001229 if (TemplateTypeParmDecl *NewTypeParm
1230 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001231 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001232 if (NewTypeParm->hasDefaultArgument() &&
1233 DiagnoseDefaultTemplateArgument(*this, TPC,
1234 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001235 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001236 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001237 NewTypeParm->removeDefaultArgument();
1238
1239 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001240 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001241 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001242
Anders Carlsson49d25572009-06-12 23:20:15 +00001243 if (NewTypeParm->isParameterPack()) {
1244 assert(!NewTypeParm->hasDefaultArgument() &&
1245 "Parameter packs can't have a default argument!");
1246 SawParameterPack = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001247 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001248 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001249 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1250 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1251 SawDefaultArgument = true;
1252 RedundantDefaultArg = true;
1253 PreviousDefaultArgLoc = NewDefaultLoc;
1254 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1255 // Merge the default argument from the old declaration to the
1256 // new declaration.
1257 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001258 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001259 true);
1260 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1261 } else if (NewTypeParm->hasDefaultArgument()) {
1262 SawDefaultArgument = true;
1263 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1264 } else if (SawDefaultArgument)
1265 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001266 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001267 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001268 // Check for unexpanded parameter packs.
1269 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001270 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001271 UPPC_NonTypeTemplateParameterType)) {
1272 Invalid = true;
1273 continue;
1274 }
1275
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001276 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001277 if (NewNonTypeParm->hasDefaultArgument() &&
1278 DiagnoseDefaultTemplateArgument(*this, TPC,
1279 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001280 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001281 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001282 }
1283
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001284 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001285 NonTypeTemplateParmDecl *OldNonTypeParm
1286 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001287 if (NewNonTypeParm->isParameterPack()) {
1288 assert(!NewNonTypeParm->hasDefaultArgument() &&
1289 "Parameter packs can't have a default argument!");
1290 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001291 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001292 NewNonTypeParm->hasDefaultArgument()) {
1293 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1294 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1295 SawDefaultArgument = true;
1296 RedundantDefaultArg = true;
1297 PreviousDefaultArgLoc = NewDefaultLoc;
1298 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1299 // Merge the default argument from the old declaration to the
1300 // new declaration.
1301 SawDefaultArgument = true;
1302 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001303 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001304 // parameter.
1305 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001306 OldNonTypeParm->getDefaultArgument(),
1307 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001308 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1309 } else if (NewNonTypeParm->hasDefaultArgument()) {
1310 SawDefaultArgument = true;
1311 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1312 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001313 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001314 } else {
Douglas Gregord684b002009-02-10 19:49:53 +00001315 TemplateTemplateParmDecl *NewTemplateParm
1316 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001317
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001318 // Check for unexpanded parameter packs, recursively.
Douglas Gregor65019ac2011-10-25 03:44:56 +00001319 if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001320 Invalid = true;
1321 continue;
1322 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001323
David Blaikie1368e582011-10-19 05:19:50 +00001324 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001325 if (NewTemplateParm->hasDefaultArgument() &&
1326 DiagnoseDefaultTemplateArgument(*this, TPC,
1327 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001328 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001329 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001330
1331 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001332 TemplateTemplateParmDecl *OldTemplateParm
1333 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001334 if (NewTemplateParm->isParameterPack()) {
1335 assert(!NewTemplateParm->hasDefaultArgument() &&
1336 "Parameter packs can't have a default argument!");
1337 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001338 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001339 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001340 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1341 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001342 SawDefaultArgument = true;
1343 RedundantDefaultArg = true;
1344 PreviousDefaultArgLoc = NewDefaultLoc;
1345 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1346 // Merge the default argument from the old declaration to the
1347 // new declaration.
1348 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001349 // FIXME: We need to create a new kind of "default argument" expression
1350 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001351 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001352 OldTemplateParm->getDefaultArgument(),
1353 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001354 PreviousDefaultArgLoc
1355 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001356 } else if (NewTemplateParm->hasDefaultArgument()) {
1357 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001358 PreviousDefaultArgLoc
1359 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001360 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001361 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001362 }
1363
David Blaikie1368e582011-10-19 05:19:50 +00001364 // C++0x [temp.param]p11:
1365 // If a template parameter of a primary class template or alias template
1366 // is a template parameter pack, it shall be the last template parameter.
1367 if (SawParameterPack && (NewParam + 1) != NewParamEnd &&
1368 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
1369 Diag((*NewParam)->getLocation(),
1370 diag::err_template_param_pack_must_be_last_template_parameter);
1371 Invalid = true;
1372 }
1373
Douglas Gregord684b002009-02-10 19:49:53 +00001374 if (RedundantDefaultArg) {
1375 // C++ [temp.param]p12:
1376 // A template-parameter shall not be given default arguments
1377 // by two different declarations in the same scope.
1378 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1379 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1380 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001381 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001382 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001383 // If a template-parameter of a class template has a default
1384 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001385 // have a default template-argument supplied or be a template parameter
1386 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001387 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001388 diag::err_template_param_default_arg_missing);
1389 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1390 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001391 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001392 }
1393
1394 // If we have an old template parameter list that we're merging
1395 // in, move on to the next parameter.
1396 if (OldParams)
1397 ++OldParam;
1398 }
1399
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001400 // We were missing some default arguments at the end of the list, so remove
1401 // all of the default arguments.
1402 if (RemoveDefaultArguments) {
1403 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1404 NewParamEnd = NewParams->end();
1405 NewParam != NewParamEnd; ++NewParam) {
1406 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1407 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001408 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001409 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1410 NTTP->removeDefaultArgument();
1411 else
1412 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1413 }
1414 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001415
Douglas Gregord684b002009-02-10 19:49:53 +00001416 return Invalid;
1417}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001418
John McCall4e2cbb22010-10-20 05:44:58 +00001419namespace {
1420
1421/// A class which looks for a use of a certain level of template
1422/// parameter.
1423struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1424 typedef RecursiveASTVisitor<DependencyChecker> super;
1425
1426 unsigned Depth;
1427 bool Match;
1428
1429 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1430 NamedDecl *ND = Params->getParam(0);
1431 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1432 Depth = PD->getDepth();
1433 } else if (NonTypeTemplateParmDecl *PD =
1434 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1435 Depth = PD->getDepth();
1436 } else {
1437 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1438 }
1439 }
1440
1441 bool Matches(unsigned ParmDepth) {
1442 if (ParmDepth >= Depth) {
1443 Match = true;
1444 return true;
1445 }
1446 return false;
1447 }
1448
1449 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1450 return !Matches(T->getDepth());
1451 }
1452
1453 bool TraverseTemplateName(TemplateName N) {
1454 if (TemplateTemplateParmDecl *PD =
1455 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1456 if (Matches(PD->getDepth())) return false;
1457 return super::TraverseTemplateName(N);
1458 }
1459
1460 bool VisitDeclRefExpr(DeclRefExpr *E) {
1461 if (NonTypeTemplateParmDecl *PD =
1462 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1463 if (PD->getDepth() == Depth) {
1464 Match = true;
1465 return false;
1466 }
1467 }
1468 return super::VisitDeclRefExpr(E);
1469 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001470
1471 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1472 return TraverseType(T->getInjectedSpecializationType());
1473 }
John McCall4e2cbb22010-10-20 05:44:58 +00001474};
1475}
1476
Douglas Gregorc8406492011-05-10 18:27:06 +00001477/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001478/// list.
1479static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001480DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001481 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001482 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001483 return Checker.Match;
1484}
1485
Douglas Gregorc8406492011-05-10 18:27:06 +00001486// Find the source range corresponding to the named type in the given
1487// nested-name-specifier, if any.
1488static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1489 QualType T,
1490 const CXXScopeSpec &SS) {
1491 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1492 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1493 if (const Type *CurType = NNS->getAsType()) {
1494 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1495 return NNSLoc.getTypeLoc().getSourceRange();
1496 } else
1497 break;
1498
1499 NNSLoc = NNSLoc.getPrefix();
1500 }
1501
1502 return SourceRange();
1503}
1504
Mike Stump1eb44332009-09-09 15:08:12 +00001505/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001506/// specifier, returning the template parameter list that applies to the
1507/// name.
1508///
1509/// \param DeclStartLoc the start of the declaration that has a scope
1510/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001511///
Douglas Gregorc8406492011-05-10 18:27:06 +00001512/// \param DeclLoc The location of the declaration itself.
1513///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001514/// \param SS the scope specifier that will be matched to the given template
1515/// parameter lists. This scope specifier precedes a qualified name that is
1516/// being declared.
1517///
1518/// \param ParamLists the template parameter lists, from the outermost to the
1519/// innermost template parameter lists.
1520///
1521/// \param NumParamLists the number of template parameter lists in ParamLists.
1522///
John McCall77e8b112010-04-13 20:37:33 +00001523/// \param IsFriend Whether to apply the slightly different rules for
1524/// matching template parameters to scope specifiers in friend
1525/// declarations.
1526///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001527/// \param IsExplicitSpecialization will be set true if the entity being
1528/// declared is an explicit specialization, false otherwise.
1529///
Mike Stump1eb44332009-09-09 15:08:12 +00001530/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001531/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001532/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001533/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001534/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001535/// itself a template).
1536TemplateParameterList *
1537Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001538 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001539 const CXXScopeSpec &SS,
1540 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001541 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001542 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001543 bool &IsExplicitSpecialization,
1544 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001545 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001546 Invalid = false;
1547
1548 // The sequence of nested types to which we will match up the template
1549 // parameter lists. We first build this list by starting with the type named
1550 // by the nested-name-specifier and walking out until we run out of types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001551 SmallVector<QualType, 4> NestedTypes;
Douglas Gregorc8406492011-05-10 18:27:06 +00001552 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001553 if (SS.getScopeRep()) {
1554 if (CXXRecordDecl *Record
1555 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1556 T = Context.getTypeDeclType(Record);
1557 else
1558 T = QualType(SS.getScopeRep()->getAsType(), 0);
1559 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001560
1561 // If we found an explicit specialization that prevents us from needing
1562 // 'template<>' headers, this will be set to the location of that
1563 // explicit specialization.
1564 SourceLocation ExplicitSpecLoc;
1565
1566 while (!T.isNull()) {
1567 NestedTypes.push_back(T);
1568
1569 // Retrieve the parent of a record type.
1570 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1571 // If this type is an explicit specialization, we're done.
1572 if (ClassTemplateSpecializationDecl *Spec
1573 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1574 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1575 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1576 ExplicitSpecLoc = Spec->getLocation();
1577 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001578 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001579 } else if (Record->getTemplateSpecializationKind()
1580 == TSK_ExplicitSpecialization) {
1581 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001582 break;
1583 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001584
1585 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1586 T = Context.getTypeDeclType(Parent);
1587 else
1588 T = QualType();
1589 continue;
1590 }
1591
1592 if (const TemplateSpecializationType *TST
1593 = T->getAs<TemplateSpecializationType>()) {
1594 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1595 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1596 T = Context.getTypeDeclType(Parent);
1597 else
1598 T = QualType();
1599 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001600 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001601 }
1602
1603 // Look one step prior in a dependent template specialization type.
1604 if (const DependentTemplateSpecializationType *DependentTST
1605 = T->getAs<DependentTemplateSpecializationType>()) {
1606 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1607 T = QualType(NNS->getAsType(), 0);
1608 else
1609 T = QualType();
1610 continue;
1611 }
1612
1613 // Look one step prior in a dependent name type.
1614 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1615 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1616 T = QualType(NNS->getAsType(), 0);
1617 else
1618 T = QualType();
1619 continue;
1620 }
1621
1622 // Retrieve the parent of an enumeration type.
1623 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1624 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1625 // check here.
1626 EnumDecl *Enum = EnumT->getDecl();
1627
1628 // Get to the parent type.
1629 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1630 T = Context.getTypeDeclType(Parent);
1631 else
1632 T = QualType();
1633 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001634 }
Mike Stump1eb44332009-09-09 15:08:12 +00001635
Douglas Gregorc8406492011-05-10 18:27:06 +00001636 T = QualType();
1637 }
1638 // Reverse the nested types list, since we want to traverse from the outermost
1639 // to the innermost while checking template-parameter-lists.
1640 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001641
Douglas Gregorc8406492011-05-10 18:27:06 +00001642 // C++0x [temp.expl.spec]p17:
1643 // A member or a member template may be nested within many
1644 // enclosing class templates. In an explicit specialization for
1645 // such a member, the member declaration shall be preceded by a
1646 // template<> for each enclosing class template that is
1647 // explicitly specialized.
Douglas Gregor89b9f102011-06-06 15:22:55 +00001648 bool SawNonEmptyTemplateParameterList = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001649 unsigned ParamIdx = 0;
1650 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1651 ++TypeIdx) {
1652 T = NestedTypes[TypeIdx];
1653
1654 // Whether we expect a 'template<>' header.
1655 bool NeedEmptyTemplateHeader = false;
1656
1657 // Whether we expect a template header with parameters.
1658 bool NeedNonemptyTemplateHeader = false;
1659
1660 // For a dependent type, the set of template parameters that we
1661 // expect to see.
1662 TemplateParameterList *ExpectedTemplateParams = 0;
1663
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001664 // C++0x [temp.expl.spec]p15:
1665 // A member or a member template may be nested within many enclosing
1666 // class templates. In an explicit specialization for such a member, the
1667 // member declaration shall be preceded by a template<> for each
1668 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001669 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1670 if (ClassTemplatePartialSpecializationDecl *Partial
1671 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1672 ExpectedTemplateParams = Partial->getTemplateParameters();
1673 NeedNonemptyTemplateHeader = true;
1674 } else if (Record->isDependentType()) {
1675 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001676 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001677 ->getTemplateParameters();
1678 NeedNonemptyTemplateHeader = true;
1679 }
1680 } else if (ClassTemplateSpecializationDecl *Spec
1681 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1682 // C++0x [temp.expl.spec]p4:
1683 // Members of an explicitly specialized class template are defined
1684 // in the same manner as members of normal classes, and not using
1685 // the template<> syntax.
1686 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1687 NeedEmptyTemplateHeader = true;
1688 else
Douglas Gregor95ea4502011-06-01 22:37:07 +00001689 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001690 } else if (Record->getTemplateSpecializationKind()) {
1691 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001692 != TSK_ExplicitSpecialization &&
1693 TypeIdx == NumTypes - 1)
1694 IsExplicitSpecialization = true;
1695
1696 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001697 }
1698 } else if (const TemplateSpecializationType *TST
1699 = T->getAs<TemplateSpecializationType>()) {
1700 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1701 ExpectedTemplateParams = Template->getTemplateParameters();
1702 NeedNonemptyTemplateHeader = true;
1703 }
1704 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1705 // FIXME: We actually could/should check the template arguments here
1706 // against the corresponding template parameter list.
1707 NeedNonemptyTemplateHeader = false;
1708 }
1709
Douglas Gregor89b9f102011-06-06 15:22:55 +00001710 // C++ [temp.expl.spec]p16:
1711 // In an explicit specialization declaration for a member of a class
1712 // template or a member template that ap- pears in namespace scope, the
1713 // member template and some of its enclosing class templates may remain
1714 // unspecialized, except that the declaration shall not explicitly
1715 // specialize a class member template if its en- closing class templates
1716 // are not explicitly specialized as well.
1717 if (ParamIdx < NumParamLists) {
1718 if (ParamLists[ParamIdx]->size() == 0) {
1719 if (SawNonEmptyTemplateParameterList) {
1720 Diag(DeclLoc, diag::err_specialize_member_of_template)
1721 << ParamLists[ParamIdx]->getSourceRange();
1722 Invalid = true;
1723 IsExplicitSpecialization = false;
1724 return 0;
1725 }
1726 } else
1727 SawNonEmptyTemplateParameterList = true;
1728 }
1729
Douglas Gregorc8406492011-05-10 18:27:06 +00001730 if (NeedEmptyTemplateHeader) {
1731 // If we're on the last of the types, and we need a 'template<>' header
1732 // here, then it's an explicit specialization.
1733 if (TypeIdx == NumTypes - 1)
1734 IsExplicitSpecialization = true;
1735
1736 if (ParamIdx < NumParamLists) {
1737 if (ParamLists[ParamIdx]->size() > 0) {
1738 // The header has template parameters when it shouldn't. Complain.
1739 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1740 diag::err_template_param_list_matches_nontemplate)
1741 << T
1742 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1743 ParamLists[ParamIdx]->getRAngleLoc())
1744 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1745 Invalid = true;
1746 return 0;
1747 }
1748
1749 // Consume this template header.
1750 ++ParamIdx;
1751 continue;
1752 }
1753
1754 if (!IsFriend) {
1755 // We don't have a template header, but we should.
1756 SourceLocation ExpectedTemplateLoc;
1757 if (NumParamLists > 0)
1758 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1759 else
1760 ExpectedTemplateLoc = DeclStartLoc;
1761
1762 Diag(DeclLoc, diag::err_template_spec_needs_header)
1763 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1764 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1765 }
1766
1767 continue;
1768 }
1769
1770 if (NeedNonemptyTemplateHeader) {
1771 // In friend declarations we can have template-ids which don't
1772 // depend on the corresponding template parameter lists. But
1773 // assume that empty parameter lists are supposed to match this
1774 // template-id.
1775 if (IsFriend && T->isDependentType()) {
1776 if (ParamIdx < NumParamLists &&
1777 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1778 ExpectedTemplateParams = 0;
1779 else
1780 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001781 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001782
Douglas Gregorc8406492011-05-10 18:27:06 +00001783 if (ParamIdx < NumParamLists) {
1784 // Check the template parameter list, if we can.
1785 if (ExpectedTemplateParams &&
1786 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1787 ExpectedTemplateParams,
1788 true, TPL_TemplateMatch))
1789 Invalid = true;
1790
1791 if (!Invalid &&
1792 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1793 TPC_ClassTemplateMember))
1794 Invalid = true;
1795
1796 ++ParamIdx;
1797 continue;
1798 }
1799
1800 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1801 << T
1802 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1803 Invalid = true;
1804 continue;
1805 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001806 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001807
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001808 // If there were at least as many template-ids as there were template
1809 // parameter lists, then there are no template parameter lists remaining for
1810 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001811 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001812 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001813
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001814 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001815 if (ParamIdx < NumParamLists - 1) {
1816 bool HasAnyExplicitSpecHeader = false;
1817 bool AllExplicitSpecHeaders = true;
1818 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1819 if (ParamLists[I]->size() == 0)
1820 HasAnyExplicitSpecHeader = true;
1821 else
1822 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001823 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001824
1825 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1826 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1827 : diag::err_template_spec_extra_headers)
1828 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1829 ParamLists[NumParamLists - 2]->getRAngleLoc());
1830
1831 // If there was a specialization somewhere, such that 'template<>' is
1832 // not required, and there were any 'template<>' headers, note where the
1833 // specialization occurred.
1834 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1835 Diag(ExplicitSpecLoc,
1836 diag::note_explicit_template_spec_does_not_need_header)
1837 << NestedTypes.back();
1838
1839 // We have a template parameter list with no corresponding scope, which
1840 // means that the resulting template declaration can't be instantiated
1841 // properly (we'll end up with dependent nodes when we shouldn't).
1842 if (!AllExplicitSpecHeaders)
1843 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001844 }
Mike Stump1eb44332009-09-09 15:08:12 +00001845
Douglas Gregor89b9f102011-06-06 15:22:55 +00001846 // C++ [temp.expl.spec]p16:
1847 // In an explicit specialization declaration for a member of a class
1848 // template or a member template that ap- pears in namespace scope, the
1849 // member template and some of its enclosing class templates may remain
1850 // unspecialized, except that the declaration shall not explicitly
1851 // specialize a class member template if its en- closing class templates
1852 // are not explicitly specialized as well.
1853 if (ParamLists[NumParamLists - 1]->size() == 0 &&
1854 SawNonEmptyTemplateParameterList) {
1855 Diag(DeclLoc, diag::err_specialize_member_of_template)
1856 << ParamLists[ParamIdx]->getSourceRange();
1857 Invalid = true;
1858 IsExplicitSpecialization = false;
1859 return 0;
1860 }
1861
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001862 // Return the last template parameter list, which corresponds to the
1863 // entity being declared.
1864 return ParamLists[NumParamLists - 1];
1865}
1866
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001867void Sema::NoteAllFoundTemplates(TemplateName Name) {
1868 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1869 Diag(Template->getLocation(), diag::note_template_declared_here)
1870 << (isa<FunctionTemplateDecl>(Template)? 0
1871 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001872 : isa<TypeAliasTemplateDecl>(Template)? 2
1873 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001874 << Template->getDeclName();
1875 return;
1876 }
1877
1878 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1879 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1880 IEnd = OST->end();
1881 I != IEnd; ++I)
1882 Diag((*I)->getLocation(), diag::note_template_declared_here)
1883 << 0 << (*I)->getDeclName();
1884
1885 return;
1886 }
1887}
1888
1889
Douglas Gregor7532dc62009-03-30 22:58:21 +00001890QualType Sema::CheckTemplateIdType(TemplateName Name,
1891 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001892 TemplateArgumentListInfo &TemplateArgs) {
John McCall14606042011-06-30 08:33:18 +00001893 DependentTemplateName *DTN
1894 = Name.getUnderlying().getAsDependentTemplateName();
Richard Smith3e4c6c42011-05-05 21:57:07 +00001895 if (DTN && DTN->isIdentifier())
1896 // When building a template-id where the template-name is dependent,
1897 // assume the template is a type template. Either our assumption is
1898 // correct, or the code is ill-formed and will be diagnosed when the
1899 // dependent name is substituted.
1900 return Context.getDependentTemplateSpecializationType(ETK_None,
1901 DTN->getQualifier(),
1902 DTN->getIdentifier(),
1903 TemplateArgs);
1904
Douglas Gregor7532dc62009-03-30 22:58:21 +00001905 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001906 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1907 // We might have a substituted template template parameter pack. If so,
1908 // build a template specialization type for it.
1909 if (Name.getAsSubstTemplateTemplateParmPack())
1910 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001911
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001912 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1913 << Name;
1914 NoteAllFoundTemplates(Name);
1915 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001916 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001917
Douglas Gregor40808ce2009-03-09 23:48:35 +00001918 // Check that the template argument list is well-formed for this
1919 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001920 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00001921 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00001922 false, Converted))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001923 return QualType();
1924
Douglas Gregor910f8002010-11-07 23:05:16 +00001925 assert((Converted.size() == Template->getTemplateParameters()->size()) &&
Douglas Gregor40808ce2009-03-09 23:48:35 +00001926 "Converted template argument list is too short!");
1927
1928 QualType CanonType;
1929
Douglas Gregor561f8122011-07-01 01:22:09 +00001930 bool InstantiationDependent = false;
Richard Smith3e4c6c42011-05-05 21:57:07 +00001931 if (TypeAliasTemplateDecl *AliasTemplate
1932 = dyn_cast<TypeAliasTemplateDecl>(Template)) {
1933 // Find the canonical type for this type alias template specialization.
1934 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1935 if (Pattern->isInvalidDecl())
1936 return QualType();
1937
1938 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1939 Converted.data(), Converted.size());
1940
1941 // Only substitute for the innermost template argument list.
1942 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001943 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001944 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1945 for (unsigned I = 0; I < Depth; ++I)
1946 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001947
1948 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1949 CanonType = SubstType(Pattern->getUnderlyingType(),
1950 TemplateArgLists, AliasTemplate->getLocation(),
1951 AliasTemplate->getDeclName());
1952 if (CanonType.isNull())
1953 return QualType();
1954 } else if (Name.isDependent() ||
1955 TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregor561f8122011-07-01 01:22:09 +00001956 TemplateArgs, InstantiationDependent)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001957 // This class template specialization is a dependent
1958 // type. Therefore, its canonical type is another class template
1959 // specialization type that contains all of the converted
1960 // arguments in canonical form. This ensures that, e.g., A<T> and
1961 // A<T, T> have identical types when A is declared as:
1962 //
1963 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001964 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001965 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001966 Converted.data(),
1967 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001968
Douglas Gregor1275ae02009-07-28 23:00:59 +00001969 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001970 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001971 // In the future, we need to teach getTemplateSpecializationType to only
1972 // build the canonical type and return that to us.
1973 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001974
1975 // This might work out to be a current instantiation, in which
1976 // case the canonical type needs to be the InjectedClassNameType.
1977 //
1978 // TODO: in theory this could be a simple hashtable lookup; most
1979 // changes to CurContext don't change the set of current
1980 // instantiations.
1981 if (isa<ClassTemplateDecl>(Template)) {
1982 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1983 // If we get out to a namespace, we're done.
1984 if (Ctx->isFileContext()) break;
1985
1986 // If this isn't a record, keep looking.
1987 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1988 if (!Record) continue;
1989
1990 // Look for one of the two cases with InjectedClassNameTypes
1991 // and check whether it's the same template.
1992 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1993 !Record->getDescribedClassTemplate())
1994 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001995
John McCall31f17ec2010-04-27 00:57:59 +00001996 // Fetch the injected class name type and check whether its
1997 // injected type is equal to the type we just built.
1998 QualType ICNT = Context.getTypeDeclType(Record);
1999 QualType Injected = cast<InjectedClassNameType>(ICNT)
2000 ->getInjectedSpecializationType();
2001
2002 if (CanonType != Injected->getCanonicalTypeInternal())
2003 continue;
2004
2005 // If so, the canonical type of this TST is the injected
2006 // class name type of the record we just found.
2007 assert(ICNT.isCanonical());
2008 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00002009 break;
2010 }
2011 }
Mike Stump1eb44332009-09-09 15:08:12 +00002012 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00002013 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00002014 // Find the class template specialization declaration that
2015 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00002016 void *InsertPos = 0;
2017 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002018 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002019 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002020 if (!Decl) {
2021 // This is the first time we have referenced this class template
2022 // specialization. Create the canonical declaration and add it to
2023 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00002024 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002025 ClassTemplate->getTemplatedDecl()->getTagKind(),
2026 ClassTemplate->getDeclContext(),
Abramo Bagnara09d82122011-10-03 20:34:03 +00002027 ClassTemplate->getTemplatedDecl()->getLocStart(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002028 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002029 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002030 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002031 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002032 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002033 Decl->setLexicalDeclContext(CurContext);
2034 }
2035
2036 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00002037 assert(isa<RecordType>(CanonType) &&
2038 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00002039 }
Mike Stump1eb44332009-09-09 15:08:12 +00002040
Douglas Gregor40808ce2009-03-09 23:48:35 +00002041 // Build the fully-sugared type for this class template
2042 // specialization, which refers back to the class template
2043 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00002044 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002045}
2046
John McCallf312b1e2010-08-26 23:41:50 +00002047TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00002048Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
2049 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002050 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002051 ASTTemplateArgsPtr TemplateArgsIn,
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002052 SourceLocation RAngleLoc,
2053 bool IsCtorOrDtorName) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002054 if (SS.isInvalid())
2055 return true;
2056
Douglas Gregor7532dc62009-03-30 22:58:21 +00002057 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00002058
Douglas Gregor40808ce2009-03-09 23:48:35 +00002059 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00002060 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00002061 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002062
Douglas Gregora88f09f2011-02-28 17:23:35 +00002063 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002064 QualType T
2065 = Context.getDependentTemplateSpecializationType(ETK_None,
2066 DTN->getQualifier(),
2067 DTN->getIdentifier(),
2068 TemplateArgs);
2069 // Build type-source information.
Douglas Gregora88f09f2011-02-28 17:23:35 +00002070 TypeLocBuilder TLB;
2071 DependentTemplateSpecializationTypeLoc SpecTL
2072 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00002073 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00002074 SpecTL.setNameLoc(TemplateLoc);
2075 SpecTL.setLAngleLoc(LAngleLoc);
2076 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00002077 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00002078 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2079 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2080 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2081 }
2082
John McCalld5532b62009-11-23 01:53:49 +00002083 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002084 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002085
2086 if (Result.isNull())
2087 return true;
2088
Douglas Gregor059101f2011-03-02 00:47:37 +00002089 // Build type-source information.
2090 TypeLocBuilder TLB;
2091 TemplateSpecializationTypeLoc SpecTL
2092 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2093 SpecTL.setTemplateNameLoc(TemplateLoc);
2094 SpecTL.setLAngleLoc(LAngleLoc);
2095 SpecTL.setRAngleLoc(RAngleLoc);
2096 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2097 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002098
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002099 // NOTE: avoid constructing an ElaboratedTypeLoc if this is a
2100 // constructor or destructor name (in such a case, the scope specifier
2101 // will be attached to the enclosing Decl or Expr node).
2102 if (SS.isNotEmpty() && !IsCtorOrDtorName) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002103 // Create an elaborated-type-specifier containing the nested-name-specifier.
2104 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2105 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2106 ElabTL.setKeywordLoc(SourceLocation());
2107 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2108 }
2109
2110 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002111}
John McCallf1bbbb42009-09-04 01:14:41 +00002112
Douglas Gregor059101f2011-03-02 00:47:37 +00002113TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002114 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002115 SourceLocation TagLoc,
2116 CXXScopeSpec &SS,
2117 TemplateTy TemplateD,
2118 SourceLocation TemplateLoc,
2119 SourceLocation LAngleLoc,
2120 ASTTemplateArgsPtr TemplateArgsIn,
2121 SourceLocation RAngleLoc) {
2122 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2123
2124 // Translate the parser's template argument list in our AST format.
2125 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2126 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2127
2128 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002129 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002130 ElaboratedTypeKeyword Keyword
2131 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002132
Douglas Gregor059101f2011-03-02 00:47:37 +00002133 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2134 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2135 DTN->getQualifier(),
2136 DTN->getIdentifier(),
2137 TemplateArgs);
2138
2139 // Build type-source information.
2140 TypeLocBuilder TLB;
2141 DependentTemplateSpecializationTypeLoc SpecTL
2142 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2143 SpecTL.setKeywordLoc(TagLoc);
2144 SpecTL.setNameLoc(TemplateLoc);
2145 SpecTL.setLAngleLoc(LAngleLoc);
2146 SpecTL.setRAngleLoc(RAngleLoc);
2147 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
2148 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2149 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2150 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2151 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002152
2153 if (TypeAliasTemplateDecl *TAT =
2154 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2155 // C++0x [dcl.type.elab]p2:
2156 // If the identifier resolves to a typedef-name or the simple-template-id
2157 // resolves to an alias template specialization, the
2158 // elaborated-type-specifier is ill-formed.
2159 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2160 Diag(TAT->getLocation(), diag::note_declared_at);
2161 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002162
2163 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2164 if (Result.isNull())
Matt Beaumont-Gay3a51d412011-08-25 23:22:24 +00002165 return TypeResult(true);
Douglas Gregor059101f2011-03-02 00:47:37 +00002166
2167 // Check the tag kind
2168 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002169 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002170
John McCall6b2becf2009-09-08 17:47:29 +00002171 IdentifierInfo *Id = D->getIdentifier();
2172 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002173
Richard Trieubbf34c02011-06-10 03:11:26 +00002174 if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
2175 TagLoc, *Id)) {
John McCall6b2becf2009-09-08 17:47:29 +00002176 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002177 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002178 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002179 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002180 }
2181 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002182
2183 // Provide source-location information for the template specialization.
2184 TypeLocBuilder TLB;
2185 TemplateSpecializationTypeLoc SpecTL
2186 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2187 SpecTL.setTemplateNameLoc(TemplateLoc);
2188 SpecTL.setLAngleLoc(LAngleLoc);
2189 SpecTL.setRAngleLoc(RAngleLoc);
2190 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2191 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002192
Douglas Gregor059101f2011-03-02 00:47:37 +00002193 // Construct an elaborated type containing the nested-name-specifier (if any)
2194 // and keyword.
2195 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2196 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2197 ElabTL.setKeywordLoc(TagLoc);
2198 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2199 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002200}
2201
John McCall60d7b3a2010-08-24 06:29:42 +00002202ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002203 SourceLocation TemplateKWLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002204 LookupResult &R,
2205 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00002206 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002207 // FIXME: Can we do any checking at this point? I guess we could check the
2208 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002209 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002210 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002211 // foo<int> could identify a single function unambiguously
2212 // This approach does NOT work, since f<int>(1);
2213 // gets resolved prior to resorting to overload resolution
2214 // i.e., template<class T> void f(double);
2215 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002216
2217 // These should be filtered out by our callers.
2218 assert(!R.empty() && "empty lookup results when building templateid");
2219 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2220
John McCallc373d482010-01-27 01:50:18 +00002221 // We don't want lookup warnings at this point.
2222 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002223
John McCallf7a1a742009-11-24 19:00:30 +00002224 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002225 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002226 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002227 TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002228 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002229 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002230 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002231
2232 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002233}
2234
John McCallf7a1a742009-11-24 19:00:30 +00002235// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002236ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002237Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002238 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002239 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00002240 const TemplateArgumentListInfo &TemplateArgs) {
2241 DeclContext *DC;
2242 if (!(DC = computeDeclContext(SS, false)) ||
2243 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002244 RequireCompleteDeclContext(SS, DC))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002245 return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo,
2246 &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002247
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002248 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002249 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002250 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2251 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002252
John McCallf7a1a742009-11-24 19:00:30 +00002253 if (R.isAmbiguous())
2254 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002255
John McCallf7a1a742009-11-24 19:00:30 +00002256 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002257 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2258 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002259 return ExprError();
2260 }
2261
2262 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002263 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2264 << (NestedNameSpecifier*) SS.getScopeRep()
2265 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002266 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2267 return ExprError();
2268 }
2269
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002270 return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002271}
2272
Douglas Gregorc45c2322009-03-31 00:43:58 +00002273/// \brief Form a dependent template name.
2274///
2275/// This action forms a dependent template name given the template
2276/// name and its (presumably dependent) scope specifier. For
2277/// example, given "MetaFun::template apply", the scope specifier \p
2278/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2279/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002280TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002281 CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002282 SourceLocation TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002283 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002284 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002285 bool EnteringContext,
2286 TemplateTy &Result) {
Richard Smithebaf0e62011-10-18 20:49:44 +00002287 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent())
2288 Diag(TemplateKWLoc,
2289 getLangOptions().CPlusPlus0x ?
2290 diag::warn_cxx98_compat_template_outside_of_template :
2291 diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002292 << FixItHint::CreateRemoval(TemplateKWLoc);
2293
Douglas Gregor0707bc52010-01-19 16:01:07 +00002294 DeclContext *LookupCtx = 0;
2295 if (SS.isSet())
2296 LookupCtx = computeDeclContext(SS, EnteringContext);
2297 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002298 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002299 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002300 // C++0x [temp.names]p5:
2301 // If a name prefixed by the keyword template is not the name of
2302 // a template, the program is ill-formed. [Note: the keyword
2303 // template may not be applied to non-template members of class
2304 // templates. -end note ] [ Note: as is the case with the
2305 // typename prefix, the template prefix is allowed in cases
2306 // where it is not strictly necessary; i.e., when the
2307 // nested-name-specifier or the expression on the left of the ->
2308 // or . is not dependent on a template-parameter, or the use
2309 // does not appear in the scope of a template. -end note]
2310 //
2311 // Note: C++03 was more strict here, because it banned the use of
2312 // the "template" keyword prior to a template-name that was not a
2313 // dependent name. C++ DR468 relaxed this requirement (the
2314 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002315 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002316 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002317 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2318 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002319 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002320 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2321 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002322 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2323 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002324 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002325 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002326 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002327 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002328 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002329 << Name.getSourceRange()
2330 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002331 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002332 } else {
2333 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002334 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002335 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002336 }
2337
Mike Stump1eb44332009-09-09 15:08:12 +00002338 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002339 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002340
Douglas Gregor014e88d2009-11-03 23:16:33 +00002341 switch (Name.getKind()) {
2342 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002343 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002344 Name.Identifier));
2345 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002346
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002347 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002348 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002349 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002350 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002351
2352 case UnqualifiedId::IK_LiteralOperatorId:
David Blaikieb219cfc2011-09-23 05:06:16 +00002353 llvm_unreachable(
2354 "We don't support these; Parse shouldn't have allowed propagation");
Sean Hunte6252d12009-11-28 08:58:14 +00002355
Douglas Gregor014e88d2009-11-03 23:16:33 +00002356 default:
2357 break;
2358 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002359
2360 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002361 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002362 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002363 << Name.getSourceRange()
2364 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002365 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002366}
2367
Mike Stump1eb44332009-09-09 15:08:12 +00002368bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002369 const TemplateArgumentLoc &AL,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002370 SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002371 const TemplateArgument &Arg = AL.getArgument();
2372
Anders Carlsson436b1562009-06-13 00:33:33 +00002373 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002374 switch(Arg.getKind()) {
2375 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002376 // C++ [temp.arg.type]p1:
2377 // A template-argument for a template-parameter which is a
2378 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002379 break;
2380 case TemplateArgument::Template: {
2381 // We have a template type parameter but the template argument
2382 // is a template without any arguments.
2383 SourceRange SR = AL.getSourceRange();
2384 TemplateName Name = Arg.getAsTemplate();
2385 Diag(SR.getBegin(), diag::err_template_missing_args)
2386 << Name << SR;
2387 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2388 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002389
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002390 return true;
2391 }
2392 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002393 // We have a template type parameter but the template argument
2394 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002395 SourceRange SR = AL.getSourceRange();
2396 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002397 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002398
Anders Carlsson436b1562009-06-13 00:33:33 +00002399 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002400 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002401 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002402
John McCalla93c9342009-12-07 02:54:59 +00002403 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002404 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002405
Anders Carlsson436b1562009-06-13 00:33:33 +00002406 // Add the converted template type argument.
Douglas Gregore559ca12011-06-17 22:11:49 +00002407 QualType ArgType = Context.getCanonicalType(Arg.getAsType());
2408
2409 // Objective-C ARC:
2410 // If an explicitly-specified template argument type is a lifetime type
2411 // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
2412 if (getLangOptions().ObjCAutoRefCount &&
2413 ArgType->isObjCLifetimeType() &&
2414 !ArgType.getObjCLifetime()) {
2415 Qualifiers Qs;
2416 Qs.setObjCLifetime(Qualifiers::OCL_Strong);
2417 ArgType = Context.getQualifiedType(ArgType, Qs);
2418 }
2419
2420 Converted.push_back(TemplateArgument(ArgType));
Anders Carlsson436b1562009-06-13 00:33:33 +00002421 return false;
2422}
2423
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002424/// \brief Substitute template arguments into the default template argument for
2425/// the given template type parameter.
2426///
2427/// \param SemaRef the semantic analysis object for which we are performing
2428/// the substitution.
2429///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002430/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002431/// for.
2432///
2433/// \param TemplateLoc the location of the template name that started the
2434/// template-id we are checking.
2435///
2436/// \param RAngleLoc the location of the right angle bracket ('>') that
2437/// terminates the template-id.
2438///
2439/// \param Param the template template parameter whose default we are
2440/// substituting into.
2441///
2442/// \param Converted the list of template arguments provided for template
2443/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002444/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002445static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002446SubstDefaultTemplateArgument(Sema &SemaRef,
2447 TemplateDecl *Template,
2448 SourceLocation TemplateLoc,
2449 SourceLocation RAngleLoc,
2450 TemplateTypeParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002451 SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002452 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002453
2454 // If the argument type is dependent, instantiate it now based
2455 // on the previously-computed template arguments.
2456 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002457 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002458 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002459
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002460 MultiLevelTemplateArgumentList AllTemplateArgs
2461 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2462
2463 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002464 Template, Converted.data(),
2465 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002466 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002467
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002468 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2469 Param->getDefaultArgumentLoc(),
2470 Param->getDeclName());
2471 }
2472
2473 return ArgType;
2474}
2475
2476/// \brief Substitute template arguments into the default template argument for
2477/// the given non-type template parameter.
2478///
2479/// \param SemaRef the semantic analysis object for which we are performing
2480/// the substitution.
2481///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002482/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002483/// for.
2484///
2485/// \param TemplateLoc the location of the template name that started the
2486/// template-id we are checking.
2487///
2488/// \param RAngleLoc the location of the right angle bracket ('>') that
2489/// terminates the template-id.
2490///
Douglas Gregor788cd062009-11-11 01:00:40 +00002491/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002492/// substituting into.
2493///
2494/// \param Converted the list of template arguments provided for template
2495/// parameters that precede \p Param in the template parameter list.
2496///
2497/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002498static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002499SubstDefaultTemplateArgument(Sema &SemaRef,
2500 TemplateDecl *Template,
2501 SourceLocation TemplateLoc,
2502 SourceLocation RAngleLoc,
2503 NonTypeTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002504 SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002505 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002506 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002507
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002508 MultiLevelTemplateArgumentList AllTemplateArgs
2509 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002510
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002511 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002512 Template, Converted.data(),
2513 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002514 SourceRange(TemplateLoc, RAngleLoc));
2515
2516 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2517}
2518
Douglas Gregor788cd062009-11-11 01:00:40 +00002519/// \brief Substitute template arguments into the default template argument for
2520/// the given template template parameter.
2521///
2522/// \param SemaRef the semantic analysis object for which we are performing
2523/// the substitution.
2524///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002525/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002526/// for.
2527///
2528/// \param TemplateLoc the location of the template name that started the
2529/// template-id we are checking.
2530///
2531/// \param RAngleLoc the location of the right angle bracket ('>') that
2532/// terminates the template-id.
2533///
2534/// \param Param the template template parameter whose default we are
2535/// substituting into.
2536///
2537/// \param Converted the list of template arguments provided for template
2538/// parameters that precede \p Param in the template parameter list.
2539///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002540/// \param QualifierLoc Will be set to the nested-name-specifier (with
2541/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002542///
Douglas Gregor788cd062009-11-11 01:00:40 +00002543/// \returns the substituted template argument, or NULL if an error occurred.
2544static TemplateName
2545SubstDefaultTemplateArgument(Sema &SemaRef,
2546 TemplateDecl *Template,
2547 SourceLocation TemplateLoc,
2548 SourceLocation RAngleLoc,
2549 TemplateTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002550 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002551 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002552 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002553 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002554
Douglas Gregor788cd062009-11-11 01:00:40 +00002555 MultiLevelTemplateArgumentList AllTemplateArgs
2556 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002557
Douglas Gregor788cd062009-11-11 01:00:40 +00002558 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002559 Template, Converted.data(),
2560 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002561 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002562
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002563 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002564 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002565 if (QualifierLoc) {
2566 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2567 AllTemplateArgs);
2568 if (!QualifierLoc)
2569 return TemplateName();
2570 }
2571
Douglas Gregor1d752d72011-03-02 18:46:51 +00002572 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002573 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002574 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002575 AllTemplateArgs);
2576}
2577
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002578/// \brief If the given template parameter has a default template
2579/// argument, substitute into that default template argument and
2580/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002581TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002582Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2583 SourceLocation TemplateLoc,
2584 SourceLocation RAngleLoc,
2585 Decl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002586 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002587 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002588 if (!TypeParm->hasDefaultArgument())
2589 return TemplateArgumentLoc();
2590
John McCalla93c9342009-12-07 02:54:59 +00002591 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002592 TemplateLoc,
2593 RAngleLoc,
2594 TypeParm,
2595 Converted);
2596 if (DI)
2597 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2598
2599 return TemplateArgumentLoc();
2600 }
2601
2602 if (NonTypeTemplateParmDecl *NonTypeParm
2603 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2604 if (!NonTypeParm->hasDefaultArgument())
2605 return TemplateArgumentLoc();
2606
John McCall60d7b3a2010-08-24 06:29:42 +00002607 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002608 TemplateLoc,
2609 RAngleLoc,
2610 NonTypeParm,
2611 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002612 if (Arg.isInvalid())
2613 return TemplateArgumentLoc();
2614
2615 Expr *ArgE = Arg.takeAs<Expr>();
2616 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2617 }
2618
2619 TemplateTemplateParmDecl *TempTempParm
2620 = cast<TemplateTemplateParmDecl>(Param);
2621 if (!TempTempParm->hasDefaultArgument())
2622 return TemplateArgumentLoc();
2623
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002624
Douglas Gregor1d752d72011-03-02 18:46:51 +00002625 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002626 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002627 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002628 RAngleLoc,
2629 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002630 Converted,
2631 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002632 if (TName.isNull())
2633 return TemplateArgumentLoc();
2634
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002635 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002636 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002637 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2638}
2639
Douglas Gregore7526412009-11-11 19:31:23 +00002640/// \brief Check that the given template argument corresponds to the given
2641/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002642///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002643/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002644/// checked.
2645///
2646/// \param Arg The template argument.
2647///
2648/// \param Template The template in which the template argument resides.
2649///
2650/// \param TemplateLoc The location of the template name for the template
2651/// whose argument list we're matching.
2652///
2653/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2654/// the template argument list.
2655///
2656/// \param ArgumentPackIndex The index into the argument pack where this
2657/// argument will be placed. Only valid if the parameter is a parameter pack.
2658///
2659/// \param Converted The checked, converted argument will be added to the
2660/// end of this small vector.
2661///
2662/// \param CTAK Describes how we arrived at this particular template argument:
2663/// explicitly written, deduced, etc.
2664///
2665/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002666bool Sema::CheckTemplateArgument(NamedDecl *Param,
2667 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002668 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002669 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002670 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002671 unsigned ArgumentPackIndex,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002672 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002673 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002674 // Check template type parameters.
2675 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002676 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002677
Douglas Gregord9e15302009-11-11 19:41:09 +00002678 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002679 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002680 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002681 // with the template arguments we've seen thus far. But if the
2682 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002683 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002684 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2685 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002686
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002687 if (NTTPType->isDependentType() &&
2688 !isa<TemplateTemplateParmDecl>(Template) &&
2689 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002690 // Do substitution on the type of the non-type template parameter.
2691 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002692 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002693 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002694
2695 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002696 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002697 NTTPType = SubstType(NTTPType,
2698 MultiLevelTemplateArgumentList(TemplateArgs),
2699 NTTP->getLocation(),
2700 NTTP->getDeclName());
2701 // If that worked, check the non-type template parameter type
2702 // for validity.
2703 if (!NTTPType.isNull())
2704 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2705 NTTP->getLocation());
2706 if (NTTPType.isNull())
2707 return true;
2708 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002709
Douglas Gregore7526412009-11-11 19:31:23 +00002710 switch (Arg.getArgument().getKind()) {
2711 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002712 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002713
Douglas Gregore7526412009-11-11 19:31:23 +00002714 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002715 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002716 ExprResult Res =
2717 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2718 Result, CTAK);
2719 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002720 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002721
Douglas Gregor910f8002010-11-07 23:05:16 +00002722 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002723 break;
2724 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002725
Douglas Gregore7526412009-11-11 19:31:23 +00002726 case TemplateArgument::Declaration:
2727 case TemplateArgument::Integral:
2728 // We've already checked this template argument, so just copy
2729 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002730 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002731 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002732
Douglas Gregore7526412009-11-11 19:31:23 +00002733 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002734 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002735 // We were given a template template argument. It may not be ill-formed;
2736 // see below.
2737 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002738 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2739 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002740 // We have a template argument such as \c T::template X, which we
2741 // parsed as a template template argument. However, since we now
2742 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002743 // template name into an expression.
2744
2745 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2746 Arg.getTemplateNameLoc());
2747
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002748 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002749 SS.Adopt(Arg.getTemplateQualifierLoc());
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002750 // FIXME: the template-template arg was a DependentTemplateName,
2751 // so it was provided with a template keyword. However, its source
2752 // location is not stored in the template argument structure.
2753 SourceLocation TemplateKWLoc;
John Wiegley429bb272011-04-08 18:41:53 +00002754 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002755 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002756 TemplateKWLoc,
2757 NameInfo, 0));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002758
Douglas Gregora7fc9012011-01-05 18:58:31 +00002759 // If we parsed the template argument as a pack expansion, create a
2760 // pack expansion expression.
2761 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002762 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2763 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002764 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002765 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002766
Douglas Gregore7526412009-11-11 19:31:23 +00002767 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002768 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2769 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002770 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002771
Douglas Gregor910f8002010-11-07 23:05:16 +00002772 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002773 break;
2774 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002775
Douglas Gregore7526412009-11-11 19:31:23 +00002776 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002777 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002778 // therefore cannot be a non-type template argument.
2779 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2780 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002781
Douglas Gregore7526412009-11-11 19:31:23 +00002782 Diag(Param->getLocation(), diag::note_template_param_here);
2783 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002784
Douglas Gregore7526412009-11-11 19:31:23 +00002785 case TemplateArgument::Type: {
2786 // We have a non-type template parameter but the template
2787 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002788
Douglas Gregore7526412009-11-11 19:31:23 +00002789 // C++ [temp.arg]p2:
2790 // In a template-argument, an ambiguity between a type-id and
2791 // an expression is resolved to a type-id, regardless of the
2792 // form of the corresponding template-parameter.
2793 //
2794 // We warn specifically about this case, since it can be rather
2795 // confusing for users.
2796 QualType T = Arg.getArgument().getAsType();
2797 SourceRange SR = Arg.getSourceRange();
2798 if (T->isFunctionType())
2799 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2800 else
2801 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2802 Diag(Param->getLocation(), diag::note_template_param_here);
2803 return true;
2804 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002805
Douglas Gregore7526412009-11-11 19:31:23 +00002806 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002807 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002808 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002809
Douglas Gregore7526412009-11-11 19:31:23 +00002810 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002811 }
2812
2813
Douglas Gregore7526412009-11-11 19:31:23 +00002814 // Check template template parameters.
2815 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002816
Douglas Gregore7526412009-11-11 19:31:23 +00002817 // Substitute into the template parameter list of the template
2818 // template parameter, since previously-supplied template arguments
2819 // may appear within the template template parameter.
2820 {
2821 // Set up a template instantiation context.
2822 LocalInstantiationScope Scope(*this);
2823 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002824 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002825 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002826
2827 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002828 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002829 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002830 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002831 MultiLevelTemplateArgumentList(TemplateArgs)));
2832 if (!TempParm)
2833 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002834 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002835
Douglas Gregore7526412009-11-11 19:31:23 +00002836 switch (Arg.getArgument().getKind()) {
2837 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002838 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002839
Douglas Gregore7526412009-11-11 19:31:23 +00002840 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002841 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002842 if (CheckTemplateArgument(TempParm, Arg))
2843 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002844
Douglas Gregor910f8002010-11-07 23:05:16 +00002845 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002846 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002847
Douglas Gregore7526412009-11-11 19:31:23 +00002848 case TemplateArgument::Expression:
2849 case TemplateArgument::Type:
2850 // We have a template template parameter but the template
2851 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002852 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
2853 << getLangOptions().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002854 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002855
Douglas Gregore7526412009-11-11 19:31:23 +00002856 case TemplateArgument::Declaration:
David Blaikie7530c032012-01-17 06:56:22 +00002857 llvm_unreachable("Declaration argument with template template parameter");
Douglas Gregore7526412009-11-11 19:31:23 +00002858 case TemplateArgument::Integral:
David Blaikie7530c032012-01-17 06:56:22 +00002859 llvm_unreachable("Integral argument with template template parameter");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002860
Douglas Gregore7526412009-11-11 19:31:23 +00002861 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002862 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002863 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002864
Douglas Gregore7526412009-11-11 19:31:23 +00002865 return false;
2866}
2867
Douglas Gregorc15cb382009-02-09 23:23:08 +00002868/// \brief Check that the given template argument list is well-formed
2869/// for specializing the given template.
2870bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2871 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002872 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002873 bool PartialTemplateArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002874 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002875 TemplateParameterList *Params = Template->getTemplateParameters();
2876 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002877 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002878 bool Invalid = false;
2879
John McCalld5532b62009-11-23 01:53:49 +00002880 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2881
Mike Stump1eb44332009-09-09 15:08:12 +00002882 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002883 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Mike Stump1eb44332009-09-09 15:08:12 +00002884
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002885 if ((NumArgs > NumParams && !HasParameterPack) ||
Douglas Gregor16134c62009-07-01 00:28:38 +00002886 (NumArgs < Params->getMinRequiredArguments() &&
2887 !PartialTemplateArgs)) {
Douglas Gregorc15cb382009-02-09 23:23:08 +00002888 // FIXME: point at either the first arg beyond what we can handle,
2889 // or the '>', depending on whether we have too many or too few
2890 // arguments.
2891 SourceRange Range;
2892 if (NumArgs > NumParams)
Douglas Gregor40808ce2009-03-09 23:48:35 +00002893 Range = SourceRange(TemplateArgs[NumParams].getLocation(), RAngleLoc);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002894 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2895 << (NumArgs > NumParams)
2896 << (isa<ClassTemplateDecl>(Template)? 0 :
2897 isa<FunctionTemplateDecl>(Template)? 1 :
2898 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2899 << Template << Range;
Douglas Gregor62cb18d2009-02-11 18:16:40 +00002900 Diag(Template->getLocation(), diag::note_template_decl_here)
2901 << Params->getSourceRange();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002902 Invalid = true;
2903 }
Mike Stump1eb44332009-09-09 15:08:12 +00002904
2905 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002906 // [...] The type and form of each template-argument specified in
2907 // a template-id shall match the type and form specified for the
2908 // corresponding parameter declared by the template in its
2909 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002910 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002911 SmallVector<TemplateArgument, 2> ArgumentPack;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002912 TemplateParameterList::iterator Param = Params->begin(),
2913 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002914 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002915 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor14be16b2010-12-20 16:57:52 +00002916 while (Param != ParamEnd) {
Douglas Gregorf35f8282009-11-11 21:54:23 +00002917 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002918 // If we have an expanded parameter pack, make sure we don't have too
2919 // many arguments.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002920 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002921 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002922 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002923 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2924 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2925 << true
2926 << (isa<ClassTemplateDecl>(Template)? 0 :
2927 isa<FunctionTemplateDecl>(Template)? 1 :
2928 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2929 << Template;
2930 Diag(Template->getLocation(), diag::note_template_decl_here)
2931 << Params->getSourceRange();
2932 return true;
2933 }
2934 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002935
Douglas Gregorf35f8282009-11-11 21:54:23 +00002936 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002937 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2938 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002939 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002940 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002941
Douglas Gregor14be16b2010-12-20 16:57:52 +00002942 if ((*Param)->isTemplateParameterPack()) {
2943 // The template parameter was a template parameter pack, so take the
2944 // deduced argument and place it on the argument pack. Note that we
2945 // stay on the same template parameter so that we can deduce more
2946 // arguments.
2947 ArgumentPack.push_back(Converted.back());
2948 Converted.pop_back();
2949 } else {
2950 // Move to the next template parameter.
2951 ++Param;
2952 }
2953 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002954 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002955 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002956
Douglas Gregor8735b292011-06-03 02:59:40 +00002957 // If we're checking a partial template argument list, we're done.
2958 if (PartialTemplateArgs) {
2959 if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
2960 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2961 ArgumentPack.data(),
2962 ArgumentPack.size()));
2963
2964 return Invalid;
2965 }
2966
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002967 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002968 // arguments, just break out now and we'll fill in the argument pack below.
2969 if ((*Param)->isTemplateParameterPack())
2970 break;
Douglas Gregorf968d832011-05-27 01:19:52 +00002971
Douglas Gregorf35f8282009-11-11 21:54:23 +00002972 // We have a default template argument that we will use.
2973 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002974
Douglas Gregorf35f8282009-11-11 21:54:23 +00002975 // Retrieve the default template argument from the template
2976 // parameter. For each kind of template parameter, we substitute the
2977 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002978 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002979 // the default argument.
2980 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
2981 if (!TTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00002982 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00002983 break;
2984 }
2985
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002986 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00002987 Template,
2988 TemplateLoc,
2989 RAngleLoc,
2990 TTP,
2991 Converted);
2992 if (!ArgType)
2993 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002994
Douglas Gregorf35f8282009-11-11 21:54:23 +00002995 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
2996 ArgType);
2997 } else if (NonTypeTemplateParmDecl *NTTP
2998 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
2999 if (!NTTP->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00003000 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00003001 break;
3002 }
3003
John McCall60d7b3a2010-08-24 06:29:42 +00003004 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003005 TemplateLoc,
3006 RAngleLoc,
3007 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003008 Converted);
3009 if (E.isInvalid())
3010 return true;
3011
3012 Expr *Ex = E.takeAs<Expr>();
3013 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
3014 } else {
3015 TemplateTemplateParmDecl *TempParm
3016 = cast<TemplateTemplateParmDecl>(*Param);
3017
3018 if (!TempParm->hasDefaultArgument()) {
Douglas Gregor8735b292011-06-03 02:59:40 +00003019 assert(Invalid && "Missing default argument");
Douglas Gregorf35f8282009-11-11 21:54:23 +00003020 break;
3021 }
3022
Douglas Gregor1d752d72011-03-02 18:46:51 +00003023 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00003024 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003025 TemplateLoc,
3026 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003027 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003028 Converted,
3029 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003030 if (Name.isNull())
3031 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003032
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003033 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
3034 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00003035 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003036
Douglas Gregorf35f8282009-11-11 21:54:23 +00003037 // Introduce an instantiation record that describes where we are using
3038 // the default template argument.
3039 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00003040 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003041 SourceRange(TemplateLoc, RAngleLoc));
3042
Douglas Gregorf35f8282009-11-11 21:54:23 +00003043 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00003044 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00003045 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00003046 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003047
Douglas Gregor67714232011-03-03 02:41:12 +00003048 // Core issue 150 (assumed resolution): if this is a template template
3049 // parameter, keep track of the default template arguments from the
3050 // template definition.
3051 if (isTemplateTemplateParameter)
3052 TemplateArgs.addArgument(Arg);
3053
Douglas Gregor14be16b2010-12-20 16:57:52 +00003054 // Move to the next template parameter and argument.
3055 ++Param;
3056 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003057 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003058
Douglas Gregor14be16b2010-12-20 16:57:52 +00003059 // Form argument packs for each of the parameter packs remaining.
3060 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00003061 // If we're checking a partial list of template arguments, don't fill
3062 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003063
3064 if ((*Param)->isTemplateParameterPack()) {
David Blaikie1368e582011-10-19 05:19:50 +00003065 if (!HasParameterPack)
3066 return true;
Douglas Gregor8735b292011-06-03 02:59:40 +00003067 if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00003068 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00003069 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003070 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3071 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00003072 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00003073 ArgumentPack.clear();
3074 }
3075 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003076
Douglas Gregor14be16b2010-12-20 16:57:52 +00003077 ++Param;
3078 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003079
Douglas Gregorc15cb382009-02-09 23:23:08 +00003080 return Invalid;
3081}
3082
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003083namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003084 class UnnamedLocalNoLinkageFinder
3085 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003086 {
3087 Sema &S;
3088 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003089
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003090 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003091
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003092 public:
3093 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3094
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003095 bool Visit(QualType T) {
3096 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003097 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003098
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003099#define TYPE(Class, Parent) \
3100 bool Visit##Class##Type(const Class##Type *);
3101#define ABSTRACT_TYPE(Class, Parent) \
3102 bool Visit##Class##Type(const Class##Type *) { return false; }
3103#define NON_CANONICAL_TYPE(Class, Parent) \
3104 bool Visit##Class##Type(const Class##Type *) { return false; }
3105#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003106
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003107 bool VisitTagDecl(const TagDecl *Tag);
3108 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3109 };
3110}
3111
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003112bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003113 return false;
3114}
3115
3116bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3117 return Visit(T->getElementType());
3118}
3119
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003120bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003121 return Visit(T->getPointeeType());
3122}
3123
3124bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003125 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003126 return Visit(T->getPointeeType());
3127}
3128
3129bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003130 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003131 return Visit(T->getPointeeType());
3132}
3133
3134bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003135 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003136 return Visit(T->getPointeeType());
3137}
3138
3139bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003140 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003141 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3142}
3143
3144bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003145 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003146 return Visit(T->getElementType());
3147}
3148
3149bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003150 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003151 return Visit(T->getElementType());
3152}
3153
3154bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003155 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003156 return Visit(T->getElementType());
3157}
3158
3159bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003160 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003161 return Visit(T->getElementType());
3162}
3163
3164bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003165 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003166 return Visit(T->getElementType());
3167}
3168
3169bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3170 return Visit(T->getElementType());
3171}
3172
3173bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3174 return Visit(T->getElementType());
3175}
3176
3177bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3178 const FunctionProtoType* T) {
3179 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003180 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003181 A != AEnd; ++A) {
3182 if (Visit(*A))
3183 return true;
3184 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003185
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003186 return Visit(T->getResultType());
3187}
3188
3189bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3190 const FunctionNoProtoType* T) {
3191 return Visit(T->getResultType());
3192}
3193
3194bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3195 const UnresolvedUsingType*) {
3196 return false;
3197}
3198
3199bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3200 return false;
3201}
3202
3203bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3204 return Visit(T->getUnderlyingType());
3205}
3206
3207bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3208 return false;
3209}
3210
Sean Huntca63c202011-05-24 22:41:36 +00003211bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3212 const UnaryTransformType*) {
3213 return false;
3214}
3215
Richard Smith34b41d92011-02-20 03:19:35 +00003216bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3217 return Visit(T->getDeducedType());
3218}
3219
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003220bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3221 return VisitTagDecl(T->getDecl());
3222}
3223
3224bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3225 return VisitTagDecl(T->getDecl());
3226}
3227
3228bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3229 const TemplateTypeParmType*) {
3230 return false;
3231}
3232
Douglas Gregorc3069d62011-01-14 02:55:32 +00003233bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3234 const SubstTemplateTypeParmPackType *) {
3235 return false;
3236}
3237
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003238bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3239 const TemplateSpecializationType*) {
3240 return false;
3241}
3242
3243bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3244 const InjectedClassNameType* T) {
3245 return VisitTagDecl(T->getDecl());
3246}
3247
3248bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3249 const DependentNameType* T) {
3250 return VisitNestedNameSpecifier(T->getQualifier());
3251}
3252
3253bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3254 const DependentTemplateSpecializationType* T) {
3255 return VisitNestedNameSpecifier(T->getQualifier());
3256}
3257
Douglas Gregor7536dd52010-12-20 02:24:11 +00003258bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3259 const PackExpansionType* T) {
3260 return Visit(T->getPattern());
3261}
3262
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003263bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3264 return false;
3265}
3266
3267bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3268 const ObjCInterfaceType *) {
3269 return false;
3270}
3271
3272bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3273 const ObjCObjectPointerType *) {
3274 return false;
3275}
3276
Eli Friedmanb001de72011-10-06 23:00:33 +00003277bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) {
3278 return Visit(T->getValueType());
3279}
3280
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003281bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3282 if (Tag->getDeclContext()->isFunctionOrMethod()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003283 S.Diag(SR.getBegin(),
3284 S.getLangOptions().CPlusPlus0x ?
3285 diag::warn_cxx98_compat_template_arg_local_type :
3286 diag::ext_template_arg_local_type)
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003287 << S.Context.getTypeDeclType(Tag) << SR;
3288 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003289 }
3290
Richard Smith162e1c12011-04-15 14:24:37 +00003291 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003292 S.Diag(SR.getBegin(),
3293 S.getLangOptions().CPlusPlus0x ?
3294 diag::warn_cxx98_compat_template_arg_unnamed_type :
3295 diag::ext_template_arg_unnamed_type) << SR;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003296 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3297 return true;
3298 }
3299
3300 return false;
3301}
3302
3303bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3304 NestedNameSpecifier *NNS) {
3305 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3306 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003307
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003308 switch (NNS->getKind()) {
3309 case NestedNameSpecifier::Identifier:
3310 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003311 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003312 case NestedNameSpecifier::Global:
3313 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003314
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003315 case NestedNameSpecifier::TypeSpec:
3316 case NestedNameSpecifier::TypeSpecWithTemplate:
3317 return Visit(QualType(NNS->getAsType(), 0));
3318 }
David Blaikie7530c032012-01-17 06:56:22 +00003319 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003320}
3321
3322
Douglas Gregorc15cb382009-02-09 23:23:08 +00003323/// \brief Check a template argument against its corresponding
3324/// template type parameter.
3325///
3326/// This routine implements the semantics of C++ [temp.arg.type]. It
3327/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003328bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003329 TypeSourceInfo *ArgInfo) {
3330 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003331 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003332 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003333
3334 if (Arg->isVariablyModifiedType()) {
3335 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003336 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003337 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003338 }
3339
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003340 // C++03 [temp.arg.type]p2:
3341 // A local type, a type with no linkage, an unnamed type or a type
3342 // compounded from any of these types shall not be used as a
3343 // template-argument for a template type-parameter.
3344 //
Richard Smithebaf0e62011-10-18 20:49:44 +00003345 // C++11 allows these, and even in C++03 we allow them as an extension with
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003346 // a warning.
Richard Smithebaf0e62011-10-18 20:49:44 +00003347 if (LangOpts.CPlusPlus0x ?
3348 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type,
3349 SR.getBegin()) != DiagnosticsEngine::Ignored ||
3350 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type,
3351 SR.getBegin()) != DiagnosticsEngine::Ignored :
3352 Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003353 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3354 (void)Finder.Visit(Context.getCanonicalType(Arg));
3355 }
3356
Douglas Gregorc15cb382009-02-09 23:23:08 +00003357 return false;
3358}
3359
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003360/// \brief Checks whether the given template argument is the address
3361/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003362static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003363CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3364 NonTypeTemplateParmDecl *Param,
3365 QualType ParamType,
3366 Expr *ArgIn,
3367 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003368 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003369 Expr *Arg = ArgIn;
3370 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003371
3372 // See through any implicit casts we added to fix the type.
John McCall91a57552011-07-15 05:09:51 +00003373 Arg = Arg->IgnoreImpCasts();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003374
3375 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003376 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003377 // A template-argument for a non-type, non-template
3378 // template-parameter shall be one of: [...]
3379 //
3380 // -- the address of an object or function with external
3381 // linkage, including function templates and function
3382 // template-ids but excluding non-static class members,
3383 // expressed as & id-expression where the & is optional if
3384 // the name refers to a function or array, or if the
3385 // corresponding template-parameter is a reference; or
Mike Stump1eb44332009-09-09 15:08:12 +00003386
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003387 // In C++98/03 mode, give an extension warning on any extra parentheses.
3388 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3389 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003390 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003391 if (!Invalid && !ExtraParens) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003392 S.Diag(Arg->getSourceRange().getBegin(),
Richard Smithebaf0e62011-10-18 20:49:44 +00003393 S.getLangOptions().CPlusPlus0x ?
3394 diag::warn_cxx98_compat_template_arg_extra_parens :
3395 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003396 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003397 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003398 }
3399
3400 Arg = Parens->getSubExpr();
3401 }
3402
John McCall91a57552011-07-15 05:09:51 +00003403 while (SubstNonTypeTemplateParmExpr *subst =
3404 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3405 Arg = subst->getReplacement()->IgnoreImpCasts();
3406
Douglas Gregorb7a09262010-04-01 18:32:35 +00003407 bool AddressTaken = false;
3408 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003409 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003410 if (UnOp->getOpcode() == UO_AddrOf) {
John McCall91a57552011-07-15 05:09:51 +00003411 Arg = UnOp->getSubExpr();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003412 AddressTaken = true;
3413 AddrOpLoc = UnOp->getOperatorLoc();
3414 }
Francois Picheta343a412011-04-29 09:08:14 +00003415 }
John McCall91a57552011-07-15 05:09:51 +00003416
Francois Pichet62ec1f22011-09-17 17:15:52 +00003417 if (S.getLangOptions().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) {
John McCall91a57552011-07-15 05:09:51 +00003418 Converted = TemplateArgument(ArgIn);
3419 return false;
3420 }
3421
3422 while (SubstNonTypeTemplateParmExpr *subst =
3423 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3424 Arg = subst->getReplacement()->IgnoreImpCasts();
3425
3426 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003427 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003428 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3429 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003430 S.Diag(Param->getLocation(), diag::note_template_param_here);
3431 return true;
3432 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003433
3434 // Stop checking the precise nature of the argument if it is value dependent,
3435 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003436 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003437 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003438 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003439 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003440
Douglas Gregorb7a09262010-04-01 18:32:35 +00003441 if (!isa<ValueDecl>(DRE->getDecl())) {
3442 S.Diag(Arg->getSourceRange().getBegin(),
3443 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003444 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003445 S.Diag(Param->getLocation(), diag::note_template_param_here);
3446 return true;
3447 }
3448
3449 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003450
3451 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003452 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3453 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003454 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003455 S.Diag(Param->getLocation(), diag::note_template_param_here);
3456 return true;
3457 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003458
3459 // Cannot refer to non-static member functions
3460 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003461 if (!Method->isStatic()) {
3462 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003463 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003464 S.Diag(Param->getLocation(), diag::note_template_param_here);
3465 return true;
3466 }
Mike Stump1eb44332009-09-09 15:08:12 +00003467
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003468 // Functions must have external linkage.
3469 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003470 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003471 S.Diag(Arg->getSourceRange().getBegin(),
3472 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003473 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003474 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003475 << true;
3476 return true;
3477 }
3478
3479 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003480 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003481
Douglas Gregorb7a09262010-04-01 18:32:35 +00003482 // If the template parameter has pointer type, the function decays.
3483 if (ParamType->isPointerType() && !AddressTaken)
3484 ArgType = S.Context.getPointerType(Func->getType());
3485 else if (AddressTaken && ParamType->isReferenceType()) {
3486 // If we originally had an address-of operator, but the
3487 // parameter has reference type, complain and (if things look
3488 // like they will work) drop the address-of operator.
3489 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3490 ParamType.getNonReferenceType())) {
3491 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3492 << ParamType;
3493 S.Diag(Param->getLocation(), diag::note_template_param_here);
3494 return true;
3495 }
3496
3497 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3498 << ParamType
3499 << FixItHint::CreateRemoval(AddrOpLoc);
3500 S.Diag(Param->getLocation(), diag::note_template_param_here);
3501
3502 ArgType = Func->getType();
3503 }
3504 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003505 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003506 S.Diag(Arg->getSourceRange().getBegin(),
3507 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003508 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003509 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003510 << true;
3511 return true;
3512 }
3513
Douglas Gregorb7a09262010-04-01 18:32:35 +00003514 // A value of reference type is not an object.
3515 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003516 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003517 diag::err_template_arg_reference_var)
3518 << Var->getType() << Arg->getSourceRange();
3519 S.Diag(Param->getLocation(), diag::note_template_param_here);
3520 return true;
3521 }
3522
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003523 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003524 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003525
3526 // If the template parameter has pointer type, we must have taken
3527 // the address of this object.
3528 if (ParamType->isReferenceType()) {
3529 if (AddressTaken) {
3530 // If we originally had an address-of operator, but the
3531 // parameter has reference type, complain and (if things look
3532 // like they will work) drop the address-of operator.
3533 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3534 ParamType.getNonReferenceType())) {
3535 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3536 << ParamType;
3537 S.Diag(Param->getLocation(), diag::note_template_param_here);
3538 return true;
3539 }
3540
3541 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3542 << ParamType
3543 << FixItHint::CreateRemoval(AddrOpLoc);
3544 S.Diag(Param->getLocation(), diag::note_template_param_here);
3545
3546 ArgType = Var->getType();
3547 }
3548 } else if (!AddressTaken && ParamType->isPointerType()) {
3549 if (Var->getType()->isArrayType()) {
3550 // Array-to-pointer decay.
3551 ArgType = S.Context.getArrayDecayedType(Var->getType());
3552 } else {
3553 // If the template parameter has pointer type but the address of
3554 // this object was not taken, complain and (possibly) recover by
3555 // taking the address of the entity.
3556 ArgType = S.Context.getPointerType(Var->getType());
3557 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3558 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3559 << ParamType;
3560 S.Diag(Param->getLocation(), diag::note_template_param_here);
3561 return true;
3562 }
3563
3564 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3565 << ParamType
3566 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3567
3568 S.Diag(Param->getLocation(), diag::note_template_param_here);
3569 }
3570 }
3571 } else {
3572 // We found something else, but we don't know specifically what it is.
3573 S.Diag(Arg->getSourceRange().getBegin(),
3574 diag::err_template_arg_not_object_or_func)
3575 << Arg->getSourceRange();
3576 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3577 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003578 }
Mike Stump1eb44332009-09-09 15:08:12 +00003579
John McCallf85e1932011-06-15 23:02:42 +00003580 bool ObjCLifetimeConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003581 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003582 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
John McCallf85e1932011-06-15 23:02:42 +00003583 S.IsQualificationConversion(ArgType, ParamType, false,
3584 ObjCLifetimeConversion)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003585 // For pointer-to-object types, qualification conversions are
3586 // permitted.
3587 } else {
3588 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3589 if (!ParamRef->getPointeeType()->isFunctionType()) {
3590 // C++ [temp.arg.nontype]p5b3:
3591 // For a non-type template-parameter of type reference to
3592 // object, no conversions apply. The type referred to by the
3593 // reference may be more cv-qualified than the (otherwise
3594 // identical) type of the template- argument. The
3595 // template-parameter is bound directly to the
3596 // template-argument, which shall be an lvalue.
3597
3598 // FIXME: Other qualifiers?
3599 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3600 unsigned ArgQuals = ArgType.getCVRQualifiers();
3601
3602 if ((ParamQuals | ArgQuals) != ParamQuals) {
3603 S.Diag(Arg->getSourceRange().getBegin(),
3604 diag::err_template_arg_ref_bind_ignores_quals)
3605 << ParamType << Arg->getType()
3606 << Arg->getSourceRange();
3607 S.Diag(Param->getLocation(), diag::note_template_param_here);
3608 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003609 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003610 }
3611 }
3612
3613 // At this point, the template argument refers to an object or
3614 // function with external linkage. We now need to check whether the
3615 // argument and parameter types are compatible.
3616 if (!S.Context.hasSameUnqualifiedType(ArgType,
3617 ParamType.getNonReferenceType())) {
3618 // We can't perform this conversion or binding.
3619 if (ParamType->isReferenceType())
3620 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
John McCall91a57552011-07-15 05:09:51 +00003621 << ParamType << ArgIn->getType() << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003622 else
3623 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
John McCall91a57552011-07-15 05:09:51 +00003624 << ArgIn->getType() << ParamType << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003625 S.Diag(Param->getLocation(), diag::note_template_param_here);
3626 return true;
3627 }
3628 }
3629
3630 // Create the template argument.
3631 Converted = TemplateArgument(Entity->getCanonicalDecl());
Douglas Gregor77c13e02010-04-24 18:20:53 +00003632 S.MarkDeclarationReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003633 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003634}
3635
3636/// \brief Checks whether the given template argument is a pointer to
3637/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003638bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003639 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003640 bool Invalid = false;
3641
3642 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003643 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003644 Arg = Cast->getSubExpr();
3645
3646 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003647 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003648 // A template-argument for a non-type, non-template
3649 // template-parameter shall be one of: [...]
3650 //
3651 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003652 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003653
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003654 // In C++98/03 mode, give an extension warning on any extra parentheses.
3655 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3656 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003657 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003658 if (!Invalid && !ExtraParens) {
Mike Stump1eb44332009-09-09 15:08:12 +00003659 Diag(Arg->getSourceRange().getBegin(),
Richard Smithebaf0e62011-10-18 20:49:44 +00003660 getLangOptions().CPlusPlus0x ?
3661 diag::warn_cxx98_compat_template_arg_extra_parens :
3662 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003663 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003664 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003665 }
3666
3667 Arg = Parens->getSubExpr();
3668 }
3669
John McCall91a57552011-07-15 05:09:51 +00003670 while (SubstNonTypeTemplateParmExpr *subst =
3671 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3672 Arg = subst->getReplacement()->IgnoreImpCasts();
3673
Douglas Gregorcaddba02009-11-12 18:38:13 +00003674 // A pointer-to-member constant written &Class::member.
3675 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003676 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003677 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3678 if (DRE && !DRE->getQualifier())
3679 DRE = 0;
3680 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003681 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003682 // A constant of pointer-to-member type.
3683 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3684 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3685 if (VD->getType()->isMemberPointerType()) {
3686 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003687 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003688 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3689 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003690 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003691 else
3692 Converted = TemplateArgument(VD->getCanonicalDecl());
3693 return Invalid;
3694 }
3695 }
3696 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003697
Douglas Gregorcaddba02009-11-12 18:38:13 +00003698 DRE = 0;
3699 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003700
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003701 if (!DRE)
3702 return Diag(Arg->getSourceRange().getBegin(),
3703 diag::err_template_arg_not_pointer_to_member_form)
3704 << Arg->getSourceRange();
3705
3706 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3707 assert((isa<FieldDecl>(DRE->getDecl()) ||
3708 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3709 "Only non-static member pointers can make it here");
3710
3711 // Okay: this is the address of a non-static member, and therefore
3712 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003713 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003714 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003715 else
3716 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003717 return Invalid;
3718 }
3719
3720 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003721 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003722 diag::err_template_arg_not_pointer_to_member_form)
3723 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003724 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003725 diag::note_template_arg_refers_here);
3726 return true;
3727}
3728
Douglas Gregorc15cb382009-02-09 23:23:08 +00003729/// \brief Check a template argument against its corresponding
3730/// non-type template parameter.
3731///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003732/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003733/// If an error occurred, it returns ExprError(); otherwise, it
3734/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003735/// InstantiatedParamType is the type of the non-type template
3736/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003737ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3738 QualType InstantiatedParamType, Expr *Arg,
3739 TemplateArgument &Converted,
3740 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003741 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3742
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003743 // If either the parameter has a dependent type or the argument is
3744 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003745 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3746 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003747 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003748 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003749 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003750
3751 // C++ [temp.arg.nontype]p5:
3752 // The following conversions are performed on each expression used
3753 // as a non-type template-argument. If a non-type
3754 // template-argument cannot be converted to the type of the
3755 // corresponding template-parameter then the program is
3756 // ill-formed.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003757 QualType ParamType = InstantiatedParamType;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003758 if (ParamType->isIntegralOrEnumerationType()) {
Richard Smith8ef7b202012-01-18 23:55:52 +00003759 // C++11:
3760 // -- for a non-type template-parameter of integral or
3761 // enumeration type, conversions permitted in a converted
3762 // constant expression are applied.
3763 //
3764 // C++98:
3765 // -- for a non-type template-parameter of integral or
3766 // enumeration type, integral promotions (4.5) and integral
3767 // conversions (4.7) are applied.
3768
3769 if (CTAK == CTAK_Deduced &&
3770 !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) {
3771 // C++ [temp.deduct.type]p17:
3772 // If, in the declaration of a function template with a non-type
3773 // template-parameter, the non-type template-parameter is used
3774 // in an expression in the function parameter-list and, if the
3775 // corresponding template-argument is deduced, the
3776 // template-argument type shall match the type of the
3777 // template-parameter exactly, except that a template-argument
3778 // deduced from an array bound may be of any integral type.
3779 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3780 << Arg->getType().getUnqualifiedType()
3781 << ParamType.getUnqualifiedType();
3782 Diag(Param->getLocation(), diag::note_template_param_here);
3783 return ExprError();
3784 }
3785
3786 if (getLangOptions().CPlusPlus0x) {
3787 // We can't check arbitrary value-dependent arguments.
3788 // FIXME: If there's no viable conversion to the template parameter type,
3789 // we should be able to diagnose that prior to instantiation.
3790 if (Arg->isValueDependent()) {
3791 Converted = TemplateArgument(Arg);
3792 return Owned(Arg);
3793 }
3794
3795 // C++ [temp.arg.nontype]p1:
3796 // A template-argument for a non-type, non-template template-parameter
3797 // shall be one of:
3798 //
3799 // -- for a non-type template-parameter of integral or enumeration
3800 // type, a converted constant expression of the type of the
3801 // template-parameter; or
3802 llvm::APSInt Value;
3803 ExprResult ArgResult =
3804 CheckConvertedConstantExpression(Arg, ParamType, Value,
3805 CCEK_TemplateArg);
3806 if (ArgResult.isInvalid())
3807 return ExprError();
3808
3809 // Widen the argument value to sizeof(parameter type). This is almost
3810 // always a no-op, except when the parameter type is bool. In
3811 // that case, this may extend the argument from 1 bit to 8 bits.
3812 QualType IntegerType = ParamType;
3813 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
3814 IntegerType = Enum->getDecl()->getIntegerType();
3815 Value = Value.extOrTrunc(Context.getTypeSize(IntegerType));
3816
3817 Converted = TemplateArgument(Value, Context.getCanonicalType(ParamType));
3818 return ArgResult;
3819 }
3820
Richard Smith4f870622011-10-27 22:11:44 +00003821 ExprResult ArgResult = DefaultLvalueConversion(Arg);
3822 if (ArgResult.isInvalid())
3823 return ExprError();
3824 Arg = ArgResult.take();
3825
3826 QualType ArgType = Arg->getType();
3827
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003828 // C++ [temp.arg.nontype]p1:
3829 // A template-argument for a non-type, non-template
3830 // template-parameter shall be one of:
3831 //
3832 // -- an integral constant-expression of integral or enumeration
3833 // type; or
3834 // -- the name of a non-type template-parameter; or
3835 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003836 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003837 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003838 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003839 diag::err_template_arg_not_integral_or_enumeral)
3840 << ArgType << Arg->getSourceRange();
3841 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003842 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003843 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003844 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003845 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3846 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003847 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003848 }
3849
Douglas Gregor02024a92010-03-28 02:42:43 +00003850 // From here on out, all we care about are the unqualified forms
3851 // of the parameter and argument types.
3852 ParamType = ParamType.getUnqualifiedType();
3853 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003854
3855 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003856 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003857 // Okay: no conversion necessary
John McCalldaa8e4e2010-11-15 09:13:47 +00003858 } else if (ParamType->isBooleanType()) {
3859 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003860 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003861 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3862 !ParamType->isEnumeralType()) {
3863 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003864 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003865 } else {
3866 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003867 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003868 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003869 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003870 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003871 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003872 }
3873
Douglas Gregorc7469372011-05-04 21:55:00 +00003874 // Add the value of this argument to the list of converted
3875 // arguments. We use the bitwidth and signedness of the template
3876 // parameter.
3877 if (Arg->isValueDependent()) {
3878 // The argument is value-dependent. Create a new
3879 // TemplateArgument with the converted expression.
3880 Converted = TemplateArgument(Arg);
3881 return Owned(Arg);
3882 }
3883
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003884 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003885 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003886 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003887
Douglas Gregorc7469372011-05-04 21:55:00 +00003888 if (ParamType->isBooleanType()) {
3889 // Value must be zero or one.
3890 Value = Value != 0;
3891 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3892 if (Value.getBitWidth() != AllowedBits)
3893 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003894 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003895 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003896 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003897
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003898 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003899 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003900 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003901 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003902 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003903 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003904
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003905 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00003906 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003907 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003908 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3909 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3910 << Arg->getSourceRange();
3911 Diag(Param->getLocation(), diag::note_template_param_here);
3912 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003913
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003914 // Complain if we overflowed the template parameter's type.
3915 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00003916 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003917 RequiredBits = OldValue.getActiveBits();
3918 else if (OldValue.isUnsigned())
3919 RequiredBits = OldValue.getActiveBits() + 1;
3920 else
3921 RequiredBits = OldValue.getMinSignedBits();
3922 if (RequiredBits > AllowedBits) {
3923 Diag(Arg->getSourceRange().getBegin(),
3924 diag::warn_template_arg_too_large)
3925 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3926 << Arg->getSourceRange();
3927 Diag(Param->getLocation(), diag::note_template_param_here);
3928 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003929 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003930
John McCall833ca992009-10-29 08:12:44 +00003931 Converted = TemplateArgument(Value,
Douglas Gregor6b63f552011-08-09 01:55:14 +00003932 ParamType->isEnumeralType()
3933 ? Context.getCanonicalType(ParamType)
3934 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00003935 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003936 }
Douglas Gregora35284b2009-02-11 00:19:33 +00003937
Richard Smith4f870622011-10-27 22:11:44 +00003938 QualType ArgType = Arg->getType();
John McCall6bb80172010-03-30 21:47:33 +00003939 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
3940
Douglas Gregorb7a09262010-04-01 18:32:35 +00003941 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
3942 // from a template argument of type std::nullptr_t to a non-type
3943 // template parameter of type pointer to object, pointer to
3944 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00003945 if (ArgType->isNullPtrType()) {
3946 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
3947 Converted = TemplateArgument((NamedDecl *)0);
3948 return Owned(Arg);
3949 }
3950
3951 if (ParamType->isNullPtrType()) {
3952 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
3953 Converted = TemplateArgument(Zero, Context.NullPtrTy);
3954 return Owned(Arg);
3955 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003956 }
3957
Douglas Gregorb86b0572009-02-11 01:18:59 +00003958 // Handle pointer-to-function, reference-to-function, and
3959 // pointer-to-member-function all in (roughly) the same way.
3960 if (// -- For a non-type template-parameter of type pointer to
3961 // function, only the function-to-pointer conversion (4.3) is
3962 // applied. If the template-argument represents a set of
3963 // overloaded functions (or a pointer to such), the matching
3964 // function is selected from the set (13.4).
3965 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003966 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003967 // -- For a non-type template-parameter of type reference to
3968 // function, no conversions apply. If the template-argument
3969 // represents a set of overloaded functions, the matching
3970 // function is selected from the set (13.4).
3971 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003972 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00003973 // -- For a non-type template-parameter of type pointer to
3974 // member function, no conversions apply. If the
3975 // template-argument represents a set of overloaded member
3976 // functions, the matching member function is selected from
3977 // the set (13.4).
3978 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00003979 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00003980 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003981
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003982 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003983 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003984 true,
3985 FoundResult)) {
3986 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00003987 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003988
3989 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
3990 ArgType = Arg->getType();
3991 } else
John Wiegley429bb272011-04-08 18:41:53 +00003992 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00003993 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003994
John Wiegley429bb272011-04-08 18:41:53 +00003995 if (!ParamType->isMemberPointerType()) {
3996 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
3997 ParamType,
3998 Arg, Converted))
3999 return ExprError();
4000 return Owned(Arg);
4001 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00004002
John McCallf85e1932011-06-15 23:02:42 +00004003 bool ObjCLifetimeConversion;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00004004 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
John McCallf85e1932011-06-15 23:02:42 +00004005 false, ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00004006 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
4007 Arg->getValueKind()).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004008 } else if (!Context.hasSameUnqualifiedType(ArgType,
4009 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00004010 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00004011 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00004012 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00004013 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00004014 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00004015 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00004016 }
Mike Stump1eb44332009-09-09 15:08:12 +00004017
John Wiegley429bb272011-04-08 18:41:53 +00004018 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
4019 return ExprError();
4020 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00004021 }
4022
Chris Lattnerfe90de72009-02-20 21:37:53 +00004023 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00004024 // -- for a non-type template-parameter of type pointer to
4025 // object, qualification conversions (4.4) and the
4026 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00004027 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00004028 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00004029 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004030
John Wiegley429bb272011-04-08 18:41:53 +00004031 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4032 ParamType,
4033 Arg, Converted))
4034 return ExprError();
4035 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004036 }
Mike Stump1eb44332009-09-09 15:08:12 +00004037
Ted Kremenek6217b802009-07-29 21:53:49 +00004038 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00004039 // -- For a non-type template-parameter of type reference to
4040 // object, no conversions apply. The type referred to by the
4041 // reference may be more cv-qualified than the (otherwise
4042 // identical) type of the template-argument. The
4043 // template-parameter is bound directly to the
4044 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00004045 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00004046 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004047
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004048 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004049 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
4050 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004051 true,
4052 FoundResult)) {
4053 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00004054 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004055
4056 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
4057 ArgType = Arg->getType();
4058 } else
John Wiegley429bb272011-04-08 18:41:53 +00004059 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00004060 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004061
John Wiegley429bb272011-04-08 18:41:53 +00004062 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4063 ParamType,
4064 Arg, Converted))
4065 return ExprError();
4066 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00004067 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00004068
4069 // -- For a non-type template-parameter of type pointer to data
4070 // member, qualification conversions (4.4) are applied.
4071 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
4072
John McCallf85e1932011-06-15 23:02:42 +00004073 bool ObjCLifetimeConversion;
Douglas Gregor8e6563b2009-02-11 18:22:40 +00004074 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00004075 // Types match exactly: nothing more to do here.
John McCallf85e1932011-06-15 23:02:42 +00004076 } else if (IsQualificationConversion(ArgType, ParamType, false,
4077 ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00004078 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
4079 Arg->getValueKind()).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004080 } else {
4081 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00004082 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00004083 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00004084 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004085 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00004086 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004087 }
4088
John Wiegley429bb272011-04-08 18:41:53 +00004089 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
4090 return ExprError();
4091 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00004092}
4093
4094/// \brief Check a template argument against its corresponding
4095/// template template parameter.
4096///
4097/// This routine implements the semantics of C++ [temp.arg.template].
4098/// It returns true if an error occurred, and false otherwise.
4099bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00004100 const TemplateArgumentLoc &Arg) {
4101 TemplateName Name = Arg.getArgument().getAsTemplate();
4102 TemplateDecl *Template = Name.getAsTemplateDecl();
4103 if (!Template) {
4104 // Any dependent template name is fine.
4105 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
4106 return false;
4107 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00004108
Richard Smith3e4c6c42011-05-05 21:57:07 +00004109 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00004110 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00004111 // the name of a class template or an alias template, expressed as an
4112 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00004113 // primary class templates are considered when matching the
4114 // template template argument with the corresponding parameter;
4115 // partial specializations are not considered even if their
4116 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004117 //
4118 // Note that we also allow template template parameters here, which
4119 // will happen when we are dealing with, e.g., class template
4120 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00004121 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00004122 !isa<TemplateTemplateParmDecl>(Template) &&
4123 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00004124 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00004125 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00004126 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00004127 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00004128 << Template;
4129 }
4130
4131 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
4132 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004133 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004134 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00004135 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00004136}
4137
Douglas Gregor02024a92010-03-28 02:42:43 +00004138/// \brief Given a non-type template argument that refers to a
4139/// declaration and the type of its corresponding non-type template
4140/// parameter, produce an expression that properly refers to that
4141/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004142ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004143Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
4144 QualType ParamType,
4145 SourceLocation Loc) {
4146 assert(Arg.getKind() == TemplateArgument::Declaration &&
4147 "Only declaration template arguments permitted here");
4148 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
4149
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004150 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00004151 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
4152 // If the value is a class member, we might have a pointer-to-member.
4153 // Determine whether the non-type template template parameter is of
4154 // pointer-to-member type. If so, we need to build an appropriate
4155 // expression for a pointer-to-member, since a "normal" DeclRefExpr
4156 // would refer to the member itself.
4157 if (ParamType->isMemberPointerType()) {
4158 QualType ClassType
4159 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
4160 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00004161 = NestedNameSpecifier::Create(Context, 0, false,
4162 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00004163 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00004164 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00004165
4166 // The actual value-ness of this is unimportant, but for
4167 // internal consistency's sake, references to instance methods
4168 // are r-values.
4169 ExprValueKind VK = VK_LValue;
4170 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
4171 VK = VK_RValue;
4172
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004173 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00004174 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00004175 VK,
John McCallf89e55a2010-11-18 06:31:45 +00004176 Loc,
4177 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00004178 if (RefExpr.isInvalid())
4179 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004180
John McCall2de56d12010-08-25 11:45:40 +00004181 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004182
Douglas Gregorc0c83002010-04-30 21:46:38 +00004183 // We might need to perform a trailing qualification conversion, since
4184 // the element type on the parameter could be more qualified than the
4185 // element type in the expression we constructed.
John McCallf85e1932011-06-15 23:02:42 +00004186 bool ObjCLifetimeConversion;
Douglas Gregorc0c83002010-04-30 21:46:38 +00004187 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John McCallf85e1932011-06-15 23:02:42 +00004188 ParamType.getUnqualifiedType(), false,
4189 ObjCLifetimeConversion))
John Wiegley429bb272011-04-08 18:41:53 +00004190 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004191
Douglas Gregor02024a92010-03-28 02:42:43 +00004192 assert(!RefExpr.isInvalid() &&
4193 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004194 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004195 return move(RefExpr);
4196 }
4197 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004198
Douglas Gregor02024a92010-03-28 02:42:43 +00004199 QualType T = VD->getType().getNonReferenceType();
4200 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004201 // When the non-type template parameter is a pointer, take the
4202 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004203 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004204 if (RefExpr.isInvalid())
4205 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004206
4207 if (T->isFunctionType() || T->isArrayType()) {
4208 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004209 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4210 if (RefExpr.isInvalid())
4211 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004212
4213 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004214 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004215
Douglas Gregorb7a09262010-04-01 18:32:35 +00004216 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004217 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004218 }
4219
John McCallf89e55a2010-11-18 06:31:45 +00004220 ExprValueKind VK = VK_RValue;
4221
Douglas Gregor02024a92010-03-28 02:42:43 +00004222 // If the non-type template parameter has reference type, qualify the
4223 // resulting declaration reference with the extra qualifiers on the
4224 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004225 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4226 VK = VK_LValue;
4227 T = Context.getQualifiedType(T,
4228 TargetRef->getPointeeType().getQualifiers());
4229 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004230
John McCallf89e55a2010-11-18 06:31:45 +00004231 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004232}
4233
4234/// \brief Construct a new expression that refers to the given
4235/// integral template argument with the given source-location
4236/// information.
4237///
4238/// This routine takes care of the mapping from an integral template
4239/// argument (which may have any integral type) to the appropriate
4240/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004241ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004242Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4243 SourceLocation Loc) {
4244 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004245 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004246 QualType T = Arg.getIntegralType();
Douglas Gregor5cee1192011-07-27 05:40:30 +00004247 if (T->isAnyCharacterType()) {
4248 CharacterLiteral::CharacterKind Kind;
4249 if (T->isWideCharType())
4250 Kind = CharacterLiteral::Wide;
4251 else if (T->isChar16Type())
4252 Kind = CharacterLiteral::UTF16;
4253 else if (T->isChar32Type())
4254 Kind = CharacterLiteral::UTF32;
4255 else
4256 Kind = CharacterLiteral::Ascii;
4257
Douglas Gregor02024a92010-03-28 02:42:43 +00004258 return Owned(new (Context) CharacterLiteral(
Douglas Gregor5cee1192011-07-27 05:40:30 +00004259 Arg.getAsIntegral()->getZExtValue(),
4260 Kind, T, Loc));
4261 }
4262
Douglas Gregor02024a92010-03-28 02:42:43 +00004263 if (T->isBooleanType())
4264 return Owned(new (Context) CXXBoolLiteralExpr(
4265 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004266 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004267
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004268 if (T->isNullPtrType())
4269 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4270
Chris Lattner223de242011-04-25 20:37:58 +00004271 // If this is an enum type that we're instantiating, we need to use an integer
4272 // type the same size as the enumerator. We don't want to build an
4273 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004274 QualType BT;
4275 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004276 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004277 else
4278 BT = T;
4279
John McCall4e9272d2011-07-15 07:47:58 +00004280 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
4281 if (T->isEnumeralType()) {
4282 // FIXME: This is a hack. We need a better way to handle substituted
4283 // non-type template parameters.
4284 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4285 Context.getTrivialTypeSourceInfo(T, Loc),
4286 Loc, Loc);
4287 }
4288
4289 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004290}
4291
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004292/// \brief Match two template parameters within template parameter lists.
4293static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4294 bool Complain,
4295 Sema::TemplateParameterListEqualKind Kind,
4296 SourceLocation TemplateArgLoc) {
4297 // Check the actual kind (type, non-type, template).
4298 if (Old->getKind() != New->getKind()) {
4299 if (Complain) {
4300 unsigned NextDiag = diag::err_template_param_different_kind;
4301 if (TemplateArgLoc.isValid()) {
4302 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4303 NextDiag = diag::note_template_param_different_kind;
4304 }
4305 S.Diag(New->getLocation(), NextDiag)
4306 << (Kind != Sema::TPL_TemplateMatch);
4307 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4308 << (Kind != Sema::TPL_TemplateMatch);
4309 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004310
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004311 return false;
4312 }
4313
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004314 // Check that both are parameter packs are neither are parameter packs.
4315 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004316 // template template parameter, the template template parameter can have
4317 // a parameter pack where the template template argument does not.
4318 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4319 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4320 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004321 if (Complain) {
4322 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4323 if (TemplateArgLoc.isValid()) {
4324 S.Diag(TemplateArgLoc,
4325 diag::err_template_arg_template_params_mismatch);
4326 NextDiag = diag::note_template_parameter_pack_non_pack;
4327 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004328
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004329 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4330 : isa<NonTypeTemplateParmDecl>(New)? 1
4331 : 2;
4332 S.Diag(New->getLocation(), NextDiag)
4333 << ParamKind << New->isParameterPack();
4334 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4335 << ParamKind << Old->isParameterPack();
4336 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004337
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004338 return false;
4339 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004340
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004341 // For non-type template parameters, check the type of the parameter.
4342 if (NonTypeTemplateParmDecl *OldNTTP
4343 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4344 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004345
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004346 // If we are matching a template template argument to a template
4347 // template parameter and one of the non-type template parameter types
4348 // is dependent, then we must wait until template instantiation time
4349 // to actually compare the arguments.
4350 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4351 (OldNTTP->getType()->isDependentType() ||
4352 NewNTTP->getType()->isDependentType()))
4353 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004354
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004355 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4356 if (Complain) {
4357 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4358 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004359 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004360 diag::err_template_arg_template_params_mismatch);
4361 NextDiag = diag::note_template_nontype_parm_different_type;
4362 }
4363 S.Diag(NewNTTP->getLocation(), NextDiag)
4364 << NewNTTP->getType()
4365 << (Kind != Sema::TPL_TemplateMatch);
4366 S.Diag(OldNTTP->getLocation(),
4367 diag::note_template_nontype_parm_prev_declaration)
4368 << OldNTTP->getType();
4369 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004370
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004371 return false;
4372 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004373
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004374 return true;
4375 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004376
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004377 // For template template parameters, check the template parameter types.
4378 // The template parameter lists of template template
4379 // parameters must agree.
4380 if (TemplateTemplateParmDecl *OldTTP
4381 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004382 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004383 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4384 OldTTP->getTemplateParameters(),
4385 Complain,
4386 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004387 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004388 : Kind),
4389 TemplateArgLoc);
4390 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004391
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004392 return true;
4393}
Douglas Gregor02024a92010-03-28 02:42:43 +00004394
Douglas Gregora0347822011-01-13 00:08:50 +00004395/// \brief Diagnose a known arity mismatch when comparing template argument
4396/// lists.
4397static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004398void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004399 TemplateParameterList *New,
4400 TemplateParameterList *Old,
4401 Sema::TemplateParameterListEqualKind Kind,
4402 SourceLocation TemplateArgLoc) {
4403 unsigned NextDiag = diag::err_template_param_list_different_arity;
4404 if (TemplateArgLoc.isValid()) {
4405 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4406 NextDiag = diag::note_template_param_list_different_arity;
4407 }
4408 S.Diag(New->getTemplateLoc(), NextDiag)
4409 << (New->size() > Old->size())
4410 << (Kind != Sema::TPL_TemplateMatch)
4411 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4412 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4413 << (Kind != Sema::TPL_TemplateMatch)
4414 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4415}
4416
Douglas Gregorddc29e12009-02-06 22:42:48 +00004417/// \brief Determine whether the given template parameter lists are
4418/// equivalent.
4419///
Mike Stump1eb44332009-09-09 15:08:12 +00004420/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004421/// source code as part of a new template declaration.
4422///
4423/// \param Old The old template parameter list, typically found via
4424/// name lookup of the template declared with this template parameter
4425/// list.
4426///
4427/// \param Complain If true, this routine will produce a diagnostic if
4428/// the template parameter lists are not equivalent.
4429///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004430/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004431///
4432/// \param TemplateArgLoc If this source location is valid, then we
4433/// are actually checking the template parameter list of a template
4434/// argument (New) against the template parameter list of its
4435/// corresponding template template parameter (Old). We produce
4436/// slightly different diagnostics in this scenario.
4437///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004438/// \returns True if the template parameter lists are equal, false
4439/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004440bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004441Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4442 TemplateParameterList *Old,
4443 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004444 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004445 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004446 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4447 if (Complain)
4448 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4449 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004450
4451 return false;
4452 }
4453
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004454 // C++0x [temp.arg.template]p3:
4455 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004456 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004457 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004458 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004459 // template-parameter-list of P. [...]
4460 TemplateParameterList::iterator NewParm = New->begin();
4461 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004462 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004463 OldParmEnd = Old->end();
4464 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004465 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4466 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004467 if (NewParm == NewParmEnd) {
4468 if (Complain)
4469 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4470 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004471
Douglas Gregora0347822011-01-13 00:08:50 +00004472 return false;
4473 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004474
Douglas Gregora0347822011-01-13 00:08:50 +00004475 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4476 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004477 return false;
4478
Douglas Gregora0347822011-01-13 00:08:50 +00004479 ++NewParm;
4480 continue;
4481 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004482
Douglas Gregora0347822011-01-13 00:08:50 +00004483 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004484 // [...] When P's template- parameter-list contains a template parameter
4485 // pack (14.5.3), the template parameter pack will match zero or more
4486 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004487 // template-parameter-list of A with the same type and form as the
4488 // template parameter pack in P (ignoring whether those template
4489 // parameters are template parameter packs).
4490 for (; NewParm != NewParmEnd; ++NewParm) {
4491 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4492 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004493 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004494 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004495 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004496
Douglas Gregora0347822011-01-13 00:08:50 +00004497 // Make sure we exhausted all of the arguments.
4498 if (NewParm != NewParmEnd) {
4499 if (Complain)
4500 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4501 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004502
Douglas Gregora0347822011-01-13 00:08:50 +00004503 return false;
4504 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004505
Douglas Gregorddc29e12009-02-06 22:42:48 +00004506 return true;
4507}
4508
4509/// \brief Check whether a template can be declared within this scope.
4510///
4511/// If the template declaration is valid in this scope, returns
4512/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004513bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004514Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorfb35e8f2011-11-03 16:37:14 +00004515 if (!S)
4516 return false;
4517
Douglas Gregorddc29e12009-02-06 22:42:48 +00004518 // Find the nearest enclosing declaration scope.
4519 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4520 (S->getFlags() & Scope::TemplateParamScope) != 0)
4521 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004522
Douglas Gregorddc29e12009-02-06 22:42:48 +00004523 // C++ [temp]p2:
4524 // A template-declaration can appear only as a namespace scope or
4525 // class scope declaration.
4526 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004527 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4528 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004529 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004530 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004531
Eli Friedman1503f772009-07-31 01:43:05 +00004532 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004533 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004534
4535 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4536 return false;
4537
Mike Stump1eb44332009-09-09 15:08:12 +00004538 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004539 diag::err_template_outside_namespace_or_class_scope)
4540 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004541}
Douglas Gregorcc636682009-02-17 23:15:12 +00004542
Douglas Gregord5cb8762009-10-07 00:13:32 +00004543/// \brief Determine what kind of template specialization the given declaration
4544/// is.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00004545static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004546 if (!D)
4547 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004548
Douglas Gregorf6b11852009-10-08 15:14:33 +00004549 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4550 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004551 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4552 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004553 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4554 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004555
Douglas Gregord5cb8762009-10-07 00:13:32 +00004556 return TSK_Undeclared;
4557}
4558
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004559/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004560/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004561///
Douglas Gregor9302da62009-10-14 23:50:59 +00004562/// This routine determines whether a template specialization can be declared
4563/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004564///
4565/// \param S the semantic analysis object for which this check is being
4566/// performed.
4567///
4568/// \param Specialized the entity being specialized or instantiated, which
4569/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004570/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004571/// member class).
4572///
4573/// \param PrevDecl the previous declaration of this entity, if any.
4574///
4575/// \param Loc the location of the explicit specialization or instantiation of
4576/// this entity.
4577///
4578/// \param IsPartialSpecialization whether this is a partial specialization of
4579/// a class template.
4580///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004581/// \returns true if there was an error that we cannot recover from, false
4582/// otherwise.
4583static bool CheckTemplateSpecializationScope(Sema &S,
4584 NamedDecl *Specialized,
4585 NamedDecl *PrevDecl,
4586 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004587 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004588 // Keep these "kind" numbers in sync with the %select statements in the
4589 // various diagnostics emitted by this routine.
4590 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004591 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004592 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004593 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004594 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004595 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004596 EntityKind = 3;
4597 else if (isa<VarDecl>(Specialized))
4598 EntityKind = 4;
4599 else if (isa<RecordDecl>(Specialized))
4600 EntityKind = 5;
4601 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004602 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4603 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004604 return true;
4605 }
4606
Douglas Gregor88b70942009-02-25 22:02:03 +00004607 // C++ [temp.expl.spec]p2:
4608 // An explicit specialization shall be declared in the namespace
4609 // of which the template is a member, or, for member templates, in
4610 // the namespace of which the enclosing class or enclosing class
4611 // template is a member. An explicit specialization of a member
4612 // function, member class or static data member of a class
4613 // template shall be declared in the namespace of which the class
4614 // template is a member. Such a declaration may also be a
4615 // definition. If the declaration is not a definition, the
4616 // specialization may be defined later in the name- space in which
4617 // the explicit specialization was declared, or in a namespace
4618 // that encloses the one in which the explicit specialization was
4619 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004620 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004621 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004622 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004623 return true;
4624 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004625
Douglas Gregor0a407472009-10-07 17:30:37 +00004626 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004627 if (S.getLangOptions().MicrosoftExt) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00004628 // Do not warn for class scope explicit specialization during
4629 // instantiation, warning was already emitted during pattern
4630 // semantic analysis.
4631 if (!S.ActiveTemplateInstantiations.size())
4632 S.Diag(Loc, diag::ext_function_specialization_in_class)
4633 << Specialized;
4634 } else {
4635 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4636 << Specialized;
4637 return true;
4638 }
Douglas Gregor0a407472009-10-07 17:30:37 +00004639 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004640
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004641 if (S.CurContext->isRecord() &&
4642 !S.CurContext->Equals(Specialized->getDeclContext())) {
4643 // Make sure that we're specializing in the right record context.
4644 // Otherwise, things can go horribly wrong.
4645 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4646 << Specialized;
4647 return true;
4648 }
4649
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004650 // C++ [temp.class.spec]p6:
4651 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004652 // in any namespace scope in which its definition may be defined (14.5.1
4653 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004654 bool ComplainedAboutScope = false;
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004655 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004656 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004657 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004658 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004659 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4660 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004661 // C++ [temp.exp.spec]p2:
4662 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004663 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004664 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004665 // An explicit specialization of a member function, member class or
4666 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004667 // namespace of which the class template is a member.
4668 //
4669 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004670 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004671 // the specialized template.
Richard Smithebaf0e62011-10-18 20:49:44 +00004672 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) {
4673 bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext);
4674 if (isa<TranslationUnitDecl>(SpecializedContext)) {
4675 assert(!IsCPlusPlus0xExtension &&
4676 "DC encloses TU but isn't in enclosing namespace set");
4677 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global)
Douglas Gregora4d5de52010-09-12 05:24:55 +00004678 << EntityKind << Specialized;
Richard Smithebaf0e62011-10-18 20:49:44 +00004679 } else if (isa<NamespaceDecl>(SpecializedContext)) {
4680 int Diag;
4681 if (!IsCPlusPlus0xExtension)
4682 Diag = diag::err_template_spec_decl_out_of_scope;
4683 else if (!S.getLangOptions().CPlusPlus0x)
4684 Diag = diag::ext_template_spec_decl_out_of_scope;
4685 else
4686 Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope;
4687 S.Diag(Loc, Diag)
4688 << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext);
4689 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004690
Douglas Gregor9302da62009-10-14 23:50:59 +00004691 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Richard Smithebaf0e62011-10-18 20:49:44 +00004692 ComplainedAboutScope =
4693 !(IsCPlusPlus0xExtension && S.getLangOptions().CPlusPlus0x);
Douglas Gregor88b70942009-02-25 22:02:03 +00004694 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004695 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004696
4697 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004698 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004699 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004700 // specializations of function templates, static data members, and member
4701 // functions, so we skip the check here for those kinds of entities.
4702 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004703 // Should we refactor that check, so that it occurs later?
4704 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004705 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4706 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004707 if (isa<TranslationUnitDecl>(SpecializedContext))
4708 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4709 << EntityKind << Specialized;
4710 else if (isa<NamespaceDecl>(SpecializedContext))
4711 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4712 << EntityKind << Specialized
4713 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004714
Douglas Gregor9302da62009-10-14 23:50:59 +00004715 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004716 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004717
Douglas Gregord5cb8762009-10-07 00:13:32 +00004718 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004719
Douglas Gregor88b70942009-02-25 22:02:03 +00004720 return false;
4721}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004722
Douglas Gregorbacb9492011-01-03 21:13:47 +00004723/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4724/// that checks non-type template partial specialization arguments.
4725static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4726 NonTypeTemplateParmDecl *Param,
4727 const TemplateArgument *Args,
4728 unsigned NumArgs) {
4729 for (unsigned I = 0; I != NumArgs; ++I) {
4730 if (Args[I].getKind() == TemplateArgument::Pack) {
4731 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004732 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004733 Args[I].pack_size()))
4734 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004735
Douglas Gregore94866f2009-06-12 21:21:02 +00004736 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004737 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004738
Douglas Gregorbacb9492011-01-03 21:13:47 +00004739 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004740 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004741 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004742 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004743
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004744 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004745 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4746 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004747
4748 // Strip off any implicit casts we added as part of type checking.
4749 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4750 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004751
Douglas Gregore94866f2009-06-12 21:21:02 +00004752 // C++ [temp.class.spec]p8:
4753 // A non-type argument is non-specialized if it is the name of a
4754 // non-type parameter. All other non-type arguments are
4755 // specialized.
4756 //
4757 // Below, we check the two conditions that only apply to
4758 // specialized non-type arguments, so skip any non-specialized
4759 // arguments.
4760 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004761 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004762 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004763
Douglas Gregore94866f2009-06-12 21:21:02 +00004764 // C++ [temp.class.spec]p9:
4765 // Within the argument list of a class template partial
4766 // specialization, the following restrictions apply:
4767 // -- A partially specialized non-type argument expression
4768 // shall not involve a template parameter of the partial
4769 // specialization except when the argument expression is a
4770 // simple identifier.
4771 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004772 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004773 diag::err_dependent_non_type_arg_in_partial_spec)
4774 << ArgExpr->getSourceRange();
4775 return true;
4776 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004777
Douglas Gregore94866f2009-06-12 21:21:02 +00004778 // -- The type of a template parameter corresponding to a
4779 // specialized non-type argument shall not be dependent on a
4780 // parameter of the specialization.
4781 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004782 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004783 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4784 << Param->getType()
4785 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004786 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004787 return true;
4788 }
4789 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004790
Douglas Gregorbacb9492011-01-03 21:13:47 +00004791 return false;
4792}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004793
Douglas Gregorbacb9492011-01-03 21:13:47 +00004794/// \brief Check the non-type template arguments of a class template
4795/// partial specialization according to C++ [temp.class.spec]p9.
4796///
4797/// \param TemplateParams the template parameters of the primary class
4798/// template.
4799///
4800/// \param TemplateArg the template arguments of the class template
4801/// partial specialization.
4802///
4803/// \returns true if there was an error, false otherwise.
4804static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4805 TemplateParameterList *TemplateParams,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004806 SmallVectorImpl<TemplateArgument> &TemplateArgs) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004807 const TemplateArgument *ArgList = TemplateArgs.data();
4808
4809 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4810 NonTypeTemplateParmDecl *Param
4811 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4812 if (!Param)
4813 continue;
4814
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004815 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004816 &ArgList[I], 1))
4817 return true;
4818 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004819
4820 return false;
4821}
4822
John McCalld226f652010-08-21 09:40:31 +00004823DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004824Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4825 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004826 SourceLocation KWLoc,
Douglas Gregord023aec2011-09-09 20:53:38 +00004827 SourceLocation ModulePrivateLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004828 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004829 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004830 SourceLocation TemplateNameLoc,
4831 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004832 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004833 SourceLocation RAngleLoc,
4834 AttributeList *Attr,
4835 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004836 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004837
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004838 // NOTE: KWLoc is the location of the tag keyword. This will instead
4839 // store the location of the outermost template keyword in the declaration.
4840 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4841 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4842
Douglas Gregorcc636682009-02-17 23:15:12 +00004843 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004844 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004845 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004846 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4847
4848 if (!ClassTemplate) {
4849 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004850 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004851 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4852 return true;
4853 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004854
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004855 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004856 bool isPartialSpecialization = false;
4857
Douglas Gregor88b70942009-02-25 22:02:03 +00004858 // Check the validity of the template headers that introduce this
4859 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004860 // FIXME: We probably shouldn't complain about these headers for
4861 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004862 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004863 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004864 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4865 TemplateNameLoc,
4866 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004867 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004868 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004869 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004870 isExplicitSpecialization,
4871 Invalid);
4872 if (Invalid)
4873 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004874
Douglas Gregor05396e22009-08-25 17:23:04 +00004875 if (TemplateParams && TemplateParams->size() > 0) {
4876 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004877
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004878 if (TUK == TUK_Friend) {
4879 Diag(KWLoc, diag::err_partial_specialization_friend)
4880 << SourceRange(LAngleLoc, RAngleLoc);
4881 return true;
4882 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004883
Douglas Gregor05396e22009-08-25 17:23:04 +00004884 // C++ [temp.class.spec]p10:
4885 // The template parameter list of a specialization shall not
4886 // contain default template argument values.
4887 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4888 Decl *Param = TemplateParams->getParam(I);
4889 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4890 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004891 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004892 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004893 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004894 }
4895 } else if (NonTypeTemplateParmDecl *NTTP
4896 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4897 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004898 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004899 diag::err_default_arg_in_partial_spec)
4900 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004901 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004902 }
4903 } else {
4904 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004905 if (TTP->hasDefaultArgument()) {
4906 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004907 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004908 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004909 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004910 }
4911 }
4912 }
Douglas Gregora735b202009-10-13 14:39:41 +00004913 } else if (TemplateParams) {
4914 if (TUK == TUK_Friend)
4915 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004916 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004917 SourceRange(TemplateParams->getTemplateLoc(),
4918 TemplateParams->getRAngleLoc()))
4919 << SourceRange(LAngleLoc, RAngleLoc);
4920 else
4921 isExplicitSpecialization = true;
4922 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004923 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004924 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004925 isExplicitSpecialization = true;
4926 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004927
Douglas Gregorcc636682009-02-17 23:15:12 +00004928 // Check that the specialization uses the same tag kind as the
4929 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004930 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
4931 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004932 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00004933 Kind, TUK == TUK_Definition, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00004934 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00004935 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00004936 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00004937 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00004938 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00004939 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00004940 diag::note_previous_use);
4941 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
4942 }
4943
Douglas Gregor40808ce2009-03-09 23:48:35 +00004944 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00004945 TemplateArgumentListInfo TemplateArgs;
4946 TemplateArgs.setLAngleLoc(LAngleLoc);
4947 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00004948 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00004949
Douglas Gregor925910d2011-01-03 20:35:03 +00004950 // Check for unexpanded parameter packs in any of the template arguments.
4951 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004952 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00004953 UPPC_PartialSpecialization))
4954 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004955
Douglas Gregorcc636682009-02-17 23:15:12 +00004956 // Check that the template argument list is well-formed for this
4957 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004958 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00004959 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
4960 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00004961 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00004962
Douglas Gregor910f8002010-11-07 23:05:16 +00004963 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregorcc636682009-02-17 23:15:12 +00004964 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00004965
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004966 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00004967 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004968 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004969 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00004970 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00004971 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00004972 return true;
4973
Douglas Gregor561f8122011-07-01 01:22:09 +00004974 bool InstantiationDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004975 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00004976 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004977 TemplateArgs.getArgumentArray(),
Douglas Gregor561f8122011-07-01 01:22:09 +00004978 TemplateArgs.size(),
4979 InstantiationDependent)) {
Douglas Gregorde090962010-02-09 00:37:32 +00004980 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
4981 << ClassTemplate->getDeclName();
4982 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00004983 }
4984 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004985
Douglas Gregorcc636682009-02-17 23:15:12 +00004986 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004987 ClassTemplateSpecializationDecl *PrevDecl = 0;
4988
4989 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004990 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004991 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004992 = ClassTemplate->findPartialSpecialization(Converted.data(),
4993 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00004994 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004995 else
4996 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00004997 = ClassTemplate->findSpecialization(Converted.data(),
4998 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00004999
5000 ClassTemplateSpecializationDecl *Specialization = 0;
5001
Douglas Gregor88b70942009-02-25 22:02:03 +00005002 // Check whether we can declare a class template specialization in
5003 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005004 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005005 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
5006 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00005007 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00005008 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005009
Douglas Gregorb88e8882009-07-30 17:40:51 +00005010 // The canonical type
5011 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005012 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005013 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00005014 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005015 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005016 // arguments was referenced but not declared, or we're only
5017 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005018 // declaration node as our own, updating its source location and
5019 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005020 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005021 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005022 if (TemplateParameterLists.size() > 0) {
5023 Specialization->setTemplateParameterListsInfo(Context,
5024 TemplateParameterLists.size(),
5025 (TemplateParameterList**) TemplateParameterLists.release());
5026 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005027 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00005028 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005029 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00005030 // Build the canonical type that describes the converted template
5031 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00005032 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
5033 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00005034 Converted.data(),
5035 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005036
5037 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00005038 ClassTemplate->getInjectedClassNameSpecialization())) {
5039 // C++ [temp.class.spec]p9b3:
5040 //
5041 // -- The argument list of the specialization shall not be identical
5042 // to the implicit argument list of the primary template.
5043 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
Douglas Gregor8d267c52011-09-09 02:06:17 +00005044 << (TUK == TUK_Definition)
5045 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
Douglas Gregorb9c66312010-12-23 17:13:55 +00005046 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
5047 ClassTemplate->getIdentifier(),
5048 TemplateNameLoc,
5049 Attr,
5050 TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +00005051 AS_none, /*ModulePrivateLoc=*/SourceLocation(),
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005052 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00005053 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00005054 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00005055
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005056 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005057 ClassTemplatePartialSpecializationDecl *PrevPartial
5058 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005059 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005060 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00005061 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00005062 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005063 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005064 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00005065 TemplateParams,
5066 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005067 Converted.data(),
5068 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00005069 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00005070 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005071 PrevPartial,
5072 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00005073 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005074 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005075 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005076 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00005077 (TemplateParameterList**) TemplateParameterLists.release());
5078 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005079
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005080 if (!PrevPartial)
5081 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005082 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00005083
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005084 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00005085 // template specialization, make a note of that.
5086 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
5087 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005088
Douglas Gregor031a5882009-06-13 00:26:55 +00005089 // Check that all of the template parameters of the class template
5090 // partial specialization are deducible from the template
5091 // arguments. If not, this class template partial specialization
5092 // will never be used.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005093 SmallVector<bool, 8> DeducibleParams;
Douglas Gregor031a5882009-06-13 00:26:55 +00005094 DeducibleParams.resize(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005095 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00005096 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00005097 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00005098 unsigned NumNonDeducible = 0;
5099 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I)
5100 if (!DeducibleParams[I])
5101 ++NumNonDeducible;
5102
5103 if (NumNonDeducible) {
5104 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
5105 << (NumNonDeducible > 1)
5106 << SourceRange(TemplateNameLoc, RAngleLoc);
5107 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
5108 if (!DeducibleParams[I]) {
5109 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
5110 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00005111 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005112 diag::note_partial_spec_unused_parameter)
5113 << Param->getDeclName();
5114 else
Mike Stump1eb44332009-09-09 15:08:12 +00005115 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005116 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00005117 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00005118 }
5119 }
5120 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005121 } else {
5122 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005123 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005124 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005125 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00005126 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005127 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005128 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005129 Converted.data(),
5130 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005131 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005132 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005133 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005134 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005135 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00005136 (TemplateParameterList**) TemplateParameterLists.release());
5137 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005138
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005139 if (!PrevDecl)
5140 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00005141
5142 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005143 }
5144
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005145 // C++ [temp.expl.spec]p6:
5146 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005147 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005148 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005149 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005150 // use occurs; no diagnostic is required.
5151 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005152 bool Okay = false;
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005153 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005154 // Is there any previous explicit specialization declaration?
5155 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5156 Okay = true;
5157 break;
5158 }
5159 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005160
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005161 if (!Okay) {
5162 SourceRange Range(TemplateNameLoc, RAngleLoc);
5163 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
5164 << Context.getTypeDeclType(Specialization) << Range;
5165
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005166 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005167 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005168 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005169 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005170 return true;
5171 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005172 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005173
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005174 // If this is not a friend, note that this is an explicit specialization.
5175 if (TUK != TUK_Friend)
5176 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005177
5178 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005179 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00005180 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005181 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005182 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005183 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00005184 Diag(Def->getLocation(), diag::note_previous_definition);
5185 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00005186 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005187 }
5188 }
5189
John McCall7f1b9872010-12-18 03:30:47 +00005190 if (Attr)
5191 ProcessDeclAttributeList(S, Specialization, Attr);
5192
Douglas Gregord023aec2011-09-09 20:53:38 +00005193 if (ModulePrivateLoc.isValid())
5194 Diag(Specialization->getLocation(), diag::err_module_private_specialization)
5195 << (isPartialSpecialization? 1 : 0)
5196 << FixItHint::CreateRemoval(ModulePrivateLoc);
5197
Douglas Gregorfc705b82009-02-26 22:19:44 +00005198 // Build the fully-sugared type for this class template
5199 // specialization as the user wrote in the specialization
5200 // itself. This means that we'll pretty-print the type retrieved
5201 // from the specialization's declaration the way that the user
5202 // actually wrote the specialization, rather than formatting the
5203 // name based on the "canonical" representation used to store the
5204 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005205 TypeSourceInfo *WrittenTy
5206 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5207 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005208 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005209 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005210 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005211 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00005212 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00005213
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005214 // C++ [temp.expl.spec]p9:
5215 // A template explicit specialization is in the scope of the
5216 // namespace in which the template was defined.
5217 //
5218 // We actually implement this paragraph where we set the semantic
5219 // context (in the creation of the ClassTemplateSpecializationDecl),
5220 // but we also maintain the lexical context where the actual
5221 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005222 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005223
Douglas Gregorcc636682009-02-17 23:15:12 +00005224 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005225 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005226 Specialization->startDefinition();
5227
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005228 if (TUK == TUK_Friend) {
5229 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5230 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005231 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005232 /*FIXME:*/KWLoc);
5233 Friend->setAccess(AS_public);
5234 CurContext->addDecl(Friend);
5235 } else {
5236 // Add the specialization into its lexical context, so that it can
5237 // be seen when iterating through the list of declarations in that
5238 // context. However, specializations are not found by name lookup.
5239 CurContext->addDecl(Specialization);
5240 }
John McCalld226f652010-08-21 09:40:31 +00005241 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005242}
Douglas Gregord57959a2009-03-27 23:10:48 +00005243
John McCalld226f652010-08-21 09:40:31 +00005244Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005245 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005246 Declarator &D) {
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005247 return HandleDeclarator(S, D, move(TemplateParameterLists));
Douglas Gregore542c862009-06-23 23:11:28 +00005248}
5249
John McCalld226f652010-08-21 09:40:31 +00005250Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005251 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005252 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005253 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005254 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005255
Douglas Gregor52591bf2009-06-24 00:54:41 +00005256 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005257 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005258 }
Mike Stump1eb44332009-09-09 15:08:12 +00005259
Douglas Gregor52591bf2009-06-24 00:54:41 +00005260 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005261
Douglas Gregor45fa5602011-11-07 20:56:01 +00005262 D.setFunctionDefinitionKind(FDK_Definition);
John McCalld226f652010-08-21 09:40:31 +00005263 Decl *DP = HandleDeclarator(ParentScope, D,
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005264 move(TemplateParameterLists));
Mike Stump1eb44332009-09-09 15:08:12 +00005265 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005266 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005267 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005268 FunctionTemplate->getTemplatedDecl());
5269 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5270 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5271 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005272}
5273
John McCall75042392010-02-11 01:33:53 +00005274/// \brief Strips various properties off an implicit instantiation
5275/// that has just been explicitly specialized.
5276static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00005277 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005278
5279 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5280 FD->setInlineSpecified(false);
5281 }
5282}
5283
Nico Weberd1d512a2012-01-09 19:52:25 +00005284/// \brief Compute the diagnostic location for an explicit instantiation
5285// declaration or definition.
5286static SourceLocation DiagLocForExplicitInstantiation(
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005287 NamedDecl* D, SourceLocation PointOfInstantiation) {
Nico Weberd1d512a2012-01-09 19:52:25 +00005288 // Explicit instantiations following a specialization have no effect and
5289 // hence no PointOfInstantiation. In that case, walk decl backwards
5290 // until a valid name loc is found.
5291 SourceLocation PrevDiagLoc = PointOfInstantiation;
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005292 for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid();
5293 Prev = Prev->getPreviousDecl()) {
Nico Weberd1d512a2012-01-09 19:52:25 +00005294 PrevDiagLoc = Prev->getLocation();
5295 }
5296 assert(PrevDiagLoc.isValid() &&
5297 "Explicit instantiation without point of instantiation?");
5298 return PrevDiagLoc;
5299}
5300
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005301/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005302/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005303/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005304/// new specialization/instantiation will have any effect.
5305///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005306/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005307/// instantiation.
5308///
5309/// \param NewTSK the kind of the new explicit specialization or instantiation.
5310///
5311/// \param PrevDecl the previous declaration of the entity.
5312///
5313/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5314///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005315/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005316/// declaration was instantiated (either implicitly or explicitly).
5317///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005318/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005319/// specialization or instantiation has no effect and should be ignored.
5320///
5321/// \returns true if there was an error that should prevent the introduction of
5322/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005323bool
5324Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5325 TemplateSpecializationKind NewTSK,
5326 NamedDecl *PrevDecl,
5327 TemplateSpecializationKind PrevTSK,
5328 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005329 bool &HasNoEffect) {
5330 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005331
Douglas Gregor454885e2009-10-15 15:54:05 +00005332 switch (NewTSK) {
5333 case TSK_Undeclared:
5334 case TSK_ImplicitInstantiation:
David Blaikieb219cfc2011-09-23 05:06:16 +00005335 llvm_unreachable("Don't check implicit instantiations here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005336
Douglas Gregor454885e2009-10-15 15:54:05 +00005337 case TSK_ExplicitSpecialization:
5338 switch (PrevTSK) {
5339 case TSK_Undeclared:
5340 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005341 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005342 // explicitly specialized or has merely been mentioned without any
5343 // instantiation.
5344 return false;
5345
5346 case TSK_ImplicitInstantiation:
5347 if (PrevPointOfInstantiation.isInvalid()) {
5348 // The declaration itself has not actually been instantiated, so it is
5349 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005350 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005351 return false;
5352 }
5353 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005354
Douglas Gregor454885e2009-10-15 15:54:05 +00005355 case TSK_ExplicitInstantiationDeclaration:
5356 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005357 assert((PrevTSK == TSK_ImplicitInstantiation ||
5358 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005359 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005360
Douglas Gregor454885e2009-10-15 15:54:05 +00005361 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005362 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005363 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005364 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005365 // implicit instantiation to take place, in every translation unit in
5366 // which such a use occurs; no diagnostic is required.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005367 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005368 // Is there any previous explicit specialization declaration?
5369 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5370 return false;
5371 }
5372
Douglas Gregor0d035142009-10-27 18:42:08 +00005373 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005374 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005375 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005376 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005377
Douglas Gregor454885e2009-10-15 15:54:05 +00005378 return true;
5379 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005380
Douglas Gregor454885e2009-10-15 15:54:05 +00005381 case TSK_ExplicitInstantiationDeclaration:
5382 switch (PrevTSK) {
5383 case TSK_ExplicitInstantiationDeclaration:
5384 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005385 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005386 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005387
Douglas Gregor454885e2009-10-15 15:54:05 +00005388 case TSK_Undeclared:
5389 case TSK_ImplicitInstantiation:
5390 // We're explicitly instantiating something that may have already been
5391 // implicitly instantiated; that's fine.
5392 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005393
Douglas Gregor454885e2009-10-15 15:54:05 +00005394 case TSK_ExplicitSpecialization:
5395 // C++0x [temp.explicit]p4:
5396 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005397 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005398 // specialization for that template, the explicit instantiation has no
5399 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005400 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005401 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005402
Douglas Gregor454885e2009-10-15 15:54:05 +00005403 case TSK_ExplicitInstantiationDefinition:
5404 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005405 // If an entity is the subject of both an explicit instantiation
5406 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005407 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005408 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005409 diag::err_explicit_instantiation_declaration_after_definition);
Nico Weberff91d242011-12-23 20:58:04 +00005410
5411 // Explicit instantiations following a specialization have no effect and
5412 // hence no PrevPointOfInstantiation. In that case, walk decl backwards
5413 // until a valid name loc is found.
Nico Weberd1d512a2012-01-09 19:52:25 +00005414 Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
5415 diag::note_explicit_instantiation_definition_here);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005416 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005417 return false;
5418 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005419
Douglas Gregor454885e2009-10-15 15:54:05 +00005420 case TSK_ExplicitInstantiationDefinition:
5421 switch (PrevTSK) {
5422 case TSK_Undeclared:
5423 case TSK_ImplicitInstantiation:
5424 // We're explicitly instantiating something that may have already been
5425 // implicitly instantiated; that's fine.
5426 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005427
Douglas Gregor454885e2009-10-15 15:54:05 +00005428 case TSK_ExplicitSpecialization:
5429 // C++ DR 259, C++0x [temp.explicit]p4:
5430 // For a given set of template parameters, if an explicit
5431 // instantiation of a template appears after a declaration of
5432 // an explicit specialization for that template, the explicit
5433 // instantiation has no effect.
5434 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005435 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005436 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005437 // has been explicitly specialized.
Richard Smithebaf0e62011-10-18 20:49:44 +00005438 Diag(NewLoc, getLangOptions().CPlusPlus0x ?
5439 diag::warn_cxx98_compat_explicit_instantiation_after_specialization :
5440 diag::ext_explicit_instantiation_after_specialization)
5441 << PrevDecl;
5442 Diag(PrevDecl->getLocation(),
5443 diag::note_previous_template_specialization);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005444 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005445 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005446
Douglas Gregor454885e2009-10-15 15:54:05 +00005447 case TSK_ExplicitInstantiationDeclaration:
5448 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005449 // were previously asked to suppress instantiations. That's fine.
Nico Weberff91d242011-12-23 20:58:04 +00005450
5451 // C++0x [temp.explicit]p4:
5452 // For a given set of template parameters, if an explicit instantiation
5453 // of a template appears after a declaration of an explicit
5454 // specialization for that template, the explicit instantiation has no
5455 // effect.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005456 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Nico Weberff91d242011-12-23 20:58:04 +00005457 // Is there any previous explicit specialization declaration?
5458 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5459 HasNoEffect = true;
5460 break;
5461 }
5462 }
5463
Douglas Gregor454885e2009-10-15 15:54:05 +00005464 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005465
Douglas Gregor454885e2009-10-15 15:54:05 +00005466 case TSK_ExplicitInstantiationDefinition:
5467 // C++0x [temp.spec]p5:
5468 // For a given template and a given set of template-arguments,
5469 // - an explicit instantiation definition shall appear at most once
5470 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005471 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005472 << PrevDecl;
Nico Weberd1d512a2012-01-09 19:52:25 +00005473 Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
Douglas Gregor0d035142009-10-27 18:42:08 +00005474 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005475 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005476 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005477 }
Douglas Gregor454885e2009-10-15 15:54:05 +00005478 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005479
David Blaikieb219cfc2011-09-23 05:06:16 +00005480 llvm_unreachable("Missing specialization/instantiation case?");
Douglas Gregor454885e2009-10-15 15:54:05 +00005481}
5482
John McCallaf2094e2010-04-08 09:05:18 +00005483/// \brief Perform semantic analysis for the given dependent function
5484/// template specialization. The only possible way to get a dependent
5485/// function template specialization is with a friend declaration,
5486/// like so:
5487///
5488/// template <class T> void foo(T);
5489/// template <class T> class A {
5490/// friend void foo<>(T);
5491/// };
5492///
5493/// There really isn't any useful analysis we can do here, so we
5494/// just store the information.
5495bool
5496Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5497 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5498 LookupResult &Previous) {
5499 // Remove anything from Previous that isn't a function template in
5500 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005501 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005502 LookupResult::Filter F = Previous.makeFilter();
5503 while (F.hasNext()) {
5504 NamedDecl *D = F.next()->getUnderlyingDecl();
5505 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005506 !FDLookupContext->InEnclosingNamespaceSetOf(
5507 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005508 F.erase();
5509 }
5510 F.done();
5511
5512 // Should this be diagnosed here?
5513 if (Previous.empty()) return true;
5514
5515 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5516 ExplicitTemplateArgs);
5517 return false;
5518}
5519
Abramo Bagnarae03db982010-05-20 15:32:11 +00005520/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005521/// specialization.
5522///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005523/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005524/// explicit function template specialization. On successful completion,
5525/// the function declaration \p FD will become a function template
5526/// specialization.
5527///
5528/// \param FD the function declaration, which will be updated to become a
5529/// function template specialization.
5530///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005531/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5532/// if any. Note that this may be valid info even when 0 arguments are
5533/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5534/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005535///
Francois Pichet59e7c562011-07-08 06:21:47 +00005536/// \param Previous the set of declarations that may be specialized by
Abramo Bagnarae03db982010-05-20 15:32:11 +00005537/// this function specialization.
5538bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005539Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005540 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005541 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005542 // The set of function template specializations that could match this
5543 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005544 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005545
Sebastian Redl7a126a42010-08-31 00:36:30 +00005546 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005547 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5548 I != E; ++I) {
5549 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5550 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005551 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005552 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005553 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5554 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005555 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005556
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005557 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005558 // A trailing template-argument can be left unspecified in the
5559 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005560 // provided it can be deduced from the function argument type.
5561 // Perform template argument deduction to determine whether we may be
5562 // specializing this template.
5563 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005564 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005565 FunctionDecl *Specialization = 0;
5566 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005567 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005568 FD->getType(),
5569 Specialization,
5570 Info)) {
5571 // FIXME: Template argument deduction failed; record why it failed, so
5572 // that we can provide nifty diagnostics.
5573 (void)TDK;
5574 continue;
5575 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005576
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005577 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005578 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005579 }
5580 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005581
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005582 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005583 UnresolvedSetIterator Result
5584 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005585 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005586 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005587 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005588 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005589 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005590 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005591 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005592 return true;
John McCallc373d482010-01-27 01:50:18 +00005593
5594 // Ignore access information; it doesn't figure into redeclaration checking.
5595 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005596
5597 FunctionTemplateSpecializationInfo *SpecInfo
5598 = Specialization->getTemplateSpecializationInfo();
5599 assert(SpecInfo && "Function template specialization info missing?");
Francois Pichet59e7c562011-07-08 06:21:47 +00005600
5601 // Note: do not overwrite location info if previous template
5602 // specialization kind was explicit.
5603 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5604 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5605 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005606
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005607 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005608 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005609
5610 // If this is a friend declaration, then we're not really declaring
5611 // an explicit specialization.
5612 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005613
Douglas Gregord5cb8762009-10-07 00:13:32 +00005614 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005615 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005616 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005617 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005618 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005619 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005620 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005621
5622 // C++ [temp.expl.spec]p6:
5623 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005624 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005625 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005626 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005627 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005628 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005629 if (!isFriend &&
5630 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005631 TSK_ExplicitSpecialization,
5632 Specialization,
5633 SpecInfo->getTemplateSpecializationKind(),
5634 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005635 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005636 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005637
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005638 // Mark the prior declaration as an explicit specialization, so that later
5639 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005640 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005641 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005642 MarkUnusedFileScopedDecl(Specialization);
5643 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005644
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005645 // Turn the given function declaration into a function template
5646 // specialization, with the template arguments from the previous
5647 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005648 // Take copies of (semantic and syntactic) template argument lists.
5649 const TemplateArgumentList* TemplArgs = new (Context)
5650 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
Douglas Gregor838db382010-02-11 01:19:42 +00005651 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005652 TemplArgs, /*InsertPos=*/0,
5653 SpecInfo->getTemplateSpecializationKind(),
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00005654 ExplicitTemplateArgs);
Douglas Gregore885e182011-05-21 18:53:30 +00005655 FD->setStorageClass(Specialization->getStorageClass());
5656
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005657 // The "previous declaration" for this function template specialization is
5658 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005659 Previous.clear();
5660 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005661 return false;
5662}
5663
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005664/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005665/// specialization.
5666///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005667/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005668/// explicit member function specialization. On successful completion,
5669/// the function declaration \p FD will become a member function
5670/// specialization.
5671///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005672/// \param Member the member declaration, which will be updated to become a
5673/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005674///
John McCall68263142009-11-18 22:49:29 +00005675/// \param Previous the set of declarations, one of which may be specialized
5676/// by this function specialization; the set will be modified to contain the
5677/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005678bool
John McCall68263142009-11-18 22:49:29 +00005679Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005680 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005681
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005682 // Try to find the member we are instantiating.
5683 NamedDecl *Instantiation = 0;
5684 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005685 MemberSpecializationInfo *MSInfo = 0;
5686
John McCall68263142009-11-18 22:49:29 +00005687 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005688 // Nowhere to look anyway.
5689 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005690 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5691 I != E; ++I) {
5692 NamedDecl *D = (*I)->getUnderlyingDecl();
5693 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005694 if (Context.hasSameType(Function->getType(), Method->getType())) {
5695 Instantiation = Method;
5696 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005697 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005698 break;
5699 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005700 }
5701 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005702 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005703 VarDecl *PrevVar;
5704 if (Previous.isSingleResult() &&
5705 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005706 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005707 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005708 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005709 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005710 }
5711 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005712 CXXRecordDecl *PrevRecord;
5713 if (Previous.isSingleResult() &&
5714 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5715 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005716 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005717 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005718 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005719 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005720
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005721 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005722 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005723 // specializations are always out-of-line, the caller will complain about
5724 // this mismatch later.
5725 return false;
5726 }
John McCall77e8b112010-04-13 20:37:33 +00005727
5728 // If this is a friend, just bail out here before we start turning
5729 // things into explicit specializations.
5730 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5731 // Preserve instantiation information.
5732 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5733 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5734 cast<CXXMethodDecl>(InstantiatedFrom),
5735 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5736 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5737 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5738 cast<CXXRecordDecl>(InstantiatedFrom),
5739 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5740 }
5741
5742 Previous.clear();
5743 Previous.addDecl(Instantiation);
5744 return false;
5745 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005746
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005747 // Make sure that this is a specialization of a member.
5748 if (!InstantiatedFrom) {
5749 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5750 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005751 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5752 return true;
5753 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005754
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005755 // C++ [temp.expl.spec]p6:
5756 // If a template, a member template or the member of a class template is
Nico Weberff91d242011-12-23 20:58:04 +00005757 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005758 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005759 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005760 // use occurs; no diagnostic is required.
5761 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005762
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005763 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005764 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5765 TSK_ExplicitSpecialization,
5766 Instantiation,
5767 MSInfo->getTemplateSpecializationKind(),
5768 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005769 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005770 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005771
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005772 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005773 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005774 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005775 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005776 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005777 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005778
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005779 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005780 // the original declaration to note that it is an explicit specialization
5781 // (if it was previously an implicit instantiation). This latter step
5782 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005783 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005784 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5785 if (InstantiationFunction->getTemplateSpecializationKind() ==
5786 TSK_ImplicitInstantiation) {
5787 InstantiationFunction->setTemplateSpecializationKind(
5788 TSK_ExplicitSpecialization);
5789 InstantiationFunction->setLocation(Member->getLocation());
5790 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005791
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005792 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5793 cast<CXXMethodDecl>(InstantiatedFrom),
5794 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005795 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005796 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005797 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5798 if (InstantiationVar->getTemplateSpecializationKind() ==
5799 TSK_ImplicitInstantiation) {
5800 InstantiationVar->setTemplateSpecializationKind(
5801 TSK_ExplicitSpecialization);
5802 InstantiationVar->setLocation(Member->getLocation());
5803 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005804
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005805 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5806 cast<VarDecl>(InstantiatedFrom),
5807 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005808 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005809 } else {
5810 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005811 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5812 if (InstantiationClass->getTemplateSpecializationKind() ==
5813 TSK_ImplicitInstantiation) {
5814 InstantiationClass->setTemplateSpecializationKind(
5815 TSK_ExplicitSpecialization);
5816 InstantiationClass->setLocation(Member->getLocation());
5817 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005818
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005819 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005820 cast<CXXRecordDecl>(InstantiatedFrom),
5821 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005822 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005823
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005824 // Save the caller the trouble of having to figure out which declaration
5825 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005826 Previous.clear();
5827 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005828 return false;
5829}
5830
Douglas Gregor558c0322009-10-14 23:41:34 +00005831/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005832///
5833/// \returns true if a serious error occurs, false otherwise.
5834static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005835 SourceLocation InstLoc,
5836 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005837 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5838 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005839
Douglas Gregor669eed82010-07-13 00:10:04 +00005840 if (CurContext->isRecord()) {
5841 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5842 << D;
5843 return true;
5844 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005845
Richard Smith3e2e91e2011-10-18 02:28:33 +00005846 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005847 // An explicit instantiation shall appear in an enclosing namespace of its
Richard Smith3e2e91e2011-10-18 02:28:33 +00005848 // template. If the name declared in the explicit instantiation is an
5849 // unqualified name, the explicit instantiation shall appear in the
5850 // namespace where its template is declared or, if that namespace is inline
5851 // (7.3.1), any namespace from its enclosing namespace set.
Douglas Gregor558c0322009-10-14 23:41:34 +00005852 //
5853 // This is DR275, which we do not retroactively apply to C++98/03.
Richard Smith3e2e91e2011-10-18 02:28:33 +00005854 if (WasQualifiedName) {
5855 if (CurContext->Encloses(OrigContext))
5856 return false;
5857 } else {
5858 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
5859 return false;
5860 }
5861
5862 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) {
5863 if (WasQualifiedName)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005864 S.Diag(InstLoc,
5865 S.getLangOptions().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005866 diag::err_explicit_instantiation_out_of_scope :
5867 diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005868 << D << NS;
5869 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005870 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005871 S.getLangOptions().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005872 diag::err_explicit_instantiation_unqualified_wrong_namespace :
5873 diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
5874 << D << NS;
5875 } else
5876 S.Diag(InstLoc,
5877 S.getLangOptions().CPlusPlus0x?
5878 diag::err_explicit_instantiation_must_be_global :
5879 diag::warn_explicit_instantiation_must_be_global_0x)
5880 << D;
Douglas Gregor558c0322009-10-14 23:41:34 +00005881 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005882 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005883}
5884
5885/// \brief Determine whether the given scope specifier has a template-id in it.
5886static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5887 if (!SS.isSet())
5888 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005889
Richard Smith3e2e91e2011-10-18 02:28:33 +00005890 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005891 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005892 // or a static data member of a class template specialization, the name of
5893 // the class template specialization in the qualified-id for the member
5894 // name shall be a simple-template-id.
5895 //
5896 // C++98 has the same restriction, just worded differently.
5897 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5898 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005899 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005900 if (isa<TemplateSpecializationType>(T))
5901 return true;
5902
5903 return false;
5904}
5905
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005906// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005907DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005908Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005909 SourceLocation ExternLoc,
5910 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005911 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005912 SourceLocation KWLoc,
5913 const CXXScopeSpec &SS,
5914 TemplateTy TemplateD,
5915 SourceLocation TemplateNameLoc,
5916 SourceLocation LAngleLoc,
5917 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005918 SourceLocation RAngleLoc,
5919 AttributeList *Attr) {
5920 // Find the class template we're specializing
5921 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005922 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005923 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5924
5925 // Check that the specialization uses the same tag kind as the
5926 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005927 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5928 assert(Kind != TTK_Enum &&
5929 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005930 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00005931 Kind, /*isDefinition*/false, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005932 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005933 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005934 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005935 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005936 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005937 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005938 diag::note_previous_use);
5939 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5940 }
5941
Douglas Gregor558c0322009-10-14 23:41:34 +00005942 // C++0x [temp.explicit]p2:
5943 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005944 // definition and an explicit instantiation declaration. An explicit
5945 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00005946 TemplateSpecializationKind TSK
5947 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
5948 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005949
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005950 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005951 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005952 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005953
5954 // Check that the template argument list is well-formed for this
5955 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005956 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005957 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5958 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005959 return true;
5960
Douglas Gregor910f8002010-11-07 23:05:16 +00005961 assert((Converted.size() == ClassTemplate->getTemplateParameters()->size()) &&
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005962 "Converted template argument list is too short!");
Mike Stump1eb44332009-09-09 15:08:12 +00005963
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005964 // Find the class template specialization declaration that
5965 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005966 void *InsertPos = 0;
5967 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005968 = ClassTemplate->findSpecialization(Converted.data(),
5969 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005970
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005971 TemplateSpecializationKind PrevDecl_TSK
5972 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
5973
Douglas Gregord5cb8762009-10-07 00:13:32 +00005974 // C++0x [temp.explicit]p2:
5975 // [...] An explicit instantiation shall appear in an enclosing
5976 // namespace of its template. [...]
5977 //
5978 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00005979 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
5980 SS.isSet()))
5981 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005982
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005983 ClassTemplateSpecializationDecl *Specialization = 0;
5984
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005985 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005986 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00005987 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005988 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00005989 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005990 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00005991 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005992
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005993 // Even though HasNoEffect == true means that this explicit instantiation
5994 // has no effect on semantics, we go on to put its syntax in the AST.
5995
5996 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
5997 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00005998 // Since the only prior class template specialization with these
5999 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006000 // declaration node as our own, updating the source location
6001 // for the template name to reflect our new declaration.
6002 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00006003 Specialization = PrevDecl;
6004 Specialization->setLocation(TemplateNameLoc);
6005 PrevDecl = 0;
6006 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006007 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006008
Douglas Gregor52604ab2009-09-11 21:19:12 +00006009 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006010 // Create a new class template specialization declaration node for
6011 // this explicit specialization.
6012 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00006013 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006014 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00006015 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006016 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00006017 Converted.data(),
6018 Converted.size(),
6019 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00006020 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006021
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00006022 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006023 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00006024 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006025 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006026 }
6027
6028 // Build the fully-sugared type for this explicit instantiation as
6029 // the user wrote in the explicit instantiation itself. This means
6030 // that we'll pretty-print the type retrieved from the
6031 // specialization's declaration the way that the user actually wrote
6032 // the explicit instantiation, rather than formatting the name based
6033 // on the "canonical" representation used to store the template
6034 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00006035 TypeSourceInfo *WrittenTy
6036 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
6037 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006038 Context.getTypeDeclType(Specialization));
6039 Specialization->setTypeAsWritten(WrittenTy);
6040 TemplateArgsIn.release();
6041
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006042 // Set source locations for keywords.
6043 Specialization->setExternLoc(ExternLoc);
6044 Specialization->setTemplateKeywordLoc(TemplateLoc);
6045
Rafael Espindola0257b7f2012-01-03 06:04:21 +00006046 if (Attr)
6047 ProcessDeclAttributeList(S, Specialization, Attr);
6048
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006049 // Add the explicit instantiation into its lexical context. However,
6050 // since explicit instantiations are never found by name lookup, we
6051 // just put it into the declaration context directly.
6052 Specialization->setLexicalDeclContext(CurContext);
6053 CurContext->addDecl(Specialization);
6054
6055 // Syntax is now OK, so return if it has no other effect on semantics.
6056 if (HasNoEffect) {
6057 // Set the template specialization kind.
6058 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006059 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00006060 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006061
6062 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006063 // A definition of a class template or class member template
6064 // shall be in scope at the point of the explicit instantiation of
6065 // the class template or class member template.
6066 //
6067 // This check comes when we actually try to perform the
6068 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006069 ClassTemplateSpecializationDecl *Def
6070 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006071 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006072 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006073 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006074 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006075 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006076 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
6077 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006078
Douglas Gregor0d035142009-10-27 18:42:08 +00006079 // Instantiate the members of this class template specialization.
6080 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006081 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006082 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00006083 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
6084
6085 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
6086 // TSK_ExplicitInstantiationDefinition
6087 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
6088 TSK == TSK_ExplicitInstantiationDefinition)
6089 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006090
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006091 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006092 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006093
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006094 // Set the template specialization kind.
6095 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006096 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006097}
6098
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006099// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00006100DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00006101Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00006102 SourceLocation ExternLoc,
6103 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00006104 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006105 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00006106 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006107 IdentifierInfo *Name,
6108 SourceLocation NameLoc,
6109 AttributeList *Attr) {
6110
Douglas Gregor402abb52009-05-28 23:31:59 +00006111 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00006112 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00006113 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00006114 KWLoc, SS, Name, NameLoc, Attr, AS_none,
Douglas Gregore7612302011-09-09 19:05:14 +00006115 /*ModulePrivateLoc=*/SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +00006116 MultiTemplateParamsArg(*this, 0, 0),
Richard Smithbdad7a22012-01-10 01:33:14 +00006117 Owned, IsDependent, SourceLocation(), false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00006118 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00006119 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
6120
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006121 if (!TagD)
6122 return true;
6123
John McCalld226f652010-08-21 09:40:31 +00006124 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006125 if (Tag->isEnum()) {
6126 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
6127 << Context.getTypeDeclType(Tag);
6128 return true;
6129 }
6130
Douglas Gregord0c87372009-05-27 17:30:49 +00006131 if (Tag->isInvalidDecl())
6132 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006133
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006134 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
6135 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
6136 if (!Pattern) {
6137 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
6138 << Context.getTypeDeclType(Record);
6139 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
6140 return true;
6141 }
6142
Douglas Gregor558c0322009-10-14 23:41:34 +00006143 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006144 // If the explicit instantiation is for a class or member class, the
6145 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00006146 // simple-template-id.
6147 //
6148 // C++98 has the same restriction, just worded differently.
6149 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00006150 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006151 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006152
Douglas Gregor558c0322009-10-14 23:41:34 +00006153 // C++0x [temp.explicit]p2:
6154 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006155 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00006156 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00006157 TemplateSpecializationKind TSK
6158 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6159 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006160
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006161 // C++0x [temp.explicit]p2:
6162 // [...] An explicit instantiation shall appear in an enclosing
6163 // namespace of its template. [...]
6164 //
6165 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00006166 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006167
Douglas Gregor454885e2009-10-15 15:54:05 +00006168 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006169 CXXRecordDecl *PrevDecl
Douglas Gregoref96ee02012-01-14 16:38:05 +00006170 = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl());
Douglas Gregor952b0172010-02-11 01:04:33 +00006171 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00006172 PrevDecl = Record;
6173 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00006174 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006175 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00006176 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006177 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00006178 PrevDecl,
6179 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006180 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006181 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006182 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006183 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00006184 return TagD;
6185 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006186
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006187 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00006188 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006189 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006190 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006191 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006192 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006193 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00006194 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006195 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00006196 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
6197 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006198 Diag(Pattern->getLocation(), diag::note_forward_declaration)
6199 << Pattern;
6200 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00006201 } else {
6202 if (InstantiateClass(NameLoc, Record, Def,
6203 getTemplateInstantiationArgs(Record),
6204 TSK))
6205 return true;
6206
Douglas Gregor952b0172010-02-11 01:04:33 +00006207 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00006208 if (!RecordDef)
6209 return true;
6210 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006211 }
6212
Douglas Gregor0d035142009-10-27 18:42:08 +00006213 // Instantiate all of the members of the class.
6214 InstantiateClassMembers(NameLoc, RecordDef,
6215 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006216
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006217 if (TSK == TSK_ExplicitInstantiationDefinition)
6218 MarkVTableUsed(NameLoc, RecordDef, true);
6219
Mike Stump390b4cc2009-05-16 07:39:55 +00006220 // FIXME: We don't have any representation for explicit instantiations of
6221 // member classes. Such a representation is not needed for compilation, but it
6222 // should be available for clients that want to see all of the declarations in
6223 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006224 return TagD;
6225}
6226
John McCallf312b1e2010-08-26 23:41:50 +00006227DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
6228 SourceLocation ExternLoc,
6229 SourceLocation TemplateLoc,
6230 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006231 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00006232 // TODO: check if/when DNInfo should replace Name.
6233 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
6234 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006235 if (!Name) {
6236 if (!D.isInvalidType())
6237 Diag(D.getDeclSpec().getSourceRange().getBegin(),
6238 diag::err_explicit_instantiation_requires_name)
6239 << D.getDeclSpec().getSourceRange()
6240 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006241
Douglas Gregord5a423b2009-09-25 18:43:00 +00006242 return true;
6243 }
6244
6245 // The scope passed in may not be a decl scope. Zip up the scope tree until
6246 // we find one that is.
6247 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6248 (S->getFlags() & Scope::TemplateParamScope) != 0)
6249 S = S->getParent();
6250
6251 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006252 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6253 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006254 if (R.isNull())
6255 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006256
Douglas Gregore885e182011-05-21 18:53:30 +00006257 // C++ [dcl.stc]p1:
6258 // A storage-class-specifier shall not be specified in [...] an explicit
6259 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006260 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006261 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6262 << Name;
6263 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006264 } else if (D.getDeclSpec().getStorageClassSpec()
6265 != DeclSpec::SCS_unspecified) {
6266 // Complain about then remove the storage class specifier.
6267 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6268 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6269
6270 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006271 }
6272
Douglas Gregor663b5a02009-10-14 20:14:33 +00006273 // C++0x [temp.explicit]p1:
6274 // [...] An explicit instantiation of a function template shall not use the
6275 // inline or constexpr specifiers.
6276 // Presumably, this also applies to member functions of class templates as
6277 // well.
Richard Smith2dc7ece2011-10-18 03:44:03 +00006278 if (D.getDeclSpec().isInlineSpecified())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006279 Diag(D.getDeclSpec().getInlineSpecLoc(),
Richard Smith2dc7ece2011-10-18 03:44:03 +00006280 getLangOptions().CPlusPlus0x ?
6281 diag::err_explicit_instantiation_inline :
6282 diag::warn_explicit_instantiation_inline_0x)
Richard Smithfe6f6482011-10-14 19:58:02 +00006283 << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
6284 if (D.getDeclSpec().isConstexprSpecified())
6285 // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is
6286 // not already specified.
6287 Diag(D.getDeclSpec().getConstexprSpecLoc(),
6288 diag::err_explicit_instantiation_constexpr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006289
Douglas Gregor558c0322009-10-14 23:41:34 +00006290 // C++0x [temp.explicit]p2:
6291 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006292 // definition and an explicit instantiation declaration. An explicit
6293 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006294 TemplateSpecializationKind TSK
6295 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6296 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006297
Abramo Bagnara25777432010-08-11 22:01:17 +00006298 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006299 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006300
6301 if (!R->isFunctionType()) {
6302 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006303 // A [...] static data member of a class template can be explicitly
6304 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006305 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006306 if (Previous.isAmbiguous())
6307 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006308
John McCall1bcee0a2009-12-02 08:25:40 +00006309 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006310 if (!Prev || !Prev->isStaticDataMember()) {
6311 // We expect to see a data data member here.
6312 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6313 << Name;
6314 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6315 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006316 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006317 return true;
6318 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006319
Douglas Gregord5a423b2009-09-25 18:43:00 +00006320 if (!Prev->getInstantiatedFromStaticDataMember()) {
6321 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006322 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006323 diag::err_explicit_instantiation_data_member_not_instantiated)
6324 << Prev;
6325 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6326 // FIXME: Can we provide a note showing where this was declared?
6327 return true;
6328 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006329
Douglas Gregor558c0322009-10-14 23:41:34 +00006330 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006331 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006332 // or a static data member of a class template specialization, the name of
6333 // the class template specialization in the qualified-id for the member
6334 // name shall be a simple-template-id.
6335 //
6336 // C++98 has the same restriction, just worded differently.
6337 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006338 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006339 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006340 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006341
Douglas Gregor558c0322009-10-14 23:41:34 +00006342 // Check the scope of this explicit instantiation.
6343 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006344
Douglas Gregor454885e2009-10-15 15:54:05 +00006345 // Verify that it is okay to explicitly instantiate here.
6346 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6347 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006348 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006349 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006350 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006351 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006352 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006353 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006354 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006355 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006356
Douglas Gregord5a423b2009-09-25 18:43:00 +00006357 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006358 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006359 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006360 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006361
Douglas Gregord5a423b2009-09-25 18:43:00 +00006362 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006363 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006364 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006365
6366 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006367 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006368 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006369 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006370 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6371 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006372 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6373 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006374 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6375 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006376 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006377 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006378 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006379 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006380 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006381
Douglas Gregord5a423b2009-09-25 18:43:00 +00006382 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006383 // A [...] function [...] can be explicitly instantiated from its template.
6384 // A member function [...] of a class template can be explicitly
6385 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006386 // template.
John McCallc373d482010-01-27 01:50:18 +00006387 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006388 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6389 P != PEnd; ++P) {
6390 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006391 if (!HasExplicitTemplateArgs) {
6392 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6393 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6394 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006395
John McCallc373d482010-01-27 01:50:18 +00006396 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006397 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6398 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006399 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006400 }
6401 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006402
Douglas Gregord5a423b2009-09-25 18:43:00 +00006403 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6404 if (!FunTmpl)
6405 continue;
6406
John McCall5769d612010-02-08 23:07:23 +00006407 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006408 FunctionDecl *Specialization = 0;
6409 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006410 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006411 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006412 R, Specialization, Info)) {
6413 // FIXME: Keep track of almost-matches?
6414 (void)TDK;
6415 continue;
6416 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006417
John McCallc373d482010-01-27 01:50:18 +00006418 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006419 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006420
Douglas Gregord5a423b2009-09-25 18:43:00 +00006421 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006422 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006423 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006424 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006425 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6426 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6427 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006428
John McCallc373d482010-01-27 01:50:18 +00006429 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006430 return true;
John McCallc373d482010-01-27 01:50:18 +00006431
6432 // Ignore access control bits, we don't need them for redeclaration checking.
6433 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006434
Douglas Gregor0a897e32009-10-15 17:21:20 +00006435 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006436 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006437 diag::err_explicit_instantiation_member_function_not_instantiated)
6438 << Specialization
6439 << (Specialization->getTemplateSpecializationKind() ==
6440 TSK_ExplicitSpecialization);
6441 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6442 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006443 }
6444
Douglas Gregoref96ee02012-01-14 16:38:05 +00006445 FunctionDecl *PrevDecl = Specialization->getPreviousDecl();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006446 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6447 PrevDecl = Specialization;
6448
Douglas Gregor0a897e32009-10-15 17:21:20 +00006449 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006450 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006451 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006452 PrevDecl,
6453 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006454 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006455 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006456 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006457
Douglas Gregor0a897e32009-10-15 17:21:20 +00006458 // FIXME: We may still want to build some representation of this
6459 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006460 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006461 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006462 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006463
6464 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Rafael Espindola256fc4d2012-01-04 05:40:59 +00006465 AttributeList *Attr = D.getDeclSpec().getAttributes().getList();
6466 if (Attr)
6467 ProcessDeclAttributeList(S, Specialization, Attr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006468
Douglas Gregor0a897e32009-10-15 17:21:20 +00006469 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006470 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006471
Douglas Gregor558c0322009-10-14 23:41:34 +00006472 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006473 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006474 // or a static data member of a class template specialization, the name of
6475 // the class template specialization in the qualified-id for the member
6476 // name shall be a simple-template-id.
6477 //
6478 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006479 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006480 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006481 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006482 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006483 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006484 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006485 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006486
Douglas Gregor558c0322009-10-14 23:41:34 +00006487 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006488 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006489 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006490 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006491 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006492
Douglas Gregord5a423b2009-09-25 18:43:00 +00006493 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006494 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006495}
6496
John McCallf312b1e2010-08-26 23:41:50 +00006497TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006498Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6499 const CXXScopeSpec &SS, IdentifierInfo *Name,
6500 SourceLocation TagLoc, SourceLocation NameLoc) {
6501 // This has to hold, because SS is expected to be defined.
6502 assert(Name && "Expected a name in a dependent tag");
6503
6504 NestedNameSpecifier *NNS
6505 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6506 if (!NNS)
6507 return true;
6508
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006509 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006510
Douglas Gregor48c89f42010-04-24 16:38:41 +00006511 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6512 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006513 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006514 return true;
6515 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006516
Douglas Gregor059101f2011-03-02 00:47:37 +00006517 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006518 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006519 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6520
6521 // Create type-source location information for this type.
6522 TypeLocBuilder TLB;
6523 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6524 TL.setKeywordLoc(TagLoc);
6525 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6526 TL.setNameLoc(NameLoc);
6527 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006528}
6529
John McCallf312b1e2010-08-26 23:41:50 +00006530TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006531Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6532 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006533 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006534 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006535 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006536
Richard Smithebaf0e62011-10-18 20:49:44 +00006537 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6538 Diag(TypenameLoc,
6539 getLangOptions().CPlusPlus0x ?
6540 diag::warn_cxx98_compat_typename_outside_of_template :
6541 diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006542 << FixItHint::CreateRemoval(TypenameLoc);
6543
Douglas Gregor2494dd02011-03-01 01:34:45 +00006544 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006545 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6546 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006547 if (T.isNull())
6548 return true;
John McCall63b43852010-04-29 23:50:39 +00006549
6550 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6551 if (isa<DependentNameType>(T)) {
6552 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006553 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006554 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006555 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006556 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006557 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006558 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006559 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006560 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006561 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006562
John McCallb3d87482010-08-24 05:47:05 +00006563 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006564}
6565
John McCallf312b1e2010-08-26 23:41:50 +00006566TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006567Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6568 const CXXScopeSpec &SS,
6569 SourceLocation TemplateLoc,
6570 TemplateTy TemplateIn,
6571 SourceLocation TemplateNameLoc,
6572 SourceLocation LAngleLoc,
6573 ASTTemplateArgsPtr TemplateArgsIn,
6574 SourceLocation RAngleLoc) {
Richard Smithebaf0e62011-10-18 20:49:44 +00006575 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6576 Diag(TypenameLoc,
6577 getLangOptions().CPlusPlus0x ?
6578 diag::warn_cxx98_compat_typename_outside_of_template :
6579 diag::ext_typename_outside_of_template)
6580 << FixItHint::CreateRemoval(TypenameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006581
6582 // Translate the parser's template argument list in our AST format.
6583 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6584 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6585
6586 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006587 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6588 // Construct a dependent template specialization type.
6589 assert(DTN && "dependent template has non-dependent name?");
6590 assert(DTN->getQualifier()
6591 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6592 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6593 DTN->getQualifier(),
6594 DTN->getIdentifier(),
6595 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006596
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006597 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006598 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006599 DependentTemplateSpecializationTypeLoc SpecTL
6600 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006601 SpecTL.setLAngleLoc(LAngleLoc);
6602 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006603 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006604 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006605 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006606 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6607 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006608 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006609 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006610
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006611 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6612 if (T.isNull())
6613 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006614
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006615 // Provide source-location information for the template specialization
6616 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006617 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006618 TemplateSpecializationTypeLoc SpecTL
6619 = Builder.push<TemplateSpecializationTypeLoc>(T);
6620
6621 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006622 SpecTL.setLAngleLoc(LAngleLoc);
6623 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006624 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006625 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6626 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6627
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006628 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6629 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6630 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006631 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6632
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006633 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6634 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006635}
6636
Douglas Gregora02411e2011-02-27 22:46:49 +00006637
Douglas Gregord57959a2009-03-27 23:10:48 +00006638/// \brief Build the type that describes a C++ typename specifier,
6639/// e.g., "typename T::type".
6640QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006641Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6642 SourceLocation KeywordLoc,
6643 NestedNameSpecifierLoc QualifierLoc,
6644 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006645 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006646 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006647 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006648
John McCall77bb1aa2010-05-01 00:40:08 +00006649 DeclContext *Ctx = computeDeclContext(SS);
6650 if (!Ctx) {
6651 // If the nested-name-specifier is dependent and couldn't be
6652 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006653 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6654 return Context.getDependentNameType(Keyword,
6655 QualifierLoc.getNestedNameSpecifier(),
6656 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006657 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006658
John McCall77bb1aa2010-05-01 00:40:08 +00006659 // If the nested-name-specifier refers to the current instantiation,
6660 // the "typename" keyword itself is superfluous. In C++03, the
6661 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6662 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006663 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006664
John McCall77bb1aa2010-05-01 00:40:08 +00006665 if (RequireCompleteDeclContext(SS, Ctx))
6666 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006667
6668 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006669 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006670 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006671 unsigned DiagID = 0;
6672 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006673 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006674 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006675 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006676 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006677
6678 case LookupResult::FoundUnresolvedValue: {
6679 // We found a using declaration that is a value. Most likely, the using
6680 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006681 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006682 IILoc);
6683 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6684 << Name << Ctx << FullRange;
6685 if (UnresolvedUsingValueDecl *Using
6686 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006687 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006688 Diag(Loc, diag::note_using_value_decl_missing_typename)
6689 << FixItHint::CreateInsertion(Loc, "typename ");
6690 }
6691 }
6692 // Fall through to create a dependent typename type, from which we can recover
6693 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006694
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006695 case LookupResult::NotFoundInCurrentInstantiation:
6696 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006697 return Context.getDependentNameType(Keyword,
6698 QualifierLoc.getNestedNameSpecifier(),
6699 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006700
6701 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006702 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006703 // We found a type. Build an ElaboratedType, since the
6704 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006705 return Context.getElaboratedType(ETK_Typename,
6706 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006707 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006708 }
6709
6710 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006711 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006712 break;
6713
6714 case LookupResult::FoundOverloaded:
6715 DiagID = diag::err_typename_nested_not_type;
6716 Referenced = *Result.begin();
6717 break;
6718
John McCall6e247262009-10-10 05:48:19 +00006719 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006720 return QualType();
6721 }
6722
6723 // If we get here, it's because name lookup did not find a
6724 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006725 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006726 IILoc);
6727 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006728 if (Referenced)
6729 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6730 << Name;
6731 return QualType();
6732}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006733
6734namespace {
6735 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006736 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006737 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006738 SourceLocation Loc;
6739 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006740
Douglas Gregor4a959d82009-08-06 16:20:37 +00006741 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006742 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006743
Mike Stump1eb44332009-09-09 15:08:12 +00006744 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006745 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006746 DeclarationName Entity)
6747 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006748 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006749
6750 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006751 /// transformed.
6752 ///
6753 /// For the purposes of type reconstruction, a type has already been
6754 /// transformed if it is NULL or if it is not dependent.
6755 bool AlreadyTransformed(QualType T) {
6756 return T.isNull() || !T->isDependentType();
6757 }
Mike Stump1eb44332009-09-09 15:08:12 +00006758
6759 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006760 /// rebuilt.
6761 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006762
Douglas Gregor4a959d82009-08-06 16:20:37 +00006763 /// \brief Returns the name of the entity whose type is being rebuilt.
6764 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006765
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006766 /// \brief Sets the "base" location and entity when that
6767 /// information is known based on another transformation.
6768 void setBase(SourceLocation Loc, DeclarationName Entity) {
6769 this->Loc = Loc;
6770 this->Entity = Entity;
6771 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006772 };
6773}
6774
Douglas Gregor4a959d82009-08-06 16:20:37 +00006775/// \brief Rebuilds a type within the context of the current instantiation.
6776///
Mike Stump1eb44332009-09-09 15:08:12 +00006777/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006778/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006779/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006780/// partial specialization thereof). This routine will rebuild that type now
6781/// that we have entered the declarator's scope, which may produce different
6782/// canonical types, e.g.,
6783///
6784/// \code
6785/// template<typename T>
6786/// struct X {
6787/// typedef T* pointer;
6788/// pointer data();
6789/// };
6790///
6791/// template<typename T>
6792/// typename X<T>::pointer X<T>::data() { ... }
6793/// \endcode
6794///
Douglas Gregor4714c122010-03-31 17:34:00 +00006795/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006796/// since we do not know that we can look into X<T> when we parsed the type.
6797/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006798/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006799/// as the canonical type of T*, allowing the return types of the out-of-line
6800/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006801TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6802 SourceLocation Loc,
6803 DeclarationName Name) {
6804 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006805 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006806
Douglas Gregor4a959d82009-08-06 16:20:37 +00006807 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6808 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006809}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006810
John McCall60d7b3a2010-08-24 06:29:42 +00006811ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006812 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6813 DeclarationName());
6814 return Rebuilder.TransformExpr(E);
6815}
6816
John McCall63b43852010-04-29 23:50:39 +00006817bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006818 if (SS.isInvalid())
6819 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006820
Douglas Gregor7e384942011-02-25 16:07:42 +00006821 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006822 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6823 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006824 NestedNameSpecifierLoc Rebuilt
6825 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6826 if (!Rebuilt)
6827 return true;
John McCall63b43852010-04-29 23:50:39 +00006828
Douglas Gregor7e384942011-02-25 16:07:42 +00006829 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006830 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006831}
6832
Douglas Gregor20606502011-10-14 15:31:12 +00006833/// \brief Rebuild the template parameters now that we know we're in a current
6834/// instantiation.
6835bool Sema::RebuildTemplateParamsInCurrentInstantiation(
6836 TemplateParameterList *Params) {
6837 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
6838 Decl *Param = Params->getParam(I);
6839
6840 // There is nothing to rebuild in a type parameter.
6841 if (isa<TemplateTypeParmDecl>(Param))
6842 continue;
6843
6844 // Rebuild the template parameter list of a template template parameter.
6845 if (TemplateTemplateParmDecl *TTP
6846 = dyn_cast<TemplateTemplateParmDecl>(Param)) {
6847 if (RebuildTemplateParamsInCurrentInstantiation(
6848 TTP->getTemplateParameters()))
6849 return true;
6850
6851 continue;
6852 }
6853
6854 // Rebuild the type of a non-type template parameter.
6855 NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param);
6856 TypeSourceInfo *NewTSI
6857 = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(),
6858 NTTP->getLocation(),
6859 NTTP->getDeclName());
6860 if (!NewTSI)
6861 return true;
6862
6863 if (NewTSI != NTTP->getTypeSourceInfo()) {
6864 NTTP->setTypeSourceInfo(NewTSI);
6865 NTTP->setType(NewTSI->getType());
6866 }
6867 }
6868
6869 return false;
6870}
6871
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006872/// \brief Produces a formatted string that describes the binding of
6873/// template parameters to template arguments.
6874std::string
6875Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6876 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006877 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006878}
6879
6880std::string
6881Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6882 const TemplateArgument *Args,
6883 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006884 llvm::SmallString<128> Str;
6885 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006886
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006887 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006888 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006889
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006890 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006891 if (I >= NumArgs)
6892 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006893
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006894 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006895 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006896 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006897 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006898
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006899 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006900 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006901 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006902 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006903 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006904
Douglas Gregor87dd6972010-12-20 16:52:59 +00006905 Out << " = ";
Douglas Gregor8987b232011-09-27 23:30:47 +00006906 Args[I].print(getPrintingPolicy(), Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006907 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006908
6909 Out << ']';
6910 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006911}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006912
6913void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6914 if (!FD)
6915 return;
6916 FD->setLateTemplateParsed(Flag);
6917}
6918
6919bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6920 DeclContext *DC = CurContext;
6921
6922 while (DC) {
6923 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6924 const FunctionDecl *FD = RD->isLocalClass();
6925 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6926 } else if (DC->isTranslationUnit() || DC->isNamespace())
6927 return false;
6928
6929 DC = DC->getParent();
6930 }
6931 return false;
6932}