| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1 | //===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/ | 
|  | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
|  | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
|  | 7 | //===----------------------------------------------------------------------===/ | 
|  | 8 | // | 
|  | 9 | //  This file implements C++ template instantiation for declarations. | 
|  | 10 | // | 
|  | 11 | //===----------------------------------------------------------------------===/ | 
|  | 12 | #include "Sema.h" | 
| Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTConsumer.h" | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" | 
|  | 15 | #include "clang/AST/DeclTemplate.h" | 
|  | 16 | #include "clang/AST/DeclVisitor.h" | 
|  | 17 | #include "clang/AST/Expr.h" | 
| Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 18 | #include "clang/Lex/Preprocessor.h" | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Compiler.h" | 
|  | 20 |  | 
|  | 21 | using namespace clang; | 
|  | 22 |  | 
|  | 23 | namespace { | 
|  | 24 | class VISIBILITY_HIDDEN TemplateDeclInstantiator | 
| Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 25 | : public DeclVisitor<TemplateDeclInstantiator, Decl *> { | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 26 | Sema &SemaRef; | 
|  | 27 | DeclContext *Owner; | 
| Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 28 | const MultiLevelTemplateArgumentList &TemplateArgs; | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 29 |  | 
|  | 30 | public: | 
|  | 31 | typedef Sema::OwningExprResult OwningExprResult; | 
|  | 32 |  | 
|  | 33 | TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner, | 
| Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 34 | const MultiLevelTemplateArgumentList &TemplateArgs) | 
| Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 35 | : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { } | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 36 |  | 
| Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 37 | // FIXME: Once we get closer to completion, replace these manually-written | 
|  | 38 | // declarations with automatically-generated ones from | 
|  | 39 | // clang/AST/DeclNodes.def. | 
| Douglas Gregor | 4f722be | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 40 | Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); | 
|  | 41 | Decl *VisitNamespaceDecl(NamespaceDecl *D); | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 42 | Decl *VisitTypedefDecl(TypedefDecl *D); | 
| Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 43 | Decl *VisitVarDecl(VarDecl *D); | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 44 | Decl *VisitFieldDecl(FieldDecl *D); | 
|  | 45 | Decl *VisitStaticAssertDecl(StaticAssertDecl *D); | 
|  | 46 | Decl *VisitEnumDecl(EnumDecl *D); | 
| Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 47 | Decl *VisitEnumConstantDecl(EnumConstantDecl *D); | 
| John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 48 | Decl *VisitFriendDecl(FriendDecl *D); | 
| Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 49 | Decl *VisitFunctionDecl(FunctionDecl *D); | 
| Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 50 | Decl *VisitCXXRecordDecl(CXXRecordDecl *D); | 
| Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 51 | Decl *VisitCXXMethodDecl(CXXMethodDecl *D, | 
|  | 52 | TemplateParameterList *TemplateParams = 0); | 
| Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 53 | Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); | 
| Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 54 | Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); | 
| Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 55 | Decl *VisitCXXConversionDecl(CXXConversionDecl *D); | 
| Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 56 | ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D); | 
| Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 57 | Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D); | 
| John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 58 | Decl *VisitClassTemplateDecl(ClassTemplateDecl *D); | 
| Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 59 | Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D); | 
| John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 60 | Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); | 
| Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 61 | Decl *VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D); | 
|  | 62 |  | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 63 | // Base case. FIXME: Remove once we can instantiate everything. | 
|  | 64 | Decl *VisitDecl(Decl *) { | 
| Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 65 | assert(false && "Template instantiation of unknown declaration kind!"); | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 66 | return 0; | 
|  | 67 | } | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 68 |  | 
| John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 69 | const LangOptions &getLangOptions() { | 
|  | 70 | return SemaRef.getLangOptions(); | 
|  | 71 | } | 
|  | 72 |  | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 73 | // Helper functions for instantiating methods. | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 74 | QualType SubstFunctionType(FunctionDecl *D, | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 75 | llvm::SmallVectorImpl<ParmVarDecl *> &Params); | 
| Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 76 | bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl); | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 77 | bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl); | 
| John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 78 |  | 
|  | 79 | TemplateParameterList * | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 80 | SubstTemplateParams(TemplateParameterList *List); | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 81 | }; | 
|  | 82 | } | 
|  | 83 |  | 
| Douglas Gregor | 4f722be | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 84 | Decl * | 
|  | 85 | TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { | 
|  | 86 | assert(false && "Translation units cannot be instantiated"); | 
|  | 87 | return D; | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | Decl * | 
|  | 91 | TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { | 
|  | 92 | assert(false && "Namespaces cannot be instantiated"); | 
|  | 93 | return D; | 
|  | 94 | } | 
|  | 95 |  | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 96 | Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { | 
|  | 97 | bool Invalid = false; | 
|  | 98 | QualType T = D->getUnderlyingType(); | 
|  | 99 | if (T->isDependentType()) { | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 100 | T = SemaRef.SubstType(T, TemplateArgs, | 
|  | 101 | D->getLocation(), D->getDeclName()); | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 102 | if (T.isNull()) { | 
|  | 103 | Invalid = true; | 
|  | 104 | T = SemaRef.Context.IntTy; | 
|  | 105 | } | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | // Create the new typedef | 
|  | 109 | TypedefDecl *Typedef | 
|  | 110 | = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(), | 
|  | 111 | D->getIdentifier(), T); | 
|  | 112 | if (Invalid) | 
|  | 113 | Typedef->setInvalidDecl(); | 
|  | 114 |  | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 115 | Owner->addDecl(Typedef); | 
| Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 116 |  | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 117 | return Typedef; | 
|  | 118 | } | 
|  | 119 |  | 
| Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 120 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 121 | // Do substitution on the type of the declaration | 
|  | 122 | QualType T = SemaRef.SubstType(D->getType(), TemplateArgs, | 
|  | 123 | D->getTypeSpecStartLoc(), | 
|  | 124 | D->getDeclName()); | 
| Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 125 | if (T.isNull()) | 
|  | 126 | return 0; | 
|  | 127 |  | 
| Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 128 | // Build the instantiated declaration | 
| Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 129 | VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner, | 
|  | 130 | D->getLocation(), D->getIdentifier(), | 
| Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 131 | T, D->getDeclaratorInfo(), | 
| Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 132 | D->getStorageClass()); | 
| Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 133 | Var->setThreadSpecified(D->isThreadSpecified()); | 
|  | 134 | Var->setCXXDirectInitializer(D->hasCXXDirectInitializer()); | 
|  | 135 | Var->setDeclaredInCondition(D->isDeclaredInCondition()); | 
|  | 136 |  | 
| Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 137 | // If we are instantiating a static data member defined | 
|  | 138 | // out-of-line, the instantiation will have the same lexical | 
|  | 139 | // context (which will be a namespace scope) as the template. | 
|  | 140 | if (D->isOutOfLine()) | 
|  | 141 | Var->setLexicalDeclContext(D->getLexicalDeclContext()); | 
|  | 142 |  | 
| Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 143 | // FIXME: In theory, we could have a previous declaration for variables that | 
|  | 144 | // are not static data members. | 
| Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 145 | bool Redeclaration = false; | 
| Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 146 | SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration); | 
| Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 147 |  | 
|  | 148 | if (D->isOutOfLine()) { | 
|  | 149 | D->getLexicalDeclContext()->addDecl(Var); | 
|  | 150 | Owner->makeDeclVisibleInContext(Var); | 
|  | 151 | } else { | 
|  | 152 | Owner->addDecl(Var); | 
|  | 153 | } | 
|  | 154 |  | 
| Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 155 | if (D->getInit()) { | 
|  | 156 | OwningExprResult Init | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 157 | = SemaRef.SubstExpr(D->getInit(), TemplateArgs); | 
| Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 158 | if (Init.isInvalid()) | 
|  | 159 | Var->setInvalidDecl(); | 
| Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 160 | else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) { | 
|  | 161 | // FIXME: We're faking all of the comma locations, which is suboptimal. | 
|  | 162 | // Do we even need these comma locations? | 
|  | 163 | llvm::SmallVector<SourceLocation, 4> FakeCommaLocs; | 
|  | 164 | if (PLE->getNumExprs() > 0) { | 
|  | 165 | FakeCommaLocs.reserve(PLE->getNumExprs() - 1); | 
|  | 166 | for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) { | 
|  | 167 | Expr *E = PLE->getExpr(I)->Retain(); | 
|  | 168 | FakeCommaLocs.push_back( | 
|  | 169 | SemaRef.PP.getLocForEndOfToken(E->getLocEnd())); | 
|  | 170 | } | 
| Douglas Gregor | e9f8eb6 | 2009-08-26 23:26:04 +0000 | [diff] [blame] | 171 | PLE->getExpr(PLE->getNumExprs() - 1)->Retain(); | 
| Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 172 | } | 
|  | 173 |  | 
|  | 174 | // Add the direct initializer to the declaration. | 
|  | 175 | SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var), | 
|  | 176 | PLE->getLParenLoc(), | 
|  | 177 | Sema::MultiExprArg(SemaRef, | 
|  | 178 | (void**)PLE->getExprs(), | 
|  | 179 | PLE->getNumExprs()), | 
|  | 180 | FakeCommaLocs.data(), | 
|  | 181 | PLE->getRParenLoc()); | 
|  | 182 |  | 
|  | 183 | // When Init is destroyed, it will destroy the instantiated ParenListExpr; | 
|  | 184 | // we've explicitly retained all of its subexpressions already. | 
|  | 185 | } else | 
| Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 186 | SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init), | 
| Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 187 | D->hasCXXDirectInitializer()); | 
| Douglas Gregor | 65b9005 | 2009-07-27 17:43:39 +0000 | [diff] [blame] | 188 | } else if (!Var->isStaticDataMember() || Var->isOutOfLine()) | 
|  | 189 | SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false); | 
| Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 190 |  | 
| Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 191 | // Link instantiations of static data members back to the template from | 
|  | 192 | // which they were instantiated. | 
|  | 193 | if (Var->isStaticDataMember()) | 
|  | 194 | SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D); | 
|  | 195 |  | 
| Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 196 | return Var; | 
|  | 197 | } | 
|  | 198 |  | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 199 | Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { | 
|  | 200 | bool Invalid = false; | 
|  | 201 | QualType T = D->getType(); | 
|  | 202 | if (T->isDependentType())  { | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 203 | T = SemaRef.SubstType(T, TemplateArgs, | 
|  | 204 | D->getLocation(), D->getDeclName()); | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 205 | if (!T.isNull() && T->isFunctionType()) { | 
|  | 206 | // C++ [temp.arg.type]p3: | 
|  | 207 | //   If a declaration acquires a function type through a type | 
|  | 208 | //   dependent on a template-parameter and this causes a | 
|  | 209 | //   declaration that does not use the syntactic form of a | 
|  | 210 | //   function declarator to have function type, the program is | 
|  | 211 | //   ill-formed. | 
|  | 212 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) | 
|  | 213 | << T; | 
|  | 214 | T = QualType(); | 
|  | 215 | Invalid = true; | 
|  | 216 | } | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | Expr *BitWidth = D->getBitWidth(); | 
|  | 220 | if (Invalid) | 
|  | 221 | BitWidth = 0; | 
|  | 222 | else if (BitWidth) { | 
| Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 223 | // The bit-width expression is not potentially evaluated. | 
|  | 224 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated); | 
|  | 225 |  | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 226 | OwningExprResult InstantiatedBitWidth | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 227 | = SemaRef.SubstExpr(BitWidth, TemplateArgs); | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 228 | if (InstantiatedBitWidth.isInvalid()) { | 
|  | 229 | Invalid = true; | 
|  | 230 | BitWidth = 0; | 
|  | 231 | } else | 
| Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 232 | BitWidth = InstantiatedBitWidth.takeAs<Expr>(); | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 233 | } | 
|  | 234 |  | 
|  | 235 | FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T, | 
| Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 236 | D->getDeclaratorInfo(), | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 237 | cast<RecordDecl>(Owner), | 
|  | 238 | D->getLocation(), | 
|  | 239 | D->isMutable(), | 
|  | 240 | BitWidth, | 
| Steve Naroff | ea218b8 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 241 | D->getTypeSpecStartLoc(), | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 242 | D->getAccess(), | 
|  | 243 | 0); | 
|  | 244 | if (Field) { | 
|  | 245 | if (Invalid) | 
|  | 246 | Field->setInvalidDecl(); | 
|  | 247 |  | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 248 | Owner->addDecl(Field); | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 249 | } | 
|  | 250 |  | 
|  | 251 | return Field; | 
|  | 252 | } | 
|  | 253 |  | 
| John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 254 | Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) { | 
|  | 255 | FriendDecl::FriendUnion FU; | 
|  | 256 |  | 
|  | 257 | // Handle friend type expressions by simply substituting template | 
|  | 258 | // parameters into the pattern type. | 
|  | 259 | if (Type *Ty = D->getFriendType()) { | 
|  | 260 | QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs, | 
|  | 261 | D->getLocation(), DeclarationName()); | 
|  | 262 | if (T.isNull()) return 0; | 
|  | 263 |  | 
|  | 264 | assert(getLangOptions().CPlusPlus0x || T->isRecordType()); | 
|  | 265 | FU = T.getTypePtr(); | 
|  | 266 |  | 
|  | 267 | // Handle everything else by appropriate substitution. | 
|  | 268 | } else { | 
|  | 269 | NamedDecl *ND = D->getFriendDecl(); | 
|  | 270 | assert(ND && "friend decl must be a decl or a type!"); | 
|  | 271 |  | 
|  | 272 | Decl *NewND = Visit(ND); | 
|  | 273 | if (!NewND) return 0; | 
|  | 274 |  | 
|  | 275 | FU = cast<NamedDecl>(NewND); | 
| John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 276 | } | 
| John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 277 |  | 
|  | 278 | FriendDecl *FD = | 
|  | 279 | FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU, | 
|  | 280 | D->getFriendLoc()); | 
|  | 281 | Owner->addDecl(FD); | 
|  | 282 | return FD; | 
| John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 283 | } | 
|  | 284 |  | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 285 | Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { | 
|  | 286 | Expr *AssertExpr = D->getAssertExpr(); | 
|  | 287 |  | 
| Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 288 | // The expression in a static assertion is not potentially evaluated. | 
|  | 289 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated); | 
|  | 290 |  | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 291 | OwningExprResult InstantiatedAssertExpr | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 292 | = SemaRef.SubstExpr(AssertExpr, TemplateArgs); | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 293 | if (InstantiatedAssertExpr.isInvalid()) | 
|  | 294 | return 0; | 
|  | 295 |  | 
| Douglas Gregor | 43d9d92 | 2009-08-08 01:41:12 +0000 | [diff] [blame] | 296 | OwningExprResult Message(SemaRef, D->getMessage()); | 
|  | 297 | D->getMessage()->Retain(); | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 298 | Decl *StaticAssert | 
| Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 299 | = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(), | 
|  | 300 | move(InstantiatedAssertExpr), | 
|  | 301 | move(Message)).getAs<Decl>(); | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 302 | return StaticAssert; | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { | 
|  | 306 | EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, | 
|  | 307 | D->getLocation(), D->getIdentifier(), | 
| Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 308 | D->getTagKeywordLoc(), | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 309 | /*PrevDecl=*/0); | 
| Douglas Gregor | 8dbc3c6 | 2009-05-27 17:20:35 +0000 | [diff] [blame] | 310 | Enum->setInstantiationOfMemberEnum(D); | 
| Douglas Gregor | 06c0fec | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 311 | Enum->setAccess(D->getAccess()); | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 312 | Owner->addDecl(Enum); | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 313 | Enum->startDefinition(); | 
|  | 314 |  | 
| Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 315 | llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators; | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 316 |  | 
|  | 317 | EnumConstantDecl *LastEnumConst = 0; | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 318 | for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(), | 
|  | 319 | ECEnd = D->enumerator_end(); | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 320 | EC != ECEnd; ++EC) { | 
|  | 321 | // The specified value for the enumerator. | 
|  | 322 | OwningExprResult Value = SemaRef.Owned((Expr *)0); | 
| Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 323 | if (Expr *UninstValue = EC->getInitExpr()) { | 
|  | 324 | // The enumerator's value expression is not potentially evaluated. | 
|  | 325 | EnterExpressionEvaluationContext Unevaluated(SemaRef, | 
|  | 326 | Action::Unevaluated); | 
|  | 327 |  | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 328 | Value = SemaRef.SubstExpr(UninstValue, TemplateArgs); | 
| Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 329 | } | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 330 |  | 
|  | 331 | // Drop the initial value and continue. | 
|  | 332 | bool isInvalid = false; | 
|  | 333 | if (Value.isInvalid()) { | 
|  | 334 | Value = SemaRef.Owned((Expr *)0); | 
|  | 335 | isInvalid = true; | 
|  | 336 | } | 
|  | 337 |  | 
|  | 338 | EnumConstantDecl *EnumConst | 
|  | 339 | = SemaRef.CheckEnumConstant(Enum, LastEnumConst, | 
|  | 340 | EC->getLocation(), EC->getIdentifier(), | 
|  | 341 | move(Value)); | 
|  | 342 |  | 
|  | 343 | if (isInvalid) { | 
|  | 344 | if (EnumConst) | 
|  | 345 | EnumConst->setInvalidDecl(); | 
|  | 346 | Enum->setInvalidDecl(); | 
|  | 347 | } | 
|  | 348 |  | 
|  | 349 | if (EnumConst) { | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 350 | Enum->addDecl(EnumConst); | 
| Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 351 | Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst)); | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 352 | LastEnumConst = EnumConst; | 
|  | 353 | } | 
|  | 354 | } | 
|  | 355 |  | 
| Mike Stump | c6e35aa | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 356 | // FIXME: Fixup LBraceLoc and RBraceLoc | 
| Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 357 | // FIXME: Empty Scope and AttributeList (required to handle attribute packed). | 
| Mike Stump | c6e35aa | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 358 | SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(), | 
|  | 359 | Sema::DeclPtrTy::make(Enum), | 
| Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 360 | &Enumerators[0], Enumerators.size(), | 
|  | 361 | 0, 0); | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 362 |  | 
|  | 363 | return Enum; | 
|  | 364 | } | 
|  | 365 |  | 
| Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 366 | Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { | 
|  | 367 | assert(false && "EnumConstantDecls can only occur within EnumDecls."); | 
|  | 368 | return 0; | 
|  | 369 | } | 
|  | 370 |  | 
| John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 371 | Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { | 
|  | 372 | TemplateParameterList *TempParams = D->getTemplateParameters(); | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 373 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); | 
| Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 374 | if (!InstParams) | 
|  | 375 | return NULL; | 
| John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 376 |  | 
|  | 377 | CXXRecordDecl *Pattern = D->getTemplatedDecl(); | 
|  | 378 | CXXRecordDecl *RecordInst | 
|  | 379 | = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner, | 
|  | 380 | Pattern->getLocation(), Pattern->getIdentifier(), | 
|  | 381 | Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL); | 
|  | 382 |  | 
|  | 383 | ClassTemplateDecl *Inst | 
|  | 384 | = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(), | 
|  | 385 | D->getIdentifier(), InstParams, RecordInst, 0); | 
|  | 386 | RecordInst->setDescribedClassTemplate(Inst); | 
|  | 387 | Inst->setAccess(D->getAccess()); | 
|  | 388 | Inst->setInstantiatedFromMemberTemplate(D); | 
|  | 389 |  | 
|  | 390 | Owner->addDecl(Inst); | 
|  | 391 | return Inst; | 
|  | 392 | } | 
|  | 393 |  | 
| Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 394 | Decl * | 
|  | 395 | TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { | 
|  | 396 | TemplateParameterList *TempParams = D->getTemplateParameters(); | 
|  | 397 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); | 
|  | 398 | if (!InstParams) | 
|  | 399 | return NULL; | 
|  | 400 |  | 
|  | 401 | // FIXME: Handle instantiation of nested function templates that aren't | 
|  | 402 | // member function templates. This could happen inside a FriendDecl. | 
|  | 403 | assert(isa<CXXMethodDecl>(D->getTemplatedDecl())); | 
|  | 404 | CXXMethodDecl *InstMethod | 
|  | 405 | = cast_or_null<CXXMethodDecl>( | 
|  | 406 | VisitCXXMethodDecl(cast<CXXMethodDecl>(D->getTemplatedDecl()), | 
|  | 407 | InstParams)); | 
|  | 408 | if (!InstMethod) | 
|  | 409 | return 0; | 
|  | 410 |  | 
|  | 411 | // Link the instantiated function template declaration to the function | 
|  | 412 | // template from which it was instantiated. | 
|  | 413 | FunctionTemplateDecl *InstTemplate = InstMethod->getDescribedFunctionTemplate(); | 
|  | 414 | assert(InstTemplate && "VisitCXXMethodDecl didn't create a template!"); | 
|  | 415 | InstTemplate->setInstantiatedFromMemberTemplate(D); | 
|  | 416 | Owner->addDecl(InstTemplate); | 
|  | 417 | return InstTemplate; | 
|  | 418 | } | 
|  | 419 |  | 
| Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 420 | Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { | 
|  | 421 | CXXRecordDecl *PrevDecl = 0; | 
|  | 422 | if (D->isInjectedClassName()) | 
|  | 423 | PrevDecl = cast<CXXRecordDecl>(Owner); | 
|  | 424 |  | 
|  | 425 | CXXRecordDecl *Record | 
|  | 426 | = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner, | 
| Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 427 | D->getLocation(), D->getIdentifier(), | 
|  | 428 | D->getTagKeywordLoc(), PrevDecl); | 
| Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 429 | Record->setImplicit(D->isImplicit()); | 
| Eli Friedman | eaba1af | 2009-08-27 19:11:42 +0000 | [diff] [blame] | 430 | // FIXME: Check against AS_none is an ugly hack to work around the issue that | 
|  | 431 | // the tag decls introduced by friend class declarations don't have an access | 
|  | 432 | // specifier. Remove once this area of the code gets sorted out. | 
|  | 433 | if (D->getAccess() != AS_none) | 
|  | 434 | Record->setAccess(D->getAccess()); | 
| Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 435 | if (!D->isInjectedClassName()) | 
|  | 436 | Record->setInstantiationOfMemberClass(D); | 
|  | 437 |  | 
| John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 438 | // If the original function was part of a friend declaration, | 
|  | 439 | // inherit its namespace state. | 
|  | 440 | if (Decl::FriendObjectKind FOK = D->getFriendObjectKind()) | 
|  | 441 | Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared); | 
|  | 442 |  | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 443 | Owner->addDecl(Record); | 
| Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 444 | return Record; | 
|  | 445 | } | 
|  | 446 |  | 
| John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 447 | /// Normal class members are of more specific types and therefore | 
|  | 448 | /// don't make it here.  This function serves two purposes: | 
|  | 449 | ///   1) instantiating function templates | 
|  | 450 | ///   2) substituting friend declarations | 
|  | 451 | /// FIXME: preserve function definitions in case #2 | 
| Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 452 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) { | 
| Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 453 | // Check whether there is already a function template specialization for | 
|  | 454 | // this declaration. | 
|  | 455 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); | 
|  | 456 | void *InsertPos = 0; | 
|  | 457 | if (FunctionTemplate) { | 
|  | 458 | llvm::FoldingSetNodeID ID; | 
|  | 459 | FunctionTemplateSpecializationInfo::Profile(ID, | 
| Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 460 | TemplateArgs.getInnermost().getFlatArgumentList(), | 
|  | 461 | TemplateArgs.getInnermost().flat_size(), | 
| Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 462 | SemaRef.Context); | 
| Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 463 |  | 
|  | 464 | FunctionTemplateSpecializationInfo *Info | 
|  | 465 | = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID, | 
|  | 466 | InsertPos); | 
|  | 467 |  | 
|  | 468 | // If we already have a function template specialization, return it. | 
|  | 469 | if (Info) | 
|  | 470 | return Info->Function; | 
|  | 471 | } | 
| Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 472 |  | 
|  | 473 | Sema::LocalInstantiationScope Scope(SemaRef); | 
|  | 474 |  | 
|  | 475 | llvm::SmallVector<ParmVarDecl *, 4> Params; | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 476 | QualType T = SubstFunctionType(D, Params); | 
| Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 477 | if (T.isNull()) | 
| Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 478 | return 0; | 
| John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 479 |  | 
| Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 480 | // Build the instantiated method declaration. | 
| John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 481 | DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext()); | 
|  | 482 | FunctionDecl *Function = | 
|  | 483 | FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(), | 
| Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 484 | D->getDeclName(), T, D->getDeclaratorInfo(), | 
|  | 485 | D->getStorageClass(), | 
| Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 486 | D->isInline(), D->hasWrittenPrototype()); | 
| John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 487 | Function->setLexicalDeclContext(Owner); | 
|  | 488 |  | 
| Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 489 | // Attach the parameters | 
|  | 490 | for (unsigned P = 0; P < Params.size(); ++P) | 
|  | 491 | Params[P]->setOwningFunction(Function); | 
|  | 492 | Function->setParams(SemaRef.Context, Params.data(), Params.size()); | 
| John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 493 |  | 
|  | 494 | // If the original function was part of a friend declaration, | 
|  | 495 | // inherit its namespace state and add it to the owner. | 
|  | 496 | if (Decl::FriendObjectKind FOK = D->getFriendObjectKind()) { | 
|  | 497 | bool WasDeclared = (FOK == Decl::FOK_Declared); | 
|  | 498 | Function->setObjectOfFriendDecl(WasDeclared); | 
|  | 499 | if (!Owner->isDependentContext()) | 
|  | 500 | DC->makeDeclVisibleInContext(Function); | 
| John McCall | f181d8a | 2009-08-29 03:16:09 +0000 | [diff] [blame^] | 501 |  | 
|  | 502 | Function->setInstantiationOfMemberFunction(D); | 
| John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 503 | } | 
| Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 504 |  | 
|  | 505 | if (InitFunctionInstantiation(Function, D)) | 
|  | 506 | Function->setInvalidDecl(); | 
|  | 507 |  | 
|  | 508 | bool Redeclaration = false; | 
|  | 509 | bool OverloadableAttrRequired = false; | 
|  | 510 | NamedDecl *PrevDecl = 0; | 
|  | 511 | SemaRef.CheckFunctionDeclaration(Function, PrevDecl, Redeclaration, | 
|  | 512 | /*FIXME:*/OverloadableAttrRequired); | 
| Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 513 |  | 
| Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 514 | if (FunctionTemplate) { | 
|  | 515 | // Record this function template specialization. | 
|  | 516 | Function->setFunctionTemplateSpecialization(SemaRef.Context, | 
|  | 517 | FunctionTemplate, | 
| Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 518 | &TemplateArgs.getInnermost(), | 
| Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 519 | InsertPos); | 
| John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 520 | } | 
|  | 521 |  | 
| Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 522 | return Function; | 
|  | 523 | } | 
|  | 524 |  | 
| Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 525 | Decl * | 
|  | 526 | TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D, | 
|  | 527 | TemplateParameterList *TemplateParams) { | 
| Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 528 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); | 
|  | 529 | void *InsertPos = 0; | 
| Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 530 | if (FunctionTemplate && !TemplateParams) { | 
|  | 531 | // We are creating a function template specialization from a function | 
|  | 532 | // template. Check whether there is already a function template | 
|  | 533 | // specialization for this particular set of template arguments. | 
| Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 534 | llvm::FoldingSetNodeID ID; | 
|  | 535 | FunctionTemplateSpecializationInfo::Profile(ID, | 
| Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 536 | TemplateArgs.getInnermost().getFlatArgumentList(), | 
|  | 537 | TemplateArgs.getInnermost().flat_size(), | 
| Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 538 | SemaRef.Context); | 
|  | 539 |  | 
|  | 540 | FunctionTemplateSpecializationInfo *Info | 
|  | 541 | = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID, | 
|  | 542 | InsertPos); | 
|  | 543 |  | 
|  | 544 | // If we already have a function template specialization, return it. | 
|  | 545 | if (Info) | 
|  | 546 | return Info->Function; | 
|  | 547 | } | 
|  | 548 |  | 
| Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 549 | Sema::LocalInstantiationScope Scope(SemaRef); | 
|  | 550 |  | 
| Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 551 | llvm::SmallVector<ParmVarDecl *, 4> Params; | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 552 | QualType T = SubstFunctionType(D, Params); | 
| Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 553 | if (T.isNull()) | 
|  | 554 | return 0; | 
|  | 555 |  | 
|  | 556 | // Build the instantiated method declaration. | 
|  | 557 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); | 
| Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 558 | CXXMethodDecl *Method = 0; | 
|  | 559 |  | 
|  | 560 | DeclarationName Name = D->getDeclName(); | 
| Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 561 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { | 
| Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 562 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); | 
|  | 563 | Name = SemaRef.Context.DeclarationNames.getCXXConstructorName( | 
|  | 564 | SemaRef.Context.getCanonicalType(ClassTy)); | 
|  | 565 | Method = CXXConstructorDecl::Create(SemaRef.Context, Record, | 
| Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 566 | Constructor->getLocation(), | 
| Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 567 | Name, T, | 
| Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 568 | Constructor->getDeclaratorInfo(), | 
|  | 569 | Constructor->isExplicit(), | 
|  | 570 | Constructor->isInline(), false); | 
|  | 571 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { | 
|  | 572 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); | 
|  | 573 | Name = SemaRef.Context.DeclarationNames.getCXXDestructorName( | 
|  | 574 | SemaRef.Context.getCanonicalType(ClassTy)); | 
|  | 575 | Method = CXXDestructorDecl::Create(SemaRef.Context, Record, | 
|  | 576 | Destructor->getLocation(), Name, | 
|  | 577 | T, Destructor->isInline(), false); | 
| Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 578 | } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { | 
|  | 579 | CanQualType ConvTy | 
|  | 580 | = SemaRef.Context.getCanonicalType( | 
|  | 581 | T->getAsFunctionType()->getResultType()); | 
|  | 582 | Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName( | 
|  | 583 | ConvTy); | 
|  | 584 | Method = CXXConversionDecl::Create(SemaRef.Context, Record, | 
|  | 585 | Conversion->getLocation(), Name, | 
|  | 586 | T, Conversion->getDeclaratorInfo(), | 
|  | 587 | Conversion->isInline(), | 
|  | 588 | Conversion->isExplicit()); | 
| Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 589 | } else { | 
|  | 590 | Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(), | 
|  | 591 | D->getDeclName(), T, D->getDeclaratorInfo(), | 
|  | 592 | D->isStatic(), D->isInline()); | 
|  | 593 | } | 
| Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 594 |  | 
| Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 595 | if (TemplateParams) { | 
|  | 596 | // Our resulting instantiation is actually a function template, since we | 
|  | 597 | // are substituting only the outer template parameters. For example, given | 
|  | 598 | // | 
|  | 599 | //   template<typename T> | 
|  | 600 | //   struct X { | 
|  | 601 | //     template<typename U> void f(T, U); | 
|  | 602 | //   }; | 
|  | 603 | // | 
|  | 604 | //   X<int> x; | 
|  | 605 | // | 
|  | 606 | // We are instantiating the member template "f" within X<int>, which means | 
|  | 607 | // substituting int for T, but leaving "f" as a member function template. | 
|  | 608 | // Build the function template itself. | 
|  | 609 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, | 
|  | 610 | Method->getLocation(), | 
|  | 611 | Method->getDeclName(), | 
|  | 612 | TemplateParams, Method); | 
|  | 613 | if (D->isOutOfLine()) | 
|  | 614 | FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); | 
|  | 615 | Method->setDescribedFunctionTemplate(FunctionTemplate); | 
|  | 616 | } else if (!FunctionTemplate) | 
| Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 617 | Method->setInstantiationOfMemberFunction(D); | 
| Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 618 |  | 
| Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 619 | // If we are instantiating a member function defined | 
|  | 620 | // out-of-line, the instantiation will have the same lexical | 
|  | 621 | // context (which will be a namespace scope) as the template. | 
|  | 622 | if (D->isOutOfLine()) | 
|  | 623 | Method->setLexicalDeclContext(D->getLexicalDeclContext()); | 
|  | 624 |  | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 625 | // Attach the parameters | 
|  | 626 | for (unsigned P = 0; P < Params.size(); ++P) | 
|  | 627 | Params[P]->setOwningFunction(Method); | 
| Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 628 | Method->setParams(SemaRef.Context, Params.data(), Params.size()); | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 629 |  | 
|  | 630 | if (InitMethodInstantiation(Method, D)) | 
|  | 631 | Method->setInvalidDecl(); | 
| Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 632 |  | 
| Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 633 | NamedDecl *PrevDecl = 0; | 
|  | 634 |  | 
| Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 635 | if (!FunctionTemplate || TemplateParams) { | 
| Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 636 | PrevDecl = SemaRef.LookupQualifiedName(Owner, Name, | 
|  | 637 | Sema::LookupOrdinaryName, true); | 
|  | 638 |  | 
|  | 639 | // In C++, the previous declaration we find might be a tag type | 
|  | 640 | // (class or enum). In this case, the new declaration will hide the | 
|  | 641 | // tag type. Note that this does does not apply if we're declaring a | 
|  | 642 | // typedef (C++ [dcl.typedef]p4). | 
|  | 643 | if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) | 
|  | 644 | PrevDecl = 0; | 
|  | 645 | } | 
| Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 646 |  | 
| Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 647 | if (FunctionTemplate && !TemplateParams) | 
| Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 648 | // Record this function template specialization. | 
|  | 649 | Method->setFunctionTemplateSpecialization(SemaRef.Context, | 
|  | 650 | FunctionTemplate, | 
| Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 651 | &TemplateArgs.getInnermost(), | 
| Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 652 | InsertPos); | 
| Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 653 |  | 
|  | 654 | bool Redeclaration = false; | 
|  | 655 | bool OverloadableAttrRequired = false; | 
|  | 656 | SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration, | 
|  | 657 | /*FIXME:*/OverloadableAttrRequired); | 
|  | 658 |  | 
|  | 659 | if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl)) | 
| Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 660 | Owner->addDecl(Method); | 
|  | 661 |  | 
| Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 662 | return Method; | 
|  | 663 | } | 
|  | 664 |  | 
| Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 665 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { | 
| Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 666 | return VisitCXXMethodDecl(D); | 
| Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 667 | } | 
|  | 668 |  | 
| Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 669 | Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { | 
| Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 670 | return VisitCXXMethodDecl(D); | 
| Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 671 | } | 
|  | 672 |  | 
| Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 673 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { | 
| Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 674 | return VisitCXXMethodDecl(D); | 
| Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 675 | } | 
|  | 676 |  | 
| Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 677 | ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 678 | QualType OrigT = SemaRef.SubstType(D->getOriginalType(), TemplateArgs, | 
| Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 679 | D->getLocation(), D->getDeclName()); | 
| Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 680 | if (OrigT.isNull()) | 
|  | 681 | return 0; | 
|  | 682 |  | 
|  | 683 | QualType T = SemaRef.adjustParameterType(OrigT); | 
|  | 684 |  | 
| Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 685 | // Allocate the parameter | 
|  | 686 | ParmVarDecl *Param = 0; | 
|  | 687 | if (T == OrigT) | 
|  | 688 | Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(), | 
| Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 689 | D->getIdentifier(), T, D->getDeclaratorInfo(), | 
|  | 690 | D->getStorageClass(), 0); | 
| Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 691 | else | 
|  | 692 | Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner, | 
|  | 693 | D->getLocation(), D->getIdentifier(), | 
| Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 694 | T, D->getDeclaratorInfo(), OrigT, | 
|  | 695 | D->getStorageClass(), 0); | 
| Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 696 |  | 
| Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 697 | // Mark the default argument as being uninstantiated. | 
|  | 698 | if (Expr *Arg = D->getDefaultArg()) | 
|  | 699 | Param->setUninstantiatedDefaultArg(Arg); | 
|  | 700 |  | 
| Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 701 | // Note: we don't try to instantiate function parameters until after | 
|  | 702 | // we've instantiated the function's type. Therefore, we don't have | 
|  | 703 | // to check for 'void' parameter types here. | 
| Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 704 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); | 
| Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 705 | return Param; | 
|  | 706 | } | 
|  | 707 |  | 
|  | 708 | Decl * | 
|  | 709 | TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) { | 
|  | 710 | // Since parameter types can decay either before or after | 
|  | 711 | // instantiation, we simply treat OriginalParmVarDecls as | 
|  | 712 | // ParmVarDecls the same way, and create one or the other depending | 
|  | 713 | // on what happens after template instantiation. | 
|  | 714 | return VisitParmVarDecl(D); | 
|  | 715 | } | 
|  | 716 |  | 
| John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 717 | Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( | 
|  | 718 | TemplateTypeParmDecl *D) { | 
|  | 719 | // TODO: don't always clone when decls are refcounted. | 
|  | 720 | const Type* T = D->getTypeForDecl(); | 
|  | 721 | assert(T->isTemplateTypeParmType()); | 
|  | 722 | const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>(); | 
|  | 723 |  | 
|  | 724 | TemplateTypeParmDecl *Inst = | 
|  | 725 | TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(), | 
|  | 726 | TTPT->getDepth(), TTPT->getIndex(), | 
|  | 727 | TTPT->getName(), | 
|  | 728 | D->wasDeclaredWithTypename(), | 
|  | 729 | D->isParameterPack()); | 
|  | 730 |  | 
|  | 731 | if (D->hasDefaultArgument()) { | 
|  | 732 | QualType DefaultPattern = D->getDefaultArgument(); | 
|  | 733 | QualType DefaultInst | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 734 | = SemaRef.SubstType(DefaultPattern, TemplateArgs, | 
|  | 735 | D->getDefaultArgumentLoc(), | 
|  | 736 | D->getDeclName()); | 
| John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 737 |  | 
|  | 738 | Inst->setDefaultArgument(DefaultInst, | 
|  | 739 | D->getDefaultArgumentLoc(), | 
|  | 740 | D->defaultArgumentWasInherited() /* preserve? */); | 
|  | 741 | } | 
|  | 742 |  | 
|  | 743 | return Inst; | 
|  | 744 | } | 
|  | 745 |  | 
| Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 746 | Decl * | 
|  | 747 | TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) { | 
|  | 748 | NestedNameSpecifier *NNS = | 
|  | 749 | SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(), | 
|  | 750 | D->getTargetNestedNameRange(), | 
|  | 751 | TemplateArgs); | 
|  | 752 | if (!NNS) | 
|  | 753 | return 0; | 
|  | 754 |  | 
|  | 755 | CXXScopeSpec SS; | 
|  | 756 | SS.setRange(D->getTargetNestedNameRange()); | 
|  | 757 | SS.setScopeRep(NNS); | 
|  | 758 |  | 
|  | 759 | return SemaRef.BuildUsingDeclaration(D->getLocation(), SS, | 
|  | 760 | D->getTargetNameLocation(), | 
|  | 761 | D->getTargetName(), 0, D->isTypeName()); | 
|  | 762 | } | 
|  | 763 |  | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 764 | Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner, | 
| Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 765 | const MultiLevelTemplateArgumentList &TemplateArgs) { | 
| Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 766 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); | 
| Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 767 | return Instantiator.Visit(D); | 
|  | 768 | } | 
|  | 769 |  | 
| John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 770 | /// \brief Instantiates a nested template parameter list in the current | 
|  | 771 | /// instantiation context. | 
|  | 772 | /// | 
|  | 773 | /// \param L The parameter list to instantiate | 
|  | 774 | /// | 
|  | 775 | /// \returns NULL if there was an error | 
|  | 776 | TemplateParameterList * | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 777 | TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { | 
| John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 778 | // Get errors for all the parameters before bailing out. | 
|  | 779 | bool Invalid = false; | 
|  | 780 |  | 
|  | 781 | unsigned N = L->size(); | 
|  | 782 | typedef llvm::SmallVector<Decl*,8> ParamVector; | 
|  | 783 | ParamVector Params; | 
|  | 784 | Params.reserve(N); | 
|  | 785 | for (TemplateParameterList::iterator PI = L->begin(), PE = L->end(); | 
|  | 786 | PI != PE; ++PI) { | 
|  | 787 | Decl *D = Visit(*PI); | 
|  | 788 | Params.push_back(D); | 
|  | 789 | Invalid = Invalid || !D; | 
|  | 790 | } | 
|  | 791 |  | 
|  | 792 | // Clean up if we had an error. | 
|  | 793 | if (Invalid) { | 
|  | 794 | for (ParamVector::iterator PI = Params.begin(), PE = Params.end(); | 
|  | 795 | PI != PE; ++PI) | 
|  | 796 | if (*PI) | 
|  | 797 | (*PI)->Destroy(SemaRef.Context); | 
|  | 798 | return NULL; | 
|  | 799 | } | 
|  | 800 |  | 
|  | 801 | TemplateParameterList *InstL | 
|  | 802 | = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), | 
|  | 803 | L->getLAngleLoc(), &Params.front(), N, | 
|  | 804 | L->getRAngleLoc()); | 
|  | 805 | return InstL; | 
|  | 806 | } | 
|  | 807 |  | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 808 | /// \brief Does substitution on the type of the given function, including | 
|  | 809 | /// all of the function parameters. | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 810 | /// | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 811 | /// \param D The function whose type will be the basis of the substitution | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 812 | /// | 
|  | 813 | /// \param Params the instantiated parameter declarations | 
|  | 814 |  | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 815 | /// \returns the instantiated function's type if successful, a NULL | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 816 | /// type if there was an error. | 
|  | 817 | QualType | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 818 | TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 819 | llvm::SmallVectorImpl<ParmVarDecl *> &Params) { | 
|  | 820 | bool InvalidDecl = false; | 
|  | 821 |  | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 822 | // Substitute all of the function's formal parameter types. | 
| Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 823 | TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs); | 
| Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 824 | llvm::SmallVector<QualType, 4> ParamTys; | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 825 | for (FunctionDecl::param_iterator P = D->param_begin(), | 
|  | 826 | PEnd = D->param_end(); | 
|  | 827 | P != PEnd; ++P) { | 
| Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 828 | if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) { | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 829 | if (PInst->getType()->isVoidType()) { | 
|  | 830 | SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type); | 
|  | 831 | PInst->setInvalidDecl(); | 
| Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 832 | } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(), | 
|  | 833 | PInst->getType(), | 
|  | 834 | diag::err_abstract_type_in_decl, | 
|  | 835 | Sema::AbstractParamType)) | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 836 | PInst->setInvalidDecl(); | 
|  | 837 |  | 
|  | 838 | Params.push_back(PInst); | 
|  | 839 | ParamTys.push_back(PInst->getType()); | 
|  | 840 |  | 
|  | 841 | if (PInst->isInvalidDecl()) | 
|  | 842 | InvalidDecl = true; | 
|  | 843 | } else | 
|  | 844 | InvalidDecl = true; | 
|  | 845 | } | 
|  | 846 |  | 
|  | 847 | // FIXME: Deallocate dead declarations. | 
|  | 848 | if (InvalidDecl) | 
|  | 849 | return QualType(); | 
|  | 850 |  | 
|  | 851 | const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType(); | 
|  | 852 | assert(Proto && "Missing prototype?"); | 
|  | 853 | QualType ResultType | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 854 | = SemaRef.SubstType(Proto->getResultType(), TemplateArgs, | 
|  | 855 | D->getLocation(), D->getDeclName()); | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 856 | if (ResultType.isNull()) | 
|  | 857 | return QualType(); | 
|  | 858 |  | 
| Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 859 | return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(), | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 860 | Proto->isVariadic(), Proto->getTypeQuals(), | 
|  | 861 | D->getLocation(), D->getDeclName()); | 
|  | 862 | } | 
|  | 863 |  | 
| Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 864 | /// \brief Initializes the common fields of an instantiation function | 
|  | 865 | /// declaration (New) from the corresponding fields of its template (Tmpl). | 
|  | 866 | /// | 
|  | 867 | /// \returns true if there was an error | 
|  | 868 | bool | 
|  | 869 | TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, | 
|  | 870 | FunctionDecl *Tmpl) { | 
|  | 871 | if (Tmpl->isDeleted()) | 
|  | 872 | New->setDeleted(); | 
| Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 873 |  | 
|  | 874 | // If we are performing substituting explicitly-specified template arguments | 
|  | 875 | // or deduced template arguments into a function template and we reach this | 
|  | 876 | // point, we are now past the point where SFINAE applies and have committed | 
|  | 877 | // to keeping the new function template specialization. We therefore | 
|  | 878 | // convert the active template instantiation for the function template | 
|  | 879 | // into a template instantiation for this specific function template | 
|  | 880 | // specialization, which is not a SFINAE context, so that we diagnose any | 
|  | 881 | // further errors in the declaration itself. | 
|  | 882 | typedef Sema::ActiveTemplateInstantiation ActiveInstType; | 
|  | 883 | ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back(); | 
|  | 884 | if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || | 
|  | 885 | ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { | 
|  | 886 | if (FunctionTemplateDecl *FunTmpl | 
|  | 887 | = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) { | 
|  | 888 | assert(FunTmpl->getTemplatedDecl() == Tmpl && | 
|  | 889 | "Deduction from the wrong function template?"); | 
| Daniel Dunbar | bcbb8bd | 2009-07-16 22:10:11 +0000 | [diff] [blame] | 890 | (void) FunTmpl; | 
| Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 891 | ActiveInst.Kind = ActiveInstType::TemplateInstantiation; | 
|  | 892 | ActiveInst.Entity = reinterpret_cast<uintptr_t>(New); | 
|  | 893 | } | 
|  | 894 | } | 
|  | 895 |  | 
| Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 896 | return false; | 
|  | 897 | } | 
|  | 898 |  | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 899 | /// \brief Initializes common fields of an instantiated method | 
|  | 900 | /// declaration (New) from the corresponding fields of its template | 
|  | 901 | /// (Tmpl). | 
|  | 902 | /// | 
|  | 903 | /// \returns true if there was an error | 
|  | 904 | bool | 
|  | 905 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, | 
|  | 906 | CXXMethodDecl *Tmpl) { | 
| Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 907 | if (InitFunctionInstantiation(New, Tmpl)) | 
|  | 908 | return true; | 
|  | 909 |  | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 910 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); | 
|  | 911 | New->setAccess(Tmpl->getAccess()); | 
| Anders Carlsson | 77b7f1d | 2009-05-14 22:15:41 +0000 | [diff] [blame] | 912 | if (Tmpl->isVirtualAsWritten()) { | 
|  | 913 | New->setVirtualAsWritten(true); | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 914 | Record->setAggregate(false); | 
|  | 915 | Record->setPOD(false); | 
| Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 916 | Record->setEmpty(false); | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 917 | Record->setPolymorphic(true); | 
|  | 918 | } | 
| Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 919 | if (Tmpl->isPure()) { | 
|  | 920 | New->setPure(); | 
|  | 921 | Record->setAbstract(true); | 
|  | 922 | } | 
|  | 923 |  | 
|  | 924 | // FIXME: attributes | 
|  | 925 | // FIXME: New needs a pointer to Tmpl | 
|  | 926 | return false; | 
|  | 927 | } | 
| Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 928 |  | 
|  | 929 | /// \brief Instantiate the definition of the given function from its | 
|  | 930 | /// template. | 
|  | 931 | /// | 
| Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 932 | /// \param PointOfInstantiation the point at which the instantiation was | 
|  | 933 | /// required. Note that this is not precisely a "point of instantiation" | 
|  | 934 | /// for the function, but it's close. | 
|  | 935 | /// | 
| Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 936 | /// \param Function the already-instantiated declaration of a | 
| Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 937 | /// function template specialization or member function of a class template | 
|  | 938 | /// specialization. | 
|  | 939 | /// | 
|  | 940 | /// \param Recursive if true, recursively instantiates any functions that | 
|  | 941 | /// are required by this instantiation. | 
| Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 942 | void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, | 
| Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 943 | FunctionDecl *Function, | 
|  | 944 | bool Recursive) { | 
| Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 945 | if (Function->isInvalidDecl()) | 
|  | 946 | return; | 
|  | 947 |  | 
| Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 948 | assert(!Function->getBody() && "Already instantiated!"); | 
| Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 949 |  | 
| Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 950 | // Find the function body that we'll be substituting. | 
| Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 951 | const FunctionDecl *PatternDecl = 0; | 
| Douglas Gregor | 5ec178f | 2009-08-28 21:09:48 +0000 | [diff] [blame] | 952 | if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate()) { | 
|  | 953 | while (Primary->getInstantiatedFromMemberTemplate()) | 
|  | 954 | Primary = Primary->getInstantiatedFromMemberTemplate(); | 
|  | 955 |  | 
| Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 956 | PatternDecl = Primary->getTemplatedDecl(); | 
| Douglas Gregor | 5ec178f | 2009-08-28 21:09:48 +0000 | [diff] [blame] | 957 | } else | 
| Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 958 | PatternDecl = Function->getInstantiatedFromMemberFunction(); | 
| Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 959 | Stmt *Pattern = 0; | 
|  | 960 | if (PatternDecl) | 
| Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 961 | Pattern = PatternDecl->getBody(PatternDecl); | 
| Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 962 |  | 
|  | 963 | if (!Pattern) | 
|  | 964 | return; | 
|  | 965 |  | 
| Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 966 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); | 
|  | 967 | if (Inst) | 
|  | 968 | return; | 
| Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 969 |  | 
| Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 970 | // If we're performing recursive template instantiation, create our own | 
|  | 971 | // queue of pending implicit instantiations that we will instantiate later, | 
|  | 972 | // while we're still within our own instantiation context. | 
|  | 973 | std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations; | 
|  | 974 | if (Recursive) | 
|  | 975 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); | 
|  | 976 |  | 
| Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 977 | ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function)); | 
|  | 978 |  | 
| Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 979 | // Introduce a new scope where local variable instantiations will be | 
|  | 980 | // recorded. | 
|  | 981 | LocalInstantiationScope Scope(*this); | 
|  | 982 |  | 
|  | 983 | // Introduce the instantiated function parameters into the local | 
|  | 984 | // instantiation scope. | 
|  | 985 | for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) | 
|  | 986 | Scope.InstantiatedLocal(PatternDecl->getParamDecl(I), | 
|  | 987 | Function->getParamDecl(I)); | 
|  | 988 |  | 
| Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 989 | // Enter the scope of this instantiation. We don't use | 
|  | 990 | // PushDeclContext because we don't have a scope. | 
|  | 991 | DeclContext *PreviousContext = CurContext; | 
|  | 992 | CurContext = Function; | 
|  | 993 |  | 
| Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 994 | // Instantiate the function body. | 
|  | 995 | OwningStmtResult Body | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 996 | = SubstStmt(Pattern, getTemplateInstantiationArgs(Function)); | 
| Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 997 |  | 
|  | 998 | ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body), | 
|  | 999 | /*IsInstantiation=*/true); | 
| Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 1000 |  | 
|  | 1001 | CurContext = PreviousContext; | 
| Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 1002 |  | 
|  | 1003 | DeclGroupRef DG(Function); | 
|  | 1004 | Consumer.HandleTopLevelDecl(DG); | 
| Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1005 |  | 
|  | 1006 | if (Recursive) { | 
|  | 1007 | // Instantiate any pending implicit instantiations found during the | 
|  | 1008 | // instantiation of this template. | 
|  | 1009 | PerformPendingImplicitInstantiations(); | 
|  | 1010 |  | 
|  | 1011 | // Restore the set of pending implicit instantiations. | 
|  | 1012 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); | 
|  | 1013 | } | 
| Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1014 | } | 
|  | 1015 |  | 
|  | 1016 | /// \brief Instantiate the definition of the given variable from its | 
|  | 1017 | /// template. | 
|  | 1018 | /// | 
| Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1019 | /// \param PointOfInstantiation the point at which the instantiation was | 
|  | 1020 | /// required. Note that this is not precisely a "point of instantiation" | 
|  | 1021 | /// for the function, but it's close. | 
|  | 1022 | /// | 
|  | 1023 | /// \param Var the already-instantiated declaration of a static member | 
|  | 1024 | /// variable of a class template specialization. | 
|  | 1025 | /// | 
|  | 1026 | /// \param Recursive if true, recursively instantiates any functions that | 
|  | 1027 | /// are required by this instantiation. | 
|  | 1028 | void Sema::InstantiateStaticDataMemberDefinition( | 
|  | 1029 | SourceLocation PointOfInstantiation, | 
|  | 1030 | VarDecl *Var, | 
|  | 1031 | bool Recursive) { | 
|  | 1032 | if (Var->isInvalidDecl()) | 
|  | 1033 | return; | 
|  | 1034 |  | 
|  | 1035 | // Find the out-of-line definition of this static data member. | 
|  | 1036 | // FIXME: Do we have to look for specializations separately? | 
|  | 1037 | VarDecl *Def = Var->getInstantiatedFromStaticDataMember(); | 
|  | 1038 | bool FoundOutOfLineDef = false; | 
|  | 1039 | assert(Def && "This data member was not instantiated from a template?"); | 
|  | 1040 | assert(Def->isStaticDataMember() && "Not a static data member?"); | 
|  | 1041 | for (VarDecl::redecl_iterator RD = Def->redecls_begin(), | 
|  | 1042 | RDEnd = Def->redecls_end(); | 
|  | 1043 | RD != RDEnd; ++RD) { | 
|  | 1044 | if (RD->getLexicalDeclContext()->isFileContext()) { | 
|  | 1045 | Def = *RD; | 
|  | 1046 | FoundOutOfLineDef = true; | 
|  | 1047 | } | 
|  | 1048 | } | 
|  | 1049 |  | 
|  | 1050 | if (!FoundOutOfLineDef) { | 
|  | 1051 | // We did not find an out-of-line definition of this static data member, | 
|  | 1052 | // so we won't perform any instantiation. Rather, we rely on the user to | 
|  | 1053 | // instantiate this definition (or provide a specialization for it) in | 
|  | 1054 | // another translation unit. | 
|  | 1055 | return; | 
|  | 1056 | } | 
|  | 1057 |  | 
|  | 1058 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); | 
|  | 1059 | if (Inst) | 
|  | 1060 | return; | 
|  | 1061 |  | 
|  | 1062 | // If we're performing recursive template instantiation, create our own | 
|  | 1063 | // queue of pending implicit instantiations that we will instantiate later, | 
|  | 1064 | // while we're still within our own instantiation context. | 
|  | 1065 | std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations; | 
|  | 1066 | if (Recursive) | 
|  | 1067 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); | 
|  | 1068 |  | 
|  | 1069 | // Enter the scope of this instantiation. We don't use | 
|  | 1070 | // PushDeclContext because we don't have a scope. | 
|  | 1071 | DeclContext *PreviousContext = CurContext; | 
|  | 1072 | CurContext = Var->getDeclContext(); | 
|  | 1073 |  | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1074 | Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), | 
| Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1075 | getTemplateInstantiationArgs(Var))); | 
| Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1076 |  | 
|  | 1077 | CurContext = PreviousContext; | 
|  | 1078 |  | 
|  | 1079 | if (Var) { | 
|  | 1080 | DeclGroupRef DG(Var); | 
|  | 1081 | Consumer.HandleTopLevelDecl(DG); | 
|  | 1082 | } | 
|  | 1083 |  | 
|  | 1084 | if (Recursive) { | 
|  | 1085 | // Instantiate any pending implicit instantiations found during the | 
|  | 1086 | // instantiation of this template. | 
|  | 1087 | PerformPendingImplicitInstantiations(); | 
|  | 1088 |  | 
|  | 1089 | // Restore the set of pending implicit instantiations. | 
|  | 1090 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); | 
|  | 1091 | } | 
| Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1092 | } | 
| Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1093 |  | 
|  | 1094 | static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { | 
|  | 1095 | if (D->getKind() != Other->getKind()) | 
|  | 1096 | return false; | 
|  | 1097 |  | 
| Douglas Gregor | a5bf7f1 | 2009-08-28 22:03:51 +0000 | [diff] [blame] | 1098 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other)) { | 
|  | 1099 | if (CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass()) | 
|  | 1100 | return Pattern->getCanonicalDecl() == D->getCanonicalDecl(); | 
|  | 1101 | else | 
|  | 1102 | return false; | 
|  | 1103 | } | 
|  | 1104 |  | 
|  | 1105 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other)) { | 
|  | 1106 | if (FunctionDecl *Pattern = Function->getInstantiatedFromMemberFunction()) | 
|  | 1107 | return Pattern->getCanonicalDecl() == D->getCanonicalDecl(); | 
|  | 1108 | else | 
|  | 1109 | return false; | 
|  | 1110 | } | 
| Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1111 |  | 
| Douglas Gregor | a5bf7f1 | 2009-08-28 22:03:51 +0000 | [diff] [blame] | 1112 | if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other)) { | 
|  | 1113 | if (EnumDecl *Pattern = Enum->getInstantiatedFromMemberEnum()) | 
|  | 1114 | return Pattern->getCanonicalDecl() == D->getCanonicalDecl(); | 
|  | 1115 | else | 
|  | 1116 | return false; | 
|  | 1117 | } | 
| Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1118 |  | 
| Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1119 | if (VarDecl *Var = dyn_cast<VarDecl>(Other)) | 
| Douglas Gregor | a5bf7f1 | 2009-08-28 22:03:51 +0000 | [diff] [blame] | 1120 | if (Var->isStaticDataMember()) { | 
|  | 1121 | if (VarDecl *Pattern = Var->getInstantiatedFromStaticDataMember()) | 
|  | 1122 | return Pattern->getCanonicalDecl() == D->getCanonicalDecl(); | 
|  | 1123 | else | 
|  | 1124 | return false; | 
|  | 1125 | } | 
|  | 1126 |  | 
| Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1127 | // FIXME: How can we find instantiations of anonymous unions? | 
|  | 1128 |  | 
|  | 1129 | return D->getDeclName() && isa<NamedDecl>(Other) && | 
|  | 1130 | D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); | 
|  | 1131 | } | 
|  | 1132 |  | 
|  | 1133 | template<typename ForwardIterator> | 
|  | 1134 | static NamedDecl *findInstantiationOf(ASTContext &Ctx, | 
|  | 1135 | NamedDecl *D, | 
|  | 1136 | ForwardIterator first, | 
|  | 1137 | ForwardIterator last) { | 
|  | 1138 | for (; first != last; ++first) | 
|  | 1139 | if (isInstantiationOf(Ctx, D, *first)) | 
|  | 1140 | return cast<NamedDecl>(*first); | 
|  | 1141 |  | 
|  | 1142 | return 0; | 
|  | 1143 | } | 
|  | 1144 |  | 
| John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1145 | /// \brief Finds the instantiation of the given declaration context | 
|  | 1146 | /// within the current instantiation. | 
|  | 1147 | /// | 
|  | 1148 | /// \returns NULL if there was an error | 
|  | 1149 | DeclContext *Sema::FindInstantiatedContext(DeclContext* DC) { | 
|  | 1150 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) { | 
|  | 1151 | Decl* ID = FindInstantiatedDecl(D); | 
|  | 1152 | return cast_or_null<DeclContext>(ID); | 
|  | 1153 | } else return DC; | 
|  | 1154 | } | 
|  | 1155 |  | 
| Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 1156 | /// \brief Find the instantiation of the given declaration within the | 
|  | 1157 | /// current instantiation. | 
| Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1158 | /// | 
|  | 1159 | /// This routine is intended to be used when \p D is a declaration | 
|  | 1160 | /// referenced from within a template, that needs to mapped into the | 
|  | 1161 | /// corresponding declaration within an instantiation. For example, | 
|  | 1162 | /// given: | 
|  | 1163 | /// | 
|  | 1164 | /// \code | 
|  | 1165 | /// template<typename T> | 
|  | 1166 | /// struct X { | 
|  | 1167 | ///   enum Kind { | 
|  | 1168 | ///     KnownValue = sizeof(T) | 
|  | 1169 | ///   }; | 
|  | 1170 | /// | 
|  | 1171 | ///   bool getKind() const { return KnownValue; } | 
|  | 1172 | /// }; | 
|  | 1173 | /// | 
|  | 1174 | /// template struct X<int>; | 
|  | 1175 | /// \endcode | 
|  | 1176 | /// | 
|  | 1177 | /// In the instantiation of X<int>::getKind(), we need to map the | 
|  | 1178 | /// EnumConstantDecl for KnownValue (which refers to | 
|  | 1179 | /// X<T>::<Kind>::KnownValue) to its instantiation | 
| Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 1180 | /// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs | 
|  | 1181 | /// this mapping from within the instantiation of X<int>. | 
| John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1182 | NamedDecl * Sema::FindInstantiatedDecl(NamedDecl *D) { | 
| Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1183 | DeclContext *ParentDC = D->getDeclContext(); | 
| Douglas Gregor | 2bba76b | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 1184 | if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) { | 
|  | 1185 | // D is a local of some kind. Look into the map of local | 
|  | 1186 | // declarations to their instantiations. | 
|  | 1187 | return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D)); | 
|  | 1188 | } | 
| Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1189 |  | 
| John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1190 | ParentDC = FindInstantiatedContext(ParentDC); | 
|  | 1191 | if (!ParentDC) return 0; | 
| Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1192 |  | 
| Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1193 | if (ParentDC != D->getDeclContext()) { | 
|  | 1194 | // We performed some kind of instantiation in the parent context, | 
|  | 1195 | // so now we need to look into the instantiated parent context to | 
|  | 1196 | // find the instantiation of the declaration D. | 
|  | 1197 | NamedDecl *Result = 0; | 
|  | 1198 | if (D->getDeclName()) { | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1199 | DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName()); | 
| Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1200 | Result = findInstantiationOf(Context, D, Found.first, Found.second); | 
|  | 1201 | } else { | 
|  | 1202 | // Since we don't have a name for the entity we're looking for, | 
|  | 1203 | // our only option is to walk through all of the declarations to | 
|  | 1204 | // find that name. This will occur in a few cases: | 
|  | 1205 | // | 
|  | 1206 | //   - anonymous struct/union within a template | 
|  | 1207 | //   - unnamed class/struct/union/enum within a template | 
|  | 1208 | // | 
|  | 1209 | // FIXME: Find a better way to find these instantiations! | 
|  | 1210 | Result = findInstantiationOf(Context, D, | 
| Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1211 | ParentDC->decls_begin(), | 
|  | 1212 | ParentDC->decls_end()); | 
| Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1213 | } | 
|  | 1214 | assert(Result && "Unable to find instantiation of declaration!"); | 
|  | 1215 | D = Result; | 
|  | 1216 | } | 
|  | 1217 |  | 
| Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1218 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) | 
| Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 1219 | if (ClassTemplateDecl *ClassTemplate | 
|  | 1220 | = Record->getDescribedClassTemplate()) { | 
|  | 1221 | // When the declaration D was parsed, it referred to the current | 
|  | 1222 | // instantiation. Therefore, look through the current context, | 
|  | 1223 | // which contains actual instantiations, to find the | 
|  | 1224 | // instantiation of the "current instantiation" that D refers | 
|  | 1225 | // to. Alternatively, we could just instantiate the | 
|  | 1226 | // injected-class-name with the current template arguments, but | 
|  | 1227 | // such an instantiation is far more expensive. | 
|  | 1228 | for (DeclContext *DC = CurContext; !DC->isFileContext(); | 
|  | 1229 | DC = DC->getParent()) { | 
|  | 1230 | if (ClassTemplateSpecializationDecl *Spec | 
|  | 1231 | = dyn_cast<ClassTemplateSpecializationDecl>(DC)) | 
| Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 1232 | if (Spec->getSpecializedTemplate()->getCanonicalDecl() | 
|  | 1233 | == ClassTemplate->getCanonicalDecl()) | 
| Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 1234 | return Spec; | 
|  | 1235 | } | 
|  | 1236 |  | 
|  | 1237 | assert(false && | 
|  | 1238 | "Unable to find declaration for the current instantiation"); | 
| Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1239 | } | 
|  | 1240 |  | 
|  | 1241 | return D; | 
|  | 1242 | } | 
| Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1243 |  | 
|  | 1244 | /// \brief Performs template instantiation for all implicit template | 
|  | 1245 | /// instantiations we have seen until this point. | 
|  | 1246 | void Sema::PerformPendingImplicitInstantiations() { | 
|  | 1247 | while (!PendingImplicitInstantiations.empty()) { | 
|  | 1248 | PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front(); | 
| Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1249 | PendingImplicitInstantiations.pop_front(); | 
| Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1250 |  | 
| Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1251 | // Instantiate function definitions | 
|  | 1252 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) { | 
| Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1253 | if (!Function->getBody()) | 
| Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1254 | InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true); | 
| Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1255 | continue; | 
|  | 1256 | } | 
| Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1257 |  | 
| Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1258 | // Instantiate static data member definitions. | 
|  | 1259 | VarDecl *Var = cast<VarDecl>(Inst.first); | 
|  | 1260 | assert(Var->isStaticDataMember() && "Not a static data member?"); | 
|  | 1261 | InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true); | 
| Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1262 | } | 
|  | 1263 | } |