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