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