blob: 7ecac00277074ad127f64660b9c8c45cae8aeeb7 [file] [log] [blame]
Douglas Gregordd861062008-12-05 18:15:24 +00001//===------- SemaTemplate.cpp - Semantic Analysis for C++ Templates -------===/
2
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//+//===----------------------------------------------------------------------===/
9
10//
11// This file implements semantic analysis for C++ templates.
12//+//===----------------------------------------------------------------------===/
13
14#include "Sema.h"
Douglas Gregor1b21c7f2008-12-05 23:32:09 +000015#include "clang/AST/Expr.h"
Douglas Gregordd861062008-12-05 18:15:24 +000016#include "clang/Parse/DeclSpec.h"
17#include "clang/Basic/LangOptions.h"
18
19using namespace clang;
20
Douglas Gregor2fa10442008-12-18 19:37:40 +000021/// isTemplateName - Determines whether the identifier II is a
22/// template name in the current scope, and returns the template
23/// declaration if II names a template. An optional CXXScope can be
24/// passed to indicate the C++ scope in which the identifier will be
25/// found.
26Sema::DeclTy *Sema::isTemplateName(IdentifierInfo &II, Scope *S,
27 const CXXScopeSpec *SS) {
28 DeclContext *DC = 0;
Steve Naroffc349ee22009-01-29 00:07:50 +000029
Douglas Gregor2fa10442008-12-18 19:37:40 +000030 if (SS) {
31 if (SS->isInvalid())
32 return 0;
33 DC = static_cast<DeclContext*>(SS->getScopeRep());
34 }
Steve Naroffc349ee22009-01-29 00:07:50 +000035 Decl *IIDecl = DC ?
36 LookupDeclInContext(&II, Decl::IDNS_Ordinary, DC, false) :
37 LookupDeclInScope(&II, Decl::IDNS_Ordinary, S, false);
Douglas Gregor2fa10442008-12-18 19:37:40 +000038
39 if (IIDecl) {
40 // FIXME: We need to represent templates via some kind of
41 // TemplateDecl, because what follows is a hack that only works in
42 // one specific case.
43 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(IIDecl)) {
44 if (FD->getType()->isDependentType())
45 return FD;
46 } else if (OverloadedFunctionDecl *Ovl
47 = dyn_cast<OverloadedFunctionDecl>(IIDecl)) {
48 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
49 FEnd = Ovl->function_end();
50 F != FEnd; ++F) {
51 if ((*F)->getType()->isDependentType())
52 return Ovl;
53 }
54 }
55 return 0;
56 }
57 return 0;
58}
59
Douglas Gregordd861062008-12-05 18:15:24 +000060/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
61/// that the template parameter 'PrevDecl' is being shadowed by a new
62/// declaration at location Loc. Returns true to indicate that this is
63/// an error, and false otherwise.
64bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregor2715a1f2008-12-08 18:40:42 +000065 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregordd861062008-12-05 18:15:24 +000066
67 // Microsoft Visual C++ permits template parameters to be shadowed.
68 if (getLangOptions().Microsoft)
69 return false;
70
71 // C++ [temp.local]p4:
72 // A template-parameter shall not be redeclared within its
73 // scope (including nested scopes).
74 Diag(Loc, diag::err_template_param_shadow)
75 << cast<NamedDecl>(PrevDecl)->getDeclName();
76 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
77 return true;
78}
79
80/// ActOnTypeParameter - Called when a C++ template type parameter
81/// (e.g., "typename T") has been parsed. Typename specifies whether
82/// the keyword "typename" was used to declare the type parameter
83/// (otherwise, "class" was used), and KeyLoc is the location of the
84/// "class" or "typename" keyword. ParamName is the name of the
85/// parameter (NULL indicates an unnamed template parameter) and
86/// ParamName is the location of the parameter name (if any).
87/// If the type parameter has a default argument, it will be added
88/// later via ActOnTypeParameterDefault.
89Sema::DeclTy *Sema::ActOnTypeParameter(Scope *S, bool Typename,
90 SourceLocation KeyLoc,
91 IdentifierInfo *ParamName,
Douglas Gregor52473432008-12-24 02:52:09 +000092 SourceLocation ParamNameLoc,
93 unsigned Depth, unsigned Position) {
Douglas Gregordd861062008-12-05 18:15:24 +000094 assert(S->isTemplateParamScope() &&
95 "Template type parameter not in template parameter scope!");
96 bool Invalid = false;
97
98 if (ParamName) {
Steve Naroffc349ee22009-01-29 00:07:50 +000099 Decl *PrevDecl = LookupDeclInScope(ParamName, Decl::IDNS_Tag, S);
Douglas Gregor2715a1f2008-12-08 18:40:42 +0000100 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregordd861062008-12-05 18:15:24 +0000101 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
102 PrevDecl);
103 }
104
105 TemplateTypeParmDecl *Param
106 = TemplateTypeParmDecl::Create(Context, CurContext,
107 ParamNameLoc, ParamName, Typename);
108 if (Invalid)
109 Param->setInvalidDecl();
110
111 if (ParamName) {
112 // Add the template parameter into the current scope.
113 S->AddDecl(Param);
114 IdResolver.AddDecl(Param);
115 }
116
117 return Param;
118}
119
120/// ActOnNonTypeTemplateParameter - Called when a C++ non-type
121/// template parameter (e.g., "int Size" in "template<int Size>
122/// class Array") has been parsed. S is the current scope and D is
123/// the parsed declarator.
Douglas Gregor52473432008-12-24 02:52:09 +0000124Sema::DeclTy *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
125 unsigned Depth,
126 unsigned Position) {
Douglas Gregordd861062008-12-05 18:15:24 +0000127 QualType T = GetTypeForDeclarator(D, S);
128
129 assert(S->isTemplateParamScope() &&
130 "Template type parameter not in template parameter scope!");
131 bool Invalid = false;
132
133 IdentifierInfo *ParamName = D.getIdentifier();
134 if (ParamName) {
Steve Naroffc349ee22009-01-29 00:07:50 +0000135 Decl *PrevDecl = LookupDeclInScope(ParamName, Decl::IDNS_Tag, S);
Douglas Gregor2715a1f2008-12-08 18:40:42 +0000136 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregordd861062008-12-05 18:15:24 +0000137 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
138 PrevDecl);
139 }
140
141 NonTypeTemplateParmDecl *Param
142 = NonTypeTemplateParmDecl::Create(Context, CurContext, D.getIdentifierLoc(),
143 ParamName, T);
144 if (Invalid)
145 Param->setInvalidDecl();
146
147 if (D.getIdentifier()) {
148 // Add the template parameter into the current scope.
149 S->AddDecl(Param);
150 IdResolver.AddDecl(Param);
151 }
152 return Param;
153}
Douglas Gregor52473432008-12-24 02:52:09 +0000154
155/// ActOnTemplateParameterList - Builds a TemplateParameterList that
156/// contains the template parameters in Params/NumParams.
157Sema::TemplateParamsTy *
158Sema::ActOnTemplateParameterList(unsigned Depth,
159 SourceLocation ExportLoc,
160 SourceLocation TemplateLoc,
161 SourceLocation LAngleLoc,
162 DeclTy **Params, unsigned NumParams,
163 SourceLocation RAngleLoc) {
164 if (ExportLoc.isValid())
165 Diag(ExportLoc, diag::note_template_export_unsupported);
166
167 return TemplateParameterList::Create(Context, (Decl**)Params, NumParams);
168}