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