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