Douglas Gregor | dd86106 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 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" |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 15 | #include "clang/AST/Expr.h" |
Douglas Gregor | dd86106 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 16 | #include "clang/Parse/DeclSpec.h" |
| 17 | #include "clang/Basic/LangOptions.h" |
| 18 | |
| 19 | using namespace clang; |
| 20 | |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 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. |
| 26 | Sema::DeclTy *Sema::isTemplateName(IdentifierInfo &II, Scope *S, |
| 27 | const CXXScopeSpec *SS) { |
| 28 | DeclContext *DC = 0; |
Steve Naroff | c349ee2 | 2009-01-29 00:07:50 +0000 | [diff] [blame^] | 29 | |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 30 | if (SS) { |
| 31 | if (SS->isInvalid()) |
| 32 | return 0; |
| 33 | DC = static_cast<DeclContext*>(SS->getScopeRep()); |
| 34 | } |
Steve Naroff | c349ee2 | 2009-01-29 00:07:50 +0000 | [diff] [blame^] | 35 | Decl *IIDecl = DC ? |
| 36 | LookupDeclInContext(&II, Decl::IDNS_Ordinary, DC, false) : |
| 37 | LookupDeclInScope(&II, Decl::IDNS_Ordinary, S, false); |
Douglas Gregor | 2fa1044 | 2008-12-18 19:37:40 +0000 | [diff] [blame] | 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 | |
Douglas Gregor | dd86106 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 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. |
| 64 | bool Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
Douglas Gregor | 2715a1f | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 65 | assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
Douglas Gregor | dd86106 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 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. |
| 89 | Sema::DeclTy *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
| 90 | SourceLocation KeyLoc, |
| 91 | IdentifierInfo *ParamName, |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 92 | SourceLocation ParamNameLoc, |
| 93 | unsigned Depth, unsigned Position) { |
Douglas Gregor | dd86106 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 94 | assert(S->isTemplateParamScope() && |
| 95 | "Template type parameter not in template parameter scope!"); |
| 96 | bool Invalid = false; |
| 97 | |
| 98 | if (ParamName) { |
Steve Naroff | c349ee2 | 2009-01-29 00:07:50 +0000 | [diff] [blame^] | 99 | Decl *PrevDecl = LookupDeclInScope(ParamName, Decl::IDNS_Tag, S); |
Douglas Gregor | 2715a1f | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 100 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | dd86106 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 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. |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 124 | Sema::DeclTy *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 125 | unsigned Depth, |
| 126 | unsigned Position) { |
Douglas Gregor | dd86106 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 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) { |
Steve Naroff | c349ee2 | 2009-01-29 00:07:50 +0000 | [diff] [blame^] | 135 | Decl *PrevDecl = LookupDeclInScope(ParamName, Decl::IDNS_Tag, S); |
Douglas Gregor | 2715a1f | 2008-12-08 18:40:42 +0000 | [diff] [blame] | 136 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
Douglas Gregor | dd86106 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 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 | } |
Douglas Gregor | 5247343 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 154 | |
| 155 | /// ActOnTemplateParameterList - Builds a TemplateParameterList that |
| 156 | /// contains the template parameters in Params/NumParams. |
| 157 | Sema::TemplateParamsTy * |
| 158 | Sema::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 | } |