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