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" |
Anders Carlsson | c17fb7b | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 18 | #include "clang/Basic/PrettyStackTrace.h" |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
| 21 | |
| 22 | using namespace clang; |
| 23 | |
| 24 | namespace { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 25 | class VISIBILITY_HIDDEN TemplateDeclInstantiator |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 26 | : public DeclVisitor<TemplateDeclInstantiator, Decl *> { |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 27 | Sema &SemaRef; |
| 28 | DeclContext *Owner; |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 29 | const MultiLevelTemplateArgumentList &TemplateArgs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 31 | public: |
| 32 | typedef Sema::OwningExprResult OwningExprResult; |
| 33 | |
| 34 | TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 35 | const MultiLevelTemplateArgumentList &TemplateArgs) |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 36 | : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 37 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 38 | // FIXME: Once we get closer to completion, replace these manually-written |
| 39 | // declarations with automatically-generated ones from |
| 40 | // clang/AST/DeclNodes.def. |
Douglas Gregor | 4f722be | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 41 | Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 42 | Decl *VisitNamespaceDecl(NamespaceDecl *D); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 43 | Decl *VisitTypedefDecl(TypedefDecl *D); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 44 | Decl *VisitVarDecl(VarDecl *D); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 45 | Decl *VisitFieldDecl(FieldDecl *D); |
| 46 | Decl *VisitStaticAssertDecl(StaticAssertDecl *D); |
| 47 | Decl *VisitEnumDecl(EnumDecl *D); |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 48 | Decl *VisitEnumConstantDecl(EnumConstantDecl *D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 49 | Decl *VisitFriendDecl(FriendDecl *D); |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 50 | Decl *VisitFunctionDecl(FunctionDecl *D, |
| 51 | TemplateParameterList *TemplateParams = 0); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 52 | Decl *VisitCXXRecordDecl(CXXRecordDecl *D); |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 53 | Decl *VisitCXXMethodDecl(CXXMethodDecl *D, |
| 54 | TemplateParameterList *TemplateParams = 0); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 55 | Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 56 | Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 57 | Decl *VisitCXXConversionDecl(CXXConversionDecl *D); |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 58 | ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 59 | Decl *VisitClassTemplateDecl(ClassTemplateDecl *D); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 60 | Decl *VisitClassTemplatePartialSpecializationDecl( |
| 61 | ClassTemplatePartialSpecializationDecl *D); |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 62 | Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 63 | Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 64 | Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 65 | Decl *VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 67 | // Base case. FIXME: Remove once we can instantiate everything. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 68 | Decl *VisitDecl(Decl *) { |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 69 | assert(false && "Template instantiation of unknown declaration kind!"); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 70 | return 0; |
| 71 | } |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 72 | |
John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 73 | const LangOptions &getLangOptions() { |
| 74 | return SemaRef.getLangOptions(); |
| 75 | } |
| 76 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 77 | // Helper functions for instantiating methods. |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 78 | QualType SubstFunctionType(FunctionDecl *D, |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 79 | llvm::SmallVectorImpl<ParmVarDecl *> &Params); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 80 | bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 81 | bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 82 | |
| 83 | TemplateParameterList * |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 84 | SubstTemplateParams(TemplateParameterList *List); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 85 | }; |
| 86 | } |
| 87 | |
Douglas Gregor | 4f722be | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 88 | Decl * |
| 89 | TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 90 | assert(false && "Translation units cannot be instantiated"); |
| 91 | return D; |
| 92 | } |
| 93 | |
| 94 | Decl * |
| 95 | TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { |
| 96 | assert(false && "Namespaces cannot be instantiated"); |
| 97 | return D; |
| 98 | } |
| 99 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 100 | Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { |
| 101 | bool Invalid = false; |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 102 | DeclaratorInfo *DI = D->getTypeDeclaratorInfo(); |
| 103 | if (DI->getType()->isDependentType()) { |
| 104 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 105 | D->getLocation(), D->getDeclName()); |
| 106 | if (!DI) { |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 107 | Invalid = true; |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 108 | DI = SemaRef.Context.getTrivialDeclaratorInfo(SemaRef.Context.IntTy); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 112 | // Create the new typedef |
| 113 | TypedefDecl *Typedef |
| 114 | = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 115 | D->getIdentifier(), DI); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 116 | if (Invalid) |
| 117 | Typedef->setInvalidDecl(); |
| 118 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 119 | Owner->addDecl(Typedef); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 121 | return Typedef; |
| 122 | } |
| 123 | |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 124 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 125 | // Do substitution on the type of the declaration |
John McCall | 0a5fa06 | 2009-10-21 02:39:02 +0000 | [diff] [blame] | 126 | DeclaratorInfo *DI = SemaRef.SubstType(D->getDeclaratorInfo(), |
| 127 | TemplateArgs, |
| 128 | D->getTypeSpecStartLoc(), |
| 129 | D->getDeclName()); |
| 130 | if (!DI) |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 131 | return 0; |
| 132 | |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 133 | // Build the instantiated declaration |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 134 | VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner, |
| 135 | D->getLocation(), D->getIdentifier(), |
John McCall | 0a5fa06 | 2009-10-21 02:39:02 +0000 | [diff] [blame] | 136 | DI->getType(), DI, |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 137 | D->getStorageClass()); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 138 | Var->setThreadSpecified(D->isThreadSpecified()); |
| 139 | Var->setCXXDirectInitializer(D->hasCXXDirectInitializer()); |
| 140 | Var->setDeclaredInCondition(D->isDeclaredInCondition()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | |
| 142 | // If we are instantiating a static data member defined |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 143 | // out-of-line, the instantiation will have the same lexical |
| 144 | // context (which will be a namespace scope) as the template. |
| 145 | if (D->isOutOfLine()) |
| 146 | Var->setLexicalDeclContext(D->getLexicalDeclContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 148 | // FIXME: In theory, we could have a previous declaration for variables that |
| 149 | // are not static data members. |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 150 | bool Redeclaration = false; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 151 | SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 153 | if (D->isOutOfLine()) { |
| 154 | D->getLexicalDeclContext()->addDecl(Var); |
| 155 | Owner->makeDeclVisibleInContext(Var); |
| 156 | } else { |
| 157 | Owner->addDecl(Var); |
| 158 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 160 | // Link instantiations of static data members back to the template from |
| 161 | // which they were instantiated. |
| 162 | if (Var->isStaticDataMember()) |
| 163 | SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D, |
| 164 | TSK_ImplicitInstantiation); |
| 165 | |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 166 | if (D->getInit()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | OwningExprResult Init |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 168 | = SemaRef.SubstExpr(D->getInit(), TemplateArgs); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 169 | if (Init.isInvalid()) |
| 170 | Var->setInvalidDecl(); |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 171 | else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | // FIXME: We're faking all of the comma locations, which is suboptimal. |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 173 | // Do we even need these comma locations? |
| 174 | llvm::SmallVector<SourceLocation, 4> FakeCommaLocs; |
| 175 | if (PLE->getNumExprs() > 0) { |
| 176 | FakeCommaLocs.reserve(PLE->getNumExprs() - 1); |
| 177 | for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) { |
| 178 | Expr *E = PLE->getExpr(I)->Retain(); |
| 179 | FakeCommaLocs.push_back( |
| 180 | SemaRef.PP.getLocForEndOfToken(E->getLocEnd())); |
| 181 | } |
Douglas Gregor | e9f8eb6 | 2009-08-26 23:26:04 +0000 | [diff] [blame] | 182 | PLE->getExpr(PLE->getNumExprs() - 1)->Retain(); |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 183 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 185 | // Add the direct initializer to the declaration. |
| 186 | SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | PLE->getLParenLoc(), |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 188 | Sema::MultiExprArg(SemaRef, |
| 189 | (void**)PLE->getExprs(), |
| 190 | PLE->getNumExprs()), |
| 191 | FakeCommaLocs.data(), |
| 192 | PLE->getRParenLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 194 | // When Init is destroyed, it will destroy the instantiated ParenListExpr; |
| 195 | // we've explicitly retained all of its subexpressions already. |
| 196 | } else |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 197 | SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init), |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 198 | D->hasCXXDirectInitializer()); |
Douglas Gregor | 65b9005 | 2009-07-27 17:43:39 +0000 | [diff] [blame] | 199 | } else if (!Var->isStaticDataMember() || Var->isOutOfLine()) |
| 200 | SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 201 | |
| 202 | return Var; |
| 203 | } |
| 204 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 205 | Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { |
| 206 | bool Invalid = false; |
John McCall | 07fb6be | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 207 | DeclaratorInfo *DI = D->getDeclaratorInfo(); |
| 208 | if (DI->getType()->isDependentType()) { |
| 209 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 210 | D->getLocation(), D->getDeclName()); |
| 211 | if (!DI) { |
| 212 | DI = D->getDeclaratorInfo(); |
| 213 | Invalid = true; |
| 214 | } else if (DI->getType()->isFunctionType()) { |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 215 | // C++ [temp.arg.type]p3: |
| 216 | // If a declaration acquires a function type through a type |
| 217 | // dependent on a template-parameter and this causes a |
| 218 | // declaration that does not use the syntactic form of a |
| 219 | // function declarator to have function type, the program is |
| 220 | // ill-formed. |
| 221 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
John McCall | 07fb6be | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 222 | << DI->getType(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 223 | Invalid = true; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | Expr *BitWidth = D->getBitWidth(); |
| 228 | if (Invalid) |
| 229 | BitWidth = 0; |
| 230 | else if (BitWidth) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 231 | // The bit-width expression is not potentially evaluated. |
| 232 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 234 | OwningExprResult InstantiatedBitWidth |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 235 | = SemaRef.SubstExpr(BitWidth, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 236 | if (InstantiatedBitWidth.isInvalid()) { |
| 237 | Invalid = true; |
| 238 | BitWidth = 0; |
| 239 | } else |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 240 | BitWidth = InstantiatedBitWidth.takeAs<Expr>(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 241 | } |
| 242 | |
John McCall | 07fb6be | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 243 | FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), |
| 244 | DI->getType(), DI, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | cast<RecordDecl>(Owner), |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 246 | D->getLocation(), |
| 247 | D->isMutable(), |
| 248 | BitWidth, |
Steve Naroff | ea218b8 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 249 | D->getTypeSpecStartLoc(), |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 250 | D->getAccess(), |
| 251 | 0); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 252 | if (!Field) { |
| 253 | cast<Decl>(Owner)->setInvalidDecl(); |
Anders Carlsson | f4b5f5c | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 254 | return 0; |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 255 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 256 | |
Anders Carlsson | f4b5f5c | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 257 | if (Invalid) |
| 258 | Field->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
Anders Carlsson | f4b5f5c | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 260 | if (!Field->getDeclName()) { |
| 261 | // Keep track of where this decl came from. |
| 262 | SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 263 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Anders Carlsson | f4b5f5c | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 265 | Field->setImplicit(D->isImplicit()); |
| 266 | Owner->addDecl(Field); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 267 | |
| 268 | return Field; |
| 269 | } |
| 270 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 271 | Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) { |
| 272 | FriendDecl::FriendUnion FU; |
| 273 | |
| 274 | // Handle friend type expressions by simply substituting template |
| 275 | // parameters into the pattern type. |
| 276 | if (Type *Ty = D->getFriendType()) { |
| 277 | QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs, |
| 278 | D->getLocation(), DeclarationName()); |
| 279 | if (T.isNull()) return 0; |
| 280 | |
| 281 | assert(getLangOptions().CPlusPlus0x || T->isRecordType()); |
| 282 | FU = T.getTypePtr(); |
| 283 | |
| 284 | // Handle everything else by appropriate substitution. |
| 285 | } else { |
| 286 | NamedDecl *ND = D->getFriendDecl(); |
| 287 | assert(ND && "friend decl must be a decl or a type!"); |
| 288 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 289 | // FIXME: We have a problem here, because the nested call to Visit(ND) |
| 290 | // will inject the thing that the friend references into the current |
| 291 | // owner, which is wrong. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 292 | Decl *NewND = Visit(ND); |
| 293 | if (!NewND) return 0; |
| 294 | |
| 295 | FU = cast<NamedDecl>(NewND); |
John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 296 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 298 | FriendDecl *FD = |
| 299 | FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU, |
| 300 | D->getFriendLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 301 | FD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 302 | Owner->addDecl(FD); |
| 303 | return FD; |
John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 306 | Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 307 | Expr *AssertExpr = D->getAssertExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 309 | // The expression in a static assertion is not potentially evaluated. |
| 310 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 312 | OwningExprResult InstantiatedAssertExpr |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 313 | = SemaRef.SubstExpr(AssertExpr, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 314 | if (InstantiatedAssertExpr.isInvalid()) |
| 315 | return 0; |
| 316 | |
Douglas Gregor | 43d9d92 | 2009-08-08 01:41:12 +0000 | [diff] [blame] | 317 | OwningExprResult Message(SemaRef, D->getMessage()); |
| 318 | D->getMessage()->Retain(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | Decl *StaticAssert |
| 320 | = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(), |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 321 | move(InstantiatedAssertExpr), |
| 322 | move(Message)).getAs<Decl>(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 323 | return StaticAssert; |
| 324 | } |
| 325 | |
| 326 | Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 327 | EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 328 | D->getLocation(), D->getIdentifier(), |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 329 | D->getTagKeywordLoc(), |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 330 | /*PrevDecl=*/0); |
Douglas Gregor | 8dbc3c6 | 2009-05-27 17:20:35 +0000 | [diff] [blame] | 331 | Enum->setInstantiationOfMemberEnum(D); |
Douglas Gregor | 06c0fec | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 332 | Enum->setAccess(D->getAccess()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 333 | Owner->addDecl(Enum); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 334 | Enum->startDefinition(); |
| 335 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 336 | llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators; |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 337 | |
| 338 | EnumConstantDecl *LastEnumConst = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 339 | for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(), |
| 340 | ECEnd = D->enumerator_end(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 341 | EC != ECEnd; ++EC) { |
| 342 | // The specified value for the enumerator. |
| 343 | OwningExprResult Value = SemaRef.Owned((Expr *)0); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 344 | if (Expr *UninstValue = EC->getInitExpr()) { |
| 345 | // The enumerator's value expression is not potentially evaluated. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | EnterExpressionEvaluationContext Unevaluated(SemaRef, |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 347 | Action::Unevaluated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 349 | Value = SemaRef.SubstExpr(UninstValue, TemplateArgs); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 350 | } |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 351 | |
| 352 | // Drop the initial value and continue. |
| 353 | bool isInvalid = false; |
| 354 | if (Value.isInvalid()) { |
| 355 | Value = SemaRef.Owned((Expr *)0); |
| 356 | isInvalid = true; |
| 357 | } |
| 358 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | EnumConstantDecl *EnumConst |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 360 | = SemaRef.CheckEnumConstant(Enum, LastEnumConst, |
| 361 | EC->getLocation(), EC->getIdentifier(), |
| 362 | move(Value)); |
| 363 | |
| 364 | if (isInvalid) { |
| 365 | if (EnumConst) |
| 366 | EnumConst->setInvalidDecl(); |
| 367 | Enum->setInvalidDecl(); |
| 368 | } |
| 369 | |
| 370 | if (EnumConst) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 371 | Enum->addDecl(EnumConst); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 372 | Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst)); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 373 | LastEnumConst = EnumConst; |
| 374 | } |
| 375 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | |
Mike Stump | c6e35aa | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 377 | // FIXME: Fixup LBraceLoc and RBraceLoc |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 378 | // FIXME: Empty Scope and AttributeList (required to handle attribute packed). |
Mike Stump | c6e35aa | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 379 | SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(), |
| 380 | Sema::DeclPtrTy::make(Enum), |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 381 | &Enumerators[0], Enumerators.size(), |
| 382 | 0, 0); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 383 | |
| 384 | return Enum; |
| 385 | } |
| 386 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 387 | Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 388 | assert(false && "EnumConstantDecls can only occur within EnumDecls."); |
| 389 | return 0; |
| 390 | } |
| 391 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 392 | Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 393 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 394 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | if (!InstParams) |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 396 | return NULL; |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 397 | |
| 398 | CXXRecordDecl *Pattern = D->getTemplatedDecl(); |
| 399 | CXXRecordDecl *RecordInst |
| 400 | = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner, |
| 401 | Pattern->getLocation(), Pattern->getIdentifier(), |
Douglas Gregor | f0510d4 | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 402 | Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL, |
| 403 | /*DelayTypeCreation=*/true); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 404 | |
| 405 | ClassTemplateDecl *Inst |
| 406 | = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 407 | D->getIdentifier(), InstParams, RecordInst, 0); |
| 408 | RecordInst->setDescribedClassTemplate(Inst); |
| 409 | Inst->setAccess(D->getAccess()); |
| 410 | Inst->setInstantiatedFromMemberTemplate(D); |
Douglas Gregor | f0510d4 | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 411 | |
| 412 | // Trigger creation of the type for the instantiation. |
| 413 | SemaRef.Context.getTypeDeclType(RecordInst); |
| 414 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 415 | Owner->addDecl(Inst); |
| 416 | return Inst; |
| 417 | } |
| 418 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 419 | Decl * |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 420 | TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl( |
| 421 | ClassTemplatePartialSpecializationDecl *D) { |
| 422 | assert(false &&"Partial specializations of member templates are unsupported"); |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | Decl * |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 427 | TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 428 | // FIXME: Dig out the out-of-line definition of this function template? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 430 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 431 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | if (!InstParams) |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 433 | return NULL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 435 | FunctionDecl *Instantiated = 0; |
| 436 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl())) |
| 437 | Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod, |
| 438 | InstParams)); |
| 439 | else |
| 440 | Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl( |
| 441 | D->getTemplatedDecl(), |
| 442 | InstParams)); |
| 443 | |
| 444 | if (!Instantiated) |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 445 | return 0; |
| 446 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | // Link the instantiated function template declaration to the function |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 448 | // template from which it was instantiated. |
Douglas Gregor | 37d68185 | 2009-10-12 22:27:17 +0000 | [diff] [blame] | 449 | FunctionTemplateDecl *InstTemplate |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 450 | = Instantiated->getDescribedFunctionTemplate(); |
Douglas Gregor | 37d68185 | 2009-10-12 22:27:17 +0000 | [diff] [blame] | 451 | InstTemplate->setAccess(D->getAccess()); |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 452 | assert(InstTemplate && |
| 453 | "VisitFunctionDecl/CXXMethodDecl didn't create a template!"); |
| 454 | if (!InstTemplate->getInstantiatedFromMemberTemplate()) |
| 455 | InstTemplate->setInstantiatedFromMemberTemplate(D); |
| 456 | |
| 457 | // Add non-friends into the owner. |
| 458 | if (!InstTemplate->getFriendObjectKind()) |
| 459 | Owner->addDecl(InstTemplate); |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 460 | return InstTemplate; |
| 461 | } |
| 462 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 463 | Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 464 | CXXRecordDecl *PrevDecl = 0; |
| 465 | if (D->isInjectedClassName()) |
| 466 | PrevDecl = cast<CXXRecordDecl>(Owner); |
| 467 | |
| 468 | CXXRecordDecl *Record |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner, |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 470 | D->getLocation(), D->getIdentifier(), |
| 471 | D->getTagKeywordLoc(), PrevDecl); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 472 | Record->setImplicit(D->isImplicit()); |
Eli Friedman | eaba1af | 2009-08-27 19:11:42 +0000 | [diff] [blame] | 473 | // FIXME: Check against AS_none is an ugly hack to work around the issue that |
| 474 | // the tag decls introduced by friend class declarations don't have an access |
| 475 | // specifier. Remove once this area of the code gets sorted out. |
| 476 | if (D->getAccess() != AS_none) |
| 477 | Record->setAccess(D->getAccess()); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 478 | if (!D->isInjectedClassName()) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 479 | Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 480 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 481 | // If the original function was part of a friend declaration, |
| 482 | // inherit its namespace state. |
| 483 | if (Decl::FriendObjectKind FOK = D->getFriendObjectKind()) |
| 484 | Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared); |
| 485 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 486 | Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion()); |
| 487 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 488 | Owner->addDecl(Record); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 489 | return Record; |
| 490 | } |
| 491 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 492 | /// Normal class members are of more specific types and therefore |
| 493 | /// don't make it here. This function serves two purposes: |
| 494 | /// 1) instantiating function templates |
| 495 | /// 2) substituting friend declarations |
| 496 | /// FIXME: preserve function definitions in case #2 |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 497 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D, |
| 498 | TemplateParameterList *TemplateParams) { |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 499 | // Check whether there is already a function template specialization for |
| 500 | // this declaration. |
| 501 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
| 502 | void *InsertPos = 0; |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 503 | if (FunctionTemplate && !TemplateParams) { |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 504 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 505 | FunctionTemplateSpecializationInfo::Profile(ID, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 506 | TemplateArgs.getInnermost().getFlatArgumentList(), |
| 507 | TemplateArgs.getInnermost().flat_size(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 508 | SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | |
| 510 | FunctionTemplateSpecializationInfo *Info |
| 511 | = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID, |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 512 | InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 513 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 514 | // If we already have a function template specialization, return it. |
| 515 | if (Info) |
| 516 | return Info->Function; |
| 517 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 518 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 519 | Sema::LocalInstantiationScope Scope(SemaRef); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 521 | llvm::SmallVector<ParmVarDecl *, 4> Params; |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 522 | QualType T = SubstFunctionType(D, Params); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 523 | if (T.isNull()) |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 524 | return 0; |
John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 525 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 526 | // Build the instantiated method declaration. |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 527 | DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(), |
| 528 | TemplateArgs); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 529 | FunctionDecl *Function = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 530 | FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 531 | D->getDeclName(), T, D->getDeclaratorInfo(), |
| 532 | D->getStorageClass(), |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 533 | D->isInlineSpecified(), D->hasWrittenPrototype()); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 534 | Function->setLexicalDeclContext(Owner); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 535 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 536 | // Attach the parameters |
| 537 | for (unsigned P = 0; P < Params.size(); ++P) |
| 538 | Params[P]->setOwningFunction(Function); |
| 539 | Function->setParams(SemaRef.Context, Params.data(), Params.size()); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 540 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 541 | if (TemplateParams) { |
| 542 | // Our resulting instantiation is actually a function template, since we |
| 543 | // are substituting only the outer template parameters. For example, given |
| 544 | // |
| 545 | // template<typename T> |
| 546 | // struct X { |
| 547 | // template<typename U> friend void f(T, U); |
| 548 | // }; |
| 549 | // |
| 550 | // X<int> x; |
| 551 | // |
| 552 | // We are instantiating the friend function template "f" within X<int>, |
| 553 | // which means substituting int for T, but leaving "f" as a friend function |
| 554 | // template. |
| 555 | // Build the function template itself. |
| 556 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner, |
| 557 | Function->getLocation(), |
| 558 | Function->getDeclName(), |
| 559 | TemplateParams, Function); |
| 560 | Function->setDescribedFunctionTemplate(FunctionTemplate); |
| 561 | FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 562 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 563 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 564 | if (InitFunctionInstantiation(Function, D)) |
| 565 | Function->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 566 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 567 | bool Redeclaration = false; |
| 568 | bool OverloadableAttrRequired = false; |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 569 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 570 | NamedDecl *PrevDecl = 0; |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 571 | if (TemplateParams || !FunctionTemplate) { |
| 572 | // Look only into the namespace where the friend would be declared to |
| 573 | // find a previous declaration. This is the innermost enclosing namespace, |
| 574 | // as described in ActOnFriendFunctionDecl. |
| 575 | Sema::LookupResult R; |
| 576 | SemaRef.LookupQualifiedName(R, DC, Function->getDeclName(), |
| 577 | Sema::LookupOrdinaryName, true); |
| 578 | |
| 579 | PrevDecl = R.getAsSingleDecl(SemaRef.Context); |
| 580 | |
| 581 | // In C++, the previous declaration we find might be a tag type |
| 582 | // (class or enum). In this case, the new declaration will hide the |
| 583 | // tag type. Note that this does does not apply if we're declaring a |
| 584 | // typedef (C++ [dcl.typedef]p4). |
| 585 | if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 586 | PrevDecl = 0; |
| 587 | } |
| 588 | |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 589 | SemaRef.CheckFunctionDeclaration(Function, PrevDecl, false, Redeclaration, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 590 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 591 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 592 | // If the original function was part of a friend declaration, |
| 593 | // inherit its namespace state and add it to the owner. |
| 594 | NamedDecl *FromFriendD |
| 595 | = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D; |
| 596 | if (FromFriendD->getFriendObjectKind()) { |
| 597 | NamedDecl *ToFriendD = 0; |
| 598 | if (TemplateParams) { |
| 599 | ToFriendD = cast<NamedDecl>(FunctionTemplate); |
| 600 | PrevDecl = FunctionTemplate->getPreviousDeclaration(); |
| 601 | } else { |
| 602 | ToFriendD = Function; |
| 603 | PrevDecl = Function->getPreviousDeclaration(); |
| 604 | } |
| 605 | ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL); |
| 606 | if (!Owner->isDependentContext() && !PrevDecl) |
| 607 | DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false); |
| 608 | |
| 609 | if (!TemplateParams) |
| 610 | Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
| 611 | } |
| 612 | |
| 613 | if (FunctionTemplate && !TemplateParams) { |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 614 | // Record this function template specialization. |
| 615 | Function->setFunctionTemplateSpecialization(SemaRef.Context, |
| 616 | FunctionTemplate, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 617 | &TemplateArgs.getInnermost(), |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 618 | InsertPos); |
John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 621 | return Function; |
| 622 | } |
| 623 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 624 | Decl * |
| 625 | TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D, |
| 626 | TemplateParameterList *TemplateParams) { |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 627 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
| 628 | void *InsertPos = 0; |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 629 | if (FunctionTemplate && !TemplateParams) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 630 | // We are creating a function template specialization from a function |
| 631 | // template. Check whether there is already a function template |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 632 | // specialization for this particular set of template arguments. |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 633 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | FunctionTemplateSpecializationInfo::Profile(ID, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 635 | TemplateArgs.getInnermost().getFlatArgumentList(), |
| 636 | TemplateArgs.getInnermost().flat_size(), |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 637 | SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
| 639 | FunctionTemplateSpecializationInfo *Info |
| 640 | = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 641 | InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 642 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 643 | // If we already have a function template specialization, return it. |
| 644 | if (Info) |
| 645 | return Info->Function; |
| 646 | } |
| 647 | |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 648 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 649 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 650 | llvm::SmallVector<ParmVarDecl *, 4> Params; |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 651 | QualType T = SubstFunctionType(D, Params); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 652 | if (T.isNull()) |
| 653 | return 0; |
| 654 | |
| 655 | // Build the instantiated method declaration. |
| 656 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 657 | CXXMethodDecl *Method = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 658 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 659 | DeclarationName Name = D->getDeclName(); |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 660 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 661 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); |
| 662 | Name = SemaRef.Context.DeclarationNames.getCXXConstructorName( |
| 663 | SemaRef.Context.getCanonicalType(ClassTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | Method = CXXConstructorDecl::Create(SemaRef.Context, Record, |
| 665 | Constructor->getLocation(), |
| 666 | Name, T, |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 667 | Constructor->getDeclaratorInfo(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 668 | Constructor->isExplicit(), |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 669 | Constructor->isInlineSpecified(), false); |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 670 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
| 671 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); |
| 672 | Name = SemaRef.Context.DeclarationNames.getCXXDestructorName( |
| 673 | SemaRef.Context.getCanonicalType(ClassTy)); |
| 674 | Method = CXXDestructorDecl::Create(SemaRef.Context, Record, |
| 675 | Destructor->getLocation(), Name, |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 676 | T, Destructor->isInlineSpecified(), false); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 677 | } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 678 | CanQualType ConvTy |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 679 | = SemaRef.Context.getCanonicalType( |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 680 | T->getAs<FunctionType>()->getResultType()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 681 | Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName( |
| 682 | ConvTy); |
| 683 | Method = CXXConversionDecl::Create(SemaRef.Context, Record, |
| 684 | Conversion->getLocation(), Name, |
| 685 | T, Conversion->getDeclaratorInfo(), |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 686 | Conversion->isInlineSpecified(), |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 687 | Conversion->isExplicit()); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 688 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 689 | Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(), |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 690 | D->getDeclName(), T, D->getDeclaratorInfo(), |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 691 | D->isStatic(), D->isInlineSpecified()); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 692 | } |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 693 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 694 | if (TemplateParams) { |
| 695 | // Our resulting instantiation is actually a function template, since we |
| 696 | // are substituting only the outer template parameters. For example, given |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 697 | // |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 698 | // template<typename T> |
| 699 | // struct X { |
| 700 | // template<typename U> void f(T, U); |
| 701 | // }; |
| 702 | // |
| 703 | // X<int> x; |
| 704 | // |
| 705 | // We are instantiating the member template "f" within X<int>, which means |
| 706 | // substituting int for T, but leaving "f" as a member function template. |
| 707 | // Build the function template itself. |
| 708 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, |
| 709 | Method->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 710 | Method->getDeclName(), |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 711 | TemplateParams, Method); |
| 712 | if (D->isOutOfLine()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 713 | FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 714 | Method->setDescribedFunctionTemplate(FunctionTemplate); |
| 715 | } else if (!FunctionTemplate) |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 716 | Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 717 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 718 | // If we are instantiating a member function defined |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 719 | // out-of-line, the instantiation will have the same lexical |
| 720 | // context (which will be a namespace scope) as the template. |
| 721 | if (D->isOutOfLine()) |
| 722 | Method->setLexicalDeclContext(D->getLexicalDeclContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 723 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 724 | // Attach the parameters |
| 725 | for (unsigned P = 0; P < Params.size(); ++P) |
| 726 | Params[P]->setOwningFunction(Method); |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 727 | Method->setParams(SemaRef.Context, Params.data(), Params.size()); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 728 | |
| 729 | if (InitMethodInstantiation(Method, D)) |
| 730 | Method->setInvalidDecl(); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 731 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 732 | NamedDecl *PrevDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 733 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 734 | if (!FunctionTemplate || TemplateParams) { |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 735 | Sema::LookupResult R; |
| 736 | SemaRef.LookupQualifiedName(R, Owner, Name, Sema::LookupOrdinaryName, true); |
| 737 | PrevDecl = R.getAsSingleDecl(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 738 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 739 | // In C++, the previous declaration we find might be a tag type |
| 740 | // (class or enum). In this case, the new declaration will hide the |
| 741 | // tag type. Note that this does does not apply if we're declaring a |
| 742 | // typedef (C++ [dcl.typedef]p4). |
| 743 | if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 744 | PrevDecl = 0; |
| 745 | } |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 746 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 747 | if (FunctionTemplate && !TemplateParams) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 748 | // Record this function template specialization. |
| 749 | Method->setFunctionTemplateSpecialization(SemaRef.Context, |
| 750 | FunctionTemplate, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 751 | &TemplateArgs.getInnermost(), |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 752 | InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 754 | bool Redeclaration = false; |
| 755 | bool OverloadableAttrRequired = false; |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 756 | SemaRef.CheckFunctionDeclaration(Method, PrevDecl, false, Redeclaration, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 757 | /*FIXME:*/OverloadableAttrRequired); |
| 758 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 759 | if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl) && |
| 760 | !Method->getFriendObjectKind()) |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 761 | Owner->addDecl(Method); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 762 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 763 | return Method; |
| 764 | } |
| 765 | |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 766 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 767 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 770 | Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 771 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 774 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 775 | return VisitCXXMethodDecl(D); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 776 | } |
| 777 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 778 | ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 779 | QualType T; |
| 780 | DeclaratorInfo *DI = D->getDeclaratorInfo(); |
| 781 | if (DI) { |
| 782 | DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(), |
| 783 | D->getDeclName()); |
| 784 | if (DI) T = DI->getType(); |
| 785 | } else { |
| 786 | T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(), |
| 787 | D->getDeclName()); |
| 788 | DI = 0; |
| 789 | } |
| 790 | |
| 791 | if (T.isNull()) |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 792 | return 0; |
| 793 | |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 794 | T = SemaRef.adjustParameterType(T); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 795 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 796 | // Allocate the parameter |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 797 | ParmVarDecl *Param |
| 798 | = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 799 | D->getIdentifier(), T, DI, D->getStorageClass(), 0); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 800 | |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 801 | // Mark the default argument as being uninstantiated. |
Douglas Gregor | f43d0b3 | 2009-09-25 06:56:31 +0000 | [diff] [blame] | 802 | if (D->hasUninstantiatedDefaultArg()) |
| 803 | Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg()); |
Douglas Gregor | 0ed0930 | 2009-09-25 07:03:22 +0000 | [diff] [blame] | 804 | else if (Expr *Arg = D->getDefaultArg()) |
| 805 | Param->setUninstantiatedDefaultArg(Arg); |
| 806 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 807 | // Note: we don't try to instantiate function parameters until after |
| 808 | // we've instantiated the function's type. Therefore, we don't have |
| 809 | // to check for 'void' parameter types here. |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 810 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 811 | return Param; |
| 812 | } |
| 813 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 814 | Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( |
| 815 | TemplateTypeParmDecl *D) { |
| 816 | // TODO: don't always clone when decls are refcounted. |
| 817 | const Type* T = D->getTypeForDecl(); |
| 818 | assert(T->isTemplateTypeParmType()); |
| 819 | const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 820 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 821 | TemplateTypeParmDecl *Inst = |
| 822 | TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 823 | TTPT->getDepth(), TTPT->getIndex(), |
| 824 | TTPT->getName(), |
| 825 | D->wasDeclaredWithTypename(), |
| 826 | D->isParameterPack()); |
| 827 | |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 828 | // FIXME: Do we actually want to perform substitution here? I don't think |
| 829 | // we do. |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 830 | if (D->hasDefaultArgument()) { |
| 831 | QualType DefaultPattern = D->getDefaultArgument(); |
| 832 | QualType DefaultInst |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 833 | = SemaRef.SubstType(DefaultPattern, TemplateArgs, |
| 834 | D->getDefaultArgumentLoc(), |
| 835 | D->getDeclName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 836 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 837 | Inst->setDefaultArgument(DefaultInst, |
| 838 | D->getDefaultArgumentLoc(), |
| 839 | D->defaultArgumentWasInherited() /* preserve? */); |
| 840 | } |
| 841 | |
| 842 | return Inst; |
| 843 | } |
| 844 | |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 845 | Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl( |
| 846 | NonTypeTemplateParmDecl *D) { |
| 847 | // Substitute into the type of the non-type template parameter. |
| 848 | QualType T; |
| 849 | DeclaratorInfo *DI = D->getDeclaratorInfo(); |
| 850 | if (DI) { |
| 851 | DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(), |
| 852 | D->getDeclName()); |
| 853 | if (DI) T = DI->getType(); |
| 854 | } else { |
| 855 | T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(), |
| 856 | D->getDeclName()); |
| 857 | DI = 0; |
| 858 | } |
| 859 | if (T.isNull()) |
| 860 | return 0; |
| 861 | |
| 862 | // Check that this type is acceptable for a non-type template parameter. |
| 863 | bool Invalid = false; |
| 864 | T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation()); |
| 865 | if (T.isNull()) { |
| 866 | T = SemaRef.Context.IntTy; |
| 867 | Invalid = true; |
| 868 | } |
| 869 | |
| 870 | NonTypeTemplateParmDecl *Param |
| 871 | = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 872 | D->getDepth() - 1, D->getPosition(), |
| 873 | D->getIdentifier(), T, DI); |
| 874 | if (Invalid) |
| 875 | Param->setInvalidDecl(); |
| 876 | |
| 877 | Param->setDefaultArgument(D->getDefaultArgument()); |
| 878 | return Param; |
| 879 | } |
| 880 | |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 881 | Decl * |
| 882 | TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 883 | NestedNameSpecifier *NNS = |
| 884 | SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(), |
| 885 | D->getTargetNestedNameRange(), |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 886 | TemplateArgs); |
| 887 | if (!NNS) |
| 888 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 889 | |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 890 | CXXScopeSpec SS; |
| 891 | SS.setRange(D->getTargetNestedNameRange()); |
| 892 | SS.setScopeRep(NNS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 893 | |
| 894 | NamedDecl *UD = |
| 895 | SemaRef.BuildUsingDeclaration(D->getLocation(), SS, |
| 896 | D->getTargetNameLocation(), |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 897 | D->getTargetName(), 0, D->isTypeName()); |
| 898 | if (UD) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD), |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 900 | D); |
| 901 | return UD; |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 902 | } |
| 903 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 904 | Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 905 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 906 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 907 | return Instantiator.Visit(D); |
| 908 | } |
| 909 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 910 | /// \brief Instantiates a nested template parameter list in the current |
| 911 | /// instantiation context. |
| 912 | /// |
| 913 | /// \param L The parameter list to instantiate |
| 914 | /// |
| 915 | /// \returns NULL if there was an error |
| 916 | TemplateParameterList * |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 917 | TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 918 | // Get errors for all the parameters before bailing out. |
| 919 | bool Invalid = false; |
| 920 | |
| 921 | unsigned N = L->size(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 922 | typedef llvm::SmallVector<NamedDecl *, 8> ParamVector; |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 923 | ParamVector Params; |
| 924 | Params.reserve(N); |
| 925 | for (TemplateParameterList::iterator PI = L->begin(), PE = L->end(); |
| 926 | PI != PE; ++PI) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 927 | NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI)); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 928 | Params.push_back(D); |
| 929 | Invalid = Invalid || !D; |
| 930 | } |
| 931 | |
| 932 | // Clean up if we had an error. |
| 933 | if (Invalid) { |
| 934 | for (ParamVector::iterator PI = Params.begin(), PE = Params.end(); |
| 935 | PI != PE; ++PI) |
| 936 | if (*PI) |
| 937 | (*PI)->Destroy(SemaRef.Context); |
| 938 | return NULL; |
| 939 | } |
| 940 | |
| 941 | TemplateParameterList *InstL |
| 942 | = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), |
| 943 | L->getLAngleLoc(), &Params.front(), N, |
| 944 | L->getRAngleLoc()); |
| 945 | return InstL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 946 | } |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 947 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 948 | /// \brief Does substitution on the type of the given function, including |
| 949 | /// all of the function parameters. |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 950 | /// |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 951 | /// \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] | 952 | /// |
| 953 | /// \param Params the instantiated parameter declarations |
| 954 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 955 | /// \returns the instantiated function's type if successful, a NULL |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 956 | /// type if there was an error. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 957 | QualType |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 958 | TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 959 | llvm::SmallVectorImpl<ParmVarDecl *> &Params) { |
| 960 | bool InvalidDecl = false; |
| 961 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 962 | // Substitute all of the function's formal parameter types. |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 963 | TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs); |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 964 | llvm::SmallVector<QualType, 4> ParamTys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | for (FunctionDecl::param_iterator P = D->param_begin(), |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 966 | PEnd = D->param_end(); |
| 967 | P != PEnd; ++P) { |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 968 | if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) { |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 969 | if (PInst->getType()->isVoidType()) { |
| 970 | SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type); |
| 971 | PInst->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(), |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 973 | PInst->getType(), |
| 974 | diag::err_abstract_type_in_decl, |
| 975 | Sema::AbstractParamType)) |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 976 | PInst->setInvalidDecl(); |
| 977 | |
| 978 | Params.push_back(PInst); |
| 979 | ParamTys.push_back(PInst->getType()); |
| 980 | |
| 981 | if (PInst->isInvalidDecl()) |
| 982 | InvalidDecl = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 983 | } else |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 984 | InvalidDecl = true; |
| 985 | } |
| 986 | |
| 987 | // FIXME: Deallocate dead declarations. |
| 988 | if (InvalidDecl) |
| 989 | return QualType(); |
| 990 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 991 | const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 992 | assert(Proto && "Missing prototype?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 993 | QualType ResultType |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 994 | = SemaRef.SubstType(Proto->getResultType(), TemplateArgs, |
| 995 | D->getLocation(), D->getDeclName()); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 996 | if (ResultType.isNull()) |
| 997 | return QualType(); |
| 998 | |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 999 | return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(), |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1000 | Proto->isVariadic(), Proto->getTypeQuals(), |
| 1001 | D->getLocation(), D->getDeclName()); |
| 1002 | } |
| 1003 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1004 | /// \brief Initializes the common fields of an instantiation function |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1005 | /// declaration (New) from the corresponding fields of its template (Tmpl). |
| 1006 | /// |
| 1007 | /// \returns true if there was an error |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1008 | bool |
| 1009 | TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1010 | FunctionDecl *Tmpl) { |
| 1011 | if (Tmpl->isDeleted()) |
| 1012 | New->setDeleted(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1013 | |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1014 | // If we are performing substituting explicitly-specified template arguments |
| 1015 | // or deduced template arguments into a function template and we reach this |
| 1016 | // point, we are now past the point where SFINAE applies and have committed |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1017 | // to keeping the new function template specialization. We therefore |
| 1018 | // convert the active template instantiation for the function template |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1019 | // into a template instantiation for this specific function template |
| 1020 | // specialization, which is not a SFINAE context, so that we diagnose any |
| 1021 | // further errors in the declaration itself. |
| 1022 | typedef Sema::ActiveTemplateInstantiation ActiveInstType; |
| 1023 | ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back(); |
| 1024 | if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || |
| 1025 | ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1026 | if (FunctionTemplateDecl *FunTmpl |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1027 | = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1028 | assert(FunTmpl->getTemplatedDecl() == Tmpl && |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1029 | "Deduction from the wrong function template?"); |
Daniel Dunbar | bcbb8bd | 2009-07-16 22:10:11 +0000 | [diff] [blame] | 1030 | (void) FunTmpl; |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1031 | ActiveInst.Kind = ActiveInstType::TemplateInstantiation; |
| 1032 | ActiveInst.Entity = reinterpret_cast<uintptr_t>(New); |
| 1033 | } |
| 1034 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1035 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1036 | return false; |
| 1037 | } |
| 1038 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1039 | /// \brief Initializes common fields of an instantiated method |
| 1040 | /// declaration (New) from the corresponding fields of its template |
| 1041 | /// (Tmpl). |
| 1042 | /// |
| 1043 | /// \returns true if there was an error |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | bool |
| 1045 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1046 | CXXMethodDecl *Tmpl) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1047 | if (InitFunctionInstantiation(New, Tmpl)) |
| 1048 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1049 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1050 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 1051 | New->setAccess(Tmpl->getAccess()); |
Anders Carlsson | 77b7f1d | 2009-05-14 22:15:41 +0000 | [diff] [blame] | 1052 | if (Tmpl->isVirtualAsWritten()) { |
| 1053 | New->setVirtualAsWritten(true); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1054 | Record->setAggregate(false); |
| 1055 | Record->setPOD(false); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 1056 | Record->setEmpty(false); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1057 | Record->setPolymorphic(true); |
| 1058 | } |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1059 | if (Tmpl->isPure()) { |
| 1060 | New->setPure(); |
| 1061 | Record->setAbstract(true); |
| 1062 | } |
| 1063 | |
| 1064 | // FIXME: attributes |
| 1065 | // FIXME: New needs a pointer to Tmpl |
| 1066 | return false; |
| 1067 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1068 | |
| 1069 | /// \brief Instantiate the definition of the given function from its |
| 1070 | /// template. |
| 1071 | /// |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1072 | /// \param PointOfInstantiation the point at which the instantiation was |
| 1073 | /// required. Note that this is not precisely a "point of instantiation" |
| 1074 | /// for the function, but it's close. |
| 1075 | /// |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1076 | /// \param Function the already-instantiated declaration of a |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1077 | /// function template specialization or member function of a class template |
| 1078 | /// specialization. |
| 1079 | /// |
| 1080 | /// \param Recursive if true, recursively instantiates any functions that |
| 1081 | /// are required by this instantiation. |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 1082 | /// |
| 1083 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 1084 | /// instantiation where the body of the function is required. Complain if |
| 1085 | /// there is no such body. |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 1086 | void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1087 | FunctionDecl *Function, |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 1088 | bool Recursive, |
| 1089 | bool DefinitionRequired) { |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 1090 | if (Function->isInvalidDecl()) |
| 1091 | return; |
| 1092 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1093 | assert(!Function->getBody() && "Already instantiated!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1095 | // Never instantiate an explicit specialization. |
| 1096 | if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
| 1097 | return; |
| 1098 | |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 1099 | // Find the function body that we'll be substituting. |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1100 | const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern(); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 1101 | Stmt *Pattern = 0; |
| 1102 | if (PatternDecl) |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1103 | Pattern = PatternDecl->getBody(PatternDecl); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 1104 | |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 1105 | if (!Pattern) { |
| 1106 | if (DefinitionRequired) { |
| 1107 | if (Function->getPrimaryTemplate()) |
| 1108 | Diag(PointOfInstantiation, |
| 1109 | diag::err_explicit_instantiation_undefined_func_template) |
| 1110 | << Function->getPrimaryTemplate(); |
| 1111 | else |
| 1112 | Diag(PointOfInstantiation, |
| 1113 | diag::err_explicit_instantiation_undefined_member) |
| 1114 | << 1 << Function->getDeclName() << Function->getDeclContext(); |
| 1115 | |
| 1116 | if (PatternDecl) |
| 1117 | Diag(PatternDecl->getLocation(), |
| 1118 | diag::note_explicit_instantiation_here); |
| 1119 | } |
| 1120 | |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 1121 | return; |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 1122 | } |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 1123 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1124 | // C++0x [temp.explicit]p9: |
| 1125 | // Except for inline functions, other explicit instantiation declarations |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1126 | // have the effect of suppressing the implicit instantiation of the entity |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1127 | // to which they refer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1128 | if (Function->getTemplateSpecializationKind() |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1129 | == TSK_ExplicitInstantiationDeclaration && |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame^] | 1130 | !PatternDecl->isInlined()) |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1131 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1132 | |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 1133 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); |
| 1134 | if (Inst) |
| 1135 | return; |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 1136 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1137 | // If we're performing recursive template instantiation, create our own |
| 1138 | // queue of pending implicit instantiations that we will instantiate later, |
| 1139 | // while we're still within our own instantiation context. |
| 1140 | std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations; |
| 1141 | if (Recursive) |
| 1142 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1143 | |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 1144 | ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function)); |
| 1145 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 1146 | // Introduce a new scope where local variable instantiations will be |
| 1147 | // recorded. |
| 1148 | LocalInstantiationScope Scope(*this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1149 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 1150 | // Introduce the instantiated function parameters into the local |
| 1151 | // instantiation scope. |
| 1152 | for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) |
| 1153 | Scope.InstantiatedLocal(PatternDecl->getParamDecl(I), |
| 1154 | Function->getParamDecl(I)); |
| 1155 | |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 1156 | // Enter the scope of this instantiation. We don't use |
| 1157 | // PushDeclContext because we don't have a scope. |
| 1158 | DeclContext *PreviousContext = CurContext; |
| 1159 | CurContext = Function; |
| 1160 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1161 | MultiLevelTemplateArgumentList TemplateArgs = |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1162 | getTemplateInstantiationArgs(Function); |
| 1163 | |
| 1164 | // If this is a constructor, instantiate the member initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1165 | if (const CXXConstructorDecl *Ctor = |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1166 | dyn_cast<CXXConstructorDecl>(PatternDecl)) { |
| 1167 | InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor, |
| 1168 | TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 1171 | // Instantiate the function body. |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1172 | OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs); |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 1173 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 1174 | if (Body.isInvalid()) |
| 1175 | Function->setInvalidDecl(); |
| 1176 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1177 | ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body), |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 1178 | /*IsInstantiation=*/true); |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 1179 | |
| 1180 | CurContext = PreviousContext; |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 1181 | |
| 1182 | DeclGroupRef DG(Function); |
| 1183 | Consumer.HandleTopLevelDecl(DG); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1184 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1185 | if (Recursive) { |
| 1186 | // Instantiate any pending implicit instantiations found during the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1187 | // instantiation of this template. |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1188 | PerformPendingImplicitInstantiations(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1189 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1190 | // Restore the set of pending implicit instantiations. |
| 1191 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); |
| 1192 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | /// \brief Instantiate the definition of the given variable from its |
| 1196 | /// template. |
| 1197 | /// |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1198 | /// \param PointOfInstantiation the point at which the instantiation was |
| 1199 | /// required. Note that this is not precisely a "point of instantiation" |
| 1200 | /// for the function, but it's close. |
| 1201 | /// |
| 1202 | /// \param Var the already-instantiated declaration of a static member |
| 1203 | /// variable of a class template specialization. |
| 1204 | /// |
| 1205 | /// \param Recursive if true, recursively instantiates any functions that |
| 1206 | /// are required by this instantiation. |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 1207 | /// |
| 1208 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 1209 | /// instantiation where an out-of-line definition of the member variable |
| 1210 | /// is required. Complain if there is no such definition. |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1211 | void Sema::InstantiateStaticDataMemberDefinition( |
| 1212 | SourceLocation PointOfInstantiation, |
| 1213 | VarDecl *Var, |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 1214 | bool Recursive, |
| 1215 | bool DefinitionRequired) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1216 | if (Var->isInvalidDecl()) |
| 1217 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1218 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1219 | // Find the out-of-line definition of this static data member. |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1220 | VarDecl *Def = Var->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1221 | assert(Def && "This data member was not instantiated from a template?"); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1222 | assert(Def->isStaticDataMember() && "Not a static data member?"); |
| 1223 | Def = Def->getOutOfLineDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1224 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1225 | if (!Def) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1226 | // We did not find an out-of-line definition of this static data member, |
| 1227 | // so we won't perform any instantiation. Rather, we rely on the user to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1228 | // instantiate this definition (or provide a specialization for it) in |
| 1229 | // another translation unit. |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 1230 | if (DefinitionRequired) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1231 | Def = Var->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 1232 | Diag(PointOfInstantiation, |
| 1233 | diag::err_explicit_instantiation_undefined_member) |
| 1234 | << 2 << Var->getDeclName() << Var->getDeclContext(); |
| 1235 | Diag(Def->getLocation(), diag::note_explicit_instantiation_here); |
| 1236 | } |
| 1237 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1238 | return; |
| 1239 | } |
| 1240 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1241 | // Never instantiate an explicit specialization. |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1242 | if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1243 | return; |
| 1244 | |
| 1245 | // C++0x [temp.explicit]p9: |
| 1246 | // Except for inline functions, other explicit instantiation declarations |
| 1247 | // have the effect of suppressing the implicit instantiation of the entity |
| 1248 | // to which they refer. |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1249 | if (Var->getTemplateSpecializationKind() |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1250 | == TSK_ExplicitInstantiationDeclaration) |
| 1251 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1252 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1253 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
| 1254 | if (Inst) |
| 1255 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1256 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1257 | // If we're performing recursive template instantiation, create our own |
| 1258 | // queue of pending implicit instantiations that we will instantiate later, |
| 1259 | // while we're still within our own instantiation context. |
| 1260 | std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations; |
| 1261 | if (Recursive) |
| 1262 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1263 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1264 | // Enter the scope of this instantiation. We don't use |
| 1265 | // PushDeclContext because we don't have a scope. |
| 1266 | DeclContext *PreviousContext = CurContext; |
| 1267 | CurContext = Var->getDeclContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1268 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1269 | VarDecl *OldVar = Var; |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1270 | Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1271 | getTemplateInstantiationArgs(Var))); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1272 | CurContext = PreviousContext; |
| 1273 | |
| 1274 | if (Var) { |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1275 | Var->setPreviousDeclaration(OldVar); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 1276 | MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo(); |
| 1277 | assert(MSInfo && "Missing member specialization information?"); |
| 1278 | Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(), |
| 1279 | MSInfo->getPointOfInstantiation()); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1280 | DeclGroupRef DG(Var); |
| 1281 | Consumer.HandleTopLevelDecl(DG); |
| 1282 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1283 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1284 | if (Recursive) { |
| 1285 | // Instantiate any pending implicit instantiations found during the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1286 | // instantiation of this template. |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1287 | PerformPendingImplicitInstantiations(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1288 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1289 | // Restore the set of pending implicit instantiations. |
| 1290 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1291 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1292 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1293 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1294 | void |
| 1295 | Sema::InstantiateMemInitializers(CXXConstructorDecl *New, |
| 1296 | const CXXConstructorDecl *Tmpl, |
| 1297 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1298 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1299 | llvm::SmallVector<MemInitTy*, 4> NewInits; |
| 1300 | |
| 1301 | // Instantiate all the initializers. |
| 1302 | for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(), |
Douglas Gregor | 72f6d67 | 2009-09-01 21:04:42 +0000 | [diff] [blame] | 1303 | InitsEnd = Tmpl->init_end(); |
| 1304 | Inits != InitsEnd; ++Inits) { |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1305 | CXXBaseOrMemberInitializer *Init = *Inits; |
| 1306 | |
| 1307 | ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1309 | // Instantiate all the arguments. |
| 1310 | for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end(); |
| 1311 | Args != ArgsEnd; ++Args) { |
| 1312 | OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs); |
| 1313 | |
| 1314 | if (NewArg.isInvalid()) |
| 1315 | New->setInvalidDecl(); |
| 1316 | else |
| 1317 | NewArgs.push_back(NewArg.takeAs<Expr>()); |
| 1318 | } |
| 1319 | |
| 1320 | MemInitResult NewInit; |
| 1321 | |
| 1322 | if (Init->isBaseInitializer()) { |
Eli Friedman | c5573a8 | 2009-08-29 22:22:07 +0000 | [diff] [blame] | 1323 | QualType BaseType(Init->getBaseClass(), 0); |
| 1324 | BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(), |
| 1325 | New->getDeclName()); |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1326 | |
| 1327 | NewInit = BuildBaseInitializer(BaseType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1328 | (Expr **)NewArgs.data(), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1329 | NewArgs.size(), |
| 1330 | Init->getSourceLocation(), |
| 1331 | Init->getRParenLoc(), |
| 1332 | New->getParent()); |
| 1333 | } else if (Init->isMemberInitializer()) { |
Anders Carlsson | 9988d5d | 2009-09-01 04:31:02 +0000 | [diff] [blame] | 1334 | FieldDecl *Member; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1335 | |
Anders Carlsson | 9988d5d | 2009-09-01 04:31:02 +0000 | [diff] [blame] | 1336 | // Is this an anonymous union? |
| 1337 | if (FieldDecl *UnionInit = Init->getAnonUnionMember()) |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 1338 | Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs)); |
Anders Carlsson | 9988d5d | 2009-09-01 04:31:02 +0000 | [diff] [blame] | 1339 | else |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 1340 | Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(), |
| 1341 | TemplateArgs)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1342 | |
| 1343 | NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1344 | NewArgs.size(), |
| 1345 | Init->getSourceLocation(), |
| 1346 | Init->getRParenLoc()); |
| 1347 | } |
| 1348 | |
| 1349 | if (NewInit.isInvalid()) |
| 1350 | New->setInvalidDecl(); |
| 1351 | else { |
| 1352 | // FIXME: It would be nice if ASTOwningVector had a release function. |
| 1353 | NewArgs.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1354 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1355 | NewInits.push_back((MemInitTy *)NewInit.get()); |
| 1356 | } |
| 1357 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1358 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1359 | // Assign all the initializers to the new constructor. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1360 | ActOnMemInitializers(DeclPtrTy::make(New), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1361 | /*FIXME: ColonLoc */ |
| 1362 | SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | NewInits.data(), NewInits.size()); |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1366 | // TODO: this could be templated if the various decl types used the |
| 1367 | // same method name. |
| 1368 | static bool isInstantiationOf(ClassTemplateDecl *Pattern, |
| 1369 | ClassTemplateDecl *Instance) { |
| 1370 | Pattern = Pattern->getCanonicalDecl(); |
| 1371 | |
| 1372 | do { |
| 1373 | Instance = Instance->getCanonicalDecl(); |
| 1374 | if (Pattern == Instance) return true; |
| 1375 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 1376 | } while (Instance); |
| 1377 | |
| 1378 | return false; |
| 1379 | } |
| 1380 | |
Douglas Gregor | 0d69653 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 1381 | static bool isInstantiationOf(FunctionTemplateDecl *Pattern, |
| 1382 | FunctionTemplateDecl *Instance) { |
| 1383 | Pattern = Pattern->getCanonicalDecl(); |
| 1384 | |
| 1385 | do { |
| 1386 | Instance = Instance->getCanonicalDecl(); |
| 1387 | if (Pattern == Instance) return true; |
| 1388 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 1389 | } while (Instance); |
| 1390 | |
| 1391 | return false; |
| 1392 | } |
| 1393 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1394 | static bool isInstantiationOf(CXXRecordDecl *Pattern, |
| 1395 | CXXRecordDecl *Instance) { |
| 1396 | Pattern = Pattern->getCanonicalDecl(); |
| 1397 | |
| 1398 | do { |
| 1399 | Instance = Instance->getCanonicalDecl(); |
| 1400 | if (Pattern == Instance) return true; |
| 1401 | Instance = Instance->getInstantiatedFromMemberClass(); |
| 1402 | } while (Instance); |
| 1403 | |
| 1404 | return false; |
| 1405 | } |
| 1406 | |
| 1407 | static bool isInstantiationOf(FunctionDecl *Pattern, |
| 1408 | FunctionDecl *Instance) { |
| 1409 | Pattern = Pattern->getCanonicalDecl(); |
| 1410 | |
| 1411 | do { |
| 1412 | Instance = Instance->getCanonicalDecl(); |
| 1413 | if (Pattern == Instance) return true; |
| 1414 | Instance = Instance->getInstantiatedFromMemberFunction(); |
| 1415 | } while (Instance); |
| 1416 | |
| 1417 | return false; |
| 1418 | } |
| 1419 | |
| 1420 | static bool isInstantiationOf(EnumDecl *Pattern, |
| 1421 | EnumDecl *Instance) { |
| 1422 | Pattern = Pattern->getCanonicalDecl(); |
| 1423 | |
| 1424 | do { |
| 1425 | Instance = Instance->getCanonicalDecl(); |
| 1426 | if (Pattern == Instance) return true; |
| 1427 | Instance = Instance->getInstantiatedFromMemberEnum(); |
| 1428 | } while (Instance); |
| 1429 | |
| 1430 | return false; |
| 1431 | } |
| 1432 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1433 | static bool isInstantiationOf(UnresolvedUsingDecl *Pattern, |
| 1434 | UsingDecl *Instance, |
| 1435 | ASTContext &C) { |
| 1436 | return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern; |
| 1437 | } |
| 1438 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1439 | static bool isInstantiationOfStaticDataMember(VarDecl *Pattern, |
| 1440 | VarDecl *Instance) { |
| 1441 | assert(Instance->isStaticDataMember()); |
| 1442 | |
| 1443 | Pattern = Pattern->getCanonicalDecl(); |
| 1444 | |
| 1445 | do { |
| 1446 | Instance = Instance->getCanonicalDecl(); |
| 1447 | if (Pattern == Instance) return true; |
| 1448 | Instance = Instance->getInstantiatedFromStaticDataMember(); |
| 1449 | } while (Instance); |
| 1450 | |
| 1451 | return false; |
| 1452 | } |
| 1453 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1454 | static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1455 | if (D->getKind() != Other->getKind()) { |
| 1456 | if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) { |
| 1457 | if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { |
| 1458 | return isInstantiationOf(UUD, UD, Ctx); |
| 1459 | } |
| 1460 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1461 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1462 | return false; |
| 1463 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1465 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other)) |
| 1466 | return isInstantiationOf(cast<CXXRecordDecl>(D), Record); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1468 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other)) |
| 1469 | return isInstantiationOf(cast<FunctionDecl>(D), Function); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1470 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1471 | if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other)) |
| 1472 | return isInstantiationOf(cast<EnumDecl>(D), Enum); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1473 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1474 | if (VarDecl *Var = dyn_cast<VarDecl>(Other)) |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1475 | if (Var->isStaticDataMember()) |
| 1476 | return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var); |
| 1477 | |
| 1478 | if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other)) |
| 1479 | return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp); |
Douglas Gregor | a5bf7f1 | 2009-08-28 22:03:51 +0000 | [diff] [blame] | 1480 | |
Douglas Gregor | 0d69653 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 1481 | if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other)) |
| 1482 | return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp); |
| 1483 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1484 | if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) { |
| 1485 | if (!Field->getDeclName()) { |
| 1486 | // This is an unnamed field. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) == |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1488 | cast<FieldDecl>(D); |
| 1489 | } |
| 1490 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1491 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1492 | return D->getDeclName() && isa<NamedDecl>(Other) && |
| 1493 | D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); |
| 1494 | } |
| 1495 | |
| 1496 | template<typename ForwardIterator> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | static NamedDecl *findInstantiationOf(ASTContext &Ctx, |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1498 | NamedDecl *D, |
| 1499 | ForwardIterator first, |
| 1500 | ForwardIterator last) { |
| 1501 | for (; first != last; ++first) |
| 1502 | if (isInstantiationOf(Ctx, D, *first)) |
| 1503 | return cast<NamedDecl>(*first); |
| 1504 | |
| 1505 | return 0; |
| 1506 | } |
| 1507 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1508 | /// \brief Finds the instantiation of the given declaration context |
| 1509 | /// within the current instantiation. |
| 1510 | /// |
| 1511 | /// \returns NULL if there was an error |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 1512 | DeclContext *Sema::FindInstantiatedContext(DeclContext* DC, |
| 1513 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1514 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) { |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 1515 | Decl* ID = FindInstantiatedDecl(D, TemplateArgs); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1516 | return cast_or_null<DeclContext>(ID); |
| 1517 | } else return DC; |
| 1518 | } |
| 1519 | |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 1520 | /// \brief Find the instantiation of the given declaration within the |
| 1521 | /// current instantiation. |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1522 | /// |
| 1523 | /// This routine is intended to be used when \p D is a declaration |
| 1524 | /// referenced from within a template, that needs to mapped into the |
| 1525 | /// corresponding declaration within an instantiation. For example, |
| 1526 | /// given: |
| 1527 | /// |
| 1528 | /// \code |
| 1529 | /// template<typename T> |
| 1530 | /// struct X { |
| 1531 | /// enum Kind { |
| 1532 | /// KnownValue = sizeof(T) |
| 1533 | /// }; |
| 1534 | /// |
| 1535 | /// bool getKind() const { return KnownValue; } |
| 1536 | /// }; |
| 1537 | /// |
| 1538 | /// template struct X<int>; |
| 1539 | /// \endcode |
| 1540 | /// |
| 1541 | /// In the instantiation of X<int>::getKind(), we need to map the |
| 1542 | /// EnumConstantDecl for KnownValue (which refers to |
| 1543 | /// X<T>::<Kind>::KnownValue) to its instantiation |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 1544 | /// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs |
| 1545 | /// this mapping from within the instantiation of X<int>. |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 1546 | NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D, |
| 1547 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 44c7384 | 2009-09-01 17:53:10 +0000 | [diff] [blame] | 1548 | if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) { |
| 1549 | // Transform all of the elements of the overloaded function set. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1550 | OverloadedFunctionDecl *Result |
Douglas Gregor | 44c7384 | 2009-09-01 17:53:10 +0000 | [diff] [blame] | 1551 | = OverloadedFunctionDecl::Create(Context, CurContext, Ovl->getDeclName()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | |
Douglas Gregor | 44c7384 | 2009-09-01 17:53:10 +0000 | [diff] [blame] | 1553 | for (OverloadedFunctionDecl::function_iterator F = Ovl->function_begin(), |
| 1554 | FEnd = Ovl->function_end(); |
| 1555 | F != FEnd; ++F) { |
| 1556 | Result->addOverload( |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 1557 | AnyFunctionDecl::getFromNamedDecl(FindInstantiatedDecl(*F, |
| 1558 | TemplateArgs))); |
Douglas Gregor | 44c7384 | 2009-09-01 17:53:10 +0000 | [diff] [blame] | 1559 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1560 | |
Douglas Gregor | 44c7384 | 2009-09-01 17:53:10 +0000 | [diff] [blame] | 1561 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1564 | DeclContext *ParentDC = D->getDeclContext(); |
Douglas Gregor | 2bba76b | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 1565 | if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) { |
| 1566 | // D is a local of some kind. Look into the map of local |
| 1567 | // declarations to their instantiations. |
| 1568 | return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D)); |
| 1569 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1570 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 1571 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 1572 | if (!Record->isDependentContext()) |
| 1573 | return D; |
| 1574 | |
| 1575 | // If the RecordDecl is actually the injected-class-name or a "templated" |
| 1576 | // declaration for a class template or class template partial |
| 1577 | // specialization, substitute into the injected-class-name of the |
| 1578 | // class template or partial specialization to find the new DeclContext. |
| 1579 | QualType T; |
| 1580 | ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate(); |
| 1581 | |
| 1582 | if (ClassTemplate) { |
| 1583 | T = ClassTemplate->getInjectedClassNameType(Context); |
| 1584 | } else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 1585 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 1586 | T = Context.getTypeDeclType(Record); |
| 1587 | ClassTemplate = PartialSpec->getSpecializedTemplate(); |
| 1588 | } |
| 1589 | |
| 1590 | if (!T.isNull()) { |
| 1591 | // Substitute into the injected-class-name to get the type corresponding |
| 1592 | // to the instantiation we want. This substitution should never fail, |
| 1593 | // since we know we can instantiate the injected-class-name or we wouldn't |
| 1594 | // have gotten to the injected-class-name! |
| 1595 | // FIXME: Can we use the CurrentInstantiationScope to avoid this extra |
| 1596 | // instantiation in the common case? |
| 1597 | T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName()); |
| 1598 | assert(!T.isNull() && "Instantiation of injected-class-name cannot fail."); |
| 1599 | |
| 1600 | if (!T->isDependentType()) { |
| 1601 | assert(T->isRecordType() && "Instantiation must produce a record type"); |
| 1602 | return T->getAs<RecordType>()->getDecl(); |
| 1603 | } |
| 1604 | |
| 1605 | // We are performing "partial" template instantiation to create the |
| 1606 | // member declarations for the members of a class template |
| 1607 | // specialization. Therefore, D is actually referring to something in |
| 1608 | // the current instantiation. Look through the current context, |
| 1609 | // which contains actual instantiations, to find the instantiation of |
| 1610 | // the "current instantiation" that D refers to. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1611 | for (DeclContext *DC = CurContext; !DC->isFileContext(); |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1612 | DC = DC->getParent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1613 | if (ClassTemplateSpecializationDecl *Spec |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1614 | = dyn_cast<ClassTemplateSpecializationDecl>(DC)) |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 1615 | if (isInstantiationOf(ClassTemplate, |
| 1616 | Spec->getSpecializedTemplate())) |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1617 | return Spec; |
| 1618 | } |
| 1619 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1620 | assert(false && |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1621 | "Unable to find declaration for the current instantiation"); |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 1622 | return Record; |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1623 | } |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 1624 | |
| 1625 | // Fall through to deal with other dependent record types (e.g., |
| 1626 | // anonymous unions in class templates). |
| 1627 | } |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1628 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 1629 | if (!ParentDC->isDependentContext()) |
| 1630 | return D; |
| 1631 | |
| 1632 | ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1633 | if (!ParentDC) |
Douglas Gregor | 44c7384 | 2009-09-01 17:53:10 +0000 | [diff] [blame] | 1634 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1635 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1636 | if (ParentDC != D->getDeclContext()) { |
| 1637 | // We performed some kind of instantiation in the parent context, |
| 1638 | // so now we need to look into the instantiated parent context to |
| 1639 | // find the instantiation of the declaration D. |
| 1640 | NamedDecl *Result = 0; |
| 1641 | if (D->getDeclName()) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1642 | DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName()); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1643 | Result = findInstantiationOf(Context, D, Found.first, Found.second); |
| 1644 | } else { |
| 1645 | // Since we don't have a name for the entity we're looking for, |
| 1646 | // our only option is to walk through all of the declarations to |
| 1647 | // find that name. This will occur in a few cases: |
| 1648 | // |
| 1649 | // - anonymous struct/union within a template |
| 1650 | // - unnamed class/struct/union/enum within a template |
| 1651 | // |
| 1652 | // FIXME: Find a better way to find these instantiations! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | Result = findInstantiationOf(Context, D, |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1654 | ParentDC->decls_begin(), |
| 1655 | ParentDC->decls_end()); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1656 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1657 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1658 | assert(Result && "Unable to find instantiation of declaration!"); |
| 1659 | D = Result; |
| 1660 | } |
| 1661 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1662 | return D; |
| 1663 | } |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1664 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1665 | /// \brief Performs template instantiation for all implicit template |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1666 | /// instantiations we have seen until this point. |
| 1667 | void Sema::PerformPendingImplicitInstantiations() { |
| 1668 | while (!PendingImplicitInstantiations.empty()) { |
| 1669 | PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front(); |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1670 | PendingImplicitInstantiations.pop_front(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1671 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1672 | // Instantiate function definitions |
| 1673 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1674 | PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function), |
Anders Carlsson | c17fb7b | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 1675 | Function->getLocation(), *this, |
| 1676 | Context.getSourceManager(), |
| 1677 | "instantiating function definition"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1679 | if (!Function->getBody()) |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1680 | InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1681 | continue; |
| 1682 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1683 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1684 | // Instantiate static data member definitions. |
| 1685 | VarDecl *Var = cast<VarDecl>(Inst.first); |
| 1686 | assert(Var->isStaticDataMember() && "Not a static data member?"); |
Anders Carlsson | c17fb7b | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 1687 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1688 | PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var), |
Anders Carlsson | c17fb7b | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 1689 | Var->getLocation(), *this, |
| 1690 | Context.getSourceManager(), |
| 1691 | "instantiating static data member " |
| 1692 | "definition"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1693 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1694 | InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1695 | } |
| 1696 | } |