Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1 | //===--- SemaTemplateInstantiateDecl.cpp - C++ Template Decl Instantiation ===/ |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | //===----------------------------------------------------------------------===/ |
| 8 | // |
| 9 | // This file implements C++ template instantiation for declarations. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===/ |
| 12 | #include "Sema.h" |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 13 | #include "Lookup.h" |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/DeclTemplate.h" |
| 17 | #include "clang/AST/DeclVisitor.h" |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 18 | #include "clang/AST/DependentDiagnostic.h" |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | a88cfbf | 2009-12-12 18:16:41 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeLoc.h" |
Anders Carlsson | c17fb7b | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 22 | #include "clang/Basic/PrettyStackTrace.h" |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | namespace { |
Benjamin Kramer | 85b4521 | 2009-11-28 19:45:26 +0000 | [diff] [blame] | 28 | class TemplateDeclInstantiator |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 29 | : public DeclVisitor<TemplateDeclInstantiator, Decl *> { |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 30 | Sema &SemaRef; |
| 31 | DeclContext *Owner; |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 32 | const MultiLevelTemplateArgumentList &TemplateArgs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | |
Anders Carlsson | d8fe2d5 | 2009-11-07 06:07:58 +0000 | [diff] [blame] | 34 | void InstantiateAttrs(Decl *Tmpl, Decl *New); |
| 35 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 36 | public: |
| 37 | typedef Sema::OwningExprResult OwningExprResult; |
| 38 | |
| 39 | TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 40 | const MultiLevelTemplateArgumentList &TemplateArgs) |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 41 | : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 43 | // FIXME: Once we get closer to completion, replace these manually-written |
| 44 | // declarations with automatically-generated ones from |
| 45 | // clang/AST/DeclNodes.def. |
Douglas Gregor | 4f722be | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 46 | Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 47 | Decl *VisitNamespaceDecl(NamespaceDecl *D); |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 48 | Decl *VisitNamespaceAliasDecl(NamespaceAliasDecl *D); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 49 | Decl *VisitTypedefDecl(TypedefDecl *D); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 50 | Decl *VisitVarDecl(VarDecl *D); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 51 | Decl *VisitFieldDecl(FieldDecl *D); |
| 52 | Decl *VisitStaticAssertDecl(StaticAssertDecl *D); |
| 53 | Decl *VisitEnumDecl(EnumDecl *D); |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 54 | Decl *VisitEnumConstantDecl(EnumConstantDecl *D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 55 | Decl *VisitFriendDecl(FriendDecl *D); |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 56 | Decl *VisitFunctionDecl(FunctionDecl *D, |
| 57 | TemplateParameterList *TemplateParams = 0); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 58 | Decl *VisitCXXRecordDecl(CXXRecordDecl *D); |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 59 | Decl *VisitCXXMethodDecl(CXXMethodDecl *D, |
| 60 | TemplateParameterList *TemplateParams = 0); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 61 | Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 62 | Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 63 | Decl *VisitCXXConversionDecl(CXXConversionDecl *D); |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 64 | ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 65 | Decl *VisitClassTemplateDecl(ClassTemplateDecl *D); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 66 | Decl *VisitClassTemplatePartialSpecializationDecl( |
| 67 | ClassTemplatePartialSpecializationDecl *D); |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 68 | Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 69 | Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 70 | Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); |
Douglas Gregor | 9106ef7 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 71 | Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); |
Douglas Gregor | 48c32a7 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 72 | Decl *VisitUsingDirectiveDecl(UsingDirectiveDecl *D); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 73 | Decl *VisitUsingDecl(UsingDecl *D); |
| 74 | Decl *VisitUsingShadowDecl(UsingShadowDecl *D); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 75 | Decl *VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); |
| 76 | Decl *VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 78 | // Base case. FIXME: Remove once we can instantiate everything. |
Douglas Gregor | 48c32a7 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 79 | Decl *VisitDecl(Decl *D) { |
| 80 | unsigned DiagID = SemaRef.getDiagnostics().getCustomDiagID( |
| 81 | Diagnostic::Error, |
| 82 | "cannot instantiate %0 yet"); |
| 83 | SemaRef.Diag(D->getLocation(), DiagID) |
| 84 | << D->getDeclKindName(); |
| 85 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 86 | return 0; |
| 87 | } |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 88 | |
John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 89 | const LangOptions &getLangOptions() { |
| 90 | return SemaRef.getLangOptions(); |
| 91 | } |
| 92 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 93 | // Helper functions for instantiating methods. |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 94 | TypeSourceInfo *SubstFunctionType(FunctionDecl *D, |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 95 | llvm::SmallVectorImpl<ParmVarDecl *> &Params); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 96 | bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 97 | bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 98 | |
| 99 | TemplateParameterList * |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 100 | SubstTemplateParams(TemplateParameterList *List); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 101 | |
| 102 | bool SubstQualifier(const DeclaratorDecl *OldDecl, |
| 103 | DeclaratorDecl *NewDecl); |
| 104 | bool SubstQualifier(const TagDecl *OldDecl, |
| 105 | TagDecl *NewDecl); |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 106 | |
| 107 | bool InstantiateClassTemplatePartialSpecialization( |
| 108 | ClassTemplateDecl *ClassTemplate, |
| 109 | ClassTemplatePartialSpecializationDecl *PartialSpec); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 110 | }; |
| 111 | } |
| 112 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 113 | bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl, |
| 114 | DeclaratorDecl *NewDecl) { |
| 115 | NestedNameSpecifier *OldQual = OldDecl->getQualifier(); |
| 116 | if (!OldQual) return false; |
| 117 | |
| 118 | SourceRange QualRange = OldDecl->getQualifierRange(); |
| 119 | |
| 120 | NestedNameSpecifier *NewQual |
| 121 | = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs); |
| 122 | if (!NewQual) |
| 123 | return true; |
| 124 | |
| 125 | NewDecl->setQualifierInfo(NewQual, QualRange); |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl, |
| 130 | TagDecl *NewDecl) { |
| 131 | NestedNameSpecifier *OldQual = OldDecl->getQualifier(); |
| 132 | if (!OldQual) return false; |
| 133 | |
| 134 | SourceRange QualRange = OldDecl->getQualifierRange(); |
| 135 | |
| 136 | NestedNameSpecifier *NewQual |
| 137 | = SemaRef.SubstNestedNameSpecifier(OldQual, QualRange, TemplateArgs); |
| 138 | if (!NewQual) |
| 139 | return true; |
| 140 | |
| 141 | NewDecl->setQualifierInfo(NewQual, QualRange); |
| 142 | return false; |
| 143 | } |
| 144 | |
Anders Carlsson | d8fe2d5 | 2009-11-07 06:07:58 +0000 | [diff] [blame] | 145 | // FIXME: Is this too simple? |
| 146 | void TemplateDeclInstantiator::InstantiateAttrs(Decl *Tmpl, Decl *New) { |
| 147 | for (const Attr *TmplAttr = Tmpl->getAttrs(); TmplAttr; |
| 148 | TmplAttr = TmplAttr->getNext()) { |
| 149 | |
| 150 | // FIXME: Is cloning correct for all attributes? |
| 151 | Attr *NewAttr = TmplAttr->clone(SemaRef.Context); |
| 152 | |
| 153 | New->addAttr(NewAttr); |
| 154 | } |
| 155 | } |
| 156 | |
Douglas Gregor | 4f722be | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 157 | Decl * |
| 158 | TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 159 | assert(false && "Translation units cannot be instantiated"); |
| 160 | return D; |
| 161 | } |
| 162 | |
| 163 | Decl * |
| 164 | TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { |
| 165 | assert(false && "Namespaces cannot be instantiated"); |
| 166 | return D; |
| 167 | } |
| 168 | |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 169 | Decl * |
| 170 | TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 171 | NamespaceAliasDecl *Inst |
| 172 | = NamespaceAliasDecl::Create(SemaRef.Context, Owner, |
| 173 | D->getNamespaceLoc(), |
| 174 | D->getAliasLoc(), |
| 175 | D->getNamespace()->getIdentifier(), |
| 176 | D->getQualifierRange(), |
| 177 | D->getQualifier(), |
| 178 | D->getTargetNameLoc(), |
| 179 | D->getNamespace()); |
| 180 | Owner->addDecl(Inst); |
| 181 | return Inst; |
| 182 | } |
| 183 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 184 | Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { |
| 185 | bool Invalid = false; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 186 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 187 | if (DI->getType()->isDependentType()) { |
| 188 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 189 | D->getLocation(), D->getDeclName()); |
| 190 | if (!DI) { |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 191 | Invalid = true; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 192 | DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 193 | } |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 194 | } else { |
| 195 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 196 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 198 | // Create the new typedef |
| 199 | TypedefDecl *Typedef |
| 200 | = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 201 | D->getIdentifier(), DI); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 202 | if (Invalid) |
| 203 | Typedef->setInvalidDecl(); |
| 204 | |
Douglas Gregor | d57a38e | 2010-04-23 16:25:07 +0000 | [diff] [blame] | 205 | if (const TagType *TT = DI->getType()->getAs<TagType>()) { |
| 206 | TagDecl *TD = TT->getDecl(); |
| 207 | |
| 208 | // If the TagDecl that the TypedefDecl points to is an anonymous decl |
| 209 | // keep track of the TypedefDecl. |
| 210 | if (!TD->getIdentifier() && !TD->getTypedefForAnonDecl()) |
| 211 | TD->setTypedefForAnonDecl(Typedef); |
| 212 | } |
| 213 | |
John McCall | 5126fd0 | 2009-12-30 00:31:22 +0000 | [diff] [blame] | 214 | if (TypedefDecl *Prev = D->getPreviousDeclaration()) { |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 215 | NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev, |
| 216 | TemplateArgs); |
John McCall | 5126fd0 | 2009-12-30 00:31:22 +0000 | [diff] [blame] | 217 | Typedef->setPreviousDeclaration(cast<TypedefDecl>(InstPrev)); |
| 218 | } |
| 219 | |
Douglas Gregor | 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()); |
| 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); |
Douglas Gregor | ac7c2c8 | 2010-05-17 16:38:00 +0000 | [diff] [blame] | 1052 | if (!SemaRef.getLangOptions().CPlusPlus0x) { |
| 1053 | // C++03 [temp.friend]p4: |
| 1054 | // When a function is defined in a friend function declaration in a |
| 1055 | // class template, the function is defined at each instantiation of the |
| 1056 | // class template. The function is defined even if it is never used. |
| 1057 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Owner)) { |
| 1058 | if (ClassTemplateSpecializationDecl *Spec |
| 1059 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 1060 | InstantiateAtPOI = Spec->getPointOfInstantiation(); |
| 1061 | else if (MemberSpecializationInfo *MSInfo |
| 1062 | = Record->getMemberSpecializationInfo()) |
| 1063 | InstantiateAtPOI = MSInfo->getPointOfInstantiation(); |
| 1064 | } |
| 1065 | |
| 1066 | if (InstantiateAtPOI.isInvalid()) |
| 1067 | InstantiateAtPOI = Function->getLocation(); |
| 1068 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1069 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1070 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1071 | if (InitFunctionInstantiation(Function, D)) |
| 1072 | Function->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1073 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1074 | bool Redeclaration = false; |
| 1075 | bool OverloadableAttrRequired = false; |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1076 | bool isExplicitSpecialization = false; |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1077 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1078 | LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(), |
| 1079 | Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 1080 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1081 | if (DependentFunctionTemplateSpecializationInfo *Info |
| 1082 | = D->getDependentSpecializationInfo()) { |
| 1083 | assert(isFriend && "non-friend has dependent specialization info?"); |
| 1084 | |
| 1085 | // This needs to be set now for future sanity. |
| 1086 | Function->setObjectOfFriendDecl(/*HasPrevious*/ true); |
| 1087 | |
| 1088 | // Instantiate the explicit template arguments. |
| 1089 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 1090 | Info->getRAngleLoc()); |
| 1091 | for (unsigned I = 0, E = Info->getNumTemplateArgs(); I != E; ++I) { |
| 1092 | TemplateArgumentLoc Loc; |
| 1093 | if (SemaRef.Subst(Info->getTemplateArg(I), Loc, TemplateArgs)) |
| 1094 | return 0; |
| 1095 | |
| 1096 | ExplicitArgs.addArgument(Loc); |
| 1097 | } |
| 1098 | |
| 1099 | // Map the candidate templates to their instantiations. |
| 1100 | for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) { |
| 1101 | Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 1102 | Info->getTemplate(I), |
| 1103 | TemplateArgs); |
| 1104 | if (!Temp) return 0; |
| 1105 | |
| 1106 | Previous.addDecl(cast<FunctionTemplateDecl>(Temp)); |
| 1107 | } |
| 1108 | |
| 1109 | if (SemaRef.CheckFunctionTemplateSpecialization(Function, |
| 1110 | &ExplicitArgs, |
| 1111 | Previous)) |
| 1112 | Function->setInvalidDecl(); |
| 1113 | |
| 1114 | isExplicitSpecialization = true; |
| 1115 | |
| 1116 | } else if (TemplateParams || !FunctionTemplate) { |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1117 | // Look only into the namespace where the friend would be declared to |
| 1118 | // find a previous declaration. This is the innermost enclosing namespace, |
| 1119 | // as described in ActOnFriendFunctionDecl. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1120 | SemaRef.LookupQualifiedName(Previous, DC); |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1121 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1122 | // In C++, the previous declaration we find might be a tag type |
| 1123 | // (class or enum). In this case, the new declaration will hide the |
| 1124 | // tag type. Note that this does does not apply if we're declaring a |
| 1125 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1126 | if (Previous.isSingleTagDecl()) |
| 1127 | Previous.clear(); |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1130 | SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous, |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1131 | isExplicitSpecialization, Redeclaration, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1132 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1133 | |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1134 | NamedDecl *PrincipalDecl = (TemplateParams |
| 1135 | ? cast<NamedDecl>(FunctionTemplate) |
| 1136 | : Function); |
| 1137 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1138 | // If the original function was part of a friend declaration, |
| 1139 | // inherit its namespace state and add it to the owner. |
John McCall | d325daa | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1140 | if (isFriend) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1141 | NamedDecl *PrevDecl; |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1142 | if (TemplateParams) |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1143 | PrevDecl = FunctionTemplate->getPreviousDeclaration(); |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1144 | else |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1145 | PrevDecl = Function->getPreviousDeclaration(); |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1146 | |
| 1147 | PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0); |
| 1148 | DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false); |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1151 | if (Function->isOverloadedOperator() && !DC->isRecord() && |
| 1152 | PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 1153 | PrincipalDecl->setNonMemberOperator(); |
| 1154 | |
Douglas Gregor | ac7c2c8 | 2010-05-17 16:38:00 +0000 | [diff] [blame] | 1155 | // If we need to instantiate this function now (because it is a C++98/03 |
| 1156 | // friend function defined inside a class template), do so. |
| 1157 | if (InstantiateAtPOI.isValid()) |
| 1158 | SemaRef.MarkDeclarationReferenced(InstantiateAtPOI, Function); |
| 1159 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1160 | return Function; |
| 1161 | } |
| 1162 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1163 | Decl * |
| 1164 | TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D, |
| 1165 | TemplateParameterList *TemplateParams) { |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1166 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
| 1167 | void *InsertPos = 0; |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1168 | if (FunctionTemplate && !TemplateParams) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1169 | // We are creating a function template specialization from a function |
| 1170 | // template. Check whether there is already a function template |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1171 | // specialization for this particular set of template arguments. |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1172 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1173 | FunctionTemplateSpecializationInfo::Profile(ID, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1174 | TemplateArgs.getInnermost().getFlatArgumentList(), |
| 1175 | TemplateArgs.getInnermost().flat_size(), |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1176 | SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1177 | |
| 1178 | FunctionTemplateSpecializationInfo *Info |
| 1179 | = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID, |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1180 | InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1181 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1182 | // If we already have a function template specialization, return it. |
| 1183 | if (Info) |
| 1184 | return Info->Function; |
| 1185 | } |
| 1186 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1187 | bool isFriend; |
| 1188 | if (FunctionTemplate) |
| 1189 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1190 | else |
| 1191 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1192 | |
Douglas Gregor | 79c2278 | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1193 | bool MergeWithParentScope = (TemplateParams != 0) || |
| 1194 | !(isa<Decl>(Owner) && |
| 1195 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
| 1196 | Sema::LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 1197 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 1198 | llvm::SmallVector<ParmVarDecl *, 4> Params; |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1199 | TypeSourceInfo *TInfo = D->getTypeSourceInfo(); |
| 1200 | TInfo = SubstFunctionType(D, Params); |
| 1201 | if (!TInfo) |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1202 | return 0; |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1203 | QualType T = TInfo->getType(); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1204 | |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 1205 | // \brief If the type of this function is not *directly* a function |
| 1206 | // type, then we're instantiating the a function that was declared |
| 1207 | // via a typedef, e.g., |
| 1208 | // |
| 1209 | // typedef int functype(int, int); |
| 1210 | // functype func; |
| 1211 | // |
| 1212 | // In this case, we'll just go instantiate the ParmVarDecls that we |
| 1213 | // synthesized in the method declaration. |
| 1214 | if (!isa<FunctionProtoType>(T)) { |
| 1215 | assert(!Params.size() && "Instantiating type could not yield parameters"); |
| 1216 | for (unsigned I = 0, N = D->getNumParams(); I != N; ++I) { |
| 1217 | ParmVarDecl *P = SemaRef.SubstParmVarDecl(D->getParamDecl(I), |
| 1218 | TemplateArgs); |
| 1219 | if (!P) |
| 1220 | return 0; |
| 1221 | |
| 1222 | Params.push_back(P); |
| 1223 | } |
| 1224 | } |
| 1225 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1226 | NestedNameSpecifier *Qualifier = D->getQualifier(); |
| 1227 | if (Qualifier) { |
| 1228 | Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier, |
| 1229 | D->getQualifierRange(), |
| 1230 | TemplateArgs); |
| 1231 | if (!Qualifier) return 0; |
| 1232 | } |
| 1233 | |
| 1234 | DeclContext *DC = Owner; |
| 1235 | if (isFriend) { |
| 1236 | if (Qualifier) { |
| 1237 | CXXScopeSpec SS; |
| 1238 | SS.setScopeRep(Qualifier); |
| 1239 | SS.setRange(D->getQualifierRange()); |
| 1240 | DC = SemaRef.computeDeclContext(SS); |
| 1241 | } else { |
| 1242 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), |
| 1243 | D->getDeclContext(), |
| 1244 | TemplateArgs); |
| 1245 | } |
| 1246 | if (!DC) return 0; |
| 1247 | } |
| 1248 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1249 | // Build the instantiated method declaration. |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1250 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1251 | CXXMethodDecl *Method = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1252 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1253 | DeclarationName Name = D->getDeclName(); |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1254 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1255 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); |
| 1256 | Name = SemaRef.Context.DeclarationNames.getCXXConstructorName( |
| 1257 | SemaRef.Context.getCanonicalType(ClassTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1258 | Method = CXXConstructorDecl::Create(SemaRef.Context, Record, |
| 1259 | Constructor->getLocation(), |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1260 | Name, T, TInfo, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1261 | Constructor->isExplicit(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1262 | Constructor->isInlineSpecified(), |
| 1263 | false); |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1264 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
| 1265 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); |
| 1266 | Name = SemaRef.Context.DeclarationNames.getCXXDestructorName( |
| 1267 | SemaRef.Context.getCanonicalType(ClassTy)); |
| 1268 | Method = CXXDestructorDecl::Create(SemaRef.Context, Record, |
| 1269 | Destructor->getLocation(), Name, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1270 | T, Destructor->isInlineSpecified(), |
| 1271 | false); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1272 | } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1273 | CanQualType ConvTy |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1274 | = SemaRef.Context.getCanonicalType( |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1275 | T->getAs<FunctionType>()->getResultType()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1276 | Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName( |
| 1277 | ConvTy); |
| 1278 | Method = CXXConversionDecl::Create(SemaRef.Context, Record, |
| 1279 | Conversion->getLocation(), Name, |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1280 | T, TInfo, |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 1281 | Conversion->isInlineSpecified(), |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1282 | Conversion->isExplicit()); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1283 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1284 | Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(), |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1285 | D->getDeclName(), T, TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1286 | D->isStatic(), |
| 1287 | D->getStorageClassAsWritten(), |
| 1288 | D->isInlineSpecified()); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1289 | } |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1290 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1291 | if (Qualifier) |
| 1292 | Method->setQualifierInfo(Qualifier, D->getQualifierRange()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1293 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1294 | if (TemplateParams) { |
| 1295 | // Our resulting instantiation is actually a function template, since we |
| 1296 | // are substituting only the outer template parameters. For example, given |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1297 | // |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1298 | // template<typename T> |
| 1299 | // struct X { |
| 1300 | // template<typename U> void f(T, U); |
| 1301 | // }; |
| 1302 | // |
| 1303 | // X<int> x; |
| 1304 | // |
| 1305 | // We are instantiating the member template "f" within X<int>, which means |
| 1306 | // substituting int for T, but leaving "f" as a member function template. |
| 1307 | // Build the function template itself. |
| 1308 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, |
| 1309 | Method->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1310 | Method->getDeclName(), |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1311 | TemplateParams, Method); |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1312 | if (isFriend) { |
| 1313 | FunctionTemplate->setLexicalDeclContext(Owner); |
| 1314 | FunctionTemplate->setObjectOfFriendDecl(true); |
| 1315 | } else if (D->isOutOfLine()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1316 | FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1317 | Method->setDescribedFunctionTemplate(FunctionTemplate); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1318 | } else if (FunctionTemplate) { |
| 1319 | // Record this function template specialization. |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1320 | Method->setFunctionTemplateSpecialization(FunctionTemplate, |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1321 | &TemplateArgs.getInnermost(), |
| 1322 | InsertPos); |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1323 | } else if (!isFriend) { |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1324 | // Record that this is an instantiation of a member function. |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1325 | Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1328 | // If we are instantiating a member function defined |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1329 | // out-of-line, the instantiation will have the same lexical |
| 1330 | // context (which will be a namespace scope) as the template. |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1331 | if (isFriend) { |
| 1332 | Method->setLexicalDeclContext(Owner); |
| 1333 | Method->setObjectOfFriendDecl(true); |
| 1334 | } else if (D->isOutOfLine()) |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1335 | Method->setLexicalDeclContext(D->getLexicalDeclContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1336 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1337 | // Attach the parameters |
| 1338 | for (unsigned P = 0; P < Params.size(); ++P) |
| 1339 | Params[P]->setOwningFunction(Method); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1340 | Method->setParams(Params.data(), Params.size()); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1341 | |
| 1342 | if (InitMethodInstantiation(Method, D)) |
| 1343 | Method->setInvalidDecl(); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1344 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1345 | LookupResult Previous(SemaRef, Name, SourceLocation(), |
| 1346 | Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1347 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1348 | if (!FunctionTemplate || TemplateParams || isFriend) { |
| 1349 | SemaRef.LookupQualifiedName(Previous, Record); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1351 | // In C++, the previous declaration we find might be a tag type |
| 1352 | // (class or enum). In this case, the new declaration will hide the |
| 1353 | // tag type. Note that this does does not apply if we're declaring a |
| 1354 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1355 | if (Previous.isSingleTagDecl()) |
| 1356 | Previous.clear(); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1357 | } |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1358 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1359 | bool Redeclaration = false; |
| 1360 | bool OverloadableAttrRequired = false; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1361 | SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1362 | /*FIXME:*/OverloadableAttrRequired); |
| 1363 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 1364 | if (D->isPure()) |
| 1365 | SemaRef.CheckPureMethod(Method, SourceRange()); |
| 1366 | |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 1367 | Method->setAccess(D->getAccess()); |
| 1368 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1369 | if (FunctionTemplate) { |
| 1370 | // If there's a function template, let our caller handle it. |
| 1371 | } else if (Method->isInvalidDecl() && !Previous.empty()) { |
| 1372 | // Don't hide a (potentially) valid declaration with an invalid one. |
| 1373 | } else { |
| 1374 | NamedDecl *DeclToAdd = (TemplateParams |
| 1375 | ? cast<NamedDecl>(FunctionTemplate) |
| 1376 | : Method); |
| 1377 | if (isFriend) |
| 1378 | Record->makeDeclVisibleInContext(DeclToAdd); |
| 1379 | else |
| 1380 | Owner->addDecl(DeclToAdd); |
| 1381 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1382 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1383 | return Method; |
| 1384 | } |
| 1385 | |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 1386 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1387 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 1390 | Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1391 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 1394 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1395 | return VisitCXXMethodDecl(D); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 1398 | ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1399 | return SemaRef.SubstParmVarDecl(D, TemplateArgs); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1402 | Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( |
| 1403 | TemplateTypeParmDecl *D) { |
| 1404 | // TODO: don't always clone when decls are refcounted. |
| 1405 | const Type* T = D->getTypeForDecl(); |
| 1406 | assert(T->isTemplateTypeParmType()); |
| 1407 | const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1408 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1409 | TemplateTypeParmDecl *Inst = |
| 1410 | TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1411 | TTPT->getDepth() - 1, TTPT->getIndex(), |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1412 | TTPT->getName(), |
| 1413 | D->wasDeclaredWithTypename(), |
| 1414 | D->isParameterPack()); |
| 1415 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1416 | if (D->hasDefaultArgument()) |
| 1417 | Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1418 | |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1419 | // Introduce this template parameter's instantiation into the instantiation |
| 1420 | // scope. |
| 1421 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 1422 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1423 | return Inst; |
| 1424 | } |
| 1425 | |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1426 | Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl( |
| 1427 | NonTypeTemplateParmDecl *D) { |
| 1428 | // Substitute into the type of the non-type template parameter. |
| 1429 | QualType T; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1430 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1431 | if (DI) { |
| 1432 | DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(), |
| 1433 | D->getDeclName()); |
| 1434 | if (DI) T = DI->getType(); |
| 1435 | } else { |
| 1436 | T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(), |
| 1437 | D->getDeclName()); |
| 1438 | DI = 0; |
| 1439 | } |
| 1440 | if (T.isNull()) |
| 1441 | return 0; |
| 1442 | |
| 1443 | // Check that this type is acceptable for a non-type template parameter. |
| 1444 | bool Invalid = false; |
| 1445 | T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation()); |
| 1446 | if (T.isNull()) { |
| 1447 | T = SemaRef.Context.IntTy; |
| 1448 | Invalid = true; |
| 1449 | } |
| 1450 | |
| 1451 | NonTypeTemplateParmDecl *Param |
| 1452 | = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 1453 | D->getDepth() - 1, D->getPosition(), |
| 1454 | D->getIdentifier(), T, DI); |
| 1455 | if (Invalid) |
| 1456 | Param->setInvalidDecl(); |
| 1457 | |
| 1458 | Param->setDefaultArgument(D->getDefaultArgument()); |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1459 | |
| 1460 | // Introduce this template parameter's instantiation into the instantiation |
| 1461 | // scope. |
| 1462 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1463 | return Param; |
| 1464 | } |
| 1465 | |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 1466 | Decl * |
Douglas Gregor | 9106ef7 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 1467 | TemplateDeclInstantiator::VisitTemplateTemplateParmDecl( |
| 1468 | TemplateTemplateParmDecl *D) { |
| 1469 | // Instantiate the template parameter list of the template template parameter. |
| 1470 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1471 | TemplateParameterList *InstParams; |
| 1472 | { |
| 1473 | // Perform the actual substitution of template parameters within a new, |
| 1474 | // local instantiation scope. |
| 1475 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 1476 | InstParams = SubstTemplateParams(TempParams); |
| 1477 | if (!InstParams) |
| 1478 | return NULL; |
| 1479 | } |
| 1480 | |
| 1481 | // Build the template template parameter. |
| 1482 | TemplateTemplateParmDecl *Param |
| 1483 | = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 1484 | D->getDepth() - 1, D->getPosition(), |
| 1485 | D->getIdentifier(), InstParams); |
| 1486 | Param->setDefaultArgument(D->getDefaultArgument()); |
| 1487 | |
| 1488 | // Introduce this template parameter's instantiation into the instantiation |
| 1489 | // scope. |
| 1490 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
| 1491 | |
| 1492 | return Param; |
| 1493 | } |
| 1494 | |
Douglas Gregor | 48c32a7 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 1495 | Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| 1496 | // Using directives are never dependent, so they require no explicit |
| 1497 | |
| 1498 | UsingDirectiveDecl *Inst |
| 1499 | = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 1500 | D->getNamespaceKeyLocation(), |
| 1501 | D->getQualifierRange(), D->getQualifier(), |
| 1502 | D->getIdentLocation(), |
| 1503 | D->getNominatedNamespace(), |
| 1504 | D->getCommonAncestor()); |
| 1505 | Owner->addDecl(Inst); |
| 1506 | return Inst; |
| 1507 | } |
| 1508 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1509 | Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) { |
| 1510 | // The nested name specifier is non-dependent, so no transformation |
| 1511 | // is required. |
| 1512 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1513 | // We only need to do redeclaration lookups if we're in a class |
| 1514 | // scope (in fact, it's not really even possible in non-class |
| 1515 | // scopes). |
| 1516 | bool CheckRedeclaration = Owner->isRecord(); |
| 1517 | |
| 1518 | LookupResult Prev(SemaRef, D->getDeclName(), D->getLocation(), |
| 1519 | Sema::LookupUsingDeclName, Sema::ForRedeclaration); |
| 1520 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1521 | UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner, |
| 1522 | D->getLocation(), |
| 1523 | D->getNestedNameRange(), |
| 1524 | D->getUsingLocation(), |
| 1525 | D->getTargetNestedNameDecl(), |
| 1526 | D->getDeclName(), |
| 1527 | D->isTypeName()); |
| 1528 | |
| 1529 | CXXScopeSpec SS; |
| 1530 | SS.setScopeRep(D->getTargetNestedNameDecl()); |
| 1531 | SS.setRange(D->getNestedNameRange()); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1532 | |
| 1533 | if (CheckRedeclaration) { |
| 1534 | Prev.setHideTags(false); |
| 1535 | SemaRef.LookupQualifiedName(Prev, Owner); |
| 1536 | |
| 1537 | // Check for invalid redeclarations. |
| 1538 | if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(), |
| 1539 | D->isTypeName(), SS, |
| 1540 | D->getLocation(), Prev)) |
| 1541 | NewUD->setInvalidDecl(); |
| 1542 | |
| 1543 | } |
| 1544 | |
| 1545 | if (!NewUD->isInvalidDecl() && |
| 1546 | SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS, |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1547 | D->getLocation())) |
| 1548 | NewUD->setInvalidDecl(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1549 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1550 | SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D); |
| 1551 | NewUD->setAccess(D->getAccess()); |
| 1552 | Owner->addDecl(NewUD); |
| 1553 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1554 | // Don't process the shadow decls for an invalid decl. |
| 1555 | if (NewUD->isInvalidDecl()) |
| 1556 | return NewUD; |
| 1557 | |
John McCall | 323c310 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 1558 | bool isFunctionScope = Owner->isFunctionOrMethod(); |
| 1559 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1560 | // Process the shadow decls. |
| 1561 | for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end(); |
| 1562 | I != E; ++I) { |
| 1563 | UsingShadowDecl *Shadow = *I; |
| 1564 | NamedDecl *InstTarget = |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1565 | cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(), |
| 1566 | Shadow->getTargetDecl(), |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1567 | TemplateArgs)); |
| 1568 | |
| 1569 | if (CheckRedeclaration && |
| 1570 | SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev)) |
| 1571 | continue; |
| 1572 | |
| 1573 | UsingShadowDecl *InstShadow |
| 1574 | = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget); |
| 1575 | SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow); |
John McCall | 323c310 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 1576 | |
| 1577 | if (isFunctionScope) |
| 1578 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1579 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1580 | |
| 1581 | return NewUD; |
| 1582 | } |
| 1583 | |
| 1584 | Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1585 | // Ignore these; we handle them in bulk when processing the UsingDecl. |
| 1586 | return 0; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1589 | Decl * TemplateDeclInstantiator |
| 1590 | ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1591 | NestedNameSpecifier *NNS = |
| 1592 | SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(), |
| 1593 | D->getTargetNestedNameRange(), |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 1594 | TemplateArgs); |
| 1595 | if (!NNS) |
| 1596 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1597 | |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 1598 | CXXScopeSpec SS; |
| 1599 | SS.setRange(D->getTargetNestedNameRange()); |
| 1600 | SS.setScopeRep(NNS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1601 | |
| 1602 | NamedDecl *UD = |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 1603 | SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(), |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1604 | D->getUsingLoc(), SS, D->getLocation(), |
| 1605 | D->getDeclName(), 0, |
| 1606 | /*instantiation*/ true, |
| 1607 | /*typename*/ true, D->getTypenameLoc()); |
| 1608 | if (UD) |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1609 | SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D); |
| 1610 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1611 | return UD; |
| 1612 | } |
| 1613 | |
| 1614 | Decl * TemplateDeclInstantiator |
| 1615 | ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
| 1616 | NestedNameSpecifier *NNS = |
| 1617 | SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(), |
| 1618 | D->getTargetNestedNameRange(), |
| 1619 | TemplateArgs); |
| 1620 | if (!NNS) |
| 1621 | return 0; |
| 1622 | |
| 1623 | CXXScopeSpec SS; |
| 1624 | SS.setRange(D->getTargetNestedNameRange()); |
| 1625 | SS.setScopeRep(NNS); |
| 1626 | |
| 1627 | NamedDecl *UD = |
| 1628 | SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(), |
| 1629 | D->getUsingLoc(), SS, D->getLocation(), |
| 1630 | D->getDeclName(), 0, |
| 1631 | /*instantiation*/ true, |
| 1632 | /*typename*/ false, SourceLocation()); |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1633 | if (UD) |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1634 | SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D); |
| 1635 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1636 | return UD; |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1639 | Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1640 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1641 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
Douglas Gregor | 2fa9800 | 2010-02-16 19:28:15 +0000 | [diff] [blame] | 1642 | if (D->isInvalidDecl()) |
| 1643 | return 0; |
| 1644 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1645 | return Instantiator.Visit(D); |
| 1646 | } |
| 1647 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1648 | /// \brief Instantiates a nested template parameter list in the current |
| 1649 | /// instantiation context. |
| 1650 | /// |
| 1651 | /// \param L The parameter list to instantiate |
| 1652 | /// |
| 1653 | /// \returns NULL if there was an error |
| 1654 | TemplateParameterList * |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1655 | TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1656 | // Get errors for all the parameters before bailing out. |
| 1657 | bool Invalid = false; |
| 1658 | |
| 1659 | unsigned N = L->size(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1660 | typedef llvm::SmallVector<NamedDecl *, 8> ParamVector; |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1661 | ParamVector Params; |
| 1662 | Params.reserve(N); |
| 1663 | for (TemplateParameterList::iterator PI = L->begin(), PE = L->end(); |
| 1664 | PI != PE; ++PI) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1665 | NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI)); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1666 | Params.push_back(D); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 1667 | Invalid = Invalid || !D || D->isInvalidDecl(); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | // Clean up if we had an error. |
| 1671 | if (Invalid) { |
| 1672 | for (ParamVector::iterator PI = Params.begin(), PE = Params.end(); |
| 1673 | PI != PE; ++PI) |
| 1674 | if (*PI) |
| 1675 | (*PI)->Destroy(SemaRef.Context); |
| 1676 | return NULL; |
| 1677 | } |
| 1678 | |
| 1679 | TemplateParameterList *InstL |
| 1680 | = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), |
| 1681 | L->getLAngleLoc(), &Params.front(), N, |
| 1682 | L->getRAngleLoc()); |
| 1683 | return InstL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1684 | } |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1685 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1686 | /// \brief Instantiate the declaration of a class template partial |
| 1687 | /// specialization. |
| 1688 | /// |
| 1689 | /// \param ClassTemplate the (instantiated) class template that is partially |
| 1690 | // specialized by the instantiation of \p PartialSpec. |
| 1691 | /// |
| 1692 | /// \param PartialSpec the (uninstantiated) class template partial |
| 1693 | /// specialization that we are instantiating. |
| 1694 | /// |
| 1695 | /// \returns true if there was an error, false otherwise. |
| 1696 | bool |
| 1697 | TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization( |
| 1698 | ClassTemplateDecl *ClassTemplate, |
| 1699 | ClassTemplatePartialSpecializationDecl *PartialSpec) { |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1700 | // Create a local instantiation scope for this class template partial |
| 1701 | // specialization, which will contain the instantiations of the template |
| 1702 | // parameters. |
| 1703 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 1704 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1705 | // Substitute into the template parameters of the class template partial |
| 1706 | // specialization. |
| 1707 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); |
| 1708 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1709 | if (!InstParams) |
| 1710 | return true; |
| 1711 | |
| 1712 | // Substitute into the template arguments of the class template partial |
| 1713 | // specialization. |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1714 | const TemplateArgumentLoc *PartialSpecTemplateArgs |
| 1715 | = PartialSpec->getTemplateArgsAsWritten(); |
| 1716 | unsigned N = PartialSpec->getNumTemplateArgsAsWritten(); |
| 1717 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1718 | TemplateArgumentListInfo InstTemplateArgs; // no angle locations |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1719 | for (unsigned I = 0; I != N; ++I) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1720 | TemplateArgumentLoc Loc; |
| 1721 | if (SemaRef.Subst(PartialSpecTemplateArgs[I], Loc, TemplateArgs)) |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1722 | return true; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1723 | InstTemplateArgs.addArgument(Loc); |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | |
| 1727 | // Check that the template argument list is well-formed for this |
| 1728 | // class template. |
| 1729 | TemplateArgumentListBuilder Converted(ClassTemplate->getTemplateParameters(), |
| 1730 | InstTemplateArgs.size()); |
| 1731 | if (SemaRef.CheckTemplateArgumentList(ClassTemplate, |
| 1732 | PartialSpec->getLocation(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1733 | InstTemplateArgs, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1734 | false, |
| 1735 | Converted)) |
| 1736 | return true; |
| 1737 | |
| 1738 | // Figure out where to insert this class template partial specialization |
| 1739 | // in the member template's set of class template partial specializations. |
| 1740 | llvm::FoldingSetNodeID ID; |
| 1741 | ClassTemplatePartialSpecializationDecl::Profile(ID, |
| 1742 | Converted.getFlatArguments(), |
| 1743 | Converted.flatSize(), |
| 1744 | SemaRef.Context); |
| 1745 | void *InsertPos = 0; |
| 1746 | ClassTemplateSpecializationDecl *PrevDecl |
| 1747 | = ClassTemplate->getPartialSpecializations().FindNodeOrInsertPos(ID, |
| 1748 | InsertPos); |
| 1749 | |
| 1750 | // Build the canonical type that describes the converted template |
| 1751 | // arguments of the class template partial specialization. |
| 1752 | QualType CanonType |
| 1753 | = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate), |
| 1754 | Converted.getFlatArguments(), |
| 1755 | Converted.flatSize()); |
| 1756 | |
| 1757 | // Build the fully-sugared type for this class template |
| 1758 | // specialization as the user wrote in the specialization |
| 1759 | // itself. This means that we'll pretty-print the type retrieved |
| 1760 | // from the specialization's declaration the way that the user |
| 1761 | // actually wrote the specialization, rather than formatting the |
| 1762 | // name based on the "canonical" representation used to store the |
| 1763 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1764 | TypeSourceInfo *WrittenTy |
| 1765 | = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 1766 | TemplateName(ClassTemplate), |
| 1767 | PartialSpec->getLocation(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1768 | InstTemplateArgs, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1769 | CanonType); |
| 1770 | |
| 1771 | if (PrevDecl) { |
| 1772 | // We've already seen a partial specialization with the same template |
| 1773 | // parameters and template arguments. This can happen, for example, when |
| 1774 | // substituting the outer template arguments ends up causing two |
| 1775 | // class template partial specializations of a member class template |
| 1776 | // to have identical forms, e.g., |
| 1777 | // |
| 1778 | // template<typename T, typename U> |
| 1779 | // struct Outer { |
| 1780 | // template<typename X, typename Y> struct Inner; |
| 1781 | // template<typename Y> struct Inner<T, Y>; |
| 1782 | // template<typename Y> struct Inner<U, Y>; |
| 1783 | // }; |
| 1784 | // |
| 1785 | // Outer<int, int> outer; // error: the partial specializations of Inner |
| 1786 | // // have the same signature. |
| 1787 | SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared) |
| 1788 | << WrittenTy; |
| 1789 | SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here) |
| 1790 | << SemaRef.Context.getTypeDeclType(PrevDecl); |
| 1791 | return true; |
| 1792 | } |
| 1793 | |
| 1794 | |
| 1795 | // Create the class template partial specialization declaration. |
| 1796 | ClassTemplatePartialSpecializationDecl *InstPartialSpec |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 1797 | = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, |
| 1798 | PartialSpec->getTagKind(), |
| 1799 | Owner, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1800 | PartialSpec->getLocation(), |
| 1801 | InstParams, |
| 1802 | ClassTemplate, |
| 1803 | Converted, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1804 | InstTemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1805 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 1806 | 0, |
| 1807 | ClassTemplate->getPartialSpecializations().size()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1808 | // Substitute the nested name specifier, if any. |
| 1809 | if (SubstQualifier(PartialSpec, InstPartialSpec)) |
| 1810 | return 0; |
| 1811 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1812 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); |
| 1813 | InstPartialSpec->setTypeAsWritten(WrittenTy); |
| 1814 | |
| 1815 | // Add this partial specialization to the set of class template partial |
| 1816 | // specializations. |
| 1817 | ClassTemplate->getPartialSpecializations().InsertNode(InstPartialSpec, |
| 1818 | InsertPos); |
| 1819 | return false; |
| 1820 | } |
| 1821 | |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1822 | TypeSourceInfo* |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1823 | TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1824 | llvm::SmallVectorImpl<ParmVarDecl *> &Params) { |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1825 | TypeSourceInfo *OldTInfo = D->getTypeSourceInfo(); |
| 1826 | assert(OldTInfo && "substituting function without type source info"); |
| 1827 | assert(Params.empty() && "parameter vector is non-empty at start"); |
John McCall | 6cd3b9f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1828 | TypeSourceInfo *NewTInfo |
| 1829 | = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs, |
| 1830 | D->getTypeSpecStartLoc(), |
| 1831 | D->getDeclName()); |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1832 | if (!NewTInfo) |
| 1833 | return 0; |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1834 | |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1835 | if (NewTInfo != OldTInfo) { |
| 1836 | // Get parameters from the new type info. |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1837 | TypeLoc OldTL = OldTInfo->getTypeLoc(); |
Douglas Gregor | 6920cdc | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 1838 | if (FunctionProtoTypeLoc *OldProtoLoc |
| 1839 | = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) { |
| 1840 | TypeLoc NewTL = NewTInfo->getTypeLoc(); |
| 1841 | FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL); |
| 1842 | assert(NewProtoLoc && "Missing prototype?"); |
| 1843 | for (unsigned i = 0, i_end = NewProtoLoc->getNumArgs(); i != i_end; ++i) { |
| 1844 | // FIXME: Variadic templates will break this. |
| 1845 | Params.push_back(NewProtoLoc->getArg(i)); |
| 1846 | SemaRef.CurrentInstantiationScope->InstantiatedLocal( |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1847 | OldProtoLoc->getArg(i), |
| 1848 | NewProtoLoc->getArg(i)); |
Douglas Gregor | 6920cdc | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 1849 | } |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1850 | } |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1851 | } else { |
| 1852 | // The function type itself was not dependent and therefore no |
| 1853 | // substitution occurred. However, we still need to instantiate |
| 1854 | // the function parameters themselves. |
| 1855 | TypeLoc OldTL = OldTInfo->getTypeLoc(); |
Douglas Gregor | 6920cdc | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 1856 | if (FunctionProtoTypeLoc *OldProtoLoc |
| 1857 | = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) { |
| 1858 | for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) { |
| 1859 | ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i)); |
| 1860 | if (!Parm) |
| 1861 | return 0; |
| 1862 | Params.push_back(Parm); |
| 1863 | } |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1864 | } |
| 1865 | } |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1866 | return NewTInfo; |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1869 | /// \brief Initializes the common fields of an instantiation function |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1870 | /// declaration (New) from the corresponding fields of its template (Tmpl). |
| 1871 | /// |
| 1872 | /// \returns true if there was an error |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1873 | bool |
| 1874 | TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1875 | FunctionDecl *Tmpl) { |
| 1876 | if (Tmpl->isDeleted()) |
| 1877 | New->setDeleted(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1878 | |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1879 | // If we are performing substituting explicitly-specified template arguments |
| 1880 | // or deduced template arguments into a function template and we reach this |
| 1881 | // 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] | 1882 | // to keeping the new function template specialization. We therefore |
| 1883 | // convert the active template instantiation for the function template |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1884 | // into a template instantiation for this specific function template |
| 1885 | // specialization, which is not a SFINAE context, so that we diagnose any |
| 1886 | // further errors in the declaration itself. |
| 1887 | typedef Sema::ActiveTemplateInstantiation ActiveInstType; |
| 1888 | ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back(); |
| 1889 | if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || |
| 1890 | ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1891 | if (FunctionTemplateDecl *FunTmpl |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1892 | = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1893 | assert(FunTmpl->getTemplatedDecl() == Tmpl && |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1894 | "Deduction from the wrong function template?"); |
Daniel Dunbar | bcbb8bd | 2009-07-16 22:10:11 +0000 | [diff] [blame] | 1895 | (void) FunTmpl; |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1896 | ActiveInst.Kind = ActiveInstType::TemplateInstantiation; |
| 1897 | ActiveInst.Entity = reinterpret_cast<uintptr_t>(New); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 1898 | --SemaRef.NonInstantiationEntries; |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1899 | } |
| 1900 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1901 | |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 1902 | const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>(); |
| 1903 | assert(Proto && "Function template without prototype?"); |
| 1904 | |
| 1905 | if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() || |
| 1906 | Proto->getNoReturnAttr()) { |
| 1907 | // The function has an exception specification or a "noreturn" |
| 1908 | // attribute. Substitute into each of the exception types. |
| 1909 | llvm::SmallVector<QualType, 4> Exceptions; |
| 1910 | for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) { |
| 1911 | // FIXME: Poor location information! |
| 1912 | QualType T |
| 1913 | = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs, |
| 1914 | New->getLocation(), New->getDeclName()); |
| 1915 | if (T.isNull() || |
| 1916 | SemaRef.CheckSpecifiedExceptionType(T, New->getLocation())) |
| 1917 | continue; |
| 1918 | |
| 1919 | Exceptions.push_back(T); |
| 1920 | } |
| 1921 | |
| 1922 | // Rebuild the function type |
| 1923 | |
| 1924 | const FunctionProtoType *NewProto |
| 1925 | = New->getType()->getAs<FunctionProtoType>(); |
| 1926 | assert(NewProto && "Template instantiation without function prototype?"); |
| 1927 | New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(), |
| 1928 | NewProto->arg_type_begin(), |
| 1929 | NewProto->getNumArgs(), |
| 1930 | NewProto->isVariadic(), |
| 1931 | NewProto->getTypeQuals(), |
| 1932 | Proto->hasExceptionSpec(), |
| 1933 | Proto->hasAnyExceptionSpec(), |
| 1934 | Exceptions.size(), |
| 1935 | Exceptions.data(), |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1936 | Proto->getExtInfo())); |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 1937 | } |
| 1938 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1939 | return false; |
| 1940 | } |
| 1941 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1942 | /// \brief Initializes common fields of an instantiated method |
| 1943 | /// declaration (New) from the corresponding fields of its template |
| 1944 | /// (Tmpl). |
| 1945 | /// |
| 1946 | /// \returns true if there was an error |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1947 | bool |
| 1948 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1949 | CXXMethodDecl *Tmpl) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1950 | if (InitFunctionInstantiation(New, Tmpl)) |
| 1951 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1953 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 1954 | New->setAccess(Tmpl->getAccess()); |
Fariborz Jahanian | e7184df | 2009-12-03 18:44:40 +0000 | [diff] [blame] | 1955 | if (Tmpl->isVirtualAsWritten()) |
| 1956 | Record->setMethodAsVirtual(New); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1957 | |
| 1958 | // FIXME: attributes |
| 1959 | // FIXME: New needs a pointer to Tmpl |
| 1960 | return false; |
| 1961 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1962 | |
| 1963 | /// \brief Instantiate the definition of the given function from its |
| 1964 | /// template. |
| 1965 | /// |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1966 | /// \param PointOfInstantiation the point at which the instantiation was |
| 1967 | /// required. Note that this is not precisely a "point of instantiation" |
| 1968 | /// for the function, but it's close. |
| 1969 | /// |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1970 | /// \param Function the already-instantiated declaration of a |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1971 | /// function template specialization or member function of a class template |
| 1972 | /// specialization. |
| 1973 | /// |
| 1974 | /// \param Recursive if true, recursively instantiates any functions that |
| 1975 | /// are required by this instantiation. |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 1976 | /// |
| 1977 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 1978 | /// instantiation where the body of the function is required. Complain if |
| 1979 | /// there is no such body. |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 1980 | void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1981 | FunctionDecl *Function, |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 1982 | bool Recursive, |
| 1983 | bool DefinitionRequired) { |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 1984 | if (Function->isInvalidDecl()) |
| 1985 | return; |
| 1986 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1987 | // Never instantiate an explicit specialization. |
| 1988 | if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
| 1989 | return; |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 1990 | |
| 1991 | const FunctionDecl *Definition = 0; |
| 1992 | if (Function->getBody(Definition)) { |
| 1993 | // We are trying to instantiate a friend function specialization inside |
| 1994 | // a class template, but there is already another (non-template) definition |
| 1995 | // of the same function. |
| 1996 | if (Definition->getTemplateSpecializationKind() == TSK_Undeclared) { |
| 1997 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); |
| 1998 | if (Inst) |
| 1999 | return; |
| 2000 | |
| 2001 | Diag(Function->getLocation(), diag::err_redefinition) |
| 2002 | << Function->getDeclName(); |
| 2003 | Diag(Definition->getLocation(), diag::note_previous_definition); |
Douglas Gregor | cfe833b | 2010-05-17 17:57:54 +0000 | [diff] [blame] | 2004 | Function->setInvalidDecl(); |
| 2005 | } |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 2006 | |
| 2007 | // We have an explicit instantiation (which already occurred) and an |
| 2008 | // implicit instantiation. Return without complaint. |
| 2009 | return; |
| 2010 | } |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2011 | |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2012 | // Find the function body that we'll be substituting. |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2013 | const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern(); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2014 | Stmt *Pattern = 0; |
| 2015 | if (PatternDecl) |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 2016 | Pattern = PatternDecl->getBody(PatternDecl); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2017 | |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2018 | if (!Pattern) { |
| 2019 | if (DefinitionRequired) { |
| 2020 | if (Function->getPrimaryTemplate()) |
| 2021 | Diag(PointOfInstantiation, |
| 2022 | diag::err_explicit_instantiation_undefined_func_template) |
| 2023 | << Function->getPrimaryTemplate(); |
| 2024 | else |
| 2025 | Diag(PointOfInstantiation, |
| 2026 | diag::err_explicit_instantiation_undefined_member) |
| 2027 | << 1 << Function->getDeclName() << Function->getDeclContext(); |
| 2028 | |
| 2029 | if (PatternDecl) |
| 2030 | Diag(PatternDecl->getLocation(), |
| 2031 | diag::note_explicit_instantiation_here); |
Douglas Gregor | cfe833b | 2010-05-17 17:57:54 +0000 | [diff] [blame] | 2032 | Function->setInvalidDecl(); |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2035 | return; |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2036 | } |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2037 | |
Douglas Gregor | cfe833b | 2010-05-17 17:57:54 +0000 | [diff] [blame] | 2038 | // If this is an instantiation of friend function defined within a class |
| 2039 | // template or class template specialization or member class thereof, |
| 2040 | // determine whether there were multiple instantiations of its lexical class. |
| 2041 | if (PatternDecl->getFriendObjectKind() != Decl::FOK_None) { |
| 2042 | for (FunctionDecl::redecl_iterator R = Function->redecls_begin(), |
| 2043 | REnd = Function->redecls_end(); |
| 2044 | R != REnd; ++R) { |
| 2045 | if (*R != Function && |
| 2046 | ((*R)->getFriendObjectKind() != Decl::FOK_None)) { |
| 2047 | if (const FunctionDecl *RPattern |
| 2048 | = (*R)->getTemplateInstantiationPattern()) |
| 2049 | if (RPattern->getBody(RPattern)) { |
| 2050 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); |
| 2051 | if (Inst) |
| 2052 | return; |
| 2053 | |
| 2054 | Diag(Function->getLocation(), diag::err_redefinition) |
| 2055 | << Function->getDeclName(); |
| 2056 | Diag((*R)->getLocation(), diag::note_previous_definition); |
| 2057 | Function->setInvalidDecl(); |
| 2058 | return; |
| 2059 | } |
| 2060 | } |
| 2061 | } |
| 2062 | } |
| 2063 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2064 | // C++0x [temp.explicit]p9: |
| 2065 | // Except for inline functions, other explicit instantiation declarations |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2066 | // have the effect of suppressing the implicit instantiation of the entity |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2067 | // to which they refer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2068 | if (Function->getTemplateSpecializationKind() |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2069 | == TSK_ExplicitInstantiationDeclaration && |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 2070 | !PatternDecl->isInlined()) |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2071 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2072 | |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 2073 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); |
| 2074 | if (Inst) |
Douglas Gregor | e7089b0 | 2010-05-03 23:29:10 +0000 | [diff] [blame] | 2075 | return; |
| 2076 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2077 | // If we're performing recursive template instantiation, create our own |
| 2078 | // queue of pending implicit instantiations that we will instantiate later, |
| 2079 | // while we're still within our own instantiation context. |
| 2080 | std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations; |
| 2081 | if (Recursive) |
| 2082 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2083 | |
Douglas Gregor | 9679caf | 2010-05-12 17:27:19 +0000 | [diff] [blame] | 2084 | EnterExpressionEvaluationContext EvalContext(*this, |
| 2085 | Action::PotentiallyEvaluated); |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 2086 | ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function)); |
| 2087 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 2088 | // Introduce a new scope where local variable instantiations will be |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 2089 | // recorded, unless we're actually a member function within a local |
| 2090 | // class, in which case we need to merge our results with the parent |
| 2091 | // scope (of the enclosing function). |
| 2092 | bool MergeWithParentScope = false; |
| 2093 | if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext())) |
| 2094 | MergeWithParentScope = Rec->isLocalClass(); |
| 2095 | |
| 2096 | LocalInstantiationScope Scope(*this, MergeWithParentScope); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2097 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 2098 | // Introduce the instantiated function parameters into the local |
| 2099 | // instantiation scope. |
| 2100 | for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) |
| 2101 | Scope.InstantiatedLocal(PatternDecl->getParamDecl(I), |
| 2102 | Function->getParamDecl(I)); |
| 2103 | |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 2104 | // Enter the scope of this instantiation. We don't use |
| 2105 | // PushDeclContext because we don't have a scope. |
| 2106 | DeclContext *PreviousContext = CurContext; |
| 2107 | CurContext = Function; |
| 2108 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2109 | MultiLevelTemplateArgumentList TemplateArgs = |
Douglas Gregor | e7089b0 | 2010-05-03 23:29:10 +0000 | [diff] [blame] | 2110 | getTemplateInstantiationArgs(Function, 0, false, PatternDecl); |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2111 | |
| 2112 | // If this is a constructor, instantiate the member initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2113 | if (const CXXConstructorDecl *Ctor = |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2114 | dyn_cast<CXXConstructorDecl>(PatternDecl)) { |
| 2115 | InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor, |
| 2116 | TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 2119 | // Instantiate the function body. |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2120 | OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs); |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 2121 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 2122 | if (Body.isInvalid()) |
| 2123 | Function->setInvalidDecl(); |
| 2124 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2125 | ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body), |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 2126 | /*IsInstantiation=*/true); |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 2127 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 2128 | PerformDependentDiagnostics(PatternDecl, TemplateArgs); |
| 2129 | |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 2130 | CurContext = PreviousContext; |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 2131 | |
| 2132 | DeclGroupRef DG(Function); |
| 2133 | Consumer.HandleTopLevelDecl(DG); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2134 | |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 2135 | // This class may have local implicit instantiations that need to be |
| 2136 | // instantiation within this scope. |
| 2137 | PerformPendingImplicitInstantiations(/*LocalOnly=*/true); |
| 2138 | Scope.Exit(); |
| 2139 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2140 | if (Recursive) { |
| 2141 | // Instantiate any pending implicit instantiations found during the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2142 | // instantiation of this template. |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2143 | PerformPendingImplicitInstantiations(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2144 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2145 | // Restore the set of pending implicit instantiations. |
| 2146 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); |
| 2147 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | /// \brief Instantiate the definition of the given variable from its |
| 2151 | /// template. |
| 2152 | /// |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2153 | /// \param PointOfInstantiation the point at which the instantiation was |
| 2154 | /// required. Note that this is not precisely a "point of instantiation" |
| 2155 | /// for the function, but it's close. |
| 2156 | /// |
| 2157 | /// \param Var the already-instantiated declaration of a static member |
| 2158 | /// variable of a class template specialization. |
| 2159 | /// |
| 2160 | /// \param Recursive if true, recursively instantiates any functions that |
| 2161 | /// are required by this instantiation. |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2162 | /// |
| 2163 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 2164 | /// instantiation where an out-of-line definition of the member variable |
| 2165 | /// is required. Complain if there is no such definition. |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2166 | void Sema::InstantiateStaticDataMemberDefinition( |
| 2167 | SourceLocation PointOfInstantiation, |
| 2168 | VarDecl *Var, |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2169 | bool Recursive, |
| 2170 | bool DefinitionRequired) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2171 | if (Var->isInvalidDecl()) |
| 2172 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2173 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2174 | // Find the out-of-line definition of this static data member. |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2175 | VarDecl *Def = Var->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2176 | assert(Def && "This data member was not instantiated from a template?"); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2177 | assert(Def->isStaticDataMember() && "Not a static data member?"); |
| 2178 | Def = Def->getOutOfLineDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2179 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2180 | if (!Def) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2181 | // We did not find an out-of-line definition of this static data member, |
| 2182 | // 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] | 2183 | // instantiate this definition (or provide a specialization for it) in |
| 2184 | // another translation unit. |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2185 | if (DefinitionRequired) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2186 | Def = Var->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2187 | Diag(PointOfInstantiation, |
| 2188 | diag::err_explicit_instantiation_undefined_member) |
| 2189 | << 2 << Var->getDeclName() << Var->getDeclContext(); |
| 2190 | Diag(Def->getLocation(), diag::note_explicit_instantiation_here); |
| 2191 | } |
| 2192 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2193 | return; |
| 2194 | } |
| 2195 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2196 | // Never instantiate an explicit specialization. |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2197 | if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2198 | return; |
| 2199 | |
| 2200 | // C++0x [temp.explicit]p9: |
| 2201 | // Except for inline functions, other explicit instantiation declarations |
| 2202 | // have the effect of suppressing the implicit instantiation of the entity |
| 2203 | // to which they refer. |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2204 | if (Var->getTemplateSpecializationKind() |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2205 | == TSK_ExplicitInstantiationDeclaration) |
| 2206 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2207 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2208 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
| 2209 | if (Inst) |
| 2210 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2211 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2212 | // If we're performing recursive template instantiation, create our own |
| 2213 | // queue of pending implicit instantiations that we will instantiate later, |
| 2214 | // while we're still within our own instantiation context. |
| 2215 | std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations; |
| 2216 | if (Recursive) |
| 2217 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2218 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2219 | // Enter the scope of this instantiation. We don't use |
| 2220 | // PushDeclContext because we don't have a scope. |
| 2221 | DeclContext *PreviousContext = CurContext; |
| 2222 | CurContext = Var->getDeclContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2223 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2224 | VarDecl *OldVar = Var; |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2225 | Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2226 | getTemplateInstantiationArgs(Var))); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2227 | CurContext = PreviousContext; |
| 2228 | |
| 2229 | if (Var) { |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 2230 | MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo(); |
| 2231 | assert(MSInfo && "Missing member specialization information?"); |
| 2232 | Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(), |
| 2233 | MSInfo->getPointOfInstantiation()); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2234 | DeclGroupRef DG(Var); |
| 2235 | Consumer.HandleTopLevelDecl(DG); |
| 2236 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2237 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2238 | if (Recursive) { |
| 2239 | // Instantiate any pending implicit instantiations found during the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2240 | // instantiation of this template. |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2241 | PerformPendingImplicitInstantiations(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2242 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2243 | // Restore the set of pending implicit instantiations. |
| 2244 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2245 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2246 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2247 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2248 | void |
| 2249 | Sema::InstantiateMemInitializers(CXXConstructorDecl *New, |
| 2250 | const CXXConstructorDecl *Tmpl, |
| 2251 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2252 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2253 | llvm::SmallVector<MemInitTy*, 4> NewInits; |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2254 | bool AnyErrors = false; |
| 2255 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2256 | // Instantiate all the initializers. |
| 2257 | for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(), |
Douglas Gregor | 72f6d67 | 2009-09-01 21:04:42 +0000 | [diff] [blame] | 2258 | InitsEnd = Tmpl->init_end(); |
| 2259 | Inits != InitsEnd; ++Inits) { |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2260 | CXXBaseOrMemberInitializer *Init = *Inits; |
| 2261 | |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 2262 | SourceLocation LParenLoc, RParenLoc; |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2263 | ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2264 | llvm::SmallVector<SourceLocation, 4> CommaLocs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2265 | |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 2266 | // Instantiate the initializer. |
| 2267 | if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs, |
| 2268 | LParenLoc, CommaLocs, NewArgs, RParenLoc)) { |
| 2269 | AnyErrors = true; |
| 2270 | continue; |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2271 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2272 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2273 | MemInitResult NewInit; |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2274 | if (Init->isBaseInitializer()) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2275 | TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2276 | TemplateArgs, |
| 2277 | Init->getSourceLocation(), |
| 2278 | New->getDeclName()); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2279 | if (!BaseTInfo) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2280 | AnyErrors = true; |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2281 | New->setInvalidDecl(); |
| 2282 | continue; |
| 2283 | } |
| 2284 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2285 | NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2286 | (Expr **)NewArgs.data(), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2287 | NewArgs.size(), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2288 | Init->getLParenLoc(), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2289 | Init->getRParenLoc(), |
| 2290 | New->getParent()); |
| 2291 | } else if (Init->isMemberInitializer()) { |
Anders Carlsson | 9988d5d | 2009-09-01 04:31:02 +0000 | [diff] [blame] | 2292 | FieldDecl *Member; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2293 | |
Anders Carlsson | 9988d5d | 2009-09-01 04:31:02 +0000 | [diff] [blame] | 2294 | // Is this an anonymous union? |
| 2295 | if (FieldDecl *UnionInit = Init->getAnonUnionMember()) |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 2296 | Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(), |
| 2297 | UnionInit, TemplateArgs)); |
Anders Carlsson | 9988d5d | 2009-09-01 04:31:02 +0000 | [diff] [blame] | 2298 | else |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 2299 | Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMemberLocation(), |
| 2300 | Init->getMember(), |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2301 | TemplateArgs)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2302 | |
| 2303 | NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2304 | NewArgs.size(), |
| 2305 | Init->getSourceLocation(), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2306 | Init->getLParenLoc(), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2307 | Init->getRParenLoc()); |
| 2308 | } |
| 2309 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2310 | if (NewInit.isInvalid()) { |
| 2311 | AnyErrors = true; |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2312 | New->setInvalidDecl(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2313 | } else { |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2314 | // FIXME: It would be nice if ASTOwningVector had a release function. |
| 2315 | NewArgs.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2316 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2317 | NewInits.push_back((MemInitTy *)NewInit.get()); |
| 2318 | } |
| 2319 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2320 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2321 | // Assign all the initializers to the new constructor. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2322 | ActOnMemInitializers(DeclPtrTy::make(New), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2323 | /*FIXME: ColonLoc */ |
| 2324 | SourceLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2325 | NewInits.data(), NewInits.size(), |
| 2326 | AnyErrors); |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2329 | // TODO: this could be templated if the various decl types used the |
| 2330 | // same method name. |
| 2331 | static bool isInstantiationOf(ClassTemplateDecl *Pattern, |
| 2332 | ClassTemplateDecl *Instance) { |
| 2333 | Pattern = Pattern->getCanonicalDecl(); |
| 2334 | |
| 2335 | do { |
| 2336 | Instance = Instance->getCanonicalDecl(); |
| 2337 | if (Pattern == Instance) return true; |
| 2338 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 2339 | } while (Instance); |
| 2340 | |
| 2341 | return false; |
| 2342 | } |
| 2343 | |
Douglas Gregor | 0d69653 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 2344 | static bool isInstantiationOf(FunctionTemplateDecl *Pattern, |
| 2345 | FunctionTemplateDecl *Instance) { |
| 2346 | Pattern = Pattern->getCanonicalDecl(); |
| 2347 | |
| 2348 | do { |
| 2349 | Instance = Instance->getCanonicalDecl(); |
| 2350 | if (Pattern == Instance) return true; |
| 2351 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 2352 | } while (Instance); |
| 2353 | |
| 2354 | return false; |
| 2355 | } |
| 2356 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2357 | static bool |
| 2358 | isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern, |
| 2359 | ClassTemplatePartialSpecializationDecl *Instance) { |
| 2360 | Pattern |
| 2361 | = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl()); |
| 2362 | do { |
| 2363 | Instance = cast<ClassTemplatePartialSpecializationDecl>( |
| 2364 | Instance->getCanonicalDecl()); |
| 2365 | if (Pattern == Instance) |
| 2366 | return true; |
| 2367 | Instance = Instance->getInstantiatedFromMember(); |
| 2368 | } while (Instance); |
| 2369 | |
| 2370 | return false; |
| 2371 | } |
| 2372 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2373 | static bool isInstantiationOf(CXXRecordDecl *Pattern, |
| 2374 | CXXRecordDecl *Instance) { |
| 2375 | Pattern = Pattern->getCanonicalDecl(); |
| 2376 | |
| 2377 | do { |
| 2378 | Instance = Instance->getCanonicalDecl(); |
| 2379 | if (Pattern == Instance) return true; |
| 2380 | Instance = Instance->getInstantiatedFromMemberClass(); |
| 2381 | } while (Instance); |
| 2382 | |
| 2383 | return false; |
| 2384 | } |
| 2385 | |
| 2386 | static bool isInstantiationOf(FunctionDecl *Pattern, |
| 2387 | FunctionDecl *Instance) { |
| 2388 | Pattern = Pattern->getCanonicalDecl(); |
| 2389 | |
| 2390 | do { |
| 2391 | Instance = Instance->getCanonicalDecl(); |
| 2392 | if (Pattern == Instance) return true; |
| 2393 | Instance = Instance->getInstantiatedFromMemberFunction(); |
| 2394 | } while (Instance); |
| 2395 | |
| 2396 | return false; |
| 2397 | } |
| 2398 | |
| 2399 | static bool isInstantiationOf(EnumDecl *Pattern, |
| 2400 | EnumDecl *Instance) { |
| 2401 | Pattern = Pattern->getCanonicalDecl(); |
| 2402 | |
| 2403 | do { |
| 2404 | Instance = Instance->getCanonicalDecl(); |
| 2405 | if (Pattern == Instance) return true; |
| 2406 | Instance = Instance->getInstantiatedFromMemberEnum(); |
| 2407 | } while (Instance); |
| 2408 | |
| 2409 | return false; |
| 2410 | } |
| 2411 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2412 | static bool isInstantiationOf(UsingShadowDecl *Pattern, |
| 2413 | UsingShadowDecl *Instance, |
| 2414 | ASTContext &C) { |
| 2415 | return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern; |
| 2416 | } |
| 2417 | |
| 2418 | static bool isInstantiationOf(UsingDecl *Pattern, |
| 2419 | UsingDecl *Instance, |
| 2420 | ASTContext &C) { |
| 2421 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; |
| 2422 | } |
| 2423 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2424 | static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern, |
| 2425 | UsingDecl *Instance, |
| 2426 | ASTContext &C) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2427 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
| 2430 | static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern, |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 2431 | UsingDecl *Instance, |
| 2432 | ASTContext &C) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2433 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2436 | static bool isInstantiationOfStaticDataMember(VarDecl *Pattern, |
| 2437 | VarDecl *Instance) { |
| 2438 | assert(Instance->isStaticDataMember()); |
| 2439 | |
| 2440 | Pattern = Pattern->getCanonicalDecl(); |
| 2441 | |
| 2442 | do { |
| 2443 | Instance = Instance->getCanonicalDecl(); |
| 2444 | if (Pattern == Instance) return true; |
| 2445 | Instance = Instance->getInstantiatedFromStaticDataMember(); |
| 2446 | } while (Instance); |
| 2447 | |
| 2448 | return false; |
| 2449 | } |
| 2450 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2451 | // Other is the prospective instantiation |
| 2452 | // D is the prospective pattern |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2453 | static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 2454 | if (D->getKind() != Other->getKind()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2455 | if (UnresolvedUsingTypenameDecl *UUD |
| 2456 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 2457 | if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { |
| 2458 | return isInstantiationOf(UUD, UD, Ctx); |
| 2459 | } |
| 2460 | } |
| 2461 | |
| 2462 | if (UnresolvedUsingValueDecl *UUD |
| 2463 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 2464 | if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { |
| 2465 | return isInstantiationOf(UUD, UD, Ctx); |
| 2466 | } |
| 2467 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2468 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 2469 | return false; |
| 2470 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2471 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2472 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other)) |
| 2473 | return isInstantiationOf(cast<CXXRecordDecl>(D), Record); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2474 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2475 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other)) |
| 2476 | return isInstantiationOf(cast<FunctionDecl>(D), Function); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2477 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2478 | if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other)) |
| 2479 | return isInstantiationOf(cast<EnumDecl>(D), Enum); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2480 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2481 | if (VarDecl *Var = dyn_cast<VarDecl>(Other)) |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2482 | if (Var->isStaticDataMember()) |
| 2483 | return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var); |
| 2484 | |
| 2485 | if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other)) |
| 2486 | return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp); |
Douglas Gregor | a5bf7f1 | 2009-08-28 22:03:51 +0000 | [diff] [blame] | 2487 | |
Douglas Gregor | 0d69653 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 2488 | if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other)) |
| 2489 | return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp); |
| 2490 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2491 | if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 2492 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other)) |
| 2493 | return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D), |
| 2494 | PartialSpec); |
| 2495 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 2496 | if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) { |
| 2497 | if (!Field->getDeclName()) { |
| 2498 | // This is an unnamed field. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2499 | return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) == |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 2500 | cast<FieldDecl>(D); |
| 2501 | } |
| 2502 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2503 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2504 | if (UsingDecl *Using = dyn_cast<UsingDecl>(Other)) |
| 2505 | return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx); |
| 2506 | |
| 2507 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other)) |
| 2508 | return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx); |
| 2509 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2510 | return D->getDeclName() && isa<NamedDecl>(Other) && |
| 2511 | D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); |
| 2512 | } |
| 2513 | |
| 2514 | template<typename ForwardIterator> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2515 | static NamedDecl *findInstantiationOf(ASTContext &Ctx, |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2516 | NamedDecl *D, |
| 2517 | ForwardIterator first, |
| 2518 | ForwardIterator last) { |
| 2519 | for (; first != last; ++first) |
| 2520 | if (isInstantiationOf(Ctx, D, *first)) |
| 2521 | return cast<NamedDecl>(*first); |
| 2522 | |
| 2523 | return 0; |
| 2524 | } |
| 2525 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 2526 | /// \brief Finds the instantiation of the given declaration context |
| 2527 | /// within the current instantiation. |
| 2528 | /// |
| 2529 | /// \returns NULL if there was an error |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 2530 | DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC, |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2531 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 2532 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) { |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 2533 | Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 2534 | return cast_or_null<DeclContext>(ID); |
| 2535 | } else return DC; |
| 2536 | } |
| 2537 | |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 2538 | /// \brief Find the instantiation of the given declaration within the |
| 2539 | /// current instantiation. |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2540 | /// |
| 2541 | /// This routine is intended to be used when \p D is a declaration |
| 2542 | /// referenced from within a template, that needs to mapped into the |
| 2543 | /// corresponding declaration within an instantiation. For example, |
| 2544 | /// given: |
| 2545 | /// |
| 2546 | /// \code |
| 2547 | /// template<typename T> |
| 2548 | /// struct X { |
| 2549 | /// enum Kind { |
| 2550 | /// KnownValue = sizeof(T) |
| 2551 | /// }; |
| 2552 | /// |
| 2553 | /// bool getKind() const { return KnownValue; } |
| 2554 | /// }; |
| 2555 | /// |
| 2556 | /// template struct X<int>; |
| 2557 | /// \endcode |
| 2558 | /// |
| 2559 | /// In the instantiation of X<int>::getKind(), we need to map the |
| 2560 | /// EnumConstantDecl for KnownValue (which refers to |
| 2561 | /// X<T>::<Kind>::KnownValue) to its instantiation |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 2562 | /// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs |
| 2563 | /// this mapping from within the instantiation of X<int>. |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 2564 | NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D, |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2565 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2566 | DeclContext *ParentDC = D->getDeclContext(); |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 2567 | if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) || |
Douglas Gregor | 6d3e627 | 2010-02-05 19:54:12 +0000 | [diff] [blame] | 2568 | isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) || |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 2569 | ParentDC->isFunctionOrMethod()) { |
Douglas Gregor | 2bba76b | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 2570 | // D is a local of some kind. Look into the map of local |
| 2571 | // declarations to their instantiations. |
| 2572 | return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D)); |
| 2573 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2574 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2575 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 2576 | if (!Record->isDependentContext()) |
| 2577 | return D; |
| 2578 | |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 2579 | // If the RecordDecl is actually the injected-class-name or a |
| 2580 | // "templated" declaration for a class template, class template |
| 2581 | // partial specialization, or a member class of a class template, |
| 2582 | // substitute into the injected-class-name of the class template |
| 2583 | // or partial specialization to find the new DeclContext. |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2584 | QualType T; |
| 2585 | ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate(); |
| 2586 | |
| 2587 | if (ClassTemplate) { |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2588 | T = ClassTemplate->getInjectedClassNameSpecialization(Context); |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2589 | } else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 2590 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2591 | ClassTemplate = PartialSpec->getSpecializedTemplate(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2592 | |
| 2593 | // If we call SubstType with an InjectedClassNameType here we |
| 2594 | // can end up in an infinite loop. |
| 2595 | T = Context.getTypeDeclType(Record); |
| 2596 | assert(isa<InjectedClassNameType>(T) && |
| 2597 | "type of partial specialization is not an InjectedClassNameType"); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2598 | T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2599 | } |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2600 | |
| 2601 | if (!T.isNull()) { |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 2602 | // Substitute into the injected-class-name to get the type |
| 2603 | // corresponding to the instantiation we want, which may also be |
| 2604 | // the current instantiation (if we're in a template |
| 2605 | // definition). This substitution should never fail, since we |
| 2606 | // know we can instantiate the injected-class-name or we |
| 2607 | // wouldn't have gotten to the injected-class-name! |
| 2608 | |
| 2609 | // FIXME: Can we use the CurrentInstantiationScope to avoid this |
| 2610 | // extra instantiation in the common case? |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2611 | T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName()); |
| 2612 | assert(!T.isNull() && "Instantiation of injected-class-name cannot fail."); |
| 2613 | |
| 2614 | if (!T->isDependentType()) { |
| 2615 | assert(T->isRecordType() && "Instantiation must produce a record type"); |
| 2616 | return T->getAs<RecordType>()->getDecl(); |
| 2617 | } |
| 2618 | |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 2619 | // We are performing "partial" template instantiation to create |
| 2620 | // the member declarations for the members of a class template |
| 2621 | // specialization. Therefore, D is actually referring to something |
| 2622 | // in the current instantiation. Look through the current |
| 2623 | // context, which contains actual instantiations, to find the |
| 2624 | // instantiation of the "current instantiation" that D refers |
| 2625 | // to. |
| 2626 | bool SawNonDependentContext = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2627 | for (DeclContext *DC = CurContext; !DC->isFileContext(); |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2628 | DC = DC->getParent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2629 | if (ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 2630 | = dyn_cast<ClassTemplateSpecializationDecl>(DC)) |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2631 | if (isInstantiationOf(ClassTemplate, |
| 2632 | Spec->getSpecializedTemplate())) |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2633 | return Spec; |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 2634 | |
| 2635 | if (!DC->isDependentContext()) |
| 2636 | SawNonDependentContext = true; |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 2639 | // We're performing "instantiation" of a member of the current |
| 2640 | // instantiation while we are type-checking the |
| 2641 | // definition. Compute the declaration context and return that. |
| 2642 | assert(!SawNonDependentContext && |
| 2643 | "No dependent context while instantiating record"); |
| 2644 | DeclContext *DC = computeDeclContext(T); |
| 2645 | assert(DC && |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2646 | "Unable to find declaration for the current instantiation"); |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 2647 | return cast<CXXRecordDecl>(DC); |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2648 | } |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 2649 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2650 | // Fall through to deal with other dependent record types (e.g., |
| 2651 | // anonymous unions in class templates). |
| 2652 | } |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2653 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2654 | if (!ParentDC->isDependentContext()) |
| 2655 | return D; |
| 2656 | |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 2657 | ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2658 | if (!ParentDC) |
Douglas Gregor | 44c7384 | 2009-09-01 17:53:10 +0000 | [diff] [blame] | 2659 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2660 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2661 | if (ParentDC != D->getDeclContext()) { |
| 2662 | // We performed some kind of instantiation in the parent context, |
| 2663 | // so now we need to look into the instantiated parent context to |
| 2664 | // find the instantiation of the declaration D. |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 2665 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2666 | // If our context used to be dependent, we may need to instantiate |
| 2667 | // it before performing lookup into that context. |
| 2668 | if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) { |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 2669 | if (!Spec->isDependentContext()) { |
| 2670 | QualType T = Context.getTypeDeclType(Spec); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2671 | const RecordType *Tag = T->getAs<RecordType>(); |
| 2672 | assert(Tag && "type of non-dependent record is not a RecordType"); |
| 2673 | if (!Tag->isBeingDefined() && |
| 2674 | RequireCompleteType(Loc, T, diag::err_incomplete_type)) |
| 2675 | return 0; |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 2676 | } |
| 2677 | } |
| 2678 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2679 | NamedDecl *Result = 0; |
| 2680 | if (D->getDeclName()) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2681 | DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName()); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2682 | Result = findInstantiationOf(Context, D, Found.first, Found.second); |
| 2683 | } else { |
| 2684 | // Since we don't have a name for the entity we're looking for, |
| 2685 | // our only option is to walk through all of the declarations to |
| 2686 | // find that name. This will occur in a few cases: |
| 2687 | // |
| 2688 | // - anonymous struct/union within a template |
| 2689 | // - unnamed class/struct/union/enum within a template |
| 2690 | // |
| 2691 | // FIXME: Find a better way to find these instantiations! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2692 | Result = findInstantiationOf(Context, D, |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2693 | ParentDC->decls_begin(), |
| 2694 | ParentDC->decls_end()); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2695 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2696 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2697 | // UsingShadowDecls can instantiate to nothing because of using hiding. |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 2698 | assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() || |
| 2699 | cast<Decl>(ParentDC)->isInvalidDecl()) |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2700 | && "Unable to find instantiation of declaration!"); |
| 2701 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2702 | D = Result; |
| 2703 | } |
| 2704 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2705 | return D; |
| 2706 | } |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2707 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2708 | /// \brief Performs template instantiation for all implicit template |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2709 | /// instantiations we have seen until this point. |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 2710 | void Sema::PerformPendingImplicitInstantiations(bool LocalOnly) { |
| 2711 | while (!PendingLocalImplicitInstantiations.empty() || |
| 2712 | (!LocalOnly && !PendingImplicitInstantiations.empty())) { |
| 2713 | PendingImplicitInstantiation Inst; |
| 2714 | |
| 2715 | if (PendingLocalImplicitInstantiations.empty()) { |
| 2716 | Inst = PendingImplicitInstantiations.front(); |
| 2717 | PendingImplicitInstantiations.pop_front(); |
| 2718 | } else { |
| 2719 | Inst = PendingLocalImplicitInstantiations.front(); |
| 2720 | PendingLocalImplicitInstantiations.pop_front(); |
| 2721 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2722 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2723 | // Instantiate function definitions |
| 2724 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2725 | PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function), |
Anders Carlsson | c17fb7b | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 2726 | Function->getLocation(), *this, |
| 2727 | Context.getSourceManager(), |
| 2728 | "instantiating function definition"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2729 | |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 2730 | InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2731 | continue; |
| 2732 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2733 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2734 | // Instantiate static data member definitions. |
| 2735 | VarDecl *Var = cast<VarDecl>(Inst.first); |
| 2736 | assert(Var->isStaticDataMember() && "Not a static data member?"); |
Anders Carlsson | c17fb7b | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 2737 | |
Chandler Carruth | 291b441 | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 2738 | // Don't try to instantiate declarations if the most recent redeclaration |
| 2739 | // is invalid. |
| 2740 | if (Var->getMostRecentDeclaration()->isInvalidDecl()) |
| 2741 | continue; |
| 2742 | |
| 2743 | // Check if the most recent declaration has changed the specialization kind |
| 2744 | // and removed the need for implicit instantiation. |
| 2745 | switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) { |
| 2746 | case TSK_Undeclared: |
| 2747 | assert(false && "Cannot instantitiate an undeclared specialization."); |
| 2748 | case TSK_ExplicitInstantiationDeclaration: |
| 2749 | case TSK_ExplicitInstantiationDefinition: |
| 2750 | case TSK_ExplicitSpecialization: |
| 2751 | continue; // No longer need implicit instantiation. |
| 2752 | case TSK_ImplicitInstantiation: |
| 2753 | break; |
| 2754 | } |
| 2755 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2756 | PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var), |
Anders Carlsson | c17fb7b | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 2757 | Var->getLocation(), *this, |
| 2758 | Context.getSourceManager(), |
| 2759 | "instantiating static data member " |
| 2760 | "definition"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2761 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2762 | InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2763 | } |
| 2764 | } |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 2765 | |
| 2766 | void Sema::PerformDependentDiagnostics(const DeclContext *Pattern, |
| 2767 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 2768 | for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(), |
| 2769 | E = Pattern->ddiag_end(); I != E; ++I) { |
| 2770 | DependentDiagnostic *DD = *I; |
| 2771 | |
| 2772 | switch (DD->getKind()) { |
| 2773 | case DependentDiagnostic::Access: |
| 2774 | HandleDependentAccessCheck(*DD, TemplateArgs); |
| 2775 | break; |
| 2776 | } |
| 2777 | } |
| 2778 | } |