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