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