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