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