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