blob: 17bdd69ec242a5712939f628f5adc7658a9a5f72 [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"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000030#include "llvm/ADT/SmallString.h"
Douglas Gregorbf4ea562009-09-15 16:23:51 +000031#include "llvm/ADT/StringExtras.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000032using namespace clang;
John McCall2a7fb272010-08-25 05:32:35 +000033using namespace sema;
Douglas Gregor72c3f312008-12-05 18:15:24 +000034
John McCall78b81052010-11-10 02:40:36 +000035// Exported for use by Parser.
36SourceRange
37clang::getTemplateParamsRange(TemplateParameterList const * const *Ps,
38 unsigned N) {
39 if (!N) return SourceRange();
40 return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc());
41}
42
Douglas Gregor2dd078a2009-09-02 22:59:36 +000043/// \brief Determine whether the declaration found is acceptable as the name
44/// of a template and, if so, return that template declaration. Otherwise,
45/// returns NULL.
John McCallad00b772010-06-16 08:42:20 +000046static NamedDecl *isAcceptableTemplateName(ASTContext &Context,
47 NamedDecl *Orig) {
48 NamedDecl *D = Orig->getUnderlyingDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000049
Douglas Gregor2dd078a2009-09-02 22:59:36 +000050 if (isa<TemplateDecl>(D))
John McCallad00b772010-06-16 08:42:20 +000051 return Orig;
Mike Stump1eb44332009-09-09 15:08:12 +000052
Douglas Gregor2dd078a2009-09-02 22:59:36 +000053 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) {
54 // C++ [temp.local]p1:
55 // Like normal (non-template) classes, class templates have an
56 // injected-class-name (Clause 9). The injected-class-name
57 // can be used with or without a template-argument-list. When
58 // it is used without a template-argument-list, it is
59 // equivalent to the injected-class-name followed by the
60 // template-parameters of the class template enclosed in
61 // <>. When it is used with a template-argument-list, it
62 // refers to the specified class template specialization,
63 // which could be the current specialization or another
64 // specialization.
65 if (Record->isInjectedClassName()) {
Douglas Gregor542b5482009-10-14 17:30:58 +000066 Record = cast<CXXRecordDecl>(Record->getDeclContext());
Douglas Gregor2dd078a2009-09-02 22:59:36 +000067 if (Record->getDescribedClassTemplate())
68 return Record->getDescribedClassTemplate();
69
70 if (ClassTemplateSpecializationDecl *Spec
71 = dyn_cast<ClassTemplateSpecializationDecl>(Record))
72 return Spec->getSpecializedTemplate();
73 }
Mike Stump1eb44332009-09-09 15:08:12 +000074
Douglas Gregor2dd078a2009-09-02 22:59:36 +000075 return 0;
76 }
Mike Stump1eb44332009-09-09 15:08:12 +000077
Douglas Gregor2dd078a2009-09-02 22:59:36 +000078 return 0;
79}
80
Douglas Gregor312eadb2011-04-24 05:37:28 +000081void Sema::FilterAcceptableTemplateNames(LookupResult &R) {
Douglas Gregor01e56ae2010-04-12 20:54:26 +000082 // The set of class templates we've already seen.
83 llvm::SmallPtrSet<ClassTemplateDecl *, 8> ClassTemplates;
John McCallf7a1a742009-11-24 19:00:30 +000084 LookupResult::Filter filter = R.makeFilter();
85 while (filter.hasNext()) {
86 NamedDecl *Orig = filter.next();
Douglas Gregor312eadb2011-04-24 05:37:28 +000087 NamedDecl *Repl = isAcceptableTemplateName(Context, Orig);
John McCallf7a1a742009-11-24 19:00:30 +000088 if (!Repl)
89 filter.erase();
Douglas Gregor01e56ae2010-04-12 20:54:26 +000090 else if (Repl != Orig) {
91
92 // C++ [temp.local]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000093 // A lookup that finds an injected-class-name (10.2) can result in an
Douglas Gregor01e56ae2010-04-12 20:54:26 +000094 // ambiguity in certain cases (for example, if it is found in more than
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +000095 // one base class). If all of the injected-class-names that are found
96 // refer to specializations of the same class template, and if the name
Richard Smith3e4c6c42011-05-05 21:57:07 +000097 // is used as a template-name, the reference refers to the class
98 // template itself and not a specialization thereof, and is not
Douglas Gregor01e56ae2010-04-12 20:54:26 +000099 // ambiguous.
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000100 if (ClassTemplateDecl *ClassTmpl = dyn_cast<ClassTemplateDecl>(Repl))
101 if (!ClassTemplates.insert(ClassTmpl)) {
102 filter.erase();
103 continue;
104 }
John McCall8ba66912010-08-13 07:02:08 +0000105
106 // FIXME: we promote access to public here as a workaround to
107 // the fact that LookupResult doesn't let us remember that we
108 // found this template through a particular injected class name,
109 // which means we end up doing nasty things to the invariants.
110 // Pretending that access is public is *much* safer.
111 filter.replace(Repl, AS_public);
Douglas Gregor01e56ae2010-04-12 20:54:26 +0000112 }
John McCallf7a1a742009-11-24 19:00:30 +0000113 }
114 filter.done();
115}
116
Douglas Gregor312eadb2011-04-24 05:37:28 +0000117bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R) {
118 for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I)
119 if (isAcceptableTemplateName(Context, *I))
120 return true;
121
Douglas Gregor3b887352011-04-27 04:48:22 +0000122 return false;
Douglas Gregor312eadb2011-04-24 05:37:28 +0000123}
124
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000125TemplateNameKind Sema::isTemplateName(Scope *S,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000126 CXXScopeSpec &SS,
Abramo Bagnara7c153532010-08-06 12:11:11 +0000127 bool hasTemplateKeyword,
Douglas Gregor014e88d2009-11-03 23:16:33 +0000128 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +0000129 ParsedType ObjectTypePtr,
Douglas Gregor495c35d2009-08-25 22:51:20 +0000130 bool EnteringContext,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000131 TemplateTy &TemplateResult,
132 bool &MemberOfUnknownSpecialization) {
Douglas Gregorb862b8f2010-01-11 23:29:10 +0000133 assert(getLangOptions().CPlusPlus && "No template names in C!");
134
Douglas Gregor014e88d2009-11-03 23:16:33 +0000135 DeclarationName TName;
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000136 MemberOfUnknownSpecialization = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000137
Douglas Gregor014e88d2009-11-03 23:16:33 +0000138 switch (Name.getKind()) {
139 case UnqualifiedId::IK_Identifier:
140 TName = DeclarationName(Name.Identifier);
141 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000142
Douglas Gregor014e88d2009-11-03 23:16:33 +0000143 case UnqualifiedId::IK_OperatorFunctionId:
144 TName = Context.DeclarationNames.getCXXOperatorName(
145 Name.OperatorFunctionId.Operator);
146 break;
147
Sean Hunte6252d12009-11-28 08:58:14 +0000148 case UnqualifiedId::IK_LiteralOperatorId:
Sean Hunt3e518bd2009-11-29 07:34:05 +0000149 TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier);
150 break;
Sean Hunte6252d12009-11-28 08:58:14 +0000151
Douglas Gregor014e88d2009-11-03 23:16:33 +0000152 default:
153 return TNK_Non_template;
154 }
Mike Stump1eb44332009-09-09 15:08:12 +0000155
John McCallb3d87482010-08-24 05:47:05 +0000156 QualType ObjectType = ObjectTypePtr.get();
Mike Stump1eb44332009-09-09 15:08:12 +0000157
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000158 LookupResult R(*this, TName, Name.getSourceRange().getBegin(),
Douglas Gregorbfea2392009-12-31 08:11:17 +0000159 LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000160 LookupTemplateName(R, S, SS, ObjectType, EnteringContext,
161 MemberOfUnknownSpecialization);
John McCall67d22fb2010-08-28 20:17:00 +0000162 if (R.empty()) return TNK_Non_template;
163 if (R.isAmbiguous()) {
164 // Suppress diagnostics; we'll redo this lookup later.
John McCallb8592062010-08-13 02:23:42 +0000165 R.suppressDiagnostics();
John McCall67d22fb2010-08-28 20:17:00 +0000166
167 // FIXME: we might have ambiguous templates, in which case we
168 // should at least parse them properly!
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000169 return TNK_Non_template;
John McCallb8592062010-08-13 02:23:42 +0000170 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000171
John McCall0bd6feb2009-12-02 08:04:21 +0000172 TemplateName Template;
173 TemplateNameKind TemplateKind;
Mike Stump1eb44332009-09-09 15:08:12 +0000174
John McCall0bd6feb2009-12-02 08:04:21 +0000175 unsigned ResultCount = R.end() - R.begin();
176 if (ResultCount > 1) {
177 // We assume that we'll preserve the qualifier from a function
178 // template name in other ways.
179 Template = Context.getOverloadedTemplateName(R.begin(), R.end());
180 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000181
182 // We'll do this lookup again later.
183 R.suppressDiagnostics();
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000184 } else {
John McCall0bd6feb2009-12-02 08:04:21 +0000185 TemplateDecl *TD = cast<TemplateDecl>((*R.begin())->getUnderlyingDecl());
186
187 if (SS.isSet() && !SS.isInvalid()) {
188 NestedNameSpecifier *Qualifier
189 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
Abramo Bagnara7c153532010-08-06 12:11:11 +0000190 Template = Context.getQualifiedTemplateName(Qualifier,
191 hasTemplateKeyword, TD);
John McCall0bd6feb2009-12-02 08:04:21 +0000192 } else {
193 Template = TemplateName(TD);
194 }
195
John McCallb8592062010-08-13 02:23:42 +0000196 if (isa<FunctionTemplateDecl>(TD)) {
John McCall0bd6feb2009-12-02 08:04:21 +0000197 TemplateKind = TNK_Function_template;
John McCallb8592062010-08-13 02:23:42 +0000198
199 // We'll do this lookup again later.
200 R.suppressDiagnostics();
201 } else {
Richard Smith3e4c6c42011-05-05 21:57:07 +0000202 assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) ||
203 isa<TypeAliasTemplateDecl>(TD));
John McCall0bd6feb2009-12-02 08:04:21 +0000204 TemplateKind = TNK_Type_template;
205 }
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000206 }
Mike Stump1eb44332009-09-09 15:08:12 +0000207
John McCall0bd6feb2009-12-02 08:04:21 +0000208 TemplateResult = TemplateTy::make(Template);
209 return TemplateKind;
John McCallf7a1a742009-11-24 19:00:30 +0000210}
211
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000212bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II,
Douglas Gregor84d0a192010-01-12 21:28:44 +0000213 SourceLocation IILoc,
214 Scope *S,
215 const CXXScopeSpec *SS,
216 TemplateTy &SuggestedTemplate,
217 TemplateNameKind &SuggestedKind) {
218 // We can't recover unless there's a dependent scope specifier preceding the
219 // template name.
Douglas Gregord5ab9b02010-05-21 23:43:39 +0000220 // FIXME: Typo correction?
Douglas Gregor84d0a192010-01-12 21:28:44 +0000221 if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) ||
222 computeDeclContext(*SS))
223 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000224
Douglas Gregor84d0a192010-01-12 21:28:44 +0000225 // The code is missing a 'template' keyword prior to the dependent template
226 // name.
227 NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep();
228 Diag(IILoc, diag::err_template_kw_missing)
229 << Qualifier << II.getName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000230 << FixItHint::CreateInsertion(IILoc, "template ");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000231 SuggestedTemplate
Douglas Gregor84d0a192010-01-12 21:28:44 +0000232 = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II));
233 SuggestedKind = TNK_Dependent_template_name;
234 return true;
235}
236
John McCallf7a1a742009-11-24 19:00:30 +0000237void Sema::LookupTemplateName(LookupResult &Found,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000238 Scope *S, CXXScopeSpec &SS,
John McCallf7a1a742009-11-24 19:00:30 +0000239 QualType ObjectType,
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000240 bool EnteringContext,
241 bool &MemberOfUnknownSpecialization) {
John McCallf7a1a742009-11-24 19:00:30 +0000242 // Determine where to perform name lookup
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000243 MemberOfUnknownSpecialization = false;
John McCallf7a1a742009-11-24 19:00:30 +0000244 DeclContext *LookupCtx = 0;
245 bool isDependent = false;
246 if (!ObjectType.isNull()) {
247 // This nested-name-specifier occurs in a member access expression, e.g.,
248 // x->B::f, and we are looking into the type of the object.
249 assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist");
250 LookupCtx = computeDeclContext(ObjectType);
251 isDependent = ObjectType->isDependentType();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000252 assert((isDependent || !ObjectType->isIncompleteType()) &&
John McCallf7a1a742009-11-24 19:00:30 +0000253 "Caller should have completed object type");
Douglas Gregor1d7049a2012-01-12 16:11:24 +0000254
255 // Template names cannot appear inside an Objective-C class or object type.
256 if (ObjectType->isObjCObjectOrInterfaceType()) {
257 Found.clear();
258 return;
259 }
John McCallf7a1a742009-11-24 19:00:30 +0000260 } else if (SS.isSet()) {
261 // This nested-name-specifier occurs after another nested-name-specifier,
262 // so long into the context associated with the prior nested-name-specifier.
263 LookupCtx = computeDeclContext(SS, EnteringContext);
264 isDependent = isDependentScopeSpecifier(SS);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000265
John McCallf7a1a742009-11-24 19:00:30 +0000266 // The declaration context must be complete.
John McCall77bb1aa2010-05-01 00:40:08 +0000267 if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx))
John McCallf7a1a742009-11-24 19:00:30 +0000268 return;
269 }
270
271 bool ObjectTypeSearchedInScope = false;
272 if (LookupCtx) {
273 // Perform "qualified" name lookup into the declaration context we
274 // computed, which is either the type of the base of a member access
275 // expression or the declaration context associated with a prior
276 // nested-name-specifier.
277 LookupQualifiedName(Found, LookupCtx);
278
279 if (!ObjectType.isNull() && Found.empty()) {
280 // C++ [basic.lookup.classref]p1:
281 // In a class member access expression (5.2.5), if the . or -> token is
282 // immediately followed by an identifier followed by a <, the
283 // identifier must be looked up to determine whether the < is the
284 // beginning of a template argument list (14.2) or a less-than operator.
285 // The identifier is first looked up in the class of the object
286 // expression. If the identifier is not found, it is then looked up in
287 // the context of the entire postfix-expression and shall name a class
288 // or function template.
John McCallf7a1a742009-11-24 19:00:30 +0000289 if (S) LookupName(Found, S);
290 ObjectTypeSearchedInScope = true;
291 }
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000292 } else if (isDependent && (!S || ObjectType.isNull())) {
Douglas Gregor2e933882010-01-12 17:06:20 +0000293 // We cannot look into a dependent object type or nested nme
294 // specifier.
Douglas Gregor1fd6d442010-05-21 23:18:07 +0000295 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000296 return;
297 } else {
298 // Perform unqualified name lookup in the current scope.
299 LookupName(Found, S);
300 }
301
Douglas Gregor2e933882010-01-12 17:06:20 +0000302 if (Found.empty() && !isDependent) {
Douglas Gregorbfea2392009-12-31 08:11:17 +0000303 // If we did not find any names, attempt to correct any typos.
304 DeclarationName Name = Found.getLookupName();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000305 Found.clear();
Kaelyn Uhrainf8ec8c92012-01-13 23:10:36 +0000306 // Simple filter callback that, for keywords, only accepts the C++ *_cast
307 CorrectionCandidateCallback FilterCCC;
308 FilterCCC.WantTypeSpecifiers = false;
309 FilterCCC.WantExpressionKeywords = false;
310 FilterCCC.WantRemainingKeywords = false;
311 FilterCCC.WantCXXNamedCasts = true;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000312 if (TypoCorrection Corrected = CorrectTypo(Found.getLookupNameInfo(),
313 Found.getLookupKind(), S, &SS,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000314 FilterCCC, LookupCtx)) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000315 Found.setLookupName(Corrected.getCorrection());
316 if (Corrected.getCorrectionDecl())
317 Found.addDecl(Corrected.getCorrectionDecl());
Douglas Gregor312eadb2011-04-24 05:37:28 +0000318 FilterAcceptableTemplateNames(Found);
John McCallad00b772010-06-16 08:42:20 +0000319 if (!Found.empty()) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000320 std::string CorrectedStr(Corrected.getAsString(getLangOptions()));
321 std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions()));
Douglas Gregorbfea2392009-12-31 08:11:17 +0000322 if (LookupCtx)
323 Diag(Found.getNameLoc(), diag::err_no_member_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000324 << Name << LookupCtx << CorrectedQuotedStr << SS.getRange()
325 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000326 else
327 Diag(Found.getNameLoc(), diag::err_no_template_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000328 << Name << CorrectedQuotedStr
329 << FixItHint::CreateReplacement(Found.getNameLoc(), CorrectedStr);
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000330 if (TemplateDecl *Template = Found.getAsSingle<TemplateDecl>())
331 Diag(Template->getLocation(), diag::note_previous_decl)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000332 << CorrectedQuotedStr;
John McCallad00b772010-06-16 08:42:20 +0000333 }
Douglas Gregorbfea2392009-12-31 08:11:17 +0000334 } else {
Douglas Gregor12eb5d62010-06-29 19:27:42 +0000335 Found.setLookupName(Name);
Douglas Gregorbfea2392009-12-31 08:11:17 +0000336 }
337 }
338
Douglas Gregor312eadb2011-04-24 05:37:28 +0000339 FilterAcceptableTemplateNames(Found);
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000340 if (Found.empty()) {
341 if (isDependent)
342 MemberOfUnknownSpecialization = true;
John McCallf7a1a742009-11-24 19:00:30 +0000343 return;
Douglas Gregorf9f97a02010-07-16 16:54:17 +0000344 }
John McCallf7a1a742009-11-24 19:00:30 +0000345
346 if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope) {
347 // C++ [basic.lookup.classref]p1:
348 // [...] If the lookup in the class of the object expression finds a
349 // template, the name is also looked up in the context of the entire
350 // postfix-expression and [...]
351 //
352 LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(),
353 LookupOrdinaryName);
354 LookupName(FoundOuter, S);
Douglas Gregor312eadb2011-04-24 05:37:28 +0000355 FilterAcceptableTemplateNames(FoundOuter);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000356
John McCallf7a1a742009-11-24 19:00:30 +0000357 if (FoundOuter.empty()) {
358 // - if the name is not found, the name found in the class of the
359 // object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000360 } else if (!FoundOuter.getAsSingle<ClassTemplateDecl>() ||
361 FoundOuter.isAmbiguous()) {
John McCallf7a1a742009-11-24 19:00:30 +0000362 // - if the name is found in the context of the entire
363 // postfix-expression and does not name a class template, the name
364 // found in the class of the object expression is used, otherwise
Douglas Gregora6d1e762011-08-10 21:59:45 +0000365 FoundOuter.clear();
John McCallad00b772010-06-16 08:42:20 +0000366 } else if (!Found.isSuppressingDiagnostics()) {
John McCallf7a1a742009-11-24 19:00:30 +0000367 // - if the name found is a class template, it must refer to the same
368 // entity as the one found in the class of the object expression,
369 // otherwise the program is ill-formed.
370 if (!Found.isSingleResult() ||
371 Found.getFoundDecl()->getCanonicalDecl()
372 != FoundOuter.getFoundDecl()->getCanonicalDecl()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000373 Diag(Found.getNameLoc(),
Jeffrey Yasskin21d07e42010-06-05 01:39:57 +0000374 diag::ext_nested_name_member_ref_lookup_ambiguous)
375 << Found.getLookupName()
376 << ObjectType;
John McCallf7a1a742009-11-24 19:00:30 +0000377 Diag(Found.getRepresentativeDecl()->getLocation(),
378 diag::note_ambig_member_ref_object_type)
379 << ObjectType;
380 Diag(FoundOuter.getFoundDecl()->getLocation(),
381 diag::note_ambig_member_ref_scope);
382
383 // Recover by taking the template that we found in the object
384 // expression's type.
385 }
386 }
387 }
388}
389
John McCall2f841ba2009-12-02 03:53:29 +0000390/// ActOnDependentIdExpression - Handle a dependent id-expression that
391/// was just parsed. This is only possible with an explicit scope
392/// specifier naming a dependent type.
John McCall60d7b3a2010-08-24 06:29:42 +0000393ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000394Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000395 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000396 const DeclarationNameInfo &NameInfo,
John McCall2f841ba2009-12-02 03:53:29 +0000397 bool isAddressOfOperand,
John McCallf7a1a742009-11-24 19:00:30 +0000398 const TemplateArgumentListInfo *TemplateArgs) {
John McCallea1471e2010-05-20 01:18:31 +0000399 DeclContext *DC = getFunctionLevelDeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000400
John McCall2f841ba2009-12-02 03:53:29 +0000401 if (!isAddressOfOperand &&
John McCallea1471e2010-05-20 01:18:31 +0000402 isa<CXXMethodDecl>(DC) &&
403 cast<CXXMethodDecl>(DC)->isInstance()) {
404 QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000405
John McCallf7a1a742009-11-24 19:00:30 +0000406 // Since the 'this' expression is synthesized, we don't need to
407 // perform the double-lookup check.
408 NamedDecl *FirstQualifierInScope = 0;
409
John McCallaa81e162009-12-01 22:10:20 +0000410 return Owned(CXXDependentScopeMemberExpr::Create(Context,
411 /*This*/ 0, ThisType,
412 /*IsArrow*/ true,
John McCallf7a1a742009-11-24 19:00:30 +0000413 /*Op*/ SourceLocation(),
Douglas Gregor7c3179c2011-02-28 18:50:33 +0000414 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000415 TemplateKWLoc,
John McCallf7a1a742009-11-24 19:00:30 +0000416 FirstQualifierInScope,
Abramo Bagnara25777432010-08-11 22:01:17 +0000417 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000418 TemplateArgs));
419 }
420
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000421 return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs);
John McCallf7a1a742009-11-24 19:00:30 +0000422}
423
John McCall60d7b3a2010-08-24 06:29:42 +0000424ExprResult
John McCallf7a1a742009-11-24 19:00:30 +0000425Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000426 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000427 const DeclarationNameInfo &NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000428 const TemplateArgumentListInfo *TemplateArgs) {
429 return Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +0000430 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000431 TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000432 NameInfo,
John McCallf7a1a742009-11-24 19:00:30 +0000433 TemplateArgs));
Douglas Gregord6fb7ef2008-12-18 19:37:40 +0000434}
435
Douglas Gregor72c3f312008-12-05 18:15:24 +0000436/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
437/// that the template parameter 'PrevDecl' is being shadowed by a new
438/// declaration at location Loc. Returns true to indicate that this is
439/// an error, and false otherwise.
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000440void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +0000441 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000442
443 // Microsoft Visual C++ permits template parameters to be shadowed.
Francois Pichet62ec1f22011-09-17 17:15:52 +0000444 if (getLangOptions().MicrosoftExt)
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000445 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000446
447 // C++ [temp.local]p4:
448 // A template-parameter shall not be redeclared within its
449 // scope (including nested scopes).
Mike Stump1eb44332009-09-09 15:08:12 +0000450 Diag(Loc, diag::err_template_param_shadow)
Douglas Gregor72c3f312008-12-05 18:15:24 +0000451 << cast<NamedDecl>(PrevDecl)->getDeclName();
452 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000453 return;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000454}
455
Douglas Gregor2943aed2009-03-03 04:44:36 +0000456/// AdjustDeclIfTemplate - If the given decl happens to be a template, reset
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000457/// the parameter D to reference the templated declaration and return a pointer
458/// to the template declaration. Otherwise, do nothing to D and return null.
John McCalld226f652010-08-21 09:40:31 +0000459TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) {
460 if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) {
461 D = Temp->getTemplatedDecl();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000462 return Temp;
463 }
464 return 0;
465}
466
Douglas Gregorba68eca2011-01-05 17:40:24 +0000467ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion(
468 SourceLocation EllipsisLoc) const {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000469 assert(Kind == Template &&
Douglas Gregorba68eca2011-01-05 17:40:24 +0000470 "Only template template arguments can be pack expansions here");
471 assert(getAsTemplate().get().containsUnexpandedParameterPack() &&
472 "Template template argument pack expansion without packs");
473 ParsedTemplateArgument Result(*this);
474 Result.EllipsisLoc = EllipsisLoc;
475 return Result;
476}
477
Douglas Gregor788cd062009-11-11 01:00:40 +0000478static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef,
479 const ParsedTemplateArgument &Arg) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000480
Douglas Gregor788cd062009-11-11 01:00:40 +0000481 switch (Arg.getKind()) {
482 case ParsedTemplateArgument::Type: {
John McCalla93c9342009-12-07 02:54:59 +0000483 TypeSourceInfo *DI;
Douglas Gregor788cd062009-11-11 01:00:40 +0000484 QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000485 if (!DI)
John McCalla93c9342009-12-07 02:54:59 +0000486 DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation());
Douglas Gregor788cd062009-11-11 01:00:40 +0000487 return TemplateArgumentLoc(TemplateArgument(T), DI);
488 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000489
Douglas Gregor788cd062009-11-11 01:00:40 +0000490 case ParsedTemplateArgument::NonType: {
491 Expr *E = static_cast<Expr *>(Arg.getAsExpr());
492 return TemplateArgumentLoc(TemplateArgument(E), E);
493 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000494
Douglas Gregor788cd062009-11-11 01:00:40 +0000495 case ParsedTemplateArgument::Template: {
John McCall2b5289b2010-08-23 07:28:44 +0000496 TemplateName Template = Arg.getAsTemplate().get();
Douglas Gregor2be29f42011-01-14 23:41:42 +0000497 TemplateArgument TArg;
498 if (Arg.getEllipsisLoc().isValid())
499 TArg = TemplateArgument(Template, llvm::Optional<unsigned int>());
500 else
501 TArg = Template;
502 return TemplateArgumentLoc(TArg,
Douglas Gregorb6744ef2011-03-02 17:09:35 +0000503 Arg.getScopeSpec().getWithLocInContext(
504 SemaRef.Context),
Douglas Gregorba68eca2011-01-05 17:40:24 +0000505 Arg.getLocation(),
506 Arg.getEllipsisLoc());
Douglas Gregor788cd062009-11-11 01:00:40 +0000507 }
508 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000509
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000510 llvm_unreachable("Unhandled parsed template argument");
Douglas Gregor788cd062009-11-11 01:00:40 +0000511}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000512
Douglas Gregor788cd062009-11-11 01:00:40 +0000513/// \brief Translates template arguments as provided by the parser
514/// into template arguments used by semantic analysis.
John McCalld5532b62009-11-23 01:53:49 +0000515void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn,
516 TemplateArgumentListInfo &TemplateArgs) {
Douglas Gregor788cd062009-11-11 01:00:40 +0000517 for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I)
John McCalld5532b62009-11-23 01:53:49 +0000518 TemplateArgs.addArgument(translateTemplateArgument(*this,
519 TemplateArgsIn[I]));
Douglas Gregor788cd062009-11-11 01:00:40 +0000520}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000521
Douglas Gregor72c3f312008-12-05 18:15:24 +0000522/// ActOnTypeParameter - Called when a C++ template type parameter
523/// (e.g., "typename T") has been parsed. Typename specifies whether
524/// the keyword "typename" was used to declare the type parameter
525/// (otherwise, "class" was used), and KeyLoc is the location of the
526/// "class" or "typename" keyword. ParamName is the name of the
527/// parameter (NULL indicates an unnamed template parameter) and
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000528/// ParamNameLoc is the location of the parameter name (if any).
Douglas Gregor72c3f312008-12-05 18:15:24 +0000529/// If the type parameter has a default argument, it will be added
530/// later via ActOnTypeParameterDefault.
John McCalld226f652010-08-21 09:40:31 +0000531Decl *Sema::ActOnTypeParameter(Scope *S, bool Typename, bool Ellipsis,
532 SourceLocation EllipsisLoc,
533 SourceLocation KeyLoc,
534 IdentifierInfo *ParamName,
535 SourceLocation ParamNameLoc,
536 unsigned Depth, unsigned Position,
537 SourceLocation EqualLoc,
John McCallb3d87482010-08-24 05:47:05 +0000538 ParsedType DefaultArg) {
Mike Stump1eb44332009-09-09 15:08:12 +0000539 assert(S->isTemplateParamScope() &&
540 "Template type parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000541 bool Invalid = false;
542
543 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000544 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, ParamNameLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000545 LookupOrdinaryName,
546 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000547 if (PrevDecl && PrevDecl->isTemplateParameter()) {
548 DiagnoseTemplateParameterShadow(ParamNameLoc, PrevDecl);
549 PrevDecl = 0;
550 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000551 }
552
Douglas Gregorddc29e12009-02-06 22:42:48 +0000553 SourceLocation Loc = ParamNameLoc;
554 if (!ParamName)
555 Loc = KeyLoc;
556
Douglas Gregor72c3f312008-12-05 18:15:24 +0000557 TemplateTypeParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000558 = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +0000559 KeyLoc, Loc, Depth, Position, ParamName,
560 Typename, Ellipsis);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000561 Param->setAccess(AS_public);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000562 if (Invalid)
563 Param->setInvalidDecl();
564
565 if (ParamName) {
566 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000567 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000568 IdResolver.AddDecl(Param);
569 }
570
Douglas Gregor61c4d282011-01-05 15:48:55 +0000571 // C++0x [temp.param]p9:
572 // A default template-argument may be specified for any kind of
573 // template-parameter that is not a template parameter pack.
574 if (DefaultArg && Ellipsis) {
575 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
576 DefaultArg = ParsedType();
577 }
578
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000579 // Handle the default argument, if provided.
580 if (DefaultArg) {
581 TypeSourceInfo *DefaultTInfo;
582 GetTypeFromParser(DefaultArg, &DefaultTInfo);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000583
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000584 assert(DefaultTInfo && "expected source information for type");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000585
Douglas Gregor6f526752010-12-16 08:48:57 +0000586 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000587 if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo,
Douglas Gregor6f526752010-12-16 08:48:57 +0000588 UPPC_DefaultArgument))
589 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000590
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000591 // Check the template argument itself.
592 if (CheckTemplateArgument(Param, DefaultTInfo)) {
593 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000594 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000595 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000596
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000597 Param->setDefaultArgument(DefaultTInfo, false);
598 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000599
John McCalld226f652010-08-21 09:40:31 +0000600 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000601}
602
Douglas Gregor2943aed2009-03-03 04:44:36 +0000603/// \brief Check that the type of a non-type template parameter is
604/// well-formed.
605///
606/// \returns the (possibly-promoted) parameter type if valid;
607/// otherwise, produces a diagnostic and returns a NULL type.
Mike Stump1eb44332009-09-09 15:08:12 +0000608QualType
Douglas Gregor2943aed2009-03-03 04:44:36 +0000609Sema::CheckNonTypeTemplateParameterType(QualType T, SourceLocation Loc) {
Douglas Gregora481ec42010-05-23 19:57:01 +0000610 // We don't allow variably-modified types as the type of non-type template
611 // parameters.
612 if (T->isVariablyModifiedType()) {
613 Diag(Loc, diag::err_variably_modified_nontype_template_param)
614 << T;
615 return QualType();
616 }
617
Douglas Gregor2943aed2009-03-03 04:44:36 +0000618 // C++ [temp.param]p4:
619 //
620 // A non-type template-parameter shall have one of the following
621 // (optionally cv-qualified) types:
622 //
623 // -- integral or enumeration type,
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000624 if (T->isIntegralOrEnumerationType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000625 // -- pointer to object or pointer to function,
Eli Friedman13578692010-08-05 02:49:48 +0000626 T->isPointerType() ||
Mike Stump1eb44332009-09-09 15:08:12 +0000627 // -- reference to object or reference to function,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000628 T->isReferenceType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000629 // -- pointer to member,
Douglas Gregor2943aed2009-03-03 04:44:36 +0000630 T->isMemberPointerType() ||
Douglas Gregor84ee2ee2011-05-21 23:15:46 +0000631 // -- std::nullptr_t.
632 T->isNullPtrType() ||
Douglas Gregor2943aed2009-03-03 04:44:36 +0000633 // If T is a dependent type, we can't do the check now, so we
634 // assume that it is well-formed.
635 T->isDependentType())
636 return T;
637 // C++ [temp.param]p8:
638 //
639 // A non-type template-parameter of type "array of T" or
640 // "function returning T" is adjusted to be of type "pointer to
641 // T" or "pointer to function returning T", respectively.
642 else if (T->isArrayType())
643 // FIXME: Keep the type prior to promotion?
644 return Context.getArrayDecayedType(T);
645 else if (T->isFunctionType())
646 // FIXME: Keep the type prior to promotion?
647 return Context.getPointerType(T);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000648
Douglas Gregor2943aed2009-03-03 04:44:36 +0000649 Diag(Loc, diag::err_template_nontype_parm_bad_type)
650 << T;
651
652 return QualType();
653}
654
John McCalld226f652010-08-21 09:40:31 +0000655Decl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
656 unsigned Depth,
657 unsigned Position,
658 SourceLocation EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +0000659 Expr *Default) {
John McCallbf1a0282010-06-04 23:28:52 +0000660 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
661 QualType T = TInfo->getType();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000662
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000663 assert(S->isTemplateParamScope() &&
664 "Non-type template parameter not in template parameter scope!");
Douglas Gregor72c3f312008-12-05 18:15:24 +0000665 bool Invalid = false;
666
667 IdentifierInfo *ParamName = D.getIdentifier();
668 if (ParamName) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000669 NamedDecl *PrevDecl = LookupSingleName(S, ParamName, D.getIdentifierLoc(),
Douglas Gregorc0b39642010-04-15 23:40:53 +0000670 LookupOrdinaryName,
671 ForRedeclaration);
Douglas Gregorcb8f9512011-10-20 17:58:49 +0000672 if (PrevDecl && PrevDecl->isTemplateParameter()) {
673 DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), PrevDecl);
674 PrevDecl = 0;
675 }
Douglas Gregor72c3f312008-12-05 18:15:24 +0000676 }
677
Douglas Gregor4d2abba2010-12-16 15:36:43 +0000678 T = CheckNonTypeTemplateParameterType(T, D.getIdentifierLoc());
679 if (T.isNull()) {
Douglas Gregor2943aed2009-03-03 04:44:36 +0000680 T = Context.IntTy; // Recover with an 'int' type.
Douglas Gregorceef30c2009-03-09 16:46:39 +0000681 Invalid = true;
682 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000683
Douglas Gregor10738d32010-12-23 23:51:58 +0000684 bool IsParameterPack = D.hasEllipsis();
Douglas Gregor72c3f312008-12-05 18:15:24 +0000685 NonTypeTemplateParmDecl *Param
John McCall7a9813c2010-01-22 00:28:27 +0000686 = NonTypeTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +0000687 D.getSourceRange().getBegin(),
John McCall7a9813c2010-01-22 00:28:27 +0000688 D.getIdentifierLoc(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000689 Depth, Position, ParamName, T,
Douglas Gregor10738d32010-12-23 23:51:58 +0000690 IsParameterPack, TInfo);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000691 Param->setAccess(AS_public);
692
Douglas Gregor72c3f312008-12-05 18:15:24 +0000693 if (Invalid)
694 Param->setInvalidDecl();
695
696 if (D.getIdentifier()) {
697 // Add the template parameter into the current scope.
John McCalld226f652010-08-21 09:40:31 +0000698 S->AddDecl(Param);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000699 IdResolver.AddDecl(Param);
700 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000701
Douglas Gregor61c4d282011-01-05 15:48:55 +0000702 // C++0x [temp.param]p9:
703 // A default template-argument may be specified for any kind of
704 // template-parameter that is not a template parameter pack.
705 if (Default && IsParameterPack) {
706 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
707 Default = 0;
708 }
709
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000710 // Check the well-formedness of the default template argument, if provided.
Douglas Gregor10738d32010-12-23 23:51:58 +0000711 if (Default) {
Douglas Gregor6f526752010-12-16 08:48:57 +0000712 // Check for unexpanded parameter packs.
713 if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument))
714 return Param;
715
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000716 TemplateArgument Converted;
John Wiegley429bb272011-04-08 18:41:53 +0000717 ExprResult DefaultRes = CheckTemplateArgument(Param, Param->getType(), Default, Converted);
718 if (DefaultRes.isInvalid()) {
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000719 Param->setInvalidDecl();
John McCalld226f652010-08-21 09:40:31 +0000720 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000721 }
John Wiegley429bb272011-04-08 18:41:53 +0000722 Default = DefaultRes.take();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000723
John McCall9ae2f072010-08-23 23:25:46 +0000724 Param->setDefaultArgument(Default, false);
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000725 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000726
John McCalld226f652010-08-21 09:40:31 +0000727 return Param;
Douglas Gregor72c3f312008-12-05 18:15:24 +0000728}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000729
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000730/// ActOnTemplateTemplateParameter - Called when a C++ template template
731/// parameter (e.g. T in template <template <typename> class T> class array)
732/// has been parsed. S is the current scope.
John McCalld226f652010-08-21 09:40:31 +0000733Decl *Sema::ActOnTemplateTemplateParameter(Scope* S,
734 SourceLocation TmpLoc,
Richard Trieu90ab75b2011-09-09 03:18:59 +0000735 TemplateParameterList *Params,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000736 SourceLocation EllipsisLoc,
John McCalld226f652010-08-21 09:40:31 +0000737 IdentifierInfo *Name,
738 SourceLocation NameLoc,
739 unsigned Depth,
740 unsigned Position,
741 SourceLocation EqualLoc,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000742 ParsedTemplateArgument Default) {
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000743 assert(S->isTemplateParamScope() &&
744 "Template template parameter not in template parameter scope!");
745
746 // Construct the parameter object.
Douglas Gregor61c4d282011-01-05 15:48:55 +0000747 bool IsParameterPack = EllipsisLoc.isValid();
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000748 TemplateTemplateParmDecl *Param =
John McCall7a9813c2010-01-22 00:28:27 +0000749 TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000750 NameLoc.isInvalid()? TmpLoc : NameLoc,
751 Depth, Position, IsParameterPack,
Douglas Gregor61c4d282011-01-05 15:48:55 +0000752 Name, Params);
Douglas Gregor9a299e02011-03-04 17:52:15 +0000753 Param->setAccess(AS_public);
754
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000755 // If the template template parameter has a name, then link the identifier
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000756 // into the scope and lookup mechanisms.
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000757 if (Name) {
John McCalld226f652010-08-21 09:40:31 +0000758 S->AddDecl(Param);
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000759 IdResolver.AddDecl(Param);
760 }
761
Douglas Gregor6f526752010-12-16 08:48:57 +0000762 if (Params->size() == 0) {
763 Diag(Param->getLocation(), diag::err_template_template_parm_no_parms)
764 << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc());
765 Param->setInvalidDecl();
766 }
767
Douglas Gregor61c4d282011-01-05 15:48:55 +0000768 // C++0x [temp.param]p9:
769 // A default template-argument may be specified for any kind of
770 // template-parameter that is not a template parameter pack.
771 if (IsParameterPack && !Default.isInvalid()) {
772 Diag(EqualLoc, diag::err_template_param_pack_default_arg);
773 Default = ParsedTemplateArgument();
774 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000775
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000776 if (!Default.isInvalid()) {
777 // Check only that we have a template template argument. We don't want to
778 // try to check well-formedness now, because our template template parameter
779 // might have dependent types in its template parameters, which we wouldn't
780 // be able to match now.
781 //
782 // If none of the template template parameter's template arguments mention
783 // other template parameters, we could actually perform more checking here.
784 // However, it isn't worth doing.
785 TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default);
786 if (DefaultArg.getArgument().getAsTemplate().isNull()) {
787 Diag(DefaultArg.getLocation(), diag::err_template_arg_not_class_template)
788 << DefaultArg.getSourceRange();
John McCalld226f652010-08-21 09:40:31 +0000789 return Param;
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000790 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000791
Douglas Gregor6f526752010-12-16 08:48:57 +0000792 // Check for unexpanded parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000793 if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(),
Douglas Gregor6f526752010-12-16 08:48:57 +0000794 DefaultArg.getArgument().getAsTemplate(),
795 UPPC_DefaultArgument))
796 return Param;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000797
Douglas Gregorbb3310a2010-07-01 00:00:45 +0000798 Param->setDefaultArgument(DefaultArg, false);
Douglas Gregord684b002009-02-10 19:49:53 +0000799 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000800
John McCalld226f652010-08-21 09:40:31 +0000801 return Param;
Douglas Gregord684b002009-02-10 19:49:53 +0000802}
803
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000804/// ActOnTemplateParameterList - Builds a TemplateParameterList that
805/// contains the template parameters in Params/NumParams.
Richard Trieu90ab75b2011-09-09 03:18:59 +0000806TemplateParameterList *
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000807Sema::ActOnTemplateParameterList(unsigned Depth,
808 SourceLocation ExportLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000809 SourceLocation TemplateLoc,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000810 SourceLocation LAngleLoc,
John McCalld226f652010-08-21 09:40:31 +0000811 Decl **Params, unsigned NumParams,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000812 SourceLocation RAngleLoc) {
813 if (ExportLoc.isValid())
Douglas Gregor51ffb0c2009-11-25 18:55:14 +0000814 Diag(ExportLoc, diag::warn_template_export_unsupported);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000815
Douglas Gregorddc29e12009-02-06 22:42:48 +0000816 return TemplateParameterList::Create(Context, TemplateLoc, LAngleLoc,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000817 (NamedDecl**)Params, NumParams,
Douglas Gregorbf4ea562009-09-15 16:23:51 +0000818 RAngleLoc);
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000819}
Douglas Gregoraaba5e32009-02-04 19:02:06 +0000820
John McCallb6217662010-03-15 10:12:16 +0000821static void SetNestedNameSpecifier(TagDecl *T, const CXXScopeSpec &SS) {
822 if (SS.isSet())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000823 T->setQualifierInfo(SS.getWithLocInContext(T->getASTContext()));
John McCallb6217662010-03-15 10:12:16 +0000824}
825
John McCallf312b1e2010-08-26 23:41:50 +0000826DeclResult
John McCall0f434ec2009-07-31 02:45:11 +0000827Sema::CheckClassTemplate(Scope *S, unsigned TagSpec, TagUseKind TUK,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +0000828 SourceLocation KWLoc, CXXScopeSpec &SS,
Douglas Gregorddc29e12009-02-06 22:42:48 +0000829 IdentifierInfo *Name, SourceLocation NameLoc,
830 AttributeList *Attr,
Douglas Gregor05396e22009-08-25 17:23:04 +0000831 TemplateParameterList *TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +0000832 AccessSpecifier AS, SourceLocation ModulePrivateLoc,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +0000833 unsigned NumOuterTemplateParamLists,
834 TemplateParameterList** OuterTemplateParamLists) {
Mike Stump1eb44332009-09-09 15:08:12 +0000835 assert(TemplateParams && TemplateParams->size() > 0 &&
Douglas Gregor05396e22009-08-25 17:23:04 +0000836 "No template parameters");
John McCall0f434ec2009-07-31 02:45:11 +0000837 assert(TUK != TUK_Reference && "Can only declare or define class templates");
Douglas Gregord684b002009-02-10 19:49:53 +0000838 bool Invalid = false;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000839
840 // Check that we can declare a template here.
Douglas Gregor05396e22009-08-25 17:23:04 +0000841 if (CheckTemplateDeclScope(S, TemplateParams))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000842 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000843
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000844 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
845 assert(Kind != TTK_Enum && "can't build template of enumerated type");
Douglas Gregorddc29e12009-02-06 22:42:48 +0000846
847 // There is no such thing as an unnamed class template.
848 if (!Name) {
849 Diag(KWLoc, diag::err_template_unnamed_class);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000850 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000851 }
852
853 // Find any previous declaration with this name.
Douglas Gregor05396e22009-08-25 17:23:04 +0000854 DeclContext *SemanticContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000855 LookupResult Previous(*this, Name, NameLoc, LookupOrdinaryName,
John McCall7d384dd2009-11-18 07:57:50 +0000856 ForRedeclaration);
Douglas Gregor05396e22009-08-25 17:23:04 +0000857 if (SS.isNotEmpty() && !SS.isInvalid()) {
858 SemanticContext = computeDeclContext(SS, true);
859 if (!SemanticContext) {
860 // FIXME: Produce a reasonable diagnostic here
861 return true;
862 }
Mike Stump1eb44332009-09-09 15:08:12 +0000863
John McCall77bb1aa2010-05-01 00:40:08 +0000864 if (RequireCompleteDeclContext(SS, SemanticContext))
865 return true;
866
Douglas Gregor20606502011-10-14 15:31:12 +0000867 // If we're adding a template to a dependent context, we may need to
868 // rebuilding some of the types used within the template parameter list,
869 // now that we know what the current instantiation is.
870 if (SemanticContext->isDependentContext()) {
871 ContextRAII SavedContext(*this, SemanticContext);
872 if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams))
873 Invalid = true;
874 }
875
John McCalla24dc2e2009-11-17 02:14:36 +0000876 LookupQualifiedName(Previous, SemanticContext);
Douglas Gregor05396e22009-08-25 17:23:04 +0000877 } else {
878 SemanticContext = CurContext;
John McCalla24dc2e2009-11-17 02:14:36 +0000879 LookupName(Previous, S);
Douglas Gregor05396e22009-08-25 17:23:04 +0000880 }
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Douglas Gregor57265e32010-04-12 16:00:01 +0000882 if (Previous.isAmbiguous())
883 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000884
Douglas Gregorddc29e12009-02-06 22:42:48 +0000885 NamedDecl *PrevDecl = 0;
886 if (Previous.begin() != Previous.end())
Douglas Gregor57265e32010-04-12 16:00:01 +0000887 PrevDecl = (*Previous.begin())->getUnderlyingDecl();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000888
Douglas Gregorddc29e12009-02-06 22:42:48 +0000889 // If there is a previous declaration with the same name, check
890 // whether this is a valid redeclaration.
Mike Stump1eb44332009-09-09 15:08:12 +0000891 ClassTemplateDecl *PrevClassTemplate
Douglas Gregorddc29e12009-02-06 22:42:48 +0000892 = dyn_cast_or_null<ClassTemplateDecl>(PrevDecl);
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000893
894 // We may have found the injected-class-name of a class template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000895 // class template partial specialization, or class template specialization.
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000896 // In these cases, grab the template that is being defined or specialized.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000897 if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) &&
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000898 cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) {
899 PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000900 PrevClassTemplate
Douglas Gregord7e5bdb2009-10-09 21:11:42 +0000901 = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate();
902 if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) {
903 PrevClassTemplate
904 = cast<ClassTemplateSpecializationDecl>(PrevDecl)
905 ->getSpecializedTemplate();
906 }
907 }
908
John McCall65c49462009-12-18 11:25:59 +0000909 if (TUK == TUK_Friend) {
John McCalle129d442009-12-17 23:21:11 +0000910 // C++ [namespace.memdef]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000911 // [...] When looking for a prior declaration of a class or a function
912 // declared as a friend, and when the name of the friend class or
John McCalle129d442009-12-17 23:21:11 +0000913 // function is neither a qualified name nor a template-id, scopes outside
914 // the innermost enclosing namespace scope are not considered.
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000915 if (!SS.isSet()) {
916 DeclContext *OutermostContext = CurContext;
917 while (!OutermostContext->isFileContext())
918 OutermostContext = OutermostContext->getLookupParent();
John McCall65c49462009-12-18 11:25:59 +0000919
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000920 if (PrevDecl &&
921 (OutermostContext->Equals(PrevDecl->getDeclContext()) ||
922 OutermostContext->Encloses(PrevDecl->getDeclContext()))) {
923 SemanticContext = PrevDecl->getDeclContext();
924 } else {
925 // Declarations in outer scopes don't matter. However, the outermost
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000926 // context we computed is the semantic context for our new
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000927 // declaration.
928 PrevDecl = PrevClassTemplate = 0;
929 SemanticContext = OutermostContext;
930 }
John McCalle129d442009-12-17 23:21:11 +0000931 }
Douglas Gregorc1c9df72010-04-18 17:37:40 +0000932
John McCalle129d442009-12-17 23:21:11 +0000933 if (CurContext->isDependentContext()) {
934 // If this is a dependent context, we don't want to link the friend
935 // class template to the template in scope, because that would perform
936 // checking of the template parameter lists that can't be performed
937 // until the outer context is instantiated.
938 PrevDecl = PrevClassTemplate = 0;
939 }
940 } else if (PrevDecl && !isDeclInScope(PrevDecl, SemanticContext, S))
941 PrevDecl = PrevClassTemplate = 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +0000942
Douglas Gregorddc29e12009-02-06 22:42:48 +0000943 if (PrevClassTemplate) {
944 // Ensure that the template parameter lists are compatible.
945 if (!TemplateParameterListsAreEqual(TemplateParams,
946 PrevClassTemplate->getTemplateParameters(),
Douglas Gregorfb898e12009-11-12 16:20:59 +0000947 /*Complain=*/true,
948 TPL_TemplateMatch))
Douglas Gregor212e81c2009-03-25 00:13:59 +0000949 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000950
951 // C++ [temp.class]p4:
952 // In a redeclaration, partial specialization, explicit
953 // specialization or explicit instantiation of a class template,
954 // the class-key shall agree in kind with the original class
955 // template declaration (7.1.5.3).
956 RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl();
Richard Trieubbf34c02011-06-10 03:11:26 +0000957 if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind,
958 TUK == TUK_Definition, KWLoc, *Name)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000959 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +0000960 << Name
Douglas Gregor849b2432010-03-31 17:46:05 +0000961 << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName());
Douglas Gregorddc29e12009-02-06 22:42:48 +0000962 Diag(PrevRecordDecl->getLocation(), diag::note_previous_use);
Douglas Gregora3a83512009-04-01 23:51:29 +0000963 Kind = PrevRecordDecl->getTagKind();
Douglas Gregorddc29e12009-02-06 22:42:48 +0000964 }
965
Douglas Gregorddc29e12009-02-06 22:42:48 +0000966 // Check for redefinition of this class template.
John McCall0f434ec2009-07-31 02:45:11 +0000967 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +0000968 if (TagDecl *Def = PrevRecordDecl->getDefinition()) {
Douglas Gregorddc29e12009-02-06 22:42:48 +0000969 Diag(NameLoc, diag::err_redefinition) << Name;
970 Diag(Def->getLocation(), diag::note_previous_definition);
971 // FIXME: Would it make sense to try to "forget" the previous
972 // definition, as part of error recovery?
Douglas Gregor212e81c2009-03-25 00:13:59 +0000973 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000974 }
Douglas Gregor6311d2b2011-09-09 18:32:39 +0000975 }
Douglas Gregorddc29e12009-02-06 22:42:48 +0000976 } else if (PrevDecl && PrevDecl->isTemplateParameter()) {
977 // Maybe we will complain about the shadowed template parameter.
978 DiagnoseTemplateParameterShadow(NameLoc, PrevDecl);
979 // Just pretend that we didn't see the previous declaration.
980 PrevDecl = 0;
981 } else if (PrevDecl) {
982 // C++ [temp]p5:
983 // A class template shall not have the same name as any other
984 // template, class, function, object, enumeration, enumerator,
985 // namespace, or type in the same scope (3.3), except as specified
986 // in (14.5.4).
987 Diag(NameLoc, diag::err_redefinition_different_kind) << Name;
988 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor212e81c2009-03-25 00:13:59 +0000989 return true;
Douglas Gregorddc29e12009-02-06 22:42:48 +0000990 }
991
Douglas Gregord684b002009-02-10 19:49:53 +0000992 // Check the template parameter list of this declaration, possibly
993 // merging in the template parameter list from the previous class
994 // template declaration.
995 if (CheckTemplateParameterList(TemplateParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +0000996 PrevClassTemplate? PrevClassTemplate->getTemplateParameters() : 0,
Douglas Gregord89d86f2011-02-04 04:20:44 +0000997 (SS.isSet() && SemanticContext &&
Douglas Gregor461bf2e2011-02-04 12:22:53 +0000998 SemanticContext->isRecord() &&
999 SemanticContext->isDependentContext())
Douglas Gregord89d86f2011-02-04 04:20:44 +00001000 ? TPC_ClassTemplateMember
1001 : TPC_ClassTemplate))
Douglas Gregord684b002009-02-10 19:49:53 +00001002 Invalid = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Douglas Gregor57265e32010-04-12 16:00:01 +00001004 if (SS.isSet()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001005 // If the name of the template was qualified, we must be defining the
Douglas Gregor57265e32010-04-12 16:00:01 +00001006 // template out-of-line.
1007 if (!SS.isInvalid() && !Invalid && !PrevClassTemplate &&
Douglas Gregorea9f54a2011-11-01 21:35:16 +00001008 !(TUK == TUK_Friend && CurContext->isDependentContext())) {
Douglas Gregor57265e32010-04-12 16:00:01 +00001009 Diag(NameLoc, diag::err_member_def_does_not_match)
1010 << Name << SemanticContext << SS.getRange();
Douglas Gregorea9f54a2011-11-01 21:35:16 +00001011 Invalid = true;
1012 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001013 }
1014
Mike Stump1eb44332009-09-09 15:08:12 +00001015 CXXRecordDecl *NewClass =
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00001016 CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name,
Mike Stump1eb44332009-09-09 15:08:12 +00001017 PrevClassTemplate?
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001018 PrevClassTemplate->getTemplatedDecl() : 0,
1019 /*DelayTypeCreation=*/true);
John McCallb6217662010-03-15 10:12:16 +00001020 SetNestedNameSpecifier(NewClass, SS);
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00001021 if (NumOuterTemplateParamLists > 0)
1022 NewClass->setTemplateParameterListsInfo(Context,
1023 NumOuterTemplateParamLists,
1024 OuterTemplateParamLists);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001025
Eli Friedman572ae0a2012-02-10 02:02:21 +00001026 // Add alignment attributes if necessary; these attributes are checked when
1027 // the ASTContext lays out the structure.
1028 AddAlignmentAttributesForRecord(NewClass);
1029 AddMsStructLayoutForRecord(NewClass);
1030
Douglas Gregorddc29e12009-02-06 22:42:48 +00001031 ClassTemplateDecl *NewTemplate
1032 = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc,
1033 DeclarationName(Name), TemplateParams,
Douglas Gregor5953d8b2009-03-19 17:26:29 +00001034 NewClass, PrevClassTemplate);
Douglas Gregorbefc20e2009-03-26 00:10:35 +00001035 NewClass->setDescribedClassTemplate(NewTemplate);
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001036
Douglas Gregor2ccd89c2011-12-20 18:11:52 +00001037 if (ModulePrivateLoc.isValid())
Douglas Gregor6311d2b2011-09-09 18:32:39 +00001038 NewTemplate->setModulePrivate();
Douglas Gregor8d267c52011-09-09 02:06:17 +00001039
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001040 // Build the type for the class template declaration now.
Douglas Gregor24bae922010-07-08 18:37:38 +00001041 QualType T = NewTemplate->getInjectedClassNameSpecialization();
John McCall3cb0ebd2010-03-10 03:28:59 +00001042 T = Context.getInjectedClassNameType(NewClass, T);
Douglas Gregoraafc0cc2009-05-15 19:11:46 +00001043 assert(T->isDependentType() && "Class template type is not dependent?");
1044 (void)T;
1045
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001046 // If we are providing an explicit specialization of a member that is a
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001047 // class template, make a note of that.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001048 if (PrevClassTemplate &&
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001049 PrevClassTemplate->getInstantiatedFromMemberTemplate())
1050 PrevClassTemplate->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001051
Anders Carlsson4cbe82c2009-03-26 01:24:28 +00001052 // Set the access specifier.
Douglas Gregord85bea22009-09-26 06:47:28 +00001053 if (!Invalid && TUK != TUK_Friend)
John McCall05b23ea2009-09-14 21:59:20 +00001054 SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS);
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Douglas Gregorddc29e12009-02-06 22:42:48 +00001056 // Set the lexical context of these templates
1057 NewClass->setLexicalDeclContext(CurContext);
1058 NewTemplate->setLexicalDeclContext(CurContext);
1059
John McCall0f434ec2009-07-31 02:45:11 +00001060 if (TUK == TUK_Definition)
Douglas Gregorddc29e12009-02-06 22:42:48 +00001061 NewClass->startDefinition();
1062
1063 if (Attr)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001064 ProcessDeclAttributeList(S, NewClass, Attr);
Douglas Gregorddc29e12009-02-06 22:42:48 +00001065
John McCall05b23ea2009-09-14 21:59:20 +00001066 if (TUK != TUK_Friend)
1067 PushOnScopeChains(NewTemplate, S);
1068 else {
Douglas Gregord85bea22009-09-26 06:47:28 +00001069 if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) {
John McCall05b23ea2009-09-14 21:59:20 +00001070 NewTemplate->setAccess(PrevClassTemplate->getAccess());
Douglas Gregord85bea22009-09-26 06:47:28 +00001071 NewClass->setAccess(PrevClassTemplate->getAccess());
1072 }
John McCall05b23ea2009-09-14 21:59:20 +00001073
Douglas Gregord85bea22009-09-26 06:47:28 +00001074 NewTemplate->setObjectOfFriendDecl(/* PreviouslyDeclared = */
1075 PrevClassTemplate != NULL);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001076
John McCall05b23ea2009-09-14 21:59:20 +00001077 // Friend templates are visible in fairly strange ways.
1078 if (!CurContext->isDependentContext()) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00001079 DeclContext *DC = SemanticContext->getRedeclContext();
John McCall05b23ea2009-09-14 21:59:20 +00001080 DC->makeDeclVisibleInContext(NewTemplate, /* Recoverable = */ false);
1081 if (Scope *EnclosingScope = getScopeForDeclContext(S, DC))
1082 PushOnScopeChains(NewTemplate, EnclosingScope,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001083 /* AddToContext = */ false);
John McCall05b23ea2009-09-14 21:59:20 +00001084 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001085
Douglas Gregord85bea22009-09-26 06:47:28 +00001086 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
1087 NewClass->getLocation(),
1088 NewTemplate,
1089 /*FIXME:*/NewClass->getLocation());
1090 Friend->setAccess(AS_public);
1091 CurContext->addDecl(Friend);
John McCall05b23ea2009-09-14 21:59:20 +00001092 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00001093
Douglas Gregord684b002009-02-10 19:49:53 +00001094 if (Invalid) {
1095 NewTemplate->setInvalidDecl();
1096 NewClass->setInvalidDecl();
1097 }
John McCalld226f652010-08-21 09:40:31 +00001098 return NewTemplate;
Douglas Gregorddc29e12009-02-06 22:42:48 +00001099}
1100
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001101/// \brief Diagnose the presence of a default template argument on a
1102/// template parameter, which is ill-formed in certain contexts.
1103///
1104/// \returns true if the default template argument should be dropped.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001105static bool DiagnoseDefaultTemplateArgument(Sema &S,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001106 Sema::TemplateParamListContext TPC,
1107 SourceLocation ParamLoc,
1108 SourceRange DefArgRange) {
1109 switch (TPC) {
1110 case Sema::TPC_ClassTemplate:
Richard Smith3e4c6c42011-05-05 21:57:07 +00001111 case Sema::TPC_TypeAliasTemplate:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001112 return false;
1113
1114 case Sema::TPC_FunctionTemplate:
Douglas Gregord89d86f2011-02-04 04:20:44 +00001115 case Sema::TPC_FriendFunctionTemplateDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001116 // C++ [temp.param]p9:
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001117 // A default template-argument shall not be specified in a
1118 // function template declaration or a function template
1119 // definition [...]
Douglas Gregord89d86f2011-02-04 04:20:44 +00001120 // If a friend function template declaration specifies a default
1121 // template-argument, that declaration shall be a definition and shall be
1122 // the only declaration of the function template in the translation unit.
1123 // (C++98/03 doesn't have this wording; see DR226).
Richard Smithebaf0e62011-10-18 20:49:44 +00001124 S.Diag(ParamLoc, S.getLangOptions().CPlusPlus0x ?
1125 diag::warn_cxx98_compat_template_parameter_default_in_function_template
1126 : diag::ext_template_parameter_default_in_function_template)
1127 << DefArgRange;
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001128 return false;
1129
1130 case Sema::TPC_ClassTemplateMember:
1131 // C++0x [temp.param]p9:
1132 // A default template-argument shall not be specified in the
1133 // template-parameter-lists of the definition of a member of a
1134 // class template that appears outside of the member's class.
1135 S.Diag(ParamLoc, diag::err_template_parameter_default_template_member)
1136 << DefArgRange;
1137 return true;
1138
1139 case Sema::TPC_FriendFunctionTemplate:
1140 // C++ [temp.param]p9:
1141 // A default template-argument shall not be specified in a
1142 // friend template declaration.
1143 S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template)
1144 << DefArgRange;
1145 return true;
1146
1147 // FIXME: C++0x [temp.param]p9 allows default template-arguments
1148 // for friend function templates if there is only a single
1149 // declaration (and it is a definition). Strange!
1150 }
1151
David Blaikie7530c032012-01-17 06:56:22 +00001152 llvm_unreachable("Invalid TemplateParamListContext!");
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001153}
1154
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001155/// \brief Check for unexpanded parameter packs within the template parameters
1156/// of a template template parameter, recursively.
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00001157static bool DiagnoseUnexpandedParameterPacks(Sema &S,
1158 TemplateTemplateParmDecl *TTP) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001159 TemplateParameterList *Params = TTP->getTemplateParameters();
1160 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
1161 NamedDecl *P = Params->getParam(I);
1162 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001163 if (S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001164 NTTP->getTypeSourceInfo(),
1165 Sema::UPPC_NonTypeTemplateParameterType))
1166 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001167
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001168 continue;
1169 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001170
1171 if (TemplateTemplateParmDecl *InnerTTP
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001172 = dyn_cast<TemplateTemplateParmDecl>(P))
1173 if (DiagnoseUnexpandedParameterPacks(S, InnerTTP))
1174 return true;
1175 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001176
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001177 return false;
1178}
1179
Douglas Gregord684b002009-02-10 19:49:53 +00001180/// \brief Checks the validity of a template parameter list, possibly
1181/// considering the template parameter list from a previous
1182/// declaration.
1183///
1184/// If an "old" template parameter list is provided, it must be
1185/// equivalent (per TemplateParameterListsAreEqual) to the "new"
1186/// template parameter list.
1187///
1188/// \param NewParams Template parameter list for a new template
1189/// declaration. This template parameter list will be updated with any
1190/// default arguments that are carried through from the previous
1191/// template parameter list.
1192///
1193/// \param OldParams If provided, template parameter list from a
1194/// previous declaration of the same template. Default template
1195/// arguments will be merged from the old template parameter list to
1196/// the new template parameter list.
1197///
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001198/// \param TPC Describes the context in which we are checking the given
1199/// template parameter list.
1200///
Douglas Gregord684b002009-02-10 19:49:53 +00001201/// \returns true if an error occurred, false otherwise.
1202bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams,
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001203 TemplateParameterList *OldParams,
1204 TemplateParamListContext TPC) {
Douglas Gregord684b002009-02-10 19:49:53 +00001205 bool Invalid = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Douglas Gregord684b002009-02-10 19:49:53 +00001207 // C++ [temp.param]p10:
1208 // The set of default template-arguments available for use with a
1209 // template declaration or definition is obtained by merging the
1210 // default arguments from the definition (if in scope) and all
1211 // declarations in scope in the same way default function
1212 // arguments are (8.3.6).
1213 bool SawDefaultArgument = false;
1214 SourceLocation PreviousDefaultArgLoc;
Douglas Gregorc15cb382009-02-09 23:23:08 +00001215
Mike Stump1a35fde2009-02-11 23:03:27 +00001216 // Dummy initialization to avoid warnings.
Douglas Gregor1bc69132009-02-11 20:46:19 +00001217 TemplateParameterList::iterator OldParam = NewParams->end();
Douglas Gregord684b002009-02-10 19:49:53 +00001218 if (OldParams)
1219 OldParam = OldParams->begin();
1220
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001221 bool RemoveDefaultArguments = false;
Douglas Gregord684b002009-02-10 19:49:53 +00001222 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1223 NewParamEnd = NewParams->end();
1224 NewParam != NewParamEnd; ++NewParam) {
1225 // Variables used to diagnose redundant default arguments
1226 bool RedundantDefaultArg = false;
1227 SourceLocation OldDefaultLoc;
1228 SourceLocation NewDefaultLoc;
1229
David Blaikie1368e582011-10-19 05:19:50 +00001230 // Variable used to diagnose missing default arguments
Douglas Gregord684b002009-02-10 19:49:53 +00001231 bool MissingDefaultArg = false;
1232
David Blaikie1368e582011-10-19 05:19:50 +00001233 // Variable used to diagnose non-final parameter packs
1234 bool SawParameterPack = false;
Anders Carlsson49d25572009-06-12 23:20:15 +00001235
Douglas Gregord684b002009-02-10 19:49:53 +00001236 if (TemplateTypeParmDecl *NewTypeParm
1237 = dyn_cast<TemplateTypeParmDecl>(*NewParam)) {
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001238 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001239 if (NewTypeParm->hasDefaultArgument() &&
1240 DiagnoseDefaultTemplateArgument(*this, TPC,
1241 NewTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001242 NewTypeParm->getDefaultArgumentInfo()->getTypeLoc()
Abramo Bagnarabd054db2010-05-20 10:00:11 +00001243 .getSourceRange()))
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001244 NewTypeParm->removeDefaultArgument();
1245
1246 // Merge default arguments for template type parameters.
Mike Stump1eb44332009-09-09 15:08:12 +00001247 TemplateTypeParmDecl *OldTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001248 = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001249
Anders Carlsson49d25572009-06-12 23:20:15 +00001250 if (NewTypeParm->isParameterPack()) {
1251 assert(!NewTypeParm->hasDefaultArgument() &&
1252 "Parameter packs can't have a default argument!");
1253 SawParameterPack = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001254 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument() &&
John McCall833ca992009-10-29 08:12:44 +00001255 NewTypeParm->hasDefaultArgument()) {
Douglas Gregord684b002009-02-10 19:49:53 +00001256 OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc();
1257 NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc();
1258 SawDefaultArgument = true;
1259 RedundantDefaultArg = true;
1260 PreviousDefaultArgLoc = NewDefaultLoc;
1261 } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) {
1262 // Merge the default argument from the old declaration to the
1263 // new declaration.
1264 SawDefaultArgument = true;
John McCall833ca992009-10-29 08:12:44 +00001265 NewTypeParm->setDefaultArgument(OldTypeParm->getDefaultArgumentInfo(),
Douglas Gregord684b002009-02-10 19:49:53 +00001266 true);
1267 PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc();
1268 } else if (NewTypeParm->hasDefaultArgument()) {
1269 SawDefaultArgument = true;
1270 PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc();
1271 } else if (SawDefaultArgument)
1272 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001273 } else if (NonTypeTemplateParmDecl *NewNonTypeParm
Douglas Gregord684b002009-02-10 19:49:53 +00001274 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001275 // Check for unexpanded parameter packs.
1276 if (DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001277 NewNonTypeParm->getTypeSourceInfo(),
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001278 UPPC_NonTypeTemplateParameterType)) {
1279 Invalid = true;
1280 continue;
1281 }
1282
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001283 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001284 if (NewNonTypeParm->hasDefaultArgument() &&
1285 DiagnoseDefaultTemplateArgument(*this, TPC,
1286 NewNonTypeParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001287 NewNonTypeParm->getDefaultArgument()->getSourceRange())) {
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001288 NewNonTypeParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001289 }
1290
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001291 // Merge default arguments for non-type template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001292 NonTypeTemplateParmDecl *OldNonTypeParm
1293 = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001294 if (NewNonTypeParm->isParameterPack()) {
1295 assert(!NewNonTypeParm->hasDefaultArgument() &&
1296 "Parameter packs can't have a default argument!");
1297 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001298 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001299 NewNonTypeParm->hasDefaultArgument()) {
1300 OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc();
1301 NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc();
1302 SawDefaultArgument = true;
1303 RedundantDefaultArg = true;
1304 PreviousDefaultArgLoc = NewDefaultLoc;
1305 } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) {
1306 // Merge the default argument from the old declaration to the
1307 // new declaration.
1308 SawDefaultArgument = true;
1309 // FIXME: We need to create a new kind of "default argument"
Douglas Gregor61c4d282011-01-05 15:48:55 +00001310 // expression that points to a previous non-type template
Douglas Gregord684b002009-02-10 19:49:53 +00001311 // parameter.
1312 NewNonTypeParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001313 OldNonTypeParm->getDefaultArgument(),
1314 /*Inherited=*/ true);
Douglas Gregord684b002009-02-10 19:49:53 +00001315 PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc();
1316 } else if (NewNonTypeParm->hasDefaultArgument()) {
1317 SawDefaultArgument = true;
1318 PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc();
1319 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001320 MissingDefaultArg = true;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001321 } else {
Douglas Gregord684b002009-02-10 19:49:53 +00001322 TemplateTemplateParmDecl *NewTemplateParm
1323 = cast<TemplateTemplateParmDecl>(*NewParam);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001324
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001325 // Check for unexpanded parameter packs, recursively.
Douglas Gregor65019ac2011-10-25 03:44:56 +00001326 if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) {
Douglas Gregor4d2abba2010-12-16 15:36:43 +00001327 Invalid = true;
1328 continue;
1329 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001330
David Blaikie1368e582011-10-19 05:19:50 +00001331 // Check the presence of a default argument here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001332 if (NewTemplateParm->hasDefaultArgument() &&
1333 DiagnoseDefaultTemplateArgument(*this, TPC,
1334 NewTemplateParm->getLocation(),
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001335 NewTemplateParm->getDefaultArgument().getSourceRange()))
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001336 NewTemplateParm->removeDefaultArgument();
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001337
1338 // Merge default arguments for template template parameters
Douglas Gregord684b002009-02-10 19:49:53 +00001339 TemplateTemplateParmDecl *OldTemplateParm
1340 = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : 0;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001341 if (NewTemplateParm->isParameterPack()) {
1342 assert(!NewTemplateParm->hasDefaultArgument() &&
1343 "Parameter packs can't have a default argument!");
1344 SawParameterPack = true;
Douglas Gregor1ed64762011-01-05 16:19:19 +00001345 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument() &&
Douglas Gregord684b002009-02-10 19:49:53 +00001346 NewTemplateParm->hasDefaultArgument()) {
Douglas Gregor788cd062009-11-11 01:00:40 +00001347 OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation();
1348 NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001349 SawDefaultArgument = true;
1350 RedundantDefaultArg = true;
1351 PreviousDefaultArgLoc = NewDefaultLoc;
1352 } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) {
1353 // Merge the default argument from the old declaration to the
1354 // new declaration.
1355 SawDefaultArgument = true;
Mike Stump390b4cc2009-05-16 07:39:55 +00001356 // FIXME: We need to create a new kind of "default argument" expression
1357 // that points to a previous template template parameter.
Douglas Gregord684b002009-02-10 19:49:53 +00001358 NewTemplateParm->setDefaultArgument(
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00001359 OldTemplateParm->getDefaultArgument(),
1360 /*Inherited=*/ true);
Douglas Gregor788cd062009-11-11 01:00:40 +00001361 PreviousDefaultArgLoc
1362 = OldTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001363 } else if (NewTemplateParm->hasDefaultArgument()) {
1364 SawDefaultArgument = true;
Douglas Gregor788cd062009-11-11 01:00:40 +00001365 PreviousDefaultArgLoc
1366 = NewTemplateParm->getDefaultArgument().getLocation();
Douglas Gregord684b002009-02-10 19:49:53 +00001367 } else if (SawDefaultArgument)
Mike Stump1eb44332009-09-09 15:08:12 +00001368 MissingDefaultArg = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001369 }
1370
David Blaikie1368e582011-10-19 05:19:50 +00001371 // C++0x [temp.param]p11:
1372 // If a template parameter of a primary class template or alias template
1373 // is a template parameter pack, it shall be the last template parameter.
1374 if (SawParameterPack && (NewParam + 1) != NewParamEnd &&
1375 (TPC == TPC_ClassTemplate || TPC == TPC_TypeAliasTemplate)) {
1376 Diag((*NewParam)->getLocation(),
1377 diag::err_template_param_pack_must_be_last_template_parameter);
1378 Invalid = true;
1379 }
1380
Douglas Gregord684b002009-02-10 19:49:53 +00001381 if (RedundantDefaultArg) {
1382 // C++ [temp.param]p12:
1383 // A template-parameter shall not be given default arguments
1384 // by two different declarations in the same scope.
1385 Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition);
1386 Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg);
1387 Invalid = true;
Douglas Gregoree5d21f2011-02-04 03:57:22 +00001388 } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) {
Douglas Gregord684b002009-02-10 19:49:53 +00001389 // C++ [temp.param]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001390 // If a template-parameter of a class template has a default
1391 // template-argument, each subsequent template-parameter shall either
Douglas Gregorb49e4152011-01-05 16:21:17 +00001392 // have a default template-argument supplied or be a template parameter
1393 // pack.
Mike Stump1eb44332009-09-09 15:08:12 +00001394 Diag((*NewParam)->getLocation(),
Douglas Gregord684b002009-02-10 19:49:53 +00001395 diag::err_template_param_default_arg_missing);
1396 Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg);
1397 Invalid = true;
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001398 RemoveDefaultArguments = true;
Douglas Gregord684b002009-02-10 19:49:53 +00001399 }
1400
1401 // If we have an old template parameter list that we're merging
1402 // in, move on to the next parameter.
1403 if (OldParams)
1404 ++OldParam;
1405 }
1406
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001407 // We were missing some default arguments at the end of the list, so remove
1408 // all of the default arguments.
1409 if (RemoveDefaultArguments) {
1410 for (TemplateParameterList::iterator NewParam = NewParams->begin(),
1411 NewParamEnd = NewParams->end();
1412 NewParam != NewParamEnd; ++NewParam) {
1413 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam))
1414 TTP->removeDefaultArgument();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001415 else if (NonTypeTemplateParmDecl *NTTP
Douglas Gregorfd1a8fd2011-01-27 01:40:17 +00001416 = dyn_cast<NonTypeTemplateParmDecl>(*NewParam))
1417 NTTP->removeDefaultArgument();
1418 else
1419 cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument();
1420 }
1421 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00001422
Douglas Gregord684b002009-02-10 19:49:53 +00001423 return Invalid;
1424}
Douglas Gregorc15cb382009-02-09 23:23:08 +00001425
John McCall4e2cbb22010-10-20 05:44:58 +00001426namespace {
1427
1428/// A class which looks for a use of a certain level of template
1429/// parameter.
1430struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> {
1431 typedef RecursiveASTVisitor<DependencyChecker> super;
1432
1433 unsigned Depth;
1434 bool Match;
1435
1436 DependencyChecker(TemplateParameterList *Params) : Match(false) {
1437 NamedDecl *ND = Params->getParam(0);
1438 if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) {
1439 Depth = PD->getDepth();
1440 } else if (NonTypeTemplateParmDecl *PD =
1441 dyn_cast<NonTypeTemplateParmDecl>(ND)) {
1442 Depth = PD->getDepth();
1443 } else {
1444 Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth();
1445 }
1446 }
1447
1448 bool Matches(unsigned ParmDepth) {
1449 if (ParmDepth >= Depth) {
1450 Match = true;
1451 return true;
1452 }
1453 return false;
1454 }
1455
1456 bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
1457 return !Matches(T->getDepth());
1458 }
1459
1460 bool TraverseTemplateName(TemplateName N) {
1461 if (TemplateTemplateParmDecl *PD =
1462 dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl()))
1463 if (Matches(PD->getDepth())) return false;
1464 return super::TraverseTemplateName(N);
1465 }
1466
1467 bool VisitDeclRefExpr(DeclRefExpr *E) {
1468 if (NonTypeTemplateParmDecl *PD =
1469 dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) {
1470 if (PD->getDepth() == Depth) {
1471 Match = true;
1472 return false;
1473 }
1474 }
1475 return super::VisitDeclRefExpr(E);
1476 }
Douglas Gregor18c83392011-05-13 00:34:01 +00001477
1478 bool TraverseInjectedClassNameType(const InjectedClassNameType *T) {
1479 return TraverseType(T->getInjectedSpecializationType());
1480 }
John McCall4e2cbb22010-10-20 05:44:58 +00001481};
1482}
1483
Douglas Gregorc8406492011-05-10 18:27:06 +00001484/// Determines whether a given type depends on the given parameter
John McCall4e2cbb22010-10-20 05:44:58 +00001485/// list.
1486static bool
Douglas Gregorc8406492011-05-10 18:27:06 +00001487DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) {
John McCall4e2cbb22010-10-20 05:44:58 +00001488 DependencyChecker Checker(Params);
Douglas Gregorc8406492011-05-10 18:27:06 +00001489 Checker.TraverseType(T);
John McCall4e2cbb22010-10-20 05:44:58 +00001490 return Checker.Match;
1491}
1492
Douglas Gregorc8406492011-05-10 18:27:06 +00001493// Find the source range corresponding to the named type in the given
1494// nested-name-specifier, if any.
1495static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context,
1496 QualType T,
1497 const CXXScopeSpec &SS) {
1498 NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data());
1499 while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) {
1500 if (const Type *CurType = NNS->getAsType()) {
1501 if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0)))
1502 return NNSLoc.getTypeLoc().getSourceRange();
1503 } else
1504 break;
1505
1506 NNSLoc = NNSLoc.getPrefix();
1507 }
1508
1509 return SourceRange();
1510}
1511
Mike Stump1eb44332009-09-09 15:08:12 +00001512/// \brief Match the given template parameter lists to the given scope
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001513/// specifier, returning the template parameter list that applies to the
1514/// name.
1515///
1516/// \param DeclStartLoc the start of the declaration that has a scope
1517/// specifier or a template parameter list.
Mike Stump1eb44332009-09-09 15:08:12 +00001518///
Douglas Gregorc8406492011-05-10 18:27:06 +00001519/// \param DeclLoc The location of the declaration itself.
1520///
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001521/// \param SS the scope specifier that will be matched to the given template
1522/// parameter lists. This scope specifier precedes a qualified name that is
1523/// being declared.
1524///
1525/// \param ParamLists the template parameter lists, from the outermost to the
1526/// innermost template parameter lists.
1527///
1528/// \param NumParamLists the number of template parameter lists in ParamLists.
1529///
John McCall77e8b112010-04-13 20:37:33 +00001530/// \param IsFriend Whether to apply the slightly different rules for
1531/// matching template parameters to scope specifiers in friend
1532/// declarations.
1533///
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001534/// \param IsExplicitSpecialization will be set true if the entity being
1535/// declared is an explicit specialization, false otherwise.
1536///
Mike Stump1eb44332009-09-09 15:08:12 +00001537/// \returns the template parameter list, if any, that corresponds to the
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001538/// name that is preceded by the scope specifier @p SS. This template
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001539/// parameter list may have template parameters (if we're declaring a
Mike Stump1eb44332009-09-09 15:08:12 +00001540/// template) or may have no template parameters (if we're declaring a
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00001541/// template specialization), or may be NULL (if what we're declaring isn't
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001542/// itself a template).
1543TemplateParameterList *
1544Sema::MatchTemplateParametersToScopeSpecifier(SourceLocation DeclStartLoc,
Douglas Gregorc8406492011-05-10 18:27:06 +00001545 SourceLocation DeclLoc,
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001546 const CXXScopeSpec &SS,
1547 TemplateParameterList **ParamLists,
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001548 unsigned NumParamLists,
John McCall77e8b112010-04-13 20:37:33 +00001549 bool IsFriend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00001550 bool &IsExplicitSpecialization,
1551 bool &Invalid) {
Douglas Gregor1fef4e62009-10-07 22:35:40 +00001552 IsExplicitSpecialization = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001553 Invalid = false;
1554
1555 // The sequence of nested types to which we will match up the template
1556 // parameter lists. We first build this list by starting with the type named
1557 // by the nested-name-specifier and walking out until we run out of types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001558 SmallVector<QualType, 4> NestedTypes;
Douglas Gregorc8406492011-05-10 18:27:06 +00001559 QualType T;
Douglas Gregor714c9922011-05-15 17:27:27 +00001560 if (SS.getScopeRep()) {
1561 if (CXXRecordDecl *Record
1562 = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true)))
1563 T = Context.getTypeDeclType(Record);
1564 else
1565 T = QualType(SS.getScopeRep()->getAsType(), 0);
1566 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001567
1568 // If we found an explicit specialization that prevents us from needing
1569 // 'template<>' headers, this will be set to the location of that
1570 // explicit specialization.
1571 SourceLocation ExplicitSpecLoc;
1572
1573 while (!T.isNull()) {
1574 NestedTypes.push_back(T);
1575
1576 // Retrieve the parent of a record type.
1577 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1578 // If this type is an explicit specialization, we're done.
1579 if (ClassTemplateSpecializationDecl *Spec
1580 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1581 if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) &&
1582 Spec->getSpecializationKind() == TSK_ExplicitSpecialization) {
1583 ExplicitSpecLoc = Spec->getLocation();
1584 break;
Douglas Gregor3ebd7532009-11-23 12:11:45 +00001585 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001586 } else if (Record->getTemplateSpecializationKind()
1587 == TSK_ExplicitSpecialization) {
1588 ExplicitSpecLoc = Record->getLocation();
John McCall77e8b112010-04-13 20:37:33 +00001589 break;
1590 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001591
1592 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent()))
1593 T = Context.getTypeDeclType(Parent);
1594 else
1595 T = QualType();
1596 continue;
1597 }
1598
1599 if (const TemplateSpecializationType *TST
1600 = T->getAs<TemplateSpecializationType>()) {
1601 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1602 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext()))
1603 T = Context.getTypeDeclType(Parent);
1604 else
1605 T = QualType();
1606 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001607 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001608 }
1609
1610 // Look one step prior in a dependent template specialization type.
1611 if (const DependentTemplateSpecializationType *DependentTST
1612 = T->getAs<DependentTemplateSpecializationType>()) {
1613 if (NestedNameSpecifier *NNS = DependentTST->getQualifier())
1614 T = QualType(NNS->getAsType(), 0);
1615 else
1616 T = QualType();
1617 continue;
1618 }
1619
1620 // Look one step prior in a dependent name type.
1621 if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){
1622 if (NestedNameSpecifier *NNS = DependentName->getQualifier())
1623 T = QualType(NNS->getAsType(), 0);
1624 else
1625 T = QualType();
1626 continue;
1627 }
1628
1629 // Retrieve the parent of an enumeration type.
1630 if (const EnumType *EnumT = T->getAs<EnumType>()) {
1631 // FIXME: Forward-declared enums require a TSK_ExplicitSpecialization
1632 // check here.
1633 EnumDecl *Enum = EnumT->getDecl();
1634
1635 // Get to the parent type.
1636 if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent()))
1637 T = Context.getTypeDeclType(Parent);
1638 else
1639 T = QualType();
1640 continue;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001641 }
Mike Stump1eb44332009-09-09 15:08:12 +00001642
Douglas Gregorc8406492011-05-10 18:27:06 +00001643 T = QualType();
1644 }
1645 // Reverse the nested types list, since we want to traverse from the outermost
1646 // to the innermost while checking template-parameter-lists.
1647 std::reverse(NestedTypes.begin(), NestedTypes.end());
Douglas Gregorb88e8882009-07-30 17:40:51 +00001648
Douglas Gregorc8406492011-05-10 18:27:06 +00001649 // C++0x [temp.expl.spec]p17:
1650 // A member or a member template may be nested within many
1651 // enclosing class templates. In an explicit specialization for
1652 // such a member, the member declaration shall be preceded by a
1653 // template<> for each enclosing class template that is
1654 // explicitly specialized.
Douglas Gregor89b9f102011-06-06 15:22:55 +00001655 bool SawNonEmptyTemplateParameterList = false;
Douglas Gregorc8406492011-05-10 18:27:06 +00001656 unsigned ParamIdx = 0;
1657 for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes;
1658 ++TypeIdx) {
1659 T = NestedTypes[TypeIdx];
1660
1661 // Whether we expect a 'template<>' header.
1662 bool NeedEmptyTemplateHeader = false;
1663
1664 // Whether we expect a template header with parameters.
1665 bool NeedNonemptyTemplateHeader = false;
1666
1667 // For a dependent type, the set of template parameters that we
1668 // expect to see.
1669 TemplateParameterList *ExpectedTemplateParams = 0;
1670
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001671 // C++0x [temp.expl.spec]p15:
1672 // A member or a member template may be nested within many enclosing
1673 // class templates. In an explicit specialization for such a member, the
1674 // member declaration shall be preceded by a template<> for each
1675 // enclosing class template that is explicitly specialized.
Douglas Gregorc8406492011-05-10 18:27:06 +00001676 if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) {
1677 if (ClassTemplatePartialSpecializationDecl *Partial
1678 = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) {
1679 ExpectedTemplateParams = Partial->getTemplateParameters();
1680 NeedNonemptyTemplateHeader = true;
1681 } else if (Record->isDependentType()) {
1682 if (Record->getDescribedClassTemplate()) {
John McCall31f17ec2010-04-27 00:57:59 +00001683 ExpectedTemplateParams = Record->getDescribedClassTemplate()
Douglas Gregorc8406492011-05-10 18:27:06 +00001684 ->getTemplateParameters();
1685 NeedNonemptyTemplateHeader = true;
1686 }
1687 } else if (ClassTemplateSpecializationDecl *Spec
1688 = dyn_cast<ClassTemplateSpecializationDecl>(Record)) {
1689 // C++0x [temp.expl.spec]p4:
1690 // Members of an explicitly specialized class template are defined
1691 // in the same manner as members of normal classes, and not using
1692 // the template<> syntax.
1693 if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization)
1694 NeedEmptyTemplateHeader = true;
1695 else
Douglas Gregor95ea4502011-06-01 22:37:07 +00001696 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001697 } else if (Record->getTemplateSpecializationKind()) {
1698 if (Record->getTemplateSpecializationKind()
Douglas Gregor175c5bb2011-05-11 23:26:17 +00001699 != TSK_ExplicitSpecialization &&
1700 TypeIdx == NumTypes - 1)
1701 IsExplicitSpecialization = true;
1702
1703 continue;
Douglas Gregorc8406492011-05-10 18:27:06 +00001704 }
1705 } else if (const TemplateSpecializationType *TST
1706 = T->getAs<TemplateSpecializationType>()) {
1707 if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) {
1708 ExpectedTemplateParams = Template->getTemplateParameters();
1709 NeedNonemptyTemplateHeader = true;
1710 }
1711 } else if (T->getAs<DependentTemplateSpecializationType>()) {
1712 // FIXME: We actually could/should check the template arguments here
1713 // against the corresponding template parameter list.
1714 NeedNonemptyTemplateHeader = false;
1715 }
1716
Douglas Gregor89b9f102011-06-06 15:22:55 +00001717 // C++ [temp.expl.spec]p16:
1718 // In an explicit specialization declaration for a member of a class
1719 // template or a member template that ap- pears in namespace scope, the
1720 // member template and some of its enclosing class templates may remain
1721 // unspecialized, except that the declaration shall not explicitly
1722 // specialize a class member template if its en- closing class templates
1723 // are not explicitly specialized as well.
1724 if (ParamIdx < NumParamLists) {
1725 if (ParamLists[ParamIdx]->size() == 0) {
1726 if (SawNonEmptyTemplateParameterList) {
1727 Diag(DeclLoc, diag::err_specialize_member_of_template)
1728 << ParamLists[ParamIdx]->getSourceRange();
1729 Invalid = true;
1730 IsExplicitSpecialization = false;
1731 return 0;
1732 }
1733 } else
1734 SawNonEmptyTemplateParameterList = true;
1735 }
1736
Douglas Gregorc8406492011-05-10 18:27:06 +00001737 if (NeedEmptyTemplateHeader) {
1738 // If we're on the last of the types, and we need a 'template<>' header
1739 // here, then it's an explicit specialization.
1740 if (TypeIdx == NumTypes - 1)
1741 IsExplicitSpecialization = true;
1742
1743 if (ParamIdx < NumParamLists) {
1744 if (ParamLists[ParamIdx]->size() > 0) {
1745 // The header has template parameters when it shouldn't. Complain.
1746 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1747 diag::err_template_param_list_matches_nontemplate)
1748 << T
1749 << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(),
1750 ParamLists[ParamIdx]->getRAngleLoc())
1751 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1752 Invalid = true;
1753 return 0;
1754 }
1755
1756 // Consume this template header.
1757 ++ParamIdx;
1758 continue;
1759 }
1760
1761 if (!IsFriend) {
1762 // We don't have a template header, but we should.
1763 SourceLocation ExpectedTemplateLoc;
1764 if (NumParamLists > 0)
1765 ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc();
1766 else
1767 ExpectedTemplateLoc = DeclStartLoc;
1768
1769 Diag(DeclLoc, diag::err_template_spec_needs_header)
1770 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS)
1771 << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> ");
1772 }
1773
1774 continue;
1775 }
1776
1777 if (NeedNonemptyTemplateHeader) {
1778 // In friend declarations we can have template-ids which don't
1779 // depend on the corresponding template parameter lists. But
1780 // assume that empty parameter lists are supposed to match this
1781 // template-id.
1782 if (IsFriend && T->isDependentType()) {
1783 if (ParamIdx < NumParamLists &&
1784 DependsOnTemplateParameters(T, ParamLists[ParamIdx]))
1785 ExpectedTemplateParams = 0;
1786 else
1787 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001788 }
Douglas Gregor5b6d70e2009-11-25 17:50:39 +00001789
Douglas Gregorc8406492011-05-10 18:27:06 +00001790 if (ParamIdx < NumParamLists) {
1791 // Check the template parameter list, if we can.
1792 if (ExpectedTemplateParams &&
1793 !TemplateParameterListsAreEqual(ParamLists[ParamIdx],
1794 ExpectedTemplateParams,
1795 true, TPL_TemplateMatch))
1796 Invalid = true;
1797
1798 if (!Invalid &&
1799 CheckTemplateParameterList(ParamLists[ParamIdx], 0,
1800 TPC_ClassTemplateMember))
1801 Invalid = true;
1802
1803 ++ParamIdx;
1804 continue;
1805 }
1806
1807 Diag(DeclLoc, diag::err_template_spec_needs_template_parameters)
1808 << T
1809 << getRangeOfTypeInNestedNameSpecifier(Context, T, SS);
1810 Invalid = true;
1811 continue;
1812 }
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001813 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001814
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001815 // If there were at least as many template-ids as there were template
1816 // parameter lists, then there are no template parameter lists remaining for
1817 // the declaration itself.
John McCall4e2cbb22010-10-20 05:44:58 +00001818 if (ParamIdx >= NumParamLists)
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001819 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001820
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001821 // If there were too many template parameter lists, complain about that now.
Douglas Gregorc8406492011-05-10 18:27:06 +00001822 if (ParamIdx < NumParamLists - 1) {
1823 bool HasAnyExplicitSpecHeader = false;
1824 bool AllExplicitSpecHeaders = true;
1825 for (unsigned I = ParamIdx; I != NumParamLists - 1; ++I) {
1826 if (ParamLists[I]->size() == 0)
1827 HasAnyExplicitSpecHeader = true;
1828 else
1829 AllExplicitSpecHeaders = false;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001830 }
Douglas Gregorc8406492011-05-10 18:27:06 +00001831
1832 Diag(ParamLists[ParamIdx]->getTemplateLoc(),
1833 AllExplicitSpecHeaders? diag::warn_template_spec_extra_headers
1834 : diag::err_template_spec_extra_headers)
1835 << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(),
1836 ParamLists[NumParamLists - 2]->getRAngleLoc());
1837
1838 // If there was a specialization somewhere, such that 'template<>' is
1839 // not required, and there were any 'template<>' headers, note where the
1840 // specialization occurred.
1841 if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader)
1842 Diag(ExplicitSpecLoc,
1843 diag::note_explicit_template_spec_does_not_need_header)
1844 << NestedTypes.back();
1845
1846 // We have a template parameter list with no corresponding scope, which
1847 // means that the resulting template declaration can't be instantiated
1848 // properly (we'll end up with dependent nodes when we shouldn't).
1849 if (!AllExplicitSpecHeaders)
1850 Invalid = true;
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001851 }
Mike Stump1eb44332009-09-09 15:08:12 +00001852
Douglas Gregor89b9f102011-06-06 15:22:55 +00001853 // C++ [temp.expl.spec]p16:
1854 // In an explicit specialization declaration for a member of a class
1855 // template or a member template that ap- pears in namespace scope, the
1856 // member template and some of its enclosing class templates may remain
1857 // unspecialized, except that the declaration shall not explicitly
1858 // specialize a class member template if its en- closing class templates
1859 // are not explicitly specialized as well.
1860 if (ParamLists[NumParamLists - 1]->size() == 0 &&
1861 SawNonEmptyTemplateParameterList) {
1862 Diag(DeclLoc, diag::err_specialize_member_of_template)
1863 << ParamLists[ParamIdx]->getSourceRange();
1864 Invalid = true;
1865 IsExplicitSpecialization = false;
1866 return 0;
1867 }
1868
Douglas Gregorf59a56e2009-07-21 23:53:31 +00001869 // Return the last template parameter list, which corresponds to the
1870 // entity being declared.
1871 return ParamLists[NumParamLists - 1];
1872}
1873
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001874void Sema::NoteAllFoundTemplates(TemplateName Name) {
1875 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
1876 Diag(Template->getLocation(), diag::note_template_declared_here)
1877 << (isa<FunctionTemplateDecl>(Template)? 0
1878 : isa<ClassTemplateDecl>(Template)? 1
Richard Smith3e4c6c42011-05-05 21:57:07 +00001879 : isa<TypeAliasTemplateDecl>(Template)? 2
1880 : 3)
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001881 << Template->getDeclName();
1882 return;
1883 }
1884
1885 if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) {
1886 for (OverloadedTemplateStorage::iterator I = OST->begin(),
1887 IEnd = OST->end();
1888 I != IEnd; ++I)
1889 Diag((*I)->getLocation(), diag::note_template_declared_here)
1890 << 0 << (*I)->getDeclName();
1891
1892 return;
1893 }
1894}
1895
Douglas Gregor7532dc62009-03-30 22:58:21 +00001896QualType Sema::CheckTemplateIdType(TemplateName Name,
1897 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00001898 TemplateArgumentListInfo &TemplateArgs) {
John McCall14606042011-06-30 08:33:18 +00001899 DependentTemplateName *DTN
1900 = Name.getUnderlying().getAsDependentTemplateName();
Richard Smith3e4c6c42011-05-05 21:57:07 +00001901 if (DTN && DTN->isIdentifier())
1902 // When building a template-id where the template-name is dependent,
1903 // assume the template is a type template. Either our assumption is
1904 // correct, or the code is ill-formed and will be diagnosed when the
1905 // dependent name is substituted.
1906 return Context.getDependentTemplateSpecializationType(ETK_None,
1907 DTN->getQualifier(),
1908 DTN->getIdentifier(),
1909 TemplateArgs);
1910
Douglas Gregor7532dc62009-03-30 22:58:21 +00001911 TemplateDecl *Template = Name.getAsTemplateDecl();
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001912 if (!Template || isa<FunctionTemplateDecl>(Template)) {
1913 // We might have a substituted template template parameter pack. If so,
1914 // build a template specialization type for it.
1915 if (Name.getAsSubstTemplateTemplateParmPack())
1916 return Context.getTemplateSpecializationType(Name, TemplateArgs);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001917
Douglas Gregor6cd9d4a2011-03-04 21:37:14 +00001918 Diag(TemplateLoc, diag::err_template_id_not_a_type)
1919 << Name;
1920 NoteAllFoundTemplates(Name);
1921 return QualType();
Douglas Gregorc45c2322009-03-31 00:43:58 +00001922 }
Douglas Gregor7532dc62009-03-30 22:58:21 +00001923
Douglas Gregor40808ce2009-03-09 23:48:35 +00001924 // Check that the template argument list is well-formed for this
1925 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001926 SmallVector<TemplateArgument, 4> Converted;
Douglas Gregorb70126a2012-02-03 17:16:23 +00001927 bool ExpansionIntoFixedList = false;
John McCalld5532b62009-11-23 01:53:49 +00001928 if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs,
Douglas Gregorb70126a2012-02-03 17:16:23 +00001929 false, Converted, &ExpansionIntoFixedList))
Douglas Gregor40808ce2009-03-09 23:48:35 +00001930 return QualType();
1931
Douglas Gregor40808ce2009-03-09 23:48:35 +00001932 QualType CanonType;
1933
Douglas Gregor561f8122011-07-01 01:22:09 +00001934 bool InstantiationDependent = false;
Douglas Gregorb70126a2012-02-03 17:16:23 +00001935 TypeAliasTemplateDecl *AliasTemplate = 0;
1936 if (!ExpansionIntoFixedList &&
1937 (AliasTemplate = dyn_cast<TypeAliasTemplateDecl>(Template))) {
Richard Smith3e4c6c42011-05-05 21:57:07 +00001938 // Find the canonical type for this type alias template specialization.
1939 TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl();
1940 if (Pattern->isInvalidDecl())
1941 return QualType();
1942
1943 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1944 Converted.data(), Converted.size());
1945
1946 // Only substitute for the innermost template argument list.
1947 MultiLevelTemplateArgumentList TemplateArgLists;
Richard Smith18041742011-05-14 15:04:18 +00001948 TemplateArgLists.addOuterTemplateArguments(&TemplateArgs);
Richard Smithaff37b42011-05-12 00:06:17 +00001949 unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth();
1950 for (unsigned I = 0; I < Depth; ++I)
1951 TemplateArgLists.addOuterTemplateArguments(0, 0);
Richard Smith3e4c6c42011-05-05 21:57:07 +00001952
1953 InstantiatingTemplate Inst(*this, TemplateLoc, Template);
1954 CanonType = SubstType(Pattern->getUnderlyingType(),
1955 TemplateArgLists, AliasTemplate->getLocation(),
1956 AliasTemplate->getDeclName());
1957 if (CanonType.isNull())
1958 return QualType();
1959 } else if (Name.isDependent() ||
1960 TemplateSpecializationType::anyDependentTemplateArguments(
Douglas Gregor561f8122011-07-01 01:22:09 +00001961 TemplateArgs, InstantiationDependent)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001962 // This class template specialization is a dependent
1963 // type. Therefore, its canonical type is another class template
1964 // specialization type that contains all of the converted
1965 // arguments in canonical form. This ensures that, e.g., A<T> and
1966 // A<T, T> have identical types when A is declared as:
1967 //
1968 // template<typename T, typename U = T> struct A;
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001969 TemplateName CanonName = Context.getCanonicalTemplateName(Name);
Mike Stump1eb44332009-09-09 15:08:12 +00001970 CanonType = Context.getTemplateSpecializationType(CanonName,
Douglas Gregor910f8002010-11-07 23:05:16 +00001971 Converted.data(),
1972 Converted.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001973
Douglas Gregor1275ae02009-07-28 23:00:59 +00001974 // FIXME: CanonType is not actually the canonical type, and unfortunately
John McCall833ca992009-10-29 08:12:44 +00001975 // it is a TemplateSpecializationType that we will never use again.
Douglas Gregor1275ae02009-07-28 23:00:59 +00001976 // In the future, we need to teach getTemplateSpecializationType to only
1977 // build the canonical type and return that to us.
1978 CanonType = Context.getCanonicalType(CanonType);
John McCall31f17ec2010-04-27 00:57:59 +00001979
1980 // This might work out to be a current instantiation, in which
1981 // case the canonical type needs to be the InjectedClassNameType.
1982 //
1983 // TODO: in theory this could be a simple hashtable lookup; most
1984 // changes to CurContext don't change the set of current
1985 // instantiations.
1986 if (isa<ClassTemplateDecl>(Template)) {
1987 for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) {
1988 // If we get out to a namespace, we're done.
1989 if (Ctx->isFileContext()) break;
1990
1991 // If this isn't a record, keep looking.
1992 CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx);
1993 if (!Record) continue;
1994
1995 // Look for one of the two cases with InjectedClassNameTypes
1996 // and check whether it's the same template.
1997 if (!isa<ClassTemplatePartialSpecializationDecl>(Record) &&
1998 !Record->getDescribedClassTemplate())
1999 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002000
John McCall31f17ec2010-04-27 00:57:59 +00002001 // Fetch the injected class name type and check whether its
2002 // injected type is equal to the type we just built.
2003 QualType ICNT = Context.getTypeDeclType(Record);
2004 QualType Injected = cast<InjectedClassNameType>(ICNT)
2005 ->getInjectedSpecializationType();
2006
2007 if (CanonType != Injected->getCanonicalTypeInternal())
2008 continue;
2009
2010 // If so, the canonical type of this TST is the injected
2011 // class name type of the record we just found.
2012 assert(ICNT.isCanonical());
2013 CanonType = ICNT;
John McCall31f17ec2010-04-27 00:57:59 +00002014 break;
2015 }
2016 }
Mike Stump1eb44332009-09-09 15:08:12 +00002017 } else if (ClassTemplateDecl *ClassTemplate
Douglas Gregor7532dc62009-03-30 22:58:21 +00002018 = dyn_cast<ClassTemplateDecl>(Template)) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00002019 // Find the class template specialization declaration that
2020 // corresponds to these arguments.
Douglas Gregor40808ce2009-03-09 23:48:35 +00002021 void *InsertPos = 0;
2022 ClassTemplateSpecializationDecl *Decl
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002023 = ClassTemplate->findSpecialization(Converted.data(), Converted.size(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002024 InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002025 if (!Decl) {
2026 // This is the first time we have referenced this class template
2027 // specialization. Create the canonical declaration and add it to
2028 // the set of specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00002029 Decl = ClassTemplateSpecializationDecl::Create(Context,
Douglas Gregor13c85772010-05-06 00:28:52 +00002030 ClassTemplate->getTemplatedDecl()->getTagKind(),
2031 ClassTemplate->getDeclContext(),
Abramo Bagnara09d82122011-10-03 20:34:03 +00002032 ClassTemplate->getTemplatedDecl()->getLocStart(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002033 ClassTemplate->getLocation(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002034 ClassTemplate,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002035 Converted.data(),
Douglas Gregor910f8002010-11-07 23:05:16 +00002036 Converted.size(), 0);
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00002037 ClassTemplate->AddSpecialization(Decl, InsertPos);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002038 Decl->setLexicalDeclContext(CurContext);
2039 }
2040
2041 CanonType = Context.getTypeDeclType(Decl);
John McCall3cb0ebd2010-03-10 03:28:59 +00002042 assert(isa<RecordType>(CanonType) &&
2043 "type of non-dependent specialization is not a RecordType");
Douglas Gregor40808ce2009-03-09 23:48:35 +00002044 }
Mike Stump1eb44332009-09-09 15:08:12 +00002045
Douglas Gregor40808ce2009-03-09 23:48:35 +00002046 // Build the fully-sugared type for this class template
2047 // specialization, which refers back to the class template
2048 // specialization we created or found.
John McCall71d74bc2010-06-13 09:25:03 +00002049 return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002050}
2051
John McCallf312b1e2010-08-26 23:41:50 +00002052TypeResult
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002053Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc,
Douglas Gregor059101f2011-03-02 00:47:37 +00002054 TemplateTy TemplateD, SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00002055 SourceLocation LAngleLoc,
Douglas Gregor7532dc62009-03-30 22:58:21 +00002056 ASTTemplateArgsPtr TemplateArgsIn,
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002057 SourceLocation RAngleLoc,
2058 bool IsCtorOrDtorName) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002059 if (SS.isInvalid())
2060 return true;
2061
Douglas Gregor7532dc62009-03-30 22:58:21 +00002062 TemplateName Template = TemplateD.getAsVal<TemplateName>();
Douglas Gregor55f6b142009-02-09 18:46:07 +00002063
Douglas Gregor40808ce2009-03-09 23:48:35 +00002064 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00002065 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00002066 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregorc15cb382009-02-09 23:23:08 +00002067
Douglas Gregora88f09f2011-02-28 17:23:35 +00002068 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002069 QualType T
2070 = Context.getDependentTemplateSpecializationType(ETK_None,
2071 DTN->getQualifier(),
2072 DTN->getIdentifier(),
2073 TemplateArgs);
2074 // Build type-source information.
Douglas Gregora88f09f2011-02-28 17:23:35 +00002075 TypeLocBuilder TLB;
2076 DependentTemplateSpecializationTypeLoc SpecTL
2077 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002078 SpecTL.setElaboratedKeywordLoc(SourceLocation());
2079 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara66581d42012-02-06 22:45:07 +00002080 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002081 SpecTL.setTemplateNameLoc(TemplateLoc);
Douglas Gregora88f09f2011-02-28 17:23:35 +00002082 SpecTL.setLAngleLoc(LAngleLoc);
2083 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregora88f09f2011-02-28 17:23:35 +00002084 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2085 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2086 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2087 }
2088
John McCalld5532b62009-11-23 01:53:49 +00002089 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00002090 TemplateArgsIn.release();
Douglas Gregor31a19b62009-04-01 21:51:26 +00002091
2092 if (Result.isNull())
2093 return true;
2094
Douglas Gregor059101f2011-03-02 00:47:37 +00002095 // Build type-source information.
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002096 TypeLocBuilder TLB;
Douglas Gregor059101f2011-03-02 00:47:37 +00002097 TemplateSpecializationTypeLoc SpecTL
2098 = TLB.push<TemplateSpecializationTypeLoc>(Result);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002099 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002100 SpecTL.setTemplateNameLoc(TemplateLoc);
2101 SpecTL.setLAngleLoc(LAngleLoc);
2102 SpecTL.setRAngleLoc(RAngleLoc);
2103 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2104 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCall833ca992009-10-29 08:12:44 +00002105
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002106 // NOTE: avoid constructing an ElaboratedTypeLoc if this is a
2107 // constructor or destructor name (in such a case, the scope specifier
2108 // will be attached to the enclosing Decl or Expr node).
2109 if (SS.isNotEmpty() && !IsCtorOrDtorName) {
Douglas Gregor059101f2011-03-02 00:47:37 +00002110 // Create an elaborated-type-specifier containing the nested-name-specifier.
2111 Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result);
2112 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00002113 ElabTL.setElaboratedKeywordLoc(SourceLocation());
Douglas Gregor059101f2011-03-02 00:47:37 +00002114 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2115 }
2116
2117 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCall6b2becf2009-09-08 17:47:29 +00002118}
John McCallf1bbbb42009-09-04 01:14:41 +00002119
Douglas Gregor059101f2011-03-02 00:47:37 +00002120TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK,
John McCallf312b1e2010-08-26 23:41:50 +00002121 TypeSpecifierType TagSpec,
Douglas Gregor059101f2011-03-02 00:47:37 +00002122 SourceLocation TagLoc,
2123 CXXScopeSpec &SS,
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002124 SourceLocation TemplateKWLoc,
2125 TemplateTy TemplateD,
Douglas Gregor059101f2011-03-02 00:47:37 +00002126 SourceLocation TemplateLoc,
2127 SourceLocation LAngleLoc,
2128 ASTTemplateArgsPtr TemplateArgsIn,
2129 SourceLocation RAngleLoc) {
2130 TemplateName Template = TemplateD.getAsVal<TemplateName>();
2131
2132 // Translate the parser's template argument list in our AST format.
2133 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
2134 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
2135
2136 // Determine the tag kind
Abramo Bagnara465d41b2010-05-11 21:36:43 +00002137 TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Douglas Gregor059101f2011-03-02 00:47:37 +00002138 ElaboratedTypeKeyword Keyword
2139 = TypeWithKeyword::getKeywordForTagTypeKind(TagKind);
Mike Stump1eb44332009-09-09 15:08:12 +00002140
Douglas Gregor059101f2011-03-02 00:47:37 +00002141 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
2142 QualType T = Context.getDependentTemplateSpecializationType(Keyword,
2143 DTN->getQualifier(),
2144 DTN->getIdentifier(),
2145 TemplateArgs);
2146
2147 // Build type-source information.
2148 TypeLocBuilder TLB;
2149 DependentTemplateSpecializationTypeLoc SpecTL
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002150 = TLB.push<DependentTemplateSpecializationTypeLoc>(T);
2151 SpecTL.setElaboratedKeywordLoc(TagLoc);
2152 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara66581d42012-02-06 22:45:07 +00002153 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002154 SpecTL.setTemplateNameLoc(TemplateLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002155 SpecTL.setLAngleLoc(LAngleLoc);
2156 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002157 for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I)
2158 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
2159 return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T));
2160 }
Richard Smith3e4c6c42011-05-05 21:57:07 +00002161
2162 if (TypeAliasTemplateDecl *TAT =
2163 dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) {
2164 // C++0x [dcl.type.elab]p2:
2165 // If the identifier resolves to a typedef-name or the simple-template-id
2166 // resolves to an alias template specialization, the
2167 // elaborated-type-specifier is ill-formed.
2168 Diag(TemplateLoc, diag::err_tag_reference_non_tag) << 4;
2169 Diag(TAT->getLocation(), diag::note_declared_at);
2170 }
Douglas Gregor059101f2011-03-02 00:47:37 +00002171
2172 QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs);
2173 if (Result.isNull())
Matt Beaumont-Gay3a51d412011-08-25 23:22:24 +00002174 return TypeResult(true);
Douglas Gregor059101f2011-03-02 00:47:37 +00002175
2176 // Check the tag kind
2177 if (const RecordType *RT = Result->getAs<RecordType>()) {
John McCall6b2becf2009-09-08 17:47:29 +00002178 RecordDecl *D = RT->getDecl();
Douglas Gregor059101f2011-03-02 00:47:37 +00002179
John McCall6b2becf2009-09-08 17:47:29 +00002180 IdentifierInfo *Id = D->getIdentifier();
2181 assert(Id && "templated class must have an identifier");
Douglas Gregor059101f2011-03-02 00:47:37 +00002182
Richard Trieubbf34c02011-06-10 03:11:26 +00002183 if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition,
2184 TagLoc, *Id)) {
John McCall6b2becf2009-09-08 17:47:29 +00002185 Diag(TagLoc, diag::err_use_with_wrong_tag)
Douglas Gregor059101f2011-03-02 00:47:37 +00002186 << Result
Douglas Gregor849b2432010-03-31 17:46:05 +00002187 << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName());
John McCallc4e70192009-09-11 04:59:25 +00002188 Diag(D->getLocation(), diag::note_previous_use);
John McCallf1bbbb42009-09-04 01:14:41 +00002189 }
2190 }
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002191
Douglas Gregor059101f2011-03-02 00:47:37 +00002192 // Provide source-location information for the template specialization.
2193 TypeLocBuilder TLB;
2194 TemplateSpecializationTypeLoc SpecTL
2195 = TLB.push<TemplateSpecializationTypeLoc>(Result);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002196 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002197 SpecTL.setTemplateNameLoc(TemplateLoc);
2198 SpecTL.setLAngleLoc(LAngleLoc);
2199 SpecTL.setRAngleLoc(RAngleLoc);
2200 for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i)
2201 SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo());
John McCallf1bbbb42009-09-04 01:14:41 +00002202
Douglas Gregor059101f2011-03-02 00:47:37 +00002203 // Construct an elaborated type containing the nested-name-specifier (if any)
Abramo Bagnara55d23c92012-02-06 14:41:24 +00002204 // and tag keyword.
Douglas Gregor059101f2011-03-02 00:47:37 +00002205 Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result);
2206 ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00002207 ElabTL.setElaboratedKeywordLoc(TagLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00002208 ElabTL.setQualifierLoc(SS.getWithLocInContext(Context));
2209 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
Douglas Gregor55f6b142009-02-09 18:46:07 +00002210}
2211
John McCall60d7b3a2010-08-24 06:29:42 +00002212ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002213 SourceLocation TemplateKWLoc,
Douglas Gregor4c9be892011-02-28 20:01:57 +00002214 LookupResult &R,
2215 bool RequiresADL,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002216 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002217 // FIXME: Can we do any checking at this point? I guess we could check the
2218 // template arguments that we have against the template name, if the template
Mike Stump1eb44332009-09-09 15:08:12 +00002219 // name refers to a single template. That's not a terribly common case,
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002220 // though.
Douglas Gregor1be8eec2011-02-19 21:32:49 +00002221 // foo<int> could identify a single function unambiguously
2222 // This approach does NOT work, since f<int>(1);
2223 // gets resolved prior to resorting to overload resolution
2224 // i.e., template<class T> void f(double);
2225 // vs template<class T, class U> void f(U);
John McCallf7a1a742009-11-24 19:00:30 +00002226
2227 // These should be filtered out by our callers.
2228 assert(!R.empty() && "empty lookup results when building templateid");
2229 assert(!R.isAmbiguous() && "ambiguous lookup when building templateid");
2230
John McCallc373d482010-01-27 01:50:18 +00002231 // We don't want lookup warnings at this point.
2232 R.suppressDiagnostics();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002233
John McCallf7a1a742009-11-24 19:00:30 +00002234 UnresolvedLookupExpr *ULE
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002235 = UnresolvedLookupExpr::Create(Context, R.getNamingClass(),
Douglas Gregor4c9be892011-02-28 20:01:57 +00002236 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002237 TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002238 R.getLookupNameInfo(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002239 RequiresADL, TemplateArgs,
Douglas Gregor5a84dec2010-05-23 18:57:34 +00002240 R.begin(), R.end());
John McCallf7a1a742009-11-24 19:00:30 +00002241
2242 return Owned(ULE);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002243}
2244
John McCallf7a1a742009-11-24 19:00:30 +00002245// We actually only call this from template instantiation.
John McCall60d7b3a2010-08-24 06:29:42 +00002246ExprResult
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002247Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002248 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002249 const DeclarationNameInfo &NameInfo,
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002250 const TemplateArgumentListInfo *TemplateArgs) {
2251 assert(TemplateArgs || TemplateKWLoc.isValid());
John McCallf7a1a742009-11-24 19:00:30 +00002252 DeclContext *DC;
2253 if (!(DC = computeDeclContext(SS, false)) ||
2254 DC->isDependentContext() ||
John McCall77bb1aa2010-05-01 00:40:08 +00002255 RequireCompleteDeclContext(SS, DC))
Abramo Bagnara9d9922a2012-02-06 14:31:00 +00002256 return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs);
Mike Stump1eb44332009-09-09 15:08:12 +00002257
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002258 bool MemberOfUnknownSpecialization;
Abramo Bagnara25777432010-08-11 22:01:17 +00002259 LookupResult R(*this, NameInfo, LookupOrdinaryName);
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002260 LookupTemplateName(R, (Scope*) 0, SS, QualType(), /*Entering*/ false,
2261 MemberOfUnknownSpecialization);
Mike Stump1eb44332009-09-09 15:08:12 +00002262
John McCallf7a1a742009-11-24 19:00:30 +00002263 if (R.isAmbiguous())
2264 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002265
John McCallf7a1a742009-11-24 19:00:30 +00002266 if (R.empty()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002267 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_non_template)
2268 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002269 return ExprError();
2270 }
2271
2272 if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002273 Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template)
2274 << (NestedNameSpecifier*) SS.getScopeRep()
2275 << NameInfo.getName() << SS.getRange();
John McCallf7a1a742009-11-24 19:00:30 +00002276 Diag(Temp->getLocation(), diag::note_referenced_class_template);
2277 return ExprError();
2278 }
2279
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002280 return BuildTemplateIdExpr(SS, TemplateKWLoc, R, /*ADL*/ false, TemplateArgs);
Douglas Gregoredce4dd2009-06-30 22:34:41 +00002281}
2282
Douglas Gregorc45c2322009-03-31 00:43:58 +00002283/// \brief Form a dependent template name.
2284///
2285/// This action forms a dependent template name given the template
2286/// name and its (presumably dependent) scope specifier. For
2287/// example, given "MetaFun::template apply", the scope specifier \p
2288/// SS will be "MetaFun::", \p TemplateKWLoc contains the location
2289/// of the "template" keyword, and "apply" is the \p Name.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002290TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002291 CXXScopeSpec &SS,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002292 SourceLocation TemplateKWLoc,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002293 UnqualifiedId &Name,
John McCallb3d87482010-08-24 05:47:05 +00002294 ParsedType ObjectType,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002295 bool EnteringContext,
2296 TemplateTy &Result) {
Richard Smithebaf0e62011-10-18 20:49:44 +00002297 if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent())
2298 Diag(TemplateKWLoc,
2299 getLangOptions().CPlusPlus0x ?
2300 diag::warn_cxx98_compat_template_outside_of_template :
2301 diag::ext_template_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002302 << FixItHint::CreateRemoval(TemplateKWLoc);
2303
Douglas Gregor0707bc52010-01-19 16:01:07 +00002304 DeclContext *LookupCtx = 0;
2305 if (SS.isSet())
2306 LookupCtx = computeDeclContext(SS, EnteringContext);
2307 if (!LookupCtx && ObjectType)
John McCallb3d87482010-08-24 05:47:05 +00002308 LookupCtx = computeDeclContext(ObjectType.get());
Douglas Gregor0707bc52010-01-19 16:01:07 +00002309 if (LookupCtx) {
Douglas Gregorc45c2322009-03-31 00:43:58 +00002310 // C++0x [temp.names]p5:
2311 // If a name prefixed by the keyword template is not the name of
2312 // a template, the program is ill-formed. [Note: the keyword
2313 // template may not be applied to non-template members of class
2314 // templates. -end note ] [ Note: as is the case with the
2315 // typename prefix, the template prefix is allowed in cases
2316 // where it is not strictly necessary; i.e., when the
2317 // nested-name-specifier or the expression on the left of the ->
2318 // or . is not dependent on a template-parameter, or the use
2319 // does not appear in the scope of a template. -end note]
2320 //
2321 // Note: C++03 was more strict here, because it banned the use of
2322 // the "template" keyword prior to a template-name that was not a
2323 // dependent name. C++ DR468 relaxed this requirement (the
2324 // "template" keyword is now permitted). We follow the C++0x
Douglas Gregor732281d2010-06-14 22:07:54 +00002325 // rules, even in C++03 mode with a warning, retroactively applying the DR.
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002326 bool MemberOfUnknownSpecialization;
Abramo Bagnara7c153532010-08-06 12:11:11 +00002327 TemplateNameKind TNK = isTemplateName(0, SS, TemplateKWLoc.isValid(), Name,
2328 ObjectType, EnteringContext, Result,
Douglas Gregor1fd6d442010-05-21 23:18:07 +00002329 MemberOfUnknownSpecialization);
Douglas Gregor0707bc52010-01-19 16:01:07 +00002330 if (TNK == TNK_Non_template && LookupCtx->isDependentContext() &&
2331 isa<CXXRecordDecl>(LookupCtx) &&
Douglas Gregord078bd22011-03-11 23:27:41 +00002332 (!cast<CXXRecordDecl>(LookupCtx)->hasDefinition() ||
2333 cast<CXXRecordDecl>(LookupCtx)->hasAnyDependentBases())) {
Douglas Gregord6ab2322010-06-16 23:00:59 +00002334 // This is a dependent template. Handle it below.
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002335 } else if (TNK == TNK_Non_template) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002336 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002337 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002338 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002339 << Name.getSourceRange()
2340 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002341 return TNK_Non_template;
Douglas Gregor9edad9b2010-01-14 17:47:39 +00002342 } else {
2343 // We found something; return it.
Douglas Gregord6ab2322010-06-16 23:00:59 +00002344 return TNK;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002345 }
Douglas Gregorc45c2322009-03-31 00:43:58 +00002346 }
2347
Mike Stump1eb44332009-09-09 15:08:12 +00002348 NestedNameSpecifier *Qualifier
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002349 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002350
Douglas Gregor014e88d2009-11-03 23:16:33 +00002351 switch (Name.getKind()) {
2352 case UnqualifiedId::IK_Identifier:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002353 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregord6ab2322010-06-16 23:00:59 +00002354 Name.Identifier));
2355 return TNK_Dependent_template_name;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002356
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002357 case UnqualifiedId::IK_OperatorFunctionId:
Douglas Gregord6ab2322010-06-16 23:00:59 +00002358 Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier,
Douglas Gregorca1bdd72009-11-04 00:56:37 +00002359 Name.OperatorFunctionId.Operator));
Douglas Gregord6ab2322010-06-16 23:00:59 +00002360 return TNK_Dependent_template_name;
Sean Hunte6252d12009-11-28 08:58:14 +00002361
2362 case UnqualifiedId::IK_LiteralOperatorId:
David Blaikieb219cfc2011-09-23 05:06:16 +00002363 llvm_unreachable(
2364 "We don't support these; Parse shouldn't have allowed propagation");
Sean Hunte6252d12009-11-28 08:58:14 +00002365
Douglas Gregor014e88d2009-11-03 23:16:33 +00002366 default:
2367 break;
2368 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002369
2370 Diag(Name.getSourceRange().getBegin(),
Douglas Gregor014e88d2009-11-03 23:16:33 +00002371 diag::err_template_kw_refers_to_non_template)
Abramo Bagnara25777432010-08-11 22:01:17 +00002372 << GetNameFromUnqualifiedId(Name).getName()
Douglas Gregor0278e122010-05-05 05:58:24 +00002373 << Name.getSourceRange()
2374 << TemplateKWLoc;
Douglas Gregord6ab2322010-06-16 23:00:59 +00002375 return TNK_Non_template;
Douglas Gregorc45c2322009-03-31 00:43:58 +00002376}
2377
Mike Stump1eb44332009-09-09 15:08:12 +00002378bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param,
John McCall833ca992009-10-29 08:12:44 +00002379 const TemplateArgumentLoc &AL,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002380 SmallVectorImpl<TemplateArgument> &Converted) {
John McCall833ca992009-10-29 08:12:44 +00002381 const TemplateArgument &Arg = AL.getArgument();
2382
Anders Carlsson436b1562009-06-13 00:33:33 +00002383 // Check template type parameter.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002384 switch(Arg.getKind()) {
2385 case TemplateArgument::Type:
Anders Carlsson436b1562009-06-13 00:33:33 +00002386 // C++ [temp.arg.type]p1:
2387 // A template-argument for a template-parameter which is a
2388 // type shall be a type-id.
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002389 break;
2390 case TemplateArgument::Template: {
2391 // We have a template type parameter but the template argument
2392 // is a template without any arguments.
2393 SourceRange SR = AL.getSourceRange();
2394 TemplateName Name = Arg.getAsTemplate();
2395 Diag(SR.getBegin(), diag::err_template_missing_args)
2396 << Name << SR;
2397 if (TemplateDecl *Decl = Name.getAsTemplateDecl())
2398 Diag(Decl->getLocation(), diag::note_template_decl_here);
Anders Carlsson436b1562009-06-13 00:33:33 +00002399
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002400 return true;
2401 }
2402 default: {
Anders Carlsson436b1562009-06-13 00:33:33 +00002403 // We have a template type parameter but the template argument
2404 // is not a type.
John McCall828bff22009-10-29 18:45:58 +00002405 SourceRange SR = AL.getSourceRange();
2406 Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR;
Anders Carlsson436b1562009-06-13 00:33:33 +00002407 Diag(Param->getLocation(), diag::note_template_param_here);
Mike Stump1eb44332009-09-09 15:08:12 +00002408
Anders Carlsson436b1562009-06-13 00:33:33 +00002409 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002410 }
Jeffrey Yasskindb88d8a2010-04-08 00:03:06 +00002411 }
Anders Carlsson436b1562009-06-13 00:33:33 +00002412
John McCalla93c9342009-12-07 02:54:59 +00002413 if (CheckTemplateArgument(Param, AL.getTypeSourceInfo()))
Anders Carlsson436b1562009-06-13 00:33:33 +00002414 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002415
Anders Carlsson436b1562009-06-13 00:33:33 +00002416 // Add the converted template type argument.
Douglas Gregore559ca12011-06-17 22:11:49 +00002417 QualType ArgType = Context.getCanonicalType(Arg.getAsType());
2418
2419 // Objective-C ARC:
2420 // If an explicitly-specified template argument type is a lifetime type
2421 // with no lifetime qualifier, the __strong lifetime qualifier is inferred.
2422 if (getLangOptions().ObjCAutoRefCount &&
2423 ArgType->isObjCLifetimeType() &&
2424 !ArgType.getObjCLifetime()) {
2425 Qualifiers Qs;
2426 Qs.setObjCLifetime(Qualifiers::OCL_Strong);
2427 ArgType = Context.getQualifiedType(ArgType, Qs);
2428 }
2429
2430 Converted.push_back(TemplateArgument(ArgType));
Anders Carlsson436b1562009-06-13 00:33:33 +00002431 return false;
2432}
2433
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002434/// \brief Substitute template arguments into the default template argument for
2435/// the given template type parameter.
2436///
2437/// \param SemaRef the semantic analysis object for which we are performing
2438/// the substitution.
2439///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002440/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002441/// for.
2442///
2443/// \param TemplateLoc the location of the template name that started the
2444/// template-id we are checking.
2445///
2446/// \param RAngleLoc the location of the right angle bracket ('>') that
2447/// terminates the template-id.
2448///
2449/// \param Param the template template parameter whose default we are
2450/// substituting into.
2451///
2452/// \param Converted the list of template arguments provided for template
2453/// parameters that precede \p Param in the template parameter list.
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002454/// \returns the substituted template argument, or NULL if an error occurred.
John McCalla93c9342009-12-07 02:54:59 +00002455static TypeSourceInfo *
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002456SubstDefaultTemplateArgument(Sema &SemaRef,
2457 TemplateDecl *Template,
2458 SourceLocation TemplateLoc,
2459 SourceLocation RAngleLoc,
2460 TemplateTypeParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002461 SmallVectorImpl<TemplateArgument> &Converted) {
John McCalla93c9342009-12-07 02:54:59 +00002462 TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo();
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002463
2464 // If the argument type is dependent, instantiate it now based
2465 // on the previously-computed template arguments.
2466 if (ArgType->getType()->isDependentType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002467 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002468 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002469
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002470 MultiLevelTemplateArgumentList AllTemplateArgs
2471 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
2472
2473 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002474 Template, Converted.data(),
2475 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002476 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002477
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002478 ArgType = SemaRef.SubstType(ArgType, AllTemplateArgs,
2479 Param->getDefaultArgumentLoc(),
2480 Param->getDeclName());
2481 }
2482
2483 return ArgType;
2484}
2485
2486/// \brief Substitute template arguments into the default template argument for
2487/// the given non-type template parameter.
2488///
2489/// \param SemaRef the semantic analysis object for which we are performing
2490/// the substitution.
2491///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002492/// \param Template the template that we are synthesizing template arguments
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002493/// for.
2494///
2495/// \param TemplateLoc the location of the template name that started the
2496/// template-id we are checking.
2497///
2498/// \param RAngleLoc the location of the right angle bracket ('>') that
2499/// terminates the template-id.
2500///
Douglas Gregor788cd062009-11-11 01:00:40 +00002501/// \param Param the non-type template parameter whose default we are
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002502/// substituting into.
2503///
2504/// \param Converted the list of template arguments provided for template
2505/// parameters that precede \p Param in the template parameter list.
2506///
2507/// \returns the substituted template argument, or NULL if an error occurred.
John McCall60d7b3a2010-08-24 06:29:42 +00002508static ExprResult
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002509SubstDefaultTemplateArgument(Sema &SemaRef,
2510 TemplateDecl *Template,
2511 SourceLocation TemplateLoc,
2512 SourceLocation RAngleLoc,
2513 NonTypeTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002514 SmallVectorImpl<TemplateArgument> &Converted) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002515 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002516 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002517
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002518 MultiLevelTemplateArgumentList AllTemplateArgs
2519 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002520
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002521 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002522 Template, Converted.data(),
2523 Converted.size(),
Douglas Gregor0f8716b2009-11-09 19:17:50 +00002524 SourceRange(TemplateLoc, RAngleLoc));
2525
2526 return SemaRef.SubstExpr(Param->getDefaultArgument(), AllTemplateArgs);
2527}
2528
Douglas Gregor788cd062009-11-11 01:00:40 +00002529/// \brief Substitute template arguments into the default template argument for
2530/// the given template template parameter.
2531///
2532/// \param SemaRef the semantic analysis object for which we are performing
2533/// the substitution.
2534///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002535/// \param Template the template that we are synthesizing template arguments
Douglas Gregor788cd062009-11-11 01:00:40 +00002536/// for.
2537///
2538/// \param TemplateLoc the location of the template name that started the
2539/// template-id we are checking.
2540///
2541/// \param RAngleLoc the location of the right angle bracket ('>') that
2542/// terminates the template-id.
2543///
2544/// \param Param the template template parameter whose default we are
2545/// substituting into.
2546///
2547/// \param Converted the list of template arguments provided for template
2548/// parameters that precede \p Param in the template parameter list.
2549///
Douglas Gregor1d752d72011-03-02 18:46:51 +00002550/// \param QualifierLoc Will be set to the nested-name-specifier (with
2551/// source-location information) that precedes the template name.
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002552///
Douglas Gregor788cd062009-11-11 01:00:40 +00002553/// \returns the substituted template argument, or NULL if an error occurred.
2554static TemplateName
2555SubstDefaultTemplateArgument(Sema &SemaRef,
2556 TemplateDecl *Template,
2557 SourceLocation TemplateLoc,
2558 SourceLocation RAngleLoc,
2559 TemplateTemplateParmDecl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002560 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002561 NestedNameSpecifierLoc &QualifierLoc) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002562 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002563 Converted.data(), Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002564
Douglas Gregor788cd062009-11-11 01:00:40 +00002565 MultiLevelTemplateArgumentList AllTemplateArgs
2566 = SemaRef.getTemplateInstantiationArgs(Template, &TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002567
Douglas Gregor788cd062009-11-11 01:00:40 +00002568 Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc,
Douglas Gregor910f8002010-11-07 23:05:16 +00002569 Template, Converted.data(),
2570 Converted.size(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002571 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002572
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002573 // Substitute into the nested-name-specifier first,
Douglas Gregor1d752d72011-03-02 18:46:51 +00002574 QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc();
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002575 if (QualifierLoc) {
2576 QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc,
2577 AllTemplateArgs);
2578 if (!QualifierLoc)
2579 return TemplateName();
2580 }
2581
Douglas Gregor1d752d72011-03-02 18:46:51 +00002582 return SemaRef.SubstTemplateName(QualifierLoc,
Douglas Gregor788cd062009-11-11 01:00:40 +00002583 Param->getDefaultArgument().getArgument().getAsTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002584 Param->getDefaultArgument().getTemplateNameLoc(),
Douglas Gregor788cd062009-11-11 01:00:40 +00002585 AllTemplateArgs);
2586}
2587
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002588/// \brief If the given template parameter has a default template
2589/// argument, substitute into that default template argument and
2590/// return the corresponding template argument.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002591TemplateArgumentLoc
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002592Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template,
2593 SourceLocation TemplateLoc,
2594 SourceLocation RAngleLoc,
2595 Decl *Param,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002596 SmallVectorImpl<TemplateArgument> &Converted) {
Douglas Gregor910f8002010-11-07 23:05:16 +00002597 if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) {
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002598 if (!TypeParm->hasDefaultArgument())
2599 return TemplateArgumentLoc();
2600
John McCalla93c9342009-12-07 02:54:59 +00002601 TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002602 TemplateLoc,
2603 RAngleLoc,
2604 TypeParm,
2605 Converted);
2606 if (DI)
2607 return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI);
2608
2609 return TemplateArgumentLoc();
2610 }
2611
2612 if (NonTypeTemplateParmDecl *NonTypeParm
2613 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2614 if (!NonTypeParm->hasDefaultArgument())
2615 return TemplateArgumentLoc();
2616
John McCall60d7b3a2010-08-24 06:29:42 +00002617 ExprResult Arg = SubstDefaultTemplateArgument(*this, Template,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002618 TemplateLoc,
2619 RAngleLoc,
2620 NonTypeParm,
2621 Converted);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002622 if (Arg.isInvalid())
2623 return TemplateArgumentLoc();
2624
2625 Expr *ArgE = Arg.takeAs<Expr>();
2626 return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE);
2627 }
2628
2629 TemplateTemplateParmDecl *TempTempParm
2630 = cast<TemplateTemplateParmDecl>(Param);
2631 if (!TempTempParm->hasDefaultArgument())
2632 return TemplateArgumentLoc();
2633
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002634
Douglas Gregor1d752d72011-03-02 18:46:51 +00002635 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002636 TemplateName TName = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002637 TemplateLoc,
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002638 RAngleLoc,
2639 TempTempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002640 Converted,
2641 QualifierLoc);
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002642 if (TName.isNull())
2643 return TemplateArgumentLoc();
2644
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002645 return TemplateArgumentLoc(TemplateArgument(TName),
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002646 TempTempParm->getDefaultArgument().getTemplateQualifierLoc(),
Douglas Gregor51ffb0c2009-11-25 18:55:14 +00002647 TempTempParm->getDefaultArgument().getTemplateNameLoc());
2648}
2649
Douglas Gregore7526412009-11-11 19:31:23 +00002650/// \brief Check that the given template argument corresponds to the given
2651/// template parameter.
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002652///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002653/// \param Param The template parameter against which the argument will be
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002654/// checked.
2655///
2656/// \param Arg The template argument.
2657///
2658/// \param Template The template in which the template argument resides.
2659///
2660/// \param TemplateLoc The location of the template name for the template
2661/// whose argument list we're matching.
2662///
2663/// \param RAngleLoc The location of the right angle bracket ('>') that closes
2664/// the template argument list.
2665///
2666/// \param ArgumentPackIndex The index into the argument pack where this
2667/// argument will be placed. Only valid if the parameter is a parameter pack.
2668///
2669/// \param Converted The checked, converted argument will be added to the
2670/// end of this small vector.
2671///
2672/// \param CTAK Describes how we arrived at this particular template argument:
2673/// explicitly written, deduced, etc.
2674///
2675/// \returns true on error, false otherwise.
Douglas Gregore7526412009-11-11 19:31:23 +00002676bool Sema::CheckTemplateArgument(NamedDecl *Param,
2677 const TemplateArgumentLoc &Arg,
Douglas Gregor54c53cc2011-01-04 23:35:54 +00002678 NamedDecl *Template,
Douglas Gregore7526412009-11-11 19:31:23 +00002679 SourceLocation TemplateLoc,
Douglas Gregore7526412009-11-11 19:31:23 +00002680 SourceLocation RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002681 unsigned ArgumentPackIndex,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002682 SmallVectorImpl<TemplateArgument> &Converted,
Douglas Gregor02024a92010-03-28 02:42:43 +00002683 CheckTemplateArgumentKind CTAK) {
Douglas Gregord9e15302009-11-11 19:41:09 +00002684 // Check template type parameters.
2685 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
Douglas Gregore7526412009-11-11 19:31:23 +00002686 return CheckTemplateTypeArgument(TTP, Arg, Converted);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002687
Douglas Gregord9e15302009-11-11 19:41:09 +00002688 // Check non-type template parameters.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002689 if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) {
Douglas Gregore7526412009-11-11 19:31:23 +00002690 // Do substitution on the type of the non-type template parameter
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002691 // with the template arguments we've seen thus far. But if the
2692 // template has a dependent context then we cannot substitute yet.
Douglas Gregore7526412009-11-11 19:31:23 +00002693 QualType NTTPType = NTTP->getType();
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002694 if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack())
2695 NTTPType = NTTP->getExpansionType(ArgumentPackIndex);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002696
Peter Collingbourne9f6f6a12010-12-10 17:08:53 +00002697 if (NTTPType->isDependentType() &&
2698 !isa<TemplateTemplateParmDecl>(Template) &&
2699 !Template->getDeclContext()->isDependentContext()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002700 // Do substitution on the type of the non-type template parameter.
2701 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002702 NTTP, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002703 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002704
2705 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002706 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002707 NTTPType = SubstType(NTTPType,
2708 MultiLevelTemplateArgumentList(TemplateArgs),
2709 NTTP->getLocation(),
2710 NTTP->getDeclName());
2711 // If that worked, check the non-type template parameter type
2712 // for validity.
2713 if (!NTTPType.isNull())
2714 NTTPType = CheckNonTypeTemplateParameterType(NTTPType,
2715 NTTP->getLocation());
2716 if (NTTPType.isNull())
2717 return true;
2718 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002719
Douglas Gregore7526412009-11-11 19:31:23 +00002720 switch (Arg.getArgument().getKind()) {
2721 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002722 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002723
Douglas Gregore7526412009-11-11 19:31:23 +00002724 case TemplateArgument::Expression: {
Douglas Gregore7526412009-11-11 19:31:23 +00002725 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002726 ExprResult Res =
2727 CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(),
2728 Result, CTAK);
2729 if (Res.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002730 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002731
Douglas Gregor910f8002010-11-07 23:05:16 +00002732 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002733 break;
2734 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002735
Douglas Gregore7526412009-11-11 19:31:23 +00002736 case TemplateArgument::Declaration:
2737 case TemplateArgument::Integral:
2738 // We've already checked this template argument, so just copy
2739 // it to the list of converted arguments.
Douglas Gregor910f8002010-11-07 23:05:16 +00002740 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002741 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002742
Douglas Gregore7526412009-11-11 19:31:23 +00002743 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002744 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002745 // We were given a template template argument. It may not be ill-formed;
2746 // see below.
2747 if (DependentTemplateName *DTN
Douglas Gregora7fc9012011-01-05 18:58:31 +00002748 = Arg.getArgument().getAsTemplateOrTemplatePattern()
2749 .getAsDependentTemplateName()) {
Douglas Gregore7526412009-11-11 19:31:23 +00002750 // We have a template argument such as \c T::template X, which we
2751 // parsed as a template template argument. However, since we now
2752 // know that we need a non-type template argument, convert this
Abramo Bagnara25777432010-08-11 22:01:17 +00002753 // template name into an expression.
2754
2755 DeclarationNameInfo NameInfo(DTN->getIdentifier(),
2756 Arg.getTemplateNameLoc());
2757
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002758 CXXScopeSpec SS;
Douglas Gregorb6744ef2011-03-02 17:09:35 +00002759 SS.Adopt(Arg.getTemplateQualifierLoc());
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002760 // FIXME: the template-template arg was a DependentTemplateName,
2761 // so it was provided with a template keyword. However, its source
2762 // location is not stored in the template argument structure.
2763 SourceLocation TemplateKWLoc;
John Wiegley429bb272011-04-08 18:41:53 +00002764 ExprResult E = Owned(DependentScopeDeclRefExpr::Create(Context,
Douglas Gregor00cf3cc2011-02-25 20:49:16 +00002765 SS.getWithLocInContext(Context),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00002766 TemplateKWLoc,
2767 NameInfo, 0));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002768
Douglas Gregora7fc9012011-01-05 18:58:31 +00002769 // If we parsed the template argument as a pack expansion, create a
2770 // pack expansion expression.
2771 if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){
John Wiegley429bb272011-04-08 18:41:53 +00002772 E = ActOnPackExpansion(E.take(), Arg.getTemplateEllipsisLoc());
2773 if (E.isInvalid())
Douglas Gregora7fc9012011-01-05 18:58:31 +00002774 return true;
Douglas Gregora7fc9012011-01-05 18:58:31 +00002775 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002776
Douglas Gregore7526412009-11-11 19:31:23 +00002777 TemplateArgument Result;
John Wiegley429bb272011-04-08 18:41:53 +00002778 E = CheckTemplateArgument(NTTP, NTTPType, E.take(), Result);
2779 if (E.isInvalid())
Douglas Gregore7526412009-11-11 19:31:23 +00002780 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002781
Douglas Gregor910f8002010-11-07 23:05:16 +00002782 Converted.push_back(Result);
Douglas Gregore7526412009-11-11 19:31:23 +00002783 break;
2784 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002785
Douglas Gregore7526412009-11-11 19:31:23 +00002786 // We have a template argument that actually does refer to a class
Richard Smith3e4c6c42011-05-05 21:57:07 +00002787 // template, alias template, or template template parameter, and
Douglas Gregore7526412009-11-11 19:31:23 +00002788 // therefore cannot be a non-type template argument.
2789 Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr)
2790 << Arg.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002791
Douglas Gregore7526412009-11-11 19:31:23 +00002792 Diag(Param->getLocation(), diag::note_template_param_here);
2793 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002794
Douglas Gregore7526412009-11-11 19:31:23 +00002795 case TemplateArgument::Type: {
2796 // We have a non-type template parameter but the template
2797 // argument is a type.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002798
Douglas Gregore7526412009-11-11 19:31:23 +00002799 // C++ [temp.arg]p2:
2800 // In a template-argument, an ambiguity between a type-id and
2801 // an expression is resolved to a type-id, regardless of the
2802 // form of the corresponding template-parameter.
2803 //
2804 // We warn specifically about this case, since it can be rather
2805 // confusing for users.
2806 QualType T = Arg.getArgument().getAsType();
2807 SourceRange SR = Arg.getSourceRange();
2808 if (T->isFunctionType())
2809 Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T;
2810 else
2811 Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR;
2812 Diag(Param->getLocation(), diag::note_template_param_here);
2813 return true;
2814 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002815
Douglas Gregore7526412009-11-11 19:31:23 +00002816 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002817 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002818 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002819
Douglas Gregore7526412009-11-11 19:31:23 +00002820 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002821 }
2822
2823
Douglas Gregore7526412009-11-11 19:31:23 +00002824 // Check template template parameters.
2825 TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002826
Douglas Gregore7526412009-11-11 19:31:23 +00002827 // Substitute into the template parameter list of the template
2828 // template parameter, since previously-supplied template arguments
2829 // may appear within the template template parameter.
2830 {
2831 // Set up a template instantiation context.
2832 LocalInstantiationScope Scope(*this);
2833 InstantiatingTemplate Inst(*this, TemplateLoc, Template,
Douglas Gregor910f8002010-11-07 23:05:16 +00002834 TempParm, Converted.data(), Converted.size(),
Douglas Gregore7526412009-11-11 19:31:23 +00002835 SourceRange(TemplateLoc, RAngleLoc));
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002836
2837 TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
Douglas Gregor910f8002010-11-07 23:05:16 +00002838 Converted.data(), Converted.size());
Douglas Gregore7526412009-11-11 19:31:23 +00002839 TempParm = cast_or_null<TemplateTemplateParmDecl>(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002840 SubstDecl(TempParm, CurContext,
Douglas Gregore7526412009-11-11 19:31:23 +00002841 MultiLevelTemplateArgumentList(TemplateArgs)));
2842 if (!TempParm)
2843 return true;
Douglas Gregore7526412009-11-11 19:31:23 +00002844 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002845
Douglas Gregore7526412009-11-11 19:31:23 +00002846 switch (Arg.getArgument().getKind()) {
2847 case TemplateArgument::Null:
David Blaikieb219cfc2011-09-23 05:06:16 +00002848 llvm_unreachable("Should never see a NULL template argument here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002849
Douglas Gregore7526412009-11-11 19:31:23 +00002850 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +00002851 case TemplateArgument::TemplateExpansion:
Douglas Gregore7526412009-11-11 19:31:23 +00002852 if (CheckTemplateArgument(TempParm, Arg))
2853 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002854
Douglas Gregor910f8002010-11-07 23:05:16 +00002855 Converted.push_back(Arg.getArgument());
Douglas Gregore7526412009-11-11 19:31:23 +00002856 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002857
Douglas Gregore7526412009-11-11 19:31:23 +00002858 case TemplateArgument::Expression:
2859 case TemplateArgument::Type:
2860 // We have a template template parameter but the template
2861 // argument does not refer to a template.
Richard Smith3e4c6c42011-05-05 21:57:07 +00002862 Diag(Arg.getLocation(), diag::err_template_arg_must_be_template)
2863 << getLangOptions().CPlusPlus0x;
Douglas Gregore7526412009-11-11 19:31:23 +00002864 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002865
Douglas Gregore7526412009-11-11 19:31:23 +00002866 case TemplateArgument::Declaration:
David Blaikie7530c032012-01-17 06:56:22 +00002867 llvm_unreachable("Declaration argument with template template parameter");
Douglas Gregore7526412009-11-11 19:31:23 +00002868 case TemplateArgument::Integral:
David Blaikie7530c032012-01-17 06:56:22 +00002869 llvm_unreachable("Integral argument with template template parameter");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002870
Douglas Gregore7526412009-11-11 19:31:23 +00002871 case TemplateArgument::Pack:
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +00002872 llvm_unreachable("Caller must expand template argument packs");
Douglas Gregore7526412009-11-11 19:31:23 +00002873 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002874
Douglas Gregore7526412009-11-11 19:31:23 +00002875 return false;
2876}
2877
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002878/// \brief Diagnose an arity mismatch in the
2879static bool diagnoseArityMismatch(Sema &S, TemplateDecl *Template,
2880 SourceLocation TemplateLoc,
2881 TemplateArgumentListInfo &TemplateArgs) {
2882 TemplateParameterList *Params = Template->getTemplateParameters();
2883 unsigned NumParams = Params->size();
2884 unsigned NumArgs = TemplateArgs.size();
2885
2886 SourceRange Range;
2887 if (NumArgs > NumParams)
2888 Range = SourceRange(TemplateArgs[NumParams].getLocation(),
2889 TemplateArgs.getRAngleLoc());
2890 S.Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2891 << (NumArgs > NumParams)
2892 << (isa<ClassTemplateDecl>(Template)? 0 :
2893 isa<FunctionTemplateDecl>(Template)? 1 :
2894 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2895 << Template << Range;
2896 S.Diag(Template->getLocation(), diag::note_template_decl_here)
2897 << Params->getSourceRange();
2898 return true;
2899}
2900
Douglas Gregorc15cb382009-02-09 23:23:08 +00002901/// \brief Check that the given template argument list is well-formed
2902/// for specializing the given template.
2903bool Sema::CheckTemplateArgumentList(TemplateDecl *Template,
2904 SourceLocation TemplateLoc,
Douglas Gregor67714232011-03-03 02:41:12 +00002905 TemplateArgumentListInfo &TemplateArgs,
Douglas Gregor16134c62009-07-01 00:28:38 +00002906 bool PartialTemplateArgs,
Douglas Gregorb70126a2012-02-03 17:16:23 +00002907 SmallVectorImpl<TemplateArgument> &Converted,
2908 bool *ExpansionIntoFixedList) {
2909 if (ExpansionIntoFixedList)
2910 *ExpansionIntoFixedList = false;
2911
Douglas Gregorc15cb382009-02-09 23:23:08 +00002912 TemplateParameterList *Params = Template->getTemplateParameters();
2913 unsigned NumParams = Params->size();
John McCalld5532b62009-11-23 01:53:49 +00002914 unsigned NumArgs = TemplateArgs.size();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002915 bool Invalid = false;
2916
John McCalld5532b62009-11-23 01:53:49 +00002917 SourceLocation RAngleLoc = TemplateArgs.getRAngleLoc();
2918
Mike Stump1eb44332009-09-09 15:08:12 +00002919 bool HasParameterPack =
Anders Carlsson0ceffb52009-06-13 02:08:00 +00002920 NumParams > 0 && Params->getParam(NumParams - 1)->isTemplateParameterPack();
Douglas Gregorb70126a2012-02-03 17:16:23 +00002921
Mike Stump1eb44332009-09-09 15:08:12 +00002922 // C++ [temp.arg]p1:
Douglas Gregorc15cb382009-02-09 23:23:08 +00002923 // [...] The type and form of each template-argument specified in
2924 // a template-id shall match the type and form specified for the
2925 // corresponding parameter declared by the template in its
2926 // template-parameter-list.
Douglas Gregor67714232011-03-03 02:41:12 +00002927 bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002928 SmallVector<TemplateArgument, 2> ArgumentPack;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002929 TemplateParameterList::iterator Param = Params->begin(),
2930 ParamEnd = Params->end();
Douglas Gregorc15cb382009-02-09 23:23:08 +00002931 unsigned ArgIdx = 0;
Douglas Gregor8dde14e2011-01-24 16:14:37 +00002932 LocalInstantiationScope InstScope(*this, true);
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002933 bool SawPackExpansion = false;
Douglas Gregor14be16b2010-12-20 16:57:52 +00002934 while (Param != ParamEnd) {
Douglas Gregorf35f8282009-11-11 21:54:23 +00002935 if (ArgIdx < NumArgs) {
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002936 // If we have an expanded parameter pack, make sure we don't have too
2937 // many arguments.
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002938 // FIXME: This really should fall out from the normal arity checking.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002939 if (NonTypeTemplateParmDecl *NTTP
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002940 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002941 if (NTTP->isExpandedParameterPack() &&
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002942 ArgumentPack.size() >= NTTP->getNumExpansionTypes()) {
2943 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
2944 << true
2945 << (isa<ClassTemplateDecl>(Template)? 0 :
2946 isa<FunctionTemplateDecl>(Template)? 1 :
2947 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
2948 << Template;
2949 Diag(Template->getLocation(), diag::note_template_decl_here)
2950 << Params->getSourceRange();
2951 return true;
2952 }
2953 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002954
Douglas Gregorf35f8282009-11-11 21:54:23 +00002955 // Check the template argument we were given.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002956 if (CheckTemplateArgument(*Param, TemplateArgs[ArgIdx], Template,
2957 TemplateLoc, RAngleLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00002958 ArgumentPack.size(), Converted))
Douglas Gregorf35f8282009-11-11 21:54:23 +00002959 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002960
Douglas Gregor14be16b2010-12-20 16:57:52 +00002961 if ((*Param)->isTemplateParameterPack()) {
2962 // The template parameter was a template parameter pack, so take the
2963 // deduced argument and place it on the argument pack. Note that we
2964 // stay on the same template parameter so that we can deduce more
2965 // arguments.
2966 ArgumentPack.push_back(Converted.back());
2967 Converted.pop_back();
2968 } else {
2969 // Move to the next template parameter.
2970 ++Param;
2971 }
Douglas Gregor8fbbae52012-02-03 07:34:46 +00002972
2973 // If this template argument is a pack expansion, record that fact
2974 // and break out; we can't actually check any more.
2975 if (TemplateArgs[ArgIdx].getArgument().isPackExpansion()) {
2976 SawPackExpansion = true;
2977 ++ArgIdx;
2978 break;
2979 }
2980
Douglas Gregor14be16b2010-12-20 16:57:52 +00002981 ++ArgIdx;
Douglas Gregorf35f8282009-11-11 21:54:23 +00002982 continue;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00002983 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002984
Douglas Gregor8735b292011-06-03 02:59:40 +00002985 // If we're checking a partial template argument list, we're done.
2986 if (PartialTemplateArgs) {
2987 if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty())
2988 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
2989 ArgumentPack.data(),
2990 ArgumentPack.size()));
2991
2992 return Invalid;
2993 }
2994
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00002995 // If we have a template parameter pack with no more corresponding
Douglas Gregor14be16b2010-12-20 16:57:52 +00002996 // arguments, just break out now and we'll fill in the argument pack below.
2997 if ((*Param)->isTemplateParameterPack())
2998 break;
Douglas Gregorf968d832011-05-27 01:19:52 +00002999
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003000 // Check whether we have a default argument.
Douglas Gregorf35f8282009-11-11 21:54:23 +00003001 TemplateArgumentLoc Arg;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003002
Douglas Gregorf35f8282009-11-11 21:54:23 +00003003 // Retrieve the default template argument from the template
3004 // parameter. For each kind of template parameter, we substitute the
3005 // template arguments provided thus far and any "outer" template arguments
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003006 // (when the template parameter was part of a nested template) into
Douglas Gregorf35f8282009-11-11 21:54:23 +00003007 // the default argument.
3008 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) {
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003009 if (!TTP->hasDefaultArgument())
3010 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3011 TemplateArgs);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003012
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003013 TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003014 Template,
3015 TemplateLoc,
3016 RAngleLoc,
3017 TTP,
3018 Converted);
3019 if (!ArgType)
3020 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003021
Douglas Gregorf35f8282009-11-11 21:54:23 +00003022 Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()),
3023 ArgType);
3024 } else if (NonTypeTemplateParmDecl *NTTP
3025 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003026 if (!NTTP->hasDefaultArgument())
3027 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3028 TemplateArgs);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003029
John McCall60d7b3a2010-08-24 06:29:42 +00003030 ExprResult E = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003031 TemplateLoc,
3032 RAngleLoc,
3033 NTTP,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003034 Converted);
3035 if (E.isInvalid())
3036 return true;
3037
3038 Expr *Ex = E.takeAs<Expr>();
3039 Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex);
3040 } else {
3041 TemplateTemplateParmDecl *TempParm
3042 = cast<TemplateTemplateParmDecl>(*Param);
3043
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003044 if (!TempParm->hasDefaultArgument())
3045 return diagnoseArityMismatch(*this, Template, TemplateLoc,
3046 TemplateArgs);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003047
Douglas Gregor1d752d72011-03-02 18:46:51 +00003048 NestedNameSpecifierLoc QualifierLoc;
Douglas Gregorf35f8282009-11-11 21:54:23 +00003049 TemplateName Name = SubstDefaultTemplateArgument(*this, Template,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003050 TemplateLoc,
3051 RAngleLoc,
Douglas Gregorf35f8282009-11-11 21:54:23 +00003052 TempParm,
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003053 Converted,
3054 QualifierLoc);
Douglas Gregorf35f8282009-11-11 21:54:23 +00003055 if (Name.isNull())
3056 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003057
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003058 Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc,
3059 TempParm->getDefaultArgument().getTemplateNameLoc());
Douglas Gregorf35f8282009-11-11 21:54:23 +00003060 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003061
Douglas Gregorf35f8282009-11-11 21:54:23 +00003062 // Introduce an instantiation record that describes where we are using
3063 // the default template argument.
3064 InstantiatingTemplate Instantiating(*this, RAngleLoc, Template, *Param,
Douglas Gregor910f8002010-11-07 23:05:16 +00003065 Converted.data(), Converted.size(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003066 SourceRange(TemplateLoc, RAngleLoc));
3067
Douglas Gregorf35f8282009-11-11 21:54:23 +00003068 // Check the default template argument.
Douglas Gregord9e15302009-11-11 19:41:09 +00003069 if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc,
Douglas Gregor6952f1e2011-01-19 20:10:05 +00003070 RAngleLoc, 0, Converted))
Douglas Gregore7526412009-11-11 19:31:23 +00003071 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003072
Douglas Gregor67714232011-03-03 02:41:12 +00003073 // Core issue 150 (assumed resolution): if this is a template template
3074 // parameter, keep track of the default template arguments from the
3075 // template definition.
3076 if (isTemplateTemplateParameter)
3077 TemplateArgs.addArgument(Arg);
3078
Douglas Gregor14be16b2010-12-20 16:57:52 +00003079 // Move to the next template parameter and argument.
3080 ++Param;
3081 ++ArgIdx;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003082 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003083
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003084 // If we saw a pack expansion, then directly convert the remaining arguments,
3085 // because we don't know what parameters they'll match up with.
3086 if (SawPackExpansion) {
3087 bool AddToArgumentPack
3088 = Param != ParamEnd && (*Param)->isTemplateParameterPack();
3089 while (ArgIdx < NumArgs) {
3090 if (AddToArgumentPack)
3091 ArgumentPack.push_back(TemplateArgs[ArgIdx].getArgument());
3092 else
3093 Converted.push_back(TemplateArgs[ArgIdx].getArgument());
3094 ++ArgIdx;
3095 }
3096
3097 // Push the argument pack onto the list of converted arguments.
3098 if (AddToArgumentPack) {
3099 if (ArgumentPack.empty())
3100 Converted.push_back(TemplateArgument(0, 0));
3101 else {
3102 Converted.push_back(
3103 TemplateArgument::CreatePackCopy(Context,
3104 ArgumentPack.data(),
3105 ArgumentPack.size()));
3106 ArgumentPack.clear();
3107 }
Douglas Gregorb70126a2012-02-03 17:16:23 +00003108 } else if (ExpansionIntoFixedList) {
3109 // We have expanded a pack into a fixed list.
3110 *ExpansionIntoFixedList = true;
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003111 }
3112
3113 return Invalid;
3114 }
3115
3116 // If we have any leftover arguments, then there were too many arguments.
3117 // Complain and fail.
3118 if (ArgIdx < NumArgs)
3119 return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs);
3120
3121 // If we have an expanded parameter pack, make sure we don't have too
3122 // many arguments.
3123 // FIXME: This really should fall out from the normal arity checking.
3124 if (Param != ParamEnd) {
3125 if (NonTypeTemplateParmDecl *NTTP
3126 = dyn_cast<NonTypeTemplateParmDecl>(*Param)) {
3127 if (NTTP->isExpandedParameterPack() &&
3128 ArgumentPack.size() < NTTP->getNumExpansionTypes()) {
3129 Diag(TemplateLoc, diag::err_template_arg_list_different_arity)
3130 << false
3131 << (isa<ClassTemplateDecl>(Template)? 0 :
3132 isa<FunctionTemplateDecl>(Template)? 1 :
3133 isa<TemplateTemplateParmDecl>(Template)? 2 : 3)
3134 << Template;
3135 Diag(Template->getLocation(), diag::note_template_decl_here)
3136 << Params->getSourceRange();
3137 return true;
3138 }
3139 }
3140 }
3141
Douglas Gregor14be16b2010-12-20 16:57:52 +00003142 // Form argument packs for each of the parameter packs remaining.
3143 while (Param != ParamEnd) {
Douglas Gregord3731192011-01-10 07:32:04 +00003144 // If we're checking a partial list of template arguments, don't fill
3145 // in arguments for non-template parameter packs.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003146 if ((*Param)->isTemplateParameterPack()) {
David Blaikie1368e582011-10-19 05:19:50 +00003147 if (!HasParameterPack)
3148 return true;
Douglas Gregor8735b292011-06-03 02:59:40 +00003149 if (ArgumentPack.empty())
Douglas Gregor14be16b2010-12-20 16:57:52 +00003150 Converted.push_back(TemplateArgument(0, 0));
Douglas Gregor203e6a32011-01-11 23:09:57 +00003151 else {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003152 Converted.push_back(TemplateArgument::CreatePackCopy(Context,
3153 ArgumentPack.data(),
Douglas Gregor203e6a32011-01-11 23:09:57 +00003154 ArgumentPack.size()));
Douglas Gregor14be16b2010-12-20 16:57:52 +00003155 ArgumentPack.clear();
3156 }
Douglas Gregor8fbbae52012-02-03 07:34:46 +00003157 } else if (!PartialTemplateArgs)
3158 return diagnoseArityMismatch(*this, Template, TemplateLoc, TemplateArgs);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003159
Douglas Gregor14be16b2010-12-20 16:57:52 +00003160 ++Param;
3161 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003162
Douglas Gregorc15cb382009-02-09 23:23:08 +00003163 return Invalid;
3164}
3165
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003166namespace {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003167 class UnnamedLocalNoLinkageFinder
3168 : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool>
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003169 {
3170 Sema &S;
3171 SourceRange SR;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003172
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003173 typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003174
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003175 public:
3176 UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { }
3177
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003178 bool Visit(QualType T) {
3179 return inherited::Visit(T.getTypePtr());
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003180 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003181
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003182#define TYPE(Class, Parent) \
3183 bool Visit##Class##Type(const Class##Type *);
3184#define ABSTRACT_TYPE(Class, Parent) \
3185 bool Visit##Class##Type(const Class##Type *) { return false; }
3186#define NON_CANONICAL_TYPE(Class, Parent) \
3187 bool Visit##Class##Type(const Class##Type *) { return false; }
3188#include "clang/AST/TypeNodes.def"
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003189
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003190 bool VisitTagDecl(const TagDecl *Tag);
3191 bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS);
3192 };
3193}
3194
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003195bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003196 return false;
3197}
3198
3199bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) {
3200 return Visit(T->getElementType());
3201}
3202
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003203bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003204 return Visit(T->getPointeeType());
3205}
3206
3207bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003208 const BlockPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003209 return Visit(T->getPointeeType());
3210}
3211
3212bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003213 const LValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003214 return Visit(T->getPointeeType());
3215}
3216
3217bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003218 const RValueReferenceType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003219 return Visit(T->getPointeeType());
3220}
3221
3222bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003223 const MemberPointerType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003224 return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0));
3225}
3226
3227bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003228 const ConstantArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003229 return Visit(T->getElementType());
3230}
3231
3232bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003233 const IncompleteArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003234 return Visit(T->getElementType());
3235}
3236
3237bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003238 const VariableArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003239 return Visit(T->getElementType());
3240}
3241
3242bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003243 const DependentSizedArrayType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003244 return Visit(T->getElementType());
3245}
3246
3247bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003248 const DependentSizedExtVectorType* T) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003249 return Visit(T->getElementType());
3250}
3251
3252bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) {
3253 return Visit(T->getElementType());
3254}
3255
3256bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) {
3257 return Visit(T->getElementType());
3258}
3259
3260bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType(
3261 const FunctionProtoType* T) {
3262 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003263 AEnd = T->arg_type_end();
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003264 A != AEnd; ++A) {
3265 if (Visit(*A))
3266 return true;
3267 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003268
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003269 return Visit(T->getResultType());
3270}
3271
3272bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType(
3273 const FunctionNoProtoType* T) {
3274 return Visit(T->getResultType());
3275}
3276
3277bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType(
3278 const UnresolvedUsingType*) {
3279 return false;
3280}
3281
3282bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) {
3283 return false;
3284}
3285
3286bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) {
3287 return Visit(T->getUnderlyingType());
3288}
3289
3290bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) {
3291 return false;
3292}
3293
Sean Huntca63c202011-05-24 22:41:36 +00003294bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType(
3295 const UnaryTransformType*) {
3296 return false;
3297}
3298
Richard Smith34b41d92011-02-20 03:19:35 +00003299bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) {
3300 return Visit(T->getDeducedType());
3301}
3302
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003303bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) {
3304 return VisitTagDecl(T->getDecl());
3305}
3306
3307bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) {
3308 return VisitTagDecl(T->getDecl());
3309}
3310
3311bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType(
3312 const TemplateTypeParmType*) {
3313 return false;
3314}
3315
Douglas Gregorc3069d62011-01-14 02:55:32 +00003316bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType(
3317 const SubstTemplateTypeParmPackType *) {
3318 return false;
3319}
3320
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003321bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType(
3322 const TemplateSpecializationType*) {
3323 return false;
3324}
3325
3326bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType(
3327 const InjectedClassNameType* T) {
3328 return VisitTagDecl(T->getDecl());
3329}
3330
3331bool UnnamedLocalNoLinkageFinder::VisitDependentNameType(
3332 const DependentNameType* T) {
3333 return VisitNestedNameSpecifier(T->getQualifier());
3334}
3335
3336bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType(
3337 const DependentTemplateSpecializationType* T) {
3338 return VisitNestedNameSpecifier(T->getQualifier());
3339}
3340
Douglas Gregor7536dd52010-12-20 02:24:11 +00003341bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType(
3342 const PackExpansionType* T) {
3343 return Visit(T->getPattern());
3344}
3345
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003346bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) {
3347 return false;
3348}
3349
3350bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType(
3351 const ObjCInterfaceType *) {
3352 return false;
3353}
3354
3355bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType(
3356 const ObjCObjectPointerType *) {
3357 return false;
3358}
3359
Eli Friedmanb001de72011-10-06 23:00:33 +00003360bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) {
3361 return Visit(T->getValueType());
3362}
3363
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003364bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) {
3365 if (Tag->getDeclContext()->isFunctionOrMethod()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003366 S.Diag(SR.getBegin(),
3367 S.getLangOptions().CPlusPlus0x ?
3368 diag::warn_cxx98_compat_template_arg_local_type :
3369 diag::ext_template_arg_local_type)
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003370 << S.Context.getTypeDeclType(Tag) << SR;
3371 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003372 }
3373
Richard Smith162e1c12011-04-15 14:24:37 +00003374 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003375 S.Diag(SR.getBegin(),
3376 S.getLangOptions().CPlusPlus0x ?
3377 diag::warn_cxx98_compat_template_arg_unnamed_type :
3378 diag::ext_template_arg_unnamed_type) << SR;
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003379 S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here);
3380 return true;
3381 }
3382
3383 return false;
3384}
3385
3386bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier(
3387 NestedNameSpecifier *NNS) {
3388 if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix()))
3389 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003390
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003391 switch (NNS->getKind()) {
3392 case NestedNameSpecifier::Identifier:
3393 case NestedNameSpecifier::Namespace:
Douglas Gregor14aba762011-02-24 02:36:08 +00003394 case NestedNameSpecifier::NamespaceAlias:
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003395 case NestedNameSpecifier::Global:
3396 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003397
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003398 case NestedNameSpecifier::TypeSpec:
3399 case NestedNameSpecifier::TypeSpecWithTemplate:
3400 return Visit(QualType(NNS->getAsType(), 0));
3401 }
David Blaikie7530c032012-01-17 06:56:22 +00003402 llvm_unreachable("Invalid NestedNameSpecifier::Kind!");
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003403}
3404
3405
Douglas Gregorc15cb382009-02-09 23:23:08 +00003406/// \brief Check a template argument against its corresponding
3407/// template type parameter.
3408///
3409/// This routine implements the semantics of C++ [temp.arg.type]. It
3410/// returns true if an error occurred, and false otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00003411bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param,
John McCalla93c9342009-12-07 02:54:59 +00003412 TypeSourceInfo *ArgInfo) {
3413 assert(ArgInfo && "invalid TypeSourceInfo");
John McCall833ca992009-10-29 08:12:44 +00003414 QualType Arg = ArgInfo->getType();
Douglas Gregor0fddb972010-05-22 16:17:30 +00003415 SourceRange SR = ArgInfo->getTypeLoc().getSourceRange();
Chandler Carruth17fb8552010-09-03 21:12:34 +00003416
3417 if (Arg->isVariablyModifiedType()) {
3418 return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg;
Douglas Gregor4b52e252009-12-21 23:17:24 +00003419 } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) {
Douglas Gregor4b52e252009-12-21 23:17:24 +00003420 return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR;
Douglas Gregorc15cb382009-02-09 23:23:08 +00003421 }
3422
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003423 // C++03 [temp.arg.type]p2:
3424 // A local type, a type with no linkage, an unnamed type or a type
3425 // compounded from any of these types shall not be used as a
3426 // template-argument for a template type-parameter.
3427 //
Richard Smithebaf0e62011-10-18 20:49:44 +00003428 // C++11 allows these, and even in C++03 we allow them as an extension with
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003429 // a warning.
Richard Smithebaf0e62011-10-18 20:49:44 +00003430 if (LangOpts.CPlusPlus0x ?
3431 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_unnamed_type,
3432 SR.getBegin()) != DiagnosticsEngine::Ignored ||
3433 Diags.getDiagnosticLevel(diag::warn_cxx98_compat_template_arg_local_type,
3434 SR.getBegin()) != DiagnosticsEngine::Ignored :
3435 Arg->hasUnnamedOrLocalType()) {
Douglas Gregor5f3aeb62010-10-13 00:27:52 +00003436 UnnamedLocalNoLinkageFinder Finder(*this, SR);
3437 (void)Finder.Visit(Context.getCanonicalType(Arg));
3438 }
3439
Douglas Gregorc15cb382009-02-09 23:23:08 +00003440 return false;
3441}
3442
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003443/// \brief Checks whether the given template argument is the address
3444/// of an object or function according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003445static bool
Douglas Gregorb7a09262010-04-01 18:32:35 +00003446CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S,
3447 NonTypeTemplateParmDecl *Param,
3448 QualType ParamType,
3449 Expr *ArgIn,
3450 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003451 bool Invalid = false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003452 Expr *Arg = ArgIn;
3453 QualType ArgType = Arg->getType();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003454
3455 // See through any implicit casts we added to fix the type.
John McCall91a57552011-07-15 05:09:51 +00003456 Arg = Arg->IgnoreImpCasts();
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003457
3458 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003459 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003460 // A template-argument for a non-type, non-template
3461 // template-parameter shall be one of: [...]
3462 //
3463 // -- the address of an object or function with external
3464 // linkage, including function templates and function
3465 // template-ids but excluding non-static class members,
3466 // expressed as & id-expression where the & is optional if
3467 // the name refers to a function or array, or if the
3468 // corresponding template-parameter is a reference; or
Mike Stump1eb44332009-09-09 15:08:12 +00003469
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003470 // In C++98/03 mode, give an extension warning on any extra parentheses.
3471 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3472 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003473 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003474 if (!Invalid && !ExtraParens) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003475 S.Diag(Arg->getSourceRange().getBegin(),
Richard Smithebaf0e62011-10-18 20:49:44 +00003476 S.getLangOptions().CPlusPlus0x ?
3477 diag::warn_cxx98_compat_template_arg_extra_parens :
3478 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003479 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003480 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003481 }
3482
3483 Arg = Parens->getSubExpr();
3484 }
3485
John McCall91a57552011-07-15 05:09:51 +00003486 while (SubstNonTypeTemplateParmExpr *subst =
3487 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3488 Arg = subst->getReplacement()->IgnoreImpCasts();
3489
Douglas Gregorb7a09262010-04-01 18:32:35 +00003490 bool AddressTaken = false;
3491 SourceLocation AddrOpLoc;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003492 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003493 if (UnOp->getOpcode() == UO_AddrOf) {
John McCall91a57552011-07-15 05:09:51 +00003494 Arg = UnOp->getSubExpr();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003495 AddressTaken = true;
3496 AddrOpLoc = UnOp->getOperatorLoc();
3497 }
Francois Picheta343a412011-04-29 09:08:14 +00003498 }
John McCall91a57552011-07-15 05:09:51 +00003499
Francois Pichet62ec1f22011-09-17 17:15:52 +00003500 if (S.getLangOptions().MicrosoftExt && isa<CXXUuidofExpr>(Arg)) {
John McCall91a57552011-07-15 05:09:51 +00003501 Converted = TemplateArgument(ArgIn);
3502 return false;
3503 }
3504
3505 while (SubstNonTypeTemplateParmExpr *subst =
3506 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3507 Arg = subst->getReplacement()->IgnoreImpCasts();
3508
3509 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003510 if (!DRE) {
Douglas Gregor1a8cf732010-04-14 23:11:21 +00003511 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_decl_ref)
3512 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003513 S.Diag(Param->getLocation(), diag::note_template_param_here);
3514 return true;
3515 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003516
3517 // Stop checking the precise nature of the argument if it is value dependent,
3518 // it should be checked when instantiated.
Douglas Gregorb7a09262010-04-01 18:32:35 +00003519 if (Arg->isValueDependent()) {
John McCall3fa5cae2010-10-26 07:05:15 +00003520 Converted = TemplateArgument(ArgIn);
Chandler Carruth038cc392010-01-31 10:01:20 +00003521 return false;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003522 }
Chandler Carruth038cc392010-01-31 10:01:20 +00003523
Douglas Gregorb7a09262010-04-01 18:32:35 +00003524 if (!isa<ValueDecl>(DRE->getDecl())) {
3525 S.Diag(Arg->getSourceRange().getBegin(),
3526 diag::err_template_arg_not_object_or_func_form)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003527 << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003528 S.Diag(Param->getLocation(), diag::note_template_param_here);
3529 return true;
3530 }
3531
3532 NamedDecl *Entity = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003533
3534 // Cannot refer to non-static data members
Douglas Gregorb7a09262010-04-01 18:32:35 +00003535 if (FieldDecl *Field = dyn_cast<FieldDecl>(DRE->getDecl())) {
3536 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_field)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003537 << Field << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003538 S.Diag(Param->getLocation(), diag::note_template_param_here);
3539 return true;
3540 }
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003541
3542 // Cannot refer to non-static member functions
3543 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(DRE->getDecl()))
Douglas Gregorb7a09262010-04-01 18:32:35 +00003544 if (!Method->isStatic()) {
3545 S.Diag(Arg->getSourceRange().getBegin(), diag::err_template_arg_method)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003546 << Method << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003547 S.Diag(Param->getLocation(), diag::note_template_param_here);
3548 return true;
3549 }
Mike Stump1eb44332009-09-09 15:08:12 +00003550
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003551 // Functions must have external linkage.
3552 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003553 if (!isExternalLinkage(Func->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003554 S.Diag(Arg->getSourceRange().getBegin(),
3555 diag::err_template_arg_function_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003556 << Func << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003557 S.Diag(Func->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003558 << true;
3559 return true;
3560 }
3561
3562 // Okay: we've named a function with external linkage.
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003563 Entity = Func;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003564
Douglas Gregorb7a09262010-04-01 18:32:35 +00003565 // If the template parameter has pointer type, the function decays.
3566 if (ParamType->isPointerType() && !AddressTaken)
3567 ArgType = S.Context.getPointerType(Func->getType());
3568 else if (AddressTaken && ParamType->isReferenceType()) {
3569 // If we originally had an address-of operator, but the
3570 // parameter has reference type, complain and (if things look
3571 // like they will work) drop the address-of operator.
3572 if (!S.Context.hasSameUnqualifiedType(Func->getType(),
3573 ParamType.getNonReferenceType())) {
3574 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3575 << ParamType;
3576 S.Diag(Param->getLocation(), diag::note_template_param_here);
3577 return true;
3578 }
3579
3580 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3581 << ParamType
3582 << FixItHint::CreateRemoval(AddrOpLoc);
3583 S.Diag(Param->getLocation(), diag::note_template_param_here);
3584
3585 ArgType = Func->getType();
3586 }
3587 } else if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +00003588 if (!isExternalLinkage(Var->getLinkage())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003589 S.Diag(Arg->getSourceRange().getBegin(),
3590 diag::err_template_arg_object_not_extern)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003591 << Var << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003592 S.Diag(Var->getLocation(), diag::note_template_arg_internal_object)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003593 << true;
3594 return true;
3595 }
3596
Douglas Gregorb7a09262010-04-01 18:32:35 +00003597 // A value of reference type is not an object.
3598 if (Var->getType()->isReferenceType()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003599 S.Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorb7a09262010-04-01 18:32:35 +00003600 diag::err_template_arg_reference_var)
3601 << Var->getType() << Arg->getSourceRange();
3602 S.Diag(Param->getLocation(), diag::note_template_param_here);
3603 return true;
3604 }
3605
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003606 // Okay: we've named an object with external linkage
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003607 Entity = Var;
Douglas Gregorb7a09262010-04-01 18:32:35 +00003608
3609 // If the template parameter has pointer type, we must have taken
3610 // the address of this object.
3611 if (ParamType->isReferenceType()) {
3612 if (AddressTaken) {
3613 // If we originally had an address-of operator, but the
3614 // parameter has reference type, complain and (if things look
3615 // like they will work) drop the address-of operator.
3616 if (!S.Context.hasSameUnqualifiedType(Var->getType(),
3617 ParamType.getNonReferenceType())) {
3618 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3619 << ParamType;
3620 S.Diag(Param->getLocation(), diag::note_template_param_here);
3621 return true;
3622 }
3623
3624 S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer)
3625 << ParamType
3626 << FixItHint::CreateRemoval(AddrOpLoc);
3627 S.Diag(Param->getLocation(), diag::note_template_param_here);
3628
3629 ArgType = Var->getType();
3630 }
3631 } else if (!AddressTaken && ParamType->isPointerType()) {
3632 if (Var->getType()->isArrayType()) {
3633 // Array-to-pointer decay.
3634 ArgType = S.Context.getArrayDecayedType(Var->getType());
3635 } else {
3636 // If the template parameter has pointer type but the address of
3637 // this object was not taken, complain and (possibly) recover by
3638 // taking the address of the entity.
3639 ArgType = S.Context.getPointerType(Var->getType());
3640 if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) {
3641 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3642 << ParamType;
3643 S.Diag(Param->getLocation(), diag::note_template_param_here);
3644 return true;
3645 }
3646
3647 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_address_of)
3648 << ParamType
3649 << FixItHint::CreateInsertion(Arg->getLocStart(), "&");
3650
3651 S.Diag(Param->getLocation(), diag::note_template_param_here);
3652 }
3653 }
3654 } else {
3655 // We found something else, but we don't know specifically what it is.
3656 S.Diag(Arg->getSourceRange().getBegin(),
3657 diag::err_template_arg_not_object_or_func)
3658 << Arg->getSourceRange();
3659 S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here);
3660 return true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003661 }
Mike Stump1eb44332009-09-09 15:08:12 +00003662
John McCallf85e1932011-06-15 23:02:42 +00003663 bool ObjCLifetimeConversion;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003664 if (ParamType->isPointerType() &&
Douglas Gregorb7a09262010-04-01 18:32:35 +00003665 !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() &&
John McCallf85e1932011-06-15 23:02:42 +00003666 S.IsQualificationConversion(ArgType, ParamType, false,
3667 ObjCLifetimeConversion)) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00003668 // For pointer-to-object types, qualification conversions are
3669 // permitted.
3670 } else {
3671 if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) {
3672 if (!ParamRef->getPointeeType()->isFunctionType()) {
3673 // C++ [temp.arg.nontype]p5b3:
3674 // For a non-type template-parameter of type reference to
3675 // object, no conversions apply. The type referred to by the
3676 // reference may be more cv-qualified than the (otherwise
3677 // identical) type of the template- argument. The
3678 // template-parameter is bound directly to the
3679 // template-argument, which shall be an lvalue.
3680
3681 // FIXME: Other qualifiers?
3682 unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers();
3683 unsigned ArgQuals = ArgType.getCVRQualifiers();
3684
3685 if ((ParamQuals | ArgQuals) != ParamQuals) {
3686 S.Diag(Arg->getSourceRange().getBegin(),
3687 diag::err_template_arg_ref_bind_ignores_quals)
3688 << ParamType << Arg->getType()
3689 << Arg->getSourceRange();
3690 S.Diag(Param->getLocation(), diag::note_template_param_here);
3691 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003692 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00003693 }
3694 }
3695
3696 // At this point, the template argument refers to an object or
3697 // function with external linkage. We now need to check whether the
3698 // argument and parameter types are compatible.
3699 if (!S.Context.hasSameUnqualifiedType(ArgType,
3700 ParamType.getNonReferenceType())) {
3701 // We can't perform this conversion or binding.
3702 if (ParamType->isReferenceType())
3703 S.Diag(Arg->getLocStart(), diag::err_template_arg_no_ref_bind)
John McCall91a57552011-07-15 05:09:51 +00003704 << ParamType << ArgIn->getType() << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003705 else
3706 S.Diag(Arg->getLocStart(), diag::err_template_arg_not_convertible)
John McCall91a57552011-07-15 05:09:51 +00003707 << ArgIn->getType() << ParamType << Arg->getSourceRange();
Douglas Gregorb7a09262010-04-01 18:32:35 +00003708 S.Diag(Param->getLocation(), diag::note_template_param_here);
3709 return true;
3710 }
3711 }
3712
3713 // Create the template argument.
3714 Converted = TemplateArgument(Entity->getCanonicalDecl());
Eli Friedman5f2987c2012-02-02 03:46:19 +00003715 S.MarkAnyDeclReferenced(Arg->getLocStart(), Entity);
Douglas Gregorb7a09262010-04-01 18:32:35 +00003716 return false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003717}
3718
3719/// \brief Checks whether the given template argument is a pointer to
3720/// member constant according to C++ [temp.arg.nontype]p1.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003721bool Sema::CheckTemplateArgumentPointerToMember(Expr *Arg,
Douglas Gregorcaddba02009-11-12 18:38:13 +00003722 TemplateArgument &Converted) {
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003723 bool Invalid = false;
3724
3725 // See through any implicit casts we added to fix the type.
Eli Friedman73c39ab2009-10-20 08:27:19 +00003726 while (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003727 Arg = Cast->getSubExpr();
3728
3729 // C++ [temp.arg.nontype]p1:
Mike Stump1eb44332009-09-09 15:08:12 +00003730 //
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003731 // A template-argument for a non-type, non-template
3732 // template-parameter shall be one of: [...]
3733 //
3734 // -- a pointer to member expressed as described in 5.3.1.
Douglas Gregora2813ce2009-10-23 18:54:35 +00003735 DeclRefExpr *DRE = 0;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003736
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003737 // In C++98/03 mode, give an extension warning on any extra parentheses.
3738 // See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#773
3739 bool ExtraParens = false;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003740 while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) {
Richard Smithebaf0e62011-10-18 20:49:44 +00003741 if (!Invalid && !ExtraParens) {
Mike Stump1eb44332009-09-09 15:08:12 +00003742 Diag(Arg->getSourceRange().getBegin(),
Richard Smithebaf0e62011-10-18 20:49:44 +00003743 getLangOptions().CPlusPlus0x ?
3744 diag::warn_cxx98_compat_template_arg_extra_parens :
3745 diag::ext_template_arg_extra_parens)
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003746 << Arg->getSourceRange();
Abramo Bagnara2c5399f2010-09-13 06:06:58 +00003747 ExtraParens = true;
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003748 }
3749
3750 Arg = Parens->getSubExpr();
3751 }
3752
John McCall91a57552011-07-15 05:09:51 +00003753 while (SubstNonTypeTemplateParmExpr *subst =
3754 dyn_cast<SubstNonTypeTemplateParmExpr>(Arg))
3755 Arg = subst->getReplacement()->IgnoreImpCasts();
3756
Douglas Gregorcaddba02009-11-12 18:38:13 +00003757 // A pointer-to-member constant written &Class::member.
3758 if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) {
John McCall2de56d12010-08-25 11:45:40 +00003759 if (UnOp->getOpcode() == UO_AddrOf) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00003760 DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr());
3761 if (DRE && !DRE->getQualifier())
3762 DRE = 0;
3763 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003764 }
Douglas Gregorcaddba02009-11-12 18:38:13 +00003765 // A constant of pointer-to-member type.
3766 else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) {
3767 if (ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl())) {
3768 if (VD->getType()->isMemberPointerType()) {
3769 if (isa<NonTypeTemplateParmDecl>(VD) ||
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003770 (isa<VarDecl>(VD) &&
Douglas Gregorcaddba02009-11-12 18:38:13 +00003771 Context.getCanonicalType(VD->getType()).isConstQualified())) {
3772 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003773 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003774 else
3775 Converted = TemplateArgument(VD->getCanonicalDecl());
3776 return Invalid;
3777 }
3778 }
3779 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003780
Douglas Gregorcaddba02009-11-12 18:38:13 +00003781 DRE = 0;
3782 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003783
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003784 if (!DRE)
3785 return Diag(Arg->getSourceRange().getBegin(),
3786 diag::err_template_arg_not_pointer_to_member_form)
3787 << Arg->getSourceRange();
3788
3789 if (isa<FieldDecl>(DRE->getDecl()) || isa<CXXMethodDecl>(DRE->getDecl())) {
3790 assert((isa<FieldDecl>(DRE->getDecl()) ||
3791 !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) &&
3792 "Only non-static member pointers can make it here");
3793
3794 // Okay: this is the address of a non-static member, and therefore
3795 // a member pointer constant.
Douglas Gregorcaddba02009-11-12 18:38:13 +00003796 if (Arg->isTypeDependent() || Arg->isValueDependent())
John McCall3fa5cae2010-10-26 07:05:15 +00003797 Converted = TemplateArgument(Arg);
Douglas Gregorcaddba02009-11-12 18:38:13 +00003798 else
3799 Converted = TemplateArgument(DRE->getDecl()->getCanonicalDecl());
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003800 return Invalid;
3801 }
3802
3803 // We found something else, but we don't know specifically what it is.
Mike Stump1eb44332009-09-09 15:08:12 +00003804 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003805 diag::err_template_arg_not_pointer_to_member_form)
3806 << Arg->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00003807 Diag(DRE->getDecl()->getLocation(),
Douglas Gregorcc45cb32009-02-11 19:52:55 +00003808 diag::note_template_arg_refers_here);
3809 return true;
3810}
3811
Douglas Gregorc15cb382009-02-09 23:23:08 +00003812/// \brief Check a template argument against its corresponding
3813/// non-type template parameter.
3814///
Douglas Gregor2943aed2009-03-03 04:44:36 +00003815/// This routine implements the semantics of C++ [temp.arg.nontype].
John Wiegley429bb272011-04-08 18:41:53 +00003816/// If an error occurred, it returns ExprError(); otherwise, it
3817/// returns the converted template argument. \p
Douglas Gregor2943aed2009-03-03 04:44:36 +00003818/// InstantiatedParamType is the type of the non-type template
3819/// parameter after it has been instantiated.
John Wiegley429bb272011-04-08 18:41:53 +00003820ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
3821 QualType InstantiatedParamType, Expr *Arg,
3822 TemplateArgument &Converted,
3823 CheckTemplateArgumentKind CTAK) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00003824 SourceLocation StartLoc = Arg->getSourceRange().getBegin();
3825
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003826 // If either the parameter has a dependent type or the argument is
3827 // type-dependent, there's nothing we can check now.
Douglas Gregor40808ce2009-03-09 23:48:35 +00003828 if (InstantiatedParamType->isDependentType() || Arg->isTypeDependent()) {
3829 // FIXME: Produce a cloned, canonical expression?
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003830 Converted = TemplateArgument(Arg);
John Wiegley429bb272011-04-08 18:41:53 +00003831 return Owned(Arg);
Douglas Gregor40808ce2009-03-09 23:48:35 +00003832 }
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003833
3834 // C++ [temp.arg.nontype]p5:
3835 // The following conversions are performed on each expression used
3836 // as a non-type template-argument. If a non-type
3837 // template-argument cannot be converted to the type of the
3838 // corresponding template-parameter then the program is
3839 // ill-formed.
Douglas Gregor2943aed2009-03-03 04:44:36 +00003840 QualType ParamType = InstantiatedParamType;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003841 if (ParamType->isIntegralOrEnumerationType()) {
Richard Smith8ef7b202012-01-18 23:55:52 +00003842 // C++11:
3843 // -- for a non-type template-parameter of integral or
3844 // enumeration type, conversions permitted in a converted
3845 // constant expression are applied.
3846 //
3847 // C++98:
3848 // -- for a non-type template-parameter of integral or
3849 // enumeration type, integral promotions (4.5) and integral
3850 // conversions (4.7) are applied.
3851
3852 if (CTAK == CTAK_Deduced &&
3853 !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) {
3854 // C++ [temp.deduct.type]p17:
3855 // If, in the declaration of a function template with a non-type
3856 // template-parameter, the non-type template-parameter is used
3857 // in an expression in the function parameter-list and, if the
3858 // corresponding template-argument is deduced, the
3859 // template-argument type shall match the type of the
3860 // template-parameter exactly, except that a template-argument
3861 // deduced from an array bound may be of any integral type.
3862 Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch)
3863 << Arg->getType().getUnqualifiedType()
3864 << ParamType.getUnqualifiedType();
3865 Diag(Param->getLocation(), diag::note_template_param_here);
3866 return ExprError();
3867 }
3868
3869 if (getLangOptions().CPlusPlus0x) {
3870 // We can't check arbitrary value-dependent arguments.
3871 // FIXME: If there's no viable conversion to the template parameter type,
3872 // we should be able to diagnose that prior to instantiation.
3873 if (Arg->isValueDependent()) {
3874 Converted = TemplateArgument(Arg);
3875 return Owned(Arg);
3876 }
3877
3878 // C++ [temp.arg.nontype]p1:
3879 // A template-argument for a non-type, non-template template-parameter
3880 // shall be one of:
3881 //
3882 // -- for a non-type template-parameter of integral or enumeration
3883 // type, a converted constant expression of the type of the
3884 // template-parameter; or
3885 llvm::APSInt Value;
3886 ExprResult ArgResult =
3887 CheckConvertedConstantExpression(Arg, ParamType, Value,
3888 CCEK_TemplateArg);
3889 if (ArgResult.isInvalid())
3890 return ExprError();
3891
3892 // Widen the argument value to sizeof(parameter type). This is almost
3893 // always a no-op, except when the parameter type is bool. In
3894 // that case, this may extend the argument from 1 bit to 8 bits.
3895 QualType IntegerType = ParamType;
3896 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
3897 IntegerType = Enum->getDecl()->getIntegerType();
3898 Value = Value.extOrTrunc(Context.getTypeSize(IntegerType));
3899
3900 Converted = TemplateArgument(Value, Context.getCanonicalType(ParamType));
3901 return ArgResult;
3902 }
3903
Richard Smith4f870622011-10-27 22:11:44 +00003904 ExprResult ArgResult = DefaultLvalueConversion(Arg);
3905 if (ArgResult.isInvalid())
3906 return ExprError();
3907 Arg = ArgResult.take();
3908
3909 QualType ArgType = Arg->getType();
3910
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003911 // C++ [temp.arg.nontype]p1:
3912 // A template-argument for a non-type, non-template
3913 // template-parameter shall be one of:
3914 //
3915 // -- an integral constant-expression of integral or enumeration
3916 // type; or
3917 // -- the name of a non-type template-parameter; or
3918 SourceLocation NonConstantLoc;
Douglas Gregor3e00bad2009-02-17 01:05:43 +00003919 llvm::APSInt Value;
Douglas Gregor2ade35e2010-06-16 00:17:44 +00003920 if (!ArgType->isIntegralOrEnumerationType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003921 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003922 diag::err_template_arg_not_integral_or_enumeral)
3923 << ArgType << Arg->getSourceRange();
3924 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003925 return ExprError();
Richard Smith282e7e62012-02-04 09:53:13 +00003926 } else if (!Arg->isValueDependent()) {
3927 Arg = VerifyIntegerConstantExpression(Arg, &Value,
3928 PDiag(diag::err_template_arg_not_ice) << ArgType, false).take();
3929 if (!Arg)
3930 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003931 }
3932
Douglas Gregor02024a92010-03-28 02:42:43 +00003933 // From here on out, all we care about are the unqualified forms
3934 // of the parameter and argument types.
3935 ParamType = ParamType.getUnqualifiedType();
3936 ArgType = ArgType.getUnqualifiedType();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003937
3938 // Try to convert the argument to the parameter's type.
Douglas Gregorff524392009-11-04 21:50:46 +00003939 if (Context.hasSameType(ParamType, ArgType)) {
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003940 // Okay: no conversion necessary
John McCalldaa8e4e2010-11-15 09:13:47 +00003941 } else if (ParamType->isBooleanType()) {
3942 // This is an integral-to-boolean conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003943 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003944 } else if (IsIntegralPromotion(Arg, ArgType, ParamType) ||
3945 !ParamType->isEnumeralType()) {
3946 // This is an integral promotion or conversion.
John Wiegley429bb272011-04-08 18:41:53 +00003947 Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).take();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003948 } else {
3949 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00003950 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003951 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00003952 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003953 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00003954 return ExprError();
Douglas Gregor6ae5e662009-02-10 23:36:10 +00003955 }
3956
Douglas Gregorc7469372011-05-04 21:55:00 +00003957 // Add the value of this argument to the list of converted
3958 // arguments. We use the bitwidth and signedness of the template
3959 // parameter.
3960 if (Arg->isValueDependent()) {
3961 // The argument is value-dependent. Create a new
3962 // TemplateArgument with the converted expression.
3963 Converted = TemplateArgument(Arg);
3964 return Owned(Arg);
3965 }
3966
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003967 QualType IntegerType = Context.getCanonicalType(ParamType);
John McCall183700f2009-09-21 23:43:11 +00003968 if (const EnumType *Enum = IntegerType->getAs<EnumType>())
Douglas Gregor02cbbd22009-06-11 18:10:32 +00003969 IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType());
Douglas Gregorf80a9d52009-03-14 00:20:21 +00003970
Douglas Gregorc7469372011-05-04 21:55:00 +00003971 if (ParamType->isBooleanType()) {
3972 // Value must be zero or one.
3973 Value = Value != 0;
3974 unsigned AllowedBits = Context.getTypeSize(IntegerType);
3975 if (Value.getBitWidth() != AllowedBits)
3976 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003977 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003978 } else {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003979 llvm::APSInt OldValue = Value;
Douglas Gregorc7469372011-05-04 21:55:00 +00003980
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00003981 // Coerce the template argument's value to the value it will have
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003982 // based on the template parameter's type.
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003983 unsigned AllowedBits = Context.getTypeSize(IntegerType);
Douglas Gregor0d4fd8e2010-03-26 00:39:40 +00003984 if (Value.getBitWidth() != AllowedBits)
Jay Foad9f71a8f2010-12-07 08:25:34 +00003985 Value = Value.extOrTrunc(AllowedBits);
Douglas Gregor575a1c92011-05-20 16:38:50 +00003986 Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType());
Douglas Gregorc7469372011-05-04 21:55:00 +00003987
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003988 // Complain if an unsigned parameter received a negative value.
Douglas Gregor575a1c92011-05-20 16:38:50 +00003989 if (IntegerType->isUnsignedIntegerOrEnumerationType()
Douglas Gregorc7469372011-05-04 21:55:00 +00003990 && (OldValue.isSigned() && OldValue.isNegative())) {
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003991 Diag(Arg->getSourceRange().getBegin(), diag::warn_template_arg_negative)
3992 << OldValue.toString(10) << Value.toString(10) << Param->getType()
3993 << Arg->getSourceRange();
3994 Diag(Param->getLocation(), diag::note_template_param_here);
3995 }
Douglas Gregorc7469372011-05-04 21:55:00 +00003996
Douglas Gregor1a6e0342010-03-26 02:38:37 +00003997 // Complain if we overflowed the template parameter's type.
3998 unsigned RequiredBits;
Douglas Gregor575a1c92011-05-20 16:38:50 +00003999 if (IntegerType->isUnsignedIntegerOrEnumerationType())
Douglas Gregor1a6e0342010-03-26 02:38:37 +00004000 RequiredBits = OldValue.getActiveBits();
4001 else if (OldValue.isUnsigned())
4002 RequiredBits = OldValue.getActiveBits() + 1;
4003 else
4004 RequiredBits = OldValue.getMinSignedBits();
4005 if (RequiredBits > AllowedBits) {
4006 Diag(Arg->getSourceRange().getBegin(),
4007 diag::warn_template_arg_too_large)
4008 << OldValue.toString(10) << Value.toString(10) << Param->getType()
4009 << Arg->getSourceRange();
4010 Diag(Param->getLocation(), diag::note_template_param_here);
4011 }
Douglas Gregorf80a9d52009-03-14 00:20:21 +00004012 }
Douglas Gregor3e00bad2009-02-17 01:05:43 +00004013
John McCall833ca992009-10-29 08:12:44 +00004014 Converted = TemplateArgument(Value,
Douglas Gregor6b63f552011-08-09 01:55:14 +00004015 ParamType->isEnumeralType()
4016 ? Context.getCanonicalType(ParamType)
4017 : IntegerType);
John Wiegley429bb272011-04-08 18:41:53 +00004018 return Owned(Arg);
Douglas Gregor6ae5e662009-02-10 23:36:10 +00004019 }
Douglas Gregora35284b2009-02-11 00:19:33 +00004020
Richard Smith4f870622011-10-27 22:11:44 +00004021 QualType ArgType = Arg->getType();
John McCall6bb80172010-03-30 21:47:33 +00004022 DeclAccessPair FoundResult; // temporary for ResolveOverloadedFunction
4023
Douglas Gregorb7a09262010-04-01 18:32:35 +00004024 // C++0x [temp.arg.nontype]p5 bullets 2, 4 and 6 permit conversion
4025 // from a template argument of type std::nullptr_t to a non-type
4026 // template parameter of type pointer to object, pointer to
4027 // function, or pointer-to-member, respectively.
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004028 if (ArgType->isNullPtrType()) {
4029 if (ParamType->isPointerType() || ParamType->isMemberPointerType()) {
4030 Converted = TemplateArgument((NamedDecl *)0);
4031 return Owned(Arg);
4032 }
4033
4034 if (ParamType->isNullPtrType()) {
4035 llvm::APSInt Zero(Context.getTypeSize(Context.NullPtrTy), true);
4036 Converted = TemplateArgument(Zero, Context.NullPtrTy);
4037 return Owned(Arg);
4038 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00004039 }
4040
Douglas Gregorb86b0572009-02-11 01:18:59 +00004041 // Handle pointer-to-function, reference-to-function, and
4042 // pointer-to-member-function all in (roughly) the same way.
4043 if (// -- For a non-type template-parameter of type pointer to
4044 // function, only the function-to-pointer conversion (4.3) is
4045 // applied. If the template-argument represents a set of
4046 // overloaded functions (or a pointer to such), the matching
4047 // function is selected from the set (13.4).
4048 (ParamType->isPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00004049 ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00004050 // -- For a non-type template-parameter of type reference to
4051 // function, no conversions apply. If the template-argument
4052 // represents a set of overloaded functions, the matching
4053 // function is selected from the set (13.4).
4054 (ParamType->isReferenceType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00004055 ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) ||
Douglas Gregorb86b0572009-02-11 01:18:59 +00004056 // -- For a non-type template-parameter of type pointer to
4057 // member function, no conversions apply. If the
4058 // template-argument represents a set of overloaded member
4059 // functions, the matching member function is selected from
4060 // the set (13.4).
4061 (ParamType->isMemberPointerType() &&
Ted Kremenek6217b802009-07-29 21:53:49 +00004062 ParamType->getAs<MemberPointerType>()->getPointeeType()
Douglas Gregorb86b0572009-02-11 01:18:59 +00004063 ->isFunctionType())) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004064
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004065 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004066 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType,
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004067 true,
4068 FoundResult)) {
4069 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00004070 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004071
4072 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
4073 ArgType = Arg->getType();
4074 } else
John Wiegley429bb272011-04-08 18:41:53 +00004075 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00004076 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004077
John Wiegley429bb272011-04-08 18:41:53 +00004078 if (!ParamType->isMemberPointerType()) {
4079 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4080 ParamType,
4081 Arg, Converted))
4082 return ExprError();
4083 return Owned(Arg);
4084 }
Douglas Gregorb7a09262010-04-01 18:32:35 +00004085
John McCallf85e1932011-06-15 23:02:42 +00004086 bool ObjCLifetimeConversion;
Douglas Gregor14d0aee2011-01-27 00:58:17 +00004087 if (IsQualificationConversion(ArgType, ParamType.getNonReferenceType(),
John McCallf85e1932011-06-15 23:02:42 +00004088 false, ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00004089 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
4090 Arg->getValueKind()).take();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004091 } else if (!Context.hasSameUnqualifiedType(ArgType,
4092 ParamType.getNonReferenceType())) {
Douglas Gregora35284b2009-02-11 00:19:33 +00004093 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00004094 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregora35284b2009-02-11 00:19:33 +00004095 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00004096 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregora35284b2009-02-11 00:19:33 +00004097 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00004098 return ExprError();
Douglas Gregora35284b2009-02-11 00:19:33 +00004099 }
Mike Stump1eb44332009-09-09 15:08:12 +00004100
John Wiegley429bb272011-04-08 18:41:53 +00004101 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
4102 return ExprError();
4103 return Owned(Arg);
Douglas Gregora35284b2009-02-11 00:19:33 +00004104 }
4105
Chris Lattnerfe90de72009-02-20 21:37:53 +00004106 if (ParamType->isPointerType()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00004107 // -- for a non-type template-parameter of type pointer to
4108 // object, qualification conversions (4.4) and the
4109 // array-to-pointer conversion (4.2) are applied.
Sebastian Redl6e8ed162009-05-10 18:38:11 +00004110 // C++0x also allows a value of std::nullptr_t.
Eli Friedman13578692010-08-05 02:49:48 +00004111 assert(ParamType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00004112 "Only object pointers allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004113
John Wiegley429bb272011-04-08 18:41:53 +00004114 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4115 ParamType,
4116 Arg, Converted))
4117 return ExprError();
4118 return Owned(Arg);
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004119 }
Mike Stump1eb44332009-09-09 15:08:12 +00004120
Ted Kremenek6217b802009-07-29 21:53:49 +00004121 if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
Douglas Gregorb86b0572009-02-11 01:18:59 +00004122 // -- For a non-type template-parameter of type reference to
4123 // object, no conversions apply. The type referred to by the
4124 // reference may be more cv-qualified than the (otherwise
4125 // identical) type of the template-argument. The
4126 // template-parameter is bound directly to the
4127 // template-argument, which must be an lvalue.
Eli Friedman13578692010-08-05 02:49:48 +00004128 assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() &&
Douglas Gregorb86b0572009-02-11 01:18:59 +00004129 "Only object references allowed here");
Douglas Gregorf684e6e2009-02-11 00:44:29 +00004130
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004131 if (Arg->getType() == Context.OverloadTy) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004132 if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg,
4133 ParamRefType->getPointeeType(),
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004134 true,
4135 FoundResult)) {
4136 if (DiagnoseUseOfDecl(Fn, Arg->getSourceRange().getBegin()))
John Wiegley429bb272011-04-08 18:41:53 +00004137 return ExprError();
Douglas Gregor1a8cf732010-04-14 23:11:21 +00004138
4139 Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn);
4140 ArgType = Arg->getType();
4141 } else
John Wiegley429bb272011-04-08 18:41:53 +00004142 return ExprError();
Douglas Gregorb86b0572009-02-11 01:18:59 +00004143 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004144
John Wiegley429bb272011-04-08 18:41:53 +00004145 if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param,
4146 ParamType,
4147 Arg, Converted))
4148 return ExprError();
4149 return Owned(Arg);
Douglas Gregorb86b0572009-02-11 01:18:59 +00004150 }
Douglas Gregor658bbb52009-02-11 16:16:59 +00004151
4152 // -- For a non-type template-parameter of type pointer to data
4153 // member, qualification conversions (4.4) are applied.
4154 assert(ParamType->isMemberPointerType() && "Only pointers to members remain");
4155
John McCallf85e1932011-06-15 23:02:42 +00004156 bool ObjCLifetimeConversion;
Douglas Gregor8e6563b2009-02-11 18:22:40 +00004157 if (Context.hasSameUnqualifiedType(ParamType, ArgType)) {
Douglas Gregor658bbb52009-02-11 16:16:59 +00004158 // Types match exactly: nothing more to do here.
John McCallf85e1932011-06-15 23:02:42 +00004159 } else if (IsQualificationConversion(ArgType, ParamType, false,
4160 ObjCLifetimeConversion)) {
Eli Friedmanc1c0dfb2011-09-27 21:58:52 +00004161 Arg = ImpCastExprToType(Arg, ParamType, CK_NoOp,
4162 Arg->getValueKind()).take();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004163 } else {
4164 // We can't perform this conversion.
Mike Stump1eb44332009-09-09 15:08:12 +00004165 Diag(Arg->getSourceRange().getBegin(),
Douglas Gregor658bbb52009-02-11 16:16:59 +00004166 diag::err_template_arg_not_convertible)
Douglas Gregor2943aed2009-03-03 04:44:36 +00004167 << Arg->getType() << InstantiatedParamType << Arg->getSourceRange();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004168 Diag(Param->getLocation(), diag::note_template_param_here);
John Wiegley429bb272011-04-08 18:41:53 +00004169 return ExprError();
Douglas Gregor658bbb52009-02-11 16:16:59 +00004170 }
4171
John Wiegley429bb272011-04-08 18:41:53 +00004172 if (CheckTemplateArgumentPointerToMember(Arg, Converted))
4173 return ExprError();
4174 return Owned(Arg);
Douglas Gregorc15cb382009-02-09 23:23:08 +00004175}
4176
4177/// \brief Check a template argument against its corresponding
4178/// template template parameter.
4179///
4180/// This routine implements the semantics of C++ [temp.arg.template].
4181/// It returns true if an error occurred, and false otherwise.
4182bool Sema::CheckTemplateArgument(TemplateTemplateParmDecl *Param,
Douglas Gregor788cd062009-11-11 01:00:40 +00004183 const TemplateArgumentLoc &Arg) {
4184 TemplateName Name = Arg.getArgument().getAsTemplate();
4185 TemplateDecl *Template = Name.getAsTemplateDecl();
4186 if (!Template) {
4187 // Any dependent template name is fine.
4188 assert(Name.isDependent() && "Non-dependent template isn't a declaration?");
4189 return false;
4190 }
Douglas Gregordd0574e2009-02-10 00:24:35 +00004191
Richard Smith3e4c6c42011-05-05 21:57:07 +00004192 // C++0x [temp.arg.template]p1:
Douglas Gregordd0574e2009-02-10 00:24:35 +00004193 // A template-argument for a template template-parameter shall be
Richard Smith3e4c6c42011-05-05 21:57:07 +00004194 // the name of a class template or an alias template, expressed as an
4195 // id-expression. When the template-argument names a class template, only
Douglas Gregordd0574e2009-02-10 00:24:35 +00004196 // primary class templates are considered when matching the
4197 // template template argument with the corresponding parameter;
4198 // partial specializations are not considered even if their
4199 // parameter lists match that of the template template parameter.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004200 //
4201 // Note that we also allow template template parameters here, which
4202 // will happen when we are dealing with, e.g., class template
4203 // partial specializations.
Mike Stump1eb44332009-09-09 15:08:12 +00004204 if (!isa<ClassTemplateDecl>(Template) &&
Richard Smith3e4c6c42011-05-05 21:57:07 +00004205 !isa<TemplateTemplateParmDecl>(Template) &&
4206 !isa<TypeAliasTemplateDecl>(Template)) {
Mike Stump1eb44332009-09-09 15:08:12 +00004207 assert(isa<FunctionTemplateDecl>(Template) &&
Douglas Gregordd0574e2009-02-10 00:24:35 +00004208 "Only function templates are possible here");
Douglas Gregor788cd062009-11-11 01:00:40 +00004209 Diag(Arg.getLocation(), diag::err_template_arg_not_class_template);
Douglas Gregore53060f2009-06-25 22:08:12 +00004210 Diag(Template->getLocation(), diag::note_template_arg_refers_here_func)
Douglas Gregordd0574e2009-02-10 00:24:35 +00004211 << Template;
4212 }
4213
4214 return !TemplateParameterListsAreEqual(Template->getTemplateParameters(),
4215 Param->getTemplateParameters(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004216 true,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004217 TPL_TemplateTemplateArgumentMatch,
Douglas Gregor788cd062009-11-11 01:00:40 +00004218 Arg.getLocation());
Douglas Gregorc15cb382009-02-09 23:23:08 +00004219}
4220
Douglas Gregor02024a92010-03-28 02:42:43 +00004221/// \brief Given a non-type template argument that refers to a
4222/// declaration and the type of its corresponding non-type template
4223/// parameter, produce an expression that properly refers to that
4224/// declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004225ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004226Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg,
4227 QualType ParamType,
4228 SourceLocation Loc) {
4229 assert(Arg.getKind() == TemplateArgument::Declaration &&
4230 "Only declaration template arguments permitted here");
4231 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl());
4232
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004233 if (VD->getDeclContext()->isRecord() &&
Douglas Gregor02024a92010-03-28 02:42:43 +00004234 (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD))) {
4235 // If the value is a class member, we might have a pointer-to-member.
4236 // Determine whether the non-type template template parameter is of
4237 // pointer-to-member type. If so, we need to build an appropriate
4238 // expression for a pointer-to-member, since a "normal" DeclRefExpr
4239 // would refer to the member itself.
4240 if (ParamType->isMemberPointerType()) {
4241 QualType ClassType
4242 = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext()));
4243 NestedNameSpecifier *Qualifier
John McCall9ae2f072010-08-23 23:25:46 +00004244 = NestedNameSpecifier::Create(Context, 0, false,
4245 ClassType.getTypePtr());
Douglas Gregor02024a92010-03-28 02:42:43 +00004246 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00004247 SS.MakeTrivial(Context, Qualifier, Loc);
John McCalldfa1edb2010-11-23 20:48:44 +00004248
4249 // The actual value-ness of this is unimportant, but for
4250 // internal consistency's sake, references to instance methods
4251 // are r-values.
4252 ExprValueKind VK = VK_LValue;
4253 if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance())
4254 VK = VK_RValue;
4255
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004256 ExprResult RefExpr = BuildDeclRefExpr(VD,
John McCallf89e55a2010-11-18 06:31:45 +00004257 VD->getType().getNonReferenceType(),
John McCalldfa1edb2010-11-23 20:48:44 +00004258 VK,
John McCallf89e55a2010-11-18 06:31:45 +00004259 Loc,
4260 &SS);
Douglas Gregor02024a92010-03-28 02:42:43 +00004261 if (RefExpr.isInvalid())
4262 return ExprError();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004263
John McCall2de56d12010-08-25 11:45:40 +00004264 RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004265
Douglas Gregorc0c83002010-04-30 21:46:38 +00004266 // We might need to perform a trailing qualification conversion, since
4267 // the element type on the parameter could be more qualified than the
4268 // element type in the expression we constructed.
John McCallf85e1932011-06-15 23:02:42 +00004269 bool ObjCLifetimeConversion;
Douglas Gregorc0c83002010-04-30 21:46:38 +00004270 if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(),
John McCallf85e1932011-06-15 23:02:42 +00004271 ParamType.getUnqualifiedType(), false,
4272 ObjCLifetimeConversion))
John Wiegley429bb272011-04-08 18:41:53 +00004273 RefExpr = ImpCastExprToType(RefExpr.take(), ParamType.getUnqualifiedType(), CK_NoOp);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004274
Douglas Gregor02024a92010-03-28 02:42:43 +00004275 assert(!RefExpr.isInvalid() &&
4276 Context.hasSameType(((Expr*) RefExpr.get())->getType(),
Douglas Gregorc0c83002010-04-30 21:46:38 +00004277 ParamType.getUnqualifiedType()));
Douglas Gregor02024a92010-03-28 02:42:43 +00004278 return move(RefExpr);
4279 }
4280 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004281
Douglas Gregor02024a92010-03-28 02:42:43 +00004282 QualType T = VD->getType().getNonReferenceType();
4283 if (ParamType->isPointerType()) {
Douglas Gregorb7a09262010-04-01 18:32:35 +00004284 // When the non-type template parameter is a pointer, take the
4285 // address of the declaration.
John McCallf89e55a2010-11-18 06:31:45 +00004286 ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004287 if (RefExpr.isInvalid())
4288 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004289
4290 if (T->isFunctionType() || T->isArrayType()) {
4291 // Decay functions and arrays.
John Wiegley429bb272011-04-08 18:41:53 +00004292 RefExpr = DefaultFunctionArrayConversion(RefExpr.take());
4293 if (RefExpr.isInvalid())
4294 return ExprError();
Douglas Gregorb7a09262010-04-01 18:32:35 +00004295
4296 return move(RefExpr);
Douglas Gregor02024a92010-03-28 02:42:43 +00004297 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004298
Douglas Gregorb7a09262010-04-01 18:32:35 +00004299 // Take the address of everything else
John McCall2de56d12010-08-25 11:45:40 +00004300 return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get());
Douglas Gregor02024a92010-03-28 02:42:43 +00004301 }
4302
John McCallf89e55a2010-11-18 06:31:45 +00004303 ExprValueKind VK = VK_RValue;
4304
Douglas Gregor02024a92010-03-28 02:42:43 +00004305 // If the non-type template parameter has reference type, qualify the
4306 // resulting declaration reference with the extra qualifiers on the
4307 // type that the reference refers to.
John McCallf89e55a2010-11-18 06:31:45 +00004308 if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) {
4309 VK = VK_LValue;
4310 T = Context.getQualifiedType(T,
4311 TargetRef->getPointeeType().getQualifiers());
4312 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004313
John McCallf89e55a2010-11-18 06:31:45 +00004314 return BuildDeclRefExpr(VD, T, VK, Loc);
Douglas Gregor02024a92010-03-28 02:42:43 +00004315}
4316
4317/// \brief Construct a new expression that refers to the given
4318/// integral template argument with the given source-location
4319/// information.
4320///
4321/// This routine takes care of the mapping from an integral template
4322/// argument (which may have any integral type) to the appropriate
4323/// literal value.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004324ExprResult
Douglas Gregor02024a92010-03-28 02:42:43 +00004325Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg,
4326 SourceLocation Loc) {
4327 assert(Arg.getKind() == TemplateArgument::Integral &&
Douglas Gregord3731192011-01-10 07:32:04 +00004328 "Operation is only valid for integral template arguments");
Douglas Gregor02024a92010-03-28 02:42:43 +00004329 QualType T = Arg.getIntegralType();
Douglas Gregor5cee1192011-07-27 05:40:30 +00004330 if (T->isAnyCharacterType()) {
4331 CharacterLiteral::CharacterKind Kind;
4332 if (T->isWideCharType())
4333 Kind = CharacterLiteral::Wide;
4334 else if (T->isChar16Type())
4335 Kind = CharacterLiteral::UTF16;
4336 else if (T->isChar32Type())
4337 Kind = CharacterLiteral::UTF32;
4338 else
4339 Kind = CharacterLiteral::Ascii;
4340
Douglas Gregor02024a92010-03-28 02:42:43 +00004341 return Owned(new (Context) CharacterLiteral(
Douglas Gregor5cee1192011-07-27 05:40:30 +00004342 Arg.getAsIntegral()->getZExtValue(),
4343 Kind, T, Loc));
4344 }
4345
Douglas Gregor02024a92010-03-28 02:42:43 +00004346 if (T->isBooleanType())
4347 return Owned(new (Context) CXXBoolLiteralExpr(
4348 Arg.getAsIntegral()->getBoolValue(),
Chris Lattner223de242011-04-25 20:37:58 +00004349 T, Loc));
Douglas Gregor02024a92010-03-28 02:42:43 +00004350
Douglas Gregor84ee2ee2011-05-21 23:15:46 +00004351 if (T->isNullPtrType())
4352 return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc));
4353
Chris Lattner223de242011-04-25 20:37:58 +00004354 // If this is an enum type that we're instantiating, we need to use an integer
4355 // type the same size as the enumerator. We don't want to build an
4356 // IntegerLiteral with enum type.
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004357 QualType BT;
4358 if (const EnumType *ET = T->getAs<EnumType>())
Chris Lattner223de242011-04-25 20:37:58 +00004359 BT = ET->getDecl()->getIntegerType();
Peter Collingbournefb7b3632010-12-15 15:06:14 +00004360 else
4361 BT = T;
4362
John McCall4e9272d2011-07-15 07:47:58 +00004363 Expr *E = IntegerLiteral::Create(Context, *Arg.getAsIntegral(), BT, Loc);
4364 if (T->isEnumeralType()) {
4365 // FIXME: This is a hack. We need a better way to handle substituted
4366 // non-type template parameters.
4367 E = CStyleCastExpr::Create(Context, T, VK_RValue, CK_IntegralCast, E, 0,
4368 Context.getTrivialTypeSourceInfo(T, Loc),
4369 Loc, Loc);
4370 }
4371
4372 return Owned(E);
Douglas Gregor02024a92010-03-28 02:42:43 +00004373}
4374
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004375/// \brief Match two template parameters within template parameter lists.
4376static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old,
4377 bool Complain,
4378 Sema::TemplateParameterListEqualKind Kind,
4379 SourceLocation TemplateArgLoc) {
4380 // Check the actual kind (type, non-type, template).
4381 if (Old->getKind() != New->getKind()) {
4382 if (Complain) {
4383 unsigned NextDiag = diag::err_template_param_different_kind;
4384 if (TemplateArgLoc.isValid()) {
4385 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4386 NextDiag = diag::note_template_param_different_kind;
4387 }
4388 S.Diag(New->getLocation(), NextDiag)
4389 << (Kind != Sema::TPL_TemplateMatch);
4390 S.Diag(Old->getLocation(), diag::note_template_prev_declaration)
4391 << (Kind != Sema::TPL_TemplateMatch);
4392 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004393
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004394 return false;
4395 }
4396
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004397 // Check that both are parameter packs are neither are parameter packs.
4398 // However, if we are matching a template template argument to a
Douglas Gregora0347822011-01-13 00:08:50 +00004399 // template template parameter, the template template parameter can have
4400 // a parameter pack where the template template argument does not.
4401 if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() &&
4402 !(Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4403 Old->isTemplateParameterPack())) {
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004404 if (Complain) {
4405 unsigned NextDiag = diag::err_template_parameter_pack_non_pack;
4406 if (TemplateArgLoc.isValid()) {
4407 S.Diag(TemplateArgLoc,
4408 diag::err_template_arg_template_params_mismatch);
4409 NextDiag = diag::note_template_parameter_pack_non_pack;
4410 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004411
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004412 unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0
4413 : isa<NonTypeTemplateParmDecl>(New)? 1
4414 : 2;
4415 S.Diag(New->getLocation(), NextDiag)
4416 << ParamKind << New->isParameterPack();
4417 S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here)
4418 << ParamKind << Old->isParameterPack();
4419 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004420
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004421 return false;
4422 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004423
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004424 // For non-type template parameters, check the type of the parameter.
4425 if (NonTypeTemplateParmDecl *OldNTTP
4426 = dyn_cast<NonTypeTemplateParmDecl>(Old)) {
4427 NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004428
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004429 // If we are matching a template template argument to a template
4430 // template parameter and one of the non-type template parameter types
4431 // is dependent, then we must wait until template instantiation time
4432 // to actually compare the arguments.
4433 if (Kind == Sema::TPL_TemplateTemplateArgumentMatch &&
4434 (OldNTTP->getType()->isDependentType() ||
4435 NewNTTP->getType()->isDependentType()))
4436 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004437
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004438 if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) {
4439 if (Complain) {
4440 unsigned NextDiag = diag::err_template_nontype_parm_different_type;
4441 if (TemplateArgLoc.isValid()) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004442 S.Diag(TemplateArgLoc,
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004443 diag::err_template_arg_template_params_mismatch);
4444 NextDiag = diag::note_template_nontype_parm_different_type;
4445 }
4446 S.Diag(NewNTTP->getLocation(), NextDiag)
4447 << NewNTTP->getType()
4448 << (Kind != Sema::TPL_TemplateMatch);
4449 S.Diag(OldNTTP->getLocation(),
4450 diag::note_template_nontype_parm_prev_declaration)
4451 << OldNTTP->getType();
4452 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004453
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004454 return false;
4455 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004456
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004457 return true;
4458 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004459
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004460 // For template template parameters, check the template parameter types.
4461 // The template parameter lists of template template
4462 // parameters must agree.
4463 if (TemplateTemplateParmDecl *OldTTP
4464 = dyn_cast<TemplateTemplateParmDecl>(Old)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004465 TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New);
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004466 return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(),
4467 OldTTP->getTemplateParameters(),
4468 Complain,
4469 (Kind == Sema::TPL_TemplateMatch
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004470 ? Sema::TPL_TemplateTemplateParmMatch
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004471 : Kind),
4472 TemplateArgLoc);
4473 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004474
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004475 return true;
4476}
Douglas Gregor02024a92010-03-28 02:42:43 +00004477
Douglas Gregora0347822011-01-13 00:08:50 +00004478/// \brief Diagnose a known arity mismatch when comparing template argument
4479/// lists.
4480static
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004481void DiagnoseTemplateParameterListArityMismatch(Sema &S,
Douglas Gregora0347822011-01-13 00:08:50 +00004482 TemplateParameterList *New,
4483 TemplateParameterList *Old,
4484 Sema::TemplateParameterListEqualKind Kind,
4485 SourceLocation TemplateArgLoc) {
4486 unsigned NextDiag = diag::err_template_param_list_different_arity;
4487 if (TemplateArgLoc.isValid()) {
4488 S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch);
4489 NextDiag = diag::note_template_param_list_different_arity;
4490 }
4491 S.Diag(New->getTemplateLoc(), NextDiag)
4492 << (New->size() > Old->size())
4493 << (Kind != Sema::TPL_TemplateMatch)
4494 << SourceRange(New->getTemplateLoc(), New->getRAngleLoc());
4495 S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
4496 << (Kind != Sema::TPL_TemplateMatch)
4497 << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc());
4498}
4499
Douglas Gregorddc29e12009-02-06 22:42:48 +00004500/// \brief Determine whether the given template parameter lists are
4501/// equivalent.
4502///
Mike Stump1eb44332009-09-09 15:08:12 +00004503/// \param New The new template parameter list, typically written in the
Douglas Gregorddc29e12009-02-06 22:42:48 +00004504/// source code as part of a new template declaration.
4505///
4506/// \param Old The old template parameter list, typically found via
4507/// name lookup of the template declared with this template parameter
4508/// list.
4509///
4510/// \param Complain If true, this routine will produce a diagnostic if
4511/// the template parameter lists are not equivalent.
4512///
Douglas Gregorfb898e12009-11-12 16:20:59 +00004513/// \param Kind describes how we are to match the template parameter lists.
Douglas Gregordd0574e2009-02-10 00:24:35 +00004514///
4515/// \param TemplateArgLoc If this source location is valid, then we
4516/// are actually checking the template parameter list of a template
4517/// argument (New) against the template parameter list of its
4518/// corresponding template template parameter (Old). We produce
4519/// slightly different diagnostics in this scenario.
4520///
Douglas Gregorddc29e12009-02-06 22:42:48 +00004521/// \returns True if the template parameter lists are equal, false
4522/// otherwise.
Mike Stump1eb44332009-09-09 15:08:12 +00004523bool
Douglas Gregorddc29e12009-02-06 22:42:48 +00004524Sema::TemplateParameterListsAreEqual(TemplateParameterList *New,
4525 TemplateParameterList *Old,
4526 bool Complain,
Douglas Gregorfb898e12009-11-12 16:20:59 +00004527 TemplateParameterListEqualKind Kind,
Douglas Gregordd0574e2009-02-10 00:24:35 +00004528 SourceLocation TemplateArgLoc) {
Douglas Gregora0347822011-01-13 00:08:50 +00004529 if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) {
4530 if (Complain)
4531 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4532 TemplateArgLoc);
Douglas Gregorddc29e12009-02-06 22:42:48 +00004533
4534 return false;
4535 }
4536
Douglas Gregorab7ddf02011-01-12 23:45:44 +00004537 // C++0x [temp.arg.template]p3:
4538 // A template-argument matches a template template-parameter (call it P)
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004539 // when each of the template parameters in the template-parameter-list of
Richard Smith3e4c6c42011-05-05 21:57:07 +00004540 // the template-argument's corresponding class template or alias template
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004541 // (call it A) matches the corresponding template parameter in the
Douglas Gregora0347822011-01-13 00:08:50 +00004542 // template-parameter-list of P. [...]
4543 TemplateParameterList::iterator NewParm = New->begin();
4544 TemplateParameterList::iterator NewParmEnd = New->end();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004545 for (TemplateParameterList::iterator OldParm = Old->begin(),
Douglas Gregora0347822011-01-13 00:08:50 +00004546 OldParmEnd = Old->end();
4547 OldParm != OldParmEnd; ++OldParm) {
Douglas Gregorc421f542011-01-13 18:47:47 +00004548 if (Kind != TPL_TemplateTemplateArgumentMatch ||
4549 !(*OldParm)->isTemplateParameterPack()) {
Douglas Gregora0347822011-01-13 00:08:50 +00004550 if (NewParm == NewParmEnd) {
4551 if (Complain)
4552 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4553 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004554
Douglas Gregora0347822011-01-13 00:08:50 +00004555 return false;
4556 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004557
Douglas Gregora0347822011-01-13 00:08:50 +00004558 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4559 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004560 return false;
4561
Douglas Gregora0347822011-01-13 00:08:50 +00004562 ++NewParm;
4563 continue;
4564 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004565
Douglas Gregora0347822011-01-13 00:08:50 +00004566 // C++0x [temp.arg.template]p3:
NAKAMURA Takumi00995302011-01-27 07:09:49 +00004567 // [...] When P's template- parameter-list contains a template parameter
4568 // pack (14.5.3), the template parameter pack will match zero or more
4569 // template parameters or template parameter packs in the
Douglas Gregora0347822011-01-13 00:08:50 +00004570 // template-parameter-list of A with the same type and form as the
4571 // template parameter pack in P (ignoring whether those template
4572 // parameters are template parameter packs).
4573 for (; NewParm != NewParmEnd; ++NewParm) {
4574 if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain,
4575 Kind, TemplateArgLoc))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004576 return false;
Douglas Gregora0347822011-01-13 00:08:50 +00004577 }
Douglas Gregorddc29e12009-02-06 22:42:48 +00004578 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004579
Douglas Gregora0347822011-01-13 00:08:50 +00004580 // Make sure we exhausted all of the arguments.
4581 if (NewParm != NewParmEnd) {
4582 if (Complain)
4583 DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind,
4584 TemplateArgLoc);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004585
Douglas Gregora0347822011-01-13 00:08:50 +00004586 return false;
4587 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004588
Douglas Gregorddc29e12009-02-06 22:42:48 +00004589 return true;
4590}
4591
4592/// \brief Check whether a template can be declared within this scope.
4593///
4594/// If the template declaration is valid in this scope, returns
4595/// false. Otherwise, issues a diagnostic and returns true.
Mike Stump1eb44332009-09-09 15:08:12 +00004596bool
Douglas Gregor05396e22009-08-25 17:23:04 +00004597Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) {
Douglas Gregorfb35e8f2011-11-03 16:37:14 +00004598 if (!S)
4599 return false;
4600
Douglas Gregorddc29e12009-02-06 22:42:48 +00004601 // Find the nearest enclosing declaration scope.
4602 while ((S->getFlags() & Scope::DeclScope) == 0 ||
4603 (S->getFlags() & Scope::TemplateParamScope) != 0)
4604 S = S->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00004605
Douglas Gregorddc29e12009-02-06 22:42:48 +00004606 // C++ [temp]p2:
4607 // A template-declaration can appear only as a namespace scope or
4608 // class scope declaration.
4609 DeclContext *Ctx = static_cast<DeclContext *>(S->getEntity());
Eli Friedman1503f772009-07-31 01:43:05 +00004610 if (Ctx && isa<LinkageSpecDecl>(Ctx) &&
4611 cast<LinkageSpecDecl>(Ctx)->getLanguage() != LinkageSpecDecl::lang_cxx)
Mike Stump1eb44332009-09-09 15:08:12 +00004612 return Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage)
Douglas Gregor05396e22009-08-25 17:23:04 +00004613 << TemplateParams->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00004614
Eli Friedman1503f772009-07-31 01:43:05 +00004615 while (Ctx && isa<LinkageSpecDecl>(Ctx))
Douglas Gregorddc29e12009-02-06 22:42:48 +00004616 Ctx = Ctx->getParent();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004617
4618 if (Ctx && (Ctx->isFileContext() || Ctx->isRecord()))
4619 return false;
4620
Mike Stump1eb44332009-09-09 15:08:12 +00004621 return Diag(TemplateParams->getTemplateLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004622 diag::err_template_outside_namespace_or_class_scope)
4623 << TemplateParams->getSourceRange();
Douglas Gregorddc29e12009-02-06 22:42:48 +00004624}
Douglas Gregorcc636682009-02-17 23:15:12 +00004625
Douglas Gregord5cb8762009-10-07 00:13:32 +00004626/// \brief Determine what kind of template specialization the given declaration
4627/// is.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00004628static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004629 if (!D)
4630 return TSK_Undeclared;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004631
Douglas Gregorf6b11852009-10-08 15:14:33 +00004632 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D))
4633 return Record->getTemplateSpecializationKind();
Douglas Gregord5cb8762009-10-07 00:13:32 +00004634 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D))
4635 return Function->getTemplateSpecializationKind();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00004636 if (VarDecl *Var = dyn_cast<VarDecl>(D))
4637 return Var->getTemplateSpecializationKind();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004638
Douglas Gregord5cb8762009-10-07 00:13:32 +00004639 return TSK_Undeclared;
4640}
4641
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004642/// \brief Check whether a specialization is well-formed in the current
Douglas Gregor9302da62009-10-14 23:50:59 +00004643/// context.
Douglas Gregor88b70942009-02-25 22:02:03 +00004644///
Douglas Gregor9302da62009-10-14 23:50:59 +00004645/// This routine determines whether a template specialization can be declared
4646/// in the current context (C++ [temp.expl.spec]p2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004647///
4648/// \param S the semantic analysis object for which this check is being
4649/// performed.
4650///
4651/// \param Specialized the entity being specialized or instantiated, which
4652/// may be a kind of template (class template, function template, etc.) or
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004653/// a member of a class template (member function, static data member,
Douglas Gregord5cb8762009-10-07 00:13:32 +00004654/// member class).
4655///
4656/// \param PrevDecl the previous declaration of this entity, if any.
4657///
4658/// \param Loc the location of the explicit specialization or instantiation of
4659/// this entity.
4660///
4661/// \param IsPartialSpecialization whether this is a partial specialization of
4662/// a class template.
4663///
Douglas Gregord5cb8762009-10-07 00:13:32 +00004664/// \returns true if there was an error that we cannot recover from, false
4665/// otherwise.
4666static bool CheckTemplateSpecializationScope(Sema &S,
4667 NamedDecl *Specialized,
4668 NamedDecl *PrevDecl,
4669 SourceLocation Loc,
Douglas Gregor9302da62009-10-14 23:50:59 +00004670 bool IsPartialSpecialization) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004671 // Keep these "kind" numbers in sync with the %select statements in the
4672 // various diagnostics emitted by this routine.
4673 int EntityKind = 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004674 if (isa<ClassTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004675 EntityKind = IsPartialSpecialization? 1 : 0;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004676 else if (isa<FunctionTemplateDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004677 EntityKind = 2;
Ted Kremenekfe62b062011-01-14 22:31:36 +00004678 else if (isa<CXXMethodDecl>(Specialized))
Douglas Gregord5cb8762009-10-07 00:13:32 +00004679 EntityKind = 3;
4680 else if (isa<VarDecl>(Specialized))
4681 EntityKind = 4;
4682 else if (isa<RecordDecl>(Specialized))
4683 EntityKind = 5;
4684 else {
Douglas Gregor9302da62009-10-14 23:50:59 +00004685 S.Diag(Loc, diag::err_template_spec_unknown_kind);
4686 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregord5cb8762009-10-07 00:13:32 +00004687 return true;
4688 }
4689
Douglas Gregor88b70942009-02-25 22:02:03 +00004690 // C++ [temp.expl.spec]p2:
4691 // An explicit specialization shall be declared in the namespace
4692 // of which the template is a member, or, for member templates, in
4693 // the namespace of which the enclosing class or enclosing class
4694 // template is a member. An explicit specialization of a member
4695 // function, member class or static data member of a class
4696 // template shall be declared in the namespace of which the class
4697 // template is a member. Such a declaration may also be a
4698 // definition. If the declaration is not a definition, the
4699 // specialization may be defined later in the name- space in which
4700 // the explicit specialization was declared, or in a namespace
4701 // that encloses the one in which the explicit specialization was
4702 // declared.
Sebastian Redl7a126a42010-08-31 00:36:30 +00004703 if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004704 S.Diag(Loc, diag::err_template_spec_decl_function_scope)
Douglas Gregor9302da62009-10-14 23:50:59 +00004705 << Specialized;
Douglas Gregor88b70942009-02-25 22:02:03 +00004706 return true;
4707 }
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004708
Douglas Gregor0a407472009-10-07 17:30:37 +00004709 if (S.CurContext->isRecord() && !IsPartialSpecialization) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00004710 if (S.getLangOptions().MicrosoftExt) {
Francois Pichetaf0f4d02011-08-14 03:52:19 +00004711 // Do not warn for class scope explicit specialization during
4712 // instantiation, warning was already emitted during pattern
4713 // semantic analysis.
4714 if (!S.ActiveTemplateInstantiations.size())
4715 S.Diag(Loc, diag::ext_function_specialization_in_class)
4716 << Specialized;
4717 } else {
4718 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4719 << Specialized;
4720 return true;
4721 }
Douglas Gregor0a407472009-10-07 17:30:37 +00004722 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004723
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004724 if (S.CurContext->isRecord() &&
4725 !S.CurContext->Equals(Specialized->getDeclContext())) {
4726 // Make sure that we're specializing in the right record context.
4727 // Otherwise, things can go horribly wrong.
4728 S.Diag(Loc, diag::err_template_spec_decl_class_scope)
4729 << Specialized;
4730 return true;
4731 }
4732
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004733 // C++ [temp.class.spec]p6:
4734 // A class template partial specialization may be declared or redeclared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004735 // in any namespace scope in which its definition may be defined (14.5.1
4736 // and 14.5.2).
Douglas Gregord5cb8762009-10-07 00:13:32 +00004737 bool ComplainedAboutScope = false;
Douglas Gregor8e0c1182011-10-20 16:41:18 +00004738 DeclContext *SpecializedContext
Douglas Gregord5cb8762009-10-07 00:13:32 +00004739 = Specialized->getDeclContext()->getEnclosingNamespaceContext();
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004740 DeclContext *DC = S.CurContext->getEnclosingNamespaceContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004741 if ((!PrevDecl ||
Douglas Gregor9302da62009-10-14 23:50:59 +00004742 getTemplateSpecializationKind(PrevDecl) == TSK_Undeclared ||
4743 getTemplateSpecializationKind(PrevDecl) == TSK_ImplicitInstantiation)){
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004744 // C++ [temp.exp.spec]p2:
4745 // An explicit specialization shall be declared in the namespace of which
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004746 // the template is a member, or, for member templates, in the namespace
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004747 // of which the enclosing class or enclosing class template is a member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004748 // An explicit specialization of a member function, member class or
4749 // static data member of a class template shall be declared in the
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004750 // namespace of which the class template is a member.
4751 //
4752 // C++0x [temp.expl.spec]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004753 // An explicit specialization shall be declared in a namespace enclosing
Douglas Gregor121dc9a2010-09-12 05:08:28 +00004754 // the specialized template.
Richard Smithebaf0e62011-10-18 20:49:44 +00004755 if (!DC->InEnclosingNamespaceSetOf(SpecializedContext)) {
4756 bool IsCPlusPlus0xExtension = DC->Encloses(SpecializedContext);
4757 if (isa<TranslationUnitDecl>(SpecializedContext)) {
4758 assert(!IsCPlusPlus0xExtension &&
4759 "DC encloses TU but isn't in enclosing namespace set");
4760 S.Diag(Loc, diag::err_template_spec_decl_out_of_scope_global)
Douglas Gregora4d5de52010-09-12 05:24:55 +00004761 << EntityKind << Specialized;
Richard Smithebaf0e62011-10-18 20:49:44 +00004762 } else if (isa<NamespaceDecl>(SpecializedContext)) {
4763 int Diag;
4764 if (!IsCPlusPlus0xExtension)
4765 Diag = diag::err_template_spec_decl_out_of_scope;
4766 else if (!S.getLangOptions().CPlusPlus0x)
4767 Diag = diag::ext_template_spec_decl_out_of_scope;
4768 else
4769 Diag = diag::warn_cxx98_compat_template_spec_decl_out_of_scope;
4770 S.Diag(Loc, Diag)
4771 << EntityKind << Specialized << cast<NamedDecl>(SpecializedContext);
4772 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004773
Douglas Gregor9302da62009-10-14 23:50:59 +00004774 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Richard Smithebaf0e62011-10-18 20:49:44 +00004775 ComplainedAboutScope =
4776 !(IsCPlusPlus0xExtension && S.getLangOptions().CPlusPlus0x);
Douglas Gregor88b70942009-02-25 22:02:03 +00004777 }
Douglas Gregor88b70942009-02-25 22:02:03 +00004778 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004779
4780 // Make sure that this redeclaration (or definition) occurs in an enclosing
Douglas Gregor9302da62009-10-14 23:50:59 +00004781 // namespace.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004782 // Note that HandleDeclarator() performs this check for explicit
Douglas Gregord5cb8762009-10-07 00:13:32 +00004783 // specializations of function templates, static data members, and member
4784 // functions, so we skip the check here for those kinds of entities.
4785 // FIXME: HandleDeclarator's diagnostics aren't quite as good, though.
Douglas Gregor7974c3b2009-10-07 17:21:34 +00004786 // Should we refactor that check, so that it occurs later?
4787 if (!ComplainedAboutScope && !DC->Encloses(SpecializedContext) &&
Douglas Gregor9302da62009-10-14 23:50:59 +00004788 !(isa<FunctionTemplateDecl>(Specialized) || isa<VarDecl>(Specialized) ||
4789 isa<FunctionDecl>(Specialized))) {
Douglas Gregord5cb8762009-10-07 00:13:32 +00004790 if (isa<TranslationUnitDecl>(SpecializedContext))
4791 S.Diag(Loc, diag::err_template_spec_redecl_global_scope)
4792 << EntityKind << Specialized;
4793 else if (isa<NamespaceDecl>(SpecializedContext))
4794 S.Diag(Loc, diag::err_template_spec_redecl_out_of_scope)
4795 << EntityKind << Specialized
4796 << cast<NamedDecl>(SpecializedContext);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004797
Douglas Gregor9302da62009-10-14 23:50:59 +00004798 S.Diag(Specialized->getLocation(), diag::note_specialized_entity);
Douglas Gregor88b70942009-02-25 22:02:03 +00004799 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004800
Douglas Gregord5cb8762009-10-07 00:13:32 +00004801 // FIXME: check for specialization-after-instantiation errors and such.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004802
Douglas Gregor88b70942009-02-25 22:02:03 +00004803 return false;
4804}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004805
Douglas Gregorbacb9492011-01-03 21:13:47 +00004806/// \brief Subroutine of Sema::CheckClassTemplatePartialSpecializationArgs
4807/// that checks non-type template partial specialization arguments.
4808static bool CheckNonTypeClassTemplatePartialSpecializationArgs(Sema &S,
4809 NonTypeTemplateParmDecl *Param,
4810 const TemplateArgument *Args,
4811 unsigned NumArgs) {
4812 for (unsigned I = 0; I != NumArgs; ++I) {
4813 if (Args[I].getKind() == TemplateArgument::Pack) {
4814 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004815 Args[I].pack_begin(),
Douglas Gregorbacb9492011-01-03 21:13:47 +00004816 Args[I].pack_size()))
4817 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004818
Douglas Gregore94866f2009-06-12 21:21:02 +00004819 continue;
Douglas Gregorbacb9492011-01-03 21:13:47 +00004820 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004821
Douglas Gregorbacb9492011-01-03 21:13:47 +00004822 Expr *ArgExpr = Args[I].getAsExpr();
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004823 if (!ArgExpr) {
Douglas Gregore94866f2009-06-12 21:21:02 +00004824 continue;
Douglas Gregor6aa75cf2009-06-12 22:08:06 +00004825 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004826
Douglas Gregor7a21fd42011-01-03 21:37:45 +00004827 // We can have a pack expansion of any of the bullets below.
Douglas Gregorbacb9492011-01-03 21:13:47 +00004828 if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr))
4829 ArgExpr = Expansion->getPattern();
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004830
4831 // Strip off any implicit casts we added as part of type checking.
4832 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr))
4833 ArgExpr = ICE->getSubExpr();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004834
Douglas Gregore94866f2009-06-12 21:21:02 +00004835 // C++ [temp.class.spec]p8:
4836 // A non-type argument is non-specialized if it is the name of a
4837 // non-type parameter. All other non-type arguments are
4838 // specialized.
4839 //
4840 // Below, we check the two conditions that only apply to
4841 // specialized non-type arguments, so skip any non-specialized
4842 // arguments.
4843 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr))
Douglas Gregor54c53cc2011-01-04 23:35:54 +00004844 if (isa<NonTypeTemplateParmDecl>(DRE->getDecl()))
Douglas Gregore94866f2009-06-12 21:21:02 +00004845 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004846
Douglas Gregore94866f2009-06-12 21:21:02 +00004847 // C++ [temp.class.spec]p9:
4848 // Within the argument list of a class template partial
4849 // specialization, the following restrictions apply:
4850 // -- A partially specialized non-type argument expression
4851 // shall not involve a template parameter of the partial
4852 // specialization except when the argument expression is a
4853 // simple identifier.
4854 if (ArgExpr->isTypeDependent() || ArgExpr->isValueDependent()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004855 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004856 diag::err_dependent_non_type_arg_in_partial_spec)
4857 << ArgExpr->getSourceRange();
4858 return true;
4859 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004860
Douglas Gregore94866f2009-06-12 21:21:02 +00004861 // -- The type of a template parameter corresponding to a
4862 // specialized non-type argument shall not be dependent on a
4863 // parameter of the specialization.
4864 if (Param->getType()->isDependentType()) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004865 S.Diag(ArgExpr->getLocStart(),
Douglas Gregore94866f2009-06-12 21:21:02 +00004866 diag::err_dependent_typed_non_type_arg_in_partial_spec)
4867 << Param->getType()
4868 << ArgExpr->getSourceRange();
Douglas Gregorbacb9492011-01-03 21:13:47 +00004869 S.Diag(Param->getLocation(), diag::note_template_param_here);
Douglas Gregore94866f2009-06-12 21:21:02 +00004870 return true;
4871 }
4872 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004873
Douglas Gregorbacb9492011-01-03 21:13:47 +00004874 return false;
4875}
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004876
Douglas Gregorbacb9492011-01-03 21:13:47 +00004877/// \brief Check the non-type template arguments of a class template
4878/// partial specialization according to C++ [temp.class.spec]p9.
4879///
4880/// \param TemplateParams the template parameters of the primary class
4881/// template.
4882///
4883/// \param TemplateArg the template arguments of the class template
4884/// partial specialization.
4885///
4886/// \returns true if there was an error, false otherwise.
4887static bool CheckClassTemplatePartialSpecializationArgs(Sema &S,
4888 TemplateParameterList *TemplateParams,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004889 SmallVectorImpl<TemplateArgument> &TemplateArgs) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00004890 const TemplateArgument *ArgList = TemplateArgs.data();
4891
4892 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4893 NonTypeTemplateParmDecl *Param
4894 = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I));
4895 if (!Param)
4896 continue;
4897
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004898 if (CheckNonTypeClassTemplatePartialSpecializationArgs(S, Param,
Douglas Gregorbacb9492011-01-03 21:13:47 +00004899 &ArgList[I], 1))
4900 return true;
4901 }
Douglas Gregore94866f2009-06-12 21:21:02 +00004902
4903 return false;
4904}
4905
John McCalld226f652010-08-21 09:40:31 +00004906DeclResult
John McCall0f434ec2009-07-31 02:45:11 +00004907Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
4908 TagUseKind TUK,
Mike Stump1eb44332009-09-09 15:08:12 +00004909 SourceLocation KWLoc,
Douglas Gregord023aec2011-09-09 20:53:38 +00004910 SourceLocation ModulePrivateLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004911 CXXScopeSpec &SS,
Douglas Gregor7532dc62009-03-30 22:58:21 +00004912 TemplateTy TemplateD,
Douglas Gregorcc636682009-02-17 23:15:12 +00004913 SourceLocation TemplateNameLoc,
4914 SourceLocation LAngleLoc,
Douglas Gregor40808ce2009-03-09 23:48:35 +00004915 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregorcc636682009-02-17 23:15:12 +00004916 SourceLocation RAngleLoc,
4917 AttributeList *Attr,
4918 MultiTemplateParamsArg TemplateParameterLists) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004919 assert(TUK != TUK_Reference && "References are not specializations");
John McCallf1bbbb42009-09-04 01:14:41 +00004920
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00004921 // NOTE: KWLoc is the location of the tag keyword. This will instead
4922 // store the location of the outermost template keyword in the declaration.
4923 SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0
4924 ? TemplateParameterLists.get()[0]->getTemplateLoc() : SourceLocation();
4925
Douglas Gregorcc636682009-02-17 23:15:12 +00004926 // Find the class template we're specializing
Douglas Gregor7532dc62009-03-30 22:58:21 +00004927 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00004928 ClassTemplateDecl *ClassTemplate
Douglas Gregor8b13c082009-11-12 00:46:20 +00004929 = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl());
4930
4931 if (!ClassTemplate) {
4932 Diag(TemplateNameLoc, diag::err_not_class_template_specialization)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004933 << (Name.getAsTemplateDecl() &&
Douglas Gregor8b13c082009-11-12 00:46:20 +00004934 isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl()));
4935 return true;
4936 }
Douglas Gregorcc636682009-02-17 23:15:12 +00004937
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004938 bool isExplicitSpecialization = false;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00004939 bool isPartialSpecialization = false;
4940
Douglas Gregor88b70942009-02-25 22:02:03 +00004941 // Check the validity of the template headers that introduce this
4942 // template.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00004943 // FIXME: We probably shouldn't complain about these headers for
4944 // friend declarations.
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004945 bool Invalid = false;
Douglas Gregor05396e22009-08-25 17:23:04 +00004946 TemplateParameterList *TemplateParams
Douglas Gregorc8406492011-05-10 18:27:06 +00004947 = MatchTemplateParametersToScopeSpecifier(TemplateNameLoc,
4948 TemplateNameLoc,
4949 SS,
Mike Stump1eb44332009-09-09 15:08:12 +00004950 (TemplateParameterList**)TemplateParameterLists.get(),
Douglas Gregor1fef4e62009-10-07 22:35:40 +00004951 TemplateParameterLists.size(),
John McCall77e8b112010-04-13 20:37:33 +00004952 TUK == TUK_Friend,
Douglas Gregor0167f3c2010-07-14 23:14:12 +00004953 isExplicitSpecialization,
4954 Invalid);
4955 if (Invalid)
4956 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004957
Douglas Gregor05396e22009-08-25 17:23:04 +00004958 if (TemplateParams && TemplateParams->size() > 0) {
4959 isPartialSpecialization = true;
Douglas Gregor88b70942009-02-25 22:02:03 +00004960
Douglas Gregorb0ee93c2010-12-21 08:14:57 +00004961 if (TUK == TUK_Friend) {
4962 Diag(KWLoc, diag::err_partial_specialization_friend)
4963 << SourceRange(LAngleLoc, RAngleLoc);
4964 return true;
4965 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00004966
Douglas Gregor05396e22009-08-25 17:23:04 +00004967 // C++ [temp.class.spec]p10:
4968 // The template parameter list of a specialization shall not
4969 // contain default template argument values.
4970 for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
4971 Decl *Param = TemplateParams->getParam(I);
4972 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) {
4973 if (TTP->hasDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004974 Diag(TTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004975 diag::err_default_arg_in_partial_spec);
John McCall833ca992009-10-29 08:12:44 +00004976 TTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004977 }
4978 } else if (NonTypeTemplateParmDecl *NTTP
4979 = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
4980 if (Expr *DefArg = NTTP->getDefaultArgument()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004981 Diag(NTTP->getDefaultArgumentLoc(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004982 diag::err_default_arg_in_partial_spec)
4983 << DefArg->getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004984 NTTP->removeDefaultArgument();
Douglas Gregor05396e22009-08-25 17:23:04 +00004985 }
4986 } else {
4987 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param);
Douglas Gregor788cd062009-11-11 01:00:40 +00004988 if (TTP->hasDefaultArgument()) {
4989 Diag(TTP->getDefaultArgument().getLocation(),
Douglas Gregor05396e22009-08-25 17:23:04 +00004990 diag::err_default_arg_in_partial_spec)
Douglas Gregor788cd062009-11-11 01:00:40 +00004991 << TTP->getDefaultArgument().getSourceRange();
Abramo Bagnarad92f7a22010-06-09 09:26:05 +00004992 TTP->removeDefaultArgument();
Douglas Gregorba1ecb52009-06-12 19:43:02 +00004993 }
4994 }
4995 }
Douglas Gregora735b202009-10-13 14:39:41 +00004996 } else if (TemplateParams) {
4997 if (TUK == TUK_Friend)
4998 Diag(KWLoc, diag::err_template_spec_friend)
Douglas Gregor849b2432010-03-31 17:46:05 +00004999 << FixItHint::CreateRemoval(
Douglas Gregora735b202009-10-13 14:39:41 +00005000 SourceRange(TemplateParams->getTemplateLoc(),
5001 TemplateParams->getRAngleLoc()))
5002 << SourceRange(LAngleLoc, RAngleLoc);
5003 else
5004 isExplicitSpecialization = true;
5005 } else if (TUK != TUK_Friend) {
Douglas Gregor05396e22009-08-25 17:23:04 +00005006 Diag(KWLoc, diag::err_template_spec_needs_header)
Douglas Gregor849b2432010-03-31 17:46:05 +00005007 << FixItHint::CreateInsertion(KWLoc, "template<> ");
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005008 isExplicitSpecialization = true;
5009 }
Douglas Gregor88b70942009-02-25 22:02:03 +00005010
Douglas Gregorcc636682009-02-17 23:15:12 +00005011 // Check that the specialization uses the same tag kind as the
5012 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00005013 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
5014 assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005015 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00005016 Kind, TUK == TUK_Definition, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00005017 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00005018 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregora3a83512009-04-01 23:51:29 +00005019 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00005020 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregora3a83512009-04-01 23:51:29 +00005021 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00005022 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005023 diag::note_previous_use);
5024 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
5025 }
5026
Douglas Gregor40808ce2009-03-09 23:48:35 +00005027 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00005028 TemplateArgumentListInfo TemplateArgs;
5029 TemplateArgs.setLAngleLoc(LAngleLoc);
5030 TemplateArgs.setRAngleLoc(RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00005031 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00005032
Douglas Gregor925910d2011-01-03 20:35:03 +00005033 // Check for unexpanded parameter packs in any of the template arguments.
5034 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005035 if (DiagnoseUnexpandedParameterPack(TemplateArgs[I],
Douglas Gregor925910d2011-01-03 20:35:03 +00005036 UPPC_PartialSpecialization))
5037 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005038
Douglas Gregorcc636682009-02-17 23:15:12 +00005039 // Check that the template argument list is well-formed for this
5040 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00005041 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00005042 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
5043 TemplateArgs, false, Converted))
Douglas Gregor212e81c2009-03-25 00:13:59 +00005044 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005045
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005046 // Find the class template (partial) specialization declaration that
Douglas Gregorcc636682009-02-17 23:15:12 +00005047 // corresponds to these arguments.
Douglas Gregorba1ecb52009-06-12 19:43:02 +00005048 if (isPartialSpecialization) {
Douglas Gregorbacb9492011-01-03 21:13:47 +00005049 if (CheckClassTemplatePartialSpecializationArgs(*this,
Douglas Gregore94866f2009-06-12 21:21:02 +00005050 ClassTemplate->getTemplateParameters(),
Douglas Gregorb9c66312010-12-23 17:13:55 +00005051 Converted))
Douglas Gregore94866f2009-06-12 21:21:02 +00005052 return true;
5053
Douglas Gregor561f8122011-07-01 01:22:09 +00005054 bool InstantiationDependent;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005055 if (!Name.isDependent() &&
Douglas Gregorde090962010-02-09 00:37:32 +00005056 !TemplateSpecializationType::anyDependentTemplateArguments(
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005057 TemplateArgs.getArgumentArray(),
Douglas Gregor561f8122011-07-01 01:22:09 +00005058 TemplateArgs.size(),
5059 InstantiationDependent)) {
Douglas Gregorde090962010-02-09 00:37:32 +00005060 Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized)
5061 << ClassTemplate->getDeclName();
5062 isPartialSpecialization = false;
Douglas Gregorde090962010-02-09 00:37:32 +00005063 }
5064 }
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005065
Douglas Gregorcc636682009-02-17 23:15:12 +00005066 void *InsertPos = 0;
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005067 ClassTemplateSpecializationDecl *PrevDecl = 0;
5068
5069 if (isPartialSpecialization)
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005070 // FIXME: Template parameter list matters, too
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005071 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005072 = ClassTemplate->findPartialSpecialization(Converted.data(),
5073 Converted.size(),
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005074 InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005075 else
5076 PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00005077 = ClassTemplate->findSpecialization(Converted.data(),
5078 Converted.size(), InsertPos);
Douglas Gregorcc636682009-02-17 23:15:12 +00005079
5080 ClassTemplateSpecializationDecl *Specialization = 0;
5081
Douglas Gregor88b70942009-02-25 22:02:03 +00005082 // Check whether we can declare a class template specialization in
5083 // the current scope.
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005084 if (TUK != TUK_Friend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005085 CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl,
5086 TemplateNameLoc,
Douglas Gregor9302da62009-10-14 23:50:59 +00005087 isPartialSpecialization))
Douglas Gregor212e81c2009-03-25 00:13:59 +00005088 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005089
Douglas Gregorb88e8882009-07-30 17:40:51 +00005090 // The canonical type
5091 QualType CanonType;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005092 if (PrevDecl &&
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005093 (PrevDecl->getSpecializationKind() == TSK_Undeclared ||
Douglas Gregorde090962010-02-09 00:37:32 +00005094 TUK == TUK_Friend)) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005095 // Since the only prior class template specialization with these
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005096 // arguments was referenced but not declared, or we're only
5097 // referencing this specialization as a friend, reuse that
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005098 // declaration node as our own, updating its source location and
5099 // the list of outer template parameters to reflect our new declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005100 Specialization = PrevDecl;
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005101 Specialization->setLocation(TemplateNameLoc);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005102 if (TemplateParameterLists.size() > 0) {
5103 Specialization->setTemplateParameterListsInfo(Context,
5104 TemplateParameterLists.size(),
5105 (TemplateParameterList**) TemplateParameterLists.release());
5106 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005107 PrevDecl = 0;
Douglas Gregorb88e8882009-07-30 17:40:51 +00005108 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005109 } else if (isPartialSpecialization) {
Douglas Gregorb88e8882009-07-30 17:40:51 +00005110 // Build the canonical type that describes the converted template
5111 // arguments of the class template partial specialization.
Douglas Gregorde090962010-02-09 00:37:32 +00005112 TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name);
5113 CanonType = Context.getTemplateSpecializationType(CanonTemplate,
Douglas Gregorb9c66312010-12-23 17:13:55 +00005114 Converted.data(),
5115 Converted.size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005116
5117 if (Context.hasSameType(CanonType,
Douglas Gregorb9c66312010-12-23 17:13:55 +00005118 ClassTemplate->getInjectedClassNameSpecialization())) {
5119 // C++ [temp.class.spec]p9b3:
5120 //
5121 // -- The argument list of the specialization shall not be identical
5122 // to the implicit argument list of the primary template.
5123 Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template)
Douglas Gregor8d267c52011-09-09 02:06:17 +00005124 << (TUK == TUK_Definition)
5125 << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc));
Douglas Gregorb9c66312010-12-23 17:13:55 +00005126 return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS,
5127 ClassTemplate->getIdentifier(),
5128 TemplateNameLoc,
5129 Attr,
5130 TemplateParams,
Douglas Gregore7612302011-09-09 19:05:14 +00005131 AS_none, /*ModulePrivateLoc=*/SourceLocation(),
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005132 TemplateParameterLists.size() - 1,
Abramo Bagnarac57c17d2011-03-10 13:28:31 +00005133 (TemplateParameterList**) TemplateParameterLists.release());
Douglas Gregorb9c66312010-12-23 17:13:55 +00005134 }
Douglas Gregorb88e8882009-07-30 17:40:51 +00005135
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005136 // Create a new class template partial specialization declaration node.
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005137 ClassTemplatePartialSpecializationDecl *PrevPartial
5138 = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl);
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005139 unsigned SequenceNumber = PrevPartial? PrevPartial->getSequenceNumber()
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005140 : ClassTemplate->getNextPartialSpecSequenceNumber();
Mike Stump1eb44332009-09-09 15:08:12 +00005141 ClassTemplatePartialSpecializationDecl *Partial
Douglas Gregor13c85772010-05-06 00:28:52 +00005142 = ClassTemplatePartialSpecializationDecl::Create(Context, Kind,
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005143 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005144 KWLoc, TemplateNameLoc,
Anders Carlsson91fdf6f2009-06-05 04:06:48 +00005145 TemplateParams,
5146 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005147 Converted.data(),
5148 Converted.size(),
John McCalld5532b62009-11-23 01:53:49 +00005149 TemplateArgs,
John McCall3cb0ebd2010-03-10 03:28:59 +00005150 CanonType,
Douglas Gregordc60c1e2010-04-30 05:56:50 +00005151 PrevPartial,
5152 SequenceNumber);
John McCallb6217662010-03-15 10:12:16 +00005153 SetNestedNameSpecifier(Partial, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005154 if (TemplateParameterLists.size() > 1 && SS.isSet()) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005155 Partial->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005156 TemplateParameterLists.size() - 1,
Abramo Bagnara9b934882010-06-12 08:15:14 +00005157 (TemplateParameterList**) TemplateParameterLists.release());
5158 }
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005159
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005160 if (!PrevPartial)
5161 ClassTemplate->AddPartialSpecialization(Partial, InsertPos);
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005162 Specialization = Partial;
Douglas Gregor031a5882009-06-13 00:26:55 +00005163
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005164 // If we are providing an explicit specialization of a member class
Douglas Gregored9c0f92009-10-29 00:04:11 +00005165 // template specialization, make a note of that.
5166 if (PrevPartial && PrevPartial->getInstantiatedFromMember())
5167 PrevPartial->setMemberSpecialization();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005168
Douglas Gregor031a5882009-06-13 00:26:55 +00005169 // Check that all of the template parameters of the class template
5170 // partial specialization are deducible from the template
5171 // arguments. If not, this class template partial specialization
5172 // will never be used.
Benjamin Kramer013b3662012-01-30 16:17:39 +00005173 llvm::SmallBitVector DeducibleParams(TemplateParams->size());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005174 MarkUsedTemplateParameters(Partial->getTemplateArgs(), true,
Douglas Gregored9c0f92009-10-29 00:04:11 +00005175 TemplateParams->getDepth(),
Douglas Gregore73bb602009-09-14 21:25:05 +00005176 DeducibleParams);
Douglas Gregor031a5882009-06-13 00:26:55 +00005177
Benjamin Kramer013b3662012-01-30 16:17:39 +00005178 if (!DeducibleParams.all()) {
5179 unsigned NumNonDeducible = DeducibleParams.size()-DeducibleParams.count();
Douglas Gregor031a5882009-06-13 00:26:55 +00005180 Diag(TemplateNameLoc, diag::warn_partial_specs_not_deducible)
5181 << (NumNonDeducible > 1)
5182 << SourceRange(TemplateNameLoc, RAngleLoc);
5183 for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) {
5184 if (!DeducibleParams[I]) {
5185 NamedDecl *Param = cast<NamedDecl>(TemplateParams->getParam(I));
5186 if (Param->getDeclName())
Mike Stump1eb44332009-09-09 15:08:12 +00005187 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005188 diag::note_partial_spec_unused_parameter)
5189 << Param->getDeclName();
5190 else
Mike Stump1eb44332009-09-09 15:08:12 +00005191 Diag(Param->getLocation(),
Douglas Gregor031a5882009-06-13 00:26:55 +00005192 diag::note_partial_spec_unused_parameter)
Benjamin Kramer476d8b82010-08-11 14:47:12 +00005193 << "<anonymous>";
Douglas Gregor031a5882009-06-13 00:26:55 +00005194 }
5195 }
5196 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005197 } else {
5198 // Create a new class template specialization declaration node for
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005199 // this explicit specialization or friend declaration.
Douglas Gregorcc636682009-02-17 23:15:12 +00005200 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00005201 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregorcc636682009-02-17 23:15:12 +00005202 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00005203 KWLoc, TemplateNameLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005204 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00005205 Converted.data(),
5206 Converted.size(),
Douglas Gregorcc636682009-02-17 23:15:12 +00005207 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00005208 SetNestedNameSpecifier(Specialization, SS);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005209 if (TemplateParameterLists.size() > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00005210 Specialization->setTemplateParameterListsInfo(Context,
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005211 TemplateParameterLists.size(),
Abramo Bagnara9b934882010-06-12 08:15:14 +00005212 (TemplateParameterList**) TemplateParameterLists.release());
5213 }
Douglas Gregorcc636682009-02-17 23:15:12 +00005214
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00005215 if (!PrevDecl)
5216 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Douglas Gregorb88e8882009-07-30 17:40:51 +00005217
5218 CanonType = Context.getTypeDeclType(Specialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005219 }
5220
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005221 // C++ [temp.expl.spec]p6:
5222 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005223 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005224 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005225 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005226 // use occurs; no diagnostic is required.
5227 if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005228 bool Okay = false;
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005229 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005230 // Is there any previous explicit specialization declaration?
5231 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5232 Okay = true;
5233 break;
5234 }
5235 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005236
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005237 if (!Okay) {
5238 SourceRange Range(TemplateNameLoc, RAngleLoc);
5239 Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
5240 << Context.getTypeDeclType(Specialization) << Range;
5241
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005242 Diag(PrevDecl->getPointOfInstantiation(),
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005243 diag::note_instantiation_required_here)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005244 << (PrevDecl->getTemplateSpecializationKind()
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005245 != TSK_ImplicitInstantiation);
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005246 return true;
5247 }
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005248 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005249
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005250 // If this is not a friend, note that this is an explicit specialization.
5251 if (TUK != TUK_Friend)
5252 Specialization->setSpecializationKind(TSK_ExplicitSpecialization);
Douglas Gregorcc636682009-02-17 23:15:12 +00005253
5254 // Check that this isn't a redefinition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005255 if (TUK == TUK_Definition) {
Douglas Gregor952b0172010-02-11 01:04:33 +00005256 if (RecordDecl *Def = Specialization->getDefinition()) {
Douglas Gregorcc636682009-02-17 23:15:12 +00005257 SourceRange Range(TemplateNameLoc, RAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00005258 Diag(TemplateNameLoc, diag::err_redefinition)
Douglas Gregorc8ab2562009-05-31 09:31:02 +00005259 << Context.getTypeDeclType(Specialization) << Range;
Douglas Gregorcc636682009-02-17 23:15:12 +00005260 Diag(Def->getLocation(), diag::note_previous_definition);
5261 Specialization->setInvalidDecl();
Douglas Gregor212e81c2009-03-25 00:13:59 +00005262 return true;
Douglas Gregorcc636682009-02-17 23:15:12 +00005263 }
5264 }
5265
John McCall7f1b9872010-12-18 03:30:47 +00005266 if (Attr)
5267 ProcessDeclAttributeList(S, Specialization, Attr);
5268
Douglas Gregord023aec2011-09-09 20:53:38 +00005269 if (ModulePrivateLoc.isValid())
5270 Diag(Specialization->getLocation(), diag::err_module_private_specialization)
5271 << (isPartialSpecialization? 1 : 0)
5272 << FixItHint::CreateRemoval(ModulePrivateLoc);
5273
Douglas Gregorfc705b82009-02-26 22:19:44 +00005274 // Build the fully-sugared type for this class template
5275 // specialization as the user wrote in the specialization
5276 // itself. This means that we'll pretty-print the type retrieved
5277 // from the specialization's declaration the way that the user
5278 // actually wrote the specialization, rather than formatting the
5279 // name based on the "canonical" representation used to store the
5280 // template arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00005281 TypeSourceInfo *WrittenTy
5282 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
5283 TemplateArgs, CanonType);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005284 if (TUK != TUK_Friend) {
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005285 Specialization->setTypeAsWritten(WrittenTy);
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00005286 Specialization->setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005287 }
Douglas Gregor40808ce2009-03-09 23:48:35 +00005288 TemplateArgsIn.release();
Douglas Gregorcc636682009-02-17 23:15:12 +00005289
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +00005290 // C++ [temp.expl.spec]p9:
5291 // A template explicit specialization is in the scope of the
5292 // namespace in which the template was defined.
5293 //
5294 // We actually implement this paragraph where we set the semantic
5295 // context (in the creation of the ClassTemplateSpecializationDecl),
5296 // but we also maintain the lexical context where the actual
5297 // definition occurs.
Douglas Gregorcc636682009-02-17 23:15:12 +00005298 Specialization->setLexicalDeclContext(CurContext);
Mike Stump1eb44332009-09-09 15:08:12 +00005299
Douglas Gregorcc636682009-02-17 23:15:12 +00005300 // We may be starting the definition of this specialization.
John McCall0f434ec2009-07-31 02:45:11 +00005301 if (TUK == TUK_Definition)
Douglas Gregorcc636682009-02-17 23:15:12 +00005302 Specialization->startDefinition();
5303
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005304 if (TUK == TUK_Friend) {
5305 FriendDecl *Friend = FriendDecl::Create(Context, CurContext,
5306 TemplateNameLoc,
John McCall32f2fb52010-03-25 18:04:51 +00005307 WrittenTy,
Douglas Gregorfc9cd612009-09-26 20:57:03 +00005308 /*FIXME:*/KWLoc);
5309 Friend->setAccess(AS_public);
5310 CurContext->addDecl(Friend);
5311 } else {
5312 // Add the specialization into its lexical context, so that it can
5313 // be seen when iterating through the list of declarations in that
5314 // context. However, specializations are not found by name lookup.
5315 CurContext->addDecl(Specialization);
5316 }
John McCalld226f652010-08-21 09:40:31 +00005317 return Specialization;
Douglas Gregorcc636682009-02-17 23:15:12 +00005318}
Douglas Gregord57959a2009-03-27 23:10:48 +00005319
John McCalld226f652010-08-21 09:40:31 +00005320Decl *Sema::ActOnTemplateDeclarator(Scope *S,
Douglas Gregore542c862009-06-23 23:11:28 +00005321 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005322 Declarator &D) {
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005323 return HandleDeclarator(S, D, move(TemplateParameterLists));
Douglas Gregore542c862009-06-23 23:11:28 +00005324}
5325
John McCalld226f652010-08-21 09:40:31 +00005326Decl *Sema::ActOnStartOfFunctionTemplateDef(Scope *FnBodyScope,
Douglas Gregor52591bf2009-06-24 00:54:41 +00005327 MultiTemplateParamsArg TemplateParameterLists,
John McCalld226f652010-08-21 09:40:31 +00005328 Declarator &D) {
Douglas Gregor52591bf2009-06-24 00:54:41 +00005329 assert(getCurFunctionDecl() == 0 && "Function parsing confused");
Abramo Bagnara075f8f12010-12-10 16:29:40 +00005330 DeclaratorChunk::FunctionTypeInfo &FTI = D.getFunctionTypeInfo();
Mike Stump1eb44332009-09-09 15:08:12 +00005331
Douglas Gregor52591bf2009-06-24 00:54:41 +00005332 if (FTI.hasPrototype) {
Mike Stump1eb44332009-09-09 15:08:12 +00005333 // FIXME: Diagnose arguments without names in C.
Douglas Gregor52591bf2009-06-24 00:54:41 +00005334 }
Mike Stump1eb44332009-09-09 15:08:12 +00005335
Douglas Gregor52591bf2009-06-24 00:54:41 +00005336 Scope *ParentScope = FnBodyScope->getParent();
Mike Stump1eb44332009-09-09 15:08:12 +00005337
Douglas Gregor45fa5602011-11-07 20:56:01 +00005338 D.setFunctionDefinitionKind(FDK_Definition);
John McCalld226f652010-08-21 09:40:31 +00005339 Decl *DP = HandleDeclarator(ParentScope, D,
Kaelyn Uhrain2c712f52011-10-11 00:28:45 +00005340 move(TemplateParameterLists));
Mike Stump1eb44332009-09-09 15:08:12 +00005341 if (FunctionTemplateDecl *FunctionTemplate
John McCalld226f652010-08-21 09:40:31 +00005342 = dyn_cast_or_null<FunctionTemplateDecl>(DP))
Mike Stump1eb44332009-09-09 15:08:12 +00005343 return ActOnStartOfFunctionDef(FnBodyScope,
John McCalld226f652010-08-21 09:40:31 +00005344 FunctionTemplate->getTemplatedDecl());
5345 if (FunctionDecl *Function = dyn_cast_or_null<FunctionDecl>(DP))
5346 return ActOnStartOfFunctionDef(FnBodyScope, Function);
5347 return 0;
Douglas Gregor52591bf2009-06-24 00:54:41 +00005348}
5349
John McCall75042392010-02-11 01:33:53 +00005350/// \brief Strips various properties off an implicit instantiation
5351/// that has just been explicitly specialized.
5352static void StripImplicitInstantiation(NamedDecl *D) {
Sean Huntcf807c42010-08-18 23:23:40 +00005353 D->dropAttrs();
John McCall75042392010-02-11 01:33:53 +00005354
5355 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
5356 FD->setInlineSpecified(false);
5357 }
5358}
5359
Nico Weberd1d512a2012-01-09 19:52:25 +00005360/// \brief Compute the diagnostic location for an explicit instantiation
5361// declaration or definition.
5362static SourceLocation DiagLocForExplicitInstantiation(
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005363 NamedDecl* D, SourceLocation PointOfInstantiation) {
Nico Weberd1d512a2012-01-09 19:52:25 +00005364 // Explicit instantiations following a specialization have no effect and
5365 // hence no PointOfInstantiation. In that case, walk decl backwards
5366 // until a valid name loc is found.
5367 SourceLocation PrevDiagLoc = PointOfInstantiation;
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005368 for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid();
5369 Prev = Prev->getPreviousDecl()) {
Nico Weberd1d512a2012-01-09 19:52:25 +00005370 PrevDiagLoc = Prev->getLocation();
5371 }
5372 assert(PrevDiagLoc.isValid() &&
5373 "Explicit instantiation without point of instantiation?");
5374 return PrevDiagLoc;
5375}
5376
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005377/// \brief Diagnose cases where we have an explicit template specialization
Douglas Gregor454885e2009-10-15 15:54:05 +00005378/// before/after an explicit template instantiation, producing diagnostics
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005379/// for those cases where they are required and determining whether the
Douglas Gregor454885e2009-10-15 15:54:05 +00005380/// new specialization/instantiation will have any effect.
5381///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005382/// \param NewLoc the location of the new explicit specialization or
Douglas Gregor454885e2009-10-15 15:54:05 +00005383/// instantiation.
5384///
5385/// \param NewTSK the kind of the new explicit specialization or instantiation.
5386///
5387/// \param PrevDecl the previous declaration of the entity.
5388///
5389/// \param PrevTSK the kind of the old explicit specialization or instantiatin.
5390///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005391/// \param PrevPointOfInstantiation if valid, indicates where the previus
Douglas Gregor454885e2009-10-15 15:54:05 +00005392/// declaration was instantiated (either implicitly or explicitly).
5393///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005394/// \param HasNoEffect will be set to true to indicate that the new
Douglas Gregor454885e2009-10-15 15:54:05 +00005395/// specialization or instantiation has no effect and should be ignored.
5396///
5397/// \returns true if there was an error that should prevent the introduction of
5398/// the new declaration into the AST, false otherwise.
Douglas Gregor0d035142009-10-27 18:42:08 +00005399bool
5400Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc,
5401 TemplateSpecializationKind NewTSK,
5402 NamedDecl *PrevDecl,
5403 TemplateSpecializationKind PrevTSK,
5404 SourceLocation PrevPointOfInstantiation,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005405 bool &HasNoEffect) {
5406 HasNoEffect = false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005407
Douglas Gregor454885e2009-10-15 15:54:05 +00005408 switch (NewTSK) {
5409 case TSK_Undeclared:
5410 case TSK_ImplicitInstantiation:
David Blaikieb219cfc2011-09-23 05:06:16 +00005411 llvm_unreachable("Don't check implicit instantiations here");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005412
Douglas Gregor454885e2009-10-15 15:54:05 +00005413 case TSK_ExplicitSpecialization:
5414 switch (PrevTSK) {
5415 case TSK_Undeclared:
5416 case TSK_ExplicitSpecialization:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005417 // Okay, we're just specializing something that is either already
Douglas Gregor454885e2009-10-15 15:54:05 +00005418 // explicitly specialized or has merely been mentioned without any
5419 // instantiation.
5420 return false;
5421
5422 case TSK_ImplicitInstantiation:
5423 if (PrevPointOfInstantiation.isInvalid()) {
5424 // The declaration itself has not actually been instantiated, so it is
5425 // still okay to specialize it.
John McCall75042392010-02-11 01:33:53 +00005426 StripImplicitInstantiation(PrevDecl);
Douglas Gregor454885e2009-10-15 15:54:05 +00005427 return false;
5428 }
5429 // Fall through
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005430
Douglas Gregor454885e2009-10-15 15:54:05 +00005431 case TSK_ExplicitInstantiationDeclaration:
5432 case TSK_ExplicitInstantiationDefinition:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005433 assert((PrevTSK == TSK_ImplicitInstantiation ||
5434 PrevPointOfInstantiation.isValid()) &&
Douglas Gregor454885e2009-10-15 15:54:05 +00005435 "Explicit instantiation without point of instantiation?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005436
Douglas Gregor454885e2009-10-15 15:54:05 +00005437 // C++ [temp.expl.spec]p6:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005438 // If a template, a member template or the member of a class template
Douglas Gregor454885e2009-10-15 15:54:05 +00005439 // is explicitly specialized then that specialization shall be declared
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005440 // before the first use of that specialization that would cause an
Douglas Gregor454885e2009-10-15 15:54:05 +00005441 // implicit instantiation to take place, in every translation unit in
5442 // which such a use occurs; no diagnostic is required.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005443 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Douglas Gregordc0a11c2010-02-26 06:03:23 +00005444 // Is there any previous explicit specialization declaration?
5445 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
5446 return false;
5447 }
5448
Douglas Gregor0d035142009-10-27 18:42:08 +00005449 Diag(NewLoc, diag::err_specialization_after_instantiation)
Douglas Gregor454885e2009-10-15 15:54:05 +00005450 << PrevDecl;
Douglas Gregor0d035142009-10-27 18:42:08 +00005451 Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)
Douglas Gregor454885e2009-10-15 15:54:05 +00005452 << (PrevTSK != TSK_ImplicitInstantiation);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005453
Douglas Gregor454885e2009-10-15 15:54:05 +00005454 return true;
5455 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005456
Douglas Gregor454885e2009-10-15 15:54:05 +00005457 case TSK_ExplicitInstantiationDeclaration:
5458 switch (PrevTSK) {
5459 case TSK_ExplicitInstantiationDeclaration:
5460 // This explicit instantiation declaration is redundant (that's okay).
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005461 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005462 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005463
Douglas Gregor454885e2009-10-15 15:54:05 +00005464 case TSK_Undeclared:
5465 case TSK_ImplicitInstantiation:
5466 // We're explicitly instantiating something that may have already been
5467 // implicitly instantiated; that's fine.
5468 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005469
Douglas Gregor454885e2009-10-15 15:54:05 +00005470 case TSK_ExplicitSpecialization:
5471 // C++0x [temp.explicit]p4:
5472 // For a given set of template parameters, if an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005473 // of a template appears after a declaration of an explicit
Douglas Gregor454885e2009-10-15 15:54:05 +00005474 // specialization for that template, the explicit instantiation has no
5475 // effect.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005476 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005477 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005478
Douglas Gregor454885e2009-10-15 15:54:05 +00005479 case TSK_ExplicitInstantiationDefinition:
5480 // C++0x [temp.explicit]p10:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005481 // If an entity is the subject of both an explicit instantiation
5482 // declaration and an explicit instantiation definition in the same
Douglas Gregor454885e2009-10-15 15:54:05 +00005483 // translation unit, the definition shall follow the declaration.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005484 Diag(NewLoc,
Douglas Gregor0d035142009-10-27 18:42:08 +00005485 diag::err_explicit_instantiation_declaration_after_definition);
Nico Weberff91d242011-12-23 20:58:04 +00005486
5487 // Explicit instantiations following a specialization have no effect and
5488 // hence no PrevPointOfInstantiation. In that case, walk decl backwards
5489 // until a valid name loc is found.
Nico Weberd1d512a2012-01-09 19:52:25 +00005490 Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
5491 diag::note_explicit_instantiation_definition_here);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005492 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005493 return false;
5494 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005495
Douglas Gregor454885e2009-10-15 15:54:05 +00005496 case TSK_ExplicitInstantiationDefinition:
5497 switch (PrevTSK) {
5498 case TSK_Undeclared:
5499 case TSK_ImplicitInstantiation:
5500 // We're explicitly instantiating something that may have already been
5501 // implicitly instantiated; that's fine.
5502 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005503
Douglas Gregor454885e2009-10-15 15:54:05 +00005504 case TSK_ExplicitSpecialization:
5505 // C++ DR 259, C++0x [temp.explicit]p4:
5506 // For a given set of template parameters, if an explicit
5507 // instantiation of a template appears after a declaration of
5508 // an explicit specialization for that template, the explicit
5509 // instantiation has no effect.
5510 //
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005511 // In C++98/03 mode, we only give an extension warning here, because it
Douglas Gregorc42b6522010-04-09 21:02:29 +00005512 // is not harmful to try to explicitly instantiate something that
Douglas Gregor454885e2009-10-15 15:54:05 +00005513 // has been explicitly specialized.
Richard Smithebaf0e62011-10-18 20:49:44 +00005514 Diag(NewLoc, getLangOptions().CPlusPlus0x ?
5515 diag::warn_cxx98_compat_explicit_instantiation_after_specialization :
5516 diag::ext_explicit_instantiation_after_specialization)
5517 << PrevDecl;
5518 Diag(PrevDecl->getLocation(),
5519 diag::note_previous_template_specialization);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005520 HasNoEffect = true;
Douglas Gregor454885e2009-10-15 15:54:05 +00005521 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005522
Douglas Gregor454885e2009-10-15 15:54:05 +00005523 case TSK_ExplicitInstantiationDeclaration:
5524 // We're explicity instantiating a definition for something for which we
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005525 // were previously asked to suppress instantiations. That's fine.
Nico Weberff91d242011-12-23 20:58:04 +00005526
5527 // C++0x [temp.explicit]p4:
5528 // For a given set of template parameters, if an explicit instantiation
5529 // of a template appears after a declaration of an explicit
5530 // specialization for that template, the explicit instantiation has no
5531 // effect.
Douglas Gregorf785a7d2012-01-14 15:55:47 +00005532 for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) {
Nico Weberff91d242011-12-23 20:58:04 +00005533 // Is there any previous explicit specialization declaration?
5534 if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
5535 HasNoEffect = true;
5536 break;
5537 }
5538 }
5539
Douglas Gregor454885e2009-10-15 15:54:05 +00005540 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005541
Douglas Gregor454885e2009-10-15 15:54:05 +00005542 case TSK_ExplicitInstantiationDefinition:
5543 // C++0x [temp.spec]p5:
5544 // For a given template and a given set of template-arguments,
5545 // - an explicit instantiation definition shall appear at most once
5546 // in a program,
Douglas Gregor0d035142009-10-27 18:42:08 +00005547 Diag(NewLoc, diag::err_explicit_instantiation_duplicate)
Douglas Gregor454885e2009-10-15 15:54:05 +00005548 << PrevDecl;
Nico Weberd1d512a2012-01-09 19:52:25 +00005549 Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation),
Douglas Gregor0d035142009-10-27 18:42:08 +00005550 diag::note_previous_explicit_instantiation);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005551 HasNoEffect = true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005552 return false;
Douglas Gregor454885e2009-10-15 15:54:05 +00005553 }
Douglas Gregor454885e2009-10-15 15:54:05 +00005554 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005555
David Blaikieb219cfc2011-09-23 05:06:16 +00005556 llvm_unreachable("Missing specialization/instantiation case?");
Douglas Gregor454885e2009-10-15 15:54:05 +00005557}
5558
John McCallaf2094e2010-04-08 09:05:18 +00005559/// \brief Perform semantic analysis for the given dependent function
5560/// template specialization. The only possible way to get a dependent
5561/// function template specialization is with a friend declaration,
5562/// like so:
5563///
5564/// template <class T> void foo(T);
5565/// template <class T> class A {
5566/// friend void foo<>(T);
5567/// };
5568///
5569/// There really isn't any useful analysis we can do here, so we
5570/// just store the information.
5571bool
5572Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD,
5573 const TemplateArgumentListInfo &ExplicitTemplateArgs,
5574 LookupResult &Previous) {
5575 // Remove anything from Previous that isn't a function template in
5576 // the correct context.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005577 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCallaf2094e2010-04-08 09:05:18 +00005578 LookupResult::Filter F = Previous.makeFilter();
5579 while (F.hasNext()) {
5580 NamedDecl *D = F.next()->getUnderlyingDecl();
5581 if (!isa<FunctionTemplateDecl>(D) ||
Sebastian Redl7a126a42010-08-31 00:36:30 +00005582 !FDLookupContext->InEnclosingNamespaceSetOf(
5583 D->getDeclContext()->getRedeclContext()))
John McCallaf2094e2010-04-08 09:05:18 +00005584 F.erase();
5585 }
5586 F.done();
5587
5588 // Should this be diagnosed here?
5589 if (Previous.empty()) return true;
5590
5591 FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(),
5592 ExplicitTemplateArgs);
5593 return false;
5594}
5595
Abramo Bagnarae03db982010-05-20 15:32:11 +00005596/// \brief Perform semantic analysis for the given function template
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005597/// specialization.
5598///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005599/// This routine performs all of the semantic analysis required for an
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005600/// explicit function template specialization. On successful completion,
5601/// the function declaration \p FD will become a function template
5602/// specialization.
5603///
5604/// \param FD the function declaration, which will be updated to become a
5605/// function template specialization.
5606///
Abramo Bagnarae03db982010-05-20 15:32:11 +00005607/// \param ExplicitTemplateArgs the explicitly-provided template arguments,
5608/// if any. Note that this may be valid info even when 0 arguments are
5609/// explicitly provided as in, e.g., \c void sort<>(char*, char*);
5610/// as it anyway contains info on the angle brackets locations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005611///
Francois Pichet59e7c562011-07-08 06:21:47 +00005612/// \param Previous the set of declarations that may be specialized by
Abramo Bagnarae03db982010-05-20 15:32:11 +00005613/// this function specialization.
5614bool
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005615Sema::CheckFunctionTemplateSpecialization(FunctionDecl *FD,
Douglas Gregor67714232011-03-03 02:41:12 +00005616 TemplateArgumentListInfo *ExplicitTemplateArgs,
John McCall68263142009-11-18 22:49:29 +00005617 LookupResult &Previous) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005618 // The set of function template specializations that could match this
5619 // explicit function template specialization.
John McCallc373d482010-01-27 01:50:18 +00005620 UnresolvedSet<8> Candidates;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005621
Sebastian Redl7a126a42010-08-31 00:36:30 +00005622 DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext();
John McCall68263142009-11-18 22:49:29 +00005623 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5624 I != E; ++I) {
5625 NamedDecl *Ovl = (*I)->getUnderlyingDecl();
5626 if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005627 // Only consider templates found within the same semantic lookup scope as
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005628 // FD.
Sebastian Redl7a126a42010-08-31 00:36:30 +00005629 if (!FDLookupContext->InEnclosingNamespaceSetOf(
5630 Ovl->getDeclContext()->getRedeclContext()))
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005631 continue;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005632
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005633 // C++ [temp.expl.spec]p11:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005634 // A trailing template-argument can be left unspecified in the
5635 // template-id naming an explicit function template specialization
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005636 // provided it can be deduced from the function argument type.
5637 // Perform template argument deduction to determine whether we may be
5638 // specializing this template.
5639 // FIXME: It is somewhat wasteful to build
John McCall5769d612010-02-08 23:07:23 +00005640 TemplateDeductionInfo Info(Context, FD->getLocation());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005641 FunctionDecl *Specialization = 0;
5642 if (TemplateDeductionResult TDK
John McCalld5532b62009-11-23 01:53:49 +00005643 = DeduceTemplateArguments(FunTmpl, ExplicitTemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005644 FD->getType(),
5645 Specialization,
5646 Info)) {
5647 // FIXME: Template argument deduction failed; record why it failed, so
5648 // that we can provide nifty diagnostics.
5649 (void)TDK;
5650 continue;
5651 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005652
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005653 // Record this candidate.
John McCallc373d482010-01-27 01:50:18 +00005654 Candidates.addDecl(Specialization, I.getAccess());
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005655 }
5656 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005657
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005658 // Find the most specialized function template.
John McCallc373d482010-01-27 01:50:18 +00005659 UnresolvedSetIterator Result
5660 = getMostSpecialized(Candidates.begin(), Candidates.end(),
Douglas Gregor5c7bf422011-01-11 17:34:58 +00005661 TPOC_Other, 0, FD->getLocation(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005662 PDiag(diag::err_function_template_spec_no_match)
Douglas Gregorc5df30f2009-09-26 03:41:46 +00005663 << FD->getDeclName(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005664 PDiag(diag::err_function_template_spec_ambiguous)
John McCalld5532b62009-11-23 01:53:49 +00005665 << FD->getDeclName() << (ExplicitTemplateArgs != 0),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00005666 PDiag(diag::note_function_template_spec_matched));
John McCallc373d482010-01-27 01:50:18 +00005667 if (Result == Candidates.end())
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005668 return true;
John McCallc373d482010-01-27 01:50:18 +00005669
5670 // Ignore access information; it doesn't figure into redeclaration checking.
5671 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
Abramo Bagnaraabfb4052011-03-04 17:20:30 +00005672
5673 FunctionTemplateSpecializationInfo *SpecInfo
5674 = Specialization->getTemplateSpecializationInfo();
5675 assert(SpecInfo && "Function template specialization info missing?");
Francois Pichet59e7c562011-07-08 06:21:47 +00005676
5677 // Note: do not overwrite location info if previous template
5678 // specialization kind was explicit.
5679 TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind();
5680 if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation)
5681 Specialization->setLocation(FD->getLocation());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005682
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005683 // FIXME: Check if the prior specialization has a point of instantiation.
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005684 // If so, we have run afoul of .
John McCall7ad650f2010-03-24 07:46:06 +00005685
5686 // If this is a friend declaration, then we're not really declaring
5687 // an explicit specialization.
5688 bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005689
Douglas Gregord5cb8762009-10-07 00:13:32 +00005690 // Check the scope of this explicit specialization.
John McCall7ad650f2010-03-24 07:46:06 +00005691 if (!isFriend &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005692 CheckTemplateSpecializationScope(*this,
Douglas Gregord5cb8762009-10-07 00:13:32 +00005693 Specialization->getPrimaryTemplate(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005694 Specialization, FD->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005695 false))
Douglas Gregord5cb8762009-10-07 00:13:32 +00005696 return true;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005697
5698 // C++ [temp.expl.spec]p6:
5699 // If a template, a member template or the member of a class template is
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005700 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005701 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005702 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005703 // use occurs; no diagnostic is required.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005704 bool HasNoEffect = false;
John McCall7ad650f2010-03-24 07:46:06 +00005705 if (!isFriend &&
5706 CheckSpecializationInstantiationRedecl(FD->getLocation(),
John McCall75042392010-02-11 01:33:53 +00005707 TSK_ExplicitSpecialization,
5708 Specialization,
5709 SpecInfo->getTemplateSpecializationKind(),
5710 SpecInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005711 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005712 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00005713
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005714 // Mark the prior declaration as an explicit specialization, so that later
5715 // clients know that this is an explicit specialization.
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005716 if (!isFriend) {
John McCall7ad650f2010-03-24 07:46:06 +00005717 SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005718 MarkUnusedFileScopedDecl(Specialization);
5719 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005720
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005721 // Turn the given function declaration into a function template
5722 // specialization, with the template arguments from the previous
5723 // specialization.
Abramo Bagnarae03db982010-05-20 15:32:11 +00005724 // Take copies of (semantic and syntactic) template argument lists.
5725 const TemplateArgumentList* TemplArgs = new (Context)
5726 TemplateArgumentList(Specialization->getTemplateSpecializationArgs());
Douglas Gregor838db382010-02-11 01:19:42 +00005727 FD->setFunctionTemplateSpecialization(Specialization->getPrimaryTemplate(),
Abramo Bagnarae03db982010-05-20 15:32:11 +00005728 TemplArgs, /*InsertPos=*/0,
5729 SpecInfo->getTemplateSpecializationKind(),
Argyrios Kyrtzidis71a76052011-09-22 20:07:09 +00005730 ExplicitTemplateArgs);
Douglas Gregore885e182011-05-21 18:53:30 +00005731 FD->setStorageClass(Specialization->getStorageClass());
5732
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005733 // The "previous declaration" for this function template specialization is
5734 // the prior function template specialization.
John McCall68263142009-11-18 22:49:29 +00005735 Previous.clear();
5736 Previous.addDecl(Specialization);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00005737 return false;
5738}
5739
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005740/// \brief Perform semantic analysis for the given non-template member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005741/// specialization.
5742///
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005743/// This routine performs all of the semantic analysis required for an
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005744/// explicit member function specialization. On successful completion,
5745/// the function declaration \p FD will become a member function
5746/// specialization.
5747///
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005748/// \param Member the member declaration, which will be updated to become a
5749/// specialization.
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005750///
John McCall68263142009-11-18 22:49:29 +00005751/// \param Previous the set of declarations, one of which may be specialized
5752/// by this function specialization; the set will be modified to contain the
5753/// redeclared member.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005754bool
John McCall68263142009-11-18 22:49:29 +00005755Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005756 assert(!isa<TemplateDecl>(Member) && "Only for non-template members");
John McCall77e8b112010-04-13 20:37:33 +00005757
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005758 // Try to find the member we are instantiating.
5759 NamedDecl *Instantiation = 0;
5760 NamedDecl *InstantiatedFrom = 0;
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005761 MemberSpecializationInfo *MSInfo = 0;
5762
John McCall68263142009-11-18 22:49:29 +00005763 if (Previous.empty()) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005764 // Nowhere to look anyway.
5765 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005766 for (LookupResult::iterator I = Previous.begin(), E = Previous.end();
5767 I != E; ++I) {
5768 NamedDecl *D = (*I)->getUnderlyingDecl();
5769 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005770 if (Context.hasSameType(Function->getType(), Method->getType())) {
5771 Instantiation = Method;
5772 InstantiatedFrom = Method->getInstantiatedFromMemberFunction();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005773 MSInfo = Method->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005774 break;
5775 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005776 }
5777 }
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005778 } else if (isa<VarDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005779 VarDecl *PrevVar;
5780 if (Previous.isSingleResult() &&
5781 (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl())))
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005782 if (PrevVar->isStaticDataMember()) {
John McCall68263142009-11-18 22:49:29 +00005783 Instantiation = PrevVar;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005784 InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005785 MSInfo = PrevVar->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005786 }
5787 } else if (isa<RecordDecl>(Member)) {
John McCall68263142009-11-18 22:49:29 +00005788 CXXRecordDecl *PrevRecord;
5789 if (Previous.isSingleResult() &&
5790 (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) {
5791 Instantiation = PrevRecord;
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005792 InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass();
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005793 MSInfo = PrevRecord->getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005794 }
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005795 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005796
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005797 if (!Instantiation) {
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005798 // There is no previous declaration that matches. Since member
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005799 // specializations are always out-of-line, the caller will complain about
5800 // this mismatch later.
5801 return false;
5802 }
John McCall77e8b112010-04-13 20:37:33 +00005803
5804 // If this is a friend, just bail out here before we start turning
5805 // things into explicit specializations.
5806 if (Member->getFriendObjectKind() != Decl::FOK_None) {
5807 // Preserve instantiation information.
5808 if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) {
5809 cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction(
5810 cast<CXXMethodDecl>(InstantiatedFrom),
5811 cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind());
5812 } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) {
5813 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
5814 cast<CXXRecordDecl>(InstantiatedFrom),
5815 cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind());
5816 }
5817
5818 Previous.clear();
5819 Previous.addDecl(Instantiation);
5820 return false;
5821 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005822
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005823 // Make sure that this is a specialization of a member.
5824 if (!InstantiatedFrom) {
5825 Diag(Member->getLocation(), diag::err_spec_member_not_instantiated)
5826 << Member;
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005827 Diag(Instantiation->getLocation(), diag::note_specialized_decl);
5828 return true;
5829 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005830
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005831 // C++ [temp.expl.spec]p6:
5832 // If a template, a member template or the member of a class template is
Nico Weberff91d242011-12-23 20:58:04 +00005833 // explicitly specialized then that specialization shall be declared
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005834 // before the first use of that specialization that would cause an implicit
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005835 // instantiation to take place, in every translation unit in which such a
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005836 // use occurs; no diagnostic is required.
5837 assert(MSInfo && "Member specialization info missing?");
John McCall75042392010-02-11 01:33:53 +00005838
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005839 bool HasNoEffect = false;
John McCall75042392010-02-11 01:33:53 +00005840 if (CheckSpecializationInstantiationRedecl(Member->getLocation(),
5841 TSK_ExplicitSpecialization,
5842 Instantiation,
5843 MSInfo->getTemplateSpecializationKind(),
5844 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00005845 HasNoEffect))
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00005846 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005847
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005848 // Check the scope of this explicit specialization.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005849 if (CheckTemplateSpecializationScope(*this,
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005850 InstantiatedFrom,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005851 Instantiation, Member->getLocation(),
Douglas Gregor9302da62009-10-14 23:50:59 +00005852 false))
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005853 return true;
Douglas Gregor2db32322009-10-07 23:56:10 +00005854
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005855 // Note that this is an explicit instantiation of a member.
Douglas Gregorf6b11852009-10-08 15:14:33 +00005856 // the original declaration to note that it is an explicit specialization
5857 // (if it was previously an implicit instantiation). This latter step
5858 // makes bookkeeping easier.
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005859 if (isa<FunctionDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005860 FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation);
5861 if (InstantiationFunction->getTemplateSpecializationKind() ==
5862 TSK_ImplicitInstantiation) {
5863 InstantiationFunction->setTemplateSpecializationKind(
5864 TSK_ExplicitSpecialization);
5865 InstantiationFunction->setLocation(Member->getLocation());
5866 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005867
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005868 cast<FunctionDecl>(Member)->setInstantiationOfMemberFunction(
5869 cast<CXXMethodDecl>(InstantiatedFrom),
5870 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005871 MarkUnusedFileScopedDecl(InstantiationFunction);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005872 } else if (isa<VarDecl>(Member)) {
Douglas Gregorf6b11852009-10-08 15:14:33 +00005873 VarDecl *InstantiationVar = cast<VarDecl>(Instantiation);
5874 if (InstantiationVar->getTemplateSpecializationKind() ==
5875 TSK_ImplicitInstantiation) {
5876 InstantiationVar->setTemplateSpecializationKind(
5877 TSK_ExplicitSpecialization);
5878 InstantiationVar->setLocation(Member->getLocation());
5879 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005880
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005881 Context.setInstantiatedFromStaticDataMember(cast<VarDecl>(Member),
5882 cast<VarDecl>(InstantiatedFrom),
5883 TSK_ExplicitSpecialization);
Argyrios Kyrtzidisbbc64542010-08-15 01:15:20 +00005884 MarkUnusedFileScopedDecl(InstantiationVar);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005885 } else {
5886 assert(isa<CXXRecordDecl>(Member) && "Only member classes remain");
Douglas Gregorf6b11852009-10-08 15:14:33 +00005887 CXXRecordDecl *InstantiationClass = cast<CXXRecordDecl>(Instantiation);
5888 if (InstantiationClass->getTemplateSpecializationKind() ==
5889 TSK_ImplicitInstantiation) {
5890 InstantiationClass->setTemplateSpecializationKind(
5891 TSK_ExplicitSpecialization);
5892 InstantiationClass->setLocation(Member->getLocation());
5893 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005894
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005895 cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass(
Douglas Gregorf6b11852009-10-08 15:14:33 +00005896 cast<CXXRecordDecl>(InstantiatedFrom),
5897 TSK_ExplicitSpecialization);
Douglas Gregor251b4ff2009-10-08 07:24:58 +00005898 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005899
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005900 // Save the caller the trouble of having to figure out which declaration
5901 // this specialization matches.
John McCall68263142009-11-18 22:49:29 +00005902 Previous.clear();
5903 Previous.addDecl(Instantiation);
Douglas Gregor1fef4e62009-10-07 22:35:40 +00005904 return false;
5905}
5906
Douglas Gregor558c0322009-10-14 23:41:34 +00005907/// \brief Check the scope of an explicit instantiation.
Douglas Gregor669eed82010-07-13 00:10:04 +00005908///
5909/// \returns true if a serious error occurs, false otherwise.
5910static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D,
Douglas Gregor558c0322009-10-14 23:41:34 +00005911 SourceLocation InstLoc,
5912 bool WasQualifiedName) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00005913 DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext();
5914 DeclContext *CurContext = S.CurContext->getRedeclContext();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005915
Douglas Gregor669eed82010-07-13 00:10:04 +00005916 if (CurContext->isRecord()) {
5917 S.Diag(InstLoc, diag::err_explicit_instantiation_in_class)
5918 << D;
5919 return true;
5920 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005921
Richard Smith3e2e91e2011-10-18 02:28:33 +00005922 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005923 // An explicit instantiation shall appear in an enclosing namespace of its
Richard Smith3e2e91e2011-10-18 02:28:33 +00005924 // template. If the name declared in the explicit instantiation is an
5925 // unqualified name, the explicit instantiation shall appear in the
5926 // namespace where its template is declared or, if that namespace is inline
5927 // (7.3.1), any namespace from its enclosing namespace set.
Douglas Gregor558c0322009-10-14 23:41:34 +00005928 //
5929 // This is DR275, which we do not retroactively apply to C++98/03.
Richard Smith3e2e91e2011-10-18 02:28:33 +00005930 if (WasQualifiedName) {
5931 if (CurContext->Encloses(OrigContext))
5932 return false;
5933 } else {
5934 if (CurContext->InEnclosingNamespaceSetOf(OrigContext))
5935 return false;
5936 }
5937
5938 if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) {
5939 if (WasQualifiedName)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005940 S.Diag(InstLoc,
5941 S.getLangOptions().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005942 diag::err_explicit_instantiation_out_of_scope :
5943 diag::warn_explicit_instantiation_out_of_scope_0x)
Douglas Gregor558c0322009-10-14 23:41:34 +00005944 << D << NS;
5945 else
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005946 S.Diag(InstLoc,
Douglas Gregor2166beb2010-05-11 17:39:34 +00005947 S.getLangOptions().CPlusPlus0x?
Richard Smith3e2e91e2011-10-18 02:28:33 +00005948 diag::err_explicit_instantiation_unqualified_wrong_namespace :
5949 diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x)
5950 << D << NS;
5951 } else
5952 S.Diag(InstLoc,
5953 S.getLangOptions().CPlusPlus0x?
5954 diag::err_explicit_instantiation_must_be_global :
5955 diag::warn_explicit_instantiation_must_be_global_0x)
5956 << D;
Douglas Gregor558c0322009-10-14 23:41:34 +00005957 S.Diag(D->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregor669eed82010-07-13 00:10:04 +00005958 return false;
Douglas Gregor558c0322009-10-14 23:41:34 +00005959}
5960
5961/// \brief Determine whether the given scope specifier has a template-id in it.
5962static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) {
5963 if (!SS.isSet())
5964 return false;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005965
Richard Smith3e2e91e2011-10-18 02:28:33 +00005966 // C++11 [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00005967 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00005968 // or a static data member of a class template specialization, the name of
5969 // the class template specialization in the qualified-id for the member
5970 // name shall be a simple-template-id.
5971 //
5972 // C++98 has the same restriction, just worded differently.
5973 for (NestedNameSpecifier *NNS = (NestedNameSpecifier *)SS.getScopeRep();
5974 NNS; NNS = NNS->getPrefix())
John McCallf4c73712011-01-19 06:33:43 +00005975 if (const Type *T = NNS->getAsType())
Douglas Gregor558c0322009-10-14 23:41:34 +00005976 if (isa<TemplateSpecializationType>(T))
5977 return true;
5978
5979 return false;
5980}
5981
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00005982// Explicit instantiation of a class template specialization
John McCallf312b1e2010-08-26 23:41:50 +00005983DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00005984Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00005985 SourceLocation ExternLoc,
5986 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00005987 unsigned TagSpec,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005988 SourceLocation KWLoc,
5989 const CXXScopeSpec &SS,
5990 TemplateTy TemplateD,
5991 SourceLocation TemplateNameLoc,
5992 SourceLocation LAngleLoc,
5993 ASTTemplateArgsPtr TemplateArgsIn,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005994 SourceLocation RAngleLoc,
5995 AttributeList *Attr) {
5996 // Find the class template we're specializing
5997 TemplateName Name = TemplateD.getAsVal<TemplateName>();
Mike Stump1eb44332009-09-09 15:08:12 +00005998 ClassTemplateDecl *ClassTemplate
Douglas Gregor93dfdb12009-05-13 00:25:59 +00005999 = cast<ClassTemplateDecl>(Name.getAsTemplateDecl());
6000
6001 // Check that the specialization uses the same tag kind as the
6002 // original template.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006003 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
6004 assert(Kind != TTK_Enum &&
6005 "Invalid enum tag in class template explicit instantiation!");
Douglas Gregor501c5ce2009-05-14 16:41:31 +00006006 if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(),
Richard Trieubbf34c02011-06-10 03:11:26 +00006007 Kind, /*isDefinition*/false, KWLoc,
Douglas Gregor501c5ce2009-05-14 16:41:31 +00006008 *ClassTemplate->getIdentifier())) {
Mike Stump1eb44332009-09-09 15:08:12 +00006009 Diag(KWLoc, diag::err_use_with_wrong_tag)
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006010 << ClassTemplate
Douglas Gregor849b2432010-03-31 17:46:05 +00006011 << FixItHint::CreateReplacement(KWLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006012 ClassTemplate->getTemplatedDecl()->getKindName());
Mike Stump1eb44332009-09-09 15:08:12 +00006013 Diag(ClassTemplate->getTemplatedDecl()->getLocation(),
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006014 diag::note_previous_use);
6015 Kind = ClassTemplate->getTemplatedDecl()->getTagKind();
6016 }
6017
Douglas Gregor558c0322009-10-14 23:41:34 +00006018 // C++0x [temp.explicit]p2:
6019 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006020 // definition and an explicit instantiation declaration. An explicit
6021 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5cb8762009-10-07 00:13:32 +00006022 TemplateSpecializationKind TSK
6023 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6024 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006025
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006026 // Translate the parser's template argument list in our AST format.
John McCalld5532b62009-11-23 01:53:49 +00006027 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
Douglas Gregor314b97f2009-11-10 19:49:08 +00006028 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006029
6030 // Check that the template argument list is well-formed for this
6031 // template.
Chris Lattner5f9e2722011-07-23 10:55:15 +00006032 SmallVector<TemplateArgument, 4> Converted;
John McCalld5532b62009-11-23 01:53:49 +00006033 if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc,
6034 TemplateArgs, false, Converted))
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006035 return true;
6036
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006037 // Find the class template specialization declaration that
6038 // corresponds to these arguments.
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006039 void *InsertPos = 0;
6040 ClassTemplateSpecializationDecl *PrevDecl
Douglas Gregor910f8002010-11-07 23:05:16 +00006041 = ClassTemplate->findSpecialization(Converted.data(),
6042 Converted.size(), InsertPos);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006043
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006044 TemplateSpecializationKind PrevDecl_TSK
6045 = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared;
6046
Douglas Gregord5cb8762009-10-07 00:13:32 +00006047 // C++0x [temp.explicit]p2:
6048 // [...] An explicit instantiation shall appear in an enclosing
6049 // namespace of its template. [...]
6050 //
6051 // This is C++ DR 275.
Douglas Gregor669eed82010-07-13 00:10:04 +00006052 if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc,
6053 SS.isSet()))
6054 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006055
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006056 ClassTemplateSpecializationDecl *Specialization = 0;
6057
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006058 bool HasNoEffect = false;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006059 if (PrevDecl) {
Douglas Gregor0d035142009-10-27 18:42:08 +00006060 if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK,
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006061 PrevDecl, PrevDecl_TSK,
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006062 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006063 HasNoEffect))
John McCalld226f652010-08-21 09:40:31 +00006064 return PrevDecl;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006065
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006066 // Even though HasNoEffect == true means that this explicit instantiation
6067 // has no effect on semantics, we go on to put its syntax in the AST.
6068
6069 if (PrevDecl_TSK == TSK_ImplicitInstantiation ||
6070 PrevDecl_TSK == TSK_Undeclared) {
Douglas Gregor52604ab2009-09-11 21:19:12 +00006071 // Since the only prior class template specialization with these
6072 // arguments was referenced but not declared, reuse that
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006073 // declaration node as our own, updating the source location
6074 // for the template name to reflect our new declaration.
6075 // (Other source locations will be updated later.)
Douglas Gregor52604ab2009-09-11 21:19:12 +00006076 Specialization = PrevDecl;
6077 Specialization->setLocation(TemplateNameLoc);
6078 PrevDecl = 0;
6079 }
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006080 }
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006081
Douglas Gregor52604ab2009-09-11 21:19:12 +00006082 if (!Specialization) {
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006083 // Create a new class template specialization declaration node for
6084 // this explicit specialization.
6085 Specialization
Douglas Gregor13c85772010-05-06 00:28:52 +00006086 = ClassTemplateSpecializationDecl::Create(Context, Kind,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006087 ClassTemplate->getDeclContext(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00006088 KWLoc, TemplateNameLoc,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006089 ClassTemplate,
Douglas Gregor910f8002010-11-07 23:05:16 +00006090 Converted.data(),
6091 Converted.size(),
6092 PrevDecl);
John McCallb6217662010-03-15 10:12:16 +00006093 SetNestedNameSpecifier(Specialization, SS);
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006094
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00006095 if (!HasNoEffect && !PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006096 // Insert the new specialization.
Argyrios Kyrtzidiscc0b1bc2010-07-20 13:59:28 +00006097 ClassTemplate->AddSpecialization(Specialization, InsertPos);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006098 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006099 }
6100
6101 // Build the fully-sugared type for this explicit instantiation as
6102 // the user wrote in the explicit instantiation itself. This means
6103 // that we'll pretty-print the type retrieved from the
6104 // specialization's declaration the way that the user actually wrote
6105 // the explicit instantiation, rather than formatting the name based
6106 // on the "canonical" representation used to store the template
6107 // arguments in the specialization.
John McCall3cb0ebd2010-03-10 03:28:59 +00006108 TypeSourceInfo *WrittenTy
6109 = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc,
6110 TemplateArgs,
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006111 Context.getTypeDeclType(Specialization));
6112 Specialization->setTypeAsWritten(WrittenTy);
6113 TemplateArgsIn.release();
6114
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006115 // Set source locations for keywords.
6116 Specialization->setExternLoc(ExternLoc);
6117 Specialization->setTemplateKeywordLoc(TemplateLoc);
6118
Rafael Espindola0257b7f2012-01-03 06:04:21 +00006119 if (Attr)
6120 ProcessDeclAttributeList(S, Specialization, Attr);
6121
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006122 // Add the explicit instantiation into its lexical context. However,
6123 // since explicit instantiations are never found by name lookup, we
6124 // just put it into the declaration context directly.
6125 Specialization->setLexicalDeclContext(CurContext);
6126 CurContext->addDecl(Specialization);
6127
6128 // Syntax is now OK, so return if it has no other effect on semantics.
6129 if (HasNoEffect) {
6130 // Set the template specialization kind.
6131 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006132 return Specialization;
Douglas Gregord78f5982009-11-25 06:01:46 +00006133 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006134
6135 // C++ [temp.explicit]p3:
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006136 // A definition of a class template or class member template
6137 // shall be in scope at the point of the explicit instantiation of
6138 // the class template or class member template.
6139 //
6140 // This check comes when we actually try to perform the
6141 // instantiation.
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006142 ClassTemplateSpecializationDecl *Def
6143 = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006144 Specialization->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006145 if (!Def)
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006146 InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006147 else if (TSK == TSK_ExplicitInstantiationDefinition) {
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006148 MarkVTableUsed(TemplateNameLoc, Specialization, true);
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006149 Specialization->setPointOfInstantiation(Def->getPointOfInstantiation());
6150 }
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006151
Douglas Gregor0d035142009-10-27 18:42:08 +00006152 // Instantiate the members of this class template specialization.
6153 Def = cast_or_null<ClassTemplateSpecializationDecl>(
Douglas Gregor952b0172010-02-11 01:04:33 +00006154 Specialization->getDefinition());
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006155 if (Def) {
Rafael Espindolaf075b222010-03-23 19:55:22 +00006156 TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind();
6157
6158 // Fix a TSK_ExplicitInstantiationDeclaration followed by a
6159 // TSK_ExplicitInstantiationDefinition
6160 if (Old_TSK == TSK_ExplicitInstantiationDeclaration &&
6161 TSK == TSK_ExplicitInstantiationDefinition)
6162 Def->setTemplateSpecializationKind(TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006163
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006164 InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK);
Rafael Espindolab0f65ca2010-03-22 23:12:48 +00006165 }
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006166
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006167 // Set the template specialization kind.
6168 Specialization->setTemplateSpecializationKind(TSK);
John McCalld226f652010-08-21 09:40:31 +00006169 return Specialization;
Douglas Gregor93dfdb12009-05-13 00:25:59 +00006170}
6171
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006172// Explicit instantiation of a member class of a class template.
John McCalld226f652010-08-21 09:40:31 +00006173DeclResult
Mike Stump1eb44332009-09-09 15:08:12 +00006174Sema::ActOnExplicitInstantiation(Scope *S,
Douglas Gregor45f96552009-09-04 06:33:52 +00006175 SourceLocation ExternLoc,
6176 SourceLocation TemplateLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00006177 unsigned TagSpec,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006178 SourceLocation KWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00006179 CXXScopeSpec &SS,
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006180 IdentifierInfo *Name,
6181 SourceLocation NameLoc,
6182 AttributeList *Attr) {
6183
Douglas Gregor402abb52009-05-28 23:31:59 +00006184 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00006185 bool IsDependent = false;
John McCallf312b1e2010-08-26 23:41:50 +00006186 Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference,
John McCalld226f652010-08-21 09:40:31 +00006187 KWLoc, SS, Name, NameLoc, Attr, AS_none,
Douglas Gregore7612302011-09-09 19:05:14 +00006188 /*ModulePrivateLoc=*/SourceLocation(),
John McCalld226f652010-08-21 09:40:31 +00006189 MultiTemplateParamsArg(*this, 0, 0),
Richard Smithbdad7a22012-01-10 01:33:14 +00006190 Owned, IsDependent, SourceLocation(), false,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00006191 TypeResult());
John McCallc4e70192009-09-11 04:59:25 +00006192 assert(!IsDependent && "explicit instantiation of dependent name not yet handled");
6193
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006194 if (!TagD)
6195 return true;
6196
John McCalld226f652010-08-21 09:40:31 +00006197 TagDecl *Tag = cast<TagDecl>(TagD);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006198 if (Tag->isEnum()) {
6199 Diag(TemplateLoc, diag::err_explicit_instantiation_enum)
6200 << Context.getTypeDeclType(Tag);
6201 return true;
6202 }
6203
Douglas Gregord0c87372009-05-27 17:30:49 +00006204 if (Tag->isInvalidDecl())
6205 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006206
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006207 CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag);
6208 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass();
6209 if (!Pattern) {
6210 Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type)
6211 << Context.getTypeDeclType(Record);
6212 Diag(Record->getLocation(), diag::note_nontemplate_decl_here);
6213 return true;
6214 }
6215
Douglas Gregor558c0322009-10-14 23:41:34 +00006216 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006217 // If the explicit instantiation is for a class or member class, the
6218 // elaborated-type-specifier in the declaration shall include a
Douglas Gregor558c0322009-10-14 23:41:34 +00006219 // simple-template-id.
6220 //
6221 // C++98 has the same restriction, just worded differently.
6222 if (!ScopeSpecifierHasTemplateId(SS))
Douglas Gregora2dd8282010-06-16 16:26:47 +00006223 Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006224 << Record << SS.getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006225
Douglas Gregor558c0322009-10-14 23:41:34 +00006226 // C++0x [temp.explicit]p2:
6227 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006228 // definition and an explicit instantiation declaration. An explicit
Douglas Gregor558c0322009-10-14 23:41:34 +00006229 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregora74bbe22009-10-14 21:46:58 +00006230 TemplateSpecializationKind TSK
6231 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6232 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006233
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006234 // C++0x [temp.explicit]p2:
6235 // [...] An explicit instantiation shall appear in an enclosing
6236 // namespace of its template. [...]
6237 //
6238 // This is C++ DR 275.
Douglas Gregor558c0322009-10-14 23:41:34 +00006239 CheckExplicitInstantiationScope(*this, Record, NameLoc, true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006240
Douglas Gregor454885e2009-10-15 15:54:05 +00006241 // Verify that it is okay to explicitly instantiate here.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006242 CXXRecordDecl *PrevDecl
Douglas Gregoref96ee02012-01-14 16:38:05 +00006243 = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl());
Douglas Gregor952b0172010-02-11 01:04:33 +00006244 if (!PrevDecl && Record->getDefinition())
Douglas Gregor583f33b2009-10-15 18:07:02 +00006245 PrevDecl = Record;
6246 if (PrevDecl) {
Douglas Gregor454885e2009-10-15 15:54:05 +00006247 MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo();
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006248 bool HasNoEffect = false;
Douglas Gregor454885e2009-10-15 15:54:05 +00006249 assert(MSInfo && "No member specialization information?");
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006250 if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK,
Douglas Gregor454885e2009-10-15 15:54:05 +00006251 PrevDecl,
6252 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006253 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006254 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006255 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006256 if (HasNoEffect)
Douglas Gregor454885e2009-10-15 15:54:05 +00006257 return TagD;
6258 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006259
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006260 CXXRecordDecl *RecordDef
Douglas Gregor952b0172010-02-11 01:04:33 +00006261 = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor89a5bea2009-10-15 22:53:21 +00006262 if (!RecordDef) {
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006263 // C++ [temp.explicit]p3:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006264 // A definition of a member class of a class template shall be in scope
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006265 // at the point of an explicit instantiation of the member class.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006266 CXXRecordDecl *Def
Douglas Gregor952b0172010-02-11 01:04:33 +00006267 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition());
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006268 if (!Def) {
Douglas Gregore2d3a3d2009-10-15 14:05:49 +00006269 Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member)
6270 << 0 << Record->getDeclName() << Record->getDeclContext();
Douglas Gregorbf7643e2009-10-15 12:53:22 +00006271 Diag(Pattern->getLocation(), diag::note_forward_declaration)
6272 << Pattern;
6273 return true;
Douglas Gregor0d035142009-10-27 18:42:08 +00006274 } else {
6275 if (InstantiateClass(NameLoc, Record, Def,
6276 getTemplateInstantiationArgs(Record),
6277 TSK))
6278 return true;
6279
Douglas Gregor952b0172010-02-11 01:04:33 +00006280 RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition());
Douglas Gregor0d035142009-10-27 18:42:08 +00006281 if (!RecordDef)
6282 return true;
6283 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006284 }
6285
Douglas Gregor0d035142009-10-27 18:42:08 +00006286 // Instantiate all of the members of the class.
6287 InstantiateClassMembers(NameLoc, RecordDef,
6288 getTemplateInstantiationArgs(Record), TSK);
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006289
Douglas Gregor6fb745b2010-05-13 16:44:06 +00006290 if (TSK == TSK_ExplicitInstantiationDefinition)
6291 MarkVTableUsed(NameLoc, RecordDef, true);
6292
Mike Stump390b4cc2009-05-16 07:39:55 +00006293 // FIXME: We don't have any representation for explicit instantiations of
6294 // member classes. Such a representation is not needed for compilation, but it
6295 // should be available for clients that want to see all of the declarations in
6296 // the source code.
Douglas Gregor3f5b61c2009-05-14 00:28:11 +00006297 return TagD;
6298}
6299
John McCallf312b1e2010-08-26 23:41:50 +00006300DeclResult Sema::ActOnExplicitInstantiation(Scope *S,
6301 SourceLocation ExternLoc,
6302 SourceLocation TemplateLoc,
6303 Declarator &D) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006304 // Explicit instantiations always require a name.
Abramo Bagnara25777432010-08-11 22:01:17 +00006305 // TODO: check if/when DNInfo should replace Name.
6306 DeclarationNameInfo NameInfo = GetNameForDeclarator(D);
6307 DeclarationName Name = NameInfo.getName();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006308 if (!Name) {
6309 if (!D.isInvalidType())
6310 Diag(D.getDeclSpec().getSourceRange().getBegin(),
6311 diag::err_explicit_instantiation_requires_name)
6312 << D.getDeclSpec().getSourceRange()
6313 << D.getSourceRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006314
Douglas Gregord5a423b2009-09-25 18:43:00 +00006315 return true;
6316 }
6317
6318 // The scope passed in may not be a decl scope. Zip up the scope tree until
6319 // we find one that is.
6320 while ((S->getFlags() & Scope::DeclScope) == 0 ||
6321 (S->getFlags() & Scope::TemplateParamScope) != 0)
6322 S = S->getParent();
6323
6324 // Determine the type of the declaration.
John McCallbf1a0282010-06-04 23:28:52 +00006325 TypeSourceInfo *T = GetTypeForDeclarator(D, S);
6326 QualType R = T->getType();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006327 if (R.isNull())
6328 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006329
Douglas Gregore885e182011-05-21 18:53:30 +00006330 // C++ [dcl.stc]p1:
6331 // A storage-class-specifier shall not be specified in [...] an explicit
6332 // instantiation (14.7.2) directive.
Douglas Gregord5a423b2009-09-25 18:43:00 +00006333 if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) {
Douglas Gregord5a423b2009-09-25 18:43:00 +00006334 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef)
6335 << Name;
6336 return true;
Douglas Gregore885e182011-05-21 18:53:30 +00006337 } else if (D.getDeclSpec().getStorageClassSpec()
6338 != DeclSpec::SCS_unspecified) {
6339 // Complain about then remove the storage class specifier.
6340 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class)
6341 << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc());
6342
6343 D.getMutableDeclSpec().ClearStorageClassSpecs();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006344 }
6345
Douglas Gregor663b5a02009-10-14 20:14:33 +00006346 // C++0x [temp.explicit]p1:
6347 // [...] An explicit instantiation of a function template shall not use the
6348 // inline or constexpr specifiers.
6349 // Presumably, this also applies to member functions of class templates as
6350 // well.
Richard Smith2dc7ece2011-10-18 03:44:03 +00006351 if (D.getDeclSpec().isInlineSpecified())
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006352 Diag(D.getDeclSpec().getInlineSpecLoc(),
Richard Smith2dc7ece2011-10-18 03:44:03 +00006353 getLangOptions().CPlusPlus0x ?
6354 diag::err_explicit_instantiation_inline :
6355 diag::warn_explicit_instantiation_inline_0x)
Richard Smithfe6f6482011-10-14 19:58:02 +00006356 << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc());
6357 if (D.getDeclSpec().isConstexprSpecified())
6358 // FIXME: Add a fix-it to remove the 'constexpr' and add a 'const' if one is
6359 // not already specified.
6360 Diag(D.getDeclSpec().getConstexprSpecLoc(),
6361 diag::err_explicit_instantiation_constexpr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006362
Douglas Gregor558c0322009-10-14 23:41:34 +00006363 // C++0x [temp.explicit]p2:
6364 // There are two forms of explicit instantiation: an explicit instantiation
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006365 // definition and an explicit instantiation declaration. An explicit
6366 // instantiation declaration begins with the extern keyword. [...]
Douglas Gregord5a423b2009-09-25 18:43:00 +00006367 TemplateSpecializationKind TSK
6368 = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition
6369 : TSK_ExplicitInstantiationDeclaration;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006370
Abramo Bagnara25777432010-08-11 22:01:17 +00006371 LookupResult Previous(*this, NameInfo, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006372 LookupParsedName(Previous, S, &D.getCXXScopeSpec());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006373
6374 if (!R->isFunctionType()) {
6375 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006376 // A [...] static data member of a class template can be explicitly
6377 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006378 // template.
John McCalla24dc2e2009-11-17 02:14:36 +00006379 if (Previous.isAmbiguous())
6380 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006381
John McCall1bcee0a2009-12-02 08:25:40 +00006382 VarDecl *Prev = Previous.getAsSingle<VarDecl>();
Douglas Gregord5a423b2009-09-25 18:43:00 +00006383 if (!Prev || !Prev->isStaticDataMember()) {
6384 // We expect to see a data data member here.
6385 Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known)
6386 << Name;
6387 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6388 P != PEnd; ++P)
John McCallf36e02d2009-10-09 21:13:30 +00006389 Diag((*P)->getLocation(), diag::note_explicit_instantiation_here);
Douglas Gregord5a423b2009-09-25 18:43:00 +00006390 return true;
6391 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006392
Douglas Gregord5a423b2009-09-25 18:43:00 +00006393 if (!Prev->getInstantiatedFromStaticDataMember()) {
6394 // FIXME: Check for explicit specialization?
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006395 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006396 diag::err_explicit_instantiation_data_member_not_instantiated)
6397 << Prev;
6398 Diag(Prev->getLocation(), diag::note_explicit_instantiation_here);
6399 // FIXME: Can we provide a note showing where this was declared?
6400 return true;
6401 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006402
Douglas Gregor558c0322009-10-14 23:41:34 +00006403 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006404 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006405 // or a static data member of a class template specialization, the name of
6406 // the class template specialization in the qualified-id for the member
6407 // name shall be a simple-template-id.
6408 //
6409 // C++98 has the same restriction, just worded differently.
6410 if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006411 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006412 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006413 << Prev << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006414
Douglas Gregor558c0322009-10-14 23:41:34 +00006415 // Check the scope of this explicit instantiation.
6416 CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006417
Douglas Gregor454885e2009-10-15 15:54:05 +00006418 // Verify that it is okay to explicitly instantiate here.
6419 MemberSpecializationInfo *MSInfo = Prev->getMemberSpecializationInfo();
6420 assert(MSInfo && "Missing static data member specialization info?");
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006421 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006422 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev,
Douglas Gregor454885e2009-10-15 15:54:05 +00006423 MSInfo->getTemplateSpecializationKind(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006424 MSInfo->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006425 HasNoEffect))
Douglas Gregor454885e2009-10-15 15:54:05 +00006426 return true;
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006427 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006428 return (Decl*) 0;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006429
Douglas Gregord5a423b2009-09-25 18:43:00 +00006430 // Instantiate static data member.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006431 Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006432 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006433 InstantiateStaticDataMemberDefinition(D.getIdentifierLoc(), Prev);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006434
Douglas Gregord5a423b2009-09-25 18:43:00 +00006435 // FIXME: Create an ExplicitInstantiation node?
John McCalld226f652010-08-21 09:40:31 +00006436 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006437 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006438
6439 // If the declarator is a template-id, translate the parser's template
Douglas Gregor0b60d9e2009-09-25 23:53:26 +00006440 // argument list into our AST format.
Douglas Gregordb422df2009-09-25 21:45:23 +00006441 bool HasExplicitTemplateArgs = false;
John McCalld5532b62009-11-23 01:53:49 +00006442 TemplateArgumentListInfo TemplateArgs;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006443 if (D.getName().getKind() == UnqualifiedId::IK_TemplateId) {
6444 TemplateIdAnnotation *TemplateId = D.getName().TemplateId;
John McCalld5532b62009-11-23 01:53:49 +00006445 TemplateArgs.setLAngleLoc(TemplateId->LAngleLoc);
6446 TemplateArgs.setRAngleLoc(TemplateId->RAngleLoc);
Douglas Gregordb422df2009-09-25 21:45:23 +00006447 ASTTemplateArgsPtr TemplateArgsPtr(*this,
6448 TemplateId->getTemplateArgs(),
Douglas Gregordb422df2009-09-25 21:45:23 +00006449 TemplateId->NumArgs);
John McCalld5532b62009-11-23 01:53:49 +00006450 translateTemplateArguments(TemplateArgsPtr, TemplateArgs);
Douglas Gregordb422df2009-09-25 21:45:23 +00006451 HasExplicitTemplateArgs = true;
Douglas Gregorb2f81cf2009-10-01 23:51:25 +00006452 TemplateArgsPtr.release();
Douglas Gregordb422df2009-09-25 21:45:23 +00006453 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006454
Douglas Gregord5a423b2009-09-25 18:43:00 +00006455 // C++ [temp.explicit]p1:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006456 // A [...] function [...] can be explicitly instantiated from its template.
6457 // A member function [...] of a class template can be explicitly
6458 // instantiated from the member definition associated with its class
Douglas Gregord5a423b2009-09-25 18:43:00 +00006459 // template.
John McCallc373d482010-01-27 01:50:18 +00006460 UnresolvedSet<8> Matches;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006461 for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end();
6462 P != PEnd; ++P) {
6463 NamedDecl *Prev = *P;
Douglas Gregordb422df2009-09-25 21:45:23 +00006464 if (!HasExplicitTemplateArgs) {
6465 if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) {
6466 if (Context.hasSameUnqualifiedType(Method->getType(), R)) {
6467 Matches.clear();
Douglas Gregor48026d22010-01-11 18:40:55 +00006468
John McCallc373d482010-01-27 01:50:18 +00006469 Matches.addDecl(Method, P.getAccess());
Douglas Gregor48026d22010-01-11 18:40:55 +00006470 if (Method->getTemplateSpecializationKind() == TSK_Undeclared)
6471 break;
Douglas Gregordb422df2009-09-25 21:45:23 +00006472 }
Douglas Gregord5a423b2009-09-25 18:43:00 +00006473 }
6474 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006475
Douglas Gregord5a423b2009-09-25 18:43:00 +00006476 FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev);
6477 if (!FunTmpl)
6478 continue;
6479
John McCall5769d612010-02-08 23:07:23 +00006480 TemplateDeductionInfo Info(Context, D.getIdentifierLoc());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006481 FunctionDecl *Specialization = 0;
6482 if (TemplateDeductionResult TDK
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006483 = DeduceTemplateArguments(FunTmpl,
John McCalld5532b62009-11-23 01:53:49 +00006484 (HasExplicitTemplateArgs ? &TemplateArgs : 0),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006485 R, Specialization, Info)) {
6486 // FIXME: Keep track of almost-matches?
6487 (void)TDK;
6488 continue;
6489 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006490
John McCallc373d482010-01-27 01:50:18 +00006491 Matches.addDecl(Specialization, P.getAccess());
Douglas Gregord5a423b2009-09-25 18:43:00 +00006492 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006493
Douglas Gregord5a423b2009-09-25 18:43:00 +00006494 // Find the most specialized function template specialization.
John McCallc373d482010-01-27 01:50:18 +00006495 UnresolvedSetIterator Result
Douglas Gregor5c7bf422011-01-11 17:34:58 +00006496 = getMostSpecialized(Matches.begin(), Matches.end(), TPOC_Other, 0,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006497 D.getIdentifierLoc(),
Douglas Gregorfe6b2d42010-03-29 23:34:08 +00006498 PDiag(diag::err_explicit_instantiation_not_known) << Name,
6499 PDiag(diag::err_explicit_instantiation_ambiguous) << Name,
6500 PDiag(diag::note_explicit_instantiation_candidate));
Douglas Gregord5a423b2009-09-25 18:43:00 +00006501
John McCallc373d482010-01-27 01:50:18 +00006502 if (Result == Matches.end())
Douglas Gregord5a423b2009-09-25 18:43:00 +00006503 return true;
John McCallc373d482010-01-27 01:50:18 +00006504
6505 // Ignore access control bits, we don't need them for redeclaration checking.
6506 FunctionDecl *Specialization = cast<FunctionDecl>(*Result);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006507
Douglas Gregor0a897e32009-10-15 17:21:20 +00006508 if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) {
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006509 Diag(D.getIdentifierLoc(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00006510 diag::err_explicit_instantiation_member_function_not_instantiated)
6511 << Specialization
6512 << (Specialization->getTemplateSpecializationKind() ==
6513 TSK_ExplicitSpecialization);
6514 Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here);
6515 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006516 }
6517
Douglas Gregoref96ee02012-01-14 16:38:05 +00006518 FunctionDecl *PrevDecl = Specialization->getPreviousDecl();
Douglas Gregor583f33b2009-10-15 18:07:02 +00006519 if (!PrevDecl && Specialization->isThisDeclarationADefinition())
6520 PrevDecl = Specialization;
6521
Douglas Gregor0a897e32009-10-15 17:21:20 +00006522 if (PrevDecl) {
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006523 bool HasNoEffect = false;
Douglas Gregor0d035142009-10-27 18:42:08 +00006524 if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006525 PrevDecl,
6526 PrevDecl->getTemplateSpecializationKind(),
Douglas Gregor0a897e32009-10-15 17:21:20 +00006527 PrevDecl->getPointOfInstantiation(),
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006528 HasNoEffect))
Douglas Gregor0a897e32009-10-15 17:21:20 +00006529 return true;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006530
Douglas Gregor0a897e32009-10-15 17:21:20 +00006531 // FIXME: We may still want to build some representation of this
6532 // explicit specialization.
Abramo Bagnarac98971d2010-06-12 07:44:57 +00006533 if (HasNoEffect)
John McCalld226f652010-08-21 09:40:31 +00006534 return (Decl*) 0;
Douglas Gregor0a897e32009-10-15 17:21:20 +00006535 }
Anders Carlsson26d6e9d2009-11-24 05:34:41 +00006536
6537 Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc());
Rafael Espindola256fc4d2012-01-04 05:40:59 +00006538 AttributeList *Attr = D.getDeclSpec().getAttributes().getList();
6539 if (Attr)
6540 ProcessDeclAttributeList(S, Specialization, Attr);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006541
Douglas Gregor0a897e32009-10-15 17:21:20 +00006542 if (TSK == TSK_ExplicitInstantiationDefinition)
Chandler Carruth58e390e2010-08-25 08:27:02 +00006543 InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization);
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006544
Douglas Gregor558c0322009-10-14 23:41:34 +00006545 // C++0x [temp.explicit]p2:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006546 // If the explicit instantiation is for a member function, a member class
Douglas Gregor558c0322009-10-14 23:41:34 +00006547 // or a static data member of a class template specialization, the name of
6548 // the class template specialization in the qualified-id for the member
6549 // name shall be a simple-template-id.
6550 //
6551 // C++98 has the same restriction, just worded differently.
Douglas Gregor0a897e32009-10-15 17:21:20 +00006552 FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00006553 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId && !FunTmpl &&
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006554 D.getCXXScopeSpec().isSet() &&
Douglas Gregor558c0322009-10-14 23:41:34 +00006555 !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()))
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006556 Diag(D.getIdentifierLoc(),
Douglas Gregora2dd8282010-06-16 16:26:47 +00006557 diag::ext_explicit_instantiation_without_qualified_id)
Douglas Gregor558c0322009-10-14 23:41:34 +00006558 << Specialization << D.getCXXScopeSpec().getRange();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006559
Douglas Gregor558c0322009-10-14 23:41:34 +00006560 CheckExplicitInstantiationScope(*this,
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006561 FunTmpl? (NamedDecl *)FunTmpl
Douglas Gregor558c0322009-10-14 23:41:34 +00006562 : Specialization->getInstantiatedFromMemberFunction(),
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006563 D.getIdentifierLoc(),
Douglas Gregor558c0322009-10-14 23:41:34 +00006564 D.getCXXScopeSpec().isSet());
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006565
Douglas Gregord5a423b2009-09-25 18:43:00 +00006566 // FIXME: Create some kind of ExplicitInstantiationDecl here.
John McCalld226f652010-08-21 09:40:31 +00006567 return (Decl*) 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00006568}
6569
John McCallf312b1e2010-08-26 23:41:50 +00006570TypeResult
John McCallc4e70192009-09-11 04:59:25 +00006571Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK,
6572 const CXXScopeSpec &SS, IdentifierInfo *Name,
6573 SourceLocation TagLoc, SourceLocation NameLoc) {
6574 // This has to hold, because SS is expected to be defined.
6575 assert(Name && "Expected a name in a dependent tag");
6576
6577 NestedNameSpecifier *NNS
6578 = static_cast<NestedNameSpecifier *>(SS.getScopeRep());
6579 if (!NNS)
6580 return true;
6581
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006582 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec);
Daniel Dunbar12c0ade2010-04-01 16:50:48 +00006583
Douglas Gregor48c89f42010-04-24 16:38:41 +00006584 if (TUK == TUK_Declaration || TUK == TUK_Definition) {
6585 Diag(NameLoc, diag::err_dependent_tag_decl)
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006586 << (TUK == TUK_Definition) << Kind << SS.getRange();
Douglas Gregor48c89f42010-04-24 16:38:41 +00006587 return true;
6588 }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006589
Douglas Gregor059101f2011-03-02 00:47:37 +00006590 // Create the resulting type.
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006591 ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind);
Douglas Gregor059101f2011-03-02 00:47:37 +00006592 QualType Result = Context.getDependentNameType(Kwd, NNS, Name);
6593
6594 // Create type-source location information for this type.
6595 TypeLocBuilder TLB;
6596 DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result);
Abramo Bagnara38a42912012-02-06 19:09:27 +00006597 TL.setElaboratedKeywordLoc(TagLoc);
Douglas Gregor059101f2011-03-02 00:47:37 +00006598 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6599 TL.setNameLoc(NameLoc);
6600 return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result));
John McCallc4e70192009-09-11 04:59:25 +00006601}
6602
John McCallf312b1e2010-08-26 23:41:50 +00006603TypeResult
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006604Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc,
6605 const CXXScopeSpec &SS, const IdentifierInfo &II,
Douglas Gregor1a15dae2010-06-16 22:31:08 +00006606 SourceLocation IdLoc) {
Douglas Gregore29425b2011-02-28 22:42:13 +00006607 if (SS.isInvalid())
Douglas Gregord57959a2009-03-27 23:10:48 +00006608 return true;
Douglas Gregore29425b2011-02-28 22:42:13 +00006609
Richard Smithebaf0e62011-10-18 20:49:44 +00006610 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6611 Diag(TypenameLoc,
6612 getLangOptions().CPlusPlus0x ?
6613 diag::warn_cxx98_compat_typename_outside_of_template :
6614 diag::ext_typename_outside_of_template)
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006615 << FixItHint::CreateRemoval(TypenameLoc);
6616
Douglas Gregor2494dd02011-03-01 01:34:45 +00006617 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
Douglas Gregor9e876872011-03-01 18:12:44 +00006618 QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None,
6619 TypenameLoc, QualifierLoc, II, IdLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00006620 if (T.isNull())
6621 return true;
John McCall63b43852010-04-29 23:50:39 +00006622
6623 TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T);
6624 if (isa<DependentNameType>(T)) {
6625 DependentNameTypeLoc TL = cast<DependentNameTypeLoc>(TSI->getTypeLoc());
Abramo Bagnara38a42912012-02-06 19:09:27 +00006626 TL.setElaboratedKeywordLoc(TypenameLoc);
Douglas Gregor2494dd02011-03-01 01:34:45 +00006627 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006628 TL.setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006629 } else {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006630 ElaboratedTypeLoc TL = cast<ElaboratedTypeLoc>(TSI->getTypeLoc());
Abramo Bagnara38a42912012-02-06 19:09:27 +00006631 TL.setElaboratedKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006632 TL.setQualifierLoc(QualifierLoc);
John McCall4e449832010-05-28 23:32:21 +00006633 cast<TypeSpecTypeLoc>(TL.getNamedTypeLoc()).setNameLoc(IdLoc);
John McCall63b43852010-04-29 23:50:39 +00006634 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006635
John McCallb3d87482010-08-24 05:47:05 +00006636 return CreateParsedType(T, TSI);
Douglas Gregord57959a2009-03-27 23:10:48 +00006637}
6638
John McCallf312b1e2010-08-26 23:41:50 +00006639TypeResult
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006640Sema::ActOnTypenameType(Scope *S,
6641 SourceLocation TypenameLoc,
6642 const CXXScopeSpec &SS,
6643 SourceLocation TemplateKWLoc,
Douglas Gregora02411e2011-02-27 22:46:49 +00006644 TemplateTy TemplateIn,
6645 SourceLocation TemplateNameLoc,
6646 SourceLocation LAngleLoc,
6647 ASTTemplateArgsPtr TemplateArgsIn,
6648 SourceLocation RAngleLoc) {
Richard Smithebaf0e62011-10-18 20:49:44 +00006649 if (TypenameLoc.isValid() && S && !S->getTemplateParamParent())
6650 Diag(TypenameLoc,
6651 getLangOptions().CPlusPlus0x ?
6652 diag::warn_cxx98_compat_typename_outside_of_template :
6653 diag::ext_typename_outside_of_template)
6654 << FixItHint::CreateRemoval(TypenameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006655
6656 // Translate the parser's template argument list in our AST format.
6657 TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc);
6658 translateTemplateArguments(TemplateArgsIn, TemplateArgs);
6659
6660 TemplateName Template = TemplateIn.get();
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006661 if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) {
6662 // Construct a dependent template specialization type.
6663 assert(DTN && "dependent template has non-dependent name?");
6664 assert(DTN->getQualifier()
6665 == static_cast<NestedNameSpecifier*>(SS.getScopeRep()));
6666 QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename,
6667 DTN->getQualifier(),
6668 DTN->getIdentifier(),
6669 TemplateArgs);
Douglas Gregora02411e2011-02-27 22:46:49 +00006670
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006671 // Create source-location information for this type.
John McCall4e449832010-05-28 23:32:21 +00006672 TypeLocBuilder Builder;
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006673 DependentTemplateSpecializationTypeLoc SpecTL
6674 = Builder.push<DependentTemplateSpecializationTypeLoc>(T);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006675 SpecTL.setElaboratedKeywordLoc(TypenameLoc);
6676 SpecTL.setQualifierLoc(SS.getWithLocInContext(Context));
Abramo Bagnara66581d42012-02-06 22:45:07 +00006677 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006678 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006679 SpecTL.setLAngleLoc(LAngleLoc);
6680 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006681 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6682 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006683 return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T));
Douglas Gregor6946baf2009-09-02 13:05:45 +00006684 }
Douglas Gregora02411e2011-02-27 22:46:49 +00006685
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006686 QualType T = CheckTemplateIdType(Template, TemplateNameLoc, TemplateArgs);
6687 if (T.isNull())
6688 return true;
Douglas Gregora02411e2011-02-27 22:46:49 +00006689
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006690 // Provide source-location information for the template specialization type.
Douglas Gregora02411e2011-02-27 22:46:49 +00006691 TypeLocBuilder Builder;
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006692 TemplateSpecializationTypeLoc SpecTL
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006693 = Builder.push<TemplateSpecializationTypeLoc>(T);
Abramo Bagnara55d23c92012-02-06 14:41:24 +00006694 SpecTL.setTemplateKeywordLoc(TemplateKWLoc);
6695 SpecTL.setTemplateNameLoc(TemplateNameLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006696 SpecTL.setLAngleLoc(LAngleLoc);
6697 SpecTL.setRAngleLoc(RAngleLoc);
Douglas Gregora02411e2011-02-27 22:46:49 +00006698 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
6699 SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo());
6700
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006701 T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T);
6702 ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T);
Abramo Bagnara38a42912012-02-06 19:09:27 +00006703 TL.setElaboratedKeywordLoc(TypenameLoc);
Douglas Gregor9e876872011-03-01 18:12:44 +00006704 TL.setQualifierLoc(SS.getWithLocInContext(Context));
6705
Douglas Gregoref24c4b2011-03-01 16:44:30 +00006706 TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T);
6707 return CreateParsedType(T, TSI);
Douglas Gregor17343172009-04-01 00:28:59 +00006708}
6709
Douglas Gregora02411e2011-02-27 22:46:49 +00006710
Douglas Gregord57959a2009-03-27 23:10:48 +00006711/// \brief Build the type that describes a C++ typename specifier,
6712/// e.g., "typename T::type".
6713QualType
Douglas Gregore29425b2011-02-28 22:42:13 +00006714Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
6715 SourceLocation KeywordLoc,
6716 NestedNameSpecifierLoc QualifierLoc,
6717 const IdentifierInfo &II,
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006718 SourceLocation IILoc) {
John McCall77bb1aa2010-05-01 00:40:08 +00006719 CXXScopeSpec SS;
Douglas Gregore29425b2011-02-28 22:42:13 +00006720 SS.Adopt(QualifierLoc);
Douglas Gregord57959a2009-03-27 23:10:48 +00006721
John McCall77bb1aa2010-05-01 00:40:08 +00006722 DeclContext *Ctx = computeDeclContext(SS);
6723 if (!Ctx) {
6724 // If the nested-name-specifier is dependent and couldn't be
6725 // resolved to a type, build a typename type.
Douglas Gregore29425b2011-02-28 22:42:13 +00006726 assert(QualifierLoc.getNestedNameSpecifier()->isDependent());
6727 return Context.getDependentNameType(Keyword,
6728 QualifierLoc.getNestedNameSpecifier(),
6729 &II);
Douglas Gregor42af25f2009-05-11 19:58:34 +00006730 }
Douglas Gregord57959a2009-03-27 23:10:48 +00006731
John McCall77bb1aa2010-05-01 00:40:08 +00006732 // If the nested-name-specifier refers to the current instantiation,
6733 // the "typename" keyword itself is superfluous. In C++03, the
6734 // program is actually ill-formed. However, DR 382 (in C++0x CD1)
6735 // allows such extraneous "typename" keywords, and we retroactively
Douglas Gregor732281d2010-06-14 22:07:54 +00006736 // apply this DR to C++03 code with only a warning. In any case we continue.
Douglas Gregor42af25f2009-05-11 19:58:34 +00006737
John McCall77bb1aa2010-05-01 00:40:08 +00006738 if (RequireCompleteDeclContext(SS, Ctx))
6739 return QualType();
Douglas Gregord57959a2009-03-27 23:10:48 +00006740
6741 DeclarationName Name(&II);
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006742 LookupResult Result(*this, Name, IILoc, LookupOrdinaryName);
John McCalla24dc2e2009-11-17 02:14:36 +00006743 LookupQualifiedName(Result, Ctx);
Douglas Gregord57959a2009-03-27 23:10:48 +00006744 unsigned DiagID = 0;
6745 Decl *Referenced = 0;
John McCalla24dc2e2009-11-17 02:14:36 +00006746 switch (Result.getResultKind()) {
Douglas Gregord57959a2009-03-27 23:10:48 +00006747 case LookupResult::NotFound:
Douglas Gregor3f093272009-10-13 21:16:44 +00006748 DiagID = diag::err_typename_nested_not_found;
Douglas Gregord57959a2009-03-27 23:10:48 +00006749 break;
Douglas Gregord9545042010-12-09 00:06:27 +00006750
6751 case LookupResult::FoundUnresolvedValue: {
6752 // We found a using declaration that is a value. Most likely, the using
6753 // declaration itself is meant to have the 'typename' keyword.
Douglas Gregore29425b2011-02-28 22:42:13 +00006754 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Douglas Gregord9545042010-12-09 00:06:27 +00006755 IILoc);
6756 Diag(IILoc, diag::err_typename_refers_to_using_value_decl)
6757 << Name << Ctx << FullRange;
6758 if (UnresolvedUsingValueDecl *Using
6759 = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){
Douglas Gregordc355712011-02-25 00:36:19 +00006760 SourceLocation Loc = Using->getQualifierLoc().getBeginLoc();
Douglas Gregord9545042010-12-09 00:06:27 +00006761 Diag(Loc, diag::note_using_value_decl_missing_typename)
6762 << FixItHint::CreateInsertion(Loc, "typename ");
6763 }
6764 }
6765 // Fall through to create a dependent typename type, from which we can recover
6766 // better.
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006767
Douglas Gregor7d3f5762010-01-15 01:44:47 +00006768 case LookupResult::NotFoundInCurrentInstantiation:
6769 // Okay, it's a member of an unknown instantiation.
Douglas Gregore29425b2011-02-28 22:42:13 +00006770 return Context.getDependentNameType(Keyword,
6771 QualifierLoc.getNestedNameSpecifier(),
6772 &II);
Douglas Gregord57959a2009-03-27 23:10:48 +00006773
6774 case LookupResult::Found:
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006775 if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006776 // We found a type. Build an ElaboratedType, since the
6777 // typename-specifier was just sugar.
Douglas Gregore29425b2011-02-28 22:42:13 +00006778 return Context.getElaboratedType(ETK_Typename,
6779 QualifierLoc.getNestedNameSpecifier(),
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006780 Context.getTypeDeclType(Type));
Douglas Gregord57959a2009-03-27 23:10:48 +00006781 }
6782
6783 DiagID = diag::err_typename_nested_not_type;
John McCallf36e02d2009-10-09 21:13:30 +00006784 Referenced = Result.getFoundDecl();
Douglas Gregord57959a2009-03-27 23:10:48 +00006785 break;
6786
6787 case LookupResult::FoundOverloaded:
6788 DiagID = diag::err_typename_nested_not_type;
6789 Referenced = *Result.begin();
6790 break;
6791
John McCall6e247262009-10-10 05:48:19 +00006792 case LookupResult::Ambiguous:
Douglas Gregord57959a2009-03-27 23:10:48 +00006793 return QualType();
6794 }
6795
6796 // If we get here, it's because name lookup did not find a
6797 // type. Emit an appropriate diagnostic and return an error.
Douglas Gregore29425b2011-02-28 22:42:13 +00006798 SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(),
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00006799 IILoc);
6800 Diag(IILoc, DiagID) << FullRange << Name << Ctx;
Douglas Gregord57959a2009-03-27 23:10:48 +00006801 if (Referenced)
6802 Diag(Referenced->getLocation(), diag::note_typename_refers_here)
6803 << Name;
6804 return QualType();
6805}
Douglas Gregor4a959d82009-08-06 16:20:37 +00006806
6807namespace {
6808 // See Sema::RebuildTypeInCurrentInstantiation
Benjamin Kramer85b45212009-11-28 19:45:26 +00006809 class CurrentInstantiationRebuilder
Mike Stump1eb44332009-09-09 15:08:12 +00006810 : public TreeTransform<CurrentInstantiationRebuilder> {
Douglas Gregor4a959d82009-08-06 16:20:37 +00006811 SourceLocation Loc;
6812 DeclarationName Entity;
Mike Stump1eb44332009-09-09 15:08:12 +00006813
Douglas Gregor4a959d82009-08-06 16:20:37 +00006814 public:
Douglas Gregor895162d2010-04-30 18:55:50 +00006815 typedef TreeTransform<CurrentInstantiationRebuilder> inherited;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006816
Mike Stump1eb44332009-09-09 15:08:12 +00006817 CurrentInstantiationRebuilder(Sema &SemaRef,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006818 SourceLocation Loc,
Mike Stump1eb44332009-09-09 15:08:12 +00006819 DeclarationName Entity)
6820 : TreeTransform<CurrentInstantiationRebuilder>(SemaRef),
Douglas Gregor4a959d82009-08-06 16:20:37 +00006821 Loc(Loc), Entity(Entity) { }
Mike Stump1eb44332009-09-09 15:08:12 +00006822
6823 /// \brief Determine whether the given type \p T has already been
Douglas Gregor4a959d82009-08-06 16:20:37 +00006824 /// transformed.
6825 ///
6826 /// For the purposes of type reconstruction, a type has already been
6827 /// transformed if it is NULL or if it is not dependent.
6828 bool AlreadyTransformed(QualType T) {
6829 return T.isNull() || !T->isDependentType();
6830 }
Mike Stump1eb44332009-09-09 15:08:12 +00006831
6832 /// \brief Returns the location of the entity whose type is being
Douglas Gregor4a959d82009-08-06 16:20:37 +00006833 /// rebuilt.
6834 SourceLocation getBaseLocation() { return Loc; }
Mike Stump1eb44332009-09-09 15:08:12 +00006835
Douglas Gregor4a959d82009-08-06 16:20:37 +00006836 /// \brief Returns the name of the entity whose type is being rebuilt.
6837 DeclarationName getBaseEntity() { return Entity; }
Mike Stump1eb44332009-09-09 15:08:12 +00006838
Douglas Gregor972e6ce2009-10-27 06:26:26 +00006839 /// \brief Sets the "base" location and entity when that
6840 /// information is known based on another transformation.
6841 void setBase(SourceLocation Loc, DeclarationName Entity) {
6842 this->Loc = Loc;
6843 this->Entity = Entity;
6844 }
Douglas Gregordfca6f52012-02-13 22:00:16 +00006845
6846 ExprResult TransformLambdaExpr(LambdaExpr *E) {
6847 // Lambdas never need to be transformed.
6848 return E;
6849 }
Douglas Gregor4a959d82009-08-06 16:20:37 +00006850 };
6851}
6852
Douglas Gregor4a959d82009-08-06 16:20:37 +00006853/// \brief Rebuilds a type within the context of the current instantiation.
6854///
Mike Stump1eb44332009-09-09 15:08:12 +00006855/// The type \p T is part of the type of an out-of-line member definition of
Douglas Gregor4a959d82009-08-06 16:20:37 +00006856/// a class template (or class template partial specialization) that was parsed
Mike Stump1eb44332009-09-09 15:08:12 +00006857/// and constructed before we entered the scope of the class template (or
Douglas Gregor4a959d82009-08-06 16:20:37 +00006858/// partial specialization thereof). This routine will rebuild that type now
6859/// that we have entered the declarator's scope, which may produce different
6860/// canonical types, e.g.,
6861///
6862/// \code
6863/// template<typename T>
6864/// struct X {
6865/// typedef T* pointer;
6866/// pointer data();
6867/// };
6868///
6869/// template<typename T>
6870/// typename X<T>::pointer X<T>::data() { ... }
6871/// \endcode
6872///
Douglas Gregor4714c122010-03-31 17:34:00 +00006873/// Here, the type "typename X<T>::pointer" will be created as a DependentNameType,
Douglas Gregor4a959d82009-08-06 16:20:37 +00006874/// since we do not know that we can look into X<T> when we parsed the type.
6875/// This function will rebuild the type, performing the lookup of "pointer"
Abramo Bagnara465d41b2010-05-11 21:36:43 +00006876/// in X<T> and returning an ElaboratedType whose canonical type is the same
Douglas Gregor4a959d82009-08-06 16:20:37 +00006877/// as the canonical type of T*, allowing the return types of the out-of-line
6878/// definition and the declaration to match.
John McCall63b43852010-04-29 23:50:39 +00006879TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T,
6880 SourceLocation Loc,
6881 DeclarationName Name) {
6882 if (!T || !T->getType()->isDependentType())
Douglas Gregor4a959d82009-08-06 16:20:37 +00006883 return T;
Mike Stump1eb44332009-09-09 15:08:12 +00006884
Douglas Gregor4a959d82009-08-06 16:20:37 +00006885 CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name);
6886 return Rebuilder.TransformType(T);
Benjamin Kramer27ba2f02009-08-11 22:33:06 +00006887}
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006888
John McCall60d7b3a2010-08-24 06:29:42 +00006889ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) {
John McCallb3d87482010-08-24 05:47:05 +00006890 CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(),
6891 DeclarationName());
6892 return Rebuilder.TransformExpr(E);
6893}
6894
John McCall63b43852010-04-29 23:50:39 +00006895bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) {
Douglas Gregor7e384942011-02-25 16:07:42 +00006896 if (SS.isInvalid())
6897 return true;
John McCall31f17ec2010-04-27 00:57:59 +00006898
Douglas Gregor7e384942011-02-25 16:07:42 +00006899 NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context);
John McCall31f17ec2010-04-27 00:57:59 +00006900 CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(),
6901 DeclarationName());
Douglas Gregor7e384942011-02-25 16:07:42 +00006902 NestedNameSpecifierLoc Rebuilt
6903 = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc);
6904 if (!Rebuilt)
6905 return true;
John McCall63b43852010-04-29 23:50:39 +00006906
Douglas Gregor7e384942011-02-25 16:07:42 +00006907 SS.Adopt(Rebuilt);
John McCall63b43852010-04-29 23:50:39 +00006908 return false;
John McCall31f17ec2010-04-27 00:57:59 +00006909}
6910
Douglas Gregor20606502011-10-14 15:31:12 +00006911/// \brief Rebuild the template parameters now that we know we're in a current
6912/// instantiation.
6913bool Sema::RebuildTemplateParamsInCurrentInstantiation(
6914 TemplateParameterList *Params) {
6915 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
6916 Decl *Param = Params->getParam(I);
6917
6918 // There is nothing to rebuild in a type parameter.
6919 if (isa<TemplateTypeParmDecl>(Param))
6920 continue;
6921
6922 // Rebuild the template parameter list of a template template parameter.
6923 if (TemplateTemplateParmDecl *TTP
6924 = dyn_cast<TemplateTemplateParmDecl>(Param)) {
6925 if (RebuildTemplateParamsInCurrentInstantiation(
6926 TTP->getTemplateParameters()))
6927 return true;
6928
6929 continue;
6930 }
6931
6932 // Rebuild the type of a non-type template parameter.
6933 NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param);
6934 TypeSourceInfo *NewTSI
6935 = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(),
6936 NTTP->getLocation(),
6937 NTTP->getDeclName());
6938 if (!NewTSI)
6939 return true;
6940
6941 if (NewTSI != NTTP->getTypeSourceInfo()) {
6942 NTTP->setTypeSourceInfo(NewTSI);
6943 NTTP->setType(NewTSI->getType());
6944 }
6945 }
6946
6947 return false;
6948}
6949
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006950/// \brief Produces a formatted string that describes the binding of
6951/// template parameters to template arguments.
6952std::string
6953Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6954 const TemplateArgumentList &Args) {
Douglas Gregor910f8002010-11-07 23:05:16 +00006955 return getTemplateArgumentBindingsText(Params, Args.data(), Args.size());
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006956}
6957
6958std::string
6959Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params,
6960 const TemplateArgument *Args,
6961 unsigned NumArgs) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +00006962 SmallString<128> Str;
Douglas Gregor87dd6972010-12-20 16:52:59 +00006963 llvm::raw_svector_ostream Out(Str);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006964
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006965 if (!Params || Params->size() == 0 || NumArgs == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006966 return std::string();
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006967
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006968 for (unsigned I = 0, N = Params->size(); I != N; ++I) {
Douglas Gregor9148c3f2009-11-11 19:13:48 +00006969 if (I >= NumArgs)
6970 break;
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006971
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006972 if (I == 0)
Douglas Gregor87dd6972010-12-20 16:52:59 +00006973 Out << "[with ";
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006974 else
Douglas Gregor87dd6972010-12-20 16:52:59 +00006975 Out << ", ";
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006976
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006977 if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006978 Out << Id->getName();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006979 } else {
Douglas Gregor87dd6972010-12-20 16:52:59 +00006980 Out << '$' << I;
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006981 }
NAKAMURA Takumidfbb02a2011-01-27 07:10:08 +00006982
Douglas Gregor87dd6972010-12-20 16:52:59 +00006983 Out << " = ";
Douglas Gregor8987b232011-09-27 23:30:47 +00006984 Args[I].print(getPrintingPolicy(), Out);
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006985 }
Douglas Gregor87dd6972010-12-20 16:52:59 +00006986
6987 Out << ']';
6988 return Out.str();
Douglas Gregorbf4ea562009-09-15 16:23:51 +00006989}
Francois Pichet8387e2a2011-04-22 22:18:13 +00006990
6991void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, bool Flag) {
6992 if (!FD)
6993 return;
6994 FD->setLateTemplateParsed(Flag);
6995}
6996
6997bool Sema::IsInsideALocalClassWithinATemplateFunction() {
6998 DeclContext *DC = CurContext;
6999
7000 while (DC) {
7001 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) {
7002 const FunctionDecl *FD = RD->isLocalClass();
7003 return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate);
7004 } else if (DC->isTranslationUnit() || DC->isNamespace())
7005 return false;
7006
7007 DC = DC->getParent();
7008 }
7009 return false;
7010}