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