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