blob: f448664615ad00f6dc197c485ef4edd16a239fba [file] [log] [blame]
Douglas Gregor72c3f312008-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 Gregor898574e2008-12-05 23:32:09 +000015#include "clang/AST/Expr.h"
Douglas Gregor72c3f312008-12-05 18:15:24 +000016#include "clang/Parse/DeclSpec.h"
17#include "clang/Basic/LangOptions.h"
18
19using namespace clang;
20
Douglas Gregord6fb7ef2008-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;
29 if (SS) {
30 if (SS->isInvalid())
31 return 0;
32 DC = static_cast<DeclContext*>(SS->getScopeRep());
33 }
34 Decl *IIDecl = LookupDecl(&II, Decl::IDNS_Ordinary, S, DC, false);
35
36 if (IIDecl) {
37 // FIXME: We need to represent templates via some kind of
38 // TemplateDecl, because what follows is a hack that only works in
39 // one specific case.
40 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(IIDecl)) {
41 if (FD->getType()->isDependentType())
42 return FD;
43 } else if (OverloadedFunctionDecl *Ovl
44 = dyn_cast<OverloadedFunctionDecl>(IIDecl)) {
45 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(),
46 FEnd = Ovl->function_end();
47 F != FEnd; ++F) {
48 if ((*F)->getType()->isDependentType())
49 return Ovl;
50 }
51 }
52 return 0;
53 }
54 return 0;
55}
56
Douglas Gregor72c3f312008-12-05 18:15:24 +000057/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining
58/// that the template parameter 'PrevDecl' is being shadowed by a new
59/// declaration at location Loc. Returns true to indicate that this is
60/// an error, and false otherwise.
61bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) {
Douglas Gregorf57172b2008-12-08 18:40:42 +000062 assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
Douglas Gregor72c3f312008-12-05 18:15:24 +000063
64 // Microsoft Visual C++ permits template parameters to be shadowed.
65 if (getLangOptions().Microsoft)
66 return false;
67
68 // C++ [temp.local]p4:
69 // A template-parameter shall not be redeclared within its
70 // scope (including nested scopes).
71 Diag(Loc, diag::err_template_param_shadow)
72 << cast<NamedDecl>(PrevDecl)->getDeclName();
73 Diag(PrevDecl->getLocation(), diag::note_template_param_here);
74 return true;
75}
76
77/// ActOnTypeParameter - Called when a C++ template type parameter
78/// (e.g., "typename T") has been parsed. Typename specifies whether
79/// the keyword "typename" was used to declare the type parameter
80/// (otherwise, "class" was used), and KeyLoc is the location of the
81/// "class" or "typename" keyword. ParamName is the name of the
82/// parameter (NULL indicates an unnamed template parameter) and
83/// ParamName is the location of the parameter name (if any).
84/// If the type parameter has a default argument, it will be added
85/// later via ActOnTypeParameterDefault.
86Sema::DeclTy *Sema::ActOnTypeParameter(Scope *S, bool Typename,
87 SourceLocation KeyLoc,
88 IdentifierInfo *ParamName,
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +000089 SourceLocation ParamNameLoc,
90 unsigned Depth, unsigned Position) {
Douglas Gregor72c3f312008-12-05 18:15:24 +000091 assert(S->isTemplateParamScope() &&
92 "Template type parameter not in template parameter scope!");
93 bool Invalid = false;
94
95 if (ParamName) {
96 Decl *PrevDecl = LookupDecl(ParamName, Decl::IDNS_Tag, S);
Douglas Gregorf57172b2008-12-08 18:40:42 +000097 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +000098 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc,
99 PrevDecl);
100 }
101
102 TemplateTypeParmDecl *Param
103 = TemplateTypeParmDecl::Create(Context, CurContext,
104 ParamNameLoc, ParamName, Typename);
105 if (Invalid)
106 Param->setInvalidDecl();
107
108 if (ParamName) {
109 // Add the template parameter into the current scope.
110 S->AddDecl(Param);
111 IdResolver.AddDecl(Param);
112 }
113
114 return Param;
115}
116
117/// ActOnNonTypeTemplateParameter - Called when a C++ non-type
118/// template parameter (e.g., "int Size" in "template<int Size>
119/// class Array") has been parsed. S is the current scope and D is
120/// the parsed declarator.
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000121Sema::DeclTy *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
122 unsigned Depth,
123 unsigned Position) {
Douglas Gregor72c3f312008-12-05 18:15:24 +0000124 QualType T = GetTypeForDeclarator(D, S);
125
126 assert(S->isTemplateParamScope() &&
127 "Template type parameter not in template parameter scope!");
128 bool Invalid = false;
129
130 IdentifierInfo *ParamName = D.getIdentifier();
131 if (ParamName) {
132 Decl *PrevDecl = LookupDecl(ParamName, Decl::IDNS_Tag, S);
Douglas Gregorf57172b2008-12-08 18:40:42 +0000133 if (PrevDecl && PrevDecl->isTemplateParameter())
Douglas Gregor72c3f312008-12-05 18:15:24 +0000134 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(),
135 PrevDecl);
136 }
137
138 NonTypeTemplateParmDecl *Param
139 = NonTypeTemplateParmDecl::Create(Context, CurContext, D.getIdentifierLoc(),
140 ParamName, T);
141 if (Invalid)
142 Param->setInvalidDecl();
143
144 if (D.getIdentifier()) {
145 // Add the template parameter into the current scope.
146 S->AddDecl(Param);
147 IdResolver.AddDecl(Param);
148 }
149 return Param;
150}
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000151
152/// ActOnTemplateParameterList - Builds a TemplateParameterList that
153/// contains the template parameters in Params/NumParams.
154Sema::TemplateParamsTy *
155Sema::ActOnTemplateParameterList(unsigned Depth,
156 SourceLocation ExportLoc,
157 SourceLocation TemplateLoc,
158 SourceLocation LAngleLoc,
159 DeclTy **Params, unsigned NumParams,
160 SourceLocation RAngleLoc) {
161 if (ExportLoc.isValid())
162 Diag(ExportLoc, diag::note_template_export_unsupported);
163
164 return TemplateParameterList::Create(Context, (Decl**)Params, NumParams);
165}