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