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