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