blob: eca491f733ed070225735ea03a4ea6e4a855594d [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"
Benjamin Kramer013b3662012-01-30 16:17:39 +000029#include "llvm/ADT/SmallBitVector.h"
Douglas Gregorbf4ea562009-09-15 16:23:51 +000030#include "llvm/ADT/StringExtras.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000031using namespace clang;
John McCall2a7fb272010-08-25 05:32:35 +000032using namespace sema;
Douglas Gregor72c3f312008-12-05 18:15:24 +000033
John McCall78b81052010-11-10 02:40:36 +000034// Exported for use by Parser.
35SourceRange
36clang::getTemplateParamsRange(TemplateParameterList const * const *Ps,
37 unsigned N) {
38 if (!N) return SourceRange();
39 return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc());
40}
41
Douglas Gregor2dd078a2009-09-02 22:59:36 +000042/// \brief Determine whether the declaration found is acceptable as the name
43/// of a template and, if so, return that template declaration. Otherwise,
44/// returns NULL.
John McCallad00b772010-06-16 08:42:20 +000045static NamedDecl *isAcceptableTemplateName(ASTContext &Context,
46 NamedDecl *Orig) {
47 NamedDecl *D = Orig->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000048
Douglas Gregor2dd078a2009-09-02 22:59:36 +000049 if (isa<TemplateDecl>(D))
John McCallad00b772010-06-16 08:42:20 +000050 return Orig;
Mike Stump1eb44332009-09-09 15:08:12 +000051
Douglas Gregor2dd078a2009-09-02 22:59:36 +000052 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
53 // C++ [temp.local]p1:
54 // Like normal (non-template) classes, class templates have an
55 // injected-class-name (Clause 9). The injected-class-name
56 // can be used with or without a template-argument-list. When
57 // it is used without a template-argument-list, it is
58 // equivalent to the injected-class-name followed by the
59 // template-parameters of the class template enclosed in
60 // <>. When it is used with a template-argument-list, it
61 // refers to the specified class template specialization,
62 // which could be the current specialization or another
63 // specialization.
64 if (Record->isInjectedClassName()) {
Douglas Gregor542b5482009-10-14 17:30:58 +000065 Record = cast<CXXRecordDecl>(Record->getDeclContext());
Douglas Gregor2dd078a2009-09-02 22:59:36 +000066 if (Record->getDescribedClassTemplate())
67 return Record->getDescribedClassTemplate();
68
69 if (ClassTemplateSpecializationDecl *Spec
70 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
71 return Spec->getSpecializedTemplate();
72 }
Mike Stump1eb44332009-09-09 15:08:12 +000073
Douglas Gregor2dd078a2009-09-02 22:59:36 +000074 return 0;
75 }
Mike Stump1eb44332009-09-09 15:08:12 +000076
Douglas Gregor2dd078a2009-09-02 22:59:36 +000077 return 0;
78}
79
Douglas Gregor312eadb2011-04-24 05:37:28 +000080void Sema::FilterAcceptableTemplateNames(LookupResult &R) {
Douglas Gregor01e56ae2010-04-12 20:54:26 +000081 // The set of class templates we've already seen.
82 llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates;
John McCallf7a1a742009-11-24 19:00:30 +000083 LookupResult::Filter filter = R.makeFilter();
84 while (filter.hasNext()) {
85 NamedDecl *Orig = filter.next();
Douglas Gregor312eadb2011-04-24 05:37:28 +000086 NamedDecl *Repl = isAcceptableTemplateName(Context, Orig);
John McCallf7a1a742009-11-24 19:00:30 +000087 if (!Repl)
88 filter.erase();
Douglas Gregor01e56ae2010-04-12 20:54:26 +000089 else if (Repl != Orig) {
90
91 // C++ [temp.local]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000092 // A lookup that finds an injected-class-name (10.2) can result in an
Douglas Gregor01e56ae2010-04-12 20:54:26 +000093 // ambiguity in certain cases (for example, if it is found in more than
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000094 // one base class). If all of the injected-class-names that are found
95 // refer to specializations of the same class template, and if the name
Richard Smith3e4c6c42011-05-05 21:57:07 +000096 // is used as a template-name, the reference refers to the class
97 // template itself and not a specialization thereof, and is not
Douglas Gregor01e56ae2010-04-12 20:54:26 +000098 // ambiguous.
Douglas Gregor01e56ae2010-04-12 20:54:26 +000099 if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
100 if (!ClassTemplates.insert(ClassTmpl)) {
101 filter.erase();
102 continue;
103 }
John McCall8ba66912010-08-13 07:02:08 +0000104
105 // FIXME: we promote access to public here as a workaround to
106 // the fact that LookupResult doesn't let us remember that we
107 // found this template through a particular injected class name,
108 // which means we end up doing nasty things to the invariants.
109 // Pretending that access is public is *much* safer.
110 filter.replace(Repl, AS_public);
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000111 }
John McCallf7a1a742009-11-24 19:00:30 +0000112 }
113 filter.done();
114}
115
Douglas Gregor312eadb2011-04-24 05:37:28 +0000116bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R) {
117 for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I)
118 if (isAcceptableTemplateName(Context, *I))
119 return true;
120
Douglas Gregor3b887352011-04-27 04:48:22 +0000121 return false;
Douglas Gregor312eadb2011-04-24 05:37:28 +0000122}
123
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000124TemplateNameKind Sema::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000125 CXXScopeSpec &SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000126 bool hasTemplateKeyword,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000127 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +0000128 ParsedType ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000129 bool EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000130 TemplateTy &TemplateResult,
131 bool &MemberOfUnknownSpecialization) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +0000132 assert(getLangOptions().CPlusPlus && "No template names in C!");
133
Douglas Gregor014e88d2009-11-03 23:16:33 +0000134 DeclarationName TName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000135 MemberOfUnknownSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000136
Douglas Gregor014e88d2009-11-03 23:16:33 +0000137 switch (Name.getKind()) {
138 case UnqualifiedId::IK_Identifier:
139 TName = DeclarationName(Name.Identifier);
140 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000141
Douglas Gregor014e88d2009-11-03 23:16:33 +0000142 case UnqualifiedId::IK_OperatorFunctionId:
143 TName = Context.DeclarationNames.getCXXOperatorName(
144 Name.OperatorFunctionId.Operator);
145 break;
146
Sean Hunte6252d12009-11-28 08:58:14 +0000147 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +0000148 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
149 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000150
Douglas Gregor014e88d2009-11-03 23:16:33 +0000151 default:
152 return TNK_Non_template;
153 }
Mike Stump1eb44332009-09-09 15:08:12 +0000154
John McCallb3d87482010-08-24 05:47:05 +0000155 QualType ObjectType = ObjectTypePtr.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000156
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000157 LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000158 LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000159 LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
160 MemberOfUnknownSpecialization);
John McCall67d22fb2010-08-28 20:17:00 +0000161 if (R.empty()) return TNK_Non_template;
162 if (R.isAmbiguous()) {
163 // Suppress diagnostics; we'll redo this lookup later.
John McCallb8592062010-08-13 02:23:42 +0000164 R.suppressDiagnostics();
John McCall67d22fb2010-08-28 20:17:00 +0000165
166 // FIXME: we might have ambiguous templates, in which case we
167 // should at least parse them properly!
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000168 return TNK_Non_template;
John McCallb8592062010-08-13 02:23:42 +0000169 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000170
John McCall0bd6feb2009-12-02 08:04:21 +0000171 TemplateName Template;
172 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000173
John McCall0bd6feb2009-12-02 08:04:21 +0000174 unsigned ResultCount = R.end() - R.begin();
175 if (ResultCount > 1) {
176 // We assume that we'll preserve the qualifier from a function
177 // template name in other ways.
178 Template = Context.getOverloadedTemplateName(R.begin(), R.end());
179 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000180
181 // We'll do this lookup again later.
182 R.suppressDiagnostics();
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000183 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000184 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
185
186 if (SS.isSet() && !SS.isInvalid()) {
187 NestedNameSpecifier *Qualifier
188 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Abramo Bagnara7c153532010-08-06 12:11:11 +0000189 Template = Context.getQualifiedTemplateName(Qualifier,
190 hasTemplateKeyword, TD);
John McCall0bd6feb2009-12-02 08:04:21 +0000191 } else {
192 Template = TemplateName(TD);
193 }
194
John McCallb8592062010-08-13 02:23:42 +0000195 if (isa<FunctionTemplateDecl>(TD)) {
John McCall0bd6feb2009-12-02 08:04:21 +0000196 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000197
198 // We'll do this lookup again later.
199 R.suppressDiagnostics();
200 } else {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000201 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) ||
202 isa<TypeAliasTemplateDecl>(TD));
John McCall0bd6feb2009-12-02 08:04:21 +0000203 TemplateKind = TNK_Type_template;
204 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000205 }
Mike Stump1eb44332009-09-09 15:08:12 +0000206
John McCall0bd6feb2009-12-02 08:04:21 +0000207 TemplateResult = TemplateTy::make(Template);
208 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000209}
210
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000211bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Douglas Gregor84d0a192010-01-12 21:28:44 +0000212 SourceLocation IILoc,
213 Scope *S,
214 const CXXScopeSpec *SS,
215 TemplateTy &SuggestedTemplate,
216 TemplateNameKind &SuggestedKind) {
217 // We can't recover unless there's a dependent scope specifier preceding the
218 // template name.
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000219 // FIXME: Typo correction?
Douglas Gregor84d0a192010-01-12 21:28:44 +0000220 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
221 computeDeclContext(*SS))
222 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000223
Douglas Gregor84d0a192010-01-12 21:28:44 +0000224 // The code is missing a 'template' keyword prior to the dependent template
225 // name.
226 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
227 Diag(IILoc, diag::err_template_kw_missing)
228 << Qualifier << II.getName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000229 << FixItHint::CreateInsertion(IILoc, "template ");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000230 SuggestedTemplate
Douglas Gregor84d0a192010-01-12 21:28:44 +0000231 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
232 SuggestedKind = TNK_Dependent_template_name;
233 return true;
234}
235
John McCallf7a1a742009-11-24 19:00:30 +0000236void Sema::LookupTemplateName(LookupResult &Found,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000237 Scope *S, CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +0000238 QualType ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000239 bool EnteringContext,
240 bool &MemberOfUnknownSpecialization) {
John McCallf7a1a742009-11-24 19:00:30 +0000241 // Determine where to perform name lookup
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000242 MemberOfUnknownSpecialization = false;
John McCallf7a1a742009-11-24 19:00:30 +0000243 DeclContext *LookupCtx = 0;
244 bool isDependent = false;
245 if (!ObjectType.isNull()) {
246 // This nested-name-specifier occurs in a member access expression, e.g.,
247 // x->B::f, and we are looking into the type of the object.
248 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
249 LookupCtx = computeDeclContext(ObjectType);
250 isDependent = ObjectType->isDependentType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000251 assert((isDependent || !ObjectType->isIncompleteType()) &&
John McCallf7a1a742009-11-24 19:00:30 +0000252 "Caller should have completed object type");
Douglas Gregor1d7049a2012-01-12 16:11:24 +0000253
254 // Template names cannot appear inside an Objective-C class or object type.
255 if (ObjectType->isObjCObjectOrInterfaceType()) {
256 Found.clear();
257 return;
258 }
John McCallf7a1a742009-11-24 19:00:30 +0000259 } else if (SS.isSet()) {
260 // This nested-name-specifier occurs after another nested-name-specifier,
261 // so long into the context associated with the prior nested-name-specifier.
262 LookupCtx = computeDeclContext(SS, EnteringContext);
263 isDependent = isDependentScopeSpecifier(SS);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000264
John McCallf7a1a742009-11-24 19:00:30 +0000265 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000266 if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
John McCallf7a1a742009-11-24 19:00:30 +0000267 return;
268 }
269
270 bool ObjectTypeSearchedInScope = false;
271 if (LookupCtx) {
272 // Perform "qualified" name lookup into the declaration context we
273 // computed, which is either the type of the base of a member access
274 // expression or the declaration context associated with a prior
275 // nested-name-specifier.
276 LookupQualifiedName(Found, LookupCtx);
277
278 if (!ObjectType.isNull() && Found.empty()) {
279 // C++ [basic.lookup.classref]p1:
280 // In a class member access expression (5.2.5), if the . or -> token is
281 // immediately followed by an identifier followed by a <, the
282 // identifier must be looked up to determine whether the < is the
283 // beginning of a template argument list (14.2) or a less-than operator.
284 // The identifier is first looked up in the class of the object
285 // expression. If the identifier is not found, it is then looked up in
286 // the context of the entire postfix-expression and shall name a class
287 // or function template.
John McCallf7a1a742009-11-24 19:00:30 +0000288 if (S) LookupName(Found, S);
289 ObjectTypeSearchedInScope = true;
290 }
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000291 } else if (isDependent && (!S || ObjectType.isNull())) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000292 // We cannot look into a dependent object type or nested nme
293 // specifier.
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000294 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000295 return;
296 } else {
297 // Perform unqualified name lookup in the current scope.
298 LookupName(Found, S);
299 }
300
Douglas Gregor2e933882010-01-12 17:06:20 +0000301 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000302 // If we did not find any names, attempt to correct any typos.
303 DeclarationName Name = Found.getLookupName();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000304 Found.clear();
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000305 // Simple filter callback that, for keywords, only accepts the C++ *_cast
306 CorrectionCandidateCallback FilterCCC;
307 FilterCCC.WantTypeSpecifiers = false;
308 FilterCCC.WantExpressionKeywords = false;
309 FilterCCC.WantRemainingKeywords = false;
310 FilterCCC.WantCXXNamedCasts = true;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000311 if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(),
312 Found.getLookupKind(), S, &SS,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000313 FilterCCC, LookupCtx)) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000314 Found.setLookupName(Corrected.getCorrection());
315 if (Corrected.getCorrectionDecl())
316 Found.addDecl(Corrected.getCorrectionDecl());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000317 FilterAcceptableTemplateNames(Found);
John McCallad00b772010-06-16 08:42:20 +0000318 if (!Found.empty()) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000319 std::string CorrectedStr(Corrected.getAsString(getLangOptions()));
320 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions()));
Douglas Gregorbfea2392009-12-31 08:11:17 +0000321 if (LookupCtx)
322 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000323 << Name << LookupCtx << CorrectedQuotedStr << SS.getRange()
324 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000325 else
326 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000327 << Name << CorrectedQuotedStr
328 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000329 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
330 Diag(Template->getLocation(), diag::note_previous_decl)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000331 << CorrectedQuotedStr;
John McCallad00b772010-06-16 08:42:20 +0000332 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000333 } else {
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000334 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000335 }
336 }
337
Douglas Gregor312eadb2011-04-24 05:37:28 +0000338 FilterAcceptableTemplateNames(Found);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000339 if (Found.empty()) {
340 if (isDependent)
341 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000342 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000343 }
John McCallf7a1a742009-11-24 19:00:30 +0000344
345 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
346 // C++ [basic.lookup.classref]p1:
347 // [...] If the lookup in the class of the object expression finds a
348 // template, the name is also looked up in the context of the entire
349 // postfix-expression and [...]
350 //
351 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
352 LookupOrdinaryName);
353 LookupName(FoundOuter, S);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000354 FilterAcceptableTemplateNames(FoundOuter);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000355
John McCallf7a1a742009-11-24 19:00:30 +0000356 if (FoundOuter.empty()) {
357 // - if the name is not found, the name found in the class of the
358 // object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000359 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() ||
360 FoundOuter.isAmbiguous()) {
John McCallf7a1a742009-11-24 19:00:30 +0000361 // - if the name is found in the context of the entire
362 // postfix-expression and does not name a class template, the name
363 // found in the class of the object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000364 FoundOuter.clear();
John McCallad00b772010-06-16 08:42:20 +0000365 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000366 // - if the name found is a class template, it must refer to the same
367 // entity as the one found in the class of the object expression,
368 // otherwise the program is ill-formed.
369 if (!Found.isSingleResult() ||
370 Found.getFoundDecl()->getCanonicalDecl()
371 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000372 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000373 diag::ext_nested_name_member_ref_lookup_ambiguous)
374 << Found.getLookupName()
375 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000376 Diag(Found.getRepresentativeDecl()->getLocation(),
377 diag::note_ambig_member_ref_object_type)
378 << ObjectType;
379 Diag(FoundOuter.getFoundDecl()->getLocation(),
380 diag::note_ambig_member_ref_scope);
381
382 // Recover by taking the template that we found in the object
383 // expression's type.
384 }
385 }
386 }
387}
388
John McCall2f841ba2009-12-02 03:53:29 +0000389/// ActOnDependentIdExpression - Handle a dependent id-expression that
390/// was just parsed. This is only possible with an explicit scope
391/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000392ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000393Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000394 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000395 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000396 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000397 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000398 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000399
John McCall2f841ba2009-12-02 03:53:29 +0000400 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000401 isa<CXXMethodDecl>(DC) &&
402 cast<CXXMethodDecl>(DC)->isInstance()) {
403 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000404
John McCallf7a1a742009-11-24 19:00:30 +0000405 // Since the 'this' expression is synthesized, we don't need to
406 // perform the double-lookup check.
407 NamedDecl *FirstQualifierInScope = 0;
408
John McCallaa81e162009-12-01 22:10:20 +0000409 return Owned(CXXDependentScopeMemberExpr::Create(Context,
410 /*This*/ 0, ThisType,
411 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000412 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000413 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000414 TemplateKWLoc,
John McCallf7a1a742009-11-24 19:00:30 +0000415 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000416 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000417 TemplateArgs));
418 }
419
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000420 return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000421}
422
John McCall60d7b3a2010-08-24 06:29:42 +0000423ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000424Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000425 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000426 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000427 const TemplateArgumentListInfo *TemplateArgs) {
428 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000429 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000430 TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000431 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000432 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000433}
434
Douglas Gregor72c3f312008-12-05 18:15:24 +0000435/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
436/// that the template parameter 'PrevDecl' is being shadowed by a new
437/// declaration at location Loc. Returns true to indicate that this is
438/// an error, and false otherwise.
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000439void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000440 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000441
442 // Microsoft Visual C++ permits template parameters to be shadowed.
Francois Pichet62ec1f22011-09-17 17:15:52 +0000443 if (getLangOptions().MicrosoftExt)
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000444 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000445
446 // C++ [temp.local]p4:
447 // A template-parameter shall not be redeclared within its
448 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000449 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000450 << cast<NamedDecl>(PrevDecl)->getDeclName();
451 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000452 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000453}
454
Douglas Gregor2943aed2009-03-03 04:44:36 +0000455/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000456/// the parameter D to reference the templated declaration and return a pointer
457/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000458TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
459 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
460 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000461 return Temp;
462 }
463 return 0;
464}
465
Douglas Gregorba68eca2011-01-05 17:40:24 +0000466ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
467 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000468 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000469 "Only template template arguments can be pack expansions here");
470 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
471 "Template template argument pack expansion without packs");
472 ParsedTemplateArgument Result(*this);
473 Result.EllipsisLoc = EllipsisLoc;
474 return Result;
475}
476
Douglas Gregor788cd062009-11-11 01:00:40 +0000477static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
478 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000479
Douglas Gregor788cd062009-11-11 01:00:40 +0000480 switch (Arg.getKind()) {
481 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000482 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000483 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000484 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000485 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000486 return TemplateArgumentLoc(TemplateArgument(T), DI);
487 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000488
Douglas Gregor788cd062009-11-11 01:00:40 +0000489 case ParsedTemplateArgument::NonType: {
490 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
491 return TemplateArgumentLoc(TemplateArgument(E), E);
492 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000493
Douglas Gregor788cd062009-11-11 01:00:40 +0000494 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000495 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000496 TemplateArgument TArg;
497 if (Arg.getEllipsisLoc().isValid())
498 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
499 else
500 TArg = Template;
501 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000502 Arg.getScopeSpec().getWithLocInContext(
503 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000504 Arg.getLocation(),
505 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000506 }
507 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000508
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000509 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000510}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000511
Douglas Gregor788cd062009-11-11 01:00:40 +0000512/// \brief Translates template arguments as provided by the parser
513/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000514void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
515 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000516 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000517 TemplateArgs.addArgument(translateTemplateArgument(*this,
518 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000519}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000520
Douglas Gregor72c3f312008-12-05 18:15:24 +0000521/// ActOnTypeParameter - Called when a C++ template type parameter
522/// (e.g., "typename T") has been parsed. Typename specifies whether
523/// the keyword "typename" was used to declare the type parameter
524/// (otherwise, "class" was used), and KeyLoc is the location of the
525/// "class" or "typename" keyword. ParamName is the name of the
526/// parameter (NULL indicates an unnamed template parameter) and
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000527/// ParamNameLoc is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000528/// If the type parameter has a default argument, it will be added
529/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000530Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
531 SourceLocation EllipsisLoc,
532 SourceLocation KeyLoc,
533 IdentifierInfo *ParamName,
534 SourceLocation ParamNameLoc,
535 unsigned Depth, unsigned Position,
536 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000537 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000538 assert(S->isTemplateParamScope() &&
539 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000540 bool Invalid = false;
541
542 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000543 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000544 LookupOrdinaryName,
545 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000546 if (PrevDecl && PrevDecl->isTemplateParameter()) {
547 DiagnoseTemplateParameterShadow(ParamNameLoc, PrevDecl);
548 PrevDecl = 0;
549 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000550 }
551
Douglas Gregorddc29e12009-02-06 22:42:48 +0000552 SourceLocation Loc = ParamNameLoc;
553 if (!ParamName)
554 Loc = KeyLoc;
555
Douglas Gregor72c3f312008-12-05 18:15:24 +0000556 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000557 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000558 KeyLoc, Loc, Depth, Position, ParamName,
559 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000560 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000561 if (Invalid)
562 Param->setInvalidDecl();
563
564 if (ParamName) {
565 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000566 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000567 IdResolver.AddDecl(Param);
568 }
569
Douglas Gregor61c4d282011-01-05 15:48:55 +0000570 // C++0x [temp.param]p9:
571 // A default template-argument may be specified for any kind of
572 // template-parameter that is not a template parameter pack.
573 if (DefaultArg && Ellipsis) {
574 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
575 DefaultArg = ParsedType();
576 }
577
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000578 // Handle the default argument, if provided.
579 if (DefaultArg) {
580 TypeSourceInfo *DefaultTInfo;
581 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000582
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000583 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000584
Douglas Gregor6f526752010-12-16 08:48:57 +0000585 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000586 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000587 UPPC_DefaultArgument))
588 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000589
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000590 // Check the template argument itself.
591 if (CheckTemplateArgument(Param, DefaultTInfo)) {
592 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000593 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000594 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000595
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000596 Param->setDefaultArgument(DefaultTInfo, false);
597 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000598
John McCalld226f652010-08-21 09:40:31 +0000599 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000600}
601
Douglas Gregor2943aed2009-03-03 04:44:36 +0000602/// \brief Check that the type of a non-type template parameter is
603/// well-formed.
604///
605/// \returns the (possibly-promoted) parameter type if valid;
606/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000607QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000608Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000609 // We don't allow variably-modified types as the type of non-type template
610 // parameters.
611 if (T->isVariablyModifiedType()) {
612 Diag(Loc, diag::err_variably_modified_nontype_template_param)
613 << T;
614 return QualType();
615 }
616
Douglas Gregor2943aed2009-03-03 04:44:36 +0000617 // C++ [temp.param]p4:
618 //
619 // A non-type template-parameter shall have one of the following
620 // (optionally cv-qualified) types:
621 //
622 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000623 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000624 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000625 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000626 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000627 T->isReferenceType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000628 // -- pointer to member,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000629 T->isMemberPointerType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000630 // -- std::nullptr_t.
631 T->isNullPtrType() ||
Douglas Gregor2943aed2009-03-03 04:44:36 +0000632 // If T is a dependent type, we can't do the check now, so we
633 // assume that it is well-formed.
634 T->isDependentType())
635 return T;
636 // C++ [temp.param]p8:
637 //
638 // A non-type template-parameter of type "array of T" or
639 // "function returning T" is adjusted to be of type "pointer to
640 // T" or "pointer to function returning T", respectively.
641 else if (T->isArrayType())
642 // FIXME: Keep the type prior to promotion?
643 return Context.getArrayDecayedType(T);
644 else if (T->isFunctionType())
645 // FIXME: Keep the type prior to promotion?
646 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000647
Douglas Gregor2943aed2009-03-03 04:44:36 +0000648 Diag(Loc, diag::err_template_nontype_parm_bad_type)
649 << T;
650
651 return QualType();
652}
653
John McCalld226f652010-08-21 09:40:31 +0000654Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
655 unsigned Depth,
656 unsigned Position,
657 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000658 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000659 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
660 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000661
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000662 assert(S->isTemplateParamScope() &&
663 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000664 bool Invalid = false;
665
666 IdentifierInfo *ParamName = D.getIdentifier();
667 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000668 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000669 LookupOrdinaryName,
670 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000671 if (PrevDecl && PrevDecl->isTemplateParameter()) {
672 DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl);
673 PrevDecl = 0;
674 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000675 }
676
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000677 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
678 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000679 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000680 Invalid = true;
681 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000682
Douglas Gregor10738d32010-12-23 23:51:58 +0000683 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000684 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000685 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000686 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000687 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000688 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000689 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000690 Param->setAccess(AS_public);
691
Douglas Gregor72c3f312008-12-05 18:15:24 +0000692 if (Invalid)
693 Param->setInvalidDecl();
694
695 if (D.getIdentifier()) {
696 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000697 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000698 IdResolver.AddDecl(Param);
699 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000700
Douglas Gregor61c4d282011-01-05 15:48:55 +0000701 // C++0x [temp.param]p9:
702 // A default template-argument may be specified for any kind of
703 // template-parameter that is not a template parameter pack.
704 if (Default && IsParameterPack) {
705 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
706 Default = 0;
707 }
708
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000709 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000710 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000711 // Check for unexpanded parameter packs.
712 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
713 return Param;
714
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000715 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000716 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
717 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000718 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000719 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000720 }
John Wiegley429bb272011-04-08 18:41:53 +0000721 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000722
John McCall9ae2f072010-08-23 23:25:46 +0000723 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000724 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000725
John McCalld226f652010-08-21 09:40:31 +0000726 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000727}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000728
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000729/// ActOnTemplateTemplateParameter - Called when a C++ template template
730/// parameter (e.g. T in template <template <typename> class T> class array)
731/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000732Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
733 SourceLocation TmpLoc,
Richard Trieu90ab75b2011-09-09 03:18:59 +0000734 TemplateParameterList *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000735 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000736 IdentifierInfo *Name,
737 SourceLocation NameLoc,
738 unsigned Depth,
739 unsigned Position,
740 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000741 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000742 assert(S->isTemplateParamScope() &&
743 "Template template parameter not in template parameter scope!");
744
745 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000746 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000747 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000748 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000749 NameLoc.isInvalid()? TmpLoc : NameLoc,
750 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000751 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000752 Param->setAccess(AS_public);
753
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000754 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000755 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000756 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000757 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000758 IdResolver.AddDecl(Param);
759 }
760
Douglas Gregor6f526752010-12-16 08:48:57 +0000761 if (Params->size() == 0) {
762 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
763 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
764 Param->setInvalidDecl();
765 }
766
Douglas Gregor61c4d282011-01-05 15:48:55 +0000767 // C++0x [temp.param]p9:
768 // A default template-argument may be specified for any kind of
769 // template-parameter that is not a template parameter pack.
770 if (IsParameterPack && !Default.isInvalid()) {
771 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
772 Default = ParsedTemplateArgument();
773 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000774
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000775 if (!Default.isInvalid()) {
776 // Check only that we have a template template argument. We don't want to
777 // try to check well-formedness now, because our template template parameter
778 // might have dependent types in its template parameters, which we wouldn't
779 // be able to match now.
780 //
781 // If none of the template template parameter's template arguments mention
782 // other template parameters, we could actually perform more checking here.
783 // However, it isn't worth doing.
784 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
785 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
786 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
787 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000788 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000789 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000790
Douglas Gregor6f526752010-12-16 08:48:57 +0000791 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000792 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000793 DefaultArg.getArgument().getAsTemplate(),
794 UPPC_DefaultArgument))
795 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000796
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000797 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000798 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000799
John McCalld226f652010-08-21 09:40:31 +0000800 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000801}
802
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000803/// ActOnTemplateParameterList - Builds a TemplateParameterList that
804/// contains the template parameters in Params/NumParams.
Richard Trieu90ab75b2011-09-09 03:18:59 +0000805TemplateParameterList *
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000806Sema::ActOnTemplateParameterList(unsigned Depth,
807 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000808 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000809 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000810 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000811 SourceLocation RAngleLoc) {
812 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000813 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000814
Douglas Gregorddc29e12009-02-06 22:42:48 +0000815 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000816 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000817 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000818}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000819
John McCallb6217662010-03-15 10:12:16 +0000820static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
821 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000822 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000823}
824
John McCallf312b1e2010-08-26 23:41:50 +0000825DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000826Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000827 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000828 IdentifierInfo *Name, SourceLocation NameLoc,
829 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000830 TemplateParameterList *TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +0000831 AccessSpecifier AS, SourceLocation ModulePrivateLoc,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000832 unsigned NumOuterTemplateParamLists,
833 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000834 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000835 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000836 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000837 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000838
839 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000840 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000841 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000842
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000843 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
844 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000845
846 // There is no such thing as an unnamed class template.
847 if (!Name) {
848 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000849 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000850 }
851
852 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000853 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000854 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000855 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000856 if (SS.isNotEmpty() && !SS.isInvalid()) {
857 SemanticContext = computeDeclContext(SS, true);
858 if (!SemanticContext) {
859 // FIXME: Produce a reasonable diagnostic here
860 return true;
861 }
Mike Stump1eb44332009-09-09 15:08:12 +0000862
John McCall77bb1aa2010-05-01 00:40:08 +0000863 if (RequireCompleteDeclContext(SS, SemanticContext))
864 return true;
865
Douglas Gregor20606502011-10-14 15:31:12 +0000866 // If we're adding a template to a dependent context, we may need to
867 // rebuilding some of the types used within the template parameter list,
868 // now that we know what the current instantiation is.
869 if (SemanticContext->isDependentContext()) {
870 ContextRAII SavedContext(*this, SemanticContext);
871 if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams))
872 Invalid = true;
873 }
874
John McCalla24dc2e2009-11-17 02:14:36 +0000875 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000876 } else {
877 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000878 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000879 }
Mike Stump1eb44332009-09-09 15:08:12 +0000880
Douglas Gregor57265e32010-04-12 16:00:01 +0000881 if (Previous.isAmbiguous())
882 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000883
Douglas Gregorddc29e12009-02-06 22:42:48 +0000884 NamedDecl *PrevDecl = 0;
885 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000886 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000887
Douglas Gregorddc29e12009-02-06 22:42:48 +0000888 // If there is a previous declaration with the same name, check
889 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000890 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000891 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000892
893 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000894 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000895 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000896 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000897 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
898 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000899 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000900 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
901 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
902 PrevClassTemplate
903 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
904 ->getSpecializedTemplate();
905 }
906 }
907
John McCall65c49462009-12-18 11:25:59 +0000908 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000909 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000910 // [...] When looking for a prior declaration of a class or a function
911 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000912 // function is neither a qualified name nor a template-id, scopes outside
913 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000914 if (!SS.isSet()) {
915 DeclContext *OutermostContext = CurContext;
916 while (!OutermostContext->isFileContext())
917 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000918
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000919 if (PrevDecl &&
920 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
921 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
922 SemanticContext = PrevDecl->getDeclContext();
923 } else {
924 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000925 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000926 // declaration.
927 PrevDecl = PrevClassTemplate = 0;
928 SemanticContext = OutermostContext;
929 }
John McCalle129d442009-12-17 23:21:11 +0000930 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000931
John McCalle129d442009-12-17 23:21:11 +0000932 if (CurContext->isDependentContext()) {
933 // If this is a dependent context, we don't want to link the friend
934 // class template to the template in scope, because that would perform
935 // checking of the template parameter lists that can't be performed
936 // until the outer context is instantiated.
937 PrevDecl = PrevClassTemplate = 0;
938 }
939 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
940 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000941
Douglas Gregorddc29e12009-02-06 22:42:48 +0000942 if (PrevClassTemplate) {
943 // Ensure that the template parameter lists are compatible.
944 if (!TemplateParameterListsAreEqual(TemplateParams,
945 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000946 /*Complain=*/true,
947 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000948 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000949
950 // C++ [temp.class]p4:
951 // In a redeclaration, partial specialization, explicit
952 // specialization or explicit instantiation of a class template,
953 // the class-key shall agree in kind with the original class
954 // template declaration (7.1.5.3).
955 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Richard Trieubbf34c02011-06-10 03:11:26 +0000956 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind,
957 TUK == TUK_Definition, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000958 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000959 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000960 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000961 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000962 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000963 }
964
Douglas Gregorddc29e12009-02-06 22:42:48 +0000965 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000966 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000967 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000968 Diag(NameLoc, diag::err_redefinition) << Name;
969 Diag(Def->getLocation(), diag::note_previous_definition);
970 // FIXME: Would it make sense to try to "forget" the previous
971 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000972 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000973 }
Douglas Gregor6311d2b2011-09-09 18:32:39 +0000974 }
Douglas Gregorddc29e12009-02-06 22:42:48 +0000975 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
976 // Maybe we will complain about the shadowed template parameter.
977 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
978 // Just pretend that we didn't see the previous declaration.
979 PrevDecl = 0;
980 } else if (PrevDecl) {
981 // C++ [temp]p5:
982 // A class template shall not have the same name as any other
983 // template, class, function, object, enumeration, enumerator,
984 // namespace, or type in the same scope (3.3), except as specified
985 // in (14.5.4).
986 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
987 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000988 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000989 }
990
Douglas Gregord684b002009-02-10 19:49:53 +0000991 // Check the template parameter list of this declaration, possibly
992 // merging in the template parameter list from the previous class
993 // template declaration.
994 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000995 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000996 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000997 SemanticContext->isRecord() &&
998 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +0000999 ? TPC_ClassTemplateMember
1000 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +00001001 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Douglas Gregor57265e32010-04-12 16:00:01 +00001003 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001004 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +00001005 // template out-of-line.
1006 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
Douglas Gregorea9f54a2011-11-01 21:35:16 +00001007 !(TUK == TUK_Friend && CurContext->isDependentContext())) {
Douglas Gregor57265e32010-04-12 16:00:01 +00001008 Diag(NameLoc, diag::err_member_def_does_not_match)
1009 << Name << SemanticContext << SS.getRange();
Douglas Gregorea9f54a2011-11-01 21:35:16 +00001010 Invalid = true;
1011 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001012 }
1013
Mike Stump1eb44332009-09-09 15:08:12 +00001014 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001015 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +00001016 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001017 PrevClassTemplate->getTemplatedDecl() : 0,
1018 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +00001019 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00001020 if (NumOuterTemplateParamLists > 0)
1021 NewClass->setTemplateParameterListsInfo(Context,
1022 NumOuterTemplateParamLists,
1023 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001024
1025 ClassTemplateDecl *NewTemplate
1026 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
1027 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001028 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +00001029 NewClass->setDescribedClassTemplate(NewTemplate);
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001030
Douglas Gregor2ccd89c2011-12-20 18:11:52 +00001031 if (ModulePrivateLoc.isValid())
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001032 NewTemplate->setModulePrivate();
Douglas Gregor8d267c52011-09-09 02:06:17 +00001033
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001034 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +00001035 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +00001036 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001037 assert(T->isDependentType() && "Class template type is not dependent?");
1038 (void)T;
1039
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001040 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001041 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001042 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001043 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1044 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001045
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001046 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001047 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001048 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Douglas Gregorddc29e12009-02-06 22:42:48 +00001050 // Set the lexical context of these templates
1051 NewClass->setLexicalDeclContext(CurContext);
1052 NewTemplate->setLexicalDeclContext(CurContext);
1053
John McCall0f434ec2009-07-31 02:45:11 +00001054 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001055 NewClass->startDefinition();
1056
1057 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001058 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001059
John McCall05b23ea2009-09-14 21:59:20 +00001060 if (TUK != TUK_Friend)
1061 PushOnScopeChains(NewTemplate, S);
1062 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001063 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001064 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001065 NewClass->setAccess(PrevClassTemplate->getAccess());
1066 }
John McCall05b23ea2009-09-14 21:59:20 +00001067
Douglas Gregord85bea22009-09-26 06:47:28 +00001068 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1069 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001070
John McCall05b23ea2009-09-14 21:59:20 +00001071 // Friend templates are visible in fairly strange ways.
1072 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001073 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001074 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1075 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1076 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001077 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001078 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001079
Douglas Gregord85bea22009-09-26 06:47:28 +00001080 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1081 NewClass->getLocation(),
1082 NewTemplate,
1083 /*FIXME:*/NewClass->getLocation());
1084 Friend->setAccess(AS_public);
1085 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001086 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001087
Douglas Gregord684b002009-02-10 19:49:53 +00001088 if (Invalid) {
1089 NewTemplate->setInvalidDecl();
1090 NewClass->setInvalidDecl();
1091 }
John McCalld226f652010-08-21 09:40:31 +00001092 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001093}
1094
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001095/// \brief Diagnose the presence of a default template argument on a
1096/// template parameter, which is ill-formed in certain contexts.
1097///
1098/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001099static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001100 Sema::TemplateParamListContext TPC,
1101 SourceLocation ParamLoc,
1102 SourceRange DefArgRange) {
1103 switch (TPC) {
1104 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001105 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001106 return false;
1107
1108 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001109 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001110 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001111 // A default template-argument shall not be specified in a
1112 // function template declaration or a function template
1113 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001114 // If a friend function template declaration specifies a default
1115 // template-argument, that declaration shall be a definition and shall be
1116 // the only declaration of the function template in the translation unit.
1117 // (C++98/03 doesn't have this wording; see DR226).
Richard Smithebaf0e62011-10-18 20:49:44 +00001118 S.Diag(ParamLoc, S.getLangOptions().CPlusPlus0x ?
1119 diag::warn_cxx98_compat_template_parameter_default_in_function_template
1120 : diag::ext_template_parameter_default_in_function_template)
1121 << DefArgRange;
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001122 return false;
1123
1124 case Sema::TPC_ClassTemplateMember:
1125 // C++0x [temp.param]p9:
1126 // A default template-argument shall not be specified in the
1127 // template-parameter-lists of the definition of a member of a
1128 // class template that appears outside of the member's class.
1129 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1130 << DefArgRange;
1131 return true;
1132
1133 case Sema::TPC_FriendFunctionTemplate:
1134 // C++ [temp.param]p9:
1135 // A default template-argument shall not be specified in a
1136 // friend template declaration.
1137 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1138 << DefArgRange;
1139 return true;
1140
1141 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1142 // for friend function templates if there is only a single
1143 // declaration (and it is a definition). Strange!
1144 }
1145
David Blaikie7530c032012-01-17 06:56:22 +00001146 llvm_unreachable("Invalid TemplateParamListContext!");
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001147}
1148
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001149/// \brief Check for unexpanded parameter packs within the template parameters
1150/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001151static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1152 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001153 TemplateParameterList *Params = TTP->getTemplateParameters();
1154 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1155 NamedDecl *P = Params->getParam(I);
1156 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001157 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001158 NTTP->getTypeSourceInfo(),
1159 Sema::UPPC_NonTypeTemplateParameterType))
1160 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001161
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001162 continue;
1163 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001164
1165 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001166 = dyn_cast<TemplateTemplateParmDecl>(P))
1167 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1168 return true;
1169 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001170
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001171 return false;
1172}
1173
Douglas Gregord684b002009-02-10 19:49:53 +00001174/// \brief Checks the validity of a template parameter list, possibly
1175/// considering the template parameter list from a previous
1176/// declaration.
1177///
1178/// If an "old" template parameter list is provided, it must be
1179/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1180/// template parameter list.
1181///
1182/// \param NewParams Template parameter list for a new template
1183/// declaration. This template parameter list will be updated with any
1184/// default arguments that are carried through from the previous
1185/// template parameter list.
1186///
1187/// \param OldParams If provided, template parameter list from a
1188/// previous declaration of the same template. Default template
1189/// arguments will be merged from the old template parameter list to
1190/// the new template parameter list.
1191///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001192/// \param TPC Describes the context in which we are checking the given
1193/// template parameter list.
1194///
Douglas Gregord684b002009-02-10 19:49:53 +00001195/// \returns true if an error occurred, false otherwise.
1196bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001197 TemplateParameterList *OldParams,
1198 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001199 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Douglas Gregord684b002009-02-10 19:49:53 +00001201 // C++ [temp.param]p10:
1202 // The set of default template-arguments available for use with a
1203 // template declaration or definition is obtained by merging the
1204 // default arguments from the definition (if in scope) and all
1205 // declarations in scope in the same way default function
1206 // arguments are (8.3.6).
1207 bool SawDefaultArgument = false;
1208 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001209
Mike Stump1a35fde2009-02-11 23:03:27 +00001210 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001211 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001212 if (OldParams)
1213 OldParam = OldParams->begin();
1214
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001215 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001216 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1217 NewParamEnd = NewParams->end();
1218 NewParam != NewParamEnd; ++NewParam) {
1219 // Variables used to diagnose redundant default arguments
1220 bool RedundantDefaultArg = false;
1221 SourceLocation OldDefaultLoc;
1222 SourceLocation NewDefaultLoc;
1223
David Blaikie1368e582011-10-19 05:19:50 +00001224 // Variable used to diagnose missing default arguments
Douglas Gregord684b002009-02-10 19:49:53 +00001225 bool MissingDefaultArg = false;
1226
David Blaikie1368e582011-10-19 05:19:50 +00001227 // Variable used to diagnose non-final parameter packs
1228 bool SawParameterPack = false;
Anders Carlsson49d25572009-06-12 23:20:15 +00001229
Douglas Gregord684b002009-02-10 19:49:53 +00001230 if (TemplateTypeParmDecl *NewTypeParm
1231 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001232 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001233 if (NewTypeParm->hasDefaultArgument() &&
1234 DiagnoseDefaultTemplateArgument(*this, TPC,
1235 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001236 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001237 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001238 NewTypeParm->removeDefaultArgument();
1239
1240 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001241 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001242 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Anders Carlsson49d25572009-06-12 23:20:15 +00001244 if (NewTypeParm->isParameterPack()) {
1245 assert(!NewTypeParm->hasDefaultArgument() &&
1246 "Parameter packs can't have a default argument!");
1247 SawParameterPack = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001248 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001249 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001250 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1251 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1252 SawDefaultArgument = true;
1253 RedundantDefaultArg = true;
1254 PreviousDefaultArgLoc = NewDefaultLoc;
1255 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1256 // Merge the default argument from the old declaration to the
1257 // new declaration.
1258 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001259 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001260 true);
1261 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1262 } else if (NewTypeParm->hasDefaultArgument()) {
1263 SawDefaultArgument = true;
1264 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1265 } else if (SawDefaultArgument)
1266 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001267 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001268 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001269 // Check for unexpanded parameter packs.
1270 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001271 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001272 UPPC_NonTypeTemplateParameterType)) {
1273 Invalid = true;
1274 continue;
1275 }
1276
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001277 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001278 if (NewNonTypeParm->hasDefaultArgument() &&
1279 DiagnoseDefaultTemplateArgument(*this, TPC,
1280 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001281 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001282 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001283 }
1284
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001285 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001286 NonTypeTemplateParmDecl *OldNonTypeParm
1287 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001288 if (NewNonTypeParm->isParameterPack()) {
1289 assert(!NewNonTypeParm->hasDefaultArgument() &&
1290 "Parameter packs can't have a default argument!");
1291 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001292 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001293 NewNonTypeParm->hasDefaultArgument()) {
1294 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1295 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1296 SawDefaultArgument = true;
1297 RedundantDefaultArg = true;
1298 PreviousDefaultArgLoc = NewDefaultLoc;
1299 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1300 // Merge the default argument from the old declaration to the
1301 // new declaration.
1302 SawDefaultArgument = true;
1303 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001304 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001305 // parameter.
1306 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001307 OldNonTypeParm->getDefaultArgument(),
1308 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001309 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1310 } else if (NewNonTypeParm->hasDefaultArgument()) {
1311 SawDefaultArgument = true;
1312 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1313 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001314 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001315 } else {
Douglas Gregord684b002009-02-10 19:49:53 +00001316 TemplateTemplateParmDecl *NewTemplateParm
1317 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001318
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001319 // Check for unexpanded parameter packs, recursively.
Douglas Gregor65019ac2011-10-25 03:44:56 +00001320 if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001321 Invalid = true;
1322 continue;
1323 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001324
David Blaikie1368e582011-10-19 05:19:50 +00001325 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001326 if (NewTemplateParm->hasDefaultArgument() &&
1327 DiagnoseDefaultTemplateArgument(*this, TPC,
1328 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001329 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001330 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001331
1332 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001333 TemplateTemplateParmDecl *OldTemplateParm
1334 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001335 if (NewTemplateParm->isParameterPack()) {
1336 assert(!NewTemplateParm->hasDefaultArgument() &&
1337 "Parameter packs can't have a default argument!");
1338 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001339 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001340 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001341 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1342 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001343 SawDefaultArgument = true;
1344 RedundantDefaultArg = true;
1345 PreviousDefaultArgLoc = NewDefaultLoc;
1346 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1347 // Merge the default argument from the old declaration to the
1348 // new declaration.
1349 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001350 // FIXME: We need to create a new kind of "default argument" expression
1351 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001352 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001353 OldTemplateParm->getDefaultArgument(),
1354 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001355 PreviousDefaultArgLoc
1356 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001357 } else if (NewTemplateParm->hasDefaultArgument()) {
1358 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001359 PreviousDefaultArgLoc
1360 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001361 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001362 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001363 }
1364
David Blaikie1368e582011-10-19 05:19:50 +00001365 // C++0x [temp.param]p11:
1366 // If a template parameter of a primary class template or alias template
1367 // is a template parameter pack, it shall be the last template parameter.
1368 if (SawParameterPack && (NewParam + 1) != NewParamEnd &&
1369 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
1370 Diag((*NewParam)->getLocation(),
1371 diag::err_template_param_pack_must_be_last_template_parameter);
1372 Invalid = true;
1373 }
1374
Douglas Gregord684b002009-02-10 19:49:53 +00001375 if (RedundantDefaultArg) {
1376 // C++ [temp.param]p12:
1377 // A template-parameter shall not be given default arguments
1378 // by two different declarations in the same scope.
1379 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1380 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1381 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001382 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001383 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001384 // If a template-parameter of a class template has a default
1385 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001386 // have a default template-argument supplied or be a template parameter
1387 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001388 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001389 diag::err_template_param_default_arg_missing);
1390 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1391 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001392 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001393 }
1394
1395 // If we have an old template parameter list that we're merging
1396 // in, move on to the next parameter.
1397 if (OldParams)
1398 ++OldParam;
1399 }
1400
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001401 // We were missing some default arguments at the end of the list, so remove
1402 // all of the default arguments.
1403 if (RemoveDefaultArguments) {
1404 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1405 NewParamEnd = NewParams->end();
1406 NewParam != NewParamEnd; ++NewParam) {
1407 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1408 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001409 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001410 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1411 NTTP->removeDefaultArgument();
1412 else
1413 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1414 }
1415 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001416
Douglas Gregord684b002009-02-10 19:49:53 +00001417 return Invalid;
1418}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001419
John McCall4e2cbb22010-10-20 05:44:58 +00001420namespace {
1421
1422/// A class which looks for a use of a certain level of template
1423/// parameter.
1424struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1425 typedef RecursiveASTVisitor<DependencyChecker> super;
1426
1427 unsigned Depth;
1428 bool Match;
1429
1430 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1431 NamedDecl *ND = Params->getParam(0);
1432 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1433 Depth = PD->getDepth();
1434 } else if (NonTypeTemplateParmDecl *PD =
1435 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1436 Depth = PD->getDepth();
1437 } else {
1438 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1439 }
1440 }
1441
1442 bool Matches(unsigned ParmDepth) {
1443 if (ParmDepth >= Depth) {
1444 Match = true;
1445 return true;
1446 }
1447 return false;
1448 }
1449
1450 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1451 return !Matches(T->getDepth());
1452 }
1453
1454 bool TraverseTemplateName(TemplateName N) {
1455 if (TemplateTemplateParmDecl *PD =
1456 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1457 if (Matches(PD->getDepth())) return false;
1458 return super::TraverseTemplateName(N);
1459 }
1460
1461 bool VisitDeclRefExpr(DeclRefExpr *E) {
1462 if (NonTypeTemplateParmDecl *PD =
1463 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1464 if (PD->getDepth() == Depth) {
1465 Match = true;
1466 return false;
1467 }
1468 }
1469 return super::VisitDeclRefExpr(E);
1470 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001471
1472 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1473 return TraverseType(T->getInjectedSpecializationType());
1474 }
John McCall4e2cbb22010-10-20 05:44:58 +00001475};
1476}
1477
Douglas Gregorc8406492011-05-10 18:27:06 +00001478/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001479/// list.
1480static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001481DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001482 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001483 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001484 return Checker.Match;
1485}
1486
Douglas Gregorc8406492011-05-10 18:27:06 +00001487// Find the source range corresponding to the named type in the given
1488// nested-name-specifier, if any.
1489static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1490 QualType T,
1491 const CXXScopeSpec &SS) {
1492 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1493 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1494 if (const Type *CurType = NNS->getAsType()) {
1495 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1496 return NNSLoc.getTypeLoc().getSourceRange();
1497 } else
1498 break;
1499
1500 NNSLoc = NNSLoc.getPrefix();
1501 }
1502
1503 return SourceRange();
1504}
1505
Mike Stump1eb44332009-09-09 15:08:12 +00001506/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001507/// specifier, returning the template parameter list that applies to the
1508/// name.
1509///
1510/// \param DeclStartLoc the start of the declaration that has a scope
1511/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001512///
Douglas Gregorc8406492011-05-10 18:27:06 +00001513/// \param DeclLoc The location of the declaration itself.
1514///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001515/// \param SS the scope specifier that will be matched to the given template
1516/// parameter lists. This scope specifier precedes a qualified name that is
1517/// being declared.
1518///
1519/// \param ParamLists the template parameter lists, from the outermost to the
1520/// innermost template parameter lists.
1521///
1522/// \param NumParamLists the number of template parameter lists in ParamLists.
1523///
John McCall77e8b112010-04-13 20:37:33 +00001524/// \param IsFriend Whether to apply the slightly different rules for
1525/// matching template parameters to scope specifiers in friend
1526/// declarations.
1527///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001528/// \param IsExplicitSpecialization will be set true if the entity being
1529/// declared is an explicit specialization, false otherwise.
1530///
Mike Stump1eb44332009-09-09 15:08:12 +00001531/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001532/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001533/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001534/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001535/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001536/// itself a template).
1537TemplateParameterList *
1538Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001539 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001540 const CXXScopeSpec &SS,
1541 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001542 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001543 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001544 bool &IsExplicitSpecialization,
1545 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001546 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001547 Invalid = false;
1548
1549 // The sequence of nested types to which we will match up the template
1550 // parameter lists. We first build this list by starting with the type named
1551 // by the nested-name-specifier and walking out until we run out of types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001552 SmallVector<QualType, 4> NestedTypes;
Douglas Gregorc8406492011-05-10 18:27:06 +00001553 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001554 if (SS.getScopeRep()) {
1555 if (CXXRecordDecl *Record
1556 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1557 T = Context.getTypeDeclType(Record);
1558 else
1559 T = QualType(SS.getScopeRep()->getAsType(), 0);
1560 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001561
1562 // If we found an explicit specialization that prevents us from needing
1563 // 'template<>' headers, this will be set to the location of that
1564 // explicit specialization.
1565 SourceLocation ExplicitSpecLoc;
1566
1567 while (!T.isNull()) {
1568 NestedTypes.push_back(T);
1569
1570 // Retrieve the parent of a record type.
1571 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1572 // If this type is an explicit specialization, we're done.
1573 if (ClassTemplateSpecializationDecl *Spec
1574 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1575 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1576 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1577 ExplicitSpecLoc = Spec->getLocation();
1578 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001579 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001580 } else if (Record->getTemplateSpecializationKind()
1581 == TSK_ExplicitSpecialization) {
1582 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001583 break;
1584 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001585
1586 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1587 T = Context.getTypeDeclType(Parent);
1588 else
1589 T = QualType();
1590 continue;
1591 }
1592
1593 if (const TemplateSpecializationType *TST
1594 = T->getAs<TemplateSpecializationType>()) {
1595 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1596 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1597 T = Context.getTypeDeclType(Parent);
1598 else
1599 T = QualType();
1600 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001601 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001602 }
1603
1604 // Look one step prior in a dependent template specialization type.
1605 if (const DependentTemplateSpecializationType *DependentTST
1606 = T->getAs<DependentTemplateSpecializationType>()) {
1607 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1608 T = QualType(NNS->getAsType(), 0);
1609 else
1610 T = QualType();
1611 continue;
1612 }
1613
1614 // Look one step prior in a dependent name type.
1615 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1616 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1617 T = QualType(NNS->getAsType(), 0);
1618 else
1619 T = QualType();
1620 continue;
1621 }
1622
1623 // Retrieve the parent of an enumeration type.
1624 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1625 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1626 // check here.
1627 EnumDecl *Enum = EnumT->getDecl();
1628
1629 // Get to the parent type.
1630 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1631 T = Context.getTypeDeclType(Parent);
1632 else
1633 T = QualType();
1634 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001635 }
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Douglas Gregorc8406492011-05-10 18:27:06 +00001637 T = QualType();
1638 }
1639 // Reverse the nested types list, since we want to traverse from the outermost
1640 // to the innermost while checking template-parameter-lists.
1641 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001642
Douglas Gregorc8406492011-05-10 18:27:06 +00001643 // C++0x [temp.expl.spec]p17:
1644 // A member or a member template may be nested within many
1645 // enclosing class templates. In an explicit specialization for
1646 // such a member, the member declaration shall be preceded by a
1647 // template<> for each enclosing class template that is
1648 // explicitly specialized.
Douglas Gregor89b9f102011-06-06 15:22:55 +00001649 bool SawNonEmptyTemplateParameterList = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001650 unsigned ParamIdx = 0;
1651 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1652 ++TypeIdx) {
1653 T = NestedTypes[TypeIdx];
1654
1655 // Whether we expect a 'template<>' header.
1656 bool NeedEmptyTemplateHeader = false;
1657
1658 // Whether we expect a template header with parameters.
1659 bool NeedNonemptyTemplateHeader = false;
1660
1661 // For a dependent type, the set of template parameters that we
1662 // expect to see.
1663 TemplateParameterList *ExpectedTemplateParams = 0;
1664
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001665 // C++0x [temp.expl.spec]p15:
1666 // A member or a member template may be nested within many enclosing
1667 // class templates. In an explicit specialization for such a member, the
1668 // member declaration shall be preceded by a template<> for each
1669 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001670 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1671 if (ClassTemplatePartialSpecializationDecl *Partial
1672 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1673 ExpectedTemplateParams = Partial->getTemplateParameters();
1674 NeedNonemptyTemplateHeader = true;
1675 } else if (Record->isDependentType()) {
1676 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001677 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001678 ->getTemplateParameters();
1679 NeedNonemptyTemplateHeader = true;
1680 }
1681 } else if (ClassTemplateSpecializationDecl *Spec
1682 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1683 // C++0x [temp.expl.spec]p4:
1684 // Members of an explicitly specialized class template are defined
1685 // in the same manner as members of normal classes, and not using
1686 // the template<> syntax.
1687 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1688 NeedEmptyTemplateHeader = true;
1689 else
Douglas Gregor95ea4502011-06-01 22:37:07 +00001690 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001691 } else if (Record->getTemplateSpecializationKind()) {
1692 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001693 != TSK_ExplicitSpecialization &&
1694 TypeIdx == NumTypes - 1)
1695 IsExplicitSpecialization = true;
1696
1697 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001698 }
1699 } else if (const TemplateSpecializationType *TST
1700 = T->getAs<TemplateSpecializationType>()) {
1701 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1702 ExpectedTemplateParams = Template->getTemplateParameters();
1703 NeedNonemptyTemplateHeader = true;
1704 }
1705 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1706 // FIXME: We actually could/should check the template arguments here
1707 // against the corresponding template parameter list.
1708 NeedNonemptyTemplateHeader = false;
1709 }
1710
Douglas Gregor89b9f102011-06-06 15:22:55 +00001711 // C++ [temp.expl.spec]p16:
1712 // In an explicit specialization declaration for a member of a class
1713 // template or a member template that ap- pears in namespace scope, the
1714 // member template and some of its enclosing class templates may remain
1715 // unspecialized, except that the declaration shall not explicitly
1716 // specialize a class member template if its en- closing class templates
1717 // are not explicitly specialized as well.
1718 if (ParamIdx < NumParamLists) {
1719 if (ParamLists[ParamIdx]->size() == 0) {
1720 if (SawNonEmptyTemplateParameterList) {
1721 Diag(DeclLoc, diag::err_specialize_member_of_template)
1722 << ParamLists[ParamIdx]->getSourceRange();
1723 Invalid = true;
1724 IsExplicitSpecialization = false;
1725 return 0;
1726 }
1727 } else
1728 SawNonEmptyTemplateParameterList = true;
1729 }
1730
Douglas Gregorc8406492011-05-10 18:27:06 +00001731 if (NeedEmptyTemplateHeader) {
1732 // If we're on the last of the types, and we need a 'template<>' header
1733 // here, then it's an explicit specialization.
1734 if (TypeIdx == NumTypes - 1)
1735 IsExplicitSpecialization = true;
1736
1737 if (ParamIdx < NumParamLists) {
1738 if (ParamLists[ParamIdx]->size() > 0) {
1739 // The header has template parameters when it shouldn't. Complain.
1740 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1741 diag::err_template_param_list_matches_nontemplate)
1742 << T
1743 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1744 ParamLists[ParamIdx]->getRAngleLoc())
1745 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1746 Invalid = true;
1747 return 0;
1748 }
1749
1750 // Consume this template header.
1751 ++ParamIdx;
1752 continue;
1753 }
1754
1755 if (!IsFriend) {
1756 // We don't have a template header, but we should.
1757 SourceLocation ExpectedTemplateLoc;
1758 if (NumParamLists > 0)
1759 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1760 else
1761 ExpectedTemplateLoc = DeclStartLoc;
1762
1763 Diag(DeclLoc, diag::err_template_spec_needs_header)
1764 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1765 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1766 }
1767
1768 continue;
1769 }
1770
1771 if (NeedNonemptyTemplateHeader) {
1772 // In friend declarations we can have template-ids which don't
1773 // depend on the corresponding template parameter lists. But
1774 // assume that empty parameter lists are supposed to match this
1775 // template-id.
1776 if (IsFriend && T->isDependentType()) {
1777 if (ParamIdx < NumParamLists &&
1778 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1779 ExpectedTemplateParams = 0;
1780 else
1781 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001782 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001783
Douglas Gregorc8406492011-05-10 18:27:06 +00001784 if (ParamIdx < NumParamLists) {
1785 // Check the template parameter list, if we can.
1786 if (ExpectedTemplateParams &&
1787 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1788 ExpectedTemplateParams,
1789 true, TPL_TemplateMatch))
1790 Invalid = true;
1791
1792 if (!Invalid &&
1793 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1794 TPC_ClassTemplateMember))
1795 Invalid = true;
1796
1797 ++ParamIdx;
1798 continue;
1799 }
1800
1801 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1802 << T
1803 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1804 Invalid = true;
1805 continue;
1806 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001807 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001808
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001809 // If there were at least as many template-ids as there were template
1810 // parameter lists, then there are no template parameter lists remaining for
1811 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001812 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001813 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001814
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001815 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001816 if (ParamIdx < NumParamLists - 1) {
1817 bool HasAnyExplicitSpecHeader = false;
1818 bool AllExplicitSpecHeaders = true;
1819 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1820 if (ParamLists[I]->size() == 0)
1821 HasAnyExplicitSpecHeader = true;
1822 else
1823 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001824 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001825
1826 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1827 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1828 : diag::err_template_spec_extra_headers)
1829 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1830 ParamLists[NumParamLists - 2]->getRAngleLoc());
1831
1832 // If there was a specialization somewhere, such that 'template<>' is
1833 // not required, and there were any 'template<>' headers, note where the
1834 // specialization occurred.
1835 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1836 Diag(ExplicitSpecLoc,
1837 diag::note_explicit_template_spec_does_not_need_header)
1838 << NestedTypes.back();
1839
1840 // We have a template parameter list with no corresponding scope, which
1841 // means that the resulting template declaration can't be instantiated
1842 // properly (we'll end up with dependent nodes when we shouldn't).
1843 if (!AllExplicitSpecHeaders)
1844 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001845 }
Mike Stump1eb44332009-09-09 15:08:12 +00001846
Douglas Gregor89b9f102011-06-06 15:22:55 +00001847 // C++ [temp.expl.spec]p16:
1848 // In an explicit specialization declaration for a member of a class
1849 // template or a member template that ap- pears in namespace scope, the
1850 // member template and some of its enclosing class templates may remain
1851 // unspecialized, except that the declaration shall not explicitly
1852 // specialize a class member template if its en- closing class templates
1853 // are not explicitly specialized as well.
1854 if (ParamLists[NumParamLists - 1]->size() == 0 &&
1855 SawNonEmptyTemplateParameterList) {
1856 Diag(DeclLoc, diag::err_specialize_member_of_template)
1857 << ParamLists[ParamIdx]->getSourceRange();
1858 Invalid = true;
1859 IsExplicitSpecialization = false;
1860 return 0;
1861 }
1862
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001863 // Return the last template parameter list, which corresponds to the
1864 // entity being declared.
1865 return ParamLists[NumParamLists - 1];
1866}
1867
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001868void Sema::NoteAllFoundTemplates(TemplateName Name) {
1869 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1870 Diag(Template->getLocation(), diag::note_template_declared_here)
1871 << (isa<FunctionTemplateDecl>(Template)? 0
1872 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001873 : isa<TypeAliasTemplateDecl>(Template)? 2
1874 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001875 << Template->getDeclName();
1876 return;
1877 }
1878
1879 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1880 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1881 IEnd = OST->end();
1882 I != IEnd; ++I)
1883 Diag((*I)->getLocation(), diag::note_template_declared_here)
1884 << 0 << (*I)->getDeclName();
1885
1886 return;
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;
Douglas Gregorb70126a2012-02-03 17:16:23 +00001921 bool ExpansionIntoFixedList = false;
John McCalld5532b62009-11-23 01:53:49 +00001922 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregorb70126a2012-02-03 17:16:23 +00001923 false, Converted, &ExpansionIntoFixedList))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001924 return QualType();
1925
Douglas Gregor40808ce2009-03-09 23:48:35 +00001926 QualType CanonType;
1927
Douglas Gregor561f8122011-07-01 01:22:09 +00001928 bool InstantiationDependent = false;
Douglas Gregorb70126a2012-02-03 17:16:23 +00001929 TypeAliasTemplateDecl *AliasTemplate = 0;
1930 if (!ExpansionIntoFixedList &&
1931 (AliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Template))) {
Richard Smith3e4c6c42011-05-05 21:57:07 +00001932 // Find the canonical type for this type alias template specialization.
1933 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1934 if (Pattern->isInvalidDecl())
1935 return QualType();
1936
1937 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1938 Converted.data(), Converted.size());
1939
1940 // Only substitute for the innermost template argument list.
1941 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001942 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001943 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1944 for (unsigned I = 0; I < Depth; ++I)
1945 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001946
1947 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1948 CanonType = SubstType(Pattern->getUnderlyingType(),
1949 TemplateArgLists, AliasTemplate->getLocation(),
1950 AliasTemplate->getDeclName());
1951 if (CanonType.isNull())
1952 return QualType();
1953 } else if (Name.isDependent() ||
1954 TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregor561f8122011-07-01 01:22:09 +00001955 TemplateArgs, InstantiationDependent)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001956 // This class template specialization is a dependent
1957 // type. Therefore, its canonical type is another class template
1958 // specialization type that contains all of the converted
1959 // arguments in canonical form. This ensures that, e.g., A<T> and
1960 // A<T, T> have identical types when A is declared as:
1961 //
1962 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001963 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001964 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001965 Converted.data(),
1966 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001967
Douglas Gregor1275ae02009-07-28 23:00:59 +00001968 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001969 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001970 // In the future, we need to teach getTemplateSpecializationType to only
1971 // build the canonical type and return that to us.
1972 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001973
1974 // This might work out to be a current instantiation, in which
1975 // case the canonical type needs to be the InjectedClassNameType.
1976 //
1977 // TODO: in theory this could be a simple hashtable lookup; most
1978 // changes to CurContext don't change the set of current
1979 // instantiations.
1980 if (isa<ClassTemplateDecl>(Template)) {
1981 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1982 // If we get out to a namespace, we're done.
1983 if (Ctx->isFileContext()) break;
1984
1985 // If this isn't a record, keep looking.
1986 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1987 if (!Record) continue;
1988
1989 // Look for one of the two cases with InjectedClassNameTypes
1990 // and check whether it's the same template.
1991 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1992 !Record->getDescribedClassTemplate())
1993 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001994
John McCall31f17ec2010-04-27 00:57:59 +00001995 // Fetch the injected class name type and check whether its
1996 // injected type is equal to the type we just built.
1997 QualType ICNT = Context.getTypeDeclType(Record);
1998 QualType Injected = cast<InjectedClassNameType>(ICNT)
1999 ->getInjectedSpecializationType();
2000
2001 if (CanonType != Injected->getCanonicalTypeInternal())
2002 continue;
2003
2004 // If so, the canonical type of this TST is the injected
2005 // class name type of the record we just found.
2006 assert(ICNT.isCanonical());
2007 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00002008 break;
2009 }
2010 }
Mike Stump1eb44332009-09-09 15:08:12 +00002011 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00002012 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00002013 // Find the class template specialization declaration that
2014 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00002015 void *InsertPos = 0;
2016 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002017 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002018 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002019 if (!Decl) {
2020 // This is the first time we have referenced this class template
2021 // specialization. Create the canonical declaration and add it to
2022 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00002023 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002024 ClassTemplate->getTemplatedDecl()->getTagKind(),
2025 ClassTemplate->getDeclContext(),
Abramo Bagnara09d82122011-10-03 20:34:03 +00002026 ClassTemplate->getTemplatedDecl()->getLocStart(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002027 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002028 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002029 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002030 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002031 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002032 Decl->setLexicalDeclContext(CurContext);
2033 }
2034
2035 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00002036 assert(isa<RecordType>(CanonType) &&
2037 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00002038 }
Mike Stump1eb44332009-09-09 15:08:12 +00002039
Douglas Gregor40808ce2009-03-09 23:48:35 +00002040 // Build the fully-sugared type for this class template
2041 // specialization, which refers back to the class template
2042 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00002043 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002044}
2045
John McCallf312b1e2010-08-26 23:41:50 +00002046TypeResult
Douglas Gregor059101f2011-03-02 00:47:37 +00002047Sema::ActOnTemplateIdType(CXXScopeSpec &SS,
2048 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002049 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002050 ASTTemplateArgsPtr TemplateArgsIn,
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002051 SourceLocation RAngleLoc,
2052 bool IsCtorOrDtorName) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002053 if (SS.isInvalid())
2054 return true;
2055
Douglas Gregor7532dc62009-03-30 22:58:21 +00002056 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00002057
Douglas Gregor40808ce2009-03-09 23:48:35 +00002058 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00002059 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00002060 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002061
Douglas Gregora88f09f2011-02-28 17:23:35 +00002062 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002063 QualType T
2064 = Context.getDependentTemplateSpecializationType(ETK_None,
2065 DTN->getQualifier(),
2066 DTN->getIdentifier(),
2067 TemplateArgs);
2068 // Build type-source information.
Douglas Gregora88f09f2011-02-28 17:23:35 +00002069 TypeLocBuilder TLB;
2070 DependentTemplateSpecializationTypeLoc SpecTL
2071 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregor059101f2011-03-02 00:47:37 +00002072 SpecTL.setKeywordLoc(SourceLocation());
Douglas Gregora88f09f2011-02-28 17:23:35 +00002073 SpecTL.setNameLoc(TemplateLoc);
2074 SpecTL.setLAngleLoc(LAngleLoc);
2075 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00002076 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregora88f09f2011-02-28 17:23:35 +00002077 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2078 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2079 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2080 }
2081
John McCalld5532b62009-11-23 01:53:49 +00002082 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002083 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002084
2085 if (Result.isNull())
2086 return true;
2087
Douglas Gregor059101f2011-03-02 00:47:37 +00002088 // Build type-source information.
2089 TypeLocBuilder TLB;
2090 TemplateSpecializationTypeLoc SpecTL
2091 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2092 SpecTL.setTemplateNameLoc(TemplateLoc);
2093 SpecTL.setLAngleLoc(LAngleLoc);
2094 SpecTL.setRAngleLoc(RAngleLoc);
2095 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2096 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002097
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002098 // NOTE: avoid constructing an ElaboratedTypeLoc if this is a
2099 // constructor or destructor name (in such a case, the scope specifier
2100 // will be attached to the enclosing Decl or Expr node).
2101 if (SS.isNotEmpty() && !IsCtorOrDtorName) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002102 // Create an elaborated-type-specifier containing the nested-name-specifier.
2103 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2104 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2105 ElabTL.setKeywordLoc(SourceLocation());
2106 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2107 }
2108
2109 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002110}
John McCallf1bbbb42009-09-04 01:14:41 +00002111
Douglas Gregor059101f2011-03-02 00:47:37 +00002112TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002113 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002114 SourceLocation TagLoc,
2115 CXXScopeSpec &SS,
2116 TemplateTy TemplateD,
2117 SourceLocation TemplateLoc,
2118 SourceLocation LAngleLoc,
2119 ASTTemplateArgsPtr TemplateArgsIn,
2120 SourceLocation RAngleLoc) {
2121 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2122
2123 // Translate the parser's template argument list in our AST format.
2124 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2125 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2126
2127 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002128 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002129 ElaboratedTypeKeyword Keyword
2130 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002131
Douglas Gregor059101f2011-03-02 00:47:37 +00002132 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2133 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2134 DTN->getQualifier(),
2135 DTN->getIdentifier(),
2136 TemplateArgs);
2137
2138 // Build type-source information.
2139 TypeLocBuilder TLB;
2140 DependentTemplateSpecializationTypeLoc SpecTL
2141 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2142 SpecTL.setKeywordLoc(TagLoc);
2143 SpecTL.setNameLoc(TemplateLoc);
2144 SpecTL.setLAngleLoc(LAngleLoc);
2145 SpecTL.setRAngleLoc(RAngleLoc);
2146 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
2147 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2148 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2149 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2150 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002151
2152 if (TypeAliasTemplateDecl *TAT =
2153 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2154 // C++0x [dcl.type.elab]p2:
2155 // If the identifier resolves to a typedef-name or the simple-template-id
2156 // resolves to an alias template specialization, the
2157 // elaborated-type-specifier is ill-formed.
2158 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2159 Diag(TAT->getLocation(), diag::note_declared_at);
2160 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002161
2162 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2163 if (Result.isNull())
Matt Beaumont-Gay3a51d412011-08-25 23:22:24 +00002164 return TypeResult(true);
Douglas Gregor059101f2011-03-02 00:47:37 +00002165
2166 // Check the tag kind
2167 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002168 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002169
John McCall6b2becf2009-09-08 17:47:29 +00002170 IdentifierInfo *Id = D->getIdentifier();
2171 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002172
Richard Trieubbf34c02011-06-10 03:11:26 +00002173 if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
2174 TagLoc, *Id)) {
John McCall6b2becf2009-09-08 17:47:29 +00002175 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002176 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002177 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002178 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002179 }
2180 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002181
2182 // Provide source-location information for the template specialization.
2183 TypeLocBuilder TLB;
2184 TemplateSpecializationTypeLoc SpecTL
2185 = TLB.push<TemplateSpecializationTypeLoc>(Result);
2186 SpecTL.setTemplateNameLoc(TemplateLoc);
2187 SpecTL.setLAngleLoc(LAngleLoc);
2188 SpecTL.setRAngleLoc(RAngleLoc);
2189 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2190 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002191
Douglas Gregor059101f2011-03-02 00:47:37 +00002192 // Construct an elaborated type containing the nested-name-specifier (if any)
2193 // and keyword.
2194 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2195 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
2196 ElabTL.setKeywordLoc(TagLoc);
2197 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2198 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002199}
2200
John McCall60d7b3a2010-08-24 06:29:42 +00002201ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002202 SourceLocation TemplateKWLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002203 LookupResult &R,
2204 bool RequiresADL,
John McCalld5532b62009-11-23 01:53:49 +00002205 const TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002206 // FIXME: Can we do any checking at this point? I guess we could check the
2207 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002208 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002209 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002210 // foo<int> could identify a single function unambiguously
2211 // This approach does NOT work, since f<int>(1);
2212 // gets resolved prior to resorting to overload resolution
2213 // i.e., template<class T> void f(double);
2214 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002215
2216 // These should be filtered out by our callers.
2217 assert(!R.empty() && "empty lookup results when building templateid");
2218 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2219
John McCallc373d482010-01-27 01:50:18 +00002220 // We don't want lookup warnings at this point.
2221 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002222
John McCallf7a1a742009-11-24 19:00:30 +00002223 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002224 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002225 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002226 TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002227 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002228 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002229 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002230
2231 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002232}
2233
John McCallf7a1a742009-11-24 19:00:30 +00002234// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002235ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002236Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002237 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002238 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +00002239 const TemplateArgumentListInfo &TemplateArgs) {
2240 DeclContext *DC;
2241 if (!(DC = computeDeclContext(SS, false)) ||
2242 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002243 RequireCompleteDeclContext(SS, DC))
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002244 return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo,
2245 &TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002246
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002247 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002248 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002249 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2250 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002251
John McCallf7a1a742009-11-24 19:00:30 +00002252 if (R.isAmbiguous())
2253 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002254
John McCallf7a1a742009-11-24 19:00:30 +00002255 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002256 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2257 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002258 return ExprError();
2259 }
2260
2261 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002262 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2263 << (NestedNameSpecifier*) SS.getScopeRep()
2264 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002265 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2266 return ExprError();
2267 }
2268
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002269 return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002270}
2271
Douglas Gregorc45c2322009-03-31 00:43:58 +00002272/// \brief Form a dependent template name.
2273///
2274/// This action forms a dependent template name given the template
2275/// name and its (presumably dependent) scope specifier. For
2276/// example, given "MetaFun::template apply", the scope specifier \p
2277/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2278/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002279TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002280 CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002281 SourceLocation TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002282 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002283 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002284 bool EnteringContext,
2285 TemplateTy &Result) {
Richard Smithebaf0e62011-10-18 20:49:44 +00002286 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent())
2287 Diag(TemplateKWLoc,
2288 getLangOptions().CPlusPlus0x ?
2289 diag::warn_cxx98_compat_template_outside_of_template :
2290 diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002291 << FixItHint::CreateRemoval(TemplateKWLoc);
2292
Douglas Gregor0707bc52010-01-19 16:01:07 +00002293 DeclContext *LookupCtx = 0;
2294 if (SS.isSet())
2295 LookupCtx = computeDeclContext(SS, EnteringContext);
2296 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002297 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002298 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002299 // C++0x [temp.names]p5:
2300 // If a name prefixed by the keyword template is not the name of
2301 // a template, the program is ill-formed. [Note: the keyword
2302 // template may not be applied to non-template members of class
2303 // templates. -end note ] [ Note: as is the case with the
2304 // typename prefix, the template prefix is allowed in cases
2305 // where it is not strictly necessary; i.e., when the
2306 // nested-name-specifier or the expression on the left of the ->
2307 // or . is not dependent on a template-parameter, or the use
2308 // does not appear in the scope of a template. -end note]
2309 //
2310 // Note: C++03 was more strict here, because it banned the use of
2311 // the "template" keyword prior to a template-name that was not a
2312 // dependent name. C++ DR468 relaxed this requirement (the
2313 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002314 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002315 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002316 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2317 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002318 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002319 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2320 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002321 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2322 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002323 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002324 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002325 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002326 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002327 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002328 << Name.getSourceRange()
2329 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002330 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002331 } else {
2332 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002333 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002334 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002335 }
2336
Mike Stump1eb44332009-09-09 15:08:12 +00002337 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002338 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002339
Douglas Gregor014e88d2009-11-03 23:16:33 +00002340 switch (Name.getKind()) {
2341 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002342 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002343 Name.Identifier));
2344 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002345
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002346 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002347 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002348 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002349 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002350
2351 case UnqualifiedId::IK_LiteralOperatorId:
David Blaikieb219cfc2011-09-23 05:06:16 +00002352 llvm_unreachable(
2353 "We don't support these; Parse shouldn't have allowed propagation");
Sean Hunte6252d12009-11-28 08:58:14 +00002354
Douglas Gregor014e88d2009-11-03 23:16:33 +00002355 default:
2356 break;
2357 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002358
2359 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002360 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002361 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002362 << Name.getSourceRange()
2363 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002364 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002365}
2366
Mike Stump1eb44332009-09-09 15:08:12 +00002367bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002368 const TemplateArgumentLoc &AL,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002369 SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002370 const TemplateArgument &Arg = AL.getArgument();
2371
Anders Carlsson436b1562009-06-13 00:33:33 +00002372 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002373 switch(Arg.getKind()) {
2374 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002375 // C++ [temp.arg.type]p1:
2376 // A template-argument for a template-parameter which is a
2377 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002378 break;
2379 case TemplateArgument::Template: {
2380 // We have a template type parameter but the template argument
2381 // is a template without any arguments.
2382 SourceRange SR = AL.getSourceRange();
2383 TemplateName Name = Arg.getAsTemplate();
2384 Diag(SR.getBegin(), diag::err_template_missing_args)
2385 << Name << SR;
2386 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2387 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002388
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002389 return true;
2390 }
2391 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002392 // We have a template type parameter but the template argument
2393 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002394 SourceRange SR = AL.getSourceRange();
2395 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002396 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002397
Anders Carlsson436b1562009-06-13 00:33:33 +00002398 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002399 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002400 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002401
John McCalla93c9342009-12-07 02:54:59 +00002402 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002403 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002404
Anders Carlsson436b1562009-06-13 00:33:33 +00002405 // Add the converted template type argument.
Douglas Gregore559ca12011-06-17 22:11:49 +00002406 QualType ArgType = Context.getCanonicalType(Arg.getAsType());
2407
2408 // Objective-C ARC:
2409 // If an explicitly-specified template argument type is a lifetime type
2410 // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
2411 if (getLangOptions().ObjCAutoRefCount &&
2412 ArgType->isObjCLifetimeType() &&
2413 !ArgType.getObjCLifetime()) {
2414 Qualifiers Qs;
2415 Qs.setObjCLifetime(Qualifiers::OCL_Strong);
2416 ArgType = Context.getQualifiedType(ArgType, Qs);
2417 }
2418
2419 Converted.push_back(TemplateArgument(ArgType));
Anders Carlsson436b1562009-06-13 00:33:33 +00002420 return false;
2421}
2422
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002423/// \brief Substitute template arguments into the default template argument for
2424/// the given template type parameter.
2425///
2426/// \param SemaRef the semantic analysis object for which we are performing
2427/// the substitution.
2428///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002429/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002430/// for.
2431///
2432/// \param TemplateLoc the location of the template name that started the
2433/// template-id we are checking.
2434///
2435/// \param RAngleLoc the location of the right angle bracket ('>') that
2436/// terminates the template-id.
2437///
2438/// \param Param the template template parameter whose default we are
2439/// substituting into.
2440///
2441/// \param Converted the list of template arguments provided for template
2442/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002443/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002444static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002445SubstDefaultTemplateArgument(Sema &SemaRef,
2446 TemplateDecl *Template,
2447 SourceLocation TemplateLoc,
2448 SourceLocation RAngleLoc,
2449 TemplateTypeParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002450 SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002451 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002452
2453 // If the argument type is dependent, instantiate it now based
2454 // on the previously-computed template arguments.
2455 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002456 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002457 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002458
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002459 MultiLevelTemplateArgumentList AllTemplateArgs
2460 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2461
2462 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002463 Template, Converted.data(),
2464 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002465 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002466
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002467 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2468 Param->getDefaultArgumentLoc(),
2469 Param->getDeclName());
2470 }
2471
2472 return ArgType;
2473}
2474
2475/// \brief Substitute template arguments into the default template argument for
2476/// the given non-type template parameter.
2477///
2478/// \param SemaRef the semantic analysis object for which we are performing
2479/// the substitution.
2480///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002481/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002482/// for.
2483///
2484/// \param TemplateLoc the location of the template name that started the
2485/// template-id we are checking.
2486///
2487/// \param RAngleLoc the location of the right angle bracket ('>') that
2488/// terminates the template-id.
2489///
Douglas Gregor788cd062009-11-11 01:00:40 +00002490/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002491/// substituting into.
2492///
2493/// \param Converted the list of template arguments provided for template
2494/// parameters that precede \p Param in the template parameter list.
2495///
2496/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002497static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002498SubstDefaultTemplateArgument(Sema &SemaRef,
2499 TemplateDecl *Template,
2500 SourceLocation TemplateLoc,
2501 SourceLocation RAngleLoc,
2502 NonTypeTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002503 SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002504 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002505 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002506
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002507 MultiLevelTemplateArgumentList AllTemplateArgs
2508 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002509
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002510 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002511 Template, Converted.data(),
2512 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002513 SourceRange(TemplateLoc, RAngleLoc));
2514
2515 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2516}
2517
Douglas Gregor788cd062009-11-11 01:00:40 +00002518/// \brief Substitute template arguments into the default template argument for
2519/// the given template template parameter.
2520///
2521/// \param SemaRef the semantic analysis object for which we are performing
2522/// the substitution.
2523///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002524/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002525/// for.
2526///
2527/// \param TemplateLoc the location of the template name that started the
2528/// template-id we are checking.
2529///
2530/// \param RAngleLoc the location of the right angle bracket ('>') that
2531/// terminates the template-id.
2532///
2533/// \param Param the template template parameter whose default we are
2534/// substituting into.
2535///
2536/// \param Converted the list of template arguments provided for template
2537/// parameters that precede \p Param in the template parameter list.
2538///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002539/// \param QualifierLoc Will be set to the nested-name-specifier (with
2540/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002541///
Douglas Gregor788cd062009-11-11 01:00:40 +00002542/// \returns the substituted template argument, or NULL if an error occurred.
2543static TemplateName
2544SubstDefaultTemplateArgument(Sema &SemaRef,
2545 TemplateDecl *Template,
2546 SourceLocation TemplateLoc,
2547 SourceLocation RAngleLoc,
2548 TemplateTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002549 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002550 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002551 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002552 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002553
Douglas Gregor788cd062009-11-11 01:00:40 +00002554 MultiLevelTemplateArgumentList AllTemplateArgs
2555 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002556
Douglas Gregor788cd062009-11-11 01:00:40 +00002557 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002558 Template, Converted.data(),
2559 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002560 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002561
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002562 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002563 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002564 if (QualifierLoc) {
2565 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2566 AllTemplateArgs);
2567 if (!QualifierLoc)
2568 return TemplateName();
2569 }
2570
Douglas Gregor1d752d72011-03-02 18:46:51 +00002571 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002572 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002573 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002574 AllTemplateArgs);
2575}
2576
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002577/// \brief If the given template parameter has a default template
2578/// argument, substitute into that default template argument and
2579/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002580TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002581Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2582 SourceLocation TemplateLoc,
2583 SourceLocation RAngleLoc,
2584 Decl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002585 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002586 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002587 if (!TypeParm->hasDefaultArgument())
2588 return TemplateArgumentLoc();
2589
John McCalla93c9342009-12-07 02:54:59 +00002590 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002591 TemplateLoc,
2592 RAngleLoc,
2593 TypeParm,
2594 Converted);
2595 if (DI)
2596 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2597
2598 return TemplateArgumentLoc();
2599 }
2600
2601 if (NonTypeTemplateParmDecl *NonTypeParm
2602 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2603 if (!NonTypeParm->hasDefaultArgument())
2604 return TemplateArgumentLoc();
2605
John McCall60d7b3a2010-08-24 06:29:42 +00002606 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002607 TemplateLoc,
2608 RAngleLoc,
2609 NonTypeParm,
2610 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002611 if (Arg.isInvalid())
2612 return TemplateArgumentLoc();
2613
2614 Expr *ArgE = Arg.takeAs<Expr>();
2615 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2616 }
2617
2618 TemplateTemplateParmDecl *TempTempParm
2619 = cast<TemplateTemplateParmDecl>(Param);
2620 if (!TempTempParm->hasDefaultArgument())
2621 return TemplateArgumentLoc();
2622
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002623
Douglas Gregor1d752d72011-03-02 18:46:51 +00002624 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002625 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002626 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002627 RAngleLoc,
2628 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002629 Converted,
2630 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002631 if (TName.isNull())
2632 return TemplateArgumentLoc();
2633
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002634 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002635 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002636 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2637}
2638
Douglas Gregore7526412009-11-11 19:31:23 +00002639/// \brief Check that the given template argument corresponds to the given
2640/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002641///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002642/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002643/// checked.
2644///
2645/// \param Arg The template argument.
2646///
2647/// \param Template The template in which the template argument resides.
2648///
2649/// \param TemplateLoc The location of the template name for the template
2650/// whose argument list we're matching.
2651///
2652/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2653/// the template argument list.
2654///
2655/// \param ArgumentPackIndex The index into the argument pack where this
2656/// argument will be placed. Only valid if the parameter is a parameter pack.
2657///
2658/// \param Converted The checked, converted argument will be added to the
2659/// end of this small vector.
2660///
2661/// \param CTAK Describes how we arrived at this particular template argument:
2662/// explicitly written, deduced, etc.
2663///
2664/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002665bool Sema::CheckTemplateArgument(NamedDecl *Param,
2666 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002667 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002668 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002669 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002670 unsigned ArgumentPackIndex,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002671 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002672 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002673 // Check template type parameters.
2674 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002675 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002676
Douglas Gregord9e15302009-11-11 19:41:09 +00002677 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002678 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002679 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002680 // with the template arguments we've seen thus far. But if the
2681 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002682 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002683 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2684 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002685
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002686 if (NTTPType->isDependentType() &&
2687 !isa<TemplateTemplateParmDecl>(Template) &&
2688 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002689 // Do substitution on the type of the non-type template parameter.
2690 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002691 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002692 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002693
2694 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002695 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002696 NTTPType = SubstType(NTTPType,
2697 MultiLevelTemplateArgumentList(TemplateArgs),
2698 NTTP->getLocation(),
2699 NTTP->getDeclName());
2700 // If that worked, check the non-type template parameter type
2701 // for validity.
2702 if (!NTTPType.isNull())
2703 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2704 NTTP->getLocation());
2705 if (NTTPType.isNull())
2706 return true;
2707 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002708
Douglas Gregore7526412009-11-11 19:31:23 +00002709 switch (Arg.getArgument().getKind()) {
2710 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002711 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002712
Douglas Gregore7526412009-11-11 19:31:23 +00002713 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002714 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002715 ExprResult Res =
2716 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2717 Result, CTAK);
2718 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002719 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002720
Douglas Gregor910f8002010-11-07 23:05:16 +00002721 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002722 break;
2723 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002724
Douglas Gregore7526412009-11-11 19:31:23 +00002725 case TemplateArgument::Declaration:
2726 case TemplateArgument::Integral:
2727 // We've already checked this template argument, so just copy
2728 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002729 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002730 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002731
Douglas Gregore7526412009-11-11 19:31:23 +00002732 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002733 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002734 // We were given a template template argument. It may not be ill-formed;
2735 // see below.
2736 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002737 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2738 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002739 // We have a template argument such as \c T::template X, which we
2740 // parsed as a template template argument. However, since we now
2741 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002742 // template name into an expression.
2743
2744 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2745 Arg.getTemplateNameLoc());
2746
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002747 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002748 SS.Adopt(Arg.getTemplateQualifierLoc());
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002749 // FIXME: the template-template arg was a DependentTemplateName,
2750 // so it was provided with a template keyword. However, its source
2751 // location is not stored in the template argument structure.
2752 SourceLocation TemplateKWLoc;
John Wiegley429bb272011-04-08 18:41:53 +00002753 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002754 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002755 TemplateKWLoc,
2756 NameInfo, 0));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002757
Douglas Gregora7fc9012011-01-05 18:58:31 +00002758 // If we parsed the template argument as a pack expansion, create a
2759 // pack expansion expression.
2760 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002761 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2762 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002763 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002764 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002765
Douglas Gregore7526412009-11-11 19:31:23 +00002766 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002767 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2768 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002769 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002770
Douglas Gregor910f8002010-11-07 23:05:16 +00002771 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002772 break;
2773 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002774
Douglas Gregore7526412009-11-11 19:31:23 +00002775 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002776 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002777 // therefore cannot be a non-type template argument.
2778 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2779 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002780
Douglas Gregore7526412009-11-11 19:31:23 +00002781 Diag(Param->getLocation(), diag::note_template_param_here);
2782 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002783
Douglas Gregore7526412009-11-11 19:31:23 +00002784 case TemplateArgument::Type: {
2785 // We have a non-type template parameter but the template
2786 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002787
Douglas Gregore7526412009-11-11 19:31:23 +00002788 // C++ [temp.arg]p2:
2789 // In a template-argument, an ambiguity between a type-id and
2790 // an expression is resolved to a type-id, regardless of the
2791 // form of the corresponding template-parameter.
2792 //
2793 // We warn specifically about this case, since it can be rather
2794 // confusing for users.
2795 QualType T = Arg.getArgument().getAsType();
2796 SourceRange SR = Arg.getSourceRange();
2797 if (T->isFunctionType())
2798 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2799 else
2800 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2801 Diag(Param->getLocation(), diag::note_template_param_here);
2802 return true;
2803 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002804
Douglas Gregore7526412009-11-11 19:31:23 +00002805 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002806 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002807 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002808
Douglas Gregore7526412009-11-11 19:31:23 +00002809 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002810 }
2811
2812
Douglas Gregore7526412009-11-11 19:31:23 +00002813 // Check template template parameters.
2814 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002815
Douglas Gregore7526412009-11-11 19:31:23 +00002816 // Substitute into the template parameter list of the template
2817 // template parameter, since previously-supplied template arguments
2818 // may appear within the template template parameter.
2819 {
2820 // Set up a template instantiation context.
2821 LocalInstantiationScope Scope(*this);
2822 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002823 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002824 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002825
2826 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002827 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002828 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002829 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002830 MultiLevelTemplateArgumentList(TemplateArgs)));
2831 if (!TempParm)
2832 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002833 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002834
Douglas Gregore7526412009-11-11 19:31:23 +00002835 switch (Arg.getArgument().getKind()) {
2836 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002837 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002838
Douglas Gregore7526412009-11-11 19:31:23 +00002839 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002840 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002841 if (CheckTemplateArgument(TempParm, Arg))
2842 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002843
Douglas Gregor910f8002010-11-07 23:05:16 +00002844 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002845 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002846
Douglas Gregore7526412009-11-11 19:31:23 +00002847 case TemplateArgument::Expression:
2848 case TemplateArgument::Type:
2849 // We have a template template parameter but the template
2850 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002851 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
2852 << getLangOptions().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002853 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002854
Douglas Gregore7526412009-11-11 19:31:23 +00002855 case TemplateArgument::Declaration:
David Blaikie7530c032012-01-17 06:56:22 +00002856 llvm_unreachable("Declaration argument with template template parameter");
Douglas Gregore7526412009-11-11 19:31:23 +00002857 case TemplateArgument::Integral:
David Blaikie7530c032012-01-17 06:56:22 +00002858 llvm_unreachable("Integral argument with template template parameter");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002859
Douglas Gregore7526412009-11-11 19:31:23 +00002860 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002861 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002862 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002863
Douglas Gregore7526412009-11-11 19:31:23 +00002864 return false;
2865}
2866
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002867/// \brief Diagnose an arity mismatch in the
2868static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template,
2869 SourceLocation TemplateLoc,
2870 TemplateArgumentListInfo &TemplateArgs) {
2871 TemplateParameterList *Params = Template->getTemplateParameters();
2872 unsigned NumParams = Params->size();
2873 unsigned NumArgs = TemplateArgs.size();
2874
2875 SourceRange Range;
2876 if (NumArgs > NumParams)
2877 Range = SourceRange(TemplateArgs[NumParams].getLocation(),
2878 TemplateArgs.getRAngleLoc());
2879 S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2880 << (NumArgs > NumParams)
2881 << (isa<ClassTemplateDecl>(Template)? 0 :
2882 isa<FunctionTemplateDecl>(Template)? 1 :
2883 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2884 << Template << Range;
2885 S.Diag(Template->getLocation(), diag::note_template_decl_here)
2886 << Params->getSourceRange();
2887 return true;
2888}
2889
Douglas Gregorc15cb382009-02-09 23:23:08 +00002890/// \brief Check that the given template argument list is well-formed
2891/// for specializing the given template.
2892bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2893 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002894 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002895 bool PartialTemplateArgs,
Douglas Gregorb70126a2012-02-03 17:16:23 +00002896 SmallVectorImpl<TemplateArgument> &Converted,
2897 bool *ExpansionIntoFixedList) {
2898 if (ExpansionIntoFixedList)
2899 *ExpansionIntoFixedList = false;
2900
Douglas Gregorc15cb382009-02-09 23:23:08 +00002901 TemplateParameterList *Params = Template->getTemplateParameters();
2902 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002903 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002904 bool Invalid = false;
2905
John McCalld5532b62009-11-23 01:53:49 +00002906 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2907
Mike Stump1eb44332009-09-09 15:08:12 +00002908 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002909 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Douglas Gregorb70126a2012-02-03 17:16:23 +00002910
Mike Stump1eb44332009-09-09 15:08:12 +00002911 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002912 // [...] The type and form of each template-argument specified in
2913 // a template-id shall match the type and form specified for the
2914 // corresponding parameter declared by the template in its
2915 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002916 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002917 SmallVector<TemplateArgument, 2> ArgumentPack;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002918 TemplateParameterList::iterator Param = Params->begin(),
2919 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002920 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002921 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002922 bool SawPackExpansion = false;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002923 while (Param != ParamEnd) {
Douglas Gregorf35f8282009-11-11 21:54:23 +00002924 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002925 // If we have an expanded parameter pack, make sure we don't have too
2926 // many arguments.
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002927 // FIXME: This really should fall out from the normal arity checking.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002928 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002929 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002930 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002931 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2932 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2933 << true
2934 << (isa<ClassTemplateDecl>(Template)? 0 :
2935 isa<FunctionTemplateDecl>(Template)? 1 :
2936 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2937 << Template;
2938 Diag(Template->getLocation(), diag::note_template_decl_here)
2939 << Params->getSourceRange();
2940 return true;
2941 }
2942 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002943
Douglas Gregorf35f8282009-11-11 21:54:23 +00002944 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002945 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2946 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002947 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002948 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002949
Douglas Gregor14be16b2010-12-20 16:57:52 +00002950 if ((*Param)->isTemplateParameterPack()) {
2951 // The template parameter was a template parameter pack, so take the
2952 // deduced argument and place it on the argument pack. Note that we
2953 // stay on the same template parameter so that we can deduce more
2954 // arguments.
2955 ArgumentPack.push_back(Converted.back());
2956 Converted.pop_back();
2957 } else {
2958 // Move to the next template parameter.
2959 ++Param;
2960 }
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002961
2962 // If this template argument is a pack expansion, record that fact
2963 // and break out; we can't actually check any more.
2964 if (TemplateArgs[ArgIdx].getArgument().isPackExpansion()) {
2965 SawPackExpansion = true;
2966 ++ArgIdx;
2967 break;
2968 }
2969
Douglas Gregor14be16b2010-12-20 16:57:52 +00002970 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002971 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002972 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002973
Douglas Gregor8735b292011-06-03 02:59:40 +00002974 // If we're checking a partial template argument list, we're done.
2975 if (PartialTemplateArgs) {
2976 if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
2977 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2978 ArgumentPack.data(),
2979 ArgumentPack.size()));
2980
2981 return Invalid;
2982 }
2983
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002984 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002985 // arguments, just break out now and we'll fill in the argument pack below.
2986 if ((*Param)->isTemplateParameterPack())
2987 break;
Douglas Gregorf968d832011-05-27 01:19:52 +00002988
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002989 // Check whether we have a default argument.
Douglas Gregorf35f8282009-11-11 21:54:23 +00002990 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002991
Douglas Gregorf35f8282009-11-11 21:54:23 +00002992 // Retrieve the default template argument from the template
2993 // parameter. For each kind of template parameter, we substitute the
2994 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002995 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00002996 // the default argument.
2997 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002998 if (!TTP->hasDefaultArgument())
2999 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3000 TemplateArgs);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003001
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003002 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003003 Template,
3004 TemplateLoc,
3005 RAngleLoc,
3006 TTP,
3007 Converted);
3008 if (!ArgType)
3009 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003010
Douglas Gregorf35f8282009-11-11 21:54:23 +00003011 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
3012 ArgType);
3013 } else if (NonTypeTemplateParmDecl *NTTP
3014 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003015 if (!NTTP->hasDefaultArgument())
3016 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3017 TemplateArgs);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003018
John McCall60d7b3a2010-08-24 06:29:42 +00003019 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003020 TemplateLoc,
3021 RAngleLoc,
3022 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003023 Converted);
3024 if (E.isInvalid())
3025 return true;
3026
3027 Expr *Ex = E.takeAs<Expr>();
3028 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
3029 } else {
3030 TemplateTemplateParmDecl *TempParm
3031 = cast<TemplateTemplateParmDecl>(*Param);
3032
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003033 if (!TempParm->hasDefaultArgument())
3034 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3035 TemplateArgs);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003036
Douglas Gregor1d752d72011-03-02 18:46:51 +00003037 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00003038 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003039 TemplateLoc,
3040 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003041 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003042 Converted,
3043 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003044 if (Name.isNull())
3045 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003046
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003047 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
3048 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00003049 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003050
Douglas Gregorf35f8282009-11-11 21:54:23 +00003051 // Introduce an instantiation record that describes where we are using
3052 // the default template argument.
3053 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00003054 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003055 SourceRange(TemplateLoc, RAngleLoc));
3056
Douglas Gregorf35f8282009-11-11 21:54:23 +00003057 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00003058 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00003059 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00003060 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003061
Douglas Gregor67714232011-03-03 02:41:12 +00003062 // Core issue 150 (assumed resolution): if this is a template template
3063 // parameter, keep track of the default template arguments from the
3064 // template definition.
3065 if (isTemplateTemplateParameter)
3066 TemplateArgs.addArgument(Arg);
3067
Douglas Gregor14be16b2010-12-20 16:57:52 +00003068 // Move to the next template parameter and argument.
3069 ++Param;
3070 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003071 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003072
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003073 // If we saw a pack expansion, then directly convert the remaining arguments,
3074 // because we don't know what parameters they'll match up with.
3075 if (SawPackExpansion) {
3076 bool AddToArgumentPack
3077 = Param != ParamEnd && (*Param)->isTemplateParameterPack();
3078 while (ArgIdx < NumArgs) {
3079 if (AddToArgumentPack)
3080 ArgumentPack.push_back(TemplateArgs[ArgIdx].getArgument());
3081 else
3082 Converted.push_back(TemplateArgs[ArgIdx].getArgument());
3083 ++ArgIdx;
3084 }
3085
3086 // Push the argument pack onto the list of converted arguments.
3087 if (AddToArgumentPack) {
3088 if (ArgumentPack.empty())
3089 Converted.push_back(TemplateArgument(0, 0));
3090 else {
3091 Converted.push_back(
3092 TemplateArgument::CreatePackCopy(Context,
3093 ArgumentPack.data(),
3094 ArgumentPack.size()));
3095 ArgumentPack.clear();
3096 }
Douglas Gregorb70126a2012-02-03 17:16:23 +00003097 } else if (ExpansionIntoFixedList) {
3098 // We have expanded a pack into a fixed list.
3099 *ExpansionIntoFixedList = true;
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003100 }
3101
3102 return Invalid;
3103 }
3104
3105 // If we have any leftover arguments, then there were too many arguments.
3106 // Complain and fail.
3107 if (ArgIdx < NumArgs)
3108 return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs);
3109
3110 // If we have an expanded parameter pack, make sure we don't have too
3111 // many arguments.
3112 // FIXME: This really should fall out from the normal arity checking.
3113 if (Param != ParamEnd) {
3114 if (NonTypeTemplateParmDecl *NTTP
3115 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
3116 if (NTTP->isExpandedParameterPack() &&
3117 ArgumentPack.size() < NTTP->getNumExpansionTypes()) {
3118 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
3119 << false
3120 << (isa<ClassTemplateDecl>(Template)? 0 :
3121 isa<FunctionTemplateDecl>(Template)? 1 :
3122 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
3123 << Template;
3124 Diag(Template->getLocation(), diag::note_template_decl_here)
3125 << Params->getSourceRange();
3126 return true;
3127 }
3128 }
3129 }
3130
Douglas Gregor14be16b2010-12-20 16:57:52 +00003131 // Form argument packs for each of the parameter packs remaining.
3132 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00003133 // If we're checking a partial list of template arguments, don't fill
3134 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003135 if ((*Param)->isTemplateParameterPack()) {
David Blaikie1368e582011-10-19 05:19:50 +00003136 if (!HasParameterPack)
3137 return true;
Douglas Gregor8735b292011-06-03 02:59:40 +00003138 if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00003139 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00003140 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003141 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3142 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00003143 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00003144 ArgumentPack.clear();
3145 }
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003146 } else if (!PartialTemplateArgs)
3147 return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003148
Douglas Gregor14be16b2010-12-20 16:57:52 +00003149 ++Param;
3150 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003151
Douglas Gregorc15cb382009-02-09 23:23:08 +00003152 return Invalid;
3153}
3154
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003155namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003156 class UnnamedLocalNoLinkageFinder
3157 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003158 {
3159 Sema &S;
3160 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003161
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003162 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003163
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003164 public:
3165 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3166
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003167 bool Visit(QualType T) {
3168 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003169 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003170
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003171#define TYPE(Class, Parent) \
3172 bool Visit##Class##Type(const Class##Type *);
3173#define ABSTRACT_TYPE(Class, Parent) \
3174 bool Visit##Class##Type(const Class##Type *) { return false; }
3175#define NON_CANONICAL_TYPE(Class, Parent) \
3176 bool Visit##Class##Type(const Class##Type *) { return false; }
3177#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003178
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003179 bool VisitTagDecl(const TagDecl *Tag);
3180 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3181 };
3182}
3183
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003184bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003185 return false;
3186}
3187
3188bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3189 return Visit(T->getElementType());
3190}
3191
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003192bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003193 return Visit(T->getPointeeType());
3194}
3195
3196bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003197 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003198 return Visit(T->getPointeeType());
3199}
3200
3201bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003202 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003203 return Visit(T->getPointeeType());
3204}
3205
3206bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003207 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003208 return Visit(T->getPointeeType());
3209}
3210
3211bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003212 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003213 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3214}
3215
3216bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003217 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003218 return Visit(T->getElementType());
3219}
3220
3221bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003222 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003223 return Visit(T->getElementType());
3224}
3225
3226bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003227 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003228 return Visit(T->getElementType());
3229}
3230
3231bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003232 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003233 return Visit(T->getElementType());
3234}
3235
3236bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003237 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003238 return Visit(T->getElementType());
3239}
3240
3241bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3242 return Visit(T->getElementType());
3243}
3244
3245bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3246 return Visit(T->getElementType());
3247}
3248
3249bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3250 const FunctionProtoType* T) {
3251 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003252 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003253 A != AEnd; ++A) {
3254 if (Visit(*A))
3255 return true;
3256 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003257
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003258 return Visit(T->getResultType());
3259}
3260
3261bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3262 const FunctionNoProtoType* T) {
3263 return Visit(T->getResultType());
3264}
3265
3266bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3267 const UnresolvedUsingType*) {
3268 return false;
3269}
3270
3271bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3272 return false;
3273}
3274
3275bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3276 return Visit(T->getUnderlyingType());
3277}
3278
3279bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3280 return false;
3281}
3282
Sean Huntca63c202011-05-24 22:41:36 +00003283bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3284 const UnaryTransformType*) {
3285 return false;
3286}
3287
Richard Smith34b41d92011-02-20 03:19:35 +00003288bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3289 return Visit(T->getDeducedType());
3290}
3291
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003292bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3293 return VisitTagDecl(T->getDecl());
3294}
3295
3296bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3297 return VisitTagDecl(T->getDecl());
3298}
3299
3300bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3301 const TemplateTypeParmType*) {
3302 return false;
3303}
3304
Douglas Gregorc3069d62011-01-14 02:55:32 +00003305bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3306 const SubstTemplateTypeParmPackType *) {
3307 return false;
3308}
3309
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003310bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3311 const TemplateSpecializationType*) {
3312 return false;
3313}
3314
3315bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3316 const InjectedClassNameType* T) {
3317 return VisitTagDecl(T->getDecl());
3318}
3319
3320bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3321 const DependentNameType* T) {
3322 return VisitNestedNameSpecifier(T->getQualifier());
3323}
3324
3325bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3326 const DependentTemplateSpecializationType* T) {
3327 return VisitNestedNameSpecifier(T->getQualifier());
3328}
3329
Douglas Gregor7536dd52010-12-20 02:24:11 +00003330bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3331 const PackExpansionType* T) {
3332 return Visit(T->getPattern());
3333}
3334
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003335bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3336 return false;
3337}
3338
3339bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3340 const ObjCInterfaceType *) {
3341 return false;
3342}
3343
3344bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3345 const ObjCObjectPointerType *) {
3346 return false;
3347}
3348
Eli Friedmanb001de72011-10-06 23:00:33 +00003349bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) {
3350 return Visit(T->getValueType());
3351}
3352
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003353bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3354 if (Tag->getDeclContext()->isFunctionOrMethod()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003355 S.Diag(SR.getBegin(),
3356 S.getLangOptions().CPlusPlus0x ?
3357 diag::warn_cxx98_compat_template_arg_local_type :
3358 diag::ext_template_arg_local_type)
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003359 << S.Context.getTypeDeclType(Tag) << SR;
3360 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003361 }
3362
Richard Smith162e1c12011-04-15 14:24:37 +00003363 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003364 S.Diag(SR.getBegin(),
3365 S.getLangOptions().CPlusPlus0x ?
3366 diag::warn_cxx98_compat_template_arg_unnamed_type :
3367 diag::ext_template_arg_unnamed_type) << SR;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003368 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3369 return true;
3370 }
3371
3372 return false;
3373}
3374
3375bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3376 NestedNameSpecifier *NNS) {
3377 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3378 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003379
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003380 switch (NNS->getKind()) {
3381 case NestedNameSpecifier::Identifier:
3382 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003383 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003384 case NestedNameSpecifier::Global:
3385 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003386
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003387 case NestedNameSpecifier::TypeSpec:
3388 case NestedNameSpecifier::TypeSpecWithTemplate:
3389 return Visit(QualType(NNS->getAsType(), 0));
3390 }
David Blaikie7530c032012-01-17 06:56:22 +00003391 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003392}
3393
3394
Douglas Gregorc15cb382009-02-09 23:23:08 +00003395/// \brief Check a template argument against its corresponding
3396/// template type parameter.
3397///
3398/// This routine implements the semantics of C++ [temp.arg.type]. It
3399/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003400bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003401 TypeSourceInfo *ArgInfo) {
3402 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003403 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003404 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003405
3406 if (Arg->isVariablyModifiedType()) {
3407 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003408 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003409 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003410 }
3411
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003412 // C++03 [temp.arg.type]p2:
3413 // A local type, a type with no linkage, an unnamed type or a type
3414 // compounded from any of these types shall not be used as a
3415 // template-argument for a template type-parameter.
3416 //
Richard Smithebaf0e62011-10-18 20:49:44 +00003417 // C++11 allows these, and even in C++03 we allow them as an extension with
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003418 // a warning.
Richard Smithebaf0e62011-10-18 20:49:44 +00003419 if (LangOpts.CPlusPlus0x ?
3420 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type,
3421 SR.getBegin()) != DiagnosticsEngine::Ignored ||
3422 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type,
3423 SR.getBegin()) != DiagnosticsEngine::Ignored :
3424 Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003425 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3426 (void)Finder.Visit(Context.getCanonicalType(Arg));
3427 }
3428
Douglas Gregorc15cb382009-02-09 23:23:08 +00003429 return false;
3430}
3431
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003432/// \brief Checks whether the given template argument is the address
3433/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003434static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003435CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3436 NonTypeTemplateParmDecl *Param,
3437 QualType ParamType,
3438 Expr *ArgIn,
3439 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003440 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003441 Expr *Arg = ArgIn;
3442 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003443
3444 // See through any implicit casts we added to fix the type.
John McCall91a57552011-07-15 05:09:51 +00003445 Arg = Arg->IgnoreImpCasts();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003446
3447 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003448 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003449 // A template-argument for a non-type, non-template
3450 // template-parameter shall be one of: [...]
3451 //
3452 // -- the address of an object or function with external
3453 // linkage, including function templates and function
3454 // template-ids but excluding non-static class members,
3455 // expressed as & id-expression where the & is optional if
3456 // the name refers to a function or array, or if the
3457 // corresponding template-parameter is a reference; or
Mike Stump1eb44332009-09-09 15:08:12 +00003458
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003459 // In C++98/03 mode, give an extension warning on any extra parentheses.
3460 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3461 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003462 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003463 if (!Invalid && !ExtraParens) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003464 S.Diag(Arg->getSourceRange().getBegin(),
Richard Smithebaf0e62011-10-18 20:49:44 +00003465 S.getLangOptions().CPlusPlus0x ?
3466 diag::warn_cxx98_compat_template_arg_extra_parens :
3467 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003468 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003469 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003470 }
3471
3472 Arg = Parens->getSubExpr();
3473 }
3474
John McCall91a57552011-07-15 05:09:51 +00003475 while (SubstNonTypeTemplateParmExpr *subst =
3476 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3477 Arg = subst->getReplacement()->IgnoreImpCasts();
3478
Douglas Gregorb7a09262010-04-01 18:32:35 +00003479 bool AddressTaken = false;
3480 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003481 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003482 if (UnOp->getOpcode() == UO_AddrOf) {
John McCall91a57552011-07-15 05:09:51 +00003483 Arg = UnOp->getSubExpr();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003484 AddressTaken = true;
3485 AddrOpLoc = UnOp->getOperatorLoc();
3486 }
Francois Picheta343a412011-04-29 09:08:14 +00003487 }
John McCall91a57552011-07-15 05:09:51 +00003488
Francois Pichet62ec1f22011-09-17 17:15:52 +00003489 if (S.getLangOptions().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) {
John McCall91a57552011-07-15 05:09:51 +00003490 Converted = TemplateArgument(ArgIn);
3491 return false;
3492 }
3493
3494 while (SubstNonTypeTemplateParmExpr *subst =
3495 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3496 Arg = subst->getReplacement()->IgnoreImpCasts();
3497
3498 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003499 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003500 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3501 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003502 S.Diag(Param->getLocation(), diag::note_template_param_here);
3503 return true;
3504 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003505
3506 // Stop checking the precise nature of the argument if it is value dependent,
3507 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003508 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003509 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003510 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003511 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003512
Douglas Gregorb7a09262010-04-01 18:32:35 +00003513 if (!isa<ValueDecl>(DRE->getDecl())) {
3514 S.Diag(Arg->getSourceRange().getBegin(),
3515 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003516 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003517 S.Diag(Param->getLocation(), diag::note_template_param_here);
3518 return true;
3519 }
3520
3521 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003522
3523 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003524 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3525 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003526 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003527 S.Diag(Param->getLocation(), diag::note_template_param_here);
3528 return true;
3529 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003530
3531 // Cannot refer to non-static member functions
3532 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003533 if (!Method->isStatic()) {
3534 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003535 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003536 S.Diag(Param->getLocation(), diag::note_template_param_here);
3537 return true;
3538 }
Mike Stump1eb44332009-09-09 15:08:12 +00003539
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003540 // Functions must have external linkage.
3541 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003542 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003543 S.Diag(Arg->getSourceRange().getBegin(),
3544 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003545 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003546 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003547 << true;
3548 return true;
3549 }
3550
3551 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003552 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003553
Douglas Gregorb7a09262010-04-01 18:32:35 +00003554 // If the template parameter has pointer type, the function decays.
3555 if (ParamType->isPointerType() && !AddressTaken)
3556 ArgType = S.Context.getPointerType(Func->getType());
3557 else if (AddressTaken && ParamType->isReferenceType()) {
3558 // If we originally had an address-of operator, but the
3559 // parameter has reference type, complain and (if things look
3560 // like they will work) drop the address-of operator.
3561 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3562 ParamType.getNonReferenceType())) {
3563 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3564 << ParamType;
3565 S.Diag(Param->getLocation(), diag::note_template_param_here);
3566 return true;
3567 }
3568
3569 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3570 << ParamType
3571 << FixItHint::CreateRemoval(AddrOpLoc);
3572 S.Diag(Param->getLocation(), diag::note_template_param_here);
3573
3574 ArgType = Func->getType();
3575 }
3576 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003577 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003578 S.Diag(Arg->getSourceRange().getBegin(),
3579 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003580 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003581 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003582 << true;
3583 return true;
3584 }
3585
Douglas Gregorb7a09262010-04-01 18:32:35 +00003586 // A value of reference type is not an object.
3587 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003588 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003589 diag::err_template_arg_reference_var)
3590 << Var->getType() << Arg->getSourceRange();
3591 S.Diag(Param->getLocation(), diag::note_template_param_here);
3592 return true;
3593 }
3594
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003595 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003596 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003597
3598 // If the template parameter has pointer type, we must have taken
3599 // the address of this object.
3600 if (ParamType->isReferenceType()) {
3601 if (AddressTaken) {
3602 // If we originally had an address-of operator, but the
3603 // parameter has reference type, complain and (if things look
3604 // like they will work) drop the address-of operator.
3605 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3606 ParamType.getNonReferenceType())) {
3607 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3608 << ParamType;
3609 S.Diag(Param->getLocation(), diag::note_template_param_here);
3610 return true;
3611 }
3612
3613 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3614 << ParamType
3615 << FixItHint::CreateRemoval(AddrOpLoc);
3616 S.Diag(Param->getLocation(), diag::note_template_param_here);
3617
3618 ArgType = Var->getType();
3619 }
3620 } else if (!AddressTaken && ParamType->isPointerType()) {
3621 if (Var->getType()->isArrayType()) {
3622 // Array-to-pointer decay.
3623 ArgType = S.Context.getArrayDecayedType(Var->getType());
3624 } else {
3625 // If the template parameter has pointer type but the address of
3626 // this object was not taken, complain and (possibly) recover by
3627 // taking the address of the entity.
3628 ArgType = S.Context.getPointerType(Var->getType());
3629 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3630 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3631 << ParamType;
3632 S.Diag(Param->getLocation(), diag::note_template_param_here);
3633 return true;
3634 }
3635
3636 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3637 << ParamType
3638 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3639
3640 S.Diag(Param->getLocation(), diag::note_template_param_here);
3641 }
3642 }
3643 } else {
3644 // We found something else, but we don't know specifically what it is.
3645 S.Diag(Arg->getSourceRange().getBegin(),
3646 diag::err_template_arg_not_object_or_func)
3647 << Arg->getSourceRange();
3648 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3649 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003650 }
Mike Stump1eb44332009-09-09 15:08:12 +00003651
John McCallf85e1932011-06-15 23:02:42 +00003652 bool ObjCLifetimeConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003653 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003654 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
John McCallf85e1932011-06-15 23:02:42 +00003655 S.IsQualificationConversion(ArgType, ParamType, false,
3656 ObjCLifetimeConversion)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003657 // For pointer-to-object types, qualification conversions are
3658 // permitted.
3659 } else {
3660 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3661 if (!ParamRef->getPointeeType()->isFunctionType()) {
3662 // C++ [temp.arg.nontype]p5b3:
3663 // For a non-type template-parameter of type reference to
3664 // object, no conversions apply. The type referred to by the
3665 // reference may be more cv-qualified than the (otherwise
3666 // identical) type of the template- argument. The
3667 // template-parameter is bound directly to the
3668 // template-argument, which shall be an lvalue.
3669
3670 // FIXME: Other qualifiers?
3671 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3672 unsigned ArgQuals = ArgType.getCVRQualifiers();
3673
3674 if ((ParamQuals | ArgQuals) != ParamQuals) {
3675 S.Diag(Arg->getSourceRange().getBegin(),
3676 diag::err_template_arg_ref_bind_ignores_quals)
3677 << ParamType << Arg->getType()
3678 << Arg->getSourceRange();
3679 S.Diag(Param->getLocation(), diag::note_template_param_here);
3680 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003681 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003682 }
3683 }
3684
3685 // At this point, the template argument refers to an object or
3686 // function with external linkage. We now need to check whether the
3687 // argument and parameter types are compatible.
3688 if (!S.Context.hasSameUnqualifiedType(ArgType,
3689 ParamType.getNonReferenceType())) {
3690 // We can't perform this conversion or binding.
3691 if (ParamType->isReferenceType())
3692 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
John McCall91a57552011-07-15 05:09:51 +00003693 << ParamType << ArgIn->getType() << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003694 else
3695 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
John McCall91a57552011-07-15 05:09:51 +00003696 << ArgIn->getType() << ParamType << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003697 S.Diag(Param->getLocation(), diag::note_template_param_here);
3698 return true;
3699 }
3700 }
3701
3702 // Create the template argument.
3703 Converted = TemplateArgument(Entity->getCanonicalDecl());
Eli Friedman5f2987c2012-02-02 03:46:19 +00003704 S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003705 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003706}
3707
3708/// \brief Checks whether the given template argument is a pointer to
3709/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003710bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003711 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003712 bool Invalid = false;
3713
3714 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003715 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003716 Arg = Cast->getSubExpr();
3717
3718 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003719 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003720 // A template-argument for a non-type, non-template
3721 // template-parameter shall be one of: [...]
3722 //
3723 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003724 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003725
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003726 // In C++98/03 mode, give an extension warning on any extra parentheses.
3727 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3728 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003729 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003730 if (!Invalid && !ExtraParens) {
Mike Stump1eb44332009-09-09 15:08:12 +00003731 Diag(Arg->getSourceRange().getBegin(),
Richard Smithebaf0e62011-10-18 20:49:44 +00003732 getLangOptions().CPlusPlus0x ?
3733 diag::warn_cxx98_compat_template_arg_extra_parens :
3734 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003735 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003736 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003737 }
3738
3739 Arg = Parens->getSubExpr();
3740 }
3741
John McCall91a57552011-07-15 05:09:51 +00003742 while (SubstNonTypeTemplateParmExpr *subst =
3743 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3744 Arg = subst->getReplacement()->IgnoreImpCasts();
3745
Douglas Gregorcaddba02009-11-12 18:38:13 +00003746 // A pointer-to-member constant written &Class::member.
3747 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003748 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003749 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3750 if (DRE && !DRE->getQualifier())
3751 DRE = 0;
3752 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003753 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003754 // A constant of pointer-to-member type.
3755 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3756 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3757 if (VD->getType()->isMemberPointerType()) {
3758 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003759 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003760 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3761 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003762 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003763 else
3764 Converted = TemplateArgument(VD->getCanonicalDecl());
3765 return Invalid;
3766 }
3767 }
3768 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003769
Douglas Gregorcaddba02009-11-12 18:38:13 +00003770 DRE = 0;
3771 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003772
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003773 if (!DRE)
3774 return Diag(Arg->getSourceRange().getBegin(),
3775 diag::err_template_arg_not_pointer_to_member_form)
3776 << Arg->getSourceRange();
3777
3778 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3779 assert((isa<FieldDecl>(DRE->getDecl()) ||
3780 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3781 "Only non-static member pointers can make it here");
3782
3783 // Okay: this is the address of a non-static member, and therefore
3784 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003785 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003786 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003787 else
3788 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003789 return Invalid;
3790 }
3791
3792 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003793 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003794 diag::err_template_arg_not_pointer_to_member_form)
3795 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003796 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003797 diag::note_template_arg_refers_here);
3798 return true;
3799}
3800
Douglas Gregorc15cb382009-02-09 23:23:08 +00003801/// \brief Check a template argument against its corresponding
3802/// non-type template parameter.
3803///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003804/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003805/// If an error occurred, it returns ExprError(); otherwise, it
3806/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003807/// InstantiatedParamType is the type of the non-type template
3808/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003809ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3810 QualType InstantiatedParamType, Expr *Arg,
3811 TemplateArgument &Converted,
3812 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003813 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3814
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003815 // If either the parameter has a dependent type or the argument is
3816 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003817 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3818 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003819 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003820 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003821 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003822
3823 // C++ [temp.arg.nontype]p5:
3824 // The following conversions are performed on each expression used
3825 // as a non-type template-argument. If a non-type
3826 // template-argument cannot be converted to the type of the
3827 // corresponding template-parameter then the program is
3828 // ill-formed.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003829 QualType ParamType = InstantiatedParamType;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003830 if (ParamType->isIntegralOrEnumerationType()) {
Richard Smith8ef7b202012-01-18 23:55:52 +00003831 // C++11:
3832 // -- for a non-type template-parameter of integral or
3833 // enumeration type, conversions permitted in a converted
3834 // constant expression are applied.
3835 //
3836 // C++98:
3837 // -- for a non-type template-parameter of integral or
3838 // enumeration type, integral promotions (4.5) and integral
3839 // conversions (4.7) are applied.
3840
3841 if (CTAK == CTAK_Deduced &&
3842 !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) {
3843 // C++ [temp.deduct.type]p17:
3844 // If, in the declaration of a function template with a non-type
3845 // template-parameter, the non-type template-parameter is used
3846 // in an expression in the function parameter-list and, if the
3847 // corresponding template-argument is deduced, the
3848 // template-argument type shall match the type of the
3849 // template-parameter exactly, except that a template-argument
3850 // deduced from an array bound may be of any integral type.
3851 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3852 << Arg->getType().getUnqualifiedType()
3853 << ParamType.getUnqualifiedType();
3854 Diag(Param->getLocation(), diag::note_template_param_here);
3855 return ExprError();
3856 }
3857
3858 if (getLangOptions().CPlusPlus0x) {
3859 // We can't check arbitrary value-dependent arguments.
3860 // FIXME: If there's no viable conversion to the template parameter type,
3861 // we should be able to diagnose that prior to instantiation.
3862 if (Arg->isValueDependent()) {
3863 Converted = TemplateArgument(Arg);
3864 return Owned(Arg);
3865 }
3866
3867 // C++ [temp.arg.nontype]p1:
3868 // A template-argument for a non-type, non-template template-parameter
3869 // shall be one of:
3870 //
3871 // -- for a non-type template-parameter of integral or enumeration
3872 // type, a converted constant expression of the type of the
3873 // template-parameter; or
3874 llvm::APSInt Value;
3875 ExprResult ArgResult =
3876 CheckConvertedConstantExpression(Arg, ParamType, Value,
3877 CCEK_TemplateArg);
3878 if (ArgResult.isInvalid())
3879 return ExprError();
3880
3881 // Widen the argument value to sizeof(parameter type). This is almost
3882 // always a no-op, except when the parameter type is bool. In
3883 // that case, this may extend the argument from 1 bit to 8 bits.
3884 QualType IntegerType = ParamType;
3885 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
3886 IntegerType = Enum->getDecl()->getIntegerType();
3887 Value = Value.extOrTrunc(Context.getTypeSize(IntegerType));
3888
3889 Converted = TemplateArgument(Value, Context.getCanonicalType(ParamType));
3890 return ArgResult;
3891 }
3892
Richard Smith4f870622011-10-27 22:11:44 +00003893 ExprResult ArgResult = DefaultLvalueConversion(Arg);
3894 if (ArgResult.isInvalid())
3895 return ExprError();
3896 Arg = ArgResult.take();
3897
3898 QualType ArgType = Arg->getType();
3899
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003900 // C++ [temp.arg.nontype]p1:
3901 // A template-argument for a non-type, non-template
3902 // template-parameter shall be one of:
3903 //
3904 // -- an integral constant-expression of integral or enumeration
3905 // type; or
3906 // -- the name of a non-type template-parameter; or
3907 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003908 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003909 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003910 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003911 diag::err_template_arg_not_integral_or_enumeral)
3912 << ArgType << Arg->getSourceRange();
3913 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003914 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003915 } else if (!Arg->isValueDependent() &&
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003916 !Arg->isIntegerConstantExpr(Value, Context, &NonConstantLoc)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003917 Diag(NonConstantLoc, diag::err_template_arg_not_ice)
3918 << ArgType << Arg->getSourceRange();
John Wiegley429bb272011-04-08 18:41:53 +00003919 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003920 }
3921
Douglas Gregor02024a92010-03-28 02:42:43 +00003922 // From here on out, all we care about are the unqualified forms
3923 // of the parameter and argument types.
3924 ParamType = ParamType.getUnqualifiedType();
3925 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003926
3927 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003928 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003929 // Okay: no conversion necessary
John McCalldaa8e4e2010-11-15 09:13:47 +00003930 } else if (ParamType->isBooleanType()) {
3931 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003932 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003933 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3934 !ParamType->isEnumeralType()) {
3935 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003936 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003937 } else {
3938 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003939 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003940 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003941 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003942 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003943 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003944 }
3945
Douglas Gregorc7469372011-05-04 21:55:00 +00003946 // Add the value of this argument to the list of converted
3947 // arguments. We use the bitwidth and signedness of the template
3948 // parameter.
3949 if (Arg->isValueDependent()) {
3950 // The argument is value-dependent. Create a new
3951 // TemplateArgument with the converted expression.
3952 Converted = TemplateArgument(Arg);
3953 return Owned(Arg);
3954 }
3955
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003956 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003957 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003958 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003959
Douglas Gregorc7469372011-05-04 21:55:00 +00003960 if (ParamType->isBooleanType()) {
3961 // Value must be zero or one.
3962 Value = Value != 0;
3963 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3964 if (Value.getBitWidth() != AllowedBits)
3965 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003966 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003967 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003968 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003969
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003970 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003971 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003972 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003973 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003974 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003975 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003976
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003977 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00003978 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003979 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003980 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3981 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3982 << Arg->getSourceRange();
3983 Diag(Param->getLocation(), diag::note_template_param_here);
3984 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003985
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003986 // Complain if we overflowed the template parameter's type.
3987 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00003988 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003989 RequiredBits = OldValue.getActiveBits();
3990 else if (OldValue.isUnsigned())
3991 RequiredBits = OldValue.getActiveBits() + 1;
3992 else
3993 RequiredBits = OldValue.getMinSignedBits();
3994 if (RequiredBits > AllowedBits) {
3995 Diag(Arg->getSourceRange().getBegin(),
3996 diag::warn_template_arg_too_large)
3997 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3998 << Arg->getSourceRange();
3999 Diag(Param->getLocation(), diag::note_template_param_here);
4000 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00004001 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00004002
John McCall833ca992009-10-29 08:12:44 +00004003 Converted = TemplateArgument(Value,
Douglas Gregor6b63f552011-08-09 01:55:14 +00004004 ParamType->isEnumeralType()
4005 ? Context.getCanonicalType(ParamType)
4006 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00004007 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00004008 }
Douglas Gregora35284b2009-02-11 00:19:33 +00004009
Richard Smith4f870622011-10-27 22:11:44 +00004010 QualType ArgType = Arg->getType();
John McCall6bb80172010-03-30 21:47:33 +00004011 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
4012
Douglas Gregorb7a09262010-04-01 18:32:35 +00004013 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
4014 // from a template argument of type std::nullptr_t to a non-type
4015 // template parameter of type pointer to object, pointer to
4016 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004017 if (ArgType->isNullPtrType()) {
4018 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
4019 Converted = TemplateArgument((NamedDecl *)0);
4020 return Owned(Arg);
4021 }
4022
4023 if (ParamType->isNullPtrType()) {
4024 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
4025 Converted = TemplateArgument(Zero, Context.NullPtrTy);
4026 return Owned(Arg);
4027 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00004028 }
4029
Douglas Gregorb86b0572009-02-11 01:18:59 +00004030 // Handle pointer-to-function, reference-to-function, and
4031 // pointer-to-member-function all in (roughly) the same way.
4032 if (// -- For a non-type template-parameter of type pointer to
4033 // function, only the function-to-pointer conversion (4.3) is
4034 // applied. If the template-argument represents a set of
4035 // overloaded functions (or a pointer to such), the matching
4036 // function is selected from the set (13.4).
4037 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00004038 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00004039 // -- For a non-type template-parameter of type reference to
4040 // function, no conversions apply. If the template-argument
4041 // represents a set of overloaded functions, the matching
4042 // function is selected from the set (13.4).
4043 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00004044 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00004045 // -- For a non-type template-parameter of type pointer to
4046 // member function, no conversions apply. If the
4047 // template-argument represents a set of overloaded member
4048 // functions, the matching member function is selected from
4049 // the set (13.4).
4050 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00004051 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00004052 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004053
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004054 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004055 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004056 true,
4057 FoundResult)) {
4058 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00004059 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004060
4061 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
4062 ArgType = Arg->getType();
4063 } else
John Wiegley429bb272011-04-08 18:41:53 +00004064 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00004065 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004066
John Wiegley429bb272011-04-08 18:41:53 +00004067 if (!ParamType->isMemberPointerType()) {
4068 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4069 ParamType,
4070 Arg, Converted))
4071 return ExprError();
4072 return Owned(Arg);
4073 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00004074
John McCallf85e1932011-06-15 23:02:42 +00004075 bool ObjCLifetimeConversion;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00004076 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
John McCallf85e1932011-06-15 23:02:42 +00004077 false, ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00004078 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
4079 Arg->getValueKind()).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004080 } else if (!Context.hasSameUnqualifiedType(ArgType,
4081 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00004082 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00004083 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00004084 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00004085 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00004086 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00004087 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00004088 }
Mike Stump1eb44332009-09-09 15:08:12 +00004089
John Wiegley429bb272011-04-08 18:41:53 +00004090 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
4091 return ExprError();
4092 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00004093 }
4094
Chris Lattnerfe90de72009-02-20 21:37:53 +00004095 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00004096 // -- for a non-type template-parameter of type pointer to
4097 // object, qualification conversions (4.4) and the
4098 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00004099 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00004100 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00004101 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004102
John Wiegley429bb272011-04-08 18:41:53 +00004103 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4104 ParamType,
4105 Arg, Converted))
4106 return ExprError();
4107 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004108 }
Mike Stump1eb44332009-09-09 15:08:12 +00004109
Ted Kremenek6217b802009-07-29 21:53:49 +00004110 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00004111 // -- For a non-type template-parameter of type reference to
4112 // object, no conversions apply. The type referred to by the
4113 // reference may be more cv-qualified than the (otherwise
4114 // identical) type of the template-argument. The
4115 // template-parameter is bound directly to the
4116 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00004117 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00004118 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004119
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004120 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004121 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
4122 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004123 true,
4124 FoundResult)) {
4125 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00004126 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004127
4128 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
4129 ArgType = Arg->getType();
4130 } else
John Wiegley429bb272011-04-08 18:41:53 +00004131 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00004132 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004133
John Wiegley429bb272011-04-08 18:41:53 +00004134 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4135 ParamType,
4136 Arg, Converted))
4137 return ExprError();
4138 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00004139 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00004140
4141 // -- For a non-type template-parameter of type pointer to data
4142 // member, qualification conversions (4.4) are applied.
4143 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
4144
John McCallf85e1932011-06-15 23:02:42 +00004145 bool ObjCLifetimeConversion;
Douglas Gregor8e6563b2009-02-11 18:22:40 +00004146 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00004147 // Types match exactly: nothing more to do here.
John McCallf85e1932011-06-15 23:02:42 +00004148 } else if (IsQualificationConversion(ArgType, ParamType, false,
4149 ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00004150 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
4151 Arg->getValueKind()).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004152 } else {
4153 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00004154 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00004155 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00004156 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004157 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00004158 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004159 }
4160
John Wiegley429bb272011-04-08 18:41:53 +00004161 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
4162 return ExprError();
4163 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00004164}
4165
4166/// \brief Check a template argument against its corresponding
4167/// template template parameter.
4168///
4169/// This routine implements the semantics of C++ [temp.arg.template].
4170/// It returns true if an error occurred, and false otherwise.
4171bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00004172 const TemplateArgumentLoc &Arg) {
4173 TemplateName Name = Arg.getArgument().getAsTemplate();
4174 TemplateDecl *Template = Name.getAsTemplateDecl();
4175 if (!Template) {
4176 // Any dependent template name is fine.
4177 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
4178 return false;
4179 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00004180
Richard Smith3e4c6c42011-05-05 21:57:07 +00004181 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00004182 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00004183 // the name of a class template or an alias template, expressed as an
4184 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00004185 // primary class templates are considered when matching the
4186 // template template argument with the corresponding parameter;
4187 // partial specializations are not considered even if their
4188 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004189 //
4190 // Note that we also allow template template parameters here, which
4191 // will happen when we are dealing with, e.g., class template
4192 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00004193 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00004194 !isa<TemplateTemplateParmDecl>(Template) &&
4195 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00004196 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00004197 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00004198 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00004199 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00004200 << Template;
4201 }
4202
4203 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
4204 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004205 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004206 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00004207 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00004208}
4209
Douglas Gregor02024a92010-03-28 02:42:43 +00004210/// \brief Given a non-type template argument that refers to a
4211/// declaration and the type of its corresponding non-type template
4212/// parameter, produce an expression that properly refers to that
4213/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004214ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004215Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
4216 QualType ParamType,
4217 SourceLocation Loc) {
4218 assert(Arg.getKind() == TemplateArgument::Declaration &&
4219 "Only declaration template arguments permitted here");
4220 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
4221
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004222 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00004223 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
4224 // If the value is a class member, we might have a pointer-to-member.
4225 // Determine whether the non-type template template parameter is of
4226 // pointer-to-member type. If so, we need to build an appropriate
4227 // expression for a pointer-to-member, since a "normal" DeclRefExpr
4228 // would refer to the member itself.
4229 if (ParamType->isMemberPointerType()) {
4230 QualType ClassType
4231 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
4232 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00004233 = NestedNameSpecifier::Create(Context, 0, false,
4234 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00004235 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00004236 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00004237
4238 // The actual value-ness of this is unimportant, but for
4239 // internal consistency's sake, references to instance methods
4240 // are r-values.
4241 ExprValueKind VK = VK_LValue;
4242 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
4243 VK = VK_RValue;
4244
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004245 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00004246 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00004247 VK,
John McCallf89e55a2010-11-18 06:31:45 +00004248 Loc,
4249 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00004250 if (RefExpr.isInvalid())
4251 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004252
John McCall2de56d12010-08-25 11:45:40 +00004253 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004254
Douglas Gregorc0c83002010-04-30 21:46:38 +00004255 // We might need to perform a trailing qualification conversion, since
4256 // the element type on the parameter could be more qualified than the
4257 // element type in the expression we constructed.
John McCallf85e1932011-06-15 23:02:42 +00004258 bool ObjCLifetimeConversion;
Douglas Gregorc0c83002010-04-30 21:46:38 +00004259 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John McCallf85e1932011-06-15 23:02:42 +00004260 ParamType.getUnqualifiedType(), false,
4261 ObjCLifetimeConversion))
John Wiegley429bb272011-04-08 18:41:53 +00004262 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004263
Douglas Gregor02024a92010-03-28 02:42:43 +00004264 assert(!RefExpr.isInvalid() &&
4265 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004266 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004267 return move(RefExpr);
4268 }
4269 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004270
Douglas Gregor02024a92010-03-28 02:42:43 +00004271 QualType T = VD->getType().getNonReferenceType();
4272 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004273 // When the non-type template parameter is a pointer, take the
4274 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004275 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004276 if (RefExpr.isInvalid())
4277 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004278
4279 if (T->isFunctionType() || T->isArrayType()) {
4280 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004281 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4282 if (RefExpr.isInvalid())
4283 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004284
4285 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004286 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004287
Douglas Gregorb7a09262010-04-01 18:32:35 +00004288 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004289 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004290 }
4291
John McCallf89e55a2010-11-18 06:31:45 +00004292 ExprValueKind VK = VK_RValue;
4293
Douglas Gregor02024a92010-03-28 02:42:43 +00004294 // If the non-type template parameter has reference type, qualify the
4295 // resulting declaration reference with the extra qualifiers on the
4296 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004297 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4298 VK = VK_LValue;
4299 T = Context.getQualifiedType(T,
4300 TargetRef->getPointeeType().getQualifiers());
4301 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004302
John McCallf89e55a2010-11-18 06:31:45 +00004303 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004304}
4305
4306/// \brief Construct a new expression that refers to the given
4307/// integral template argument with the given source-location
4308/// information.
4309///
4310/// This routine takes care of the mapping from an integral template
4311/// argument (which may have any integral type) to the appropriate
4312/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004313ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004314Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4315 SourceLocation Loc) {
4316 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004317 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004318 QualType T = Arg.getIntegralType();
Douglas Gregor5cee1192011-07-27 05:40:30 +00004319 if (T->isAnyCharacterType()) {
4320 CharacterLiteral::CharacterKind Kind;
4321 if (T->isWideCharType())
4322 Kind = CharacterLiteral::Wide;
4323 else if (T->isChar16Type())
4324 Kind = CharacterLiteral::UTF16;
4325 else if (T->isChar32Type())
4326 Kind = CharacterLiteral::UTF32;
4327 else
4328 Kind = CharacterLiteral::Ascii;
4329
Douglas Gregor02024a92010-03-28 02:42:43 +00004330 return Owned(new (Context) CharacterLiteral(
Douglas Gregor5cee1192011-07-27 05:40:30 +00004331 Arg.getAsIntegral()->getZExtValue(),
4332 Kind, T, Loc));
4333 }
4334
Douglas Gregor02024a92010-03-28 02:42:43 +00004335 if (T->isBooleanType())
4336 return Owned(new (Context) CXXBoolLiteralExpr(
4337 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004338 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004339
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004340 if (T->isNullPtrType())
4341 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4342
Chris Lattner223de242011-04-25 20:37:58 +00004343 // If this is an enum type that we're instantiating, we need to use an integer
4344 // type the same size as the enumerator. We don't want to build an
4345 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004346 QualType BT;
4347 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004348 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004349 else
4350 BT = T;
4351
John McCall4e9272d2011-07-15 07:47:58 +00004352 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
4353 if (T->isEnumeralType()) {
4354 // FIXME: This is a hack. We need a better way to handle substituted
4355 // non-type template parameters.
4356 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4357 Context.getTrivialTypeSourceInfo(T, Loc),
4358 Loc, Loc);
4359 }
4360
4361 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004362}
4363
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004364/// \brief Match two template parameters within template parameter lists.
4365static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4366 bool Complain,
4367 Sema::TemplateParameterListEqualKind Kind,
4368 SourceLocation TemplateArgLoc) {
4369 // Check the actual kind (type, non-type, template).
4370 if (Old->getKind() != New->getKind()) {
4371 if (Complain) {
4372 unsigned NextDiag = diag::err_template_param_different_kind;
4373 if (TemplateArgLoc.isValid()) {
4374 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4375 NextDiag = diag::note_template_param_different_kind;
4376 }
4377 S.Diag(New->getLocation(), NextDiag)
4378 << (Kind != Sema::TPL_TemplateMatch);
4379 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4380 << (Kind != Sema::TPL_TemplateMatch);
4381 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004382
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004383 return false;
4384 }
4385
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004386 // Check that both are parameter packs are neither are parameter packs.
4387 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004388 // template template parameter, the template template parameter can have
4389 // a parameter pack where the template template argument does not.
4390 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4391 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4392 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004393 if (Complain) {
4394 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4395 if (TemplateArgLoc.isValid()) {
4396 S.Diag(TemplateArgLoc,
4397 diag::err_template_arg_template_params_mismatch);
4398 NextDiag = diag::note_template_parameter_pack_non_pack;
4399 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004400
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004401 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4402 : isa<NonTypeTemplateParmDecl>(New)? 1
4403 : 2;
4404 S.Diag(New->getLocation(), NextDiag)
4405 << ParamKind << New->isParameterPack();
4406 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4407 << ParamKind << Old->isParameterPack();
4408 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004409
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004410 return false;
4411 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004412
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004413 // For non-type template parameters, check the type of the parameter.
4414 if (NonTypeTemplateParmDecl *OldNTTP
4415 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4416 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004417
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004418 // If we are matching a template template argument to a template
4419 // template parameter and one of the non-type template parameter types
4420 // is dependent, then we must wait until template instantiation time
4421 // to actually compare the arguments.
4422 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4423 (OldNTTP->getType()->isDependentType() ||
4424 NewNTTP->getType()->isDependentType()))
4425 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004426
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004427 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4428 if (Complain) {
4429 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4430 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004431 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004432 diag::err_template_arg_template_params_mismatch);
4433 NextDiag = diag::note_template_nontype_parm_different_type;
4434 }
4435 S.Diag(NewNTTP->getLocation(), NextDiag)
4436 << NewNTTP->getType()
4437 << (Kind != Sema::TPL_TemplateMatch);
4438 S.Diag(OldNTTP->getLocation(),
4439 diag::note_template_nontype_parm_prev_declaration)
4440 << OldNTTP->getType();
4441 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004442
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004443 return false;
4444 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004445
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004446 return true;
4447 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004448
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004449 // For template template parameters, check the template parameter types.
4450 // The template parameter lists of template template
4451 // parameters must agree.
4452 if (TemplateTemplateParmDecl *OldTTP
4453 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004454 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004455 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4456 OldTTP->getTemplateParameters(),
4457 Complain,
4458 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004459 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004460 : Kind),
4461 TemplateArgLoc);
4462 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004463
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004464 return true;
4465}
Douglas Gregor02024a92010-03-28 02:42:43 +00004466
Douglas Gregora0347822011-01-13 00:08:50 +00004467/// \brief Diagnose a known arity mismatch when comparing template argument
4468/// lists.
4469static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004470void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004471 TemplateParameterList *New,
4472 TemplateParameterList *Old,
4473 Sema::TemplateParameterListEqualKind Kind,
4474 SourceLocation TemplateArgLoc) {
4475 unsigned NextDiag = diag::err_template_param_list_different_arity;
4476 if (TemplateArgLoc.isValid()) {
4477 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4478 NextDiag = diag::note_template_param_list_different_arity;
4479 }
4480 S.Diag(New->getTemplateLoc(), NextDiag)
4481 << (New->size() > Old->size())
4482 << (Kind != Sema::TPL_TemplateMatch)
4483 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4484 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4485 << (Kind != Sema::TPL_TemplateMatch)
4486 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4487}
4488
Douglas Gregorddc29e12009-02-06 22:42:48 +00004489/// \brief Determine whether the given template parameter lists are
4490/// equivalent.
4491///
Mike Stump1eb44332009-09-09 15:08:12 +00004492/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004493/// source code as part of a new template declaration.
4494///
4495/// \param Old The old template parameter list, typically found via
4496/// name lookup of the template declared with this template parameter
4497/// list.
4498///
4499/// \param Complain If true, this routine will produce a diagnostic if
4500/// the template parameter lists are not equivalent.
4501///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004502/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004503///
4504/// \param TemplateArgLoc If this source location is valid, then we
4505/// are actually checking the template parameter list of a template
4506/// argument (New) against the template parameter list of its
4507/// corresponding template template parameter (Old). We produce
4508/// slightly different diagnostics in this scenario.
4509///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004510/// \returns True if the template parameter lists are equal, false
4511/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004512bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004513Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4514 TemplateParameterList *Old,
4515 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004516 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004517 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004518 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4519 if (Complain)
4520 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4521 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004522
4523 return false;
4524 }
4525
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004526 // C++0x [temp.arg.template]p3:
4527 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004528 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004529 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004530 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004531 // template-parameter-list of P. [...]
4532 TemplateParameterList::iterator NewParm = New->begin();
4533 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004534 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004535 OldParmEnd = Old->end();
4536 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004537 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4538 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004539 if (NewParm == NewParmEnd) {
4540 if (Complain)
4541 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4542 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004543
Douglas Gregora0347822011-01-13 00:08:50 +00004544 return false;
4545 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004546
Douglas Gregora0347822011-01-13 00:08:50 +00004547 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4548 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004549 return false;
4550
Douglas Gregora0347822011-01-13 00:08:50 +00004551 ++NewParm;
4552 continue;
4553 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004554
Douglas Gregora0347822011-01-13 00:08:50 +00004555 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004556 // [...] When P's template- parameter-list contains a template parameter
4557 // pack (14.5.3), the template parameter pack will match zero or more
4558 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004559 // template-parameter-list of A with the same type and form as the
4560 // template parameter pack in P (ignoring whether those template
4561 // parameters are template parameter packs).
4562 for (; NewParm != NewParmEnd; ++NewParm) {
4563 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4564 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004565 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004566 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004567 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004568
Douglas Gregora0347822011-01-13 00:08:50 +00004569 // Make sure we exhausted all of the arguments.
4570 if (NewParm != NewParmEnd) {
4571 if (Complain)
4572 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4573 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004574
Douglas Gregora0347822011-01-13 00:08:50 +00004575 return false;
4576 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004577
Douglas Gregorddc29e12009-02-06 22:42:48 +00004578 return true;
4579}
4580
4581/// \brief Check whether a template can be declared within this scope.
4582///
4583/// If the template declaration is valid in this scope, returns
4584/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004585bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004586Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorfb35e8f2011-11-03 16:37:14 +00004587 if (!S)
4588 return false;
4589
Douglas Gregorddc29e12009-02-06 22:42:48 +00004590 // Find the nearest enclosing declaration scope.
4591 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4592 (S->getFlags() & Scope::TemplateParamScope) != 0)
4593 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004594
Douglas Gregorddc29e12009-02-06 22:42:48 +00004595 // C++ [temp]p2:
4596 // A template-declaration can appear only as a namespace scope or
4597 // class scope declaration.
4598 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004599 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4600 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004601 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004602 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004603
Eli Friedman1503f772009-07-31 01:43:05 +00004604 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004605 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004606
4607 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4608 return false;
4609
Mike Stump1eb44332009-09-09 15:08:12 +00004610 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004611 diag::err_template_outside_namespace_or_class_scope)
4612 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004613}
Douglas Gregorcc636682009-02-17 23:15:12 +00004614
Douglas Gregord5cb8762009-10-07 00:13:32 +00004615/// \brief Determine what kind of template specialization the given declaration
4616/// is.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00004617static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004618 if (!D)
4619 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004620
Douglas Gregorf6b11852009-10-08 15:14:33 +00004621 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4622 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004623 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4624 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004625 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4626 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004627
Douglas Gregord5cb8762009-10-07 00:13:32 +00004628 return TSK_Undeclared;
4629}
4630
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004631/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004632/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004633///
Douglas Gregor9302da62009-10-14 23:50:59 +00004634/// This routine determines whether a template specialization can be declared
4635/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004636///
4637/// \param S the semantic analysis object for which this check is being
4638/// performed.
4639///
4640/// \param Specialized the entity being specialized or instantiated, which
4641/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004642/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004643/// member class).
4644///
4645/// \param PrevDecl the previous declaration of this entity, if any.
4646///
4647/// \param Loc the location of the explicit specialization or instantiation of
4648/// this entity.
4649///
4650/// \param IsPartialSpecialization whether this is a partial specialization of
4651/// a class template.
4652///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004653/// \returns true if there was an error that we cannot recover from, false
4654/// otherwise.
4655static bool CheckTemplateSpecializationScope(Sema &S,
4656 NamedDecl *Specialized,
4657 NamedDecl *PrevDecl,
4658 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004659 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004660 // Keep these "kind" numbers in sync with the %select statements in the
4661 // various diagnostics emitted by this routine.
4662 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004663 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004664 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004665 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004666 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004667 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004668 EntityKind = 3;
4669 else if (isa<VarDecl>(Specialized))
4670 EntityKind = 4;
4671 else if (isa<RecordDecl>(Specialized))
4672 EntityKind = 5;
4673 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004674 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4675 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004676 return true;
4677 }
4678
Douglas Gregor88b70942009-02-25 22:02:03 +00004679 // C++ [temp.expl.spec]p2:
4680 // An explicit specialization shall be declared in the namespace
4681 // of which the template is a member, or, for member templates, in
4682 // the namespace of which the enclosing class or enclosing class
4683 // template is a member. An explicit specialization of a member
4684 // function, member class or static data member of a class
4685 // template shall be declared in the namespace of which the class
4686 // template is a member. Such a declaration may also be a
4687 // definition. If the declaration is not a definition, the
4688 // specialization may be defined later in the name- space in which
4689 // the explicit specialization was declared, or in a namespace
4690 // that encloses the one in which the explicit specialization was
4691 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004692 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004693 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004694 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004695 return true;
4696 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004697
Douglas Gregor0a407472009-10-07 17:30:37 +00004698 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004699 if (S.getLangOptions().MicrosoftExt) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00004700 // Do not warn for class scope explicit specialization during
4701 // instantiation, warning was already emitted during pattern
4702 // semantic analysis.
4703 if (!S.ActiveTemplateInstantiations.size())
4704 S.Diag(Loc, diag::ext_function_specialization_in_class)
4705 << Specialized;
4706 } else {
4707 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4708 << Specialized;
4709 return true;
4710 }
Douglas Gregor0a407472009-10-07 17:30:37 +00004711 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004712
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004713 if (S.CurContext->isRecord() &&
4714 !S.CurContext->Equals(Specialized->getDeclContext())) {
4715 // Make sure that we're specializing in the right record context.
4716 // Otherwise, things can go horribly wrong.
4717 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4718 << Specialized;
4719 return true;
4720 }
4721
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004722 // C++ [temp.class.spec]p6:
4723 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004724 // in any namespace scope in which its definition may be defined (14.5.1
4725 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004726 bool ComplainedAboutScope = false;
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004727 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004728 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004729 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004730 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004731 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4732 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004733 // C++ [temp.exp.spec]p2:
4734 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004735 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004736 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004737 // An explicit specialization of a member function, member class or
4738 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004739 // namespace of which the class template is a member.
4740 //
4741 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004742 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004743 // the specialized template.
Richard Smithebaf0e62011-10-18 20:49:44 +00004744 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) {
4745 bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext);
4746 if (isa<TranslationUnitDecl>(SpecializedContext)) {
4747 assert(!IsCPlusPlus0xExtension &&
4748 "DC encloses TU but isn't in enclosing namespace set");
4749 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global)
Douglas Gregora4d5de52010-09-12 05:24:55 +00004750 << EntityKind << Specialized;
Richard Smithebaf0e62011-10-18 20:49:44 +00004751 } else if (isa<NamespaceDecl>(SpecializedContext)) {
4752 int Diag;
4753 if (!IsCPlusPlus0xExtension)
4754 Diag = diag::err_template_spec_decl_out_of_scope;
4755 else if (!S.getLangOptions().CPlusPlus0x)
4756 Diag = diag::ext_template_spec_decl_out_of_scope;
4757 else
4758 Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope;
4759 S.Diag(Loc, Diag)
4760 << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext);
4761 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004762
Douglas Gregor9302da62009-10-14 23:50:59 +00004763 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Richard Smithebaf0e62011-10-18 20:49:44 +00004764 ComplainedAboutScope =
4765 !(IsCPlusPlus0xExtension && S.getLangOptions().CPlusPlus0x);
Douglas Gregor88b70942009-02-25 22:02:03 +00004766 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004767 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004768
4769 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004770 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004771 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004772 // specializations of function templates, static data members, and member
4773 // functions, so we skip the check here for those kinds of entities.
4774 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004775 // Should we refactor that check, so that it occurs later?
4776 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004777 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4778 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004779 if (isa<TranslationUnitDecl>(SpecializedContext))
4780 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4781 << EntityKind << Specialized;
4782 else if (isa<NamespaceDecl>(SpecializedContext))
4783 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4784 << EntityKind << Specialized
4785 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004786
Douglas Gregor9302da62009-10-14 23:50:59 +00004787 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004788 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004789
Douglas Gregord5cb8762009-10-07 00:13:32 +00004790 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004791
Douglas Gregor88b70942009-02-25 22:02:03 +00004792 return false;
4793}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004794
Douglas Gregorbacb9492011-01-03 21:13:47 +00004795/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4796/// that checks non-type template partial specialization arguments.
4797static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4798 NonTypeTemplateParmDecl *Param,
4799 const TemplateArgument *Args,
4800 unsigned NumArgs) {
4801 for (unsigned I = 0; I != NumArgs; ++I) {
4802 if (Args[I].getKind() == TemplateArgument::Pack) {
4803 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004804 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004805 Args[I].pack_size()))
4806 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004807
Douglas Gregore94866f2009-06-12 21:21:02 +00004808 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004809 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004810
Douglas Gregorbacb9492011-01-03 21:13:47 +00004811 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004812 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004813 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004814 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004815
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004816 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004817 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4818 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004819
4820 // Strip off any implicit casts we added as part of type checking.
4821 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4822 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004823
Douglas Gregore94866f2009-06-12 21:21:02 +00004824 // C++ [temp.class.spec]p8:
4825 // A non-type argument is non-specialized if it is the name of a
4826 // non-type parameter. All other non-type arguments are
4827 // specialized.
4828 //
4829 // Below, we check the two conditions that only apply to
4830 // specialized non-type arguments, so skip any non-specialized
4831 // arguments.
4832 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004833 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004834 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004835
Douglas Gregore94866f2009-06-12 21:21:02 +00004836 // C++ [temp.class.spec]p9:
4837 // Within the argument list of a class template partial
4838 // specialization, the following restrictions apply:
4839 // -- A partially specialized non-type argument expression
4840 // shall not involve a template parameter of the partial
4841 // specialization except when the argument expression is a
4842 // simple identifier.
4843 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004844 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004845 diag::err_dependent_non_type_arg_in_partial_spec)
4846 << ArgExpr->getSourceRange();
4847 return true;
4848 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004849
Douglas Gregore94866f2009-06-12 21:21:02 +00004850 // -- The type of a template parameter corresponding to a
4851 // specialized non-type argument shall not be dependent on a
4852 // parameter of the specialization.
4853 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004854 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004855 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4856 << Param->getType()
4857 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004858 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004859 return true;
4860 }
4861 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004862
Douglas Gregorbacb9492011-01-03 21:13:47 +00004863 return false;
4864}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004865
Douglas Gregorbacb9492011-01-03 21:13:47 +00004866/// \brief Check the non-type template arguments of a class template
4867/// partial specialization according to C++ [temp.class.spec]p9.
4868///
4869/// \param TemplateParams the template parameters of the primary class
4870/// template.
4871///
4872/// \param TemplateArg the template arguments of the class template
4873/// partial specialization.
4874///
4875/// \returns true if there was an error, false otherwise.
4876static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4877 TemplateParameterList *TemplateParams,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004878 SmallVectorImpl<TemplateArgument> &TemplateArgs) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004879 const TemplateArgument *ArgList = TemplateArgs.data();
4880
4881 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4882 NonTypeTemplateParmDecl *Param
4883 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4884 if (!Param)
4885 continue;
4886
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004887 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004888 &ArgList[I], 1))
4889 return true;
4890 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004891
4892 return false;
4893}
4894
John McCalld226f652010-08-21 09:40:31 +00004895DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004896Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4897 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004898 SourceLocation KWLoc,
Douglas Gregord023aec2011-09-09 20:53:38 +00004899 SourceLocation ModulePrivateLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004900 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004901 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004902 SourceLocation TemplateNameLoc,
4903 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004904 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004905 SourceLocation RAngleLoc,
4906 AttributeList *Attr,
4907 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004908 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004909
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004910 // NOTE: KWLoc is the location of the tag keyword. This will instead
4911 // store the location of the outermost template keyword in the declaration.
4912 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4913 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4914
Douglas Gregorcc636682009-02-17 23:15:12 +00004915 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004916 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004917 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004918 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4919
4920 if (!ClassTemplate) {
4921 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004922 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004923 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4924 return true;
4925 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004926
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004927 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004928 bool isPartialSpecialization = false;
4929
Douglas Gregor88b70942009-02-25 22:02:03 +00004930 // Check the validity of the template headers that introduce this
4931 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004932 // FIXME: We probably shouldn't complain about these headers for
4933 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004934 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004935 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004936 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4937 TemplateNameLoc,
4938 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004939 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004940 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004941 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004942 isExplicitSpecialization,
4943 Invalid);
4944 if (Invalid)
4945 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004946
Douglas Gregor05396e22009-08-25 17:23:04 +00004947 if (TemplateParams && TemplateParams->size() > 0) {
4948 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004949
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004950 if (TUK == TUK_Friend) {
4951 Diag(KWLoc, diag::err_partial_specialization_friend)
4952 << SourceRange(LAngleLoc, RAngleLoc);
4953 return true;
4954 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004955
Douglas Gregor05396e22009-08-25 17:23:04 +00004956 // C++ [temp.class.spec]p10:
4957 // The template parameter list of a specialization shall not
4958 // contain default template argument values.
4959 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4960 Decl *Param = TemplateParams->getParam(I);
4961 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4962 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004963 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004964 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004965 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004966 }
4967 } else if (NonTypeTemplateParmDecl *NTTP
4968 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4969 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004970 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004971 diag::err_default_arg_in_partial_spec)
4972 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004973 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004974 }
4975 } else {
4976 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004977 if (TTP->hasDefaultArgument()) {
4978 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004979 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004980 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004981 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004982 }
4983 }
4984 }
Douglas Gregora735b202009-10-13 14:39:41 +00004985 } else if (TemplateParams) {
4986 if (TUK == TUK_Friend)
4987 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004988 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00004989 SourceRange(TemplateParams->getTemplateLoc(),
4990 TemplateParams->getRAngleLoc()))
4991 << SourceRange(LAngleLoc, RAngleLoc);
4992 else
4993 isExplicitSpecialization = true;
4994 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00004995 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00004996 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004997 isExplicitSpecialization = true;
4998 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004999
Douglas Gregorcc636682009-02-17 23:15:12 +00005000 // Check that the specialization uses the same tag kind as the
5001 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005002 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5003 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005004 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00005005 Kind, TUK == TUK_Definition, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005006 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005007 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00005008 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005009 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00005010 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005011 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005012 diag::note_previous_use);
5013 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5014 }
5015
Douglas Gregor40808ce2009-03-09 23:48:35 +00005016 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005017 TemplateArgumentListInfo TemplateArgs;
5018 TemplateArgs.setLAngleLoc(LAngleLoc);
5019 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005020 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00005021
Douglas Gregor925910d2011-01-03 20:35:03 +00005022 // Check for unexpanded parameter packs in any of the template arguments.
5023 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005024 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00005025 UPPC_PartialSpecialization))
5026 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005027
Douglas Gregorcc636682009-02-17 23:15:12 +00005028 // Check that the template argument list is well-formed for this
5029 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005030 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005031 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5032 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00005033 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005034
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005035 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00005036 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00005037 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00005038 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00005039 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00005040 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00005041 return true;
5042
Douglas Gregor561f8122011-07-01 01:22:09 +00005043 bool InstantiationDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005044 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00005045 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005046 TemplateArgs.getArgumentArray(),
Douglas Gregor561f8122011-07-01 01:22:09 +00005047 TemplateArgs.size(),
5048 InstantiationDependent)) {
Douglas Gregorde090962010-02-09 00:37:32 +00005049 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
5050 << ClassTemplate->getDeclName();
5051 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00005052 }
5053 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005054
Douglas Gregorcc636682009-02-17 23:15:12 +00005055 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005056 ClassTemplateSpecializationDecl *PrevDecl = 0;
5057
5058 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005059 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005060 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005061 = ClassTemplate->findPartialSpecialization(Converted.data(),
5062 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005063 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005064 else
5065 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005066 = ClassTemplate->findSpecialization(Converted.data(),
5067 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00005068
5069 ClassTemplateSpecializationDecl *Specialization = 0;
5070
Douglas Gregor88b70942009-02-25 22:02:03 +00005071 // Check whether we can declare a class template specialization in
5072 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005073 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005074 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
5075 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00005076 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00005077 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005078
Douglas Gregorb88e8882009-07-30 17:40:51 +00005079 // The canonical type
5080 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005081 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005082 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00005083 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005084 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005085 // arguments was referenced but not declared, or we're only
5086 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005087 // declaration node as our own, updating its source location and
5088 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005089 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005090 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005091 if (TemplateParameterLists.size() > 0) {
5092 Specialization->setTemplateParameterListsInfo(Context,
5093 TemplateParameterLists.size(),
5094 (TemplateParameterList**) TemplateParameterLists.release());
5095 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005096 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00005097 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005098 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00005099 // Build the canonical type that describes the converted template
5100 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00005101 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
5102 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00005103 Converted.data(),
5104 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005105
5106 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00005107 ClassTemplate->getInjectedClassNameSpecialization())) {
5108 // C++ [temp.class.spec]p9b3:
5109 //
5110 // -- The argument list of the specialization shall not be identical
5111 // to the implicit argument list of the primary template.
5112 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
Douglas Gregor8d267c52011-09-09 02:06:17 +00005113 << (TUK == TUK_Definition)
5114 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
Douglas Gregorb9c66312010-12-23 17:13:55 +00005115 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
5116 ClassTemplate->getIdentifier(),
5117 TemplateNameLoc,
5118 Attr,
5119 TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +00005120 AS_none, /*ModulePrivateLoc=*/SourceLocation(),
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005121 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00005122 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00005123 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00005124
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005125 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005126 ClassTemplatePartialSpecializationDecl *PrevPartial
5127 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005128 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005129 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00005130 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00005131 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005132 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005133 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00005134 TemplateParams,
5135 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005136 Converted.data(),
5137 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00005138 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00005139 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005140 PrevPartial,
5141 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00005142 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005143 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005144 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005145 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00005146 (TemplateParameterList**) TemplateParameterLists.release());
5147 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005148
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005149 if (!PrevPartial)
5150 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005151 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00005152
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005153 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00005154 // template specialization, make a note of that.
5155 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
5156 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005157
Douglas Gregor031a5882009-06-13 00:26:55 +00005158 // Check that all of the template parameters of the class template
5159 // partial specialization are deducible from the template
5160 // arguments. If not, this class template partial specialization
5161 // will never be used.
Benjamin Kramer013b3662012-01-30 16:17:39 +00005162 llvm::SmallBitVector DeducibleParams(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005163 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00005164 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00005165 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00005166
Benjamin Kramer013b3662012-01-30 16:17:39 +00005167 if (!DeducibleParams.all()) {
5168 unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count();
Douglas Gregor031a5882009-06-13 00:26:55 +00005169 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
5170 << (NumNonDeducible > 1)
5171 << SourceRange(TemplateNameLoc, RAngleLoc);
5172 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
5173 if (!DeducibleParams[I]) {
5174 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
5175 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00005176 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005177 diag::note_partial_spec_unused_parameter)
5178 << Param->getDeclName();
5179 else
Mike Stump1eb44332009-09-09 15:08:12 +00005180 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005181 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00005182 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00005183 }
5184 }
5185 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005186 } else {
5187 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005188 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005189 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005190 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00005191 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005192 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005193 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005194 Converted.data(),
5195 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005196 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005197 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005198 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005199 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005200 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00005201 (TemplateParameterList**) TemplateParameterLists.release());
5202 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005203
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005204 if (!PrevDecl)
5205 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00005206
5207 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005208 }
5209
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005210 // C++ [temp.expl.spec]p6:
5211 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005212 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005213 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005214 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005215 // use occurs; no diagnostic is required.
5216 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005217 bool Okay = false;
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005218 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005219 // Is there any previous explicit specialization declaration?
5220 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5221 Okay = true;
5222 break;
5223 }
5224 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005225
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005226 if (!Okay) {
5227 SourceRange Range(TemplateNameLoc, RAngleLoc);
5228 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
5229 << Context.getTypeDeclType(Specialization) << Range;
5230
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005231 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005232 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005233 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005234 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005235 return true;
5236 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005237 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005238
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005239 // If this is not a friend, note that this is an explicit specialization.
5240 if (TUK != TUK_Friend)
5241 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005242
5243 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005244 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00005245 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005246 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005247 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005248 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00005249 Diag(Def->getLocation(), diag::note_previous_definition);
5250 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00005251 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005252 }
5253 }
5254
John McCall7f1b9872010-12-18 03:30:47 +00005255 if (Attr)
5256 ProcessDeclAttributeList(S, Specialization, Attr);
5257
Douglas Gregord023aec2011-09-09 20:53:38 +00005258 if (ModulePrivateLoc.isValid())
5259 Diag(Specialization->getLocation(), diag::err_module_private_specialization)
5260 << (isPartialSpecialization? 1 : 0)
5261 << FixItHint::CreateRemoval(ModulePrivateLoc);
5262
Douglas Gregorfc705b82009-02-26 22:19:44 +00005263 // Build the fully-sugared type for this class template
5264 // specialization as the user wrote in the specialization
5265 // itself. This means that we'll pretty-print the type retrieved
5266 // from the specialization's declaration the way that the user
5267 // actually wrote the specialization, rather than formatting the
5268 // name based on the "canonical" representation used to store the
5269 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005270 TypeSourceInfo *WrittenTy
5271 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5272 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005273 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005274 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005275 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005276 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00005277 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00005278
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005279 // C++ [temp.expl.spec]p9:
5280 // A template explicit specialization is in the scope of the
5281 // namespace in which the template was defined.
5282 //
5283 // We actually implement this paragraph where we set the semantic
5284 // context (in the creation of the ClassTemplateSpecializationDecl),
5285 // but we also maintain the lexical context where the actual
5286 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005287 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005288
Douglas Gregorcc636682009-02-17 23:15:12 +00005289 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005290 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005291 Specialization->startDefinition();
5292
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005293 if (TUK == TUK_Friend) {
5294 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5295 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005296 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005297 /*FIXME:*/KWLoc);
5298 Friend->setAccess(AS_public);
5299 CurContext->addDecl(Friend);
5300 } else {
5301 // Add the specialization into its lexical context, so that it can
5302 // be seen when iterating through the list of declarations in that
5303 // context. However, specializations are not found by name lookup.
5304 CurContext->addDecl(Specialization);
5305 }
John McCalld226f652010-08-21 09:40:31 +00005306 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005307}
Douglas Gregord57959a2009-03-27 23:10:48 +00005308
John McCalld226f652010-08-21 09:40:31 +00005309Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005310 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005311 Declarator &D) {
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005312 return HandleDeclarator(S, D, move(TemplateParameterLists));
Douglas Gregore542c862009-06-23 23:11:28 +00005313}
5314
John McCalld226f652010-08-21 09:40:31 +00005315Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005316 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005317 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005318 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005319 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005320
Douglas Gregor52591bf2009-06-24 00:54:41 +00005321 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005322 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005323 }
Mike Stump1eb44332009-09-09 15:08:12 +00005324
Douglas Gregor52591bf2009-06-24 00:54:41 +00005325 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005326
Douglas Gregor45fa5602011-11-07 20:56:01 +00005327 D.setFunctionDefinitionKind(FDK_Definition);
John McCalld226f652010-08-21 09:40:31 +00005328 Decl *DP = HandleDeclarator(ParentScope, D,
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005329 move(TemplateParameterLists));
Mike Stump1eb44332009-09-09 15:08:12 +00005330 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005331 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005332 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005333 FunctionTemplate->getTemplatedDecl());
5334 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5335 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5336 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005337}
5338
John McCall75042392010-02-11 01:33:53 +00005339/// \brief Strips various properties off an implicit instantiation
5340/// that has just been explicitly specialized.
5341static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00005342 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005343
5344 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5345 FD->setInlineSpecified(false);
5346 }
5347}
5348
Nico Weberd1d512a2012-01-09 19:52:25 +00005349/// \brief Compute the diagnostic location for an explicit instantiation
5350// declaration or definition.
5351static SourceLocation DiagLocForExplicitInstantiation(
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005352 NamedDecl* D, SourceLocation PointOfInstantiation) {
Nico Weberd1d512a2012-01-09 19:52:25 +00005353 // Explicit instantiations following a specialization have no effect and
5354 // hence no PointOfInstantiation. In that case, walk decl backwards
5355 // until a valid name loc is found.
5356 SourceLocation PrevDiagLoc = PointOfInstantiation;
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005357 for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid();
5358 Prev = Prev->getPreviousDecl()) {
Nico Weberd1d512a2012-01-09 19:52:25 +00005359 PrevDiagLoc = Prev->getLocation();
5360 }
5361 assert(PrevDiagLoc.isValid() &&
5362 "Explicit instantiation without point of instantiation?");
5363 return PrevDiagLoc;
5364}
5365
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005366/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005367/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005368/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005369/// new specialization/instantiation will have any effect.
5370///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005371/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005372/// instantiation.
5373///
5374/// \param NewTSK the kind of the new explicit specialization or instantiation.
5375///
5376/// \param PrevDecl the previous declaration of the entity.
5377///
5378/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5379///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005380/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005381/// declaration was instantiated (either implicitly or explicitly).
5382///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005383/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005384/// specialization or instantiation has no effect and should be ignored.
5385///
5386/// \returns true if there was an error that should prevent the introduction of
5387/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005388bool
5389Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5390 TemplateSpecializationKind NewTSK,
5391 NamedDecl *PrevDecl,
5392 TemplateSpecializationKind PrevTSK,
5393 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005394 bool &HasNoEffect) {
5395 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005396
Douglas Gregor454885e2009-10-15 15:54:05 +00005397 switch (NewTSK) {
5398 case TSK_Undeclared:
5399 case TSK_ImplicitInstantiation:
David Blaikieb219cfc2011-09-23 05:06:16 +00005400 llvm_unreachable("Don't check implicit instantiations here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005401
Douglas Gregor454885e2009-10-15 15:54:05 +00005402 case TSK_ExplicitSpecialization:
5403 switch (PrevTSK) {
5404 case TSK_Undeclared:
5405 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005406 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005407 // explicitly specialized or has merely been mentioned without any
5408 // instantiation.
5409 return false;
5410
5411 case TSK_ImplicitInstantiation:
5412 if (PrevPointOfInstantiation.isInvalid()) {
5413 // The declaration itself has not actually been instantiated, so it is
5414 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005415 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005416 return false;
5417 }
5418 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005419
Douglas Gregor454885e2009-10-15 15:54:05 +00005420 case TSK_ExplicitInstantiationDeclaration:
5421 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005422 assert((PrevTSK == TSK_ImplicitInstantiation ||
5423 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005424 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005425
Douglas Gregor454885e2009-10-15 15:54:05 +00005426 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005427 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005428 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005429 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005430 // implicit instantiation to take place, in every translation unit in
5431 // which such a use occurs; no diagnostic is required.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005432 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005433 // Is there any previous explicit specialization declaration?
5434 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5435 return false;
5436 }
5437
Douglas Gregor0d035142009-10-27 18:42:08 +00005438 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005439 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005440 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005441 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005442
Douglas Gregor454885e2009-10-15 15:54:05 +00005443 return true;
5444 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005445
Douglas Gregor454885e2009-10-15 15:54:05 +00005446 case TSK_ExplicitInstantiationDeclaration:
5447 switch (PrevTSK) {
5448 case TSK_ExplicitInstantiationDeclaration:
5449 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005450 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005451 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005452
Douglas Gregor454885e2009-10-15 15:54:05 +00005453 case TSK_Undeclared:
5454 case TSK_ImplicitInstantiation:
5455 // We're explicitly instantiating something that may have already been
5456 // implicitly instantiated; that's fine.
5457 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005458
Douglas Gregor454885e2009-10-15 15:54:05 +00005459 case TSK_ExplicitSpecialization:
5460 // C++0x [temp.explicit]p4:
5461 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005462 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005463 // specialization for that template, the explicit instantiation has no
5464 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005465 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005466 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005467
Douglas Gregor454885e2009-10-15 15:54:05 +00005468 case TSK_ExplicitInstantiationDefinition:
5469 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005470 // If an entity is the subject of both an explicit instantiation
5471 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005472 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005473 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005474 diag::err_explicit_instantiation_declaration_after_definition);
Nico Weberff91d242011-12-23 20:58:04 +00005475
5476 // Explicit instantiations following a specialization have no effect and
5477 // hence no PrevPointOfInstantiation. In that case, walk decl backwards
5478 // until a valid name loc is found.
Nico Weberd1d512a2012-01-09 19:52:25 +00005479 Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
5480 diag::note_explicit_instantiation_definition_here);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005481 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005482 return false;
5483 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005484
Douglas Gregor454885e2009-10-15 15:54:05 +00005485 case TSK_ExplicitInstantiationDefinition:
5486 switch (PrevTSK) {
5487 case TSK_Undeclared:
5488 case TSK_ImplicitInstantiation:
5489 // We're explicitly instantiating something that may have already been
5490 // implicitly instantiated; that's fine.
5491 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005492
Douglas Gregor454885e2009-10-15 15:54:05 +00005493 case TSK_ExplicitSpecialization:
5494 // C++ DR 259, C++0x [temp.explicit]p4:
5495 // For a given set of template parameters, if an explicit
5496 // instantiation of a template appears after a declaration of
5497 // an explicit specialization for that template, the explicit
5498 // instantiation has no effect.
5499 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005500 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005501 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005502 // has been explicitly specialized.
Richard Smithebaf0e62011-10-18 20:49:44 +00005503 Diag(NewLoc, getLangOptions().CPlusPlus0x ?
5504 diag::warn_cxx98_compat_explicit_instantiation_after_specialization :
5505 diag::ext_explicit_instantiation_after_specialization)
5506 << PrevDecl;
5507 Diag(PrevDecl->getLocation(),
5508 diag::note_previous_template_specialization);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005509 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005510 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005511
Douglas Gregor454885e2009-10-15 15:54:05 +00005512 case TSK_ExplicitInstantiationDeclaration:
5513 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005514 // were previously asked to suppress instantiations. That's fine.
Nico Weberff91d242011-12-23 20:58:04 +00005515
5516 // C++0x [temp.explicit]p4:
5517 // For a given set of template parameters, if an explicit instantiation
5518 // of a template appears after a declaration of an explicit
5519 // specialization for that template, the explicit instantiation has no
5520 // effect.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005521 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Nico Weberff91d242011-12-23 20:58:04 +00005522 // Is there any previous explicit specialization declaration?
5523 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5524 HasNoEffect = true;
5525 break;
5526 }
5527 }
5528
Douglas Gregor454885e2009-10-15 15:54:05 +00005529 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005530
Douglas Gregor454885e2009-10-15 15:54:05 +00005531 case TSK_ExplicitInstantiationDefinition:
5532 // C++0x [temp.spec]p5:
5533 // For a given template and a given set of template-arguments,
5534 // - an explicit instantiation definition shall appear at most once
5535 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005536 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005537 << PrevDecl;
Nico Weberd1d512a2012-01-09 19:52:25 +00005538 Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
Douglas Gregor0d035142009-10-27 18:42:08 +00005539 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005540 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005541 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005542 }
Douglas Gregor454885e2009-10-15 15:54:05 +00005543 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005544
David Blaikieb219cfc2011-09-23 05:06:16 +00005545 llvm_unreachable("Missing specialization/instantiation case?");
Douglas Gregor454885e2009-10-15 15:54:05 +00005546}
5547
John McCallaf2094e2010-04-08 09:05:18 +00005548/// \brief Perform semantic analysis for the given dependent function
5549/// template specialization. The only possible way to get a dependent
5550/// function template specialization is with a friend declaration,
5551/// like so:
5552///
5553/// template <class T> void foo(T);
5554/// template <class T> class A {
5555/// friend void foo<>(T);
5556/// };
5557///
5558/// There really isn't any useful analysis we can do here, so we
5559/// just store the information.
5560bool
5561Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5562 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5563 LookupResult &Previous) {
5564 // Remove anything from Previous that isn't a function template in
5565 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005566 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005567 LookupResult::Filter F = Previous.makeFilter();
5568 while (F.hasNext()) {
5569 NamedDecl *D = F.next()->getUnderlyingDecl();
5570 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005571 !FDLookupContext->InEnclosingNamespaceSetOf(
5572 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005573 F.erase();
5574 }
5575 F.done();
5576
5577 // Should this be diagnosed here?
5578 if (Previous.empty()) return true;
5579
5580 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5581 ExplicitTemplateArgs);
5582 return false;
5583}
5584
Abramo Bagnarae03db982010-05-20 15:32:11 +00005585/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005586/// specialization.
5587///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005588/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005589/// explicit function template specialization. On successful completion,
5590/// the function declaration \p FD will become a function template
5591/// specialization.
5592///
5593/// \param FD the function declaration, which will be updated to become a
5594/// function template specialization.
5595///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005596/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5597/// if any. Note that this may be valid info even when 0 arguments are
5598/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5599/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005600///
Francois Pichet59e7c562011-07-08 06:21:47 +00005601/// \param Previous the set of declarations that may be specialized by
Abramo Bagnarae03db982010-05-20 15:32:11 +00005602/// this function specialization.
5603bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005604Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005605 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005606 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005607 // The set of function template specializations that could match this
5608 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005609 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005610
Sebastian Redl7a126a42010-08-31 00:36:30 +00005611 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005612 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5613 I != E; ++I) {
5614 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5615 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005616 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005617 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005618 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5619 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005620 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005621
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005622 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005623 // A trailing template-argument can be left unspecified in the
5624 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005625 // provided it can be deduced from the function argument type.
5626 // Perform template argument deduction to determine whether we may be
5627 // specializing this template.
5628 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005629 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005630 FunctionDecl *Specialization = 0;
5631 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005632 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005633 FD->getType(),
5634 Specialization,
5635 Info)) {
5636 // FIXME: Template argument deduction failed; record why it failed, so
5637 // that we can provide nifty diagnostics.
5638 (void)TDK;
5639 continue;
5640 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005641
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005642 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005643 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005644 }
5645 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005646
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005647 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005648 UnresolvedSetIterator Result
5649 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005650 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005651 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005652 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005653 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005654 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005655 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005656 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005657 return true;
John McCallc373d482010-01-27 01:50:18 +00005658
5659 // Ignore access information; it doesn't figure into redeclaration checking.
5660 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005661
5662 FunctionTemplateSpecializationInfo *SpecInfo
5663 = Specialization->getTemplateSpecializationInfo();
5664 assert(SpecInfo && "Function template specialization info missing?");
Francois Pichet59e7c562011-07-08 06:21:47 +00005665
5666 // Note: do not overwrite location info if previous template
5667 // specialization kind was explicit.
5668 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5669 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5670 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005671
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005672 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005673 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005674
5675 // If this is a friend declaration, then we're not really declaring
5676 // an explicit specialization.
5677 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005678
Douglas Gregord5cb8762009-10-07 00:13:32 +00005679 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005680 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005681 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005682 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005683 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005684 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005685 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005686
5687 // C++ [temp.expl.spec]p6:
5688 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005689 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005690 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005691 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005692 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005693 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005694 if (!isFriend &&
5695 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005696 TSK_ExplicitSpecialization,
5697 Specialization,
5698 SpecInfo->getTemplateSpecializationKind(),
5699 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005700 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005701 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005702
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005703 // Mark the prior declaration as an explicit specialization, so that later
5704 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005705 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005706 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005707 MarkUnusedFileScopedDecl(Specialization);
5708 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005709
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005710 // Turn the given function declaration into a function template
5711 // specialization, with the template arguments from the previous
5712 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005713 // Take copies of (semantic and syntactic) template argument lists.
5714 const TemplateArgumentList* TemplArgs = new (Context)
5715 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
Douglas Gregor838db382010-02-11 01:19:42 +00005716 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005717 TemplArgs, /*InsertPos=*/0,
5718 SpecInfo->getTemplateSpecializationKind(),
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00005719 ExplicitTemplateArgs);
Douglas Gregore885e182011-05-21 18:53:30 +00005720 FD->setStorageClass(Specialization->getStorageClass());
5721
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005722 // The "previous declaration" for this function template specialization is
5723 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005724 Previous.clear();
5725 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005726 return false;
5727}
5728
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005729/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005730/// specialization.
5731///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005732/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005733/// explicit member function specialization. On successful completion,
5734/// the function declaration \p FD will become a member function
5735/// specialization.
5736///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005737/// \param Member the member declaration, which will be updated to become a
5738/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005739///
John McCall68263142009-11-18 22:49:29 +00005740/// \param Previous the set of declarations, one of which may be specialized
5741/// by this function specialization; the set will be modified to contain the
5742/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005743bool
John McCall68263142009-11-18 22:49:29 +00005744Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005745 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005746
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005747 // Try to find the member we are instantiating.
5748 NamedDecl *Instantiation = 0;
5749 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005750 MemberSpecializationInfo *MSInfo = 0;
5751
John McCall68263142009-11-18 22:49:29 +00005752 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005753 // Nowhere to look anyway.
5754 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005755 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5756 I != E; ++I) {
5757 NamedDecl *D = (*I)->getUnderlyingDecl();
5758 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005759 if (Context.hasSameType(Function->getType(), Method->getType())) {
5760 Instantiation = Method;
5761 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005762 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005763 break;
5764 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005765 }
5766 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005767 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005768 VarDecl *PrevVar;
5769 if (Previous.isSingleResult() &&
5770 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005771 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005772 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005773 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005774 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005775 }
5776 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005777 CXXRecordDecl *PrevRecord;
5778 if (Previous.isSingleResult() &&
5779 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5780 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005781 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005782 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005783 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005784 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005785
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005786 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005787 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005788 // specializations are always out-of-line, the caller will complain about
5789 // this mismatch later.
5790 return false;
5791 }
John McCall77e8b112010-04-13 20:37:33 +00005792
5793 // If this is a friend, just bail out here before we start turning
5794 // things into explicit specializations.
5795 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5796 // Preserve instantiation information.
5797 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5798 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5799 cast<CXXMethodDecl>(InstantiatedFrom),
5800 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5801 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5802 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5803 cast<CXXRecordDecl>(InstantiatedFrom),
5804 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5805 }
5806
5807 Previous.clear();
5808 Previous.addDecl(Instantiation);
5809 return false;
5810 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005811
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005812 // Make sure that this is a specialization of a member.
5813 if (!InstantiatedFrom) {
5814 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5815 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005816 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5817 return true;
5818 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005819
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005820 // C++ [temp.expl.spec]p6:
5821 // If a template, a member template or the member of a class template is
Nico Weberff91d242011-12-23 20:58:04 +00005822 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005823 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005824 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005825 // use occurs; no diagnostic is required.
5826 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005827
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005828 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005829 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5830 TSK_ExplicitSpecialization,
5831 Instantiation,
5832 MSInfo->getTemplateSpecializationKind(),
5833 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005834 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005835 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005836
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005837 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005838 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005839 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005840 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005841 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005842 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005843
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005844 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005845 // the original declaration to note that it is an explicit specialization
5846 // (if it was previously an implicit instantiation). This latter step
5847 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005848 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005849 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5850 if (InstantiationFunction->getTemplateSpecializationKind() ==
5851 TSK_ImplicitInstantiation) {
5852 InstantiationFunction->setTemplateSpecializationKind(
5853 TSK_ExplicitSpecialization);
5854 InstantiationFunction->setLocation(Member->getLocation());
5855 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005856
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005857 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5858 cast<CXXMethodDecl>(InstantiatedFrom),
5859 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005860 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005861 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005862 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5863 if (InstantiationVar->getTemplateSpecializationKind() ==
5864 TSK_ImplicitInstantiation) {
5865 InstantiationVar->setTemplateSpecializationKind(
5866 TSK_ExplicitSpecialization);
5867 InstantiationVar->setLocation(Member->getLocation());
5868 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005869
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005870 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5871 cast<VarDecl>(InstantiatedFrom),
5872 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005873 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005874 } else {
5875 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005876 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5877 if (InstantiationClass->getTemplateSpecializationKind() ==
5878 TSK_ImplicitInstantiation) {
5879 InstantiationClass->setTemplateSpecializationKind(
5880 TSK_ExplicitSpecialization);
5881 InstantiationClass->setLocation(Member->getLocation());
5882 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005883
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005884 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005885 cast<CXXRecordDecl>(InstantiatedFrom),
5886 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005887 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005888
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005889 // Save the caller the trouble of having to figure out which declaration
5890 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005891 Previous.clear();
5892 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005893 return false;
5894}
5895
Douglas Gregor558c0322009-10-14 23:41:34 +00005896/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005897///
5898/// \returns true if a serious error occurs, false otherwise.
5899static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005900 SourceLocation InstLoc,
5901 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005902 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5903 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005904
Douglas Gregor669eed82010-07-13 00:10:04 +00005905 if (CurContext->isRecord()) {
5906 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5907 << D;
5908 return true;
5909 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005910
Richard Smith3e2e91e2011-10-18 02:28:33 +00005911 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005912 // An explicit instantiation shall appear in an enclosing namespace of its
Richard Smith3e2e91e2011-10-18 02:28:33 +00005913 // template. If the name declared in the explicit instantiation is an
5914 // unqualified name, the explicit instantiation shall appear in the
5915 // namespace where its template is declared or, if that namespace is inline
5916 // (7.3.1), any namespace from its enclosing namespace set.
Douglas Gregor558c0322009-10-14 23:41:34 +00005917 //
5918 // This is DR275, which we do not retroactively apply to C++98/03.
Richard Smith3e2e91e2011-10-18 02:28:33 +00005919 if (WasQualifiedName) {
5920 if (CurContext->Encloses(OrigContext))
5921 return false;
5922 } else {
5923 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
5924 return false;
5925 }
5926
5927 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) {
5928 if (WasQualifiedName)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005929 S.Diag(InstLoc,
5930 S.getLangOptions().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005931 diag::err_explicit_instantiation_out_of_scope :
5932 diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005933 << D << NS;
5934 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005935 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005936 S.getLangOptions().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005937 diag::err_explicit_instantiation_unqualified_wrong_namespace :
5938 diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
5939 << D << NS;
5940 } else
5941 S.Diag(InstLoc,
5942 S.getLangOptions().CPlusPlus0x?
5943 diag::err_explicit_instantiation_must_be_global :
5944 diag::warn_explicit_instantiation_must_be_global_0x)
5945 << D;
Douglas Gregor558c0322009-10-14 23:41:34 +00005946 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005947 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005948}
5949
5950/// \brief Determine whether the given scope specifier has a template-id in it.
5951static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5952 if (!SS.isSet())
5953 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005954
Richard Smith3e2e91e2011-10-18 02:28:33 +00005955 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005956 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005957 // or a static data member of a class template specialization, the name of
5958 // the class template specialization in the qualified-id for the member
5959 // name shall be a simple-template-id.
5960 //
5961 // C++98 has the same restriction, just worded differently.
5962 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5963 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005964 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005965 if (isa<TemplateSpecializationType>(T))
5966 return true;
5967
5968 return false;
5969}
5970
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005971// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005972DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005973Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005974 SourceLocation ExternLoc,
5975 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005976 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005977 SourceLocation KWLoc,
5978 const CXXScopeSpec &SS,
5979 TemplateTy TemplateD,
5980 SourceLocation TemplateNameLoc,
5981 SourceLocation LAngleLoc,
5982 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005983 SourceLocation RAngleLoc,
5984 AttributeList *Attr) {
5985 // Find the class template we're specializing
5986 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005987 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005988 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
5989
5990 // Check that the specialization uses the same tag kind as the
5991 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005992 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5993 assert(Kind != TTK_Enum &&
5994 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005995 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00005996 Kind, /*isDefinition*/false, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005997 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005998 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005999 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00006000 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006001 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00006002 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006003 diag::note_previous_use);
6004 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
6005 }
6006
Douglas Gregor558c0322009-10-14 23:41:34 +00006007 // C++0x [temp.explicit]p2:
6008 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006009 // definition and an explicit instantiation declaration. An explicit
6010 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00006011 TemplateSpecializationKind TSK
6012 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6013 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006014
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006015 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00006016 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00006017 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006018
6019 // Check that the template argument list is well-formed for this
6020 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00006021 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00006022 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
6023 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006024 return true;
6025
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006026 // Find the class template specialization declaration that
6027 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006028 void *InsertPos = 0;
6029 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00006030 = ClassTemplate->findSpecialization(Converted.data(),
6031 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006032
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006033 TemplateSpecializationKind PrevDecl_TSK
6034 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
6035
Douglas Gregord5cb8762009-10-07 00:13:32 +00006036 // C++0x [temp.explicit]p2:
6037 // [...] An explicit instantiation shall appear in an enclosing
6038 // namespace of its template. [...]
6039 //
6040 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00006041 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
6042 SS.isSet()))
6043 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006044
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006045 ClassTemplateSpecializationDecl *Specialization = 0;
6046
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006047 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006048 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00006049 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006050 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006051 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006052 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00006053 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006054
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006055 // Even though HasNoEffect == true means that this explicit instantiation
6056 // has no effect on semantics, we go on to put its syntax in the AST.
6057
6058 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
6059 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00006060 // Since the only prior class template specialization with these
6061 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006062 // declaration node as our own, updating the source location
6063 // for the template name to reflect our new declaration.
6064 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00006065 Specialization = PrevDecl;
6066 Specialization->setLocation(TemplateNameLoc);
6067 PrevDecl = 0;
6068 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006069 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006070
Douglas Gregor52604ab2009-09-11 21:19:12 +00006071 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006072 // Create a new class template specialization declaration node for
6073 // this explicit specialization.
6074 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00006075 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006076 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00006077 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006078 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00006079 Converted.data(),
6080 Converted.size(),
6081 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00006082 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006083
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00006084 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006085 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00006086 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006087 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006088 }
6089
6090 // Build the fully-sugared type for this explicit instantiation as
6091 // the user wrote in the explicit instantiation itself. This means
6092 // that we'll pretty-print the type retrieved from the
6093 // specialization's declaration the way that the user actually wrote
6094 // the explicit instantiation, rather than formatting the name based
6095 // on the "canonical" representation used to store the template
6096 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00006097 TypeSourceInfo *WrittenTy
6098 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
6099 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006100 Context.getTypeDeclType(Specialization));
6101 Specialization->setTypeAsWritten(WrittenTy);
6102 TemplateArgsIn.release();
6103
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006104 // Set source locations for keywords.
6105 Specialization->setExternLoc(ExternLoc);
6106 Specialization->setTemplateKeywordLoc(TemplateLoc);
6107
Rafael Espindola0257b7f2012-01-03 06:04:21 +00006108 if (Attr)
6109 ProcessDeclAttributeList(S, Specialization, Attr);
6110
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006111 // Add the explicit instantiation into its lexical context. However,
6112 // since explicit instantiations are never found by name lookup, we
6113 // just put it into the declaration context directly.
6114 Specialization->setLexicalDeclContext(CurContext);
6115 CurContext->addDecl(Specialization);
6116
6117 // Syntax is now OK, so return if it has no other effect on semantics.
6118 if (HasNoEffect) {
6119 // Set the template specialization kind.
6120 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006121 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00006122 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006123
6124 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006125 // A definition of a class template or class member template
6126 // shall be in scope at the point of the explicit instantiation of
6127 // the class template or class member template.
6128 //
6129 // This check comes when we actually try to perform the
6130 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006131 ClassTemplateSpecializationDecl *Def
6132 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006133 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006134 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006135 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006136 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006137 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006138 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
6139 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006140
Douglas Gregor0d035142009-10-27 18:42:08 +00006141 // Instantiate the members of this class template specialization.
6142 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006143 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006144 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00006145 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
6146
6147 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
6148 // TSK_ExplicitInstantiationDefinition
6149 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
6150 TSK == TSK_ExplicitInstantiationDefinition)
6151 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006152
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006153 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006154 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006155
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006156 // Set the template specialization kind.
6157 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006158 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006159}
6160
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006161// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00006162DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00006163Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00006164 SourceLocation ExternLoc,
6165 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00006166 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006167 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00006168 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006169 IdentifierInfo *Name,
6170 SourceLocation NameLoc,
6171 AttributeList *Attr) {
6172
Douglas Gregor402abb52009-05-28 23:31:59 +00006173 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00006174 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00006175 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00006176 KWLoc, SS, Name, NameLoc, Attr, AS_none,
Douglas Gregore7612302011-09-09 19:05:14 +00006177 /*ModulePrivateLoc=*/SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +00006178 MultiTemplateParamsArg(*this, 0, 0),
Richard Smithbdad7a22012-01-10 01:33:14 +00006179 Owned, IsDependent, SourceLocation(), false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00006180 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00006181 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
6182
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006183 if (!TagD)
6184 return true;
6185
John McCalld226f652010-08-21 09:40:31 +00006186 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006187 if (Tag->isEnum()) {
6188 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
6189 << Context.getTypeDeclType(Tag);
6190 return true;
6191 }
6192
Douglas Gregord0c87372009-05-27 17:30:49 +00006193 if (Tag->isInvalidDecl())
6194 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006195
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006196 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
6197 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
6198 if (!Pattern) {
6199 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
6200 << Context.getTypeDeclType(Record);
6201 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
6202 return true;
6203 }
6204
Douglas Gregor558c0322009-10-14 23:41:34 +00006205 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006206 // If the explicit instantiation is for a class or member class, the
6207 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00006208 // simple-template-id.
6209 //
6210 // C++98 has the same restriction, just worded differently.
6211 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00006212 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006213 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006214
Douglas Gregor558c0322009-10-14 23:41:34 +00006215 // C++0x [temp.explicit]p2:
6216 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006217 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00006218 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00006219 TemplateSpecializationKind TSK
6220 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6221 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006222
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006223 // C++0x [temp.explicit]p2:
6224 // [...] An explicit instantiation shall appear in an enclosing
6225 // namespace of its template. [...]
6226 //
6227 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00006228 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006229
Douglas Gregor454885e2009-10-15 15:54:05 +00006230 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006231 CXXRecordDecl *PrevDecl
Douglas Gregoref96ee02012-01-14 16:38:05 +00006232 = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl());
Douglas Gregor952b0172010-02-11 01:04:33 +00006233 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00006234 PrevDecl = Record;
6235 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00006236 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006237 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00006238 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006239 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00006240 PrevDecl,
6241 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006242 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006243 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006244 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006245 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00006246 return TagD;
6247 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006248
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006249 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00006250 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006251 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006252 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006253 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006254 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006255 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00006256 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006257 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00006258 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
6259 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006260 Diag(Pattern->getLocation(), diag::note_forward_declaration)
6261 << Pattern;
6262 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00006263 } else {
6264 if (InstantiateClass(NameLoc, Record, Def,
6265 getTemplateInstantiationArgs(Record),
6266 TSK))
6267 return true;
6268
Douglas Gregor952b0172010-02-11 01:04:33 +00006269 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00006270 if (!RecordDef)
6271 return true;
6272 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006273 }
6274
Douglas Gregor0d035142009-10-27 18:42:08 +00006275 // Instantiate all of the members of the class.
6276 InstantiateClassMembers(NameLoc, RecordDef,
6277 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006278
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006279 if (TSK == TSK_ExplicitInstantiationDefinition)
6280 MarkVTableUsed(NameLoc, RecordDef, true);
6281
Mike Stump390b4cc2009-05-16 07:39:55 +00006282 // FIXME: We don't have any representation for explicit instantiations of
6283 // member classes. Such a representation is not needed for compilation, but it
6284 // should be available for clients that want to see all of the declarations in
6285 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006286 return TagD;
6287}
6288
John McCallf312b1e2010-08-26 23:41:50 +00006289DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
6290 SourceLocation ExternLoc,
6291 SourceLocation TemplateLoc,
6292 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006293 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00006294 // TODO: check if/when DNInfo should replace Name.
6295 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
6296 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006297 if (!Name) {
6298 if (!D.isInvalidType())
6299 Diag(D.getDeclSpec().getSourceRange().getBegin(),
6300 diag::err_explicit_instantiation_requires_name)
6301 << D.getDeclSpec().getSourceRange()
6302 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006303
Douglas Gregord5a423b2009-09-25 18:43:00 +00006304 return true;
6305 }
6306
6307 // The scope passed in may not be a decl scope. Zip up the scope tree until
6308 // we find one that is.
6309 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6310 (S->getFlags() & Scope::TemplateParamScope) != 0)
6311 S = S->getParent();
6312
6313 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006314 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6315 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006316 if (R.isNull())
6317 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006318
Douglas Gregore885e182011-05-21 18:53:30 +00006319 // C++ [dcl.stc]p1:
6320 // A storage-class-specifier shall not be specified in [...] an explicit
6321 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006322 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006323 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6324 << Name;
6325 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006326 } else if (D.getDeclSpec().getStorageClassSpec()
6327 != DeclSpec::SCS_unspecified) {
6328 // Complain about then remove the storage class specifier.
6329 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6330 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6331
6332 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006333 }
6334
Douglas Gregor663b5a02009-10-14 20:14:33 +00006335 // C++0x [temp.explicit]p1:
6336 // [...] An explicit instantiation of a function template shall not use the
6337 // inline or constexpr specifiers.
6338 // Presumably, this also applies to member functions of class templates as
6339 // well.
Richard Smith2dc7ece2011-10-18 03:44:03 +00006340 if (D.getDeclSpec().isInlineSpecified())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006341 Diag(D.getDeclSpec().getInlineSpecLoc(),
Richard Smith2dc7ece2011-10-18 03:44:03 +00006342 getLangOptions().CPlusPlus0x ?
6343 diag::err_explicit_instantiation_inline :
6344 diag::warn_explicit_instantiation_inline_0x)
Richard Smithfe6f6482011-10-14 19:58:02 +00006345 << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
6346 if (D.getDeclSpec().isConstexprSpecified())
6347 // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is
6348 // not already specified.
6349 Diag(D.getDeclSpec().getConstexprSpecLoc(),
6350 diag::err_explicit_instantiation_constexpr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006351
Douglas Gregor558c0322009-10-14 23:41:34 +00006352 // C++0x [temp.explicit]p2:
6353 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006354 // definition and an explicit instantiation declaration. An explicit
6355 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006356 TemplateSpecializationKind TSK
6357 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6358 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006359
Abramo Bagnara25777432010-08-11 22:01:17 +00006360 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006361 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006362
6363 if (!R->isFunctionType()) {
6364 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006365 // A [...] static data member of a class template can be explicitly
6366 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006367 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006368 if (Previous.isAmbiguous())
6369 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006370
John McCall1bcee0a2009-12-02 08:25:40 +00006371 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006372 if (!Prev || !Prev->isStaticDataMember()) {
6373 // We expect to see a data data member here.
6374 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6375 << Name;
6376 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6377 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006378 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006379 return true;
6380 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006381
Douglas Gregord5a423b2009-09-25 18:43:00 +00006382 if (!Prev->getInstantiatedFromStaticDataMember()) {
6383 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006384 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006385 diag::err_explicit_instantiation_data_member_not_instantiated)
6386 << Prev;
6387 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6388 // FIXME: Can we provide a note showing where this was declared?
6389 return true;
6390 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006391
Douglas Gregor558c0322009-10-14 23:41:34 +00006392 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006393 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006394 // or a static data member of a class template specialization, the name of
6395 // the class template specialization in the qualified-id for the member
6396 // name shall be a simple-template-id.
6397 //
6398 // C++98 has the same restriction, just worded differently.
6399 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006400 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006401 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006402 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006403
Douglas Gregor558c0322009-10-14 23:41:34 +00006404 // Check the scope of this explicit instantiation.
6405 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006406
Douglas Gregor454885e2009-10-15 15:54:05 +00006407 // Verify that it is okay to explicitly instantiate here.
6408 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6409 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006410 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006411 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006412 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006413 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006414 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006415 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006416 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006417 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006418
Douglas Gregord5a423b2009-09-25 18:43:00 +00006419 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006420 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006421 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006422 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006423
Douglas Gregord5a423b2009-09-25 18:43:00 +00006424 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006425 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006426 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006427
6428 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006429 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006430 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006431 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006432 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6433 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006434 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6435 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006436 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6437 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006438 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006439 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006440 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006441 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006442 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006443
Douglas Gregord5a423b2009-09-25 18:43:00 +00006444 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006445 // A [...] function [...] can be explicitly instantiated from its template.
6446 // A member function [...] of a class template can be explicitly
6447 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006448 // template.
John McCallc373d482010-01-27 01:50:18 +00006449 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006450 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6451 P != PEnd; ++P) {
6452 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006453 if (!HasExplicitTemplateArgs) {
6454 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6455 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6456 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006457
John McCallc373d482010-01-27 01:50:18 +00006458 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006459 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6460 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006461 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006462 }
6463 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006464
Douglas Gregord5a423b2009-09-25 18:43:00 +00006465 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6466 if (!FunTmpl)
6467 continue;
6468
John McCall5769d612010-02-08 23:07:23 +00006469 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006470 FunctionDecl *Specialization = 0;
6471 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006472 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006473 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006474 R, Specialization, Info)) {
6475 // FIXME: Keep track of almost-matches?
6476 (void)TDK;
6477 continue;
6478 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006479
John McCallc373d482010-01-27 01:50:18 +00006480 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006481 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006482
Douglas Gregord5a423b2009-09-25 18:43:00 +00006483 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006484 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006485 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006486 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006487 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6488 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6489 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006490
John McCallc373d482010-01-27 01:50:18 +00006491 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006492 return true;
John McCallc373d482010-01-27 01:50:18 +00006493
6494 // Ignore access control bits, we don't need them for redeclaration checking.
6495 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006496
Douglas Gregor0a897e32009-10-15 17:21:20 +00006497 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006498 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006499 diag::err_explicit_instantiation_member_function_not_instantiated)
6500 << Specialization
6501 << (Specialization->getTemplateSpecializationKind() ==
6502 TSK_ExplicitSpecialization);
6503 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6504 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006505 }
6506
Douglas Gregoref96ee02012-01-14 16:38:05 +00006507 FunctionDecl *PrevDecl = Specialization->getPreviousDecl();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006508 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6509 PrevDecl = Specialization;
6510
Douglas Gregor0a897e32009-10-15 17:21:20 +00006511 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006512 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006513 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006514 PrevDecl,
6515 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006516 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006517 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006518 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006519
Douglas Gregor0a897e32009-10-15 17:21:20 +00006520 // FIXME: We may still want to build some representation of this
6521 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006522 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006523 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006524 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006525
6526 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Rafael Espindola256fc4d2012-01-04 05:40:59 +00006527 AttributeList *Attr = D.getDeclSpec().getAttributes().getList();
6528 if (Attr)
6529 ProcessDeclAttributeList(S, Specialization, Attr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006530
Douglas Gregor0a897e32009-10-15 17:21:20 +00006531 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006532 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006533
Douglas Gregor558c0322009-10-14 23:41:34 +00006534 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006535 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006536 // or a static data member of a class template specialization, the name of
6537 // the class template specialization in the qualified-id for the member
6538 // name shall be a simple-template-id.
6539 //
6540 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006541 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006542 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006543 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006544 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006545 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006546 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006547 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006548
Douglas Gregor558c0322009-10-14 23:41:34 +00006549 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006550 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006551 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006552 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006553 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006554
Douglas Gregord5a423b2009-09-25 18:43:00 +00006555 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006556 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006557}
6558
John McCallf312b1e2010-08-26 23:41:50 +00006559TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006560Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6561 const CXXScopeSpec &SS, IdentifierInfo *Name,
6562 SourceLocation TagLoc, SourceLocation NameLoc) {
6563 // This has to hold, because SS is expected to be defined.
6564 assert(Name && "Expected a name in a dependent tag");
6565
6566 NestedNameSpecifier *NNS
6567 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6568 if (!NNS)
6569 return true;
6570
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006571 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006572
Douglas Gregor48c89f42010-04-24 16:38:41 +00006573 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6574 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006575 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006576 return true;
6577 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006578
Douglas Gregor059101f2011-03-02 00:47:37 +00006579 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006580 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006581 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6582
6583 // Create type-source location information for this type.
6584 TypeLocBuilder TLB;
6585 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
6586 TL.setKeywordLoc(TagLoc);
6587 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6588 TL.setNameLoc(NameLoc);
6589 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006590}
6591
John McCallf312b1e2010-08-26 23:41:50 +00006592TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006593Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6594 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006595 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006596 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006597 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006598
Richard Smithebaf0e62011-10-18 20:49:44 +00006599 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6600 Diag(TypenameLoc,
6601 getLangOptions().CPlusPlus0x ?
6602 diag::warn_cxx98_compat_typename_outside_of_template :
6603 diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006604 << FixItHint::CreateRemoval(TypenameLoc);
6605
Douglas Gregor2494dd02011-03-01 01:34:45 +00006606 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006607 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6608 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006609 if (T.isNull())
6610 return true;
John McCall63b43852010-04-29 23:50:39 +00006611
6612 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6613 if (isa<DependentNameType>(T)) {
6614 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006615 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006616 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006617 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006618 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006619 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
John McCall4e449832010-05-28 23:32:21 +00006620 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006621 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006622 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006623 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006624
John McCallb3d87482010-08-24 05:47:05 +00006625 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006626}
6627
John McCallf312b1e2010-08-26 23:41:50 +00006628TypeResult
Douglas Gregora02411e2011-02-27 22:46:49 +00006629Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6630 const CXXScopeSpec &SS,
6631 SourceLocation TemplateLoc,
6632 TemplateTy TemplateIn,
6633 SourceLocation TemplateNameLoc,
6634 SourceLocation LAngleLoc,
6635 ASTTemplateArgsPtr TemplateArgsIn,
6636 SourceLocation RAngleLoc) {
Richard Smithebaf0e62011-10-18 20:49:44 +00006637 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6638 Diag(TypenameLoc,
6639 getLangOptions().CPlusPlus0x ?
6640 diag::warn_cxx98_compat_typename_outside_of_template :
6641 diag::ext_typename_outside_of_template)
6642 << FixItHint::CreateRemoval(TypenameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006643
6644 // Translate the parser's template argument list in our AST format.
6645 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6646 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6647
6648 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006649 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6650 // Construct a dependent template specialization type.
6651 assert(DTN && "dependent template has non-dependent name?");
6652 assert(DTN->getQualifier()
6653 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6654 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6655 DTN->getQualifier(),
6656 DTN->getIdentifier(),
6657 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006658
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006659 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006660 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006661 DependentTemplateSpecializationTypeLoc SpecTL
6662 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Douglas Gregora02411e2011-02-27 22:46:49 +00006663 SpecTL.setLAngleLoc(LAngleLoc);
6664 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006665 SpecTL.setKeywordLoc(TypenameLoc);
Douglas Gregor94fdffa2011-03-01 20:11:18 +00006666 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006667 SpecTL.setNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006668 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6669 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006670 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006671 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006672
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006673 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6674 if (T.isNull())
6675 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006676
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006677 // Provide source-location information for the template specialization
6678 // type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006679 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006680 TemplateSpecializationTypeLoc SpecTL
6681 = Builder.push<TemplateSpecializationTypeLoc>(T);
6682
6683 // FIXME: No place to set the location of the 'template' keyword!
Douglas Gregora02411e2011-02-27 22:46:49 +00006684 SpecTL.setLAngleLoc(LAngleLoc);
6685 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006686 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006687 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6688 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6689
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006690 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6691 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
6692 TL.setKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006693 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6694
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006695 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6696 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006697}
6698
Douglas Gregora02411e2011-02-27 22:46:49 +00006699
Douglas Gregord57959a2009-03-27 23:10:48 +00006700/// \brief Build the type that describes a C++ typename specifier,
6701/// e.g., "typename T::type".
6702QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006703Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6704 SourceLocation KeywordLoc,
6705 NestedNameSpecifierLoc QualifierLoc,
6706 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006707 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006708 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006709 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006710
John McCall77bb1aa2010-05-01 00:40:08 +00006711 DeclContext *Ctx = computeDeclContext(SS);
6712 if (!Ctx) {
6713 // If the nested-name-specifier is dependent and couldn't be
6714 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006715 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6716 return Context.getDependentNameType(Keyword,
6717 QualifierLoc.getNestedNameSpecifier(),
6718 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006719 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006720
John McCall77bb1aa2010-05-01 00:40:08 +00006721 // If the nested-name-specifier refers to the current instantiation,
6722 // the "typename" keyword itself is superfluous. In C++03, the
6723 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6724 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006725 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006726
John McCall77bb1aa2010-05-01 00:40:08 +00006727 if (RequireCompleteDeclContext(SS, Ctx))
6728 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006729
6730 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006731 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006732 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006733 unsigned DiagID = 0;
6734 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006735 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006736 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006737 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006738 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006739
6740 case LookupResult::FoundUnresolvedValue: {
6741 // We found a using declaration that is a value. Most likely, the using
6742 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006743 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006744 IILoc);
6745 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6746 << Name << Ctx << FullRange;
6747 if (UnresolvedUsingValueDecl *Using
6748 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006749 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006750 Diag(Loc, diag::note_using_value_decl_missing_typename)
6751 << FixItHint::CreateInsertion(Loc, "typename ");
6752 }
6753 }
6754 // Fall through to create a dependent typename type, from which we can recover
6755 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006756
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006757 case LookupResult::NotFoundInCurrentInstantiation:
6758 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006759 return Context.getDependentNameType(Keyword,
6760 QualifierLoc.getNestedNameSpecifier(),
6761 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006762
6763 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006764 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006765 // We found a type. Build an ElaboratedType, since the
6766 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006767 return Context.getElaboratedType(ETK_Typename,
6768 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006769 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006770 }
6771
6772 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006773 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006774 break;
6775
6776 case LookupResult::FoundOverloaded:
6777 DiagID = diag::err_typename_nested_not_type;
6778 Referenced = *Result.begin();
6779 break;
6780
John McCall6e247262009-10-10 05:48:19 +00006781 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006782 return QualType();
6783 }
6784
6785 // If we get here, it's because name lookup did not find a
6786 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006787 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006788 IILoc);
6789 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006790 if (Referenced)
6791 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6792 << Name;
6793 return QualType();
6794}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006795
6796namespace {
6797 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006798 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006799 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006800 SourceLocation Loc;
6801 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006802
Douglas Gregor4a959d82009-08-06 16:20:37 +00006803 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006804 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006805
Mike Stump1eb44332009-09-09 15:08:12 +00006806 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006807 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006808 DeclarationName Entity)
6809 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006810 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006811
6812 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006813 /// transformed.
6814 ///
6815 /// For the purposes of type reconstruction, a type has already been
6816 /// transformed if it is NULL or if it is not dependent.
6817 bool AlreadyTransformed(QualType T) {
6818 return T.isNull() || !T->isDependentType();
6819 }
Mike Stump1eb44332009-09-09 15:08:12 +00006820
6821 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006822 /// rebuilt.
6823 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006824
Douglas Gregor4a959d82009-08-06 16:20:37 +00006825 /// \brief Returns the name of the entity whose type is being rebuilt.
6826 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006827
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006828 /// \brief Sets the "base" location and entity when that
6829 /// information is known based on another transformation.
6830 void setBase(SourceLocation Loc, DeclarationName Entity) {
6831 this->Loc = Loc;
6832 this->Entity = Entity;
6833 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006834 };
6835}
6836
Douglas Gregor4a959d82009-08-06 16:20:37 +00006837/// \brief Rebuilds a type within the context of the current instantiation.
6838///
Mike Stump1eb44332009-09-09 15:08:12 +00006839/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006840/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006841/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006842/// partial specialization thereof). This routine will rebuild that type now
6843/// that we have entered the declarator's scope, which may produce different
6844/// canonical types, e.g.,
6845///
6846/// \code
6847/// template<typename T>
6848/// struct X {
6849/// typedef T* pointer;
6850/// pointer data();
6851/// };
6852///
6853/// template<typename T>
6854/// typename X<T>::pointer X<T>::data() { ... }
6855/// \endcode
6856///
Douglas Gregor4714c122010-03-31 17:34:00 +00006857/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006858/// since we do not know that we can look into X<T> when we parsed the type.
6859/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006860/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006861/// as the canonical type of T*, allowing the return types of the out-of-line
6862/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006863TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6864 SourceLocation Loc,
6865 DeclarationName Name) {
6866 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006867 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006868
Douglas Gregor4a959d82009-08-06 16:20:37 +00006869 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6870 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006871}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006872
John McCall60d7b3a2010-08-24 06:29:42 +00006873ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006874 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6875 DeclarationName());
6876 return Rebuilder.TransformExpr(E);
6877}
6878
John McCall63b43852010-04-29 23:50:39 +00006879bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006880 if (SS.isInvalid())
6881 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006882
Douglas Gregor7e384942011-02-25 16:07:42 +00006883 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006884 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6885 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006886 NestedNameSpecifierLoc Rebuilt
6887 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6888 if (!Rebuilt)
6889 return true;
John McCall63b43852010-04-29 23:50:39 +00006890
Douglas Gregor7e384942011-02-25 16:07:42 +00006891 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006892 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006893}
6894
Douglas Gregor20606502011-10-14 15:31:12 +00006895/// \brief Rebuild the template parameters now that we know we're in a current
6896/// instantiation.
6897bool Sema::RebuildTemplateParamsInCurrentInstantiation(
6898 TemplateParameterList *Params) {
6899 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
6900 Decl *Param = Params->getParam(I);
6901
6902 // There is nothing to rebuild in a type parameter.
6903 if (isa<TemplateTypeParmDecl>(Param))
6904 continue;
6905
6906 // Rebuild the template parameter list of a template template parameter.
6907 if (TemplateTemplateParmDecl *TTP
6908 = dyn_cast<TemplateTemplateParmDecl>(Param)) {
6909 if (RebuildTemplateParamsInCurrentInstantiation(
6910 TTP->getTemplateParameters()))
6911 return true;
6912
6913 continue;
6914 }
6915
6916 // Rebuild the type of a non-type template parameter.
6917 NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param);
6918 TypeSourceInfo *NewTSI
6919 = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(),
6920 NTTP->getLocation(),
6921 NTTP->getDeclName());
6922 if (!NewTSI)
6923 return true;
6924
6925 if (NewTSI != NTTP->getTypeSourceInfo()) {
6926 NTTP->setTypeSourceInfo(NewTSI);
6927 NTTP->setType(NewTSI->getType());
6928 }
6929 }
6930
6931 return false;
6932}
6933
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006934/// \brief Produces a formatted string that describes the binding of
6935/// template parameters to template arguments.
6936std::string
6937Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6938 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006939 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006940}
6941
6942std::string
6943Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6944 const TemplateArgument *Args,
6945 unsigned NumArgs) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006946 llvm::SmallString<128> Str;
6947 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006948
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006949 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006950 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006951
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006952 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006953 if (I >= NumArgs)
6954 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006955
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006956 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006957 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006958 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006959 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006960
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006961 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006962 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006963 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006964 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006965 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006966
Douglas Gregor87dd6972010-12-20 16:52:59 +00006967 Out << " = ";
Douglas Gregor8987b232011-09-27 23:30:47 +00006968 Args[I].print(getPrintingPolicy(), Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006969 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006970
6971 Out << ']';
6972 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006973}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006974
6975void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6976 if (!FD)
6977 return;
6978 FD->setLateTemplateParsed(Flag);
6979}
6980
6981bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6982 DeclContext *DC = CurContext;
6983
6984 while (DC) {
6985 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
6986 const FunctionDecl *FD = RD->isLocalClass();
6987 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
6988 } else if (DC->isTranslationUnit() || DC->isNamespace())
6989 return false;
6990
6991 DC = DC->getParent();
6992 }
6993 return false;
6994}