SemaTemplate.cpp revision c349ee29cc5a0701288ace4ee682833b446edc4b
1//===------- 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"
15#include "clang/AST/Expr.h"
16#include "clang/Parse/DeclSpec.h"
17#include "clang/Basic/LangOptions.h"
18
19using namespace clang;
20
21/// 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
30  if (SS) {
31    if (SS->isInvalid())
32      return 0;
33    DC = static_cast<DeclContext*>(SS->getScopeRep());
34  }
35  Decl *IIDecl = DC ?
36                 LookupDeclInContext(&II, Decl::IDNS_Ordinary, DC, false) :
37                 LookupDeclInScope(&II, Decl::IDNS_Ordinary, S, false);
38
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
60/// 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) {
65  assert(PrevDecl->isTemplateParameter() && "Not a template parameter");
66
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,
92				       SourceLocation ParamNameLoc,
93                                       unsigned Depth, unsigned Position) {
94  assert(S->isTemplateParamScope() &&
95	 "Template type parameter not in template parameter scope!");
96  bool Invalid = false;
97
98  if (ParamName) {
99    Decl *PrevDecl = LookupDeclInScope(ParamName, Decl::IDNS_Tag, S);
100    if (PrevDecl && PrevDecl->isTemplateParameter())
101      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.
124Sema::DeclTy *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
125                                                  unsigned Depth,
126                                                  unsigned Position) {
127  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) {
135    Decl *PrevDecl = LookupDeclInScope(ParamName, Decl::IDNS_Tag, S);
136    if (PrevDecl && PrevDecl->isTemplateParameter())
137      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}
154
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}
169