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