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