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