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 | //===----------------------------------------------------------------------===/ |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 12 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 13 | #include "clang/Sema/Lookup.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 14 | #include "clang/Sema/PrettyDeclStackTrace.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Template.h" |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
| 18 | #include "clang/AST/DeclTemplate.h" |
| 19 | #include "clang/AST/DeclVisitor.h" |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 20 | #include "clang/AST/DependentDiagnostic.h" |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Douglas Gregor | a88cfbf | 2009-12-12 18:16:41 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 23 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 24 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace clang; |
| 27 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 28 | bool TemplateDeclInstantiator::SubstQualifier(const DeclaratorDecl *OldDecl, |
| 29 | DeclaratorDecl *NewDecl) { |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 30 | if (!OldDecl->getQualifierLoc()) |
| 31 | return false; |
| 32 | |
| 33 | NestedNameSpecifierLoc NewQualifierLoc |
| 34 | = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(), |
| 35 | TemplateArgs); |
| 36 | |
| 37 | if (!NewQualifierLoc) |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 38 | return true; |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 39 | |
| 40 | NewDecl->setQualifierInfo(NewQualifierLoc); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 41 | return false; |
| 42 | } |
| 43 | |
| 44 | bool TemplateDeclInstantiator::SubstQualifier(const TagDecl *OldDecl, |
| 45 | TagDecl *NewDecl) { |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 46 | if (!OldDecl->getQualifierLoc()) |
| 47 | return false; |
| 48 | |
| 49 | NestedNameSpecifierLoc NewQualifierLoc |
| 50 | = SemaRef.SubstNestedNameSpecifierLoc(OldDecl->getQualifierLoc(), |
| 51 | TemplateArgs); |
| 52 | |
| 53 | if (!NewQualifierLoc) |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 54 | return true; |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 55 | |
| 56 | NewDecl->setQualifierInfo(NewQualifierLoc); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 60 | // FIXME: Is this still too simple? |
John McCall | 1d8d1cc | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 61 | void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs, |
| 62 | Decl *Tmpl, Decl *New) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 63 | for (AttrVec::const_iterator i = Tmpl->attr_begin(), e = Tmpl->attr_end(); |
| 64 | i != e; ++i) { |
| 65 | const Attr *TmplAttr = *i; |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 66 | // FIXME: This should be generalized to more than just the AlignedAttr. |
| 67 | if (const AlignedAttr *Aligned = dyn_cast<AlignedAttr>(TmplAttr)) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 68 | if (Aligned->isAlignmentDependent()) { |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 69 | // The alignment expression is not potentially evaluated. |
John McCall | 1d8d1cc | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 70 | EnterExpressionEvaluationContext Unevaluated(*this, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 71 | Sema::Unevaluated); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 72 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 73 | if (Aligned->isAlignmentExpr()) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 74 | ExprResult Result = SubstExpr(Aligned->getAlignmentExpr(), |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 75 | TemplateArgs); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 76 | if (!Result.isInvalid()) |
| 77 | AddAlignedAttr(Aligned->getLocation(), New, Result.takeAs<Expr>()); |
| 78 | } |
| 79 | else { |
| 80 | TypeSourceInfo *Result = SubstType(Aligned->getAlignmentType(), |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 81 | TemplateArgs, |
| 82 | Aligned->getLocation(), |
| 83 | DeclarationName()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 84 | if (Result) |
| 85 | AddAlignedAttr(Aligned->getLocation(), New, Result); |
| 86 | } |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 87 | continue; |
| 88 | } |
| 89 | } |
| 90 | |
Anders Carlsson | d8fe2d5 | 2009-11-07 06:07:58 +0000 | [diff] [blame] | 91 | // FIXME: Is cloning correct for all attributes? |
John McCall | 1d8d1cc | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 92 | Attr *NewAttr = TmplAttr->clone(Context); |
Anders Carlsson | d8fe2d5 | 2009-11-07 06:07:58 +0000 | [diff] [blame] | 93 | New->addAttr(NewAttr); |
| 94 | } |
| 95 | } |
| 96 | |
Douglas Gregor | 4f722be | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 97 | Decl * |
| 98 | TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 99 | assert(false && "Translation units cannot be instantiated"); |
| 100 | return D; |
| 101 | } |
| 102 | |
| 103 | Decl * |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 104 | TemplateDeclInstantiator::VisitLabelDecl(LabelDecl *D) { |
| 105 | LabelDecl *Inst = LabelDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 106 | D->getIdentifier()); |
| 107 | Owner->addDecl(Inst); |
| 108 | return Inst; |
| 109 | } |
| 110 | |
| 111 | Decl * |
Douglas Gregor | 4f722be | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 112 | TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { |
| 113 | assert(false && "Namespaces cannot be instantiated"); |
| 114 | return D; |
| 115 | } |
| 116 | |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 117 | Decl * |
| 118 | TemplateDeclInstantiator::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { |
| 119 | NamespaceAliasDecl *Inst |
| 120 | = NamespaceAliasDecl::Create(SemaRef.Context, Owner, |
| 121 | D->getNamespaceLoc(), |
| 122 | D->getAliasLoc(), |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 123 | D->getIdentifier(), |
| 124 | D->getQualifierLoc(), |
John McCall | 3dbd3d5 | 2010-02-16 06:53:13 +0000 | [diff] [blame] | 125 | D->getTargetNameLoc(), |
| 126 | D->getNamespace()); |
| 127 | Owner->addDecl(Inst); |
| 128 | return Inst; |
| 129 | } |
| 130 | |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 131 | Decl *TemplateDeclInstantiator::InstantiateTypedefNameDecl(TypedefNameDecl *D, |
| 132 | bool IsTypeAlias) { |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 133 | bool Invalid = false; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 134 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
Douglas Gregor | 836adf6 | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 135 | if (DI->getType()->isDependentType() || |
| 136 | DI->getType()->isVariablyModifiedType()) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 137 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 138 | D->getLocation(), D->getDeclName()); |
| 139 | if (!DI) { |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 140 | Invalid = true; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 141 | DI = SemaRef.Context.getTrivialTypeSourceInfo(SemaRef.Context.IntTy); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 142 | } |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 143 | } else { |
| 144 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 145 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 147 | // Create the new typedef |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 148 | TypedefNameDecl *Typedef; |
| 149 | if (IsTypeAlias) |
| 150 | Typedef = TypeAliasDecl::Create(SemaRef.Context, Owner, D->getLocStart(), |
| 151 | D->getLocation(), D->getIdentifier(), DI); |
| 152 | else |
| 153 | Typedef = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocStart(), |
| 154 | D->getLocation(), D->getIdentifier(), DI); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 155 | if (Invalid) |
| 156 | Typedef->setInvalidDecl(); |
| 157 | |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 158 | // If the old typedef was the name for linkage purposes of an anonymous |
| 159 | // tag decl, re-establish that relationship for the new typedef. |
| 160 | if (const TagType *oldTagType = D->getUnderlyingType()->getAs<TagType>()) { |
| 161 | TagDecl *oldTag = oldTagType->getDecl(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 162 | if (oldTag->getTypedefNameForAnonDecl() == D) { |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 163 | TagDecl *newTag = DI->getType()->castAs<TagType>()->getDecl(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 164 | assert(!newTag->getIdentifier() && !newTag->getTypedefNameForAnonDecl()); |
| 165 | newTag->setTypedefNameForAnonDecl(Typedef); |
John McCall | cde5a40 | 2011-02-01 08:20:08 +0000 | [diff] [blame] | 166 | } |
Douglas Gregor | d57a38e | 2010-04-23 16:25:07 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 169 | if (TypedefNameDecl *Prev = D->getPreviousDeclaration()) { |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 170 | NamedDecl *InstPrev = SemaRef.FindInstantiatedDecl(D->getLocation(), Prev, |
| 171 | TemplateArgs); |
Douglas Gregor | b710722 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 172 | if (!InstPrev) |
| 173 | return 0; |
| 174 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 175 | Typedef->setPreviousDeclaration(cast<TypedefNameDecl>(InstPrev)); |
John McCall | 5126fd0 | 2009-12-30 00:31:22 +0000 | [diff] [blame] | 176 | } |
| 177 | |
John McCall | 1d8d1cc | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 178 | SemaRef.InstantiateAttrs(TemplateArgs, D, Typedef); |
Douglas Gregor | d57a38e | 2010-04-23 16:25:07 +0000 | [diff] [blame] | 179 | |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 180 | Typedef->setAccess(D->getAccess()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 181 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 182 | return Typedef; |
| 183 | } |
| 184 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 185 | Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 186 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/false); |
| 187 | Owner->addDecl(Typedef); |
| 188 | return Typedef; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | Decl *TemplateDeclInstantiator::VisitTypeAliasDecl(TypeAliasDecl *D) { |
Richard Smith | 3e4c6c4 | 2011-05-05 21:57:07 +0000 | [diff] [blame] | 192 | Decl *Typedef = InstantiateTypedefNameDecl(D, /*IsTypeAlias=*/true); |
| 193 | Owner->addDecl(Typedef); |
| 194 | return Typedef; |
| 195 | } |
| 196 | |
| 197 | Decl * |
| 198 | TemplateDeclInstantiator::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) { |
| 199 | // Create a local instantiation scope for this type alias template, which |
| 200 | // will contain the instantiations of the template parameters. |
| 201 | LocalInstantiationScope Scope(SemaRef); |
| 202 | |
| 203 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 204 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 205 | if (!InstParams) |
| 206 | return 0; |
| 207 | |
| 208 | TypeAliasDecl *Pattern = D->getTemplatedDecl(); |
| 209 | |
| 210 | TypeAliasTemplateDecl *PrevAliasTemplate = 0; |
| 211 | if (Pattern->getPreviousDeclaration()) { |
| 212 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
| 213 | if (Found.first != Found.second) { |
| 214 | PrevAliasTemplate = dyn_cast<TypeAliasTemplateDecl>(*Found.first); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | TypeAliasDecl *AliasInst = cast_or_null<TypeAliasDecl>( |
| 219 | InstantiateTypedefNameDecl(Pattern, /*IsTypeAlias=*/true)); |
| 220 | if (!AliasInst) |
| 221 | return 0; |
| 222 | |
| 223 | TypeAliasTemplateDecl *Inst |
| 224 | = TypeAliasTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 225 | D->getDeclName(), InstParams, AliasInst); |
| 226 | if (PrevAliasTemplate) |
| 227 | Inst->setPreviousDeclaration(PrevAliasTemplate); |
| 228 | |
| 229 | Inst->setAccess(D->getAccess()); |
| 230 | |
| 231 | if (!PrevAliasTemplate) |
| 232 | Inst->setInstantiatedFromMemberTemplate(D); |
| 233 | |
| 234 | Owner->addDecl(Inst); |
| 235 | |
| 236 | return Inst; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 239 | /// \brief Instantiate an initializer, breaking it into separate |
| 240 | /// initialization arguments. |
| 241 | /// |
| 242 | /// \param S The semantic analysis object. |
| 243 | /// |
| 244 | /// \param Init The initializer to instantiate. |
| 245 | /// |
| 246 | /// \param TemplateArgs Template arguments to be substituted into the |
| 247 | /// initializer. |
| 248 | /// |
| 249 | /// \param NewArgs Will be filled in with the instantiation arguments. |
| 250 | /// |
| 251 | /// \returns true if an error occurred, false otherwise |
| 252 | static bool InstantiateInitializer(Sema &S, Expr *Init, |
| 253 | const MultiLevelTemplateArgumentList &TemplateArgs, |
| 254 | SourceLocation &LParenLoc, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 255 | ASTOwningVector<Expr*> &NewArgs, |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 256 | SourceLocation &RParenLoc) { |
| 257 | NewArgs.clear(); |
| 258 | LParenLoc = SourceLocation(); |
| 259 | RParenLoc = SourceLocation(); |
| 260 | |
| 261 | if (!Init) |
| 262 | return false; |
| 263 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 264 | if (ExprWithCleanups *ExprTemp = dyn_cast<ExprWithCleanups>(Init)) |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 265 | Init = ExprTemp->getSubExpr(); |
| 266 | |
| 267 | while (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(Init)) |
| 268 | Init = Binder->getSubExpr(); |
| 269 | |
| 270 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Init)) |
| 271 | Init = ICE->getSubExprAsWritten(); |
| 272 | |
| 273 | if (ParenListExpr *ParenList = dyn_cast<ParenListExpr>(Init)) { |
| 274 | LParenLoc = ParenList->getLParenLoc(); |
| 275 | RParenLoc = ParenList->getRParenLoc(); |
Douglas Gregor | 91fc73e | 2011-01-07 19:35:17 +0000 | [diff] [blame] | 276 | return S.SubstExprs(ParenList->getExprs(), ParenList->getNumExprs(), |
| 277 | true, TemplateArgs, NewArgs); |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init)) { |
Douglas Gregor | 28329e5 | 2010-03-24 21:22:47 +0000 | [diff] [blame] | 281 | if (!isa<CXXTemporaryObjectExpr>(Construct)) { |
Douglas Gregor | 91fc73e | 2011-01-07 19:35:17 +0000 | [diff] [blame] | 282 | if (S.SubstExprs(Construct->getArgs(), Construct->getNumArgs(), true, |
| 283 | TemplateArgs, NewArgs)) |
Douglas Gregor | 28329e5 | 2010-03-24 21:22:47 +0000 | [diff] [blame] | 284 | return true; |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 285 | |
Douglas Gregor | 28329e5 | 2010-03-24 21:22:47 +0000 | [diff] [blame] | 286 | // FIXME: Fake locations! |
| 287 | LParenLoc = S.PP.getLocForEndOfToken(Init->getLocStart()); |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 288 | RParenLoc = LParenLoc; |
Douglas Gregor | 28329e5 | 2010-03-24 21:22:47 +0000 | [diff] [blame] | 289 | return false; |
| 290 | } |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 291 | } |
| 292 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 293 | ExprResult Result = S.SubstExpr(Init, TemplateArgs); |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 294 | if (Result.isInvalid()) |
| 295 | return true; |
| 296 | |
| 297 | NewArgs.push_back(Result.takeAs<Expr>()); |
| 298 | return false; |
| 299 | } |
| 300 | |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 301 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { |
Douglas Gregor | 9901c57 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 302 | // If this is the variable for an anonymous struct or union, |
| 303 | // instantiate the anonymous struct/union type first. |
| 304 | if (const RecordType *RecordTy = D->getType()->getAs<RecordType>()) |
| 305 | if (RecordTy->getDecl()->isAnonymousStructOrUnion()) |
| 306 | if (!VisitCXXRecordDecl(cast<CXXRecordDecl>(RecordTy->getDecl()))) |
| 307 | return 0; |
| 308 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 309 | // Do substitution on the type of the declaration |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 310 | TypeSourceInfo *DI = SemaRef.SubstType(D->getTypeSourceInfo(), |
John McCall | 0a5fa06 | 2009-10-21 02:39:02 +0000 | [diff] [blame] | 311 | TemplateArgs, |
| 312 | D->getTypeSpecStartLoc(), |
| 313 | D->getDeclName()); |
| 314 | if (!DI) |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 315 | return 0; |
| 316 | |
Douglas Gregor | c6dbc3f | 2010-09-12 07:37:24 +0000 | [diff] [blame] | 317 | if (DI->getType()->isFunctionType()) { |
| 318 | SemaRef.Diag(D->getLocation(), diag::err_variable_instantiates_to_function) |
| 319 | << D->isStaticDataMember() << DI->getType(); |
| 320 | return 0; |
| 321 | } |
| 322 | |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 323 | // Build the instantiated declaration |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 324 | VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 325 | D->getInnerLocStart(), |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 326 | D->getLocation(), D->getIdentifier(), |
John McCall | 0a5fa06 | 2009-10-21 02:39:02 +0000 | [diff] [blame] | 327 | DI->getType(), DI, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 328 | D->getStorageClass(), |
| 329 | D->getStorageClassAsWritten()); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 330 | Var->setThreadSpecified(D->isThreadSpecified()); |
| 331 | Var->setCXXDirectInitializer(D->hasCXXDirectInitializer()); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 332 | Var->setCXXForRangeDecl(D->isCXXForRangeDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 334 | // Substitute the nested name specifier, if any. |
| 335 | if (SubstQualifier(D, Var)) |
| 336 | return 0; |
| 337 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | // If we are instantiating a static data member defined |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 339 | // out-of-line, the instantiation will have the same lexical |
| 340 | // context (which will be a namespace scope) as the template. |
| 341 | if (D->isOutOfLine()) |
| 342 | Var->setLexicalDeclContext(D->getLexicalDeclContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 344 | Var->setAccess(D->getAccess()); |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 345 | |
Argyrios Kyrtzidis | 6b6b42a | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 346 | if (!D->isStaticDataMember()) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 347 | Var->setUsed(D->isUsed(false)); |
Argyrios Kyrtzidis | 6b6b42a | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 348 | Var->setReferenced(D->isReferenced()); |
| 349 | } |
Douglas Gregor | 4469e8a | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 350 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 351 | // FIXME: In theory, we could have a previous declaration for variables that |
| 352 | // are not static data members. |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 353 | bool Redeclaration = false; |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 354 | // FIXME: having to fake up a LookupResult is dumb. |
| 355 | LookupResult Previous(SemaRef, Var->getDeclName(), Var->getLocation(), |
Douglas Gregor | 449d0a8 | 2010-03-01 19:11:54 +0000 | [diff] [blame] | 356 | Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 357 | if (D->isStaticDataMember()) |
| 358 | SemaRef.LookupQualifiedName(Previous, Owner, false); |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 359 | SemaRef.CheckVariableDeclaration(Var, Previous, Redeclaration); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 360 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 361 | if (D->isOutOfLine()) { |
Abramo Bagnara | ea7b488 | 2010-06-04 09:35:39 +0000 | [diff] [blame] | 362 | if (!D->isStaticDataMember()) |
| 363 | D->getLexicalDeclContext()->addDecl(Var); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 364 | Owner->makeDeclVisibleInContext(Var); |
| 365 | } else { |
| 366 | Owner->addDecl(Var); |
Douglas Gregor | f7d72f5 | 2010-05-03 20:22:41 +0000 | [diff] [blame] | 367 | if (Owner->isFunctionOrMethod()) |
| 368 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Var); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 369 | } |
John McCall | 1d8d1cc | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 370 | SemaRef.InstantiateAttrs(TemplateArgs, D, Var); |
Fariborz Jahanian | 8dd0c56 | 2010-07-13 00:16:40 +0000 | [diff] [blame] | 371 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 372 | // Link instantiations of static data members back to the template from |
| 373 | // which they were instantiated. |
| 374 | if (Var->isStaticDataMember()) |
| 375 | SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D, |
Douglas Gregor | cf3293e | 2009-11-01 20:32:48 +0000 | [diff] [blame] | 376 | TSK_ImplicitInstantiation); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 377 | |
Douglas Gregor | 60c93c9 | 2010-02-09 07:26:29 +0000 | [diff] [blame] | 378 | if (Var->getAnyInitializer()) { |
| 379 | // We already have an initializer in the class. |
| 380 | } else if (D->getInit()) { |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 381 | if (Var->isStaticDataMember() && !D->isOutOfLine()) |
| 382 | SemaRef.PushExpressionEvaluationContext(Sema::Unevaluated); |
| 383 | else |
| 384 | SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); |
| 385 | |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 386 | // Instantiate the initializer. |
| 387 | SourceLocation LParenLoc, RParenLoc; |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 388 | ASTOwningVector<Expr*> InitArgs(SemaRef); |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 389 | if (!InstantiateInitializer(SemaRef, D->getInit(), TemplateArgs, LParenLoc, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 390 | InitArgs, RParenLoc)) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 391 | bool TypeMayContainAuto = true; |
Douglas Gregor | 07a77b4 | 2011-01-14 17:12:22 +0000 | [diff] [blame] | 392 | // Attach the initializer to the declaration, if we have one. |
| 393 | if (InitArgs.size() == 0) |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 394 | SemaRef.ActOnUninitializedDecl(Var, TypeMayContainAuto); |
Douglas Gregor | 07a77b4 | 2011-01-14 17:12:22 +0000 | [diff] [blame] | 395 | else if (D->hasCXXDirectInitializer()) { |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 396 | // Add the direct initializer to the declaration. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 397 | SemaRef.AddCXXDirectInitializerToDecl(Var, |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 398 | LParenLoc, |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 399 | move_arg(InitArgs), |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 400 | RParenLoc, |
| 401 | TypeMayContainAuto); |
Douglas Gregor | 07a77b4 | 2011-01-14 17:12:22 +0000 | [diff] [blame] | 402 | } else { |
| 403 | assert(InitArgs.size() == 1); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 404 | Expr *Init = InitArgs.take()[0]; |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 405 | SemaRef.AddInitializerToDecl(Var, Init, false, TypeMayContainAuto); |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 406 | } |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 407 | } else { |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 408 | // FIXME: Not too happy about invalidating the declaration |
| 409 | // because of a bogus initializer. |
| 410 | Var->setInvalidDecl(); |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Douglas Gregor | 1f5f3a4 | 2009-12-03 17:10:37 +0000 | [diff] [blame] | 413 | SemaRef.PopExpressionEvaluationContext(); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 414 | } else if ((!Var->isStaticDataMember() || Var->isOutOfLine()) && |
| 415 | !Var->isCXXForRangeDecl()) |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 416 | SemaRef.ActOnUninitializedDecl(Var, false); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 417 | |
Douglas Gregor | 5764f61 | 2010-05-08 23:05:03 +0000 | [diff] [blame] | 418 | // Diagnose unused local variables. |
| 419 | if (!Var->isInvalidDecl() && Owner->isFunctionOrMethod() && !Var->isUsed()) |
| 420 | SemaRef.DiagnoseUnusedDecl(Var); |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 421 | |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 422 | return Var; |
| 423 | } |
| 424 | |
Abramo Bagnara | 6206d53 | 2010-06-05 05:09:32 +0000 | [diff] [blame] | 425 | Decl *TemplateDeclInstantiator::VisitAccessSpecDecl(AccessSpecDecl *D) { |
| 426 | AccessSpecDecl* AD |
| 427 | = AccessSpecDecl::Create(SemaRef.Context, D->getAccess(), Owner, |
| 428 | D->getAccessSpecifierLoc(), D->getColonLoc()); |
| 429 | Owner->addHiddenDecl(AD); |
| 430 | return AD; |
| 431 | } |
| 432 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 433 | Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { |
| 434 | bool Invalid = false; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 435 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
Douglas Gregor | 836adf6 | 2010-05-24 17:22:01 +0000 | [diff] [blame] | 436 | if (DI->getType()->isDependentType() || |
| 437 | DI->getType()->isVariablyModifiedType()) { |
John McCall | 07fb6be | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 438 | DI = SemaRef.SubstType(DI, TemplateArgs, |
| 439 | D->getLocation(), D->getDeclName()); |
| 440 | if (!DI) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 441 | DI = D->getTypeSourceInfo(); |
John McCall | 07fb6be | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 442 | Invalid = true; |
| 443 | } else if (DI->getType()->isFunctionType()) { |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 444 | // C++ [temp.arg.type]p3: |
| 445 | // If a declaration acquires a function type through a type |
| 446 | // dependent on a template-parameter and this causes a |
| 447 | // declaration that does not use the syntactic form of a |
| 448 | // function declarator to have function type, the program is |
| 449 | // ill-formed. |
| 450 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
John McCall | 07fb6be | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 451 | << DI->getType(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 452 | Invalid = true; |
| 453 | } |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 454 | } else { |
| 455 | SemaRef.MarkDeclarationsReferencedInType(D->getLocation(), DI->getType()); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | Expr *BitWidth = D->getBitWidth(); |
| 459 | if (Invalid) |
| 460 | BitWidth = 0; |
| 461 | else if (BitWidth) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 462 | // The bit-width expression is not potentially evaluated. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 463 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 465 | ExprResult InstantiatedBitWidth |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 466 | = SemaRef.SubstExpr(BitWidth, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 467 | if (InstantiatedBitWidth.isInvalid()) { |
| 468 | Invalid = true; |
| 469 | BitWidth = 0; |
| 470 | } else |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 471 | BitWidth = InstantiatedBitWidth.takeAs<Expr>(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 472 | } |
| 473 | |
John McCall | 07fb6be | 2009-10-22 23:33:21 +0000 | [diff] [blame] | 474 | FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), |
| 475 | DI->getType(), DI, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 476 | cast<RecordDecl>(Owner), |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 477 | D->getLocation(), |
| 478 | D->isMutable(), |
| 479 | BitWidth, |
Steve Naroff | ea218b8 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 480 | D->getTypeSpecStartLoc(), |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 481 | D->getAccess(), |
| 482 | 0); |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 483 | if (!Field) { |
| 484 | cast<Decl>(Owner)->setInvalidDecl(); |
Anders Carlsson | f4b5f5c | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 485 | return 0; |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 486 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | |
John McCall | 1d8d1cc | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 488 | SemaRef.InstantiateAttrs(TemplateArgs, D, Field); |
Anders Carlsson | d8fe2d5 | 2009-11-07 06:07:58 +0000 | [diff] [blame] | 489 | |
Anders Carlsson | f4b5f5c | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 490 | if (Invalid) |
| 491 | Field->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | |
Anders Carlsson | f4b5f5c | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 493 | if (!Field->getDeclName()) { |
| 494 | // Keep track of where this decl came from. |
| 495 | SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D); |
Douglas Gregor | 9901c57 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 496 | } |
| 497 | if (CXXRecordDecl *Parent= dyn_cast<CXXRecordDecl>(Field->getDeclContext())) { |
| 498 | if (Parent->isAnonymousStructOrUnion() && |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 499 | Parent->getRedeclContext()->isFunctionOrMethod()) |
Douglas Gregor | 9901c57 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 500 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Field); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 501 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | |
Anders Carlsson | f4b5f5c | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 503 | Field->setImplicit(D->isImplicit()); |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 504 | Field->setAccess(D->getAccess()); |
Anders Carlsson | f4b5f5c | 2009-09-02 19:17:55 +0000 | [diff] [blame] | 505 | Owner->addDecl(Field); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 506 | |
| 507 | return Field; |
| 508 | } |
| 509 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 510 | Decl *TemplateDeclInstantiator::VisitIndirectFieldDecl(IndirectFieldDecl *D) { |
| 511 | NamedDecl **NamedChain = |
| 512 | new (SemaRef.Context)NamedDecl*[D->getChainingSize()]; |
| 513 | |
| 514 | int i = 0; |
| 515 | for (IndirectFieldDecl::chain_iterator PI = |
| 516 | D->chain_begin(), PE = D->chain_end(); |
Douglas Gregor | b710722 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 517 | PI != PE; ++PI) { |
| 518 | NamedDecl *Next = SemaRef.FindInstantiatedDecl(D->getLocation(), *PI, |
| 519 | TemplateArgs); |
| 520 | if (!Next) |
| 521 | return 0; |
| 522 | |
| 523 | NamedChain[i++] = Next; |
| 524 | } |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 525 | |
Francois Pichet | 40e1775 | 2010-12-09 10:07:54 +0000 | [diff] [blame] | 526 | QualType T = cast<FieldDecl>(NamedChain[i-1])->getType(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 527 | IndirectFieldDecl* IndirectField |
| 528 | = IndirectFieldDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
Francois Pichet | 40e1775 | 2010-12-09 10:07:54 +0000 | [diff] [blame] | 529 | D->getIdentifier(), T, |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 530 | NamedChain, D->getChainingSize()); |
| 531 | |
| 532 | |
| 533 | IndirectField->setImplicit(D->isImplicit()); |
| 534 | IndirectField->setAccess(D->getAccess()); |
| 535 | Owner->addDecl(IndirectField); |
| 536 | return IndirectField; |
| 537 | } |
| 538 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 539 | Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 540 | // Handle friend type expressions by simply substituting template |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 541 | // parameters into the pattern type and checking the result. |
John McCall | 32f2fb5 | 2010-03-25 18:04:51 +0000 | [diff] [blame] | 542 | if (TypeSourceInfo *Ty = D->getFriendType()) { |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 543 | TypeSourceInfo *InstTy; |
| 544 | // If this is an unsupported friend, don't bother substituting template |
| 545 | // arguments into it. The actual type referred to won't be used by any |
| 546 | // parts of Clang, and may not be valid for instantiating. Just use the |
| 547 | // same info for the instantiated friend. |
| 548 | if (D->isUnsupportedFriend()) { |
| 549 | InstTy = Ty; |
| 550 | } else { |
| 551 | InstTy = SemaRef.SubstType(Ty, TemplateArgs, |
| 552 | D->getLocation(), DeclarationName()); |
| 553 | } |
| 554 | if (!InstTy) |
Douglas Gregor | 7557a13 | 2009-12-24 20:56:24 +0000 | [diff] [blame] | 555 | return 0; |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 556 | |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 557 | FriendDecl *FD = SemaRef.CheckFriendTypeDecl(D->getFriendLoc(), InstTy); |
| 558 | if (!FD) |
| 559 | return 0; |
| 560 | |
| 561 | FD->setAccess(AS_public); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 562 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 563 | Owner->addDecl(FD); |
| 564 | return FD; |
| 565 | } |
| 566 | |
| 567 | NamedDecl *ND = D->getFriendDecl(); |
| 568 | assert(ND && "friend decl must be a decl or a type!"); |
| 569 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 570 | // All of the Visit implementations for the various potential friend |
| 571 | // declarations have to be carefully written to work for friend |
| 572 | // objects, with the most important detail being that the target |
| 573 | // decl should almost certainly not be placed in Owner. |
| 574 | Decl *NewND = Visit(ND); |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 575 | if (!NewND) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 577 | FriendDecl *FD = |
Douglas Gregor | 06245bf | 2010-04-07 17:57:12 +0000 | [diff] [blame] | 578 | FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 579 | cast<NamedDecl>(NewND), D->getFriendLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 580 | FD->setAccess(AS_public); |
John McCall | 9a34edb | 2010-10-19 01:40:49 +0000 | [diff] [blame] | 581 | FD->setUnsupportedFriend(D->isUnsupportedFriend()); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 582 | Owner->addDecl(FD); |
| 583 | return FD; |
John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 586 | Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 587 | Expr *AssertExpr = D->getAssertExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 588 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 589 | // The expression in a static assertion is not potentially evaluated. |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 590 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 591 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 592 | ExprResult InstantiatedAssertExpr |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 593 | = SemaRef.SubstExpr(AssertExpr, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 594 | if (InstantiatedAssertExpr.isInvalid()) |
| 595 | return 0; |
| 596 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 597 | ExprResult Message(D->getMessage()); |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 598 | D->getMessage(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 599 | return SemaRef.ActOnStaticAssertDeclaration(D->getLocation(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 600 | InstantiatedAssertExpr.get(), |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 601 | Message.get(), |
| 602 | D->getRParenLoc()); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 606 | EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, D->getLocStart(), |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 607 | D->getLocation(), D->getIdentifier(), |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 608 | /*PrevDecl=*/0, D->isScoped(), |
| 609 | D->isScopedUsingClassTag(), D->isFixed()); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 610 | if (D->isFixed()) { |
| 611 | if (TypeSourceInfo* TI = D->getIntegerTypeSourceInfo()) { |
| 612 | // If we have type source information for the underlying type, it means it |
| 613 | // has been explicitly set by the user. Perform substitution on it before |
| 614 | // moving on. |
| 615 | SourceLocation UnderlyingLoc = TI->getTypeLoc().getBeginLoc(); |
| 616 | Enum->setIntegerTypeSourceInfo(SemaRef.SubstType(TI, |
| 617 | TemplateArgs, |
| 618 | UnderlyingLoc, |
| 619 | DeclarationName())); |
| 620 | |
| 621 | if (!Enum->getIntegerTypeSourceInfo()) |
| 622 | Enum->setIntegerType(SemaRef.Context.IntTy); |
| 623 | } |
| 624 | else { |
| 625 | assert(!D->getIntegerType()->isDependentType() |
| 626 | && "Dependent type without type source info"); |
| 627 | Enum->setIntegerType(D->getIntegerType()); |
| 628 | } |
| 629 | } |
| 630 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 631 | SemaRef.InstantiateAttrs(TemplateArgs, D, Enum); |
| 632 | |
Douglas Gregor | 8dbc3c6 | 2009-05-27 17:20:35 +0000 | [diff] [blame] | 633 | Enum->setInstantiationOfMemberEnum(D); |
Douglas Gregor | 06c0fec | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 634 | Enum->setAccess(D->getAccess()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 635 | if (SubstQualifier(D, Enum)) return 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 636 | Owner->addDecl(Enum); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 637 | Enum->startDefinition(); |
| 638 | |
Douglas Gregor | 96084f1 | 2010-03-01 19:00:07 +0000 | [diff] [blame] | 639 | if (D->getDeclContext()->isFunctionOrMethod()) |
| 640 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Enum); |
| 641 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 642 | llvm::SmallVector<Decl*, 4> Enumerators; |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 643 | |
| 644 | EnumConstantDecl *LastEnumConst = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 645 | for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(), |
| 646 | ECEnd = D->enumerator_end(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 647 | EC != ECEnd; ++EC) { |
| 648 | // The specified value for the enumerator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 649 | ExprResult Value = SemaRef.Owned((Expr *)0); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 650 | if (Expr *UninstValue = EC->getInitExpr()) { |
| 651 | // The enumerator's value expression is not potentially evaluated. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 652 | EnterExpressionEvaluationContext Unevaluated(SemaRef, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 653 | Sema::Unevaluated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 655 | Value = SemaRef.SubstExpr(UninstValue, TemplateArgs); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 656 | } |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 657 | |
| 658 | // Drop the initial value and continue. |
| 659 | bool isInvalid = false; |
| 660 | if (Value.isInvalid()) { |
| 661 | Value = SemaRef.Owned((Expr *)0); |
| 662 | isInvalid = true; |
| 663 | } |
| 664 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 665 | EnumConstantDecl *EnumConst |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 666 | = SemaRef.CheckEnumConstant(Enum, LastEnumConst, |
| 667 | EC->getLocation(), EC->getIdentifier(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 668 | Value.get()); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 669 | |
| 670 | if (isInvalid) { |
| 671 | if (EnumConst) |
| 672 | EnumConst->setInvalidDecl(); |
| 673 | Enum->setInvalidDecl(); |
| 674 | } |
| 675 | |
| 676 | if (EnumConst) { |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 677 | SemaRef.InstantiateAttrs(TemplateArgs, *EC, EnumConst); |
| 678 | |
John McCall | 3b85ecf | 2010-01-23 22:37:59 +0000 | [diff] [blame] | 679 | EnumConst->setAccess(Enum->getAccess()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 680 | Enum->addDecl(EnumConst); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 681 | Enumerators.push_back(EnumConst); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 682 | LastEnumConst = EnumConst; |
Douglas Gregor | 96084f1 | 2010-03-01 19:00:07 +0000 | [diff] [blame] | 683 | |
| 684 | if (D->getDeclContext()->isFunctionOrMethod()) { |
| 685 | // If the enumeration is within a function or method, record the enum |
| 686 | // constant as a local. |
| 687 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(*EC, EnumConst); |
| 688 | } |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 689 | } |
| 690 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 691 | |
Mike Stump | c6e35aa | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 692 | // FIXME: Fixup LBraceLoc and RBraceLoc |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 693 | // FIXME: Empty Scope and AttributeList (required to handle attribute packed). |
Mike Stump | c6e35aa | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 694 | SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(), |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 695 | Enum, |
Eli Friedman | de7a0fc | 2010-08-15 02:27:09 +0000 | [diff] [blame] | 696 | Enumerators.data(), Enumerators.size(), |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 697 | 0, 0); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 698 | |
| 699 | return Enum; |
| 700 | } |
| 701 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 702 | Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 703 | assert(false && "EnumConstantDecls can only occur within EnumDecls."); |
| 704 | return 0; |
| 705 | } |
| 706 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 707 | Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 708 | bool isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 709 | |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 710 | // Create a local instantiation scope for this class template, which |
| 711 | // will contain the instantiations of the template parameters. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 712 | LocalInstantiationScope Scope(SemaRef); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 713 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 714 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 715 | if (!InstParams) |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 716 | return NULL; |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 717 | |
| 718 | CXXRecordDecl *Pattern = D->getTemplatedDecl(); |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 719 | |
| 720 | // Instantiate the qualifier. We have to do this first in case |
| 721 | // we're a friend declaration, because if we are then we need to put |
| 722 | // the new declaration in the appropriate context. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 723 | NestedNameSpecifierLoc QualifierLoc = Pattern->getQualifierLoc(); |
| 724 | if (QualifierLoc) { |
| 725 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 726 | TemplateArgs); |
| 727 | if (!QualifierLoc) |
| 728 | return 0; |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | CXXRecordDecl *PrevDecl = 0; |
| 732 | ClassTemplateDecl *PrevClassTemplate = 0; |
| 733 | |
Nick Lewycky | 37574f5 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 734 | if (!isFriend && Pattern->getPreviousDeclaration()) { |
| 735 | DeclContext::lookup_result Found = Owner->lookup(Pattern->getDeclName()); |
| 736 | if (Found.first != Found.second) { |
| 737 | PrevClassTemplate = dyn_cast<ClassTemplateDecl>(*Found.first); |
| 738 | if (PrevClassTemplate) |
| 739 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); |
| 740 | } |
| 741 | } |
| 742 | |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 743 | // If this isn't a friend, then it's a member template, in which |
| 744 | // case we just want to build the instantiation in the |
| 745 | // specialization. If it is a friend, we want to build it in |
| 746 | // the appropriate context. |
| 747 | DeclContext *DC = Owner; |
| 748 | if (isFriend) { |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 749 | if (QualifierLoc) { |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 750 | CXXScopeSpec SS; |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 751 | SS.Adopt(QualifierLoc); |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 752 | DC = SemaRef.computeDeclContext(SS); |
| 753 | if (!DC) return 0; |
| 754 | } else { |
| 755 | DC = SemaRef.FindInstantiatedContext(Pattern->getLocation(), |
| 756 | Pattern->getDeclContext(), |
| 757 | TemplateArgs); |
| 758 | } |
| 759 | |
| 760 | // Look for a previous declaration of the template in the owning |
| 761 | // context. |
| 762 | LookupResult R(SemaRef, Pattern->getDeclName(), Pattern->getLocation(), |
| 763 | Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 764 | SemaRef.LookupQualifiedName(R, DC); |
| 765 | |
| 766 | if (R.isSingleResult()) { |
| 767 | PrevClassTemplate = R.getAsSingle<ClassTemplateDecl>(); |
| 768 | if (PrevClassTemplate) |
| 769 | PrevDecl = PrevClassTemplate->getTemplatedDecl(); |
| 770 | } |
| 771 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 772 | if (!PrevClassTemplate && QualifierLoc) { |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 773 | SemaRef.Diag(Pattern->getLocation(), diag::err_not_tag_in_scope) |
Douglas Gregor | 1eabb7d | 2010-03-31 23:17:41 +0000 | [diff] [blame] | 774 | << D->getTemplatedDecl()->getTagKind() << Pattern->getDeclName() << DC |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 775 | << QualifierLoc.getSourceRange(); |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 776 | return 0; |
| 777 | } |
| 778 | |
Douglas Gregor | c53d0d7 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 779 | bool AdoptedPreviousTemplateParams = false; |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 780 | if (PrevClassTemplate) { |
Douglas Gregor | c53d0d7 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 781 | bool Complain = true; |
| 782 | |
| 783 | // HACK: libstdc++ 4.2.1 contains an ill-formed friend class |
| 784 | // template for struct std::tr1::__detail::_Map_base, where the |
| 785 | // template parameters of the friend declaration don't match the |
| 786 | // template parameters of the original declaration. In this one |
| 787 | // case, we don't complain about the ill-formed friend |
| 788 | // declaration. |
| 789 | if (isFriend && Pattern->getIdentifier() && |
| 790 | Pattern->getIdentifier()->isStr("_Map_base") && |
| 791 | DC->isNamespace() && |
| 792 | cast<NamespaceDecl>(DC)->getIdentifier() && |
| 793 | cast<NamespaceDecl>(DC)->getIdentifier()->isStr("__detail")) { |
| 794 | DeclContext *DCParent = DC->getParent(); |
| 795 | if (DCParent->isNamespace() && |
| 796 | cast<NamespaceDecl>(DCParent)->getIdentifier() && |
| 797 | cast<NamespaceDecl>(DCParent)->getIdentifier()->isStr("tr1")) { |
| 798 | DeclContext *DCParent2 = DCParent->getParent(); |
| 799 | if (DCParent2->isNamespace() && |
| 800 | cast<NamespaceDecl>(DCParent2)->getIdentifier() && |
| 801 | cast<NamespaceDecl>(DCParent2)->getIdentifier()->isStr("std") && |
| 802 | DCParent2->getParent()->isTranslationUnit()) |
| 803 | Complain = false; |
| 804 | } |
| 805 | } |
| 806 | |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 807 | TemplateParameterList *PrevParams |
| 808 | = PrevClassTemplate->getTemplateParameters(); |
| 809 | |
| 810 | // Make sure the parameter lists match. |
| 811 | if (!SemaRef.TemplateParameterListsAreEqual(InstParams, PrevParams, |
Douglas Gregor | c53d0d7 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 812 | Complain, |
| 813 | Sema::TPL_TemplateMatch)) { |
| 814 | if (Complain) |
| 815 | return 0; |
| 816 | |
| 817 | AdoptedPreviousTemplateParams = true; |
| 818 | InstParams = PrevParams; |
| 819 | } |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 820 | |
| 821 | // Do some additional validation, then merge default arguments |
| 822 | // from the existing declarations. |
Douglas Gregor | c53d0d7 | 2010-04-08 18:16:15 +0000 | [diff] [blame] | 823 | if (!AdoptedPreviousTemplateParams && |
| 824 | SemaRef.CheckTemplateParameterList(InstParams, PrevParams, |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 825 | Sema::TPC_ClassTemplate)) |
| 826 | return 0; |
| 827 | } |
| 828 | } |
| 829 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 830 | CXXRecordDecl *RecordInst |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 831 | = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), DC, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 832 | Pattern->getLocStart(), Pattern->getLocation(), |
| 833 | Pattern->getIdentifier(), PrevDecl, |
Douglas Gregor | f0510d4 | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 834 | /*DelayTypeCreation=*/true); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 835 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 836 | if (QualifierLoc) |
| 837 | RecordInst->setQualifierInfo(QualifierLoc); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 838 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 839 | ClassTemplateDecl *Inst |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 840 | = ClassTemplateDecl::Create(SemaRef.Context, DC, D->getLocation(), |
| 841 | D->getIdentifier(), InstParams, RecordInst, |
| 842 | PrevClassTemplate); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 843 | RecordInst->setDescribedClassTemplate(Inst); |
John McCall | ea7390c | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 844 | |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 845 | if (isFriend) { |
John McCall | ea7390c | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 846 | if (PrevClassTemplate) |
| 847 | Inst->setAccess(PrevClassTemplate->getAccess()); |
| 848 | else |
| 849 | Inst->setAccess(D->getAccess()); |
| 850 | |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 851 | Inst->setObjectOfFriendDecl(PrevClassTemplate != 0); |
| 852 | // TODO: do we want to track the instantiation progeny of this |
| 853 | // friend target decl? |
| 854 | } else { |
Douglas Gregor | e8c01bd | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 855 | Inst->setAccess(D->getAccess()); |
Nick Lewycky | 37574f5 | 2010-11-08 23:29:42 +0000 | [diff] [blame] | 856 | if (!PrevClassTemplate) |
| 857 | Inst->setInstantiatedFromMemberTemplate(D); |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 858 | } |
Douglas Gregor | f0510d4 | 2009-10-12 23:11:44 +0000 | [diff] [blame] | 859 | |
| 860 | // Trigger creation of the type for the instantiation. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 861 | SemaRef.Context.getInjectedClassNameType(RecordInst, |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 862 | Inst->getInjectedClassNameSpecialization()); |
John McCall | ea7390c | 2010-04-08 20:25:50 +0000 | [diff] [blame] | 863 | |
Douglas Gregor | 259571e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 864 | // Finish handling of friends. |
John McCall | 93ba857 | 2010-03-25 06:39:04 +0000 | [diff] [blame] | 865 | if (isFriend) { |
| 866 | DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false); |
Douglas Gregor | e8c01bd | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 867 | return Inst; |
Douglas Gregor | 259571e | 2009-10-30 22:42:42 +0000 | [diff] [blame] | 868 | } |
Douglas Gregor | e8c01bd | 2009-10-30 21:07:27 +0000 | [diff] [blame] | 869 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 870 | Owner->addDecl(Inst); |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 871 | |
| 872 | if (!PrevClassTemplate) { |
| 873 | // Queue up any out-of-line partial specializations of this member |
| 874 | // class template; the client will force their instantiation once |
| 875 | // the enclosing class has been instantiated. |
| 876 | llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 877 | D->getPartialSpecializations(PartialSpecs); |
| 878 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) |
| 879 | if (PartialSpecs[I]->isOutOfLine()) |
| 880 | OutOfLinePartialSpecs.push_back(std::make_pair(Inst, PartialSpecs[I])); |
| 881 | } |
| 882 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 883 | return Inst; |
| 884 | } |
| 885 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 886 | Decl * |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 887 | TemplateDeclInstantiator::VisitClassTemplatePartialSpecializationDecl( |
| 888 | ClassTemplatePartialSpecializationDecl *D) { |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 889 | ClassTemplateDecl *ClassTemplate = D->getSpecializedTemplate(); |
| 890 | |
| 891 | // Lookup the already-instantiated declaration in the instantiation |
| 892 | // of the class template and return that. |
| 893 | DeclContext::lookup_result Found |
| 894 | = Owner->lookup(ClassTemplate->getDeclName()); |
| 895 | if (Found.first == Found.second) |
| 896 | return 0; |
| 897 | |
| 898 | ClassTemplateDecl *InstClassTemplate |
| 899 | = dyn_cast<ClassTemplateDecl>(*Found.first); |
| 900 | if (!InstClassTemplate) |
| 901 | return 0; |
| 902 | |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 903 | if (ClassTemplatePartialSpecializationDecl *Result |
| 904 | = InstClassTemplate->findPartialSpecInstantiatedFromMember(D)) |
| 905 | return Result; |
| 906 | |
| 907 | return InstantiateClassTemplatePartialSpecialization(InstClassTemplate, D); |
Douglas Gregor | 7974c3b | 2009-10-07 17:21:34 +0000 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | Decl * |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 911 | TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 912 | // Create a local instantiation scope for this function template, which |
| 913 | // will contain the instantiations of the template parameters and then get |
| 914 | // merged with the local instantiation scope for the function template |
| 915 | // itself. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 916 | LocalInstantiationScope Scope(SemaRef); |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 917 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 918 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 919 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 920 | if (!InstParams) |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 921 | return NULL; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 922 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 923 | FunctionDecl *Instantiated = 0; |
| 924 | if (CXXMethodDecl *DMethod = dyn_cast<CXXMethodDecl>(D->getTemplatedDecl())) |
| 925 | Instantiated = cast_or_null<FunctionDecl>(VisitCXXMethodDecl(DMethod, |
| 926 | InstParams)); |
| 927 | else |
| 928 | Instantiated = cast_or_null<FunctionDecl>(VisitFunctionDecl( |
| 929 | D->getTemplatedDecl(), |
| 930 | InstParams)); |
| 931 | |
| 932 | if (!Instantiated) |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 933 | return 0; |
| 934 | |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 935 | Instantiated->setAccess(D->getAccess()); |
| 936 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 937 | // Link the instantiated function template declaration to the function |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 938 | // template from which it was instantiated. |
Douglas Gregor | 37d68185 | 2009-10-12 22:27:17 +0000 | [diff] [blame] | 939 | FunctionTemplateDecl *InstTemplate |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 940 | = Instantiated->getDescribedFunctionTemplate(); |
Douglas Gregor | 37d68185 | 2009-10-12 22:27:17 +0000 | [diff] [blame] | 941 | InstTemplate->setAccess(D->getAccess()); |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 942 | assert(InstTemplate && |
| 943 | "VisitFunctionDecl/CXXMethodDecl didn't create a template!"); |
John McCall | e976ffe | 2009-12-14 23:19:40 +0000 | [diff] [blame] | 944 | |
John McCall | b1a56e7 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 945 | bool isFriend = (InstTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 946 | |
John McCall | e976ffe | 2009-12-14 23:19:40 +0000 | [diff] [blame] | 947 | // Link the instantiation back to the pattern *unless* this is a |
| 948 | // non-definition friend declaration. |
| 949 | if (!InstTemplate->getInstantiatedFromMemberTemplate() && |
John McCall | b1a56e7 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 950 | !(isFriend && !D->getTemplatedDecl()->isThisDeclarationADefinition())) |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 951 | InstTemplate->setInstantiatedFromMemberTemplate(D); |
| 952 | |
John McCall | b1a56e7 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 953 | // Make declarations visible in the appropriate context. |
| 954 | if (!isFriend) |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 955 | Owner->addDecl(InstTemplate); |
John McCall | b1a56e7 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 956 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 957 | return InstTemplate; |
| 958 | } |
| 959 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 960 | Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 961 | CXXRecordDecl *PrevDecl = 0; |
| 962 | if (D->isInjectedClassName()) |
| 963 | PrevDecl = cast<CXXRecordDecl>(Owner); |
John McCall | 6c1c1b8 | 2009-12-15 22:29:06 +0000 | [diff] [blame] | 964 | else if (D->getPreviousDeclaration()) { |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 965 | NamedDecl *Prev = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 966 | D->getPreviousDeclaration(), |
John McCall | 6c1c1b8 | 2009-12-15 22:29:06 +0000 | [diff] [blame] | 967 | TemplateArgs); |
| 968 | if (!Prev) return 0; |
| 969 | PrevDecl = cast<CXXRecordDecl>(Prev); |
| 970 | } |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 971 | |
| 972 | CXXRecordDecl *Record |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 973 | = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 974 | D->getLocStart(), D->getLocation(), |
| 975 | D->getIdentifier(), PrevDecl); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 976 | |
| 977 | // Substitute the nested name specifier, if any. |
| 978 | if (SubstQualifier(D, Record)) |
| 979 | return 0; |
| 980 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 981 | Record->setImplicit(D->isImplicit()); |
Eli Friedman | eaba1af | 2009-08-27 19:11:42 +0000 | [diff] [blame] | 982 | // FIXME: Check against AS_none is an ugly hack to work around the issue that |
| 983 | // the tag decls introduced by friend class declarations don't have an access |
| 984 | // specifier. Remove once this area of the code gets sorted out. |
| 985 | if (D->getAccess() != AS_none) |
| 986 | Record->setAccess(D->getAccess()); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 987 | if (!D->isInjectedClassName()) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 988 | Record->setInstantiationOfMemberClass(D, TSK_ImplicitInstantiation); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 989 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 990 | // If the original function was part of a friend declaration, |
| 991 | // inherit its namespace state. |
| 992 | if (Decl::FriendObjectKind FOK = D->getFriendObjectKind()) |
| 993 | Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared); |
| 994 | |
Douglas Gregor | 9901c57 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 995 | // Make sure that anonymous structs and unions are recorded. |
| 996 | if (D->isAnonymousStructOrUnion()) { |
| 997 | Record->setAnonymousStructOrUnion(true); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 998 | if (Record->getDeclContext()->getRedeclContext()->isFunctionOrMethod()) |
Douglas Gregor | 9901c57 | 2010-05-21 00:31:19 +0000 | [diff] [blame] | 999 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Record); |
| 1000 | } |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1001 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1002 | Owner->addDecl(Record); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 1003 | return Record; |
| 1004 | } |
| 1005 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1006 | /// Normal class members are of more specific types and therefore |
| 1007 | /// don't make it here. This function serves two purposes: |
| 1008 | /// 1) instantiating function templates |
| 1009 | /// 2) substituting friend declarations |
| 1010 | /// FIXME: preserve function definitions in case #2 |
Douglas Gregor | 7557a13 | 2009-12-24 20:56:24 +0000 | [diff] [blame] | 1011 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1012 | TemplateParameterList *TemplateParams) { |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1013 | // Check whether there is already a function template specialization for |
| 1014 | // this declaration. |
| 1015 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
| 1016 | void *InsertPos = 0; |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1017 | if (FunctionTemplate && !TemplateParams) { |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1018 | std::pair<const TemplateArgument *, unsigned> Innermost |
| 1019 | = TemplateArgs.getInnermost(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1020 | |
Argyrios Kyrtzidis | 2c853e4 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1021 | FunctionDecl *SpecFunc |
| 1022 | = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second, |
| 1023 | InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1024 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1025 | // If we already have a function template specialization, return it. |
Argyrios Kyrtzidis | 2c853e4 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1026 | if (SpecFunc) |
| 1027 | return SpecFunc; |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1028 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1029 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1030 | bool isFriend; |
| 1031 | if (FunctionTemplate) |
| 1032 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1033 | else |
| 1034 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1035 | |
Douglas Gregor | 79c2278 | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1036 | bool MergeWithParentScope = (TemplateParams != 0) || |
Douglas Gregor | b212d9a | 2010-05-21 21:25:08 +0000 | [diff] [blame] | 1037 | Owner->isFunctionOrMethod() || |
Douglas Gregor | 79c2278 | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1038 | !(isa<Decl>(Owner) && |
| 1039 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1040 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1041 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1042 | llvm::SmallVector<ParmVarDecl *, 4> Params; |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1043 | TypeSourceInfo *TInfo = D->getTypeSourceInfo(); |
| 1044 | TInfo = SubstFunctionType(D, Params); |
| 1045 | if (!TInfo) |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1046 | return 0; |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1047 | QualType T = TInfo->getType(); |
John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 1048 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1049 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); |
| 1050 | if (QualifierLoc) { |
| 1051 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
| 1052 | TemplateArgs); |
| 1053 | if (!QualifierLoc) |
| 1054 | return 0; |
John McCall | d325daa | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
John McCall | 68b6b87 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1057 | // If we're instantiating a local function declaration, put the result |
| 1058 | // in the owner; otherwise we need to find the instantiated context. |
| 1059 | DeclContext *DC; |
| 1060 | if (D->getDeclContext()->isFunctionOrMethod()) |
| 1061 | DC = Owner; |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1062 | else if (isFriend && QualifierLoc) { |
John McCall | d325daa | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1063 | CXXScopeSpec SS; |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1064 | SS.Adopt(QualifierLoc); |
John McCall | d325daa | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1065 | DC = SemaRef.computeDeclContext(SS); |
| 1066 | if (!DC) return 0; |
| 1067 | } else { |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1068 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), D->getDeclContext(), |
| 1069 | TemplateArgs); |
John McCall | d325daa | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1070 | } |
John McCall | 68b6b87 | 2010-02-06 01:50:47 +0000 | [diff] [blame] | 1071 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1072 | FunctionDecl *Function = |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1073 | FunctionDecl::Create(SemaRef.Context, DC, D->getInnerLocStart(), |
| 1074 | D->getLocation(), D->getDeclName(), T, TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1075 | D->getStorageClass(), D->getStorageClassAsWritten(), |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 1076 | D->isInlineSpecified(), D->hasWrittenPrototype()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1077 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1078 | if (QualifierLoc) |
| 1079 | Function->setQualifierInfo(QualifierLoc); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1080 | |
John McCall | b1a56e7 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1081 | DeclContext *LexicalDC = Owner; |
| 1082 | if (!isFriend && D->isOutOfLine()) { |
| 1083 | assert(D->getDeclContext()->isFileContext()); |
| 1084 | LexicalDC = D->getDeclContext(); |
| 1085 | } |
| 1086 | |
| 1087 | Function->setLexicalDeclContext(LexicalDC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1089 | // Attach the parameters |
| 1090 | for (unsigned P = 0; P < Params.size(); ++P) |
John McCall | 3019c44 | 2010-09-17 00:50:28 +0000 | [diff] [blame] | 1091 | if (Params[P]) |
| 1092 | Params[P]->setOwningFunction(Function); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1093 | Function->setParams(Params.data(), Params.size()); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1094 | |
Douglas Gregor | ac7c2c8 | 2010-05-17 16:38:00 +0000 | [diff] [blame] | 1095 | SourceLocation InstantiateAtPOI; |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1096 | if (TemplateParams) { |
| 1097 | // Our resulting instantiation is actually a function template, since we |
| 1098 | // are substituting only the outer template parameters. For example, given |
| 1099 | // |
| 1100 | // template<typename T> |
| 1101 | // struct X { |
| 1102 | // template<typename U> friend void f(T, U); |
| 1103 | // }; |
| 1104 | // |
| 1105 | // X<int> x; |
| 1106 | // |
| 1107 | // We are instantiating the friend function template "f" within X<int>, |
| 1108 | // which means substituting int for T, but leaving "f" as a friend function |
| 1109 | // template. |
| 1110 | // Build the function template itself. |
John McCall | d325daa | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1111 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, DC, |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1112 | Function->getLocation(), |
| 1113 | Function->getDeclName(), |
| 1114 | TemplateParams, Function); |
| 1115 | Function->setDescribedFunctionTemplate(FunctionTemplate); |
John McCall | b1a56e7 | 2010-03-26 23:10:15 +0000 | [diff] [blame] | 1116 | |
| 1117 | FunctionTemplate->setLexicalDeclContext(LexicalDC); |
John McCall | d325daa | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1118 | |
| 1119 | if (isFriend && D->isThisDeclarationADefinition()) { |
| 1120 | // TODO: should we remember this connection regardless of whether |
| 1121 | // the friend declaration provided a body? |
| 1122 | FunctionTemplate->setInstantiatedFromMemberTemplate( |
| 1123 | D->getDescribedFunctionTemplate()); |
| 1124 | } |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1125 | } else if (FunctionTemplate) { |
| 1126 | // Record this function template specialization. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1127 | std::pair<const TemplateArgument *, unsigned> Innermost |
| 1128 | = TemplateArgs.getInnermost(); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1129 | Function->setFunctionTemplateSpecialization(FunctionTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1130 | TemplateArgumentList::CreateCopy(SemaRef.Context, |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1131 | Innermost.first, |
| 1132 | Innermost.second), |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1133 | InsertPos); |
John McCall | d325daa | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1134 | } else if (isFriend && D->isThisDeclarationADefinition()) { |
| 1135 | // TODO: should we remember this connection regardless of whether |
| 1136 | // the friend declaration provided a body? |
| 1137 | Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1138 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1139 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1140 | if (InitFunctionInstantiation(Function, D)) |
| 1141 | Function->setInvalidDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1142 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1143 | bool Redeclaration = false; |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1144 | bool isExplicitSpecialization = false; |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1145 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1146 | LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(), |
| 1147 | Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 1148 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1149 | if (DependentFunctionTemplateSpecializationInfo *Info |
| 1150 | = D->getDependentSpecializationInfo()) { |
| 1151 | assert(isFriend && "non-friend has dependent specialization info?"); |
| 1152 | |
| 1153 | // This needs to be set now for future sanity. |
| 1154 | Function->setObjectOfFriendDecl(/*HasPrevious*/ true); |
| 1155 | |
| 1156 | // Instantiate the explicit template arguments. |
| 1157 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 1158 | Info->getRAngleLoc()); |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1159 | if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), |
| 1160 | ExplicitArgs, TemplateArgs)) |
| 1161 | return 0; |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1162 | |
| 1163 | // Map the candidate templates to their instantiations. |
| 1164 | for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) { |
| 1165 | Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 1166 | Info->getTemplate(I), |
| 1167 | TemplateArgs); |
| 1168 | if (!Temp) return 0; |
| 1169 | |
| 1170 | Previous.addDecl(cast<FunctionTemplateDecl>(Temp)); |
| 1171 | } |
| 1172 | |
| 1173 | if (SemaRef.CheckFunctionTemplateSpecialization(Function, |
| 1174 | &ExplicitArgs, |
| 1175 | Previous)) |
| 1176 | Function->setInvalidDecl(); |
| 1177 | |
| 1178 | isExplicitSpecialization = true; |
| 1179 | |
| 1180 | } else if (TemplateParams || !FunctionTemplate) { |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1181 | // Look only into the namespace where the friend would be declared to |
| 1182 | // find a previous declaration. This is the innermost enclosing namespace, |
| 1183 | // as described in ActOnFriendFunctionDecl. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1184 | SemaRef.LookupQualifiedName(Previous, DC); |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1185 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1186 | // In C++, the previous declaration we find might be a tag type |
| 1187 | // (class or enum). In this case, the new declaration will hide the |
| 1188 | // tag type. Note that this does does not apply if we're declaring a |
| 1189 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1190 | if (Previous.isSingleTagDecl()) |
| 1191 | Previous.clear(); |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1194 | SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous, |
Peter Collingbourne | c80e811 | 2011-01-21 02:08:54 +0000 | [diff] [blame] | 1195 | isExplicitSpecialization, Redeclaration); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1196 | |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1197 | NamedDecl *PrincipalDecl = (TemplateParams |
| 1198 | ? cast<NamedDecl>(FunctionTemplate) |
| 1199 | : Function); |
| 1200 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1201 | // If the original function was part of a friend declaration, |
| 1202 | // inherit its namespace state and add it to the owner. |
John McCall | d325daa | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1203 | if (isFriend) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1204 | NamedDecl *PrevDecl; |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1205 | if (TemplateParams) |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1206 | PrevDecl = FunctionTemplate->getPreviousDeclaration(); |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1207 | else |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1208 | PrevDecl = Function->getPreviousDeclaration(); |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1209 | |
| 1210 | PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0); |
| 1211 | DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false); |
Gabor Greif | ab297ac | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1212 | |
Gabor Greif | 77535df | 2010-08-30 22:25:56 +0000 | [diff] [blame] | 1213 | bool queuedInstantiation = false; |
Gabor Greif | ab297ac | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1214 | |
Douglas Gregor | 238058c | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1215 | if (!SemaRef.getLangOptions().CPlusPlus0x && |
| 1216 | D->isThisDeclarationADefinition()) { |
| 1217 | // Check for a function body. |
| 1218 | const FunctionDecl *Definition = 0; |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1219 | if (Function->isDefined(Definition) && |
Douglas Gregor | 238058c | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1220 | Definition->getTemplateSpecializationKind() == TSK_Undeclared) { |
| 1221 | SemaRef.Diag(Function->getLocation(), diag::err_redefinition) |
| 1222 | << Function->getDeclName(); |
| 1223 | SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition); |
| 1224 | Function->setInvalidDecl(); |
| 1225 | } |
| 1226 | // Check for redefinitions due to other instantiations of this or |
| 1227 | // a similar friend function. |
| 1228 | else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(), |
| 1229 | REnd = Function->redecls_end(); |
| 1230 | R != REnd; ++R) { |
Gabor Greif | 13a8aff | 2010-08-28 15:42:30 +0000 | [diff] [blame] | 1231 | if (*R == Function) |
| 1232 | continue; |
Gabor Greif | ab297ac | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1233 | switch (R->getFriendObjectKind()) { |
| 1234 | case Decl::FOK_None: |
| 1235 | if (!queuedInstantiation && R->isUsed(false)) { |
| 1236 | if (MemberSpecializationInfo *MSInfo |
| 1237 | = Function->getMemberSpecializationInfo()) { |
| 1238 | if (MSInfo->getPointOfInstantiation().isInvalid()) { |
| 1239 | SourceLocation Loc = R->getLocation(); // FIXME |
| 1240 | MSInfo->setPointOfInstantiation(Loc); |
| 1241 | SemaRef.PendingLocalImplicitInstantiations.push_back( |
| 1242 | std::make_pair(Function, Loc)); |
| 1243 | queuedInstantiation = true; |
| 1244 | } |
| 1245 | } |
| 1246 | } |
| 1247 | break; |
| 1248 | default: |
Douglas Gregor | 238058c | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1249 | if (const FunctionDecl *RPattern |
Gabor Greif | 6a557d8 | 2010-08-28 15:46:56 +0000 | [diff] [blame] | 1250 | = R->getTemplateInstantiationPattern()) |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1251 | if (RPattern->isDefined(RPattern)) { |
Douglas Gregor | 238058c | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1252 | SemaRef.Diag(Function->getLocation(), diag::err_redefinition) |
| 1253 | << Function->getDeclName(); |
Gabor Greif | 6a557d8 | 2010-08-28 15:46:56 +0000 | [diff] [blame] | 1254 | SemaRef.Diag(R->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 238058c | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1255 | Function->setInvalidDecl(); |
| 1256 | break; |
| 1257 | } |
| 1258 | } |
| 1259 | } |
| 1260 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1263 | if (Function->isOverloadedOperator() && !DC->isRecord() && |
| 1264 | PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 1265 | PrincipalDecl->setNonMemberOperator(); |
| 1266 | |
Sean Hunt | eb88ae5 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 1267 | assert(!D->isDefaulted() && "only methods should be defaulted"); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1268 | return Function; |
| 1269 | } |
| 1270 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1271 | Decl * |
| 1272 | TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D, |
| 1273 | TemplateParameterList *TemplateParams) { |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1274 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
| 1275 | void *InsertPos = 0; |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1276 | if (FunctionTemplate && !TemplateParams) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1277 | // We are creating a function template specialization from a function |
| 1278 | // template. Check whether there is already a function template |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1279 | // specialization for this particular set of template arguments. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1280 | std::pair<const TemplateArgument *, unsigned> Innermost |
| 1281 | = TemplateArgs.getInnermost(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1282 | |
Argyrios Kyrtzidis | 2c853e4 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1283 | FunctionDecl *SpecFunc |
| 1284 | = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second, |
| 1285 | InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1286 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1287 | // If we already have a function template specialization, return it. |
Argyrios Kyrtzidis | 2c853e4 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1288 | if (SpecFunc) |
| 1289 | return SpecFunc; |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1292 | bool isFriend; |
| 1293 | if (FunctionTemplate) |
| 1294 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1295 | else |
| 1296 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1297 | |
Douglas Gregor | 79c2278 | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1298 | bool MergeWithParentScope = (TemplateParams != 0) || |
| 1299 | !(isa<Decl>(Owner) && |
| 1300 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1301 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 1302 | |
John McCall | 4eab39f | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1303 | // Instantiate enclosing template arguments for friends. |
| 1304 | llvm::SmallVector<TemplateParameterList *, 4> TempParamLists; |
| 1305 | unsigned NumTempParamLists = 0; |
| 1306 | if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) { |
| 1307 | TempParamLists.set_size(NumTempParamLists); |
| 1308 | for (unsigned I = 0; I != NumTempParamLists; ++I) { |
| 1309 | TemplateParameterList *TempParams = D->getTemplateParameterList(I); |
| 1310 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1311 | if (!InstParams) |
| 1312 | return NULL; |
| 1313 | TempParamLists[I] = InstParams; |
| 1314 | } |
| 1315 | } |
| 1316 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 1317 | llvm::SmallVector<ParmVarDecl *, 4> Params; |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1318 | TypeSourceInfo *TInfo = D->getTypeSourceInfo(); |
| 1319 | TInfo = SubstFunctionType(D, Params); |
| 1320 | if (!TInfo) |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1321 | return 0; |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1322 | QualType T = TInfo->getType(); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1323 | |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 1324 | // \brief If the type of this function, after ignoring parentheses, |
| 1325 | // is not *directly* a function type, then we're instantiating a function |
| 1326 | // that was declared via a typedef, e.g., |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 1327 | // |
| 1328 | // typedef int functype(int, int); |
| 1329 | // functype func; |
| 1330 | // |
| 1331 | // In this case, we'll just go instantiate the ParmVarDecls that we |
| 1332 | // synthesized in the method declaration. |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 1333 | if (!isa<FunctionProtoType>(T.IgnoreParens())) { |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 1334 | assert(!Params.size() && "Instantiating type could not yield parameters"); |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 1335 | llvm::SmallVector<QualType, 4> ParamTypes; |
| 1336 | if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(), |
| 1337 | D->getNumParams(), TemplateArgs, ParamTypes, |
| 1338 | &Params)) |
| 1339 | return 0; |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1342 | NestedNameSpecifierLoc QualifierLoc = D->getQualifierLoc(); |
| 1343 | if (QualifierLoc) { |
| 1344 | QualifierLoc = SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1345 | TemplateArgs); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1346 | if (!QualifierLoc) |
| 1347 | return 0; |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | DeclContext *DC = Owner; |
| 1351 | if (isFriend) { |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1352 | if (QualifierLoc) { |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1353 | CXXScopeSpec SS; |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1354 | SS.Adopt(QualifierLoc); |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1355 | DC = SemaRef.computeDeclContext(SS); |
John McCall | c54d688 | 2010-10-19 05:01:53 +0000 | [diff] [blame] | 1356 | |
| 1357 | if (DC && SemaRef.RequireCompleteDeclContext(SS, DC)) |
| 1358 | return 0; |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1359 | } else { |
| 1360 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), |
| 1361 | D->getDeclContext(), |
| 1362 | TemplateArgs); |
| 1363 | } |
| 1364 | if (!DC) return 0; |
| 1365 | } |
| 1366 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1367 | // Build the instantiated method declaration. |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1368 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1369 | CXXMethodDecl *Method = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1370 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1371 | SourceLocation StartLoc = D->getInnerLocStart(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1372 | DeclarationNameInfo NameInfo |
| 1373 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1374 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1375 | Method = CXXConstructorDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1376 | StartLoc, NameInfo, T, TInfo, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | Constructor->isExplicit(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1378 | Constructor->isInlineSpecified(), |
Sean Hunt | 5f802e5 | 2011-05-06 00:11:07 +0000 | [diff] [blame] | 1379 | false); |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1380 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1381 | Method = CXXDestructorDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1382 | StartLoc, NameInfo, T, TInfo, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1383 | Destructor->isInlineSpecified(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1384 | false); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1385 | } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1386 | Method = CXXConversionDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1387 | StartLoc, NameInfo, T, TInfo, |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 1388 | Conversion->isInlineSpecified(), |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1389 | Conversion->isExplicit(), |
| 1390 | Conversion->getLocEnd()); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1391 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1392 | Method = CXXMethodDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1393 | StartLoc, NameInfo, T, TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1394 | D->isStatic(), |
| 1395 | D->getStorageClassAsWritten(), |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1396 | D->isInlineSpecified(), |
| 1397 | D->getLocEnd()); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1398 | } |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1399 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1400 | if (QualifierLoc) |
| 1401 | Method->setQualifierInfo(QualifierLoc); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1402 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1403 | if (TemplateParams) { |
| 1404 | // Our resulting instantiation is actually a function template, since we |
| 1405 | // are substituting only the outer template parameters. For example, given |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1406 | // |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1407 | // template<typename T> |
| 1408 | // struct X { |
| 1409 | // template<typename U> void f(T, U); |
| 1410 | // }; |
| 1411 | // |
| 1412 | // X<int> x; |
| 1413 | // |
| 1414 | // We are instantiating the member template "f" within X<int>, which means |
| 1415 | // substituting int for T, but leaving "f" as a member function template. |
| 1416 | // Build the function template itself. |
| 1417 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, |
| 1418 | Method->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1419 | Method->getDeclName(), |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1420 | TemplateParams, Method); |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1421 | if (isFriend) { |
| 1422 | FunctionTemplate->setLexicalDeclContext(Owner); |
| 1423 | FunctionTemplate->setObjectOfFriendDecl(true); |
| 1424 | } else if (D->isOutOfLine()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1425 | FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1426 | Method->setDescribedFunctionTemplate(FunctionTemplate); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1427 | } else if (FunctionTemplate) { |
| 1428 | // Record this function template specialization. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1429 | std::pair<const TemplateArgument *, unsigned> Innermost |
| 1430 | = TemplateArgs.getInnermost(); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1431 | Method->setFunctionTemplateSpecialization(FunctionTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1432 | TemplateArgumentList::CreateCopy(SemaRef.Context, |
| 1433 | Innermost.first, |
| 1434 | Innermost.second), |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1435 | InsertPos); |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1436 | } else if (!isFriend) { |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1437 | // Record that this is an instantiation of a member function. |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1438 | Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1441 | // If we are instantiating a member function defined |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1442 | // out-of-line, the instantiation will have the same lexical |
| 1443 | // context (which will be a namespace scope) as the template. |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1444 | if (isFriend) { |
John McCall | 4eab39f | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1445 | if (NumTempParamLists) |
| 1446 | Method->setTemplateParameterListsInfo(SemaRef.Context, |
| 1447 | NumTempParamLists, |
| 1448 | TempParamLists.data()); |
| 1449 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1450 | Method->setLexicalDeclContext(Owner); |
| 1451 | Method->setObjectOfFriendDecl(true); |
| 1452 | } else if (D->isOutOfLine()) |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1453 | Method->setLexicalDeclContext(D->getLexicalDeclContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1454 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1455 | // Attach the parameters |
| 1456 | for (unsigned P = 0; P < Params.size(); ++P) |
| 1457 | Params[P]->setOwningFunction(Method); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1458 | Method->setParams(Params.data(), Params.size()); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1459 | |
| 1460 | if (InitMethodInstantiation(Method, D)) |
| 1461 | Method->setInvalidDecl(); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1462 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1463 | LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName, |
| 1464 | Sema::ForRedeclaration); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1465 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1466 | if (!FunctionTemplate || TemplateParams || isFriend) { |
| 1467 | SemaRef.LookupQualifiedName(Previous, Record); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1468 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1469 | // In C++, the previous declaration we find might be a tag type |
| 1470 | // (class or enum). In this case, the new declaration will hide the |
| 1471 | // tag type. Note that this does does not apply if we're declaring a |
| 1472 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1473 | if (Previous.isSingleTagDecl()) |
| 1474 | Previous.clear(); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1475 | } |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1476 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1477 | bool Redeclaration = false; |
Peter Collingbourne | c80e811 | 2011-01-21 02:08:54 +0000 | [diff] [blame] | 1478 | SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1479 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 1480 | if (D->isPure()) |
| 1481 | SemaRef.CheckPureMethod(Method, SourceRange()); |
| 1482 | |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 1483 | Method->setAccess(D->getAccess()); |
| 1484 | |
Anders Carlsson | 9eefa22 | 2011-01-20 06:52:44 +0000 | [diff] [blame] | 1485 | SemaRef.CheckOverrideControl(Method); |
| 1486 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1487 | if (FunctionTemplate) { |
| 1488 | // If there's a function template, let our caller handle it. |
| 1489 | } else if (Method->isInvalidDecl() && !Previous.empty()) { |
| 1490 | // Don't hide a (potentially) valid declaration with an invalid one. |
| 1491 | } else { |
| 1492 | NamedDecl *DeclToAdd = (TemplateParams |
| 1493 | ? cast<NamedDecl>(FunctionTemplate) |
| 1494 | : Method); |
| 1495 | if (isFriend) |
| 1496 | Record->makeDeclVisibleInContext(DeclToAdd); |
| 1497 | else |
| 1498 | Owner->addDecl(DeclToAdd); |
| 1499 | } |
Sean Hunt | eb88ae5 | 2011-05-23 21:07:59 +0000 | [diff] [blame] | 1500 | |
| 1501 | if (D->isExplicitlyDefaulted()) { |
| 1502 | SemaRef.SetDeclDefaulted(Method, Method->getLocation()); |
| 1503 | } else { |
| 1504 | assert(!D->isDefaulted() && |
| 1505 | "should not implicitly default uninstantiated function"); |
| 1506 | } |
| 1507 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1508 | return Method; |
| 1509 | } |
| 1510 | |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 1511 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1512 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 1515 | Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1516 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 1519 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1520 | return VisitCXXMethodDecl(D); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 1523 | ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { |
John McCall | fb44de9 | 2011-05-01 22:35:37 +0000 | [diff] [blame] | 1524 | return SemaRef.SubstParmVarDecl(D, TemplateArgs, /*indexAdjustment*/ 0, |
| 1525 | llvm::Optional<unsigned>()); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1528 | Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( |
| 1529 | TemplateTypeParmDecl *D) { |
| 1530 | // TODO: don't always clone when decls are refcounted. |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 1531 | assert(D->getTypeForDecl()->isTemplateTypeParmType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1533 | TemplateTypeParmDecl *Inst = |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 1534 | TemplateTypeParmDecl::Create(SemaRef.Context, Owner, |
| 1535 | D->getLocStart(), D->getLocation(), |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 1536 | D->getDepth() - TemplateArgs.getNumLevels(), |
| 1537 | D->getIndex(), D->getIdentifier(), |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1538 | D->wasDeclaredWithTypename(), |
| 1539 | D->isParameterPack()); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1540 | Inst->setAccess(AS_public); |
| 1541 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1542 | if (D->hasDefaultArgument()) |
| 1543 | Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1544 | |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1545 | // Introduce this template parameter's instantiation into the instantiation |
| 1546 | // scope. |
| 1547 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 1548 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1549 | return Inst; |
| 1550 | } |
| 1551 | |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1552 | Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl( |
| 1553 | NonTypeTemplateParmDecl *D) { |
| 1554 | // Substitute into the type of the non-type template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1555 | TypeLoc TL = D->getTypeSourceInfo()->getTypeLoc(); |
| 1556 | llvm::SmallVector<TypeSourceInfo *, 4> ExpandedParameterPackTypesAsWritten; |
| 1557 | llvm::SmallVector<QualType, 4> ExpandedParameterPackTypes; |
| 1558 | bool IsExpandedParameterPack = false; |
| 1559 | TypeSourceInfo *DI; |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1560 | QualType T; |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1561 | bool Invalid = false; |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1562 | |
| 1563 | if (D->isExpandedParameterPack()) { |
| 1564 | // The non-type template parameter pack is an already-expanded pack |
| 1565 | // expansion of types. Substitute into each of the expanded types. |
| 1566 | ExpandedParameterPackTypes.reserve(D->getNumExpansionTypes()); |
| 1567 | ExpandedParameterPackTypesAsWritten.reserve(D->getNumExpansionTypes()); |
| 1568 | for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) { |
| 1569 | TypeSourceInfo *NewDI =SemaRef.SubstType(D->getExpansionTypeSourceInfo(I), |
| 1570 | TemplateArgs, |
| 1571 | D->getLocation(), |
| 1572 | D->getDeclName()); |
| 1573 | if (!NewDI) |
| 1574 | return 0; |
| 1575 | |
| 1576 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); |
| 1577 | QualType NewT =SemaRef.CheckNonTypeTemplateParameterType(NewDI->getType(), |
| 1578 | D->getLocation()); |
| 1579 | if (NewT.isNull()) |
| 1580 | return 0; |
| 1581 | ExpandedParameterPackTypes.push_back(NewT); |
| 1582 | } |
| 1583 | |
| 1584 | IsExpandedParameterPack = true; |
| 1585 | DI = D->getTypeSourceInfo(); |
| 1586 | T = DI->getType(); |
| 1587 | } else if (isa<PackExpansionTypeLoc>(TL)) { |
| 1588 | // The non-type template parameter pack's type is a pack expansion of types. |
| 1589 | // Determine whether we need to expand this parameter pack into separate |
| 1590 | // types. |
| 1591 | PackExpansionTypeLoc Expansion = cast<PackExpansionTypeLoc>(TL); |
| 1592 | TypeLoc Pattern = Expansion.getPatternLoc(); |
| 1593 | llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 1594 | SemaRef.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 1595 | |
| 1596 | // Determine whether the set of unexpanded parameter packs can and should |
| 1597 | // be expanded. |
| 1598 | bool Expand = true; |
| 1599 | bool RetainExpansion = false; |
| 1600 | llvm::Optional<unsigned> OrigNumExpansions |
| 1601 | = Expansion.getTypePtr()->getNumExpansions(); |
| 1602 | llvm::Optional<unsigned> NumExpansions = OrigNumExpansions; |
| 1603 | if (SemaRef.CheckParameterPacksForExpansion(Expansion.getEllipsisLoc(), |
| 1604 | Pattern.getSourceRange(), |
| 1605 | Unexpanded.data(), |
| 1606 | Unexpanded.size(), |
| 1607 | TemplateArgs, |
| 1608 | Expand, RetainExpansion, |
| 1609 | NumExpansions)) |
| 1610 | return 0; |
| 1611 | |
| 1612 | if (Expand) { |
| 1613 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
| 1614 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, I); |
| 1615 | TypeSourceInfo *NewDI = SemaRef.SubstType(Pattern, TemplateArgs, |
| 1616 | D->getLocation(), |
| 1617 | D->getDeclName()); |
| 1618 | if (!NewDI) |
| 1619 | return 0; |
| 1620 | |
| 1621 | ExpandedParameterPackTypesAsWritten.push_back(NewDI); |
| 1622 | QualType NewT = SemaRef.CheckNonTypeTemplateParameterType( |
| 1623 | NewDI->getType(), |
| 1624 | D->getLocation()); |
| 1625 | if (NewT.isNull()) |
| 1626 | return 0; |
| 1627 | ExpandedParameterPackTypes.push_back(NewT); |
| 1628 | } |
| 1629 | |
| 1630 | // Note that we have an expanded parameter pack. The "type" of this |
| 1631 | // expanded parameter pack is the original expansion type, but callers |
| 1632 | // will end up using the expanded parameter pack types for type-checking. |
| 1633 | IsExpandedParameterPack = true; |
| 1634 | DI = D->getTypeSourceInfo(); |
| 1635 | T = DI->getType(); |
| 1636 | } else { |
| 1637 | // We cannot fully expand the pack expansion now, so substitute into the |
| 1638 | // pattern and create a new pack expansion type. |
| 1639 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 1640 | TypeSourceInfo *NewPattern = SemaRef.SubstType(Pattern, TemplateArgs, |
| 1641 | D->getLocation(), |
| 1642 | D->getDeclName()); |
| 1643 | if (!NewPattern) |
| 1644 | return 0; |
| 1645 | |
| 1646 | DI = SemaRef.CheckPackExpansion(NewPattern, Expansion.getEllipsisLoc(), |
| 1647 | NumExpansions); |
| 1648 | if (!DI) |
| 1649 | return 0; |
| 1650 | |
| 1651 | T = DI->getType(); |
| 1652 | } |
| 1653 | } else { |
| 1654 | // Simple case: substitution into a parameter that is not a parameter pack. |
| 1655 | DI = SemaRef.SubstType(D->getTypeSourceInfo(), TemplateArgs, |
| 1656 | D->getLocation(), D->getDeclName()); |
| 1657 | if (!DI) |
| 1658 | return 0; |
| 1659 | |
| 1660 | // Check that this type is acceptable for a non-type template parameter. |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1661 | T = SemaRef.CheckNonTypeTemplateParameterType(DI->getType(), |
| 1662 | D->getLocation()); |
| 1663 | if (T.isNull()) { |
| 1664 | T = SemaRef.Context.IntTy; |
| 1665 | Invalid = true; |
| 1666 | } |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1669 | NonTypeTemplateParmDecl *Param; |
| 1670 | if (IsExpandedParameterPack) |
| 1671 | Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1672 | D->getInnerLocStart(), |
| 1673 | D->getLocation(), |
Douglas Gregor | 71b87e4 | 2010-08-30 23:23:59 +0000 | [diff] [blame] | 1674 | D->getDepth() - TemplateArgs.getNumLevels(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1675 | D->getPosition(), |
| 1676 | D->getIdentifier(), T, |
| 1677 | DI, |
| 1678 | ExpandedParameterPackTypes.data(), |
| 1679 | ExpandedParameterPackTypes.size(), |
| 1680 | ExpandedParameterPackTypesAsWritten.data()); |
| 1681 | else |
| 1682 | Param = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1683 | D->getInnerLocStart(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 1684 | D->getLocation(), |
| 1685 | D->getDepth() - TemplateArgs.getNumLevels(), |
| 1686 | D->getPosition(), |
| 1687 | D->getIdentifier(), T, |
| 1688 | D->isParameterPack(), DI); |
| 1689 | |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1690 | Param->setAccess(AS_public); |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1691 | if (Invalid) |
| 1692 | Param->setInvalidDecl(); |
| 1693 | |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1694 | Param->setDefaultArgument(D->getDefaultArgument(), false); |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1695 | |
| 1696 | // Introduce this template parameter's instantiation into the instantiation |
| 1697 | // scope. |
| 1698 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1699 | return Param; |
| 1700 | } |
| 1701 | |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 1702 | Decl * |
Douglas Gregor | 9106ef7 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 1703 | TemplateDeclInstantiator::VisitTemplateTemplateParmDecl( |
| 1704 | TemplateTemplateParmDecl *D) { |
| 1705 | // Instantiate the template parameter list of the template template parameter. |
| 1706 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1707 | TemplateParameterList *InstParams; |
| 1708 | { |
| 1709 | // Perform the actual substitution of template parameters within a new, |
| 1710 | // local instantiation scope. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1711 | LocalInstantiationScope Scope(SemaRef); |
Douglas Gregor | 9106ef7 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 1712 | InstParams = SubstTemplateParams(TempParams); |
| 1713 | if (!InstParams) |
| 1714 | return NULL; |
| 1715 | } |
| 1716 | |
| 1717 | // Build the template template parameter. |
| 1718 | TemplateTemplateParmDecl *Param |
| 1719 | = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
Douglas Gregor | 71b87e4 | 2010-08-30 23:23:59 +0000 | [diff] [blame] | 1720 | D->getDepth() - TemplateArgs.getNumLevels(), |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1721 | D->getPosition(), D->isParameterPack(), |
| 1722 | D->getIdentifier(), InstParams); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1723 | Param->setDefaultArgument(D->getDefaultArgument(), false); |
Douglas Gregor | 9a299e0 | 2011-03-04 17:52:15 +0000 | [diff] [blame] | 1724 | Param->setAccess(AS_public); |
Douglas Gregor | 4469e8a | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 1725 | |
Douglas Gregor | 9106ef7 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 1726 | // Introduce this template parameter's instantiation into the instantiation |
| 1727 | // scope. |
| 1728 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
| 1729 | |
| 1730 | return Param; |
| 1731 | } |
| 1732 | |
Douglas Gregor | 48c32a7 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 1733 | Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 1734 | // Using directives are never dependent (and never contain any types or |
| 1735 | // expressions), so they require no explicit instantiation work. |
Douglas Gregor | 48c32a7 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 1736 | |
| 1737 | UsingDirectiveDecl *Inst |
| 1738 | = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 1739 | D->getNamespaceKeyLocation(), |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 1740 | D->getQualifierLoc(), |
Douglas Gregor | 48c32a7 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 1741 | D->getIdentLocation(), |
| 1742 | D->getNominatedNamespace(), |
| 1743 | D->getCommonAncestor()); |
| 1744 | Owner->addDecl(Inst); |
| 1745 | return Inst; |
| 1746 | } |
| 1747 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1748 | Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) { |
Douglas Gregor | 1b39820 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 1749 | |
| 1750 | // The nested name specifier may be dependent, for example |
| 1751 | // template <typename T> struct t { |
| 1752 | // struct s1 { T f1(); }; |
| 1753 | // struct s2 : s1 { using s1::f1; }; |
| 1754 | // }; |
| 1755 | // template struct t<int>; |
| 1756 | // Here, in using s1::f1, s1 refers to t<T>::s1; |
| 1757 | // we need to substitute for t<int>::s1. |
Douglas Gregor | 5149f37 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 1758 | NestedNameSpecifierLoc QualifierLoc |
| 1759 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), |
| 1760 | TemplateArgs); |
| 1761 | if (!QualifierLoc) |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1762 | return 0; |
Douglas Gregor | 1b39820 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 1763 | |
| 1764 | // The name info is non-dependent, so no transformation |
| 1765 | // is required. |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1766 | DeclarationNameInfo NameInfo = D->getNameInfo(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1767 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1768 | // We only need to do redeclaration lookups if we're in a class |
| 1769 | // scope (in fact, it's not really even possible in non-class |
| 1770 | // scopes). |
| 1771 | bool CheckRedeclaration = Owner->isRecord(); |
| 1772 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1773 | LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName, |
| 1774 | Sema::ForRedeclaration); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1775 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1776 | UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner, |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1777 | D->getUsingLocation(), |
Douglas Gregor | 5149f37 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 1778 | QualifierLoc, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1779 | NameInfo, |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1780 | D->isTypeName()); |
| 1781 | |
Douglas Gregor | 5149f37 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 1782 | CXXScopeSpec SS; |
| 1783 | SS.Adopt(QualifierLoc); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1784 | if (CheckRedeclaration) { |
| 1785 | Prev.setHideTags(false); |
| 1786 | SemaRef.LookupQualifiedName(Prev, Owner); |
| 1787 | |
| 1788 | // Check for invalid redeclarations. |
| 1789 | if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(), |
| 1790 | D->isTypeName(), SS, |
| 1791 | D->getLocation(), Prev)) |
| 1792 | NewUD->setInvalidDecl(); |
| 1793 | |
| 1794 | } |
| 1795 | |
| 1796 | if (!NewUD->isInvalidDecl() && |
| 1797 | SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS, |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1798 | D->getLocation())) |
| 1799 | NewUD->setInvalidDecl(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1800 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1801 | SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D); |
| 1802 | NewUD->setAccess(D->getAccess()); |
| 1803 | Owner->addDecl(NewUD); |
| 1804 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1805 | // Don't process the shadow decls for an invalid decl. |
| 1806 | if (NewUD->isInvalidDecl()) |
| 1807 | return NewUD; |
| 1808 | |
John McCall | 323c310 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 1809 | bool isFunctionScope = Owner->isFunctionOrMethod(); |
| 1810 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1811 | // Process the shadow decls. |
| 1812 | for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end(); |
| 1813 | I != E; ++I) { |
| 1814 | UsingShadowDecl *Shadow = *I; |
| 1815 | NamedDecl *InstTarget = |
Douglas Gregor | b710722 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 1816 | cast_or_null<NamedDecl>(SemaRef.FindInstantiatedDecl( |
| 1817 | Shadow->getLocation(), |
| 1818 | Shadow->getTargetDecl(), |
| 1819 | TemplateArgs)); |
| 1820 | if (!InstTarget) |
| 1821 | return 0; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1822 | |
| 1823 | if (CheckRedeclaration && |
| 1824 | SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev)) |
| 1825 | continue; |
| 1826 | |
| 1827 | UsingShadowDecl *InstShadow |
| 1828 | = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget); |
| 1829 | SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow); |
John McCall | 323c310 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 1830 | |
| 1831 | if (isFunctionScope) |
| 1832 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1833 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1834 | |
| 1835 | return NewUD; |
| 1836 | } |
| 1837 | |
| 1838 | Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1839 | // Ignore these; we handle them in bulk when processing the UsingDecl. |
| 1840 | return 0; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1843 | Decl * TemplateDeclInstantiator |
| 1844 | ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { |
Douglas Gregor | 5149f37 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 1845 | NestedNameSpecifierLoc QualifierLoc |
| 1846 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), |
| 1847 | TemplateArgs); |
| 1848 | if (!QualifierLoc) |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 1849 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1850 | |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 1851 | CXXScopeSpec SS; |
Douglas Gregor | 5149f37 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 1852 | SS.Adopt(QualifierLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1853 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1854 | // Since NameInfo refers to a typename, it cannot be a C++ special name. |
| 1855 | // Hence, no tranformation is required for it. |
| 1856 | DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1857 | NamedDecl *UD = |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 1858 | SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(), |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1859 | D->getUsingLoc(), SS, NameInfo, 0, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1860 | /*instantiation*/ true, |
| 1861 | /*typename*/ true, D->getTypenameLoc()); |
Douglas Gregor | 4469e8a | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 1862 | if (UD) |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1863 | SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D); |
| 1864 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1865 | return UD; |
| 1866 | } |
| 1867 | |
| 1868 | Decl * TemplateDeclInstantiator |
| 1869 | ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
Douglas Gregor | 5149f37 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 1870 | NestedNameSpecifierLoc QualifierLoc |
| 1871 | = SemaRef.SubstNestedNameSpecifierLoc(D->getQualifierLoc(), TemplateArgs); |
| 1872 | if (!QualifierLoc) |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1873 | return 0; |
Douglas Gregor | 5149f37 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 1874 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1875 | CXXScopeSpec SS; |
Douglas Gregor | 5149f37 | 2011-02-25 15:54:31 +0000 | [diff] [blame] | 1876 | SS.Adopt(QualifierLoc); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1877 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1878 | DeclarationNameInfo NameInfo |
| 1879 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
| 1880 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1881 | NamedDecl *UD = |
| 1882 | SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(), |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1883 | D->getUsingLoc(), SS, NameInfo, 0, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1884 | /*instantiation*/ true, |
| 1885 | /*typename*/ false, SourceLocation()); |
Douglas Gregor | 4469e8a | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 1886 | if (UD) |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1887 | SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D); |
| 1888 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1889 | return UD; |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1892 | Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1893 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1894 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
Douglas Gregor | 2fa9800 | 2010-02-16 19:28:15 +0000 | [diff] [blame] | 1895 | if (D->isInvalidDecl()) |
| 1896 | return 0; |
| 1897 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1898 | return Instantiator.Visit(D); |
| 1899 | } |
| 1900 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1901 | /// \brief Instantiates a nested template parameter list in the current |
| 1902 | /// instantiation context. |
| 1903 | /// |
| 1904 | /// \param L The parameter list to instantiate |
| 1905 | /// |
| 1906 | /// \returns NULL if there was an error |
| 1907 | TemplateParameterList * |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1908 | TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1909 | // Get errors for all the parameters before bailing out. |
| 1910 | bool Invalid = false; |
| 1911 | |
| 1912 | unsigned N = L->size(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1913 | typedef llvm::SmallVector<NamedDecl *, 8> ParamVector; |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1914 | ParamVector Params; |
| 1915 | Params.reserve(N); |
| 1916 | for (TemplateParameterList::iterator PI = L->begin(), PE = L->end(); |
| 1917 | PI != PE; ++PI) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1918 | NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI)); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1919 | Params.push_back(D); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 1920 | Invalid = Invalid || !D || D->isInvalidDecl(); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | // Clean up if we had an error. |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 1924 | if (Invalid) |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1925 | return NULL; |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1926 | |
| 1927 | TemplateParameterList *InstL |
| 1928 | = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), |
| 1929 | L->getLAngleLoc(), &Params.front(), N, |
| 1930 | L->getRAngleLoc()); |
| 1931 | return InstL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1932 | } |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1933 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1934 | /// \brief Instantiate the declaration of a class template partial |
| 1935 | /// specialization. |
| 1936 | /// |
| 1937 | /// \param ClassTemplate the (instantiated) class template that is partially |
| 1938 | // specialized by the instantiation of \p PartialSpec. |
| 1939 | /// |
| 1940 | /// \param PartialSpec the (uninstantiated) class template partial |
| 1941 | /// specialization that we are instantiating. |
| 1942 | /// |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1943 | /// \returns The instantiated partial specialization, if successful; otherwise, |
| 1944 | /// NULL to indicate an error. |
| 1945 | ClassTemplatePartialSpecializationDecl * |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1946 | TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization( |
| 1947 | ClassTemplateDecl *ClassTemplate, |
| 1948 | ClassTemplatePartialSpecializationDecl *PartialSpec) { |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1949 | // Create a local instantiation scope for this class template partial |
| 1950 | // specialization, which will contain the instantiations of the template |
| 1951 | // parameters. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1952 | LocalInstantiationScope Scope(SemaRef); |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1953 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1954 | // Substitute into the template parameters of the class template partial |
| 1955 | // specialization. |
| 1956 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); |
| 1957 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1958 | if (!InstParams) |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1959 | return 0; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1960 | |
| 1961 | // Substitute into the template arguments of the class template partial |
| 1962 | // specialization. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1963 | TemplateArgumentListInfo InstTemplateArgs; // no angle locations |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1964 | if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(), |
| 1965 | PartialSpec->getNumTemplateArgsAsWritten(), |
| 1966 | InstTemplateArgs, TemplateArgs)) |
| 1967 | return 0; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1968 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1969 | // Check that the template argument list is well-formed for this |
| 1970 | // class template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1971 | llvm::SmallVector<TemplateArgument, 4> Converted; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1972 | if (SemaRef.CheckTemplateArgumentList(ClassTemplate, |
| 1973 | PartialSpec->getLocation(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1974 | InstTemplateArgs, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1975 | false, |
| 1976 | Converted)) |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1977 | return 0; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1978 | |
| 1979 | // Figure out where to insert this class template partial specialization |
| 1980 | // in the member template's set of class template partial specializations. |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1981 | void *InsertPos = 0; |
| 1982 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1983 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 1984 | Converted.size(), InsertPos); |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1985 | |
| 1986 | // Build the canonical type that describes the converted template |
| 1987 | // arguments of the class template partial specialization. |
| 1988 | QualType CanonType |
| 1989 | = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1990 | Converted.data(), |
| 1991 | Converted.size()); |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1992 | |
| 1993 | // Build the fully-sugared type for this class template |
| 1994 | // specialization as the user wrote in the specialization |
| 1995 | // itself. This means that we'll pretty-print the type retrieved |
| 1996 | // from the specialization's declaration the way that the user |
| 1997 | // actually wrote the specialization, rather than formatting the |
| 1998 | // name based on the "canonical" representation used to store the |
| 1999 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2000 | TypeSourceInfo *WrittenTy |
| 2001 | = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 2002 | TemplateName(ClassTemplate), |
| 2003 | PartialSpec->getLocation(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2004 | InstTemplateArgs, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2005 | CanonType); |
| 2006 | |
| 2007 | if (PrevDecl) { |
| 2008 | // We've already seen a partial specialization with the same template |
| 2009 | // parameters and template arguments. This can happen, for example, when |
| 2010 | // substituting the outer template arguments ends up causing two |
| 2011 | // class template partial specializations of a member class template |
| 2012 | // to have identical forms, e.g., |
| 2013 | // |
| 2014 | // template<typename T, typename U> |
| 2015 | // struct Outer { |
| 2016 | // template<typename X, typename Y> struct Inner; |
| 2017 | // template<typename Y> struct Inner<T, Y>; |
| 2018 | // template<typename Y> struct Inner<U, Y>; |
| 2019 | // }; |
| 2020 | // |
| 2021 | // Outer<int, int> outer; // error: the partial specializations of Inner |
| 2022 | // // have the same signature. |
| 2023 | SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared) |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2024 | << WrittenTy->getType(); |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2025 | SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here) |
| 2026 | << SemaRef.Context.getTypeDeclType(PrevDecl); |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2027 | return 0; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | |
| 2031 | // Create the class template partial specialization declaration. |
| 2032 | ClassTemplatePartialSpecializationDecl *InstPartialSpec |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 2033 | = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, |
| 2034 | PartialSpec->getTagKind(), |
| 2035 | Owner, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2036 | PartialSpec->getLocStart(), |
| 2037 | PartialSpec->getLocation(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2038 | InstParams, |
| 2039 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2040 | Converted.data(), |
| 2041 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2042 | InstTemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2043 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 2044 | 0, |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2045 | ClassTemplate->getNextPartialSpecSequenceNumber()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2046 | // Substitute the nested name specifier, if any. |
| 2047 | if (SubstQualifier(PartialSpec, InstPartialSpec)) |
| 2048 | return 0; |
| 2049 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2050 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); |
Douglas Gregor | 4469e8a | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 2051 | InstPartialSpec->setTypeAsWritten(WrittenTy); |
| 2052 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2053 | // Add this partial specialization to the set of class template partial |
| 2054 | // specializations. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 2055 | ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos); |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 2056 | return InstPartialSpec; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2059 | TypeSourceInfo* |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2060 | TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2061 | llvm::SmallVectorImpl<ParmVarDecl *> &Params) { |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2062 | TypeSourceInfo *OldTInfo = D->getTypeSourceInfo(); |
| 2063 | assert(OldTInfo && "substituting function without type source info"); |
| 2064 | assert(Params.empty() && "parameter vector is non-empty at start"); |
John McCall | 6cd3b9f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 2065 | TypeSourceInfo *NewTInfo |
| 2066 | = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs, |
| 2067 | D->getTypeSpecStartLoc(), |
| 2068 | D->getDeclName()); |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2069 | if (!NewTInfo) |
| 2070 | return 0; |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2071 | |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 2072 | if (NewTInfo != OldTInfo) { |
| 2073 | // Get parameters from the new type info. |
Abramo Bagnara | 140a2bd | 2010-12-13 22:27:55 +0000 | [diff] [blame] | 2074 | TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens(); |
Douglas Gregor | 6920cdc | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 2075 | if (FunctionProtoTypeLoc *OldProtoLoc |
| 2076 | = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) { |
Abramo Bagnara | 140a2bd | 2010-12-13 22:27:55 +0000 | [diff] [blame] | 2077 | TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens(); |
Douglas Gregor | 6920cdc | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 2078 | FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL); |
| 2079 | assert(NewProtoLoc && "Missing prototype?"); |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2080 | unsigned NewIdx = 0, NumNewParams = NewProtoLoc->getNumArgs(); |
| 2081 | for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs(); |
| 2082 | OldIdx != NumOldParams; ++OldIdx) { |
| 2083 | ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx); |
| 2084 | if (!OldParam->isParameterPack() || |
| 2085 | (NewIdx < NumNewParams && |
| 2086 | NewProtoLoc->getArg(NewIdx)->isParameterPack())) { |
| 2087 | // Simple case: normal parameter, or a parameter pack that's |
| 2088 | // instantiated to a (still-dependent) parameter pack. |
| 2089 | ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++); |
| 2090 | Params.push_back(NewParam); |
| 2091 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam, |
| 2092 | NewParam); |
| 2093 | continue; |
| 2094 | } |
| 2095 | |
| 2096 | // Parameter pack: make the instantiation an argument pack. |
| 2097 | SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack( |
| 2098 | OldParam); |
Douglas Gregor | 21371ea | 2011-01-11 03:14:20 +0000 | [diff] [blame] | 2099 | unsigned NumArgumentsInExpansion |
| 2100 | = SemaRef.getNumArgumentsInExpansion(OldParam->getType(), |
| 2101 | TemplateArgs); |
| 2102 | while (NumArgumentsInExpansion--) { |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2103 | ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++); |
| 2104 | Params.push_back(NewParam); |
| 2105 | SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg(OldParam, |
| 2106 | NewParam); |
| 2107 | } |
Douglas Gregor | 6920cdc | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 2108 | } |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 2109 | } |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 2110 | } else { |
| 2111 | // The function type itself was not dependent and therefore no |
| 2112 | // substitution occurred. However, we still need to instantiate |
| 2113 | // the function parameters themselves. |
Abramo Bagnara | 140a2bd | 2010-12-13 22:27:55 +0000 | [diff] [blame] | 2114 | TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens(); |
Douglas Gregor | 6920cdc | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 2115 | if (FunctionProtoTypeLoc *OldProtoLoc |
| 2116 | = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) { |
| 2117 | for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) { |
| 2118 | ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i)); |
| 2119 | if (!Parm) |
| 2120 | return 0; |
| 2121 | Params.push_back(Parm); |
| 2122 | } |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 2123 | } |
| 2124 | } |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 2125 | return NewTInfo; |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2128 | /// \brief Initializes the common fields of an instantiation function |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2129 | /// declaration (New) from the corresponding fields of its template (Tmpl). |
| 2130 | /// |
| 2131 | /// \returns true if there was an error |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2132 | bool |
| 2133 | TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2134 | FunctionDecl *Tmpl) { |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2135 | if (Tmpl->isDeletedAsWritten()) |
| 2136 | New->setDeletedAsWritten(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2137 | |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 2138 | // If we are performing substituting explicitly-specified template arguments |
| 2139 | // or deduced template arguments into a function template and we reach this |
| 2140 | // 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] | 2141 | // to keeping the new function template specialization. We therefore |
| 2142 | // convert the active template instantiation for the function template |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 2143 | // into a template instantiation for this specific function template |
| 2144 | // specialization, which is not a SFINAE context, so that we diagnose any |
| 2145 | // further errors in the declaration itself. |
| 2146 | typedef Sema::ActiveTemplateInstantiation ActiveInstType; |
| 2147 | ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back(); |
| 2148 | if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || |
| 2149 | ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2150 | if (FunctionTemplateDecl *FunTmpl |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 2151 | = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2152 | assert(FunTmpl->getTemplatedDecl() == Tmpl && |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 2153 | "Deduction from the wrong function template?"); |
Daniel Dunbar | bcbb8bd | 2009-07-16 22:10:11 +0000 | [diff] [blame] | 2154 | (void) FunTmpl; |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 2155 | ActiveInst.Kind = ActiveInstType::TemplateInstantiation; |
| 2156 | ActiveInst.Entity = reinterpret_cast<uintptr_t>(New); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 2157 | --SemaRef.NonInstantiationEntries; |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 2158 | } |
| 2159 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2160 | |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 2161 | const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>(); |
| 2162 | assert(Proto && "Function template without prototype?"); |
| 2163 | |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2164 | if (Proto->hasExceptionSpec() || Proto->getNoReturnAttr()) { |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 2165 | // The function has an exception specification or a "noreturn" |
| 2166 | // attribute. Substitute into each of the exception types. |
| 2167 | llvm::SmallVector<QualType, 4> Exceptions; |
| 2168 | for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) { |
| 2169 | // FIXME: Poor location information! |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 2170 | if (const PackExpansionType *PackExpansion |
| 2171 | = Proto->getExceptionType(I)->getAs<PackExpansionType>()) { |
| 2172 | // We have a pack expansion. Instantiate it. |
| 2173 | llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 2174 | SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(), |
| 2175 | Unexpanded); |
| 2176 | assert(!Unexpanded.empty() && |
| 2177 | "Pack expansion without parameter packs?"); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2178 | |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 2179 | bool Expand = false; |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2180 | bool RetainExpansion = false; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2181 | llvm::Optional<unsigned> NumExpansions |
| 2182 | = PackExpansion->getNumExpansions(); |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 2183 | if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(), |
| 2184 | SourceRange(), |
| 2185 | Unexpanded.data(), |
| 2186 | Unexpanded.size(), |
| 2187 | TemplateArgs, |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2188 | Expand, |
| 2189 | RetainExpansion, |
| 2190 | NumExpansions)) |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 2191 | break; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2192 | |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 2193 | if (!Expand) { |
| 2194 | // We can't expand this pack expansion into separate arguments yet; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2195 | // just substitute into the pattern and create a new pack expansion |
| 2196 | // type. |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 2197 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 2198 | QualType T = SemaRef.SubstType(PackExpansion->getPattern(), |
| 2199 | TemplateArgs, |
| 2200 | New->getLocation(), New->getDeclName()); |
| 2201 | if (T.isNull()) |
| 2202 | break; |
| 2203 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2204 | T = SemaRef.Context.getPackExpansionType(T, NumExpansions); |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 2205 | Exceptions.push_back(T); |
| 2206 | continue; |
| 2207 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2208 | |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 2209 | // Substitute into the pack expansion pattern for each template |
| 2210 | bool Invalid = false; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2211 | for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) { |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 2212 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx); |
| 2213 | |
| 2214 | QualType T = SemaRef.SubstType(PackExpansion->getPattern(), |
| 2215 | TemplateArgs, |
| 2216 | New->getLocation(), New->getDeclName()); |
| 2217 | if (T.isNull()) { |
| 2218 | Invalid = true; |
| 2219 | break; |
| 2220 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2221 | |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 2222 | Exceptions.push_back(T); |
| 2223 | } |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2224 | |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 2225 | if (Invalid) |
| 2226 | break; |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2227 | |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 2228 | continue; |
| 2229 | } |
| 2230 | |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 2231 | QualType T |
| 2232 | = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs, |
| 2233 | New->getLocation(), New->getDeclName()); |
| 2234 | if (T.isNull() || |
| 2235 | SemaRef.CheckSpecifiedExceptionType(T, New->getLocation())) |
| 2236 | continue; |
| 2237 | |
| 2238 | Exceptions.push_back(T); |
| 2239 | } |
Sebastian Redl | 56fb926 | 2011-03-14 18:51:50 +0000 | [diff] [blame] | 2240 | Expr *NoexceptExpr = 0; |
| 2241 | if (Expr *OldNoexceptExpr = Proto->getNoexceptExpr()) { |
Douglas Gregor | 3617e19 | 2011-06-01 15:55:51 +0000 | [diff] [blame^] | 2242 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Sema::Unevaluated); |
Sebastian Redl | 56fb926 | 2011-03-14 18:51:50 +0000 | [diff] [blame] | 2243 | ExprResult E = SemaRef.SubstExpr(OldNoexceptExpr, TemplateArgs); |
| 2244 | if (E.isUsable()) |
| 2245 | NoexceptExpr = E.take(); |
| 2246 | } |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 2247 | |
| 2248 | // Rebuild the function type |
| 2249 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2250 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
Sebastian Redl | 60618fa | 2011-03-12 11:50:43 +0000 | [diff] [blame] | 2251 | EPI.ExceptionSpecType = Proto->getExceptionSpecType(); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2252 | EPI.NumExceptions = Exceptions.size(); |
| 2253 | EPI.Exceptions = Exceptions.data(); |
Sebastian Redl | 56fb926 | 2011-03-14 18:51:50 +0000 | [diff] [blame] | 2254 | EPI.NoexceptExpr = NoexceptExpr; |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2255 | EPI.ExtInfo = Proto->getExtInfo(); |
| 2256 | |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 2257 | const FunctionProtoType *NewProto |
| 2258 | = New->getType()->getAs<FunctionProtoType>(); |
| 2259 | assert(NewProto && "Template instantiation without function prototype?"); |
| 2260 | New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(), |
| 2261 | NewProto->arg_type_begin(), |
| 2262 | NewProto->getNumArgs(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2263 | EPI)); |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
John McCall | 1d8d1cc | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 2266 | SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New); |
Douglas Gregor | 7cf84d6 | 2010-06-15 17:05:35 +0000 | [diff] [blame] | 2267 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2268 | return false; |
| 2269 | } |
| 2270 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2271 | /// \brief Initializes common fields of an instantiated method |
| 2272 | /// declaration (New) from the corresponding fields of its template |
| 2273 | /// (Tmpl). |
| 2274 | /// |
| 2275 | /// \returns true if there was an error |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2276 | bool |
| 2277 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2278 | CXXMethodDecl *Tmpl) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2279 | if (InitFunctionInstantiation(New, Tmpl)) |
| 2280 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2281 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2282 | New->setAccess(Tmpl->getAccess()); |
Fariborz Jahanian | e7184df | 2009-12-03 18:44:40 +0000 | [diff] [blame] | 2283 | if (Tmpl->isVirtualAsWritten()) |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 2284 | New->setVirtualAsWritten(true); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2285 | |
| 2286 | // FIXME: attributes |
| 2287 | // FIXME: New needs a pointer to Tmpl |
| 2288 | return false; |
| 2289 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2290 | |
| 2291 | /// \brief Instantiate the definition of the given function from its |
| 2292 | /// template. |
| 2293 | /// |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2294 | /// \param PointOfInstantiation the point at which the instantiation was |
| 2295 | /// required. Note that this is not precisely a "point of instantiation" |
| 2296 | /// for the function, but it's close. |
| 2297 | /// |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2298 | /// \param Function the already-instantiated declaration of a |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2299 | /// function template specialization or member function of a class template |
| 2300 | /// specialization. |
| 2301 | /// |
| 2302 | /// \param Recursive if true, recursively instantiates any functions that |
| 2303 | /// are required by this instantiation. |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2304 | /// |
| 2305 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 2306 | /// instantiation where the body of the function is required. Complain if |
| 2307 | /// there is no such body. |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 2308 | void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2309 | FunctionDecl *Function, |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2310 | bool Recursive, |
| 2311 | bool DefinitionRequired) { |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2312 | if (Function->isInvalidDecl() || Function->isDefined()) |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 2313 | return; |
| 2314 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2315 | // Never instantiate an explicit specialization. |
| 2316 | if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
| 2317 | return; |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 2318 | |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2319 | // Find the function body that we'll be substituting. |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2320 | const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern(); |
Sean Hunt | f996e05 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 2321 | assert(PatternDecl && "instantiating a non-template"); |
| 2322 | |
| 2323 | Stmt *Pattern = PatternDecl->getBody(PatternDecl); |
| 2324 | assert(PatternDecl && "template definition is not a template"); |
| 2325 | if (!Pattern) { |
| 2326 | // Try to find a defaulted definition |
| 2327 | PatternDecl->isDefined(PatternDecl); |
Sean Hunt | dfab854 | 2011-05-25 22:02:25 +0000 | [diff] [blame] | 2328 | } |
Sean Hunt | f996e05 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 2329 | assert(PatternDecl && "template definition is not a template"); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2330 | |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2331 | // Postpone late parsed template instantiations. |
Sean Hunt | f996e05 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 2332 | if (PatternDecl->isLateTemplateParsed() && |
Nick Lewycky | 8a29bc0 | 2011-05-12 03:51:24 +0000 | [diff] [blame] | 2333 | !LateTemplateParser) { |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2334 | PendingInstantiations.push_back( |
| 2335 | std::make_pair(Function, PointOfInstantiation)); |
| 2336 | return; |
| 2337 | } |
| 2338 | |
| 2339 | // Call the LateTemplateParser callback if there a need to late parse |
| 2340 | // a templated function definition. |
Sean Hunt | f996e05 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 2341 | if (!Pattern && PatternDecl->isLateTemplateParsed() && |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2342 | LateTemplateParser) { |
Francois Pichet | 4a47e8d | 2011-04-23 11:52:20 +0000 | [diff] [blame] | 2343 | LateTemplateParser(OpaqueParser, PatternDecl); |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2344 | Pattern = PatternDecl->getBody(PatternDecl); |
| 2345 | } |
| 2346 | |
Sean Hunt | f996e05 | 2011-05-27 20:00:14 +0000 | [diff] [blame] | 2347 | if (!Pattern && !PatternDecl->isDefaulted()) { |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2348 | if (DefinitionRequired) { |
| 2349 | if (Function->getPrimaryTemplate()) |
| 2350 | Diag(PointOfInstantiation, |
| 2351 | diag::err_explicit_instantiation_undefined_func_template) |
| 2352 | << Function->getPrimaryTemplate(); |
| 2353 | else |
| 2354 | Diag(PointOfInstantiation, |
| 2355 | diag::err_explicit_instantiation_undefined_member) |
| 2356 | << 1 << Function->getDeclName() << Function->getDeclContext(); |
| 2357 | |
| 2358 | if (PatternDecl) |
| 2359 | Diag(PatternDecl->getLocation(), |
| 2360 | diag::note_explicit_instantiation_here); |
Douglas Gregor | cfe833b | 2010-05-17 17:57:54 +0000 | [diff] [blame] | 2361 | Function->setInvalidDecl(); |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 2362 | } else if (Function->getTemplateSpecializationKind() |
| 2363 | == TSK_ExplicitInstantiationDefinition) { |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2364 | PendingInstantiations.push_back( |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 2365 | std::make_pair(Function, PointOfInstantiation)); |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2366 | } |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 2367 | |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2368 | return; |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2369 | } |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2370 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2371 | // C++0x [temp.explicit]p9: |
| 2372 | // Except for inline functions, other explicit instantiation declarations |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2373 | // have the effect of suppressing the implicit instantiation of the entity |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2374 | // to which they refer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2375 | if (Function->getTemplateSpecializationKind() |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2376 | == TSK_ExplicitInstantiationDeclaration && |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 2377 | !PatternDecl->isInlined()) |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2378 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2379 | |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 2380 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); |
| 2381 | if (Inst) |
Douglas Gregor | e7089b0 | 2010-05-03 23:29:10 +0000 | [diff] [blame] | 2382 | return; |
| 2383 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2384 | // If we're performing recursive template instantiation, create our own |
| 2385 | // queue of pending implicit instantiations that we will instantiate later, |
| 2386 | // while we're still within our own instantiation context. |
Nick Lewycky | 2a5f99e | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 2387 | llvm::SmallVector<VTableUse, 16> SavedVTableUses; |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2388 | std::deque<PendingImplicitInstantiation> SavedPendingInstantiations; |
Nick Lewycky | 2a5f99e | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 2389 | if (Recursive) { |
| 2390 | VTableUses.swap(SavedVTableUses); |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2391 | PendingInstantiations.swap(SavedPendingInstantiations); |
Nick Lewycky | 2a5f99e | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 2392 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2393 | |
Douglas Gregor | 9679caf | 2010-05-12 17:27:19 +0000 | [diff] [blame] | 2394 | EnterExpressionEvaluationContext EvalContext(*this, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2395 | Sema::PotentiallyEvaluated); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2396 | ActOnStartOfFunctionDef(0, Function); |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 2397 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 2398 | // Introduce a new scope where local variable instantiations will be |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 2399 | // recorded, unless we're actually a member function within a local |
| 2400 | // class, in which case we need to merge our results with the parent |
| 2401 | // scope (of the enclosing function). |
| 2402 | bool MergeWithParentScope = false; |
| 2403 | if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext())) |
| 2404 | MergeWithParentScope = Rec->isLocalClass(); |
| 2405 | |
| 2406 | LocalInstantiationScope Scope(*this, MergeWithParentScope); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2407 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 2408 | // Introduce the instantiated function parameters into the local |
Peter Collingbourne | 8a6c0f1 | 2010-07-18 16:45:46 +0000 | [diff] [blame] | 2409 | // instantiation scope, and set the parameter names to those used |
| 2410 | // in the template. |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2411 | unsigned FParamIdx = 0; |
Peter Collingbourne | 8a6c0f1 | 2010-07-18 16:45:46 +0000 | [diff] [blame] | 2412 | for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) { |
| 2413 | const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I); |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2414 | if (!PatternParam->isParameterPack()) { |
| 2415 | // Simple case: not a parameter pack. |
| 2416 | assert(FParamIdx < Function->getNumParams()); |
| 2417 | ParmVarDecl *FunctionParam = Function->getParamDecl(I); |
| 2418 | FunctionParam->setDeclName(PatternParam->getDeclName()); |
| 2419 | Scope.InstantiatedLocal(PatternParam, FunctionParam); |
| 2420 | ++FParamIdx; |
| 2421 | continue; |
| 2422 | } |
| 2423 | |
| 2424 | // Expand the parameter pack. |
| 2425 | Scope.MakeInstantiatedLocalArgPack(PatternParam); |
| 2426 | for (unsigned NumFParams = Function->getNumParams(); |
| 2427 | FParamIdx < NumFParams; |
| 2428 | ++FParamIdx) { |
| 2429 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); |
| 2430 | FunctionParam->setDeclName(PatternParam->getDeclName()); |
| 2431 | Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam); |
| 2432 | } |
Peter Collingbourne | 8a6c0f1 | 2010-07-18 16:45:46 +0000 | [diff] [blame] | 2433 | } |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 2434 | |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 2435 | // Enter the scope of this instantiation. We don't use |
| 2436 | // PushDeclContext because we don't have a scope. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 2437 | Sema::ContextRAII savedContext(*this, Function); |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 2438 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2439 | MultiLevelTemplateArgumentList TemplateArgs = |
Douglas Gregor | e7089b0 | 2010-05-03 23:29:10 +0000 | [diff] [blame] | 2440 | getTemplateInstantiationArgs(Function, 0, false, PatternDecl); |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2441 | |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 2442 | if (PatternDecl->isDefaulted()) { |
| 2443 | ActOnFinishFunctionBody(Function, 0, /*IsInstantiation=*/true); |
| 2444 | |
| 2445 | SetDeclDefaulted(Function, PatternDecl->getLocation()); |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 2446 | } else { |
| 2447 | // If this is a constructor, instantiate the member initializers. |
| 2448 | if (const CXXConstructorDecl *Ctor = |
| 2449 | dyn_cast<CXXConstructorDecl>(PatternDecl)) { |
| 2450 | InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor, |
| 2451 | TemplateArgs); |
| 2452 | } |
| 2453 | |
| 2454 | // Instantiate the function body. |
| 2455 | StmtResult Body = SubstStmt(Pattern, TemplateArgs); |
| 2456 | |
| 2457 | if (Body.isInvalid()) |
| 2458 | Function->setInvalidDecl(); |
| 2459 | |
| 2460 | ActOnFinishFunctionBody(Function, Body.get(), |
| 2461 | /*IsInstantiation=*/true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 2464 | PerformDependentDiagnostics(PatternDecl, TemplateArgs); |
| 2465 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 2466 | savedContext.pop(); |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 2467 | |
| 2468 | DeclGroupRef DG(Function); |
| 2469 | Consumer.HandleTopLevelDecl(DG); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2470 | |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 2471 | // This class may have local implicit instantiations that need to be |
| 2472 | // instantiation within this scope. |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2473 | PerformPendingInstantiations(/*LocalOnly=*/true); |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 2474 | Scope.Exit(); |
| 2475 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2476 | if (Recursive) { |
Nick Lewycky | 2a5f99e | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 2477 | // Define any pending vtables. |
| 2478 | DefineUsedVTables(); |
| 2479 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2480 | // Instantiate any pending implicit instantiations found during the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2481 | // instantiation of this template. |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2482 | PerformPendingInstantiations(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2483 | |
Nick Lewycky | 2a5f99e | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 2484 | // Restore the set of pending vtables. |
Nick Lewycky | 8155910 | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 2485 | assert(VTableUses.empty() && |
| 2486 | "VTableUses should be empty before it is discarded."); |
Nick Lewycky | 2a5f99e | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 2487 | VTableUses.swap(SavedVTableUses); |
| 2488 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2489 | // Restore the set of pending implicit instantiations. |
Nick Lewycky | 8155910 | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 2490 | assert(PendingInstantiations.empty() && |
| 2491 | "PendingInstantiations should be empty before it is discarded."); |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2492 | PendingInstantiations.swap(SavedPendingInstantiations); |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2493 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
| 2496 | /// \brief Instantiate the definition of the given variable from its |
| 2497 | /// template. |
| 2498 | /// |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2499 | /// \param PointOfInstantiation the point at which the instantiation was |
| 2500 | /// required. Note that this is not precisely a "point of instantiation" |
| 2501 | /// for the function, but it's close. |
| 2502 | /// |
| 2503 | /// \param Var the already-instantiated declaration of a static member |
| 2504 | /// variable of a class template specialization. |
| 2505 | /// |
| 2506 | /// \param Recursive if true, recursively instantiates any functions that |
| 2507 | /// are required by this instantiation. |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2508 | /// |
| 2509 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 2510 | /// instantiation where an out-of-line definition of the member variable |
| 2511 | /// is required. Complain if there is no such definition. |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2512 | void Sema::InstantiateStaticDataMemberDefinition( |
| 2513 | SourceLocation PointOfInstantiation, |
| 2514 | VarDecl *Var, |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2515 | bool Recursive, |
| 2516 | bool DefinitionRequired) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2517 | if (Var->isInvalidDecl()) |
| 2518 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2519 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2520 | // Find the out-of-line definition of this static data member. |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2521 | VarDecl *Def = Var->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2522 | assert(Def && "This data member was not instantiated from a template?"); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2523 | assert(Def->isStaticDataMember() && "Not a static data member?"); |
| 2524 | Def = Def->getOutOfLineDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2525 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2526 | if (!Def) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2527 | // We did not find an out-of-line definition of this static data member, |
| 2528 | // 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] | 2529 | // instantiate this definition (or provide a specialization for it) in |
| 2530 | // another translation unit. |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2531 | if (DefinitionRequired) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2532 | Def = Var->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2533 | Diag(PointOfInstantiation, |
| 2534 | diag::err_explicit_instantiation_undefined_member) |
| 2535 | << 2 << Var->getDeclName() << Var->getDeclContext(); |
| 2536 | Diag(Def->getLocation(), diag::note_explicit_instantiation_here); |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 2537 | } else if (Var->getTemplateSpecializationKind() |
| 2538 | == TSK_ExplicitInstantiationDefinition) { |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2539 | PendingInstantiations.push_back( |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 2540 | std::make_pair(Var, PointOfInstantiation)); |
| 2541 | } |
| 2542 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2543 | return; |
| 2544 | } |
| 2545 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2546 | // Never instantiate an explicit specialization. |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2547 | if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2548 | return; |
| 2549 | |
| 2550 | // C++0x [temp.explicit]p9: |
| 2551 | // Except for inline functions, other explicit instantiation declarations |
| 2552 | // have the effect of suppressing the implicit instantiation of the entity |
| 2553 | // to which they refer. |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2554 | if (Var->getTemplateSpecializationKind() |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2555 | == TSK_ExplicitInstantiationDeclaration) |
| 2556 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2557 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2558 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
| 2559 | if (Inst) |
| 2560 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2561 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2562 | // If we're performing recursive template instantiation, create our own |
| 2563 | // queue of pending implicit instantiations that we will instantiate later, |
| 2564 | // while we're still within our own instantiation context. |
Nick Lewycky | 8155910 | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 2565 | llvm::SmallVector<VTableUse, 16> SavedVTableUses; |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2566 | std::deque<PendingImplicitInstantiation> SavedPendingInstantiations; |
Nick Lewycky | 8155910 | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 2567 | if (Recursive) { |
| 2568 | VTableUses.swap(SavedVTableUses); |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2569 | PendingInstantiations.swap(SavedPendingInstantiations); |
Nick Lewycky | 8155910 | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 2570 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2571 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2572 | // Enter the scope of this instantiation. We don't use |
| 2573 | // PushDeclContext because we don't have a scope. |
John McCall | f5ba7e0 | 2011-02-14 20:37:25 +0000 | [diff] [blame] | 2574 | ContextRAII previousContext(*this, Var->getDeclContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2575 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2576 | VarDecl *OldVar = Var; |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2577 | Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), |
Nico Weber | 6bb4dcb | 2010-11-28 22:53:37 +0000 | [diff] [blame] | 2578 | getTemplateInstantiationArgs(Var))); |
John McCall | f5ba7e0 | 2011-02-14 20:37:25 +0000 | [diff] [blame] | 2579 | |
| 2580 | previousContext.pop(); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2581 | |
| 2582 | if (Var) { |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 2583 | MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo(); |
| 2584 | assert(MSInfo && "Missing member specialization information?"); |
| 2585 | Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(), |
| 2586 | MSInfo->getPointOfInstantiation()); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2587 | DeclGroupRef DG(Var); |
| 2588 | Consumer.HandleTopLevelDecl(DG); |
| 2589 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2590 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2591 | if (Recursive) { |
Nick Lewycky | 8155910 | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 2592 | // Define any newly required vtables. |
| 2593 | DefineUsedVTables(); |
| 2594 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2595 | // Instantiate any pending implicit instantiations found during the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2596 | // instantiation of this template. |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2597 | PerformPendingInstantiations(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2598 | |
Nick Lewycky | 8155910 | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 2599 | // Restore the set of pending vtables. |
| 2600 | assert(VTableUses.empty() && |
| 2601 | "VTableUses should be empty before it is discarded, " |
| 2602 | "while instantiating static data member."); |
| 2603 | VTableUses.swap(SavedVTableUses); |
| 2604 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2605 | // Restore the set of pending implicit instantiations. |
Nick Lewycky | 8155910 | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 2606 | assert(PendingInstantiations.empty() && |
| 2607 | "PendingInstantiations should be empty before it is discarded, " |
| 2608 | "while instantiating static data member."); |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2609 | PendingInstantiations.swap(SavedPendingInstantiations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2610 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2611 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2612 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2613 | void |
| 2614 | Sema::InstantiateMemInitializers(CXXConstructorDecl *New, |
| 2615 | const CXXConstructorDecl *Tmpl, |
| 2616 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2617 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2618 | llvm::SmallVector<MemInitTy*, 4> NewInits; |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2619 | bool AnyErrors = false; |
| 2620 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2621 | // Instantiate all the initializers. |
| 2622 | for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(), |
Douglas Gregor | 72f6d67 | 2009-09-01 21:04:42 +0000 | [diff] [blame] | 2623 | InitsEnd = Tmpl->init_end(); |
| 2624 | Inits != InitsEnd; ++Inits) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2625 | CXXCtorInitializer *Init = *Inits; |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2626 | |
Chandler Carruth | 030ef47 | 2010-09-03 21:54:20 +0000 | [diff] [blame] | 2627 | // Only instantiate written initializers, let Sema re-construct implicit |
| 2628 | // ones. |
| 2629 | if (!Init->isWritten()) |
| 2630 | continue; |
| 2631 | |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 2632 | SourceLocation LParenLoc, RParenLoc; |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2633 | ASTOwningVector<Expr*> NewArgs(*this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2634 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2635 | SourceLocation EllipsisLoc; |
| 2636 | |
| 2637 | if (Init->isPackExpansion()) { |
| 2638 | // This is a pack expansion. We should expand it now. |
| 2639 | TypeLoc BaseTL = Init->getBaseClassInfo()->getTypeLoc(); |
| 2640 | llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 2641 | collectUnexpandedParameterPacks(BaseTL, Unexpanded); |
| 2642 | bool ShouldExpand = false; |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2643 | bool RetainExpansion = false; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2644 | llvm::Optional<unsigned> NumExpansions; |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2645 | if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(), |
| 2646 | BaseTL.getSourceRange(), |
| 2647 | Unexpanded.data(), |
| 2648 | Unexpanded.size(), |
| 2649 | TemplateArgs, ShouldExpand, |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2650 | RetainExpansion, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2651 | NumExpansions)) { |
| 2652 | AnyErrors = true; |
| 2653 | New->setInvalidDecl(); |
| 2654 | continue; |
| 2655 | } |
| 2656 | assert(ShouldExpand && "Partial instantiation of base initializer?"); |
| 2657 | |
| 2658 | // Loop over all of the arguments in the argument pack(s), |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2659 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2660 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I); |
| 2661 | |
| 2662 | // Instantiate the initializer. |
| 2663 | if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs, |
| 2664 | LParenLoc, NewArgs, RParenLoc)) { |
| 2665 | AnyErrors = true; |
| 2666 | break; |
| 2667 | } |
| 2668 | |
| 2669 | // Instantiate the base type. |
| 2670 | TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(), |
| 2671 | TemplateArgs, |
| 2672 | Init->getSourceLocation(), |
| 2673 | New->getDeclName()); |
| 2674 | if (!BaseTInfo) { |
| 2675 | AnyErrors = true; |
| 2676 | break; |
| 2677 | } |
| 2678 | |
| 2679 | // Build the initializer. |
| 2680 | MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(), |
| 2681 | BaseTInfo, |
| 2682 | (Expr **)NewArgs.data(), |
| 2683 | NewArgs.size(), |
| 2684 | Init->getLParenLoc(), |
| 2685 | Init->getRParenLoc(), |
| 2686 | New->getParent(), |
| 2687 | SourceLocation()); |
| 2688 | if (NewInit.isInvalid()) { |
| 2689 | AnyErrors = true; |
| 2690 | break; |
| 2691 | } |
| 2692 | |
| 2693 | NewInits.push_back(NewInit.get()); |
| 2694 | NewArgs.clear(); |
| 2695 | } |
| 2696 | |
| 2697 | continue; |
| 2698 | } |
| 2699 | |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 2700 | // Instantiate the initializer. |
| 2701 | if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 2702 | LParenLoc, NewArgs, RParenLoc)) { |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 2703 | AnyErrors = true; |
| 2704 | continue; |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2705 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2706 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2707 | MemInitResult NewInit; |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2708 | if (Init->isBaseInitializer()) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2709 | TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2710 | TemplateArgs, |
| 2711 | Init->getSourceLocation(), |
| 2712 | New->getDeclName()); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2713 | if (!BaseTInfo) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2714 | AnyErrors = true; |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2715 | New->setInvalidDecl(); |
| 2716 | continue; |
| 2717 | } |
| 2718 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2719 | NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2720 | (Expr **)NewArgs.data(), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2721 | NewArgs.size(), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2722 | Init->getLParenLoc(), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2723 | Init->getRParenLoc(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2724 | New->getParent(), |
| 2725 | EllipsisLoc); |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2726 | } else if (Init->isMemberInitializer()) { |
Douglas Gregor | b710722 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 2727 | FieldDecl *Member = cast_or_null<FieldDecl>(FindInstantiatedDecl( |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2728 | Init->getMemberLocation(), |
| 2729 | Init->getMember(), |
| 2730 | TemplateArgs)); |
Douglas Gregor | b710722 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 2731 | if (!Member) { |
| 2732 | AnyErrors = true; |
| 2733 | New->setInvalidDecl(); |
| 2734 | continue; |
| 2735 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2736 | |
| 2737 | NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2738 | NewArgs.size(), |
| 2739 | Init->getSourceLocation(), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2740 | Init->getLParenLoc(), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2741 | Init->getRParenLoc()); |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2742 | } else if (Init->isIndirectMemberInitializer()) { |
| 2743 | IndirectFieldDecl *IndirectMember = |
Douglas Gregor | b710722 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 2744 | cast_or_null<IndirectFieldDecl>(FindInstantiatedDecl( |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2745 | Init->getMemberLocation(), |
| 2746 | Init->getIndirectMember(), TemplateArgs)); |
| 2747 | |
Douglas Gregor | b710722 | 2011-03-04 19:46:35 +0000 | [diff] [blame] | 2748 | if (!IndirectMember) { |
| 2749 | AnyErrors = true; |
| 2750 | New->setInvalidDecl(); |
| 2751 | continue; |
| 2752 | } |
| 2753 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2754 | NewInit = BuildMemberInitializer(IndirectMember, (Expr **)NewArgs.data(), |
| 2755 | NewArgs.size(), |
| 2756 | Init->getSourceLocation(), |
| 2757 | Init->getLParenLoc(), |
| 2758 | Init->getRParenLoc()); |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2759 | } |
| 2760 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2761 | if (NewInit.isInvalid()) { |
| 2762 | AnyErrors = true; |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2763 | New->setInvalidDecl(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2764 | } else { |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2765 | // FIXME: It would be nice if ASTOwningVector had a release function. |
| 2766 | NewArgs.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2767 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2768 | NewInits.push_back((MemInitTy *)NewInit.get()); |
| 2769 | } |
| 2770 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2771 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2772 | // Assign all the initializers to the new constructor. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2773 | ActOnMemInitializers(New, |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2774 | /*FIXME: ColonLoc */ |
| 2775 | SourceLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2776 | NewInits.data(), NewInits.size(), |
| 2777 | AnyErrors); |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2780 | // TODO: this could be templated if the various decl types used the |
| 2781 | // same method name. |
| 2782 | static bool isInstantiationOf(ClassTemplateDecl *Pattern, |
| 2783 | ClassTemplateDecl *Instance) { |
| 2784 | Pattern = Pattern->getCanonicalDecl(); |
| 2785 | |
| 2786 | do { |
| 2787 | Instance = Instance->getCanonicalDecl(); |
| 2788 | if (Pattern == Instance) return true; |
| 2789 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 2790 | } while (Instance); |
| 2791 | |
| 2792 | return false; |
| 2793 | } |
| 2794 | |
Douglas Gregor | 0d69653 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 2795 | static bool isInstantiationOf(FunctionTemplateDecl *Pattern, |
| 2796 | FunctionTemplateDecl *Instance) { |
| 2797 | Pattern = Pattern->getCanonicalDecl(); |
| 2798 | |
| 2799 | do { |
| 2800 | Instance = Instance->getCanonicalDecl(); |
| 2801 | if (Pattern == Instance) return true; |
| 2802 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 2803 | } while (Instance); |
| 2804 | |
| 2805 | return false; |
| 2806 | } |
| 2807 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2808 | static bool |
| 2809 | isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern, |
| 2810 | ClassTemplatePartialSpecializationDecl *Instance) { |
| 2811 | Pattern |
| 2812 | = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl()); |
| 2813 | do { |
| 2814 | Instance = cast<ClassTemplatePartialSpecializationDecl>( |
| 2815 | Instance->getCanonicalDecl()); |
| 2816 | if (Pattern == Instance) |
| 2817 | return true; |
| 2818 | Instance = Instance->getInstantiatedFromMember(); |
| 2819 | } while (Instance); |
| 2820 | |
| 2821 | return false; |
| 2822 | } |
| 2823 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2824 | static bool isInstantiationOf(CXXRecordDecl *Pattern, |
| 2825 | CXXRecordDecl *Instance) { |
| 2826 | Pattern = Pattern->getCanonicalDecl(); |
| 2827 | |
| 2828 | do { |
| 2829 | Instance = Instance->getCanonicalDecl(); |
| 2830 | if (Pattern == Instance) return true; |
| 2831 | Instance = Instance->getInstantiatedFromMemberClass(); |
| 2832 | } while (Instance); |
| 2833 | |
| 2834 | return false; |
| 2835 | } |
| 2836 | |
| 2837 | static bool isInstantiationOf(FunctionDecl *Pattern, |
| 2838 | FunctionDecl *Instance) { |
| 2839 | Pattern = Pattern->getCanonicalDecl(); |
| 2840 | |
| 2841 | do { |
| 2842 | Instance = Instance->getCanonicalDecl(); |
| 2843 | if (Pattern == Instance) return true; |
| 2844 | Instance = Instance->getInstantiatedFromMemberFunction(); |
| 2845 | } while (Instance); |
| 2846 | |
| 2847 | return false; |
| 2848 | } |
| 2849 | |
| 2850 | static bool isInstantiationOf(EnumDecl *Pattern, |
| 2851 | EnumDecl *Instance) { |
| 2852 | Pattern = Pattern->getCanonicalDecl(); |
| 2853 | |
| 2854 | do { |
| 2855 | Instance = Instance->getCanonicalDecl(); |
| 2856 | if (Pattern == Instance) return true; |
| 2857 | Instance = Instance->getInstantiatedFromMemberEnum(); |
| 2858 | } while (Instance); |
| 2859 | |
| 2860 | return false; |
| 2861 | } |
| 2862 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2863 | static bool isInstantiationOf(UsingShadowDecl *Pattern, |
| 2864 | UsingShadowDecl *Instance, |
| 2865 | ASTContext &C) { |
| 2866 | return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern; |
| 2867 | } |
| 2868 | |
| 2869 | static bool isInstantiationOf(UsingDecl *Pattern, |
| 2870 | UsingDecl *Instance, |
| 2871 | ASTContext &C) { |
| 2872 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; |
| 2873 | } |
| 2874 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2875 | static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern, |
| 2876 | UsingDecl *Instance, |
| 2877 | ASTContext &C) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2878 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern, |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 2882 | UsingDecl *Instance, |
| 2883 | ASTContext &C) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2884 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2887 | static bool isInstantiationOfStaticDataMember(VarDecl *Pattern, |
| 2888 | VarDecl *Instance) { |
| 2889 | assert(Instance->isStaticDataMember()); |
| 2890 | |
| 2891 | Pattern = Pattern->getCanonicalDecl(); |
| 2892 | |
| 2893 | do { |
| 2894 | Instance = Instance->getCanonicalDecl(); |
| 2895 | if (Pattern == Instance) return true; |
| 2896 | Instance = Instance->getInstantiatedFromStaticDataMember(); |
| 2897 | } while (Instance); |
| 2898 | |
| 2899 | return false; |
| 2900 | } |
| 2901 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2902 | // Other is the prospective instantiation |
| 2903 | // D is the prospective pattern |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2904 | static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 2905 | if (D->getKind() != Other->getKind()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2906 | if (UnresolvedUsingTypenameDecl *UUD |
| 2907 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 2908 | if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { |
| 2909 | return isInstantiationOf(UUD, UD, Ctx); |
| 2910 | } |
| 2911 | } |
| 2912 | |
| 2913 | if (UnresolvedUsingValueDecl *UUD |
| 2914 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 2915 | if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { |
| 2916 | return isInstantiationOf(UUD, UD, Ctx); |
| 2917 | } |
| 2918 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2919 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 2920 | return false; |
| 2921 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2922 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2923 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other)) |
| 2924 | return isInstantiationOf(cast<CXXRecordDecl>(D), Record); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2925 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2926 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other)) |
| 2927 | return isInstantiationOf(cast<FunctionDecl>(D), Function); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2928 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2929 | if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other)) |
| 2930 | return isInstantiationOf(cast<EnumDecl>(D), Enum); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2931 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2932 | if (VarDecl *Var = dyn_cast<VarDecl>(Other)) |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2933 | if (Var->isStaticDataMember()) |
| 2934 | return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var); |
| 2935 | |
| 2936 | if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other)) |
| 2937 | return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp); |
Douglas Gregor | a5bf7f1 | 2009-08-28 22:03:51 +0000 | [diff] [blame] | 2938 | |
Douglas Gregor | 0d69653 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 2939 | if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other)) |
| 2940 | return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp); |
| 2941 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2942 | if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 2943 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other)) |
| 2944 | return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D), |
| 2945 | PartialSpec); |
| 2946 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 2947 | if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) { |
| 2948 | if (!Field->getDeclName()) { |
| 2949 | // This is an unnamed field. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2950 | return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) == |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 2951 | cast<FieldDecl>(D); |
| 2952 | } |
| 2953 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2954 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2955 | if (UsingDecl *Using = dyn_cast<UsingDecl>(Other)) |
| 2956 | return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx); |
| 2957 | |
| 2958 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other)) |
| 2959 | return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx); |
| 2960 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2961 | return D->getDeclName() && isa<NamedDecl>(Other) && |
| 2962 | D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); |
| 2963 | } |
| 2964 | |
| 2965 | template<typename ForwardIterator> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2966 | static NamedDecl *findInstantiationOf(ASTContext &Ctx, |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2967 | NamedDecl *D, |
| 2968 | ForwardIterator first, |
| 2969 | ForwardIterator last) { |
| 2970 | for (; first != last; ++first) |
| 2971 | if (isInstantiationOf(Ctx, D, *first)) |
| 2972 | return cast<NamedDecl>(*first); |
| 2973 | |
| 2974 | return 0; |
| 2975 | } |
| 2976 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 2977 | /// \brief Finds the instantiation of the given declaration context |
| 2978 | /// within the current instantiation. |
| 2979 | /// |
| 2980 | /// \returns NULL if there was an error |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 2981 | DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC, |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2982 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 2983 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) { |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 2984 | Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 2985 | return cast_or_null<DeclContext>(ID); |
| 2986 | } else return DC; |
| 2987 | } |
| 2988 | |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 2989 | /// \brief Find the instantiation of the given declaration within the |
| 2990 | /// current instantiation. |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2991 | /// |
| 2992 | /// This routine is intended to be used when \p D is a declaration |
| 2993 | /// referenced from within a template, that needs to mapped into the |
| 2994 | /// corresponding declaration within an instantiation. For example, |
| 2995 | /// given: |
| 2996 | /// |
| 2997 | /// \code |
| 2998 | /// template<typename T> |
| 2999 | /// struct X { |
| 3000 | /// enum Kind { |
| 3001 | /// KnownValue = sizeof(T) |
| 3002 | /// }; |
| 3003 | /// |
| 3004 | /// bool getKind() const { return KnownValue; } |
| 3005 | /// }; |
| 3006 | /// |
| 3007 | /// template struct X<int>; |
| 3008 | /// \endcode |
| 3009 | /// |
| 3010 | /// In the instantiation of X<int>::getKind(), we need to map the |
| 3011 | /// EnumConstantDecl for KnownValue (which refers to |
| 3012 | /// X<T>::<Kind>::KnownValue) to its instantiation |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 3013 | /// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs |
| 3014 | /// this mapping from within the instantiation of X<int>. |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 3015 | NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D, |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 3016 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3017 | DeclContext *ParentDC = D->getDeclContext(); |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 3018 | if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) || |
Douglas Gregor | 6d3e627 | 2010-02-05 19:54:12 +0000 | [diff] [blame] | 3019 | isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) || |
John McCall | 7667245 | 2010-08-19 23:06:02 +0000 | [diff] [blame] | 3020 | (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) { |
Douglas Gregor | 2bba76b | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 3021 | // D is a local of some kind. Look into the map of local |
| 3022 | // declarations to their instantiations. |
Chris Lattner | d8e5499 | 2011-02-17 19:47:42 +0000 | [diff] [blame] | 3023 | typedef LocalInstantiationScope::DeclArgumentPack DeclArgumentPack; |
| 3024 | llvm::PointerUnion<Decl *, DeclArgumentPack *> *Found |
| 3025 | = CurrentInstantiationScope->findInstantiationOf(D); |
Chris Lattner | d8e5499 | 2011-02-17 19:47:42 +0000 | [diff] [blame] | 3026 | |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 3027 | if (Found) { |
| 3028 | if (Decl *FD = Found->dyn_cast<Decl *>()) |
| 3029 | return cast<NamedDecl>(FD); |
| 3030 | |
| 3031 | unsigned PackIdx = ArgumentPackSubstitutionIndex; |
| 3032 | return cast<NamedDecl>((*Found->get<DeclArgumentPack *>())[PackIdx]); |
| 3033 | } |
| 3034 | |
| 3035 | // If we didn't find the decl, then we must have a label decl that hasn't |
| 3036 | // been found yet. Lazily instantiate it and return it now. |
| 3037 | assert(isa<LabelDecl>(D)); |
Chris Lattner | d8e5499 | 2011-02-17 19:47:42 +0000 | [diff] [blame] | 3038 | |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 3039 | Decl *Inst = SubstDecl(D, CurContext, TemplateArgs); |
| 3040 | assert(Inst && "Failed to instantiate label??"); |
| 3041 | |
| 3042 | CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 3043 | return cast<LabelDecl>(Inst); |
Douglas Gregor | 2bba76b | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 3044 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3045 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 3046 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 3047 | if (!Record->isDependentContext()) |
| 3048 | return D; |
| 3049 | |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 3050 | // If the RecordDecl is actually the injected-class-name or a |
| 3051 | // "templated" declaration for a class template, class template |
| 3052 | // partial specialization, or a member class of a class template, |
| 3053 | // substitute into the injected-class-name of the class template |
| 3054 | // or partial specialization to find the new DeclContext. |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 3055 | QualType T; |
| 3056 | ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate(); |
| 3057 | |
| 3058 | if (ClassTemplate) { |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 3059 | T = ClassTemplate->getInjectedClassNameSpecialization(); |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 3060 | } else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 3061 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 3062 | ClassTemplate = PartialSpec->getSpecializedTemplate(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3063 | |
| 3064 | // If we call SubstType with an InjectedClassNameType here we |
| 3065 | // can end up in an infinite loop. |
| 3066 | T = Context.getTypeDeclType(Record); |
| 3067 | assert(isa<InjectedClassNameType>(T) && |
| 3068 | "type of partial specialization is not an InjectedClassNameType"); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3069 | T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3070 | } |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 3071 | |
| 3072 | if (!T.isNull()) { |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 3073 | // Substitute into the injected-class-name to get the type |
| 3074 | // corresponding to the instantiation we want, which may also be |
| 3075 | // the current instantiation (if we're in a template |
| 3076 | // definition). This substitution should never fail, since we |
| 3077 | // know we can instantiate the injected-class-name or we |
| 3078 | // wouldn't have gotten to the injected-class-name! |
| 3079 | |
| 3080 | // FIXME: Can we use the CurrentInstantiationScope to avoid this |
| 3081 | // extra instantiation in the common case? |
Douglas Gregor | b46ae39 | 2011-03-03 21:48:55 +0000 | [diff] [blame] | 3082 | T = SubstType(T, TemplateArgs, Loc, DeclarationName()); |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 3083 | assert(!T.isNull() && "Instantiation of injected-class-name cannot fail."); |
| 3084 | |
| 3085 | if (!T->isDependentType()) { |
| 3086 | assert(T->isRecordType() && "Instantiation must produce a record type"); |
| 3087 | return T->getAs<RecordType>()->getDecl(); |
| 3088 | } |
| 3089 | |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 3090 | // We are performing "partial" template instantiation to create |
| 3091 | // the member declarations for the members of a class template |
| 3092 | // specialization. Therefore, D is actually referring to something |
| 3093 | // in the current instantiation. Look through the current |
| 3094 | // context, which contains actual instantiations, to find the |
| 3095 | // instantiation of the "current instantiation" that D refers |
| 3096 | // to. |
| 3097 | bool SawNonDependentContext = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3098 | for (DeclContext *DC = CurContext; !DC->isFileContext(); |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 3099 | DC = DC->getParent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3100 | if (ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 3101 | = dyn_cast<ClassTemplateSpecializationDecl>(DC)) |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 3102 | if (isInstantiationOf(ClassTemplate, |
| 3103 | Spec->getSpecializedTemplate())) |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 3104 | return Spec; |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 3105 | |
| 3106 | if (!DC->isDependentContext()) |
| 3107 | SawNonDependentContext = true; |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 3108 | } |
| 3109 | |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 3110 | // We're performing "instantiation" of a member of the current |
| 3111 | // instantiation while we are type-checking the |
| 3112 | // definition. Compute the declaration context and return that. |
| 3113 | assert(!SawNonDependentContext && |
| 3114 | "No dependent context while instantiating record"); |
| 3115 | DeclContext *DC = computeDeclContext(T); |
| 3116 | assert(DC && |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 3117 | "Unable to find declaration for the current instantiation"); |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 3118 | return cast<CXXRecordDecl>(DC); |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 3119 | } |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 3120 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 3121 | // Fall through to deal with other dependent record types (e.g., |
| 3122 | // anonymous unions in class templates). |
| 3123 | } |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 3124 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 3125 | if (!ParentDC->isDependentContext()) |
| 3126 | return D; |
| 3127 | |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 3128 | ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3129 | if (!ParentDC) |
Douglas Gregor | 44c7384 | 2009-09-01 17:53:10 +0000 | [diff] [blame] | 3130 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3131 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3132 | if (ParentDC != D->getDeclContext()) { |
| 3133 | // We performed some kind of instantiation in the parent context, |
| 3134 | // so now we need to look into the instantiated parent context to |
| 3135 | // find the instantiation of the declaration D. |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 3136 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3137 | // If our context used to be dependent, we may need to instantiate |
| 3138 | // it before performing lookup into that context. |
Douglas Gregor | eff1dbe | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 3139 | bool IsBeingInstantiated = false; |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3140 | if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) { |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 3141 | if (!Spec->isDependentContext()) { |
| 3142 | QualType T = Context.getTypeDeclType(Spec); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3143 | const RecordType *Tag = T->getAs<RecordType>(); |
| 3144 | assert(Tag && "type of non-dependent record is not a RecordType"); |
Douglas Gregor | eff1dbe | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 3145 | if (Tag->isBeingDefined()) |
| 3146 | IsBeingInstantiated = true; |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 3147 | if (!Tag->isBeingDefined() && |
| 3148 | RequireCompleteType(Loc, T, diag::err_incomplete_type)) |
| 3149 | return 0; |
Douglas Gregor | a43064c | 2010-11-05 23:22:45 +0000 | [diff] [blame] | 3150 | |
| 3151 | ParentDC = Tag->getDecl(); |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 3152 | } |
| 3153 | } |
| 3154 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3155 | NamedDecl *Result = 0; |
| 3156 | if (D->getDeclName()) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3157 | DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName()); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3158 | Result = findInstantiationOf(Context, D, Found.first, Found.second); |
| 3159 | } else { |
| 3160 | // Since we don't have a name for the entity we're looking for, |
| 3161 | // our only option is to walk through all of the declarations to |
| 3162 | // find that name. This will occur in a few cases: |
| 3163 | // |
| 3164 | // - anonymous struct/union within a template |
| 3165 | // - unnamed class/struct/union/enum within a template |
| 3166 | // |
| 3167 | // FIXME: Find a better way to find these instantiations! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3168 | Result = findInstantiationOf(Context, D, |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3169 | ParentDC->decls_begin(), |
| 3170 | ParentDC->decls_end()); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3171 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3172 | |
Douglas Gregor | eff1dbe | 2011-03-06 20:12:45 +0000 | [diff] [blame] | 3173 | if (!Result) { |
| 3174 | if (isa<UsingShadowDecl>(D)) { |
| 3175 | // UsingShadowDecls can instantiate to nothing because of using hiding. |
| 3176 | } else if (Diags.hasErrorOccurred()) { |
| 3177 | // We've already complained about something, so most likely this |
| 3178 | // declaration failed to instantiate. There's no point in complaining |
| 3179 | // further, since this is normal in invalid code. |
| 3180 | } else if (IsBeingInstantiated) { |
| 3181 | // The class in which this member exists is currently being |
| 3182 | // instantiated, and we haven't gotten around to instantiating this |
| 3183 | // member yet. This can happen when the code uses forward declarations |
| 3184 | // of member classes, and introduces ordering dependencies via |
| 3185 | // template instantiation. |
| 3186 | Diag(Loc, diag::err_member_not_yet_instantiated) |
| 3187 | << D->getDeclName() |
| 3188 | << Context.getTypeDeclType(cast<CXXRecordDecl>(ParentDC)); |
| 3189 | Diag(D->getLocation(), diag::note_non_instantiated_member_here); |
| 3190 | } else { |
| 3191 | // We should have found something, but didn't. |
| 3192 | llvm_unreachable("Unable to find instantiation of declaration!"); |
| 3193 | } |
| 3194 | } |
| 3195 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3196 | D = Result; |
| 3197 | } |
| 3198 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 3199 | return D; |
| 3200 | } |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 3201 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3202 | /// \brief Performs template instantiation for all implicit template |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 3203 | /// instantiations we have seen until this point. |
Nick Lewycky | 8155910 | 2011-05-31 07:58:42 +0000 | [diff] [blame] | 3204 | void Sema::PerformPendingInstantiations(bool LocalOnly) { |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 3205 | while (!PendingLocalImplicitInstantiations.empty() || |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3206 | (!LocalOnly && !PendingInstantiations.empty())) { |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 3207 | PendingImplicitInstantiation Inst; |
| 3208 | |
| 3209 | if (PendingLocalImplicitInstantiations.empty()) { |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 3210 | Inst = PendingInstantiations.front(); |
| 3211 | PendingInstantiations.pop_front(); |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 3212 | } else { |
| 3213 | Inst = PendingLocalImplicitInstantiations.front(); |
| 3214 | PendingLocalImplicitInstantiations.pop_front(); |
| 3215 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3216 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3217 | // Instantiate function definitions |
| 3218 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3219 | PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(), |
| 3220 | "instantiating function definition"); |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3221 | bool DefinitionRequired = Function->getTemplateSpecializationKind() == |
| 3222 | TSK_ExplicitInstantiationDefinition; |
| 3223 | InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true, |
| 3224 | DefinitionRequired); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3225 | continue; |
| 3226 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3227 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 3228 | // Instantiate static data member definitions. |
| 3229 | VarDecl *Var = cast<VarDecl>(Inst.first); |
| 3230 | assert(Var->isStaticDataMember() && "Not a static data member?"); |
Anders Carlsson | c17fb7b | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 3231 | |
Chandler Carruth | 291b441 | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 3232 | // Don't try to instantiate declarations if the most recent redeclaration |
| 3233 | // is invalid. |
| 3234 | if (Var->getMostRecentDeclaration()->isInvalidDecl()) |
| 3235 | continue; |
| 3236 | |
| 3237 | // Check if the most recent declaration has changed the specialization kind |
| 3238 | // and removed the need for implicit instantiation. |
| 3239 | switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) { |
| 3240 | case TSK_Undeclared: |
| 3241 | assert(false && "Cannot instantitiate an undeclared specialization."); |
| 3242 | case TSK_ExplicitInstantiationDeclaration: |
Chandler Carruth | 291b441 | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 3243 | case TSK_ExplicitSpecialization: |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3244 | continue; // No longer need to instantiate this type. |
| 3245 | case TSK_ExplicitInstantiationDefinition: |
| 3246 | // We only need an instantiation if the pending instantiation *is* the |
| 3247 | // explicit instantiation. |
| 3248 | if (Var != Var->getMostRecentDeclaration()) continue; |
Chandler Carruth | 291b441 | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 3249 | case TSK_ImplicitInstantiation: |
| 3250 | break; |
| 3251 | } |
| 3252 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3253 | PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(), |
| 3254 | "instantiating static data member " |
| 3255 | "definition"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3256 | |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 3257 | bool DefinitionRequired = Var->getTemplateSpecializationKind() == |
| 3258 | TSK_ExplicitInstantiationDefinition; |
| 3259 | InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true, |
| 3260 | DefinitionRequired); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 3261 | } |
| 3262 | } |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 3263 | |
| 3264 | void Sema::PerformDependentDiagnostics(const DeclContext *Pattern, |
| 3265 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 3266 | for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(), |
| 3267 | E = Pattern->ddiag_end(); I != E; ++I) { |
| 3268 | DependentDiagnostic *DD = *I; |
| 3269 | |
| 3270 | switch (DD->getKind()) { |
| 3271 | case DependentDiagnostic::Access: |
| 3272 | HandleDependentAccessCheck(*DD, TemplateArgs); |
| 3273 | break; |
| 3274 | } |
| 3275 | } |
| 3276 | } |