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" |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 13 | #include "Lookup.h" |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/DeclTemplate.h" |
| 17 | #include "clang/AST/DeclVisitor.h" |
| 18 | #include "clang/AST/Expr.h" |
Anders Carlsson | c17fb7b | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 19 | #include "clang/Basic/PrettyStackTrace.h" |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| 23 | |
| 24 | namespace { |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 25 | class 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 | |
Anders Carlsson | d8fe2d5 | 2009-11-07 06:07:58 +0000 | [diff] [blame] | 31 | void InstantiateAttrs(Decl *Tmpl, Decl *New); |
| 32 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 33 | public: |
| 34 | typedef Sema::OwningExprResult OwningExprResult; |
| 35 | |
| 36 | TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 37 | const MultiLevelTemplateArgumentList &TemplateArgs) |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 38 | : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 39 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 40 | // FIXME: Once we get closer to completion, replace these manually-written |
| 41 | // declarations with automatically-generated ones from |
| 42 | // clang/AST/DeclNodes.def. |
Douglas Gregor | 4f722be | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 43 | Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 44 | Decl *VisitNamespaceDecl(NamespaceDecl *D); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 45 | Decl *VisitTypedefDecl(TypedefDecl *D); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 46 | Decl *VisitVarDecl(VarDecl *D); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 47 | Decl *VisitFieldDecl(FieldDecl *D); |
| 48 | Decl *VisitStaticAssertDecl(StaticAssertDecl *D); |
| 49 | Decl *VisitEnumDecl(EnumDecl *D); |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 50 | Decl *VisitEnumConstantDecl(EnumConstantDecl *D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 51 | Decl *VisitFriendDecl(FriendDecl *D); |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 52 | Decl *VisitFunctionDecl(FunctionDecl *D, |
| 53 | TemplateParameterList *TemplateParams = 0); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 54 | Decl *VisitCXXRecordDecl(CXXRecordDecl *D); |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 55 | Decl *VisitCXXMethodDecl(CXXMethodDecl *D, |
| 56 | TemplateParameterList *TemplateParams = 0); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 57 | Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 58 | Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 59 | Decl *VisitCXXConversionDecl(CXXConversionDecl *D); |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 60 | ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 61 | Decl *VisitClassTemplateDecl(ClassTemplateDecl *D); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 62 | Decl *VisitClassTemplatePartialSpecializationDecl( |
| 63 | ClassTemplatePartialSpecializationDecl *D); |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 64 | Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 65 | Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 66 | Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
Douglas Gregor | 9106ef7 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 67 | Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
Douglas Gregor | 48c32a7 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 68 | Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 69 | Decl *VisitUsingDecl(UsingDecl *D); |
| 70 | Decl *VisitUsingShadowDecl(UsingShadowDecl *D); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 71 | Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); |
| 72 | Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 74 | // Base case. FIXME: Remove once we can instantiate everything. |
Douglas Gregor | 48c32a7 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 75 | Decl *VisitDecl(Decl *D) { |
| 76 | unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID( |
| 77 | Diagnostic::Error, |
| 78 | "cannot instantiate %0 yet"); |
| 79 | SemaRef.Diag(D->getLocation(), DiagID) |
| 80 | << D->getDeclKindName(); |
| 81 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 82 | return 0; |
| 83 | } |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 84 | |
John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 85 | const LangOptions &getLangOptions() { |
| 86 | return SemaRef.getLangOptions(); |
| 87 | } |
| 88 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 89 | // Helper functions for instantiating methods. |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 90 | QualType SubstFunctionType(FunctionDecl *D, |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 91 | llvm::SmallVectorImpl<ParmVarDecl *> &Params); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 92 | bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 93 | bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 94 | |
| 95 | TemplateParameterList * |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 96 | SubstTemplateParams(TemplateParameterList *List); |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 97 | |
| 98 | bool InstantiateClassTemplatePartialSpecialization( |
| 99 | ClassTemplateDecl *ClassTemplate, |
| 100 | ClassTemplatePartialSpecializationDecl *PartialSpec); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 101 | }; |
| 102 | } |
| 103 | |
Anders Carlsson | d8fe2d5 | 2009-11-07 06:07:58 +0000 | [diff] [blame] | 104 | // FIXME: Is this too simple? |
| 105 | void TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) { |
| 106 | for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr; |
| 107 | TmplAttr = TmplAttr->getNext()) { |
| 108 | |
| 109 | // FIXME: Is cloning correct for all attributes? |
| 110 | Attr *NewAttr = TmplAttr->clone(SemaRef.Context); |
| 111 | |
| 112 | New->addAttr(NewAttr); |
| 113 | } |
| 114 | } |
| 115 | |
Douglas Gregor | 4f722be | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 116 | Decl * |
| 117 | TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 118 | assert(false && "Translation units cannot be instantiated"); |
| 119 | return D; |
| 120 | } |
| 121 | |
| 122 | Decl * |
| 123 | TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { |
| 124 | assert(false && "Namespaces cannot be instantiated"); |
| 125 | return D; |
| 126 | } |
| 127 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 128 | Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { |
| 129 | bool Invalid = false; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 130 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 131 | if (DI->getType()->isDependentType()) { |
| 132 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 133 | D->getLocation(), D->getDeclName()); |
| 134 | if (!DI) { |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 135 | Invalid = true; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 136 | DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 139 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 140 | // Create the new typedef |
| 141 | TypedefDecl *Typedef |
| 142 | = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 143 | D->getIdentifier(), DI); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 144 | if (Invalid) |
| 145 | Typedef->setInvalidDecl(); |
| 146 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 147 | Owner->addDecl(Typedef); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 149 | return Typedef; |
| 150 | } |
| 151 | |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 152 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 153 | // Do substitution on the type of the declaration |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 154 | TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(), |
John McCall | 0a5fa06 | 2009-10-21 02:39:02 +0000 | [diff] [blame] | 155 | TemplateArgs, |
| 156 | D->getTypeSpecStartLoc(), |
| 157 | D->getDeclName()); |
| 158 | if (!DI) |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 159 | return 0; |
| 160 | |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 161 | // Build the instantiated declaration |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 162 | VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner, |
| 163 | D->getLocation(), D->getIdentifier(), |
John McCall | 0a5fa06 | 2009-10-21 02:39:02 +0000 | [diff] [blame] | 164 | DI->getType(), DI, |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 165 | D->getStorageClass()); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 166 | Var->setThreadSpecified(D->isThreadSpecified()); |
| 167 | Var->setCXXDirectInitializer(D->hasCXXDirectInitializer()); |
| 168 | Var->setDeclaredInCondition(D->isDeclaredInCondition()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | |
| 170 | // If we are instantiating a static data member defined |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 171 | // out-of-line, the instantiation will have the same lexical |
| 172 | // context (which will be a namespace scope) as the template. |
| 173 | if (D->isOutOfLine()) |
| 174 | Var->setLexicalDeclContext(D->getLexicalDeclContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 176 | // FIXME: In theory, we could have a previous declaration for variables that |
| 177 | // are not static data members. |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 178 | bool Redeclaration = false; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 179 | // FIXME: having to fake up a LookupResult is dumb. |
| 180 | LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(), |
| 181 | Sema::LookupOrdinaryName); |
| 182 | SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 184 | if (D->isOutOfLine()) { |
| 185 | D->getLexicalDeclContext()->addDecl(Var); |
| 186 | Owner->makeDeclVisibleInContext(Var); |
| 187 | } else { |
| 188 | Owner->addDecl(Var); |
| 189 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 191 | // Link instantiations of static data members back to the template from |
| 192 | // which they were instantiated. |
| 193 | if (Var->isStaticDataMember()) |
| 194 | SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D, |
Douglas Gregor | cf3293e | 2009-11-01 20:32:48 +0000 | [diff] [blame] | 195 | TSK_ImplicitInstantiation); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 196 | |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 197 | if (D->getInit()) { |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 198 | if (Var->isStaticDataMember() && !D->isOutOfLine()) |
| 199 | SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated); |
| 200 | else |
| 201 | SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); |
| 202 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | OwningExprResult Init |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 204 | = SemaRef.SubstExpr(D->getInit(), TemplateArgs); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 205 | if (Init.isInvalid()) |
| 206 | Var->setInvalidDecl(); |
Sebastian Redl | 42dddbe | 2009-11-08 10:16:43 +0000 | [diff] [blame] | 207 | else if (!D->getType()->isDependentType() && |
| 208 | !D->getInit()->isTypeDependent() && |
Douglas Gregor | e48319a | 2009-11-09 17:16:50 +0000 | [diff] [blame] | 209 | !D->getInit()->isValueDependent()) { |
Sebastian Redl | 42dddbe | 2009-11-08 10:16:43 +0000 | [diff] [blame] | 210 | // If neither the declaration's type nor its initializer are dependent, |
| 211 | // we don't want to redo all the checking, especially since the |
| 212 | // initializer might have been wrapped by a CXXConstructExpr since we did |
| 213 | // it the first time. |
Anders Carlsson | a244dc3 | 2009-11-24 16:52:50 +0000 | [diff] [blame] | 214 | Var->setType(D->getType()); |
Sebastian Redl | 42dddbe | 2009-11-08 10:16:43 +0000 | [diff] [blame] | 215 | Var->setInit(SemaRef.Context, Init.takeAs<Expr>()); |
| 216 | } |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 217 | else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | // FIXME: We're faking all of the comma locations, which is suboptimal. |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 219 | // Do we even need these comma locations? |
| 220 | llvm::SmallVector<SourceLocation, 4> FakeCommaLocs; |
| 221 | if (PLE->getNumExprs() > 0) { |
| 222 | FakeCommaLocs.reserve(PLE->getNumExprs() - 1); |
| 223 | for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) { |
| 224 | Expr *E = PLE->getExpr(I)->Retain(); |
| 225 | FakeCommaLocs.push_back( |
| 226 | SemaRef.PP.getLocForEndOfToken(E->getLocEnd())); |
| 227 | } |
Douglas Gregor | e9f8eb6 | 2009-08-26 23:26:04 +0000 | [diff] [blame] | 228 | PLE->getExpr(PLE->getNumExprs() - 1)->Retain(); |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 229 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 231 | // Add the direct initializer to the declaration. |
| 232 | SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | PLE->getLParenLoc(), |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 234 | Sema::MultiExprArg(SemaRef, |
| 235 | (void**)PLE->getExprs(), |
| 236 | PLE->getNumExprs()), |
| 237 | FakeCommaLocs.data(), |
| 238 | PLE->getRParenLoc()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 240 | // When Init is destroyed, it will destroy the instantiated ParenListExpr; |
| 241 | // we've explicitly retained all of its subexpressions already. |
| 242 | } else |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 243 | SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init), |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 244 | D->hasCXXDirectInitializer()); |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 245 | SemaRef.PopExpressionEvaluationContext(); |
Douglas Gregor | 65b9005 | 2009-07-27 17:43:39 +0000 | [diff] [blame] | 246 | } else if (!Var->isStaticDataMember() || Var->isOutOfLine()) |
| 247 | SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 248 | |
| 249 | return Var; |
| 250 | } |
| 251 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 252 | Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { |
| 253 | bool Invalid = false; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 254 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
John McCall | 07fb6be | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 255 | if (DI->getType()->isDependentType()) { |
| 256 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 257 | D->getLocation(), D->getDeclName()); |
| 258 | if (!DI) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 259 | DI = D->getTypeSourceInfo(); |
John McCall | 07fb6be | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 260 | Invalid = true; |
| 261 | } else if (DI->getType()->isFunctionType()) { |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 262 | // C++ [temp.arg.type]p3: |
| 263 | // If a declaration acquires a function type through a type |
| 264 | // dependent on a template-parameter and this causes a |
| 265 | // declaration that does not use the syntactic form of a |
| 266 | // function declarator to have function type, the program is |
| 267 | // ill-formed. |
| 268 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
John McCall | 07fb6be | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 269 | << DI->getType(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 270 | Invalid = true; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | Expr *BitWidth = D->getBitWidth(); |
| 275 | if (Invalid) |
| 276 | BitWidth = 0; |
| 277 | else if (BitWidth) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 278 | // The bit-width expression is not potentially evaluated. |
| 279 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 281 | OwningExprResult InstantiatedBitWidth |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 282 | = SemaRef.SubstExpr(BitWidth, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 283 | if (InstantiatedBitWidth.isInvalid()) { |
| 284 | Invalid = true; |
| 285 | BitWidth = 0; |
| 286 | } else |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 287 | BitWidth = InstantiatedBitWidth.takeAs<Expr>(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 288 | } |
| 289 | |
John McCall | 07fb6be | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 290 | FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), |
| 291 | DI->getType(), DI, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 292 | cast<RecordDecl>(Owner), |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 293 | D->getLocation(), |
| 294 | D->isMutable(), |
| 295 | BitWidth, |
Steve Naroff | ea218b8 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 296 | D->getTypeSpecStartLoc(), |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 297 | D->getAccess(), |
| 298 | 0); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 299 | if (!Field) { |
| 300 | cast<Decl>(Owner)->setInvalidDecl(); |
Anders Carlsson | f4b5f5c | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 301 | return 0; |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 302 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
Anders Carlsson | d8fe2d5 | 2009-11-07 06:07:58 +0000 | [diff] [blame] | 304 | InstantiateAttrs(D, Field); |
| 305 | |
Anders Carlsson | f4b5f5c | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 306 | if (Invalid) |
| 307 | Field->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | |
Anders Carlsson | f4b5f5c | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 309 | if (!Field->getDeclName()) { |
| 310 | // Keep track of where this decl came from. |
| 311 | SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 312 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | |
Anders Carlsson | f4b5f5c | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 314 | Field->setImplicit(D->isImplicit()); |
| 315 | Owner->addDecl(Field); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 316 | |
| 317 | return Field; |
| 318 | } |
| 319 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 320 | Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) { |
| 321 | FriendDecl::FriendUnion FU; |
| 322 | |
| 323 | // Handle friend type expressions by simply substituting template |
| 324 | // parameters into the pattern type. |
| 325 | if (Type *Ty = D->getFriendType()) { |
| 326 | QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs, |
| 327 | D->getLocation(), DeclarationName()); |
| 328 | if (T.isNull()) return 0; |
| 329 | |
| 330 | assert(getLangOptions().CPlusPlus0x || T->isRecordType()); |
| 331 | FU = T.getTypePtr(); |
| 332 | |
| 333 | // Handle everything else by appropriate substitution. |
| 334 | } else { |
| 335 | NamedDecl *ND = D->getFriendDecl(); |
| 336 | assert(ND && "friend decl must be a decl or a type!"); |
| 337 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 338 | // FIXME: We have a problem here, because the nested call to Visit(ND) |
| 339 | // will inject the thing that the friend references into the current |
| 340 | // owner, which is wrong. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 341 | Decl *NewND = Visit(ND); |
| 342 | if (!NewND) return 0; |
| 343 | |
| 344 | FU = cast<NamedDecl>(NewND); |
John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 345 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 346 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 347 | FriendDecl *FD = |
| 348 | FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU, |
| 349 | D->getFriendLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 350 | FD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 351 | Owner->addDecl(FD); |
| 352 | return FD; |
John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 355 | Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 356 | Expr *AssertExpr = D->getAssertExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 358 | // The expression in a static assertion is not potentially evaluated. |
| 359 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 361 | OwningExprResult InstantiatedAssertExpr |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 362 | = SemaRef.SubstExpr(AssertExpr, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 363 | if (InstantiatedAssertExpr.isInvalid()) |
| 364 | return 0; |
| 365 | |
Douglas Gregor | 43d9d92 | 2009-08-08 01:41:12 +0000 | [diff] [blame] | 366 | OwningExprResult Message(SemaRef, D->getMessage()); |
| 367 | D->getMessage()->Retain(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | Decl *StaticAssert |
| 369 | = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(), |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 370 | move(InstantiatedAssertExpr), |
| 371 | move(Message)).getAs<Decl>(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 372 | return StaticAssert; |
| 373 | } |
| 374 | |
| 375 | Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 377 | D->getLocation(), D->getIdentifier(), |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 378 | D->getTagKeywordLoc(), |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 379 | /*PrevDecl=*/0); |
Douglas Gregor | 8dbc3c6 | 2009-05-27 17:20:35 +0000 | [diff] [blame] | 380 | Enum->setInstantiationOfMemberEnum(D); |
Douglas Gregor | 06c0fec | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 381 | Enum->setAccess(D->getAccess()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 382 | Owner->addDecl(Enum); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 383 | Enum->startDefinition(); |
| 384 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 385 | llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators; |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 386 | |
| 387 | EnumConstantDecl *LastEnumConst = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 388 | for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(), |
| 389 | ECEnd = D->enumerator_end(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 390 | EC != ECEnd; ++EC) { |
| 391 | // The specified value for the enumerator. |
| 392 | OwningExprResult Value = SemaRef.Owned((Expr *)0); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 393 | if (Expr *UninstValue = EC->getInitExpr()) { |
| 394 | // The enumerator's value expression is not potentially evaluated. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | EnterExpressionEvaluationContext Unevaluated(SemaRef, |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 396 | Action::Unevaluated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 397 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 398 | Value = SemaRef.SubstExpr(UninstValue, TemplateArgs); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 399 | } |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 400 | |
| 401 | // Drop the initial value and continue. |
| 402 | bool isInvalid = false; |
| 403 | if (Value.isInvalid()) { |
| 404 | Value = SemaRef.Owned((Expr *)0); |
| 405 | isInvalid = true; |
| 406 | } |
| 407 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | EnumConstantDecl *EnumConst |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 409 | = SemaRef.CheckEnumConstant(Enum, LastEnumConst, |
| 410 | EC->getLocation(), EC->getIdentifier(), |
| 411 | move(Value)); |
| 412 | |
| 413 | if (isInvalid) { |
| 414 | if (EnumConst) |
| 415 | EnumConst->setInvalidDecl(); |
| 416 | Enum->setInvalidDecl(); |
| 417 | } |
| 418 | |
| 419 | if (EnumConst) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 420 | Enum->addDecl(EnumConst); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 421 | Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst)); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 422 | LastEnumConst = EnumConst; |
| 423 | } |
| 424 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | |
Mike Stump | c6e35aa | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 426 | // FIXME: Fixup LBraceLoc and RBraceLoc |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 427 | // FIXME: Empty Scope and AttributeList (required to handle attribute packed). |
Mike Stump | c6e35aa | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 428 | SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(), |
| 429 | Sema::DeclPtrTy::make(Enum), |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 430 | &Enumerators[0], Enumerators.size(), |
| 431 | 0, 0); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 432 | |
| 433 | return Enum; |
| 434 | } |
| 435 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 436 | Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 437 | assert(false && "EnumConstantDecls can only occur within EnumDecls."); |
| 438 | return 0; |
| 439 | } |
| 440 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 441 | namespace { |
| 442 | class SortDeclByLocation { |
| 443 | SourceManager &SourceMgr; |
| 444 | |
| 445 | public: |
| 446 | explicit SortDeclByLocation(SourceManager &SourceMgr) |
| 447 | : SourceMgr(SourceMgr) { } |
| 448 | |
| 449 | bool operator()(const Decl *X, const Decl *Y) const { |
| 450 | return SourceMgr.isBeforeInTranslationUnit(X->getLocation(), |
| 451 | Y->getLocation()); |
| 452 | } |
| 453 | }; |
| 454 | } |
| 455 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 456 | Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 457 | // Create a local instantiation scope for this class template, which |
| 458 | // will contain the instantiations of the template parameters. |
| 459 | Sema::LocalInstantiationScope Scope(SemaRef); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 460 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 461 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | if (!InstParams) |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 463 | return NULL; |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 464 | |
| 465 | CXXRecordDecl *Pattern = D->getTemplatedDecl(); |
| 466 | CXXRecordDecl *RecordInst |
| 467 | = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner, |
| 468 | Pattern->getLocation(), Pattern->getIdentifier(), |
Douglas Gregor | f0510d4 | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 469 | Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL, |
| 470 | /*DelayTypeCreation=*/true); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 471 | |
| 472 | ClassTemplateDecl *Inst |
| 473 | = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 474 | D->getIdentifier(), InstParams, RecordInst, 0); |
| 475 | RecordInst->setDescribedClassTemplate(Inst); |
Douglas Gregor | e8c01bd | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 476 | if (D->getFriendObjectKind()) |
| 477 | Inst->setObjectOfFriendDecl(true); |
| 478 | else |
| 479 | Inst->setAccess(D->getAccess()); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 480 | Inst->setInstantiatedFromMemberTemplate(D); |
Douglas Gregor | f0510d4 | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 481 | |
| 482 | // Trigger creation of the type for the instantiation. |
| 483 | SemaRef.Context.getTypeDeclType(RecordInst); |
| 484 | |
Douglas Gregor | 259571e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 485 | // Finish handling of friends. |
| 486 | if (Inst->getFriendObjectKind()) { |
Douglas Gregor | e8c01bd | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 487 | return Inst; |
Douglas Gregor | 259571e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 488 | } |
Douglas Gregor | e8c01bd | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 489 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 490 | Owner->addDecl(Inst); |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 491 | |
| 492 | // First, we sort the partial specializations by location, so |
| 493 | // that we instantiate them in the order they were declared. |
| 494 | llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 495 | for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator |
| 496 | P = D->getPartialSpecializations().begin(), |
| 497 | PEnd = D->getPartialSpecializations().end(); |
| 498 | P != PEnd; ++P) |
| 499 | PartialSpecs.push_back(&*P); |
| 500 | std::sort(PartialSpecs.begin(), PartialSpecs.end(), |
| 501 | SortDeclByLocation(SemaRef.SourceMgr)); |
| 502 | |
| 503 | // Instantiate all of the partial specializations of this member class |
| 504 | // template. |
| 505 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) |
| 506 | InstantiateClassTemplatePartialSpecialization(Inst, PartialSpecs[I]); |
| 507 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 508 | return Inst; |
| 509 | } |
| 510 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 511 | Decl * |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 512 | TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl( |
| 513 | ClassTemplatePartialSpecializationDecl *D) { |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 514 | ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); |
| 515 | |
| 516 | // Lookup the already-instantiated declaration in the instantiation |
| 517 | // of the class template and return that. |
| 518 | DeclContext::lookup_result Found |
| 519 | = Owner->lookup(ClassTemplate->getDeclName()); |
| 520 | if (Found.first == Found.second) |
| 521 | return 0; |
| 522 | |
| 523 | ClassTemplateDecl *InstClassTemplate |
| 524 | = dyn_cast<ClassTemplateDecl>(*Found.first); |
| 525 | if (!InstClassTemplate) |
| 526 | return 0; |
| 527 | |
| 528 | Decl *DCanon = D->getCanonicalDecl(); |
| 529 | for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator |
| 530 | P = InstClassTemplate->getPartialSpecializations().begin(), |
| 531 | PEnd = InstClassTemplate->getPartialSpecializations().end(); |
| 532 | P != PEnd; ++P) { |
| 533 | if (P->getInstantiatedFromMember()->getCanonicalDecl() == DCanon) |
| 534 | return &*P; |
| 535 | } |
| 536 | |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | Decl * |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 541 | TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 542 | // Create a local instantiation scope for this function template, which |
| 543 | // will contain the instantiations of the template parameters and then get |
| 544 | // merged with the local instantiation scope for the function template |
| 545 | // itself. |
| 546 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 547 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 548 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 549 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | if (!InstParams) |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 551 | return NULL; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 552 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 553 | FunctionDecl *Instantiated = 0; |
| 554 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl())) |
| 555 | Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod, |
| 556 | InstParams)); |
| 557 | else |
| 558 | Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl( |
| 559 | D->getTemplatedDecl(), |
| 560 | InstParams)); |
| 561 | |
| 562 | if (!Instantiated) |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 563 | return 0; |
| 564 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 565 | // Link the instantiated function template declaration to the function |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 566 | // template from which it was instantiated. |
Douglas Gregor | 37d68185 | 2009-10-12 22:27:17 +0000 | [diff] [blame] | 567 | FunctionTemplateDecl *InstTemplate |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 568 | = Instantiated->getDescribedFunctionTemplate(); |
Douglas Gregor | 37d68185 | 2009-10-12 22:27:17 +0000 | [diff] [blame] | 569 | InstTemplate->setAccess(D->getAccess()); |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 570 | assert(InstTemplate && |
| 571 | "VisitFunctionDecl/CXXMethodDecl didn't create a template!"); |
| 572 | if (!InstTemplate->getInstantiatedFromMemberTemplate()) |
| 573 | InstTemplate->setInstantiatedFromMemberTemplate(D); |
| 574 | |
| 575 | // Add non-friends into the owner. |
| 576 | if (!InstTemplate->getFriendObjectKind()) |
| 577 | Owner->addDecl(InstTemplate); |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 578 | return InstTemplate; |
| 579 | } |
| 580 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 581 | Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 582 | CXXRecordDecl *PrevDecl = 0; |
| 583 | if (D->isInjectedClassName()) |
| 584 | PrevDecl = cast<CXXRecordDecl>(Owner); |
| 585 | |
| 586 | CXXRecordDecl *Record |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 587 | = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner, |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 588 | D->getLocation(), D->getIdentifier(), |
| 589 | D->getTagKeywordLoc(), PrevDecl); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 590 | Record->setImplicit(D->isImplicit()); |
Eli Friedman | eaba1af | 2009-08-27 19:11:42 +0000 | [diff] [blame] | 591 | // FIXME: Check against AS_none is an ugly hack to work around the issue that |
| 592 | // the tag decls introduced by friend class declarations don't have an access |
| 593 | // specifier. Remove once this area of the code gets sorted out. |
| 594 | if (D->getAccess() != AS_none) |
| 595 | Record->setAccess(D->getAccess()); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 596 | if (!D->isInjectedClassName()) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 597 | Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 598 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 599 | // If the original function was part of a friend declaration, |
| 600 | // inherit its namespace state. |
| 601 | if (Decl::FriendObjectKind FOK = D->getFriendObjectKind()) |
| 602 | Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared); |
| 603 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 604 | Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion()); |
| 605 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 606 | Owner->addDecl(Record); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 607 | return Record; |
| 608 | } |
| 609 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 610 | /// Normal class members are of more specific types and therefore |
| 611 | /// don't make it here. This function serves two purposes: |
| 612 | /// 1) instantiating function templates |
| 613 | /// 2) substituting friend declarations |
| 614 | /// FIXME: preserve function definitions in case #2 |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 615 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D, |
| 616 | TemplateParameterList *TemplateParams) { |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 617 | // Check whether there is already a function template specialization for |
| 618 | // this declaration. |
| 619 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
| 620 | void *InsertPos = 0; |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 621 | if (FunctionTemplate && !TemplateParams) { |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 622 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 623 | FunctionTemplateSpecializationInfo::Profile(ID, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 624 | TemplateArgs.getInnermost().getFlatArgumentList(), |
| 625 | TemplateArgs.getInnermost().flat_size(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 626 | SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 627 | |
| 628 | FunctionTemplateSpecializationInfo *Info |
| 629 | = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID, |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 630 | InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 631 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 632 | // If we already have a function template specialization, return it. |
| 633 | if (Info) |
| 634 | return Info->Function; |
| 635 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 636 | |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 637 | Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 639 | llvm::SmallVector<ParmVarDecl *, 4> Params; |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 640 | QualType T = SubstFunctionType(D, Params); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 641 | if (T.isNull()) |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 642 | return 0; |
John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 643 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 644 | // Build the instantiated method declaration. |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 645 | DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext(), |
| 646 | TemplateArgs); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 647 | FunctionDecl *Function = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 648 | FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 649 | D->getDeclName(), T, D->getTypeSourceInfo(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 650 | D->getStorageClass(), |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 651 | D->isInlineSpecified(), D->hasWrittenPrototype()); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 652 | Function->setLexicalDeclContext(Owner); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 653 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 654 | // Attach the parameters |
| 655 | for (unsigned P = 0; P < Params.size(); ++P) |
| 656 | Params[P]->setOwningFunction(Function); |
| 657 | Function->setParams(SemaRef.Context, Params.data(), Params.size()); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 658 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 659 | if (TemplateParams) { |
| 660 | // Our resulting instantiation is actually a function template, since we |
| 661 | // are substituting only the outer template parameters. For example, given |
| 662 | // |
| 663 | // template<typename T> |
| 664 | // struct X { |
| 665 | // template<typename U> friend void f(T, U); |
| 666 | // }; |
| 667 | // |
| 668 | // X<int> x; |
| 669 | // |
| 670 | // We are instantiating the friend function template "f" within X<int>, |
| 671 | // which means substituting int for T, but leaving "f" as a friend function |
| 672 | // template. |
| 673 | // Build the function template itself. |
| 674 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Owner, |
| 675 | Function->getLocation(), |
| 676 | Function->getDeclName(), |
| 677 | TemplateParams, Function); |
| 678 | Function->setDescribedFunctionTemplate(FunctionTemplate); |
| 679 | FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 680 | } else if (FunctionTemplate) { |
| 681 | // Record this function template specialization. |
| 682 | Function->setFunctionTemplateSpecialization(SemaRef.Context, |
| 683 | FunctionTemplate, |
| 684 | &TemplateArgs.getInnermost(), |
| 685 | InsertPos); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 686 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 687 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 688 | if (InitFunctionInstantiation(Function, D)) |
| 689 | Function->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 690 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 691 | bool Redeclaration = false; |
| 692 | bool OverloadableAttrRequired = false; |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 693 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 694 | LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(), |
| 695 | Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 696 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 697 | if (TemplateParams || !FunctionTemplate) { |
| 698 | // Look only into the namespace where the friend would be declared to |
| 699 | // find a previous declaration. This is the innermost enclosing namespace, |
| 700 | // as described in ActOnFriendFunctionDecl. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 701 | SemaRef.LookupQualifiedName(Previous, DC); |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 702 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 703 | // In C++, the previous declaration we find might be a tag type |
| 704 | // (class or enum). In this case, the new declaration will hide the |
| 705 | // tag type. Note that this does does not apply if we're declaring a |
| 706 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 707 | if (Previous.isSingleTagDecl()) |
| 708 | Previous.clear(); |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 709 | } |
| 710 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 711 | SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous, |
| 712 | false, Redeclaration, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 713 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 714 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 715 | // If the original function was part of a friend declaration, |
| 716 | // inherit its namespace state and add it to the owner. |
| 717 | NamedDecl *FromFriendD |
| 718 | = TemplateParams? cast<NamedDecl>(D->getDescribedFunctionTemplate()) : D; |
| 719 | if (FromFriendD->getFriendObjectKind()) { |
| 720 | NamedDecl *ToFriendD = 0; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 721 | NamedDecl *PrevDecl; |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 722 | if (TemplateParams) { |
| 723 | ToFriendD = cast<NamedDecl>(FunctionTemplate); |
| 724 | PrevDecl = FunctionTemplate->getPreviousDeclaration(); |
| 725 | } else { |
| 726 | ToFriendD = Function; |
| 727 | PrevDecl = Function->getPreviousDeclaration(); |
| 728 | } |
| 729 | ToFriendD->setObjectOfFriendDecl(PrevDecl != NULL); |
| 730 | if (!Owner->isDependentContext() && !PrevDecl) |
| 731 | DC->makeDeclVisibleInContext(ToFriendD, /* Recoverable = */ false); |
| 732 | |
| 733 | if (!TemplateParams) |
| 734 | Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
| 735 | } |
| 736 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 737 | return Function; |
| 738 | } |
| 739 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 740 | Decl * |
| 741 | TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D, |
| 742 | TemplateParameterList *TemplateParams) { |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 743 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
| 744 | void *InsertPos = 0; |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 745 | if (FunctionTemplate && !TemplateParams) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 | // We are creating a function template specialization from a function |
| 747 | // template. Check whether there is already a function template |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 748 | // specialization for this particular set of template arguments. |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 749 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 750 | FunctionTemplateSpecializationInfo::Profile(ID, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 751 | TemplateArgs.getInnermost().getFlatArgumentList(), |
| 752 | TemplateArgs.getInnermost().flat_size(), |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 753 | SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 754 | |
| 755 | FunctionTemplateSpecializationInfo *Info |
| 756 | = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 757 | InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 758 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 759 | // If we already have a function template specialization, return it. |
| 760 | if (Info) |
| 761 | return Info->Function; |
| 762 | } |
| 763 | |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 764 | Sema::LocalInstantiationScope Scope(SemaRef, TemplateParams != 0); |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 765 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 766 | llvm::SmallVector<ParmVarDecl *, 4> Params; |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 767 | QualType T = SubstFunctionType(D, Params); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 768 | if (T.isNull()) |
| 769 | return 0; |
| 770 | |
| 771 | // Build the instantiated method declaration. |
| 772 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 773 | CXXMethodDecl *Method = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 775 | DeclarationName Name = D->getDeclName(); |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 776 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 777 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); |
| 778 | Name = SemaRef.Context.DeclarationNames.getCXXConstructorName( |
| 779 | SemaRef.Context.getCanonicalType(ClassTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 780 | Method = CXXConstructorDecl::Create(SemaRef.Context, Record, |
| 781 | Constructor->getLocation(), |
| 782 | Name, T, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 783 | Constructor->getTypeSourceInfo(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 784 | Constructor->isExplicit(), |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 785 | Constructor->isInlineSpecified(), false); |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 786 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
| 787 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); |
| 788 | Name = SemaRef.Context.DeclarationNames.getCXXDestructorName( |
| 789 | SemaRef.Context.getCanonicalType(ClassTy)); |
| 790 | Method = CXXDestructorDecl::Create(SemaRef.Context, Record, |
| 791 | Destructor->getLocation(), Name, |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 792 | T, Destructor->isInlineSpecified(), false); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 793 | } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 794 | CanQualType ConvTy |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 795 | = SemaRef.Context.getCanonicalType( |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 796 | T->getAs<FunctionType>()->getResultType()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 797 | Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName( |
| 798 | ConvTy); |
| 799 | Method = CXXConversionDecl::Create(SemaRef.Context, Record, |
| 800 | Conversion->getLocation(), Name, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 801 | T, Conversion->getTypeSourceInfo(), |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 802 | Conversion->isInlineSpecified(), |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 803 | Conversion->isExplicit()); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 804 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 805 | Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 806 | D->getDeclName(), T, D->getTypeSourceInfo(), |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 807 | D->isStatic(), D->isInlineSpecified()); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 808 | } |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 809 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 810 | if (TemplateParams) { |
| 811 | // Our resulting instantiation is actually a function template, since we |
| 812 | // are substituting only the outer template parameters. For example, given |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 813 | // |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 814 | // template<typename T> |
| 815 | // struct X { |
| 816 | // template<typename U> void f(T, U); |
| 817 | // }; |
| 818 | // |
| 819 | // X<int> x; |
| 820 | // |
| 821 | // We are instantiating the member template "f" within X<int>, which means |
| 822 | // substituting int for T, but leaving "f" as a member function template. |
| 823 | // Build the function template itself. |
| 824 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, |
| 825 | Method->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 826 | Method->getDeclName(), |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 827 | TemplateParams, Method); |
| 828 | if (D->isOutOfLine()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 829 | FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 830 | Method->setDescribedFunctionTemplate(FunctionTemplate); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 831 | } else if (FunctionTemplate) { |
| 832 | // Record this function template specialization. |
| 833 | Method->setFunctionTemplateSpecialization(SemaRef.Context, |
| 834 | FunctionTemplate, |
| 835 | &TemplateArgs.getInnermost(), |
| 836 | InsertPos); |
| 837 | } else { |
| 838 | // Record that this is an instantiation of a member function. |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 839 | Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 842 | // If we are instantiating a member function defined |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 843 | // out-of-line, the instantiation will have the same lexical |
| 844 | // context (which will be a namespace scope) as the template. |
| 845 | if (D->isOutOfLine()) |
| 846 | Method->setLexicalDeclContext(D->getLexicalDeclContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 847 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 848 | // Attach the parameters |
| 849 | for (unsigned P = 0; P < Params.size(); ++P) |
| 850 | Params[P]->setOwningFunction(Method); |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 851 | Method->setParams(SemaRef.Context, Params.data(), Params.size()); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 852 | |
| 853 | if (InitMethodInstantiation(Method, D)) |
| 854 | Method->setInvalidDecl(); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 855 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 856 | LookupResult Previous(SemaRef, Name, SourceLocation(), |
| 857 | Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 858 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 859 | if (!FunctionTemplate || TemplateParams) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 860 | SemaRef.LookupQualifiedName(Previous, Owner); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 862 | // In C++, the previous declaration we find might be a tag type |
| 863 | // (class or enum). In this case, the new declaration will hide the |
| 864 | // tag type. Note that this does does not apply if we're declaring a |
| 865 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 866 | if (Previous.isSingleTagDecl()) |
| 867 | Previous.clear(); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 868 | } |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 869 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 870 | bool Redeclaration = false; |
| 871 | bool OverloadableAttrRequired = false; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 872 | SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 873 | /*FIXME:*/OverloadableAttrRequired); |
| 874 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 875 | if (D->isPure()) |
| 876 | SemaRef.CheckPureMethod(Method, SourceRange()); |
| 877 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 878 | if (!FunctionTemplate && (!Method->isInvalidDecl() || Previous.empty()) && |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 879 | !Method->getFriendObjectKind()) |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 880 | Owner->addDecl(Method); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 881 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 882 | return Method; |
| 883 | } |
| 884 | |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 885 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 886 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 889 | Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 890 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 893 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 894 | return VisitCXXMethodDecl(D); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 897 | ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 898 | QualType T; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 899 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 900 | if (DI) { |
| 901 | DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(), |
| 902 | D->getDeclName()); |
| 903 | if (DI) T = DI->getType(); |
| 904 | } else { |
| 905 | T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(), |
| 906 | D->getDeclName()); |
| 907 | DI = 0; |
| 908 | } |
| 909 | |
| 910 | if (T.isNull()) |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 911 | return 0; |
| 912 | |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 913 | T = SemaRef.adjustParameterType(T); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 914 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 915 | // Allocate the parameter |
John McCall | 58e4677 | 2009-10-23 21:48:59 +0000 | [diff] [blame] | 916 | ParmVarDecl *Param |
| 917 | = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 918 | D->getIdentifier(), T, DI, D->getStorageClass(), 0); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 919 | |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 920 | // Mark the default argument as being uninstantiated. |
Douglas Gregor | f43d0b3 | 2009-09-25 06:56:31 +0000 | [diff] [blame] | 921 | if (D->hasUninstantiatedDefaultArg()) |
| 922 | Param->setUninstantiatedDefaultArg(D->getUninstantiatedDefaultArg()); |
Douglas Gregor | 0ed0930 | 2009-09-25 07:03:22 +0000 | [diff] [blame] | 923 | else if (Expr *Arg = D->getDefaultArg()) |
| 924 | Param->setUninstantiatedDefaultArg(Arg); |
| 925 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 926 | // Note: we don't try to instantiate function parameters until after |
| 927 | // we've instantiated the function's type. Therefore, we don't have |
| 928 | // to check for 'void' parameter types here. |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 929 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 930 | return Param; |
| 931 | } |
| 932 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 933 | Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( |
| 934 | TemplateTypeParmDecl *D) { |
| 935 | // TODO: don't always clone when decls are refcounted. |
| 936 | const Type* T = D->getTypeForDecl(); |
| 937 | assert(T->isTemplateTypeParmType()); |
| 938 | const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 939 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 940 | TemplateTypeParmDecl *Inst = |
| 941 | TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 942 | TTPT->getDepth() - 1, TTPT->getIndex(), |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 943 | TTPT->getName(), |
| 944 | D->wasDeclaredWithTypename(), |
| 945 | D->isParameterPack()); |
| 946 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 947 | if (D->hasDefaultArgument()) |
| 948 | Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 949 | |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 950 | // Introduce this template parameter's instantiation into the instantiation |
| 951 | // scope. |
| 952 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 953 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 954 | return Inst; |
| 955 | } |
| 956 | |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 957 | Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl( |
| 958 | NonTypeTemplateParmDecl *D) { |
| 959 | // Substitute into the type of the non-type template parameter. |
| 960 | QualType T; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 961 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 962 | if (DI) { |
| 963 | DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(), |
| 964 | D->getDeclName()); |
| 965 | if (DI) T = DI->getType(); |
| 966 | } else { |
| 967 | T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(), |
| 968 | D->getDeclName()); |
| 969 | DI = 0; |
| 970 | } |
| 971 | if (T.isNull()) |
| 972 | return 0; |
| 973 | |
| 974 | // Check that this type is acceptable for a non-type template parameter. |
| 975 | bool Invalid = false; |
| 976 | T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation()); |
| 977 | if (T.isNull()) { |
| 978 | T = SemaRef.Context.IntTy; |
| 979 | Invalid = true; |
| 980 | } |
| 981 | |
| 982 | NonTypeTemplateParmDecl *Param |
| 983 | = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 984 | D->getDepth() - 1, D->getPosition(), |
| 985 | D->getIdentifier(), T, DI); |
| 986 | if (Invalid) |
| 987 | Param->setInvalidDecl(); |
| 988 | |
| 989 | Param->setDefaultArgument(D->getDefaultArgument()); |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 990 | |
| 991 | // Introduce this template parameter's instantiation into the instantiation |
| 992 | // scope. |
| 993 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 994 | return Param; |
| 995 | } |
| 996 | |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 997 | Decl * |
Douglas Gregor | 9106ef7 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 998 | TemplateDeclInstantiator::VisitTemplateTemplateParmDecl( |
| 999 | TemplateTemplateParmDecl *D) { |
| 1000 | // Instantiate the template parameter list of the template template parameter. |
| 1001 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1002 | TemplateParameterList *InstParams; |
| 1003 | { |
| 1004 | // Perform the actual substitution of template parameters within a new, |
| 1005 | // local instantiation scope. |
| 1006 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 1007 | InstParams = SubstTemplateParams(TempParams); |
| 1008 | if (!InstParams) |
| 1009 | return NULL; |
| 1010 | } |
| 1011 | |
| 1012 | // Build the template template parameter. |
| 1013 | TemplateTemplateParmDecl *Param |
| 1014 | = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 1015 | D->getDepth() - 1, D->getPosition(), |
| 1016 | D->getIdentifier(), InstParams); |
| 1017 | Param->setDefaultArgument(D->getDefaultArgument()); |
| 1018 | |
| 1019 | // Introduce this template parameter's instantiation into the instantiation |
| 1020 | // scope. |
| 1021 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
| 1022 | |
| 1023 | return Param; |
| 1024 | } |
| 1025 | |
Douglas Gregor | 48c32a7 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 1026 | Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| 1027 | // Using directives are never dependent, so they require no explicit |
| 1028 | |
| 1029 | UsingDirectiveDecl *Inst |
| 1030 | = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 1031 | D->getNamespaceKeyLocation(), |
| 1032 | D->getQualifierRange(), D->getQualifier(), |
| 1033 | D->getIdentLocation(), |
| 1034 | D->getNominatedNamespace(), |
| 1035 | D->getCommonAncestor()); |
| 1036 | Owner->addDecl(Inst); |
| 1037 | return Inst; |
| 1038 | } |
| 1039 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1040 | Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) { |
| 1041 | // The nested name specifier is non-dependent, so no transformation |
| 1042 | // is required. |
| 1043 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1044 | // We only need to do redeclaration lookups if we're in a class |
| 1045 | // scope (in fact, it's not really even possible in non-class |
| 1046 | // scopes). |
| 1047 | bool CheckRedeclaration = Owner->isRecord(); |
| 1048 | |
| 1049 | LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(), |
| 1050 | Sema::LookupUsingDeclName, Sema::ForRedeclaration); |
| 1051 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1052 | UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner, |
| 1053 | D->getLocation(), |
| 1054 | D->getNestedNameRange(), |
| 1055 | D->getUsingLocation(), |
| 1056 | D->getTargetNestedNameDecl(), |
| 1057 | D->getDeclName(), |
| 1058 | D->isTypeName()); |
| 1059 | |
| 1060 | CXXScopeSpec SS; |
| 1061 | SS.setScopeRep(D->getTargetNestedNameDecl()); |
| 1062 | SS.setRange(D->getNestedNameRange()); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1063 | |
| 1064 | if (CheckRedeclaration) { |
| 1065 | Prev.setHideTags(false); |
| 1066 | SemaRef.LookupQualifiedName(Prev, Owner); |
| 1067 | |
| 1068 | // Check for invalid redeclarations. |
| 1069 | if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(), |
| 1070 | D->isTypeName(), SS, |
| 1071 | D->getLocation(), Prev)) |
| 1072 | NewUD->setInvalidDecl(); |
| 1073 | |
| 1074 | } |
| 1075 | |
| 1076 | if (!NewUD->isInvalidDecl() && |
| 1077 | SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS, |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1078 | D->getLocation())) |
| 1079 | NewUD->setInvalidDecl(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1080 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1081 | SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D); |
| 1082 | NewUD->setAccess(D->getAccess()); |
| 1083 | Owner->addDecl(NewUD); |
| 1084 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1085 | // Don't process the shadow decls for an invalid decl. |
| 1086 | if (NewUD->isInvalidDecl()) |
| 1087 | return NewUD; |
| 1088 | |
| 1089 | // Process the shadow decls. |
| 1090 | for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end(); |
| 1091 | I != E; ++I) { |
| 1092 | UsingShadowDecl *Shadow = *I; |
| 1093 | NamedDecl *InstTarget = |
| 1094 | cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getTargetDecl(), |
| 1095 | TemplateArgs)); |
| 1096 | |
| 1097 | if (CheckRedeclaration && |
| 1098 | SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev)) |
| 1099 | continue; |
| 1100 | |
| 1101 | UsingShadowDecl *InstShadow |
| 1102 | = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget); |
| 1103 | SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow); |
| 1104 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1105 | |
| 1106 | return NewUD; |
| 1107 | } |
| 1108 | |
| 1109 | Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1110 | // Ignore these; we handle them in bulk when processing the UsingDecl. |
| 1111 | return 0; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1114 | Decl * TemplateDeclInstantiator |
| 1115 | ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1116 | NestedNameSpecifier *NNS = |
| 1117 | SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(), |
| 1118 | D->getTargetNestedNameRange(), |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 1119 | TemplateArgs); |
| 1120 | if (!NNS) |
| 1121 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1122 | |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 1123 | CXXScopeSpec SS; |
| 1124 | SS.setRange(D->getTargetNestedNameRange()); |
| 1125 | SS.setScopeRep(NNS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1126 | |
| 1127 | NamedDecl *UD = |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 1128 | SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(), |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1129 | D->getUsingLoc(), SS, D->getLocation(), |
| 1130 | D->getDeclName(), 0, |
| 1131 | /*instantiation*/ true, |
| 1132 | /*typename*/ true, D->getTypenameLoc()); |
| 1133 | if (UD) |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1134 | SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D); |
| 1135 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1136 | return UD; |
| 1137 | } |
| 1138 | |
| 1139 | Decl * TemplateDeclInstantiator |
| 1140 | ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
| 1141 | NestedNameSpecifier *NNS = |
| 1142 | SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(), |
| 1143 | D->getTargetNestedNameRange(), |
| 1144 | TemplateArgs); |
| 1145 | if (!NNS) |
| 1146 | return 0; |
| 1147 | |
| 1148 | CXXScopeSpec SS; |
| 1149 | SS.setRange(D->getTargetNestedNameRange()); |
| 1150 | SS.setScopeRep(NNS); |
| 1151 | |
| 1152 | NamedDecl *UD = |
| 1153 | SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(), |
| 1154 | D->getUsingLoc(), SS, D->getLocation(), |
| 1155 | D->getDeclName(), 0, |
| 1156 | /*instantiation*/ true, |
| 1157 | /*typename*/ false, SourceLocation()); |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1158 | if (UD) |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1159 | SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D); |
| 1160 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1161 | return UD; |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1164 | Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1165 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1166 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1167 | return Instantiator.Visit(D); |
| 1168 | } |
| 1169 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1170 | /// \brief Instantiates a nested template parameter list in the current |
| 1171 | /// instantiation context. |
| 1172 | /// |
| 1173 | /// \param L The parameter list to instantiate |
| 1174 | /// |
| 1175 | /// \returns NULL if there was an error |
| 1176 | TemplateParameterList * |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1177 | TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1178 | // Get errors for all the parameters before bailing out. |
| 1179 | bool Invalid = false; |
| 1180 | |
| 1181 | unsigned N = L->size(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1182 | typedef llvm::SmallVector<NamedDecl *, 8> ParamVector; |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1183 | ParamVector Params; |
| 1184 | Params.reserve(N); |
| 1185 | for (TemplateParameterList::iterator PI = L->begin(), PE = L->end(); |
| 1186 | PI != PE; ++PI) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1187 | NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI)); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1188 | Params.push_back(D); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 1189 | Invalid = Invalid || !D || D->isInvalidDecl(); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | // Clean up if we had an error. |
| 1193 | if (Invalid) { |
| 1194 | for (ParamVector::iterator PI = Params.begin(), PE = Params.end(); |
| 1195 | PI != PE; ++PI) |
| 1196 | if (*PI) |
| 1197 | (*PI)->Destroy(SemaRef.Context); |
| 1198 | return NULL; |
| 1199 | } |
| 1200 | |
| 1201 | TemplateParameterList *InstL |
| 1202 | = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), |
| 1203 | L->getLAngleLoc(), &Params.front(), N, |
| 1204 | L->getRAngleLoc()); |
| 1205 | return InstL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1206 | } |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1207 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1208 | /// \brief Instantiate the declaration of a class template partial |
| 1209 | /// specialization. |
| 1210 | /// |
| 1211 | /// \param ClassTemplate the (instantiated) class template that is partially |
| 1212 | // specialized by the instantiation of \p PartialSpec. |
| 1213 | /// |
| 1214 | /// \param PartialSpec the (uninstantiated) class template partial |
| 1215 | /// specialization that we are instantiating. |
| 1216 | /// |
| 1217 | /// \returns true if there was an error, false otherwise. |
| 1218 | bool |
| 1219 | TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization( |
| 1220 | ClassTemplateDecl *ClassTemplate, |
| 1221 | ClassTemplatePartialSpecializationDecl *PartialSpec) { |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1222 | // Create a local instantiation scope for this class template partial |
| 1223 | // specialization, which will contain the instantiations of the template |
| 1224 | // parameters. |
| 1225 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 1226 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1227 | // Substitute into the template parameters of the class template partial |
| 1228 | // specialization. |
| 1229 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); |
| 1230 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1231 | if (!InstParams) |
| 1232 | return true; |
| 1233 | |
| 1234 | // Substitute into the template arguments of the class template partial |
| 1235 | // specialization. |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1236 | const TemplateArgumentLoc *PartialSpecTemplateArgs |
| 1237 | = PartialSpec->getTemplateArgsAsWritten(); |
| 1238 | unsigned N = PartialSpec->getNumTemplateArgsAsWritten(); |
| 1239 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1240 | TemplateArgumentListInfo InstTemplateArgs; // no angle locations |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1241 | for (unsigned I = 0; I != N; ++I) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1242 | TemplateArgumentLoc Loc; |
| 1243 | if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs)) |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1244 | return true; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1245 | InstTemplateArgs.addArgument(Loc); |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | |
| 1249 | // Check that the template argument list is well-formed for this |
| 1250 | // class template. |
| 1251 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 1252 | InstTemplateArgs.size()); |
| 1253 | if (SemaRef.CheckTemplateArgumentList(ClassTemplate, |
| 1254 | PartialSpec->getLocation(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1255 | InstTemplateArgs, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1256 | false, |
| 1257 | Converted)) |
| 1258 | return true; |
| 1259 | |
| 1260 | // Figure out where to insert this class template partial specialization |
| 1261 | // in the member template's set of class template partial specializations. |
| 1262 | llvm::FoldingSetNodeID ID; |
| 1263 | ClassTemplatePartialSpecializationDecl::Profile(ID, |
| 1264 | Converted.getFlatArguments(), |
| 1265 | Converted.flatSize(), |
| 1266 | SemaRef.Context); |
| 1267 | void *InsertPos = 0; |
| 1268 | ClassTemplateSpecializationDecl *PrevDecl |
| 1269 | = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID, |
| 1270 | InsertPos); |
| 1271 | |
| 1272 | // Build the canonical type that describes the converted template |
| 1273 | // arguments of the class template partial specialization. |
| 1274 | QualType CanonType |
| 1275 | = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate), |
| 1276 | Converted.getFlatArguments(), |
| 1277 | Converted.flatSize()); |
| 1278 | |
| 1279 | // Build the fully-sugared type for this class template |
| 1280 | // specialization as the user wrote in the specialization |
| 1281 | // itself. This means that we'll pretty-print the type retrieved |
| 1282 | // from the specialization's declaration the way that the user |
| 1283 | // actually wrote the specialization, rather than formatting the |
| 1284 | // name based on the "canonical" representation used to store the |
| 1285 | // template arguments in the specialization. |
| 1286 | QualType WrittenTy |
| 1287 | = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1288 | InstTemplateArgs, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1289 | CanonType); |
| 1290 | |
| 1291 | if (PrevDecl) { |
| 1292 | // We've already seen a partial specialization with the same template |
| 1293 | // parameters and template arguments. This can happen, for example, when |
| 1294 | // substituting the outer template arguments ends up causing two |
| 1295 | // class template partial specializations of a member class template |
| 1296 | // to have identical forms, e.g., |
| 1297 | // |
| 1298 | // template<typename T, typename U> |
| 1299 | // struct Outer { |
| 1300 | // template<typename X, typename Y> struct Inner; |
| 1301 | // template<typename Y> struct Inner<T, Y>; |
| 1302 | // template<typename Y> struct Inner<U, Y>; |
| 1303 | // }; |
| 1304 | // |
| 1305 | // Outer<int, int> outer; // error: the partial specializations of Inner |
| 1306 | // // have the same signature. |
| 1307 | SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared) |
| 1308 | << WrittenTy; |
| 1309 | SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here) |
| 1310 | << SemaRef.Context.getTypeDeclType(PrevDecl); |
| 1311 | return true; |
| 1312 | } |
| 1313 | |
| 1314 | |
| 1315 | // Create the class template partial specialization declaration. |
| 1316 | ClassTemplatePartialSpecializationDecl *InstPartialSpec |
| 1317 | = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, Owner, |
| 1318 | PartialSpec->getLocation(), |
| 1319 | InstParams, |
| 1320 | ClassTemplate, |
| 1321 | Converted, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1322 | InstTemplateArgs, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1323 | 0); |
| 1324 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); |
| 1325 | InstPartialSpec->setTypeAsWritten(WrittenTy); |
| 1326 | |
| 1327 | // Add this partial specialization to the set of class template partial |
| 1328 | // specializations. |
| 1329 | ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec, |
| 1330 | InsertPos); |
| 1331 | return false; |
| 1332 | } |
| 1333 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1334 | /// \brief Does substitution on the type of the given function, including |
| 1335 | /// all of the function parameters. |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1336 | /// |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1337 | /// \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] | 1338 | /// |
| 1339 | /// \param Params the instantiated parameter declarations |
| 1340 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1341 | /// \returns the instantiated function's type if successful, a NULL |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1342 | /// type if there was an error. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1343 | QualType |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1344 | TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1345 | llvm::SmallVectorImpl<ParmVarDecl *> &Params) { |
| 1346 | bool InvalidDecl = false; |
| 1347 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1348 | // Substitute all of the function's formal parameter types. |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1349 | TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs); |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 1350 | llvm::SmallVector<QualType, 4> ParamTys; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1351 | for (FunctionDecl::param_iterator P = D->param_begin(), |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1352 | PEnd = D->param_end(); |
| 1353 | P != PEnd; ++P) { |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 1354 | if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) { |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1355 | if (PInst->getType()->isVoidType()) { |
| 1356 | SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type); |
| 1357 | PInst->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1358 | } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(), |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1359 | PInst->getType(), |
| 1360 | diag::err_abstract_type_in_decl, |
| 1361 | Sema::AbstractParamType)) |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1362 | PInst->setInvalidDecl(); |
| 1363 | |
| 1364 | Params.push_back(PInst); |
| 1365 | ParamTys.push_back(PInst->getType()); |
| 1366 | |
| 1367 | if (PInst->isInvalidDecl()) |
| 1368 | InvalidDecl = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | } else |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1370 | InvalidDecl = true; |
| 1371 | } |
| 1372 | |
| 1373 | // FIXME: Deallocate dead declarations. |
| 1374 | if (InvalidDecl) |
| 1375 | return QualType(); |
| 1376 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1377 | const FunctionProtoType *Proto = D->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1378 | assert(Proto && "Missing prototype?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1379 | QualType ResultType |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1380 | = SemaRef.SubstType(Proto->getResultType(), TemplateArgs, |
| 1381 | D->getLocation(), D->getDeclName()); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1382 | if (ResultType.isNull()) |
| 1383 | return QualType(); |
| 1384 | |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 1385 | return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(), |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1386 | Proto->isVariadic(), Proto->getTypeQuals(), |
| 1387 | D->getLocation(), D->getDeclName()); |
| 1388 | } |
| 1389 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1390 | /// \brief Initializes the common fields of an instantiation function |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1391 | /// declaration (New) from the corresponding fields of its template (Tmpl). |
| 1392 | /// |
| 1393 | /// \returns true if there was an error |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1394 | bool |
| 1395 | TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1396 | FunctionDecl *Tmpl) { |
| 1397 | if (Tmpl->isDeleted()) |
| 1398 | New->setDeleted(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1399 | |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1400 | // If we are performing substituting explicitly-specified template arguments |
| 1401 | // or deduced template arguments into a function template and we reach this |
| 1402 | // 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] | 1403 | // to keeping the new function template specialization. We therefore |
| 1404 | // convert the active template instantiation for the function template |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1405 | // into a template instantiation for this specific function template |
| 1406 | // specialization, which is not a SFINAE context, so that we diagnose any |
| 1407 | // further errors in the declaration itself. |
| 1408 | typedef Sema::ActiveTemplateInstantiation ActiveInstType; |
| 1409 | ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back(); |
| 1410 | if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || |
| 1411 | ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | if (FunctionTemplateDecl *FunTmpl |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1413 | = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | assert(FunTmpl->getTemplatedDecl() == Tmpl && |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1415 | "Deduction from the wrong function template?"); |
Daniel Dunbar | bcbb8bd | 2009-07-16 22:10:11 +0000 | [diff] [blame] | 1416 | (void) FunTmpl; |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1417 | ActiveInst.Kind = ActiveInstType::TemplateInstantiation; |
| 1418 | ActiveInst.Entity = reinterpret_cast<uintptr_t>(New); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 1419 | --SemaRef.NonInstantiationEntries; |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1420 | } |
| 1421 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1422 | |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 1423 | const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>(); |
| 1424 | assert(Proto && "Function template without prototype?"); |
| 1425 | |
| 1426 | if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() || |
| 1427 | Proto->getNoReturnAttr()) { |
| 1428 | // The function has an exception specification or a "noreturn" |
| 1429 | // attribute. Substitute into each of the exception types. |
| 1430 | llvm::SmallVector<QualType, 4> Exceptions; |
| 1431 | for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) { |
| 1432 | // FIXME: Poor location information! |
| 1433 | QualType T |
| 1434 | = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs, |
| 1435 | New->getLocation(), New->getDeclName()); |
| 1436 | if (T.isNull() || |
| 1437 | SemaRef.CheckSpecifiedExceptionType(T, New->getLocation())) |
| 1438 | continue; |
| 1439 | |
| 1440 | Exceptions.push_back(T); |
| 1441 | } |
| 1442 | |
| 1443 | // Rebuild the function type |
| 1444 | |
| 1445 | const FunctionProtoType *NewProto |
| 1446 | = New->getType()->getAs<FunctionProtoType>(); |
| 1447 | assert(NewProto && "Template instantiation without function prototype?"); |
| 1448 | New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(), |
| 1449 | NewProto->arg_type_begin(), |
| 1450 | NewProto->getNumArgs(), |
| 1451 | NewProto->isVariadic(), |
| 1452 | NewProto->getTypeQuals(), |
| 1453 | Proto->hasExceptionSpec(), |
| 1454 | Proto->hasAnyExceptionSpec(), |
| 1455 | Exceptions.size(), |
| 1456 | Exceptions.data(), |
| 1457 | Proto->getNoReturnAttr())); |
| 1458 | } |
| 1459 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1460 | return false; |
| 1461 | } |
| 1462 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1463 | /// \brief Initializes common fields of an instantiated method |
| 1464 | /// declaration (New) from the corresponding fields of its template |
| 1465 | /// (Tmpl). |
| 1466 | /// |
| 1467 | /// \returns true if there was an error |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1468 | bool |
| 1469 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1470 | CXXMethodDecl *Tmpl) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1471 | if (InitFunctionInstantiation(New, Tmpl)) |
| 1472 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1473 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1474 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 1475 | New->setAccess(Tmpl->getAccess()); |
Fariborz Jahanian | e7184df | 2009-12-03 18:44:40 +0000 | [diff] [blame] | 1476 | if (Tmpl->isVirtualAsWritten()) |
| 1477 | Record->setMethodAsVirtual(New); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1478 | |
| 1479 | // FIXME: attributes |
| 1480 | // FIXME: New needs a pointer to Tmpl |
| 1481 | return false; |
| 1482 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1483 | |
| 1484 | /// \brief Instantiate the definition of the given function from its |
| 1485 | /// template. |
| 1486 | /// |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1487 | /// \param PointOfInstantiation the point at which the instantiation was |
| 1488 | /// required. Note that this is not precisely a "point of instantiation" |
| 1489 | /// for the function, but it's close. |
| 1490 | /// |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1491 | /// \param Function the already-instantiated declaration of a |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1492 | /// function template specialization or member function of a class template |
| 1493 | /// specialization. |
| 1494 | /// |
| 1495 | /// \param Recursive if true, recursively instantiates any functions that |
| 1496 | /// are required by this instantiation. |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 1497 | /// |
| 1498 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 1499 | /// instantiation where the body of the function is required. Complain if |
| 1500 | /// there is no such body. |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 1501 | void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1502 | FunctionDecl *Function, |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 1503 | bool Recursive, |
| 1504 | bool DefinitionRequired) { |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 1505 | if (Function->isInvalidDecl()) |
| 1506 | return; |
| 1507 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1508 | assert(!Function->getBody() && "Already instantiated!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1510 | // Never instantiate an explicit specialization. |
| 1511 | if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
| 1512 | return; |
| 1513 | |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 1514 | // Find the function body that we'll be substituting. |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 1515 | const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern(); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 1516 | Stmt *Pattern = 0; |
| 1517 | if (PatternDecl) |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1518 | Pattern = PatternDecl->getBody(PatternDecl); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 1519 | |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 1520 | if (!Pattern) { |
| 1521 | if (DefinitionRequired) { |
| 1522 | if (Function->getPrimaryTemplate()) |
| 1523 | Diag(PointOfInstantiation, |
| 1524 | diag::err_explicit_instantiation_undefined_func_template) |
| 1525 | << Function->getPrimaryTemplate(); |
| 1526 | else |
| 1527 | Diag(PointOfInstantiation, |
| 1528 | diag::err_explicit_instantiation_undefined_member) |
| 1529 | << 1 << Function->getDeclName() << Function->getDeclContext(); |
| 1530 | |
| 1531 | if (PatternDecl) |
| 1532 | Diag(PatternDecl->getLocation(), |
| 1533 | diag::note_explicit_instantiation_here); |
| 1534 | } |
| 1535 | |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 1536 | return; |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 1537 | } |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 1538 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1539 | // C++0x [temp.explicit]p9: |
| 1540 | // Except for inline functions, other explicit instantiation declarations |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1541 | // have the effect of suppressing the implicit instantiation of the entity |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1542 | // to which they refer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1543 | if (Function->getTemplateSpecializationKind() |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1544 | == TSK_ExplicitInstantiationDeclaration && |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1545 | !PatternDecl->isInlined()) |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 1546 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1547 | |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 1548 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); |
| 1549 | if (Inst) |
| 1550 | return; |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 1551 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1552 | // If we're performing recursive template instantiation, create our own |
| 1553 | // queue of pending implicit instantiations that we will instantiate later, |
| 1554 | // while we're still within our own instantiation context. |
| 1555 | std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations; |
| 1556 | if (Recursive) |
| 1557 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 1559 | ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function)); |
| 1560 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 1561 | // Introduce a new scope where local variable instantiations will be |
| 1562 | // recorded. |
| 1563 | LocalInstantiationScope Scope(*this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1564 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 1565 | // Introduce the instantiated function parameters into the local |
| 1566 | // instantiation scope. |
| 1567 | for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) |
| 1568 | Scope.InstantiatedLocal(PatternDecl->getParamDecl(I), |
| 1569 | Function->getParamDecl(I)); |
| 1570 | |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 1571 | // Enter the scope of this instantiation. We don't use |
| 1572 | // PushDeclContext because we don't have a scope. |
| 1573 | DeclContext *PreviousContext = CurContext; |
| 1574 | CurContext = Function; |
| 1575 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | MultiLevelTemplateArgumentList TemplateArgs = |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1577 | getTemplateInstantiationArgs(Function); |
| 1578 | |
| 1579 | // If this is a constructor, instantiate the member initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1580 | if (const CXXConstructorDecl *Ctor = |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1581 | dyn_cast<CXXConstructorDecl>(PatternDecl)) { |
| 1582 | InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor, |
| 1583 | TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 1586 | // Instantiate the function body. |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1587 | OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs); |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 1588 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 1589 | if (Body.isInvalid()) |
| 1590 | Function->setInvalidDecl(); |
| 1591 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1592 | ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body), |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 1593 | /*IsInstantiation=*/true); |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 1594 | |
| 1595 | CurContext = PreviousContext; |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 1596 | |
| 1597 | DeclGroupRef DG(Function); |
| 1598 | Consumer.HandleTopLevelDecl(DG); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1599 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1600 | if (Recursive) { |
| 1601 | // Instantiate any pending implicit instantiations found during the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1602 | // instantiation of this template. |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1603 | PerformPendingImplicitInstantiations(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1604 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1605 | // Restore the set of pending implicit instantiations. |
| 1606 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); |
| 1607 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | /// \brief Instantiate the definition of the given variable from its |
| 1611 | /// template. |
| 1612 | /// |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1613 | /// \param PointOfInstantiation the point at which the instantiation was |
| 1614 | /// required. Note that this is not precisely a "point of instantiation" |
| 1615 | /// for the function, but it's close. |
| 1616 | /// |
| 1617 | /// \param Var the already-instantiated declaration of a static member |
| 1618 | /// variable of a class template specialization. |
| 1619 | /// |
| 1620 | /// \param Recursive if true, recursively instantiates any functions that |
| 1621 | /// are required by this instantiation. |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 1622 | /// |
| 1623 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 1624 | /// instantiation where an out-of-line definition of the member variable |
| 1625 | /// is required. Complain if there is no such definition. |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1626 | void Sema::InstantiateStaticDataMemberDefinition( |
| 1627 | SourceLocation PointOfInstantiation, |
| 1628 | VarDecl *Var, |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 1629 | bool Recursive, |
| 1630 | bool DefinitionRequired) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1631 | if (Var->isInvalidDecl()) |
| 1632 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1633 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1634 | // Find the out-of-line definition of this static data member. |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1635 | VarDecl *Def = Var->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1636 | assert(Def && "This data member was not instantiated from a template?"); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1637 | assert(Def->isStaticDataMember() && "Not a static data member?"); |
| 1638 | Def = Def->getOutOfLineDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1639 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1640 | if (!Def) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1641 | // We did not find an out-of-line definition of this static data member, |
| 1642 | // 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] | 1643 | // instantiate this definition (or provide a specialization for it) in |
| 1644 | // another translation unit. |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 1645 | if (DefinitionRequired) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1646 | Def = Var->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 1647 | Diag(PointOfInstantiation, |
| 1648 | diag::err_explicit_instantiation_undefined_member) |
| 1649 | << 2 << Var->getDeclName() << Var->getDeclContext(); |
| 1650 | Diag(Def->getLocation(), diag::note_explicit_instantiation_here); |
| 1651 | } |
| 1652 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1653 | return; |
| 1654 | } |
| 1655 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1656 | // Never instantiate an explicit specialization. |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1657 | if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1658 | return; |
| 1659 | |
| 1660 | // C++0x [temp.explicit]p9: |
| 1661 | // Except for inline functions, other explicit instantiation declarations |
| 1662 | // have the effect of suppressing the implicit instantiation of the entity |
| 1663 | // to which they refer. |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1664 | if (Var->getTemplateSpecializationKind() |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1665 | == TSK_ExplicitInstantiationDeclaration) |
| 1666 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1667 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1668 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
| 1669 | if (Inst) |
| 1670 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1671 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1672 | // If we're performing recursive template instantiation, create our own |
| 1673 | // queue of pending implicit instantiations that we will instantiate later, |
| 1674 | // while we're still within our own instantiation context. |
| 1675 | std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations; |
| 1676 | if (Recursive) |
| 1677 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1678 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1679 | // Enter the scope of this instantiation. We don't use |
| 1680 | // PushDeclContext because we don't have a scope. |
| 1681 | DeclContext *PreviousContext = CurContext; |
| 1682 | CurContext = Var->getDeclContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1683 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1684 | VarDecl *OldVar = Var; |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1685 | Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1686 | getTemplateInstantiationArgs(Var))); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1687 | CurContext = PreviousContext; |
| 1688 | |
| 1689 | if (Var) { |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1690 | Var->setPreviousDeclaration(OldVar); |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 1691 | MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo(); |
| 1692 | assert(MSInfo && "Missing member specialization information?"); |
| 1693 | Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(), |
| 1694 | MSInfo->getPointOfInstantiation()); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1695 | DeclGroupRef DG(Var); |
| 1696 | Consumer.HandleTopLevelDecl(DG); |
| 1697 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1698 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1699 | if (Recursive) { |
| 1700 | // Instantiate any pending implicit instantiations found during the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1701 | // instantiation of this template. |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1702 | PerformPendingImplicitInstantiations(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1703 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1704 | // Restore the set of pending implicit instantiations. |
| 1705 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1706 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1707 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1708 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1709 | void |
| 1710 | Sema::InstantiateMemInitializers(CXXConstructorDecl *New, |
| 1711 | const CXXConstructorDecl *Tmpl, |
| 1712 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1713 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1714 | llvm::SmallVector<MemInitTy*, 4> NewInits; |
| 1715 | |
| 1716 | // Instantiate all the initializers. |
| 1717 | for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(), |
Douglas Gregor | 72f6d67 | 2009-09-01 21:04:42 +0000 | [diff] [blame] | 1718 | InitsEnd = Tmpl->init_end(); |
| 1719 | Inits != InitsEnd; ++Inits) { |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1720 | CXXBaseOrMemberInitializer *Init = *Inits; |
| 1721 | |
| 1722 | ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1723 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1724 | // Instantiate all the arguments. |
| 1725 | for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end(); |
| 1726 | Args != ArgsEnd; ++Args) { |
| 1727 | OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs); |
| 1728 | |
| 1729 | if (NewArg.isInvalid()) |
| 1730 | New->setInvalidDecl(); |
| 1731 | else |
| 1732 | NewArgs.push_back(NewArg.takeAs<Expr>()); |
| 1733 | } |
| 1734 | |
| 1735 | MemInitResult NewInit; |
| 1736 | |
| 1737 | if (Init->isBaseInitializer()) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1738 | TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1739 | TemplateArgs, |
| 1740 | Init->getSourceLocation(), |
| 1741 | New->getDeclName()); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1742 | if (!BaseTInfo) { |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1743 | New->setInvalidDecl(); |
| 1744 | continue; |
| 1745 | } |
| 1746 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1747 | NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1748 | (Expr **)NewArgs.data(), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1749 | NewArgs.size(), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1750 | Init->getLParenLoc(), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1751 | Init->getRParenLoc(), |
| 1752 | New->getParent()); |
| 1753 | } else if (Init->isMemberInitializer()) { |
Anders Carlsson | 9988d5d | 2009-09-01 04:31:02 +0000 | [diff] [blame] | 1754 | FieldDecl *Member; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1755 | |
Anders Carlsson | 9988d5d | 2009-09-01 04:31:02 +0000 | [diff] [blame] | 1756 | // Is this an anonymous union? |
| 1757 | if (FieldDecl *UnionInit = Init->getAnonUnionMember()) |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 1758 | Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit, TemplateArgs)); |
Anders Carlsson | 9988d5d | 2009-09-01 04:31:02 +0000 | [diff] [blame] | 1759 | else |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 1760 | Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember(), |
| 1761 | TemplateArgs)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1762 | |
| 1763 | NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1764 | NewArgs.size(), |
| 1765 | Init->getSourceLocation(), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1766 | Init->getLParenLoc(), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1767 | Init->getRParenLoc()); |
| 1768 | } |
| 1769 | |
| 1770 | if (NewInit.isInvalid()) |
| 1771 | New->setInvalidDecl(); |
| 1772 | else { |
| 1773 | // FIXME: It would be nice if ASTOwningVector had a release function. |
| 1774 | NewArgs.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1775 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1776 | NewInits.push_back((MemInitTy *)NewInit.get()); |
| 1777 | } |
| 1778 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1779 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1780 | // Assign all the initializers to the new constructor. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1781 | ActOnMemInitializers(DeclPtrTy::make(New), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1782 | /*FIXME: ColonLoc */ |
| 1783 | SourceLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1784 | NewInits.data(), NewInits.size()); |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1787 | // TODO: this could be templated if the various decl types used the |
| 1788 | // same method name. |
| 1789 | static bool isInstantiationOf(ClassTemplateDecl *Pattern, |
| 1790 | ClassTemplateDecl *Instance) { |
| 1791 | Pattern = Pattern->getCanonicalDecl(); |
| 1792 | |
| 1793 | do { |
| 1794 | Instance = Instance->getCanonicalDecl(); |
| 1795 | if (Pattern == Instance) return true; |
| 1796 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 1797 | } while (Instance); |
| 1798 | |
| 1799 | return false; |
| 1800 | } |
| 1801 | |
Douglas Gregor | 0d69653 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 1802 | static bool isInstantiationOf(FunctionTemplateDecl *Pattern, |
| 1803 | FunctionTemplateDecl *Instance) { |
| 1804 | Pattern = Pattern->getCanonicalDecl(); |
| 1805 | |
| 1806 | do { |
| 1807 | Instance = Instance->getCanonicalDecl(); |
| 1808 | if (Pattern == Instance) return true; |
| 1809 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 1810 | } while (Instance); |
| 1811 | |
| 1812 | return false; |
| 1813 | } |
| 1814 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1815 | static bool |
| 1816 | isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern, |
| 1817 | ClassTemplatePartialSpecializationDecl *Instance) { |
| 1818 | Pattern |
| 1819 | = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl()); |
| 1820 | do { |
| 1821 | Instance = cast<ClassTemplatePartialSpecializationDecl>( |
| 1822 | Instance->getCanonicalDecl()); |
| 1823 | if (Pattern == Instance) |
| 1824 | return true; |
| 1825 | Instance = Instance->getInstantiatedFromMember(); |
| 1826 | } while (Instance); |
| 1827 | |
| 1828 | return false; |
| 1829 | } |
| 1830 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1831 | static bool isInstantiationOf(CXXRecordDecl *Pattern, |
| 1832 | CXXRecordDecl *Instance) { |
| 1833 | Pattern = Pattern->getCanonicalDecl(); |
| 1834 | |
| 1835 | do { |
| 1836 | Instance = Instance->getCanonicalDecl(); |
| 1837 | if (Pattern == Instance) return true; |
| 1838 | Instance = Instance->getInstantiatedFromMemberClass(); |
| 1839 | } while (Instance); |
| 1840 | |
| 1841 | return false; |
| 1842 | } |
| 1843 | |
| 1844 | static bool isInstantiationOf(FunctionDecl *Pattern, |
| 1845 | FunctionDecl *Instance) { |
| 1846 | Pattern = Pattern->getCanonicalDecl(); |
| 1847 | |
| 1848 | do { |
| 1849 | Instance = Instance->getCanonicalDecl(); |
| 1850 | if (Pattern == Instance) return true; |
| 1851 | Instance = Instance->getInstantiatedFromMemberFunction(); |
| 1852 | } while (Instance); |
| 1853 | |
| 1854 | return false; |
| 1855 | } |
| 1856 | |
| 1857 | static bool isInstantiationOf(EnumDecl *Pattern, |
| 1858 | EnumDecl *Instance) { |
| 1859 | Pattern = Pattern->getCanonicalDecl(); |
| 1860 | |
| 1861 | do { |
| 1862 | Instance = Instance->getCanonicalDecl(); |
| 1863 | if (Pattern == Instance) return true; |
| 1864 | Instance = Instance->getInstantiatedFromMemberEnum(); |
| 1865 | } while (Instance); |
| 1866 | |
| 1867 | return false; |
| 1868 | } |
| 1869 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1870 | static bool isInstantiationOf(UsingShadowDecl *Pattern, |
| 1871 | UsingShadowDecl *Instance, |
| 1872 | ASTContext &C) { |
| 1873 | return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern; |
| 1874 | } |
| 1875 | |
| 1876 | static bool isInstantiationOf(UsingDecl *Pattern, |
| 1877 | UsingDecl *Instance, |
| 1878 | ASTContext &C) { |
| 1879 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; |
| 1880 | } |
| 1881 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1882 | static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern, |
| 1883 | UsingDecl *Instance, |
| 1884 | ASTContext &C) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1885 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern, |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1889 | UsingDecl *Instance, |
| 1890 | ASTContext &C) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1891 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1894 | static bool isInstantiationOfStaticDataMember(VarDecl *Pattern, |
| 1895 | VarDecl *Instance) { |
| 1896 | assert(Instance->isStaticDataMember()); |
| 1897 | |
| 1898 | Pattern = Pattern->getCanonicalDecl(); |
| 1899 | |
| 1900 | do { |
| 1901 | Instance = Instance->getCanonicalDecl(); |
| 1902 | if (Pattern == Instance) return true; |
| 1903 | Instance = Instance->getInstantiatedFromStaticDataMember(); |
| 1904 | } while (Instance); |
| 1905 | |
| 1906 | return false; |
| 1907 | } |
| 1908 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1909 | // Other is the prospective instantiation |
| 1910 | // D is the prospective pattern |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1911 | static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1912 | if (D->getKind() != Other->getKind()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1913 | if (UnresolvedUsingTypenameDecl *UUD |
| 1914 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 1915 | if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { |
| 1916 | return isInstantiationOf(UUD, UD, Ctx); |
| 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | if (UnresolvedUsingValueDecl *UUD |
| 1921 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1922 | if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { |
| 1923 | return isInstantiationOf(UUD, UD, Ctx); |
| 1924 | } |
| 1925 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1926 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1927 | return false; |
| 1928 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1929 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1930 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other)) |
| 1931 | return isInstantiationOf(cast<CXXRecordDecl>(D), Record); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1932 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1933 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other)) |
| 1934 | return isInstantiationOf(cast<FunctionDecl>(D), Function); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1935 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1936 | if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other)) |
| 1937 | return isInstantiationOf(cast<EnumDecl>(D), Enum); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1938 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1939 | if (VarDecl *Var = dyn_cast<VarDecl>(Other)) |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1940 | if (Var->isStaticDataMember()) |
| 1941 | return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var); |
| 1942 | |
| 1943 | if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other)) |
| 1944 | return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp); |
Douglas Gregor | a5bf7f1 | 2009-08-28 22:03:51 +0000 | [diff] [blame] | 1945 | |
Douglas Gregor | 0d69653 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 1946 | if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other)) |
| 1947 | return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp); |
| 1948 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1949 | if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 1950 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other)) |
| 1951 | return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D), |
| 1952 | PartialSpec); |
| 1953 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1954 | if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) { |
| 1955 | if (!Field->getDeclName()) { |
| 1956 | // This is an unnamed field. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1957 | return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) == |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1958 | cast<FieldDecl>(D); |
| 1959 | } |
| 1960 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1961 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1962 | if (UsingDecl *Using = dyn_cast<UsingDecl>(Other)) |
| 1963 | return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx); |
| 1964 | |
| 1965 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other)) |
| 1966 | return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx); |
| 1967 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1968 | return D->getDeclName() && isa<NamedDecl>(Other) && |
| 1969 | D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); |
| 1970 | } |
| 1971 | |
| 1972 | template<typename ForwardIterator> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1973 | static NamedDecl *findInstantiationOf(ASTContext &Ctx, |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1974 | NamedDecl *D, |
| 1975 | ForwardIterator first, |
| 1976 | ForwardIterator last) { |
| 1977 | for (; first != last; ++first) |
| 1978 | if (isInstantiationOf(Ctx, D, *first)) |
| 1979 | return cast<NamedDecl>(*first); |
| 1980 | |
| 1981 | return 0; |
| 1982 | } |
| 1983 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1984 | /// \brief Finds the instantiation of the given declaration context |
| 1985 | /// within the current instantiation. |
| 1986 | /// |
| 1987 | /// \returns NULL if there was an error |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 1988 | DeclContext *Sema::FindInstantiatedContext(DeclContext* DC, |
| 1989 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1990 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) { |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 1991 | Decl* ID = FindInstantiatedDecl(D, TemplateArgs); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1992 | return cast_or_null<DeclContext>(ID); |
| 1993 | } else return DC; |
| 1994 | } |
| 1995 | |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 1996 | /// \brief Find the instantiation of the given declaration within the |
| 1997 | /// current instantiation. |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1998 | /// |
| 1999 | /// This routine is intended to be used when \p D is a declaration |
| 2000 | /// referenced from within a template, that needs to mapped into the |
| 2001 | /// corresponding declaration within an instantiation. For example, |
| 2002 | /// given: |
| 2003 | /// |
| 2004 | /// \code |
| 2005 | /// template<typename T> |
| 2006 | /// struct X { |
| 2007 | /// enum Kind { |
| 2008 | /// KnownValue = sizeof(T) |
| 2009 | /// }; |
| 2010 | /// |
| 2011 | /// bool getKind() const { return KnownValue; } |
| 2012 | /// }; |
| 2013 | /// |
| 2014 | /// template struct X<int>; |
| 2015 | /// \endcode |
| 2016 | /// |
| 2017 | /// In the instantiation of X<int>::getKind(), we need to map the |
| 2018 | /// EnumConstantDecl for KnownValue (which refers to |
| 2019 | /// X<T>::<Kind>::KnownValue) to its instantiation |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 2020 | /// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs |
| 2021 | /// this mapping from within the instantiation of X<int>. |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2022 | NamedDecl *Sema::FindInstantiatedDecl(NamedDecl *D, |
| 2023 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2024 | DeclContext *ParentDC = D->getDeclContext(); |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 2025 | if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) || |
| 2026 | isa<TemplateTypeParmDecl>(D) || isa<TemplateTypeParmDecl>(D) || |
| 2027 | ParentDC->isFunctionOrMethod()) { |
Douglas Gregor | 2bba76b | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 2028 | // D is a local of some kind. Look into the map of local |
| 2029 | // declarations to their instantiations. |
| 2030 | return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D)); |
| 2031 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2032 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2033 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 2034 | if (!Record->isDependentContext()) |
| 2035 | return D; |
| 2036 | |
| 2037 | // If the RecordDecl is actually the injected-class-name or a "templated" |
| 2038 | // declaration for a class template or class template partial |
| 2039 | // specialization, substitute into the injected-class-name of the |
| 2040 | // class template or partial specialization to find the new DeclContext. |
| 2041 | QualType T; |
| 2042 | ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate(); |
| 2043 | |
| 2044 | if (ClassTemplate) { |
| 2045 | T = ClassTemplate->getInjectedClassNameType(Context); |
| 2046 | } else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 2047 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 2048 | T = Context.getTypeDeclType(Record); |
| 2049 | ClassTemplate = PartialSpec->getSpecializedTemplate(); |
| 2050 | } |
| 2051 | |
| 2052 | if (!T.isNull()) { |
| 2053 | // Substitute into the injected-class-name to get the type corresponding |
| 2054 | // to the instantiation we want. This substitution should never fail, |
| 2055 | // since we know we can instantiate the injected-class-name or we wouldn't |
| 2056 | // have gotten to the injected-class-name! |
| 2057 | // FIXME: Can we use the CurrentInstantiationScope to avoid this extra |
| 2058 | // instantiation in the common case? |
| 2059 | T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName()); |
| 2060 | assert(!T.isNull() && "Instantiation of injected-class-name cannot fail."); |
| 2061 | |
| 2062 | if (!T->isDependentType()) { |
| 2063 | assert(T->isRecordType() && "Instantiation must produce a record type"); |
| 2064 | return T->getAs<RecordType>()->getDecl(); |
| 2065 | } |
| 2066 | |
| 2067 | // We are performing "partial" template instantiation to create the |
| 2068 | // member declarations for the members of a class template |
| 2069 | // specialization. Therefore, D is actually referring to something in |
| 2070 | // the current instantiation. Look through the current context, |
| 2071 | // which contains actual instantiations, to find the instantiation of |
| 2072 | // the "current instantiation" that D refers to. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2073 | for (DeclContext *DC = CurContext; !DC->isFileContext(); |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2074 | DC = DC->getParent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2075 | if (ClassTemplateSpecializationDecl *Spec |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2076 | = dyn_cast<ClassTemplateSpecializationDecl>(DC)) |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2077 | if (isInstantiationOf(ClassTemplate, |
| 2078 | Spec->getSpecializedTemplate())) |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2079 | return Spec; |
| 2080 | } |
| 2081 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2082 | assert(false && |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2083 | "Unable to find declaration for the current instantiation"); |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2084 | return Record; |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2085 | } |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2086 | |
| 2087 | // Fall through to deal with other dependent record types (e.g., |
| 2088 | // anonymous unions in class templates). |
| 2089 | } |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2090 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2091 | if (!ParentDC->isDependentContext()) |
| 2092 | return D; |
| 2093 | |
| 2094 | ParentDC = FindInstantiatedContext(ParentDC, TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2095 | if (!ParentDC) |
Douglas Gregor | 44c7384 | 2009-09-01 17:53:10 +0000 | [diff] [blame] | 2096 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2097 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2098 | if (ParentDC != D->getDeclContext()) { |
| 2099 | // We performed some kind of instantiation in the parent context, |
| 2100 | // so now we need to look into the instantiated parent context to |
| 2101 | // find the instantiation of the declaration D. |
| 2102 | NamedDecl *Result = 0; |
| 2103 | if (D->getDeclName()) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2104 | DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName()); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2105 | Result = findInstantiationOf(Context, D, Found.first, Found.second); |
| 2106 | } else { |
| 2107 | // Since we don't have a name for the entity we're looking for, |
| 2108 | // our only option is to walk through all of the declarations to |
| 2109 | // find that name. This will occur in a few cases: |
| 2110 | // |
| 2111 | // - anonymous struct/union within a template |
| 2112 | // - unnamed class/struct/union/enum within a template |
| 2113 | // |
| 2114 | // FIXME: Find a better way to find these instantiations! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2115 | Result = findInstantiationOf(Context, D, |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2116 | ParentDC->decls_begin(), |
| 2117 | ParentDC->decls_end()); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2118 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2119 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2120 | // UsingShadowDecls can instantiate to nothing because of using hiding. |
| 2121 | assert((Result || isa<UsingShadowDecl>(D)) |
| 2122 | && "Unable to find instantiation of declaration!"); |
| 2123 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2124 | D = Result; |
| 2125 | } |
| 2126 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2127 | return D; |
| 2128 | } |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2129 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2130 | /// \brief Performs template instantiation for all implicit template |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2131 | /// instantiations we have seen until this point. |
| 2132 | void Sema::PerformPendingImplicitInstantiations() { |
| 2133 | while (!PendingImplicitInstantiations.empty()) { |
| 2134 | PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front(); |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2135 | PendingImplicitInstantiations.pop_front(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2136 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2137 | // Instantiate function definitions |
| 2138 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2139 | PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function), |
Anders Carlsson | c17fb7b | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 2140 | Function->getLocation(), *this, |
| 2141 | Context.getSourceManager(), |
| 2142 | "instantiating function definition"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2143 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 2144 | if (!Function->getBody()) |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2145 | InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2146 | continue; |
| 2147 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2148 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2149 | // Instantiate static data member definitions. |
| 2150 | VarDecl *Var = cast<VarDecl>(Inst.first); |
| 2151 | assert(Var->isStaticDataMember() && "Not a static data member?"); |
Anders Carlsson | c17fb7b | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 2152 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2153 | PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var), |
Anders Carlsson | c17fb7b | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 2154 | Var->getLocation(), *this, |
| 2155 | Context.getSourceManager(), |
| 2156 | "instantiating static data member " |
| 2157 | "definition"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2158 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2159 | InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2160 | } |
| 2161 | } |