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