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