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