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