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