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