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