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