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