SemaTemplate.cpp revision aaba5e346dffdbad5d1c42765a89e4a7afb0da67
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/AST/DeclTemplate.h" 17#include "clang/Parse/DeclSpec.h" 18#include "clang/Basic/LangOptions.h" 19 20using namespace clang; 21 22/// isTemplateName - Determines whether the identifier II is a 23/// template name in the current scope, and returns the template 24/// declaration if II names a template. An optional CXXScope can be 25/// passed to indicate the C++ scope in which the identifier will be 26/// found. 27Sema::DeclTy *Sema::isTemplateName(IdentifierInfo &II, Scope *S, 28 const CXXScopeSpec *SS) { 29 DeclContext *DC = 0; 30 31 if (SS) { 32 if (SS->isInvalid()) 33 return 0; 34 DC = static_cast<DeclContext*>(SS->getScopeRep()); 35 } 36 NamedDecl *IIDecl = LookupParsedName(S, SS, &II, LookupOrdinaryName); 37 38 if (IIDecl) { 39 // FIXME: We need to represent templates via some kind of 40 // TemplateDecl, because what follows is a hack that only works in 41 // one specific case. 42 if (isa<TemplateDecl>(IIDecl)) 43 return IIDecl; 44 45 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(IIDecl)) { 46 if (FD->getType()->isDependentType()) 47 return FD; 48 } else if (OverloadedFunctionDecl *Ovl 49 = dyn_cast<OverloadedFunctionDecl>(IIDecl)) { 50 for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(), 51 FEnd = Ovl->function_end(); 52 F != FEnd; ++F) { 53 if ((*F)->getType()->isDependentType()) 54 return Ovl; 55 } 56 } 57 } 58 return 0; 59} 60 61/// DiagnoseTemplateParameterShadow - Produce a diagnostic complaining 62/// that the template parameter 'PrevDecl' is being shadowed by a new 63/// declaration at location Loc. Returns true to indicate that this is 64/// an error, and false otherwise. 65bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { 66 assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); 67 68 // Microsoft Visual C++ permits template parameters to be shadowed. 69 if (getLangOptions().Microsoft) 70 return false; 71 72 // C++ [temp.local]p4: 73 // A template-parameter shall not be redeclared within its 74 // scope (including nested scopes). 75 Diag(Loc, diag::err_template_param_shadow) 76 << cast<NamedDecl>(PrevDecl)->getDeclName(); 77 Diag(PrevDecl->getLocation(), diag::note_template_param_here); 78 return true; 79} 80 81/// AdjustDeclForTemplates - If the given decl happens to be a template, reset 82/// the parameter D to reference the templated declaration and return a pointer 83/// to the template declaration. Otherwise, do nothing to D and return null. 84TemplateDecl *Sema::AdjustDeclIfTemplate(DeclTy *&D) 85{ 86 if(TemplateDecl *Temp = dyn_cast<TemplateDecl>(static_cast<Decl*>(D))) { 87 D = Temp->getTemplatedDecl(); 88 return Temp; 89 } 90 return 0; 91} 92 93/// ActOnTypeParameter - Called when a C++ template type parameter 94/// (e.g., "typename T") has been parsed. Typename specifies whether 95/// the keyword "typename" was used to declare the type parameter 96/// (otherwise, "class" was used), and KeyLoc is the location of the 97/// "class" or "typename" keyword. ParamName is the name of the 98/// parameter (NULL indicates an unnamed template parameter) and 99/// ParamName is the location of the parameter name (if any). 100/// If the type parameter has a default argument, it will be added 101/// later via ActOnTypeParameterDefault. 102Sema::DeclTy *Sema::ActOnTypeParameter(Scope *S, bool Typename, 103 SourceLocation KeyLoc, 104 IdentifierInfo *ParamName, 105 SourceLocation ParamNameLoc, 106 unsigned Depth, unsigned Position) { 107 assert(S->isTemplateParamScope() && 108 "Template type parameter not in template parameter scope!"); 109 bool Invalid = false; 110 111 if (ParamName) { 112 NamedDecl *PrevDecl = LookupName(S, ParamName, LookupTagName); 113 if (PrevDecl && PrevDecl->isTemplateParameter()) 114 Invalid = Invalid || DiagnoseTemplateParameterShadow(ParamNameLoc, 115 PrevDecl); 116 } 117 118 TemplateTypeParmDecl *Param 119 = TemplateTypeParmDecl::Create(Context, CurContext, ParamNameLoc, 120 Depth, Position, ParamName, Typename); 121 if (Invalid) 122 Param->setInvalidDecl(); 123 124 if (ParamName) { 125 // Add the template parameter into the current scope. 126 S->AddDecl(Param); 127 IdResolver.AddDecl(Param); 128 } 129 130 return Param; 131} 132 133/// ActOnNonTypeTemplateParameter - Called when a C++ non-type 134/// template parameter (e.g., "int Size" in "template<int Size> 135/// class Array") has been parsed. S is the current scope and D is 136/// the parsed declarator. 137Sema::DeclTy *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, 138 unsigned Depth, 139 unsigned Position) { 140 QualType T = GetTypeForDeclarator(D, S); 141 142 assert(S->isTemplateParamScope() && 143 "Non-type template parameter not in template parameter scope!"); 144 bool Invalid = false; 145 146 IdentifierInfo *ParamName = D.getIdentifier(); 147 if (ParamName) { 148 NamedDecl *PrevDecl = LookupName(S, ParamName, LookupTagName); 149 if (PrevDecl && PrevDecl->isTemplateParameter()) 150 Invalid = Invalid || DiagnoseTemplateParameterShadow(D.getIdentifierLoc(), 151 PrevDecl); 152 } 153 154 NonTypeTemplateParmDecl *Param 155 = NonTypeTemplateParmDecl::Create(Context, CurContext, D.getIdentifierLoc(), 156 Depth, Position, ParamName, T); 157 if (Invalid) 158 Param->setInvalidDecl(); 159 160 if (D.getIdentifier()) { 161 // Add the template parameter into the current scope. 162 S->AddDecl(Param); 163 IdResolver.AddDecl(Param); 164 } 165 return Param; 166} 167 168 169/// ActOnTemplateTemplateParameter - Called when a C++ template template 170/// parameter (e.g. T in template <template <typename> class T> class array) 171/// has been parsed. S is the current scope. 172Sema::DeclTy *Sema::ActOnTemplateTemplateParameter(Scope* S, 173 SourceLocation TmpLoc, 174 TemplateParamsTy *Params, 175 IdentifierInfo *Name, 176 SourceLocation NameLoc, 177 unsigned Depth, 178 unsigned Position) 179{ 180 assert(S->isTemplateParamScope() && 181 "Template template parameter not in template parameter scope!"); 182 183 // Construct the parameter object. 184 TemplateTemplateParmDecl *Param = 185 TemplateTemplateParmDecl::Create(Context, CurContext, TmpLoc, Depth, 186 Position, Name, 187 (TemplateParameterList*)Params); 188 189 // Make sure the parameter is valid. 190 // FIXME: Decl object is not currently invalidated anywhere so this doesn't 191 // do anything yet. However, if the template parameter list or (eventual) 192 // default value is ever invalidated, that will propagate here. 193 bool Invalid = false; 194 if (Invalid) { 195 Param->setInvalidDecl(); 196 } 197 198 // If the tt-param has a name, then link the identifier into the scope 199 // and lookup mechanisms. 200 if (Name) { 201 S->AddDecl(Param); 202 IdResolver.AddDecl(Param); 203 } 204 205 return Param; 206} 207 208/// ActOnTemplateParameterList - Builds a TemplateParameterList that 209/// contains the template parameters in Params/NumParams. 210Sema::TemplateParamsTy * 211Sema::ActOnTemplateParameterList(unsigned Depth, 212 SourceLocation ExportLoc, 213 SourceLocation TemplateLoc, 214 SourceLocation LAngleLoc, 215 DeclTy **Params, unsigned NumParams, 216 SourceLocation RAngleLoc) { 217 if (ExportLoc.isValid()) 218 Diag(ExportLoc, diag::note_template_export_unsupported); 219 220 return TemplateParameterList::Create(Context, (Decl**)Params, NumParams); 221} 222 223