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