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