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