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