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