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