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; |
| 1056 | bool OverloadableAttrRequired = false; |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1057 | bool isExplicitSpecialization = false; |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1058 | |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1059 | LookupResult Previous(SemaRef, Function->getDeclName(), SourceLocation(), |
| 1060 | Sema::LookupOrdinaryName, Sema::ForRedeclaration); |
| 1061 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1062 | if (DependentFunctionTemplateSpecializationInfo *Info |
| 1063 | = D->getDependentSpecializationInfo()) { |
| 1064 | assert(isFriend && "non-friend has dependent specialization info?"); |
| 1065 | |
| 1066 | // This needs to be set now for future sanity. |
| 1067 | Function->setObjectOfFriendDecl(/*HasPrevious*/ true); |
| 1068 | |
| 1069 | // Instantiate the explicit template arguments. |
| 1070 | TemplateArgumentListInfo ExplicitArgs(Info->getLAngleLoc(), |
| 1071 | Info->getRAngleLoc()); |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1072 | if (SemaRef.Subst(Info->getTemplateArgs(), Info->getNumTemplateArgs(), |
| 1073 | ExplicitArgs, TemplateArgs)) |
| 1074 | return 0; |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1075 | |
| 1076 | // Map the candidate templates to their instantiations. |
| 1077 | for (unsigned I = 0, E = Info->getNumTemplates(); I != E; ++I) { |
| 1078 | Decl *Temp = SemaRef.FindInstantiatedDecl(D->getLocation(), |
| 1079 | Info->getTemplate(I), |
| 1080 | TemplateArgs); |
| 1081 | if (!Temp) return 0; |
| 1082 | |
| 1083 | Previous.addDecl(cast<FunctionTemplateDecl>(Temp)); |
| 1084 | } |
| 1085 | |
| 1086 | if (SemaRef.CheckFunctionTemplateSpecialization(Function, |
| 1087 | &ExplicitArgs, |
| 1088 | Previous)) |
| 1089 | Function->setInvalidDecl(); |
| 1090 | |
| 1091 | isExplicitSpecialization = true; |
| 1092 | |
| 1093 | } else if (TemplateParams || !FunctionTemplate) { |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1094 | // Look only into the namespace where the friend would be declared to |
| 1095 | // find a previous declaration. This is the innermost enclosing namespace, |
| 1096 | // as described in ActOnFriendFunctionDecl. |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1097 | SemaRef.LookupQualifiedName(Previous, DC); |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1098 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1099 | // In C++, the previous declaration we find might be a tag type |
| 1100 | // (class or enum). In this case, the new declaration will hide the |
| 1101 | // tag type. Note that this does does not apply if we're declaring a |
| 1102 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1103 | if (Previous.isSingleTagDecl()) |
| 1104 | Previous.clear(); |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1107 | SemaRef.CheckFunctionDeclaration(/*Scope*/ 0, Function, Previous, |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 1108 | isExplicitSpecialization, Redeclaration, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1109 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1110 | |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1111 | NamedDecl *PrincipalDecl = (TemplateParams |
| 1112 | ? cast<NamedDecl>(FunctionTemplate) |
| 1113 | : Function); |
| 1114 | |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1115 | // If the original function was part of a friend declaration, |
| 1116 | // inherit its namespace state and add it to the owner. |
John McCall | d325daa | 2010-03-26 04:53:08 +0000 | [diff] [blame] | 1117 | if (isFriend) { |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1118 | NamedDecl *PrevDecl; |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1119 | if (TemplateParams) |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1120 | PrevDecl = FunctionTemplate->getPreviousDeclaration(); |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1121 | else |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1122 | PrevDecl = Function->getPreviousDeclaration(); |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1123 | |
| 1124 | PrincipalDecl->setObjectOfFriendDecl(PrevDecl != 0); |
| 1125 | DC->makeDeclVisibleInContext(PrincipalDecl, /*Recoverable=*/ false); |
Gabor Greif | ab297ac | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1126 | |
Gabor Greif | 77535df | 2010-08-30 22:25:56 +0000 | [diff] [blame] | 1127 | bool queuedInstantiation = false; |
Gabor Greif | ab297ac | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1128 | |
Douglas Gregor | 238058c | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1129 | if (!SemaRef.getLangOptions().CPlusPlus0x && |
| 1130 | D->isThisDeclarationADefinition()) { |
| 1131 | // Check for a function body. |
| 1132 | const FunctionDecl *Definition = 0; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1133 | if (Function->hasBody(Definition) && |
Douglas Gregor | 238058c | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1134 | Definition->getTemplateSpecializationKind() == TSK_Undeclared) { |
| 1135 | SemaRef.Diag(Function->getLocation(), diag::err_redefinition) |
| 1136 | << Function->getDeclName(); |
| 1137 | SemaRef.Diag(Definition->getLocation(), diag::note_previous_definition); |
| 1138 | Function->setInvalidDecl(); |
| 1139 | } |
| 1140 | // Check for redefinitions due to other instantiations of this or |
| 1141 | // a similar friend function. |
| 1142 | else for (FunctionDecl::redecl_iterator R = Function->redecls_begin(), |
| 1143 | REnd = Function->redecls_end(); |
| 1144 | R != REnd; ++R) { |
Gabor Greif | 13a8aff | 2010-08-28 15:42:30 +0000 | [diff] [blame] | 1145 | if (*R == Function) |
| 1146 | continue; |
Gabor Greif | ab297ac | 2010-08-30 21:10:05 +0000 | [diff] [blame] | 1147 | switch (R->getFriendObjectKind()) { |
| 1148 | case Decl::FOK_None: |
| 1149 | if (!queuedInstantiation && R->isUsed(false)) { |
| 1150 | if (MemberSpecializationInfo *MSInfo |
| 1151 | = Function->getMemberSpecializationInfo()) { |
| 1152 | if (MSInfo->getPointOfInstantiation().isInvalid()) { |
| 1153 | SourceLocation Loc = R->getLocation(); // FIXME |
| 1154 | MSInfo->setPointOfInstantiation(Loc); |
| 1155 | SemaRef.PendingLocalImplicitInstantiations.push_back( |
| 1156 | std::make_pair(Function, Loc)); |
| 1157 | queuedInstantiation = true; |
| 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | break; |
| 1162 | default: |
Douglas Gregor | 238058c | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1163 | if (const FunctionDecl *RPattern |
Gabor Greif | 6a557d8 | 2010-08-28 15:46:56 +0000 | [diff] [blame] | 1164 | = R->getTemplateInstantiationPattern()) |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1165 | if (RPattern->hasBody(RPattern)) { |
Douglas Gregor | 238058c | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1166 | SemaRef.Diag(Function->getLocation(), diag::err_redefinition) |
| 1167 | << Function->getDeclName(); |
Gabor Greif | 6a557d8 | 2010-08-28 15:46:56 +0000 | [diff] [blame] | 1168 | SemaRef.Diag(R->getLocation(), diag::note_previous_definition); |
Douglas Gregor | 238058c | 2010-05-18 05:45:02 +0000 | [diff] [blame] | 1169 | Function->setInvalidDecl(); |
| 1170 | break; |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | } |
Douglas Gregor | a735b20 | 2009-10-13 14:39:41 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
John McCall | 76d3264 | 2010-04-24 01:30:58 +0000 | [diff] [blame] | 1177 | if (Function->isOverloadedOperator() && !DC->isRecord() && |
| 1178 | PrincipalDecl->isInIdentifierNamespace(Decl::IDNS_Ordinary)) |
| 1179 | PrincipalDecl->setNonMemberOperator(); |
| 1180 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1181 | return Function; |
| 1182 | } |
| 1183 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1184 | Decl * |
| 1185 | TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D, |
| 1186 | TemplateParameterList *TemplateParams) { |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1187 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
| 1188 | void *InsertPos = 0; |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1189 | if (FunctionTemplate && !TemplateParams) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1190 | // We are creating a function template specialization from a function |
| 1191 | // template. Check whether there is already a function template |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1192 | // specialization for this particular set of template arguments. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1193 | std::pair<const TemplateArgument *, unsigned> Innermost |
| 1194 | = TemplateArgs.getInnermost(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1195 | |
Argyrios Kyrtzidis | 2c853e4 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1196 | FunctionDecl *SpecFunc |
| 1197 | = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second, |
| 1198 | InsertPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1200 | // If we already have a function template specialization, return it. |
Argyrios Kyrtzidis | 2c853e4 | 2010-07-20 13:59:58 +0000 | [diff] [blame] | 1201 | if (SpecFunc) |
| 1202 | return SpecFunc; |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1205 | bool isFriend; |
| 1206 | if (FunctionTemplate) |
| 1207 | isFriend = (FunctionTemplate->getFriendObjectKind() != Decl::FOK_None); |
| 1208 | else |
| 1209 | isFriend = (D->getFriendObjectKind() != Decl::FOK_None); |
| 1210 | |
Douglas Gregor | 79c2278 | 2010-01-16 20:21:20 +0000 | [diff] [blame] | 1211 | bool MergeWithParentScope = (TemplateParams != 0) || |
| 1212 | !(isa<Decl>(Owner) && |
| 1213 | cast<Decl>(Owner)->isDefinedOutsideFunctionOrMethod()); |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1214 | LocalInstantiationScope Scope(SemaRef, MergeWithParentScope); |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 1215 | |
John McCall | 4eab39f | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1216 | // Instantiate enclosing template arguments for friends. |
| 1217 | llvm::SmallVector<TemplateParameterList *, 4> TempParamLists; |
| 1218 | unsigned NumTempParamLists = 0; |
| 1219 | if (isFriend && (NumTempParamLists = D->getNumTemplateParameterLists())) { |
| 1220 | TempParamLists.set_size(NumTempParamLists); |
| 1221 | for (unsigned I = 0; I != NumTempParamLists; ++I) { |
| 1222 | TemplateParameterList *TempParams = D->getTemplateParameterList(I); |
| 1223 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1224 | if (!InstParams) |
| 1225 | return NULL; |
| 1226 | TempParamLists[I] = InstParams; |
| 1227 | } |
| 1228 | } |
| 1229 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 1230 | llvm::SmallVector<ParmVarDecl *, 4> Params; |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1231 | TypeSourceInfo *TInfo = D->getTypeSourceInfo(); |
| 1232 | TInfo = SubstFunctionType(D, Params); |
| 1233 | if (!TInfo) |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1234 | return 0; |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1235 | QualType T = TInfo->getType(); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1236 | |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 1237 | // \brief If the type of this function, after ignoring parentheses, |
| 1238 | // is not *directly* a function type, then we're instantiating a function |
| 1239 | // that was declared via a typedef, e.g., |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 1240 | // |
| 1241 | // typedef int functype(int, int); |
| 1242 | // functype func; |
| 1243 | // |
| 1244 | // In this case, we'll just go instantiate the ParmVarDecls that we |
| 1245 | // synthesized in the method declaration. |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 1246 | if (!isa<FunctionProtoType>(T.IgnoreParens())) { |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 1247 | assert(!Params.size() && "Instantiating type could not yield parameters"); |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 1248 | llvm::SmallVector<QualType, 4> ParamTypes; |
| 1249 | if (SemaRef.SubstParmTypes(D->getLocation(), D->param_begin(), |
| 1250 | D->getNumParams(), TemplateArgs, ParamTypes, |
| 1251 | &Params)) |
| 1252 | return 0; |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1255 | NestedNameSpecifier *Qualifier = D->getQualifier(); |
| 1256 | if (Qualifier) { |
| 1257 | Qualifier = SemaRef.SubstNestedNameSpecifier(Qualifier, |
| 1258 | D->getQualifierRange(), |
| 1259 | TemplateArgs); |
| 1260 | if (!Qualifier) return 0; |
| 1261 | } |
| 1262 | |
| 1263 | DeclContext *DC = Owner; |
| 1264 | if (isFriend) { |
| 1265 | if (Qualifier) { |
| 1266 | CXXScopeSpec SS; |
| 1267 | SS.setScopeRep(Qualifier); |
| 1268 | SS.setRange(D->getQualifierRange()); |
| 1269 | DC = SemaRef.computeDeclContext(SS); |
John McCall | c54d688 | 2010-10-19 05:01:53 +0000 | [diff] [blame] | 1270 | |
| 1271 | if (DC && SemaRef.RequireCompleteDeclContext(SS, DC)) |
| 1272 | return 0; |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1273 | } else { |
| 1274 | DC = SemaRef.FindInstantiatedContext(D->getLocation(), |
| 1275 | D->getDeclContext(), |
| 1276 | TemplateArgs); |
| 1277 | } |
| 1278 | if (!DC) return 0; |
| 1279 | } |
| 1280 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1281 | // Build the instantiated method declaration. |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1282 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1283 | CXXMethodDecl *Method = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1284 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1285 | DeclarationNameInfo NameInfo |
| 1286 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1287 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1288 | Method = CXXConstructorDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1289 | NameInfo, T, TInfo, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1290 | Constructor->isExplicit(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1291 | Constructor->isInlineSpecified(), |
| 1292 | false); |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1293 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1294 | Method = CXXDestructorDecl::Create(SemaRef.Context, Record, |
Craig Silverstein | b41d899 | 2010-10-21 00:44:50 +0000 | [diff] [blame] | 1295 | NameInfo, T, TInfo, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1296 | Destructor->isInlineSpecified(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1297 | false); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1298 | } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1299 | Method = CXXConversionDecl::Create(SemaRef.Context, Record, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1300 | NameInfo, T, TInfo, |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 1301 | Conversion->isInlineSpecified(), |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1302 | Conversion->isExplicit()); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1303 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1304 | Method = CXXMethodDecl::Create(SemaRef.Context, Record, |
| 1305 | NameInfo, T, TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1306 | D->isStatic(), |
| 1307 | D->getStorageClassAsWritten(), |
| 1308 | D->isInlineSpecified()); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1309 | } |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 1310 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1311 | if (Qualifier) |
| 1312 | Method->setQualifierInfo(Qualifier, D->getQualifierRange()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1313 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1314 | if (TemplateParams) { |
| 1315 | // Our resulting instantiation is actually a function template, since we |
| 1316 | // are substituting only the outer template parameters. For example, given |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1317 | // |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1318 | // template<typename T> |
| 1319 | // struct X { |
| 1320 | // template<typename U> void f(T, U); |
| 1321 | // }; |
| 1322 | // |
| 1323 | // X<int> x; |
| 1324 | // |
| 1325 | // We are instantiating the member template "f" within X<int>, which means |
| 1326 | // substituting int for T, but leaving "f" as a member function template. |
| 1327 | // Build the function template itself. |
| 1328 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, |
| 1329 | Method->getLocation(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1330 | Method->getDeclName(), |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1331 | TemplateParams, Method); |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1332 | if (isFriend) { |
| 1333 | FunctionTemplate->setLexicalDeclContext(Owner); |
| 1334 | FunctionTemplate->setObjectOfFriendDecl(true); |
| 1335 | } else if (D->isOutOfLine()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1336 | FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 1337 | Method->setDescribedFunctionTemplate(FunctionTemplate); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1338 | } else if (FunctionTemplate) { |
| 1339 | // Record this function template specialization. |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 1340 | std::pair<const TemplateArgument *, unsigned> Innermost |
| 1341 | = TemplateArgs.getInnermost(); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1342 | Method->setFunctionTemplateSpecialization(FunctionTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1343 | TemplateArgumentList::CreateCopy(SemaRef.Context, |
| 1344 | Innermost.first, |
| 1345 | Innermost.second), |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1346 | InsertPos); |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1347 | } else if (!isFriend) { |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1348 | // Record that this is an instantiation of a member function. |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 1349 | Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1352 | // If we are instantiating a member function defined |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1353 | // out-of-line, the instantiation will have the same lexical |
| 1354 | // context (which will be a namespace scope) as the template. |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1355 | if (isFriend) { |
John McCall | 4eab39f | 2010-10-19 02:26:41 +0000 | [diff] [blame] | 1356 | if (NumTempParamLists) |
| 1357 | Method->setTemplateParameterListsInfo(SemaRef.Context, |
| 1358 | NumTempParamLists, |
| 1359 | TempParamLists.data()); |
| 1360 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1361 | Method->setLexicalDeclContext(Owner); |
| 1362 | Method->setObjectOfFriendDecl(true); |
| 1363 | } else if (D->isOutOfLine()) |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1364 | Method->setLexicalDeclContext(D->getLexicalDeclContext()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1365 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1366 | // Attach the parameters |
| 1367 | for (unsigned P = 0; P < Params.size(); ++P) |
| 1368 | Params[P]->setOwningFunction(Method); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1369 | Method->setParams(Params.data(), Params.size()); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1370 | |
| 1371 | if (InitMethodInstantiation(Method, D)) |
| 1372 | Method->setInvalidDecl(); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1373 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1374 | LookupResult Previous(SemaRef, NameInfo, Sema::LookupOrdinaryName, |
| 1375 | Sema::ForRedeclaration); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1376 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1377 | if (!FunctionTemplate || TemplateParams || isFriend) { |
| 1378 | SemaRef.LookupQualifiedName(Previous, Record); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1379 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1380 | // In C++, the previous declaration we find might be a tag type |
| 1381 | // (class or enum). In this case, the new declaration will hide the |
| 1382 | // tag type. Note that this does does not apply if we're declaring a |
| 1383 | // typedef (C++ [dcl.typedef]p4). |
John McCall | 6826314 | 2009-11-18 22:49:29 +0000 | [diff] [blame] | 1384 | if (Previous.isSingleTagDecl()) |
| 1385 | Previous.clear(); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1386 | } |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1387 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1388 | bool Redeclaration = false; |
| 1389 | bool OverloadableAttrRequired = false; |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1390 | SemaRef.CheckFunctionDeclaration(0, Method, Previous, false, Redeclaration, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1391 | /*FIXME:*/OverloadableAttrRequired); |
| 1392 | |
Douglas Gregor | 4ba3136 | 2009-12-01 17:24:26 +0000 | [diff] [blame] | 1393 | if (D->isPure()) |
| 1394 | SemaRef.CheckPureMethod(Method, SourceRange()); |
| 1395 | |
John McCall | 46460a6 | 2010-01-20 21:53:11 +0000 | [diff] [blame] | 1396 | Method->setAccess(D->getAccess()); |
| 1397 | |
John McCall | b0cb022 | 2010-03-27 05:57:59 +0000 | [diff] [blame] | 1398 | if (FunctionTemplate) { |
| 1399 | // If there's a function template, let our caller handle it. |
| 1400 | } else if (Method->isInvalidDecl() && !Previous.empty()) { |
| 1401 | // Don't hide a (potentially) valid declaration with an invalid one. |
| 1402 | } else { |
| 1403 | NamedDecl *DeclToAdd = (TemplateParams |
| 1404 | ? cast<NamedDecl>(FunctionTemplate) |
| 1405 | : Method); |
| 1406 | if (isFriend) |
| 1407 | Record->makeDeclVisibleInContext(DeclToAdd); |
| 1408 | else |
| 1409 | Owner->addDecl(DeclToAdd); |
| 1410 | } |
Argyrios Kyrtzidis | bbc6454 | 2010-08-15 01:15:20 +0000 | [diff] [blame] | 1411 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1412 | return Method; |
| 1413 | } |
| 1414 | |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 1415 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 1416 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 1419 | Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 1420 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 1423 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1424 | return VisitCXXMethodDecl(D); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 1427 | ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1428 | return SemaRef.SubstParmVarDecl(D, TemplateArgs); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1431 | Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( |
| 1432 | TemplateTypeParmDecl *D) { |
| 1433 | // TODO: don't always clone when decls are refcounted. |
Douglas Gregor | efed5c8 | 2010-06-16 15:23:05 +0000 | [diff] [blame] | 1434 | const Type* T = D->getTypeForDecl(); |
| 1435 | assert(T->isTemplateTypeParmType()); |
| 1436 | const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1437 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1438 | TemplateTypeParmDecl *Inst = |
| 1439 | TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
Douglas Gregor | 71b87e4 | 2010-08-30 23:23:59 +0000 | [diff] [blame] | 1440 | TTPT->getDepth() - TemplateArgs.getNumLevels(), |
Nick Lewycky | 61139c5 | 2010-10-30 06:48:20 +0000 | [diff] [blame] | 1441 | TTPT->getIndex(), D->getIdentifier(), |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1442 | D->wasDeclaredWithTypename(), |
| 1443 | D->isParameterPack()); |
| 1444 | |
Douglas Gregor | 0f8716b | 2009-11-09 19:17:50 +0000 | [diff] [blame] | 1445 | if (D->hasDefaultArgument()) |
| 1446 | Inst->setDefaultArgument(D->getDefaultArgumentInfo(), false); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1447 | |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1448 | // Introduce this template parameter's instantiation into the instantiation |
| 1449 | // scope. |
| 1450 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Inst); |
| 1451 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1452 | return Inst; |
| 1453 | } |
| 1454 | |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1455 | Decl *TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl( |
| 1456 | NonTypeTemplateParmDecl *D) { |
| 1457 | // Substitute into the type of the non-type template parameter. |
| 1458 | QualType T; |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1459 | TypeSourceInfo *DI = D->getTypeSourceInfo(); |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1460 | if (DI) { |
| 1461 | DI = SemaRef.SubstType(DI, TemplateArgs, D->getLocation(), |
| 1462 | D->getDeclName()); |
| 1463 | if (DI) T = DI->getType(); |
| 1464 | } else { |
| 1465 | T = SemaRef.SubstType(D->getType(), TemplateArgs, D->getLocation(), |
| 1466 | D->getDeclName()); |
| 1467 | DI = 0; |
| 1468 | } |
| 1469 | if (T.isNull()) |
| 1470 | return 0; |
| 1471 | |
| 1472 | // Check that this type is acceptable for a non-type template parameter. |
| 1473 | bool Invalid = false; |
| 1474 | T = SemaRef.CheckNonTypeTemplateParameterType(T, D->getLocation()); |
| 1475 | if (T.isNull()) { |
| 1476 | T = SemaRef.Context.IntTy; |
| 1477 | Invalid = true; |
| 1478 | } |
| 1479 | |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 1480 | // FIXME: Variadic templates. |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1481 | NonTypeTemplateParmDecl *Param |
| 1482 | = NonTypeTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
Douglas Gregor | 71b87e4 | 2010-08-30 23:23:59 +0000 | [diff] [blame] | 1483 | D->getDepth() - TemplateArgs.getNumLevels(), |
| 1484 | D->getPosition(), D->getIdentifier(), T, |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 1485 | D->isParameterPack(), DI); |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1486 | if (Invalid) |
| 1487 | Param->setInvalidDecl(); |
| 1488 | |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1489 | Param->setDefaultArgument(D->getDefaultArgument(), false); |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1490 | |
| 1491 | // Introduce this template parameter's instantiation into the instantiation |
| 1492 | // scope. |
| 1493 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
Douglas Gregor | 33642df | 2009-10-23 23:25:44 +0000 | [diff] [blame] | 1494 | return Param; |
| 1495 | } |
| 1496 | |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 1497 | Decl * |
Douglas Gregor | 9106ef7 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 1498 | TemplateDeclInstantiator::VisitTemplateTemplateParmDecl( |
| 1499 | TemplateTemplateParmDecl *D) { |
| 1500 | // Instantiate the template parameter list of the template template parameter. |
| 1501 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 1502 | TemplateParameterList *InstParams; |
| 1503 | { |
| 1504 | // Perform the actual substitution of template parameters within a new, |
| 1505 | // local instantiation scope. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1506 | LocalInstantiationScope Scope(SemaRef); |
Douglas Gregor | 9106ef7 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 1507 | InstParams = SubstTemplateParams(TempParams); |
| 1508 | if (!InstParams) |
| 1509 | return NULL; |
| 1510 | } |
| 1511 | |
| 1512 | // Build the template template parameter. |
| 1513 | TemplateTemplateParmDecl *Param |
| 1514 | = TemplateTemplateParmDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
Douglas Gregor | 71b87e4 | 2010-08-30 23:23:59 +0000 | [diff] [blame] | 1515 | D->getDepth() - TemplateArgs.getNumLevels(), |
Douglas Gregor | 61c4d28 | 2011-01-05 15:48:55 +0000 | [diff] [blame] | 1516 | D->getPosition(), D->isParameterPack(), |
| 1517 | D->getIdentifier(), InstParams); |
Abramo Bagnara | d92f7a2 | 2010-06-09 09:26:05 +0000 | [diff] [blame] | 1518 | Param->setDefaultArgument(D->getDefaultArgument(), false); |
Douglas Gregor | 4469e8a | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 1519 | |
Douglas Gregor | 9106ef7 | 2009-11-11 16:58:32 +0000 | [diff] [blame] | 1520 | // Introduce this template parameter's instantiation into the instantiation |
| 1521 | // scope. |
| 1522 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
| 1523 | |
| 1524 | return Param; |
| 1525 | } |
| 1526 | |
Douglas Gregor | 48c32a7 | 2009-11-17 06:07:40 +0000 | [diff] [blame] | 1527 | Decl *TemplateDeclInstantiator::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { |
| 1528 | // Using directives are never dependent, so they require no explicit |
| 1529 | |
| 1530 | UsingDirectiveDecl *Inst |
| 1531 | = UsingDirectiveDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 1532 | D->getNamespaceKeyLocation(), |
| 1533 | D->getQualifierRange(), D->getQualifier(), |
| 1534 | D->getIdentLocation(), |
| 1535 | D->getNominatedNamespace(), |
| 1536 | D->getCommonAncestor()); |
| 1537 | Owner->addDecl(Inst); |
| 1538 | return Inst; |
| 1539 | } |
| 1540 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1541 | Decl *TemplateDeclInstantiator::VisitUsingDecl(UsingDecl *D) { |
Douglas Gregor | 1b39820 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 1542 | |
| 1543 | // The nested name specifier may be dependent, for example |
| 1544 | // template <typename T> struct t { |
| 1545 | // struct s1 { T f1(); }; |
| 1546 | // struct s2 : s1 { using s1::f1; }; |
| 1547 | // }; |
| 1548 | // template struct t<int>; |
| 1549 | // Here, in using s1::f1, s1 refers to t<T>::s1; |
| 1550 | // we need to substitute for t<int>::s1. |
| 1551 | NestedNameSpecifier *NNS = |
| 1552 | SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameDecl(), |
| 1553 | D->getNestedNameRange(), |
| 1554 | TemplateArgs); |
| 1555 | if (!NNS) |
| 1556 | return 0; |
| 1557 | |
| 1558 | // The name info is non-dependent, so no transformation |
| 1559 | // is required. |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1560 | DeclarationNameInfo NameInfo = D->getNameInfo(); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1561 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1562 | // We only need to do redeclaration lookups if we're in a class |
| 1563 | // scope (in fact, it's not really even possible in non-class |
| 1564 | // scopes). |
| 1565 | bool CheckRedeclaration = Owner->isRecord(); |
| 1566 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1567 | LookupResult Prev(SemaRef, NameInfo, Sema::LookupUsingDeclName, |
| 1568 | Sema::ForRedeclaration); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1569 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1570 | UsingDecl *NewUD = UsingDecl::Create(SemaRef.Context, Owner, |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1571 | D->getNestedNameRange(), |
| 1572 | D->getUsingLocation(), |
Douglas Gregor | 1b39820 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 1573 | NNS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1574 | NameInfo, |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1575 | D->isTypeName()); |
| 1576 | |
| 1577 | CXXScopeSpec SS; |
Douglas Gregor | 1b39820 | 2010-09-29 17:58:28 +0000 | [diff] [blame] | 1578 | SS.setScopeRep(NNS); |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1579 | SS.setRange(D->getNestedNameRange()); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1580 | |
| 1581 | if (CheckRedeclaration) { |
| 1582 | Prev.setHideTags(false); |
| 1583 | SemaRef.LookupQualifiedName(Prev, Owner); |
| 1584 | |
| 1585 | // Check for invalid redeclarations. |
| 1586 | if (SemaRef.CheckUsingDeclRedeclaration(D->getUsingLocation(), |
| 1587 | D->isTypeName(), SS, |
| 1588 | D->getLocation(), Prev)) |
| 1589 | NewUD->setInvalidDecl(); |
| 1590 | |
| 1591 | } |
| 1592 | |
| 1593 | if (!NewUD->isInvalidDecl() && |
| 1594 | SemaRef.CheckUsingDeclQualifier(D->getUsingLocation(), SS, |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1595 | D->getLocation())) |
| 1596 | NewUD->setInvalidDecl(); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1597 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1598 | SemaRef.Context.setInstantiatedFromUsingDecl(NewUD, D); |
| 1599 | NewUD->setAccess(D->getAccess()); |
| 1600 | Owner->addDecl(NewUD); |
| 1601 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1602 | // Don't process the shadow decls for an invalid decl. |
| 1603 | if (NewUD->isInvalidDecl()) |
| 1604 | return NewUD; |
| 1605 | |
John McCall | 323c310 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 1606 | bool isFunctionScope = Owner->isFunctionOrMethod(); |
| 1607 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1608 | // Process the shadow decls. |
| 1609 | for (UsingDecl::shadow_iterator I = D->shadow_begin(), E = D->shadow_end(); |
| 1610 | I != E; ++I) { |
| 1611 | UsingShadowDecl *Shadow = *I; |
| 1612 | NamedDecl *InstTarget = |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 1613 | cast<NamedDecl>(SemaRef.FindInstantiatedDecl(Shadow->getLocation(), |
| 1614 | Shadow->getTargetDecl(), |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1615 | TemplateArgs)); |
| 1616 | |
| 1617 | if (CheckRedeclaration && |
| 1618 | SemaRef.CheckUsingShadowDecl(NewUD, InstTarget, Prev)) |
| 1619 | continue; |
| 1620 | |
| 1621 | UsingShadowDecl *InstShadow |
| 1622 | = SemaRef.BuildUsingShadowDecl(/*Scope*/ 0, NewUD, InstTarget); |
| 1623 | SemaRef.Context.setInstantiatedFromUsingShadowDecl(InstShadow, Shadow); |
John McCall | 323c310 | 2009-12-22 22:26:37 +0000 | [diff] [blame] | 1624 | |
| 1625 | if (isFunctionScope) |
| 1626 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(Shadow, InstShadow); |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1627 | } |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1628 | |
| 1629 | return NewUD; |
| 1630 | } |
| 1631 | |
| 1632 | Decl *TemplateDeclInstantiator::VisitUsingShadowDecl(UsingShadowDecl *D) { |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 1633 | // Ignore these; we handle them in bulk when processing the UsingDecl. |
| 1634 | return 0; |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1637 | Decl * TemplateDeclInstantiator |
| 1638 | ::VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1639 | NestedNameSpecifier *NNS = |
| 1640 | SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(), |
| 1641 | D->getTargetNestedNameRange(), |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 1642 | TemplateArgs); |
| 1643 | if (!NNS) |
| 1644 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1645 | |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 1646 | CXXScopeSpec SS; |
| 1647 | SS.setRange(D->getTargetNestedNameRange()); |
| 1648 | SS.setScopeRep(NNS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1649 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1650 | // Since NameInfo refers to a typename, it cannot be a C++ special name. |
| 1651 | // Hence, no tranformation is required for it. |
| 1652 | DeclarationNameInfo NameInfo(D->getDeclName(), D->getLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | NamedDecl *UD = |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 1654 | SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(), |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1655 | D->getUsingLoc(), SS, NameInfo, 0, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1656 | /*instantiation*/ true, |
| 1657 | /*typename*/ true, D->getTypenameLoc()); |
Douglas Gregor | 4469e8a | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 1658 | if (UD) |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1659 | SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D); |
| 1660 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1661 | return UD; |
| 1662 | } |
| 1663 | |
| 1664 | Decl * TemplateDeclInstantiator |
| 1665 | ::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { |
| 1666 | NestedNameSpecifier *NNS = |
| 1667 | SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(), |
| 1668 | D->getTargetNestedNameRange(), |
| 1669 | TemplateArgs); |
| 1670 | if (!NNS) |
| 1671 | return 0; |
| 1672 | |
| 1673 | CXXScopeSpec SS; |
| 1674 | SS.setRange(D->getTargetNestedNameRange()); |
| 1675 | SS.setScopeRep(NNS); |
| 1676 | |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1677 | DeclarationNameInfo NameInfo |
| 1678 | = SemaRef.SubstDeclarationNameInfo(D->getNameInfo(), TemplateArgs); |
| 1679 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1680 | NamedDecl *UD = |
| 1681 | SemaRef.BuildUsingDeclaration(/*Scope*/ 0, D->getAccess(), |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1682 | D->getUsingLoc(), SS, NameInfo, 0, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1683 | /*instantiation*/ true, |
| 1684 | /*typename*/ false, SourceLocation()); |
Douglas Gregor | 4469e8a | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 1685 | if (UD) |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 1686 | SemaRef.Context.setInstantiatedFromUsingDecl(cast<UsingDecl>(UD), D); |
| 1687 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1688 | return UD; |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1691 | Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 1692 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1693 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
Douglas Gregor | 2fa9800 | 2010-02-16 19:28:15 +0000 | [diff] [blame] | 1694 | if (D->isInvalidDecl()) |
| 1695 | return 0; |
| 1696 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 1697 | return Instantiator.Visit(D); |
| 1698 | } |
| 1699 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1700 | /// \brief Instantiates a nested template parameter list in the current |
| 1701 | /// instantiation context. |
| 1702 | /// |
| 1703 | /// \param L The parameter list to instantiate |
| 1704 | /// |
| 1705 | /// \returns NULL if there was an error |
| 1706 | TemplateParameterList * |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1707 | TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1708 | // Get errors for all the parameters before bailing out. |
| 1709 | bool Invalid = false; |
| 1710 | |
| 1711 | unsigned N = L->size(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1712 | typedef llvm::SmallVector<NamedDecl *, 8> ParamVector; |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1713 | ParamVector Params; |
| 1714 | Params.reserve(N); |
| 1715 | for (TemplateParameterList::iterator PI = L->begin(), PE = L->end(); |
| 1716 | PI != PE; ++PI) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1717 | NamedDecl *D = cast_or_null<NamedDecl>(Visit(*PI)); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1718 | Params.push_back(D); |
Douglas Gregor | 9148c3f | 2009-11-11 19:13:48 +0000 | [diff] [blame] | 1719 | Invalid = Invalid || !D || D->isInvalidDecl(); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | // Clean up if we had an error. |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 1723 | if (Invalid) |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1724 | return NULL; |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1725 | |
| 1726 | TemplateParameterList *InstL |
| 1727 | = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), |
| 1728 | L->getLAngleLoc(), &Params.front(), N, |
| 1729 | L->getRAngleLoc()); |
| 1730 | return InstL; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1731 | } |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 1732 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1733 | /// \brief Instantiate the declaration of a class template partial |
| 1734 | /// specialization. |
| 1735 | /// |
| 1736 | /// \param ClassTemplate the (instantiated) class template that is partially |
| 1737 | // specialized by the instantiation of \p PartialSpec. |
| 1738 | /// |
| 1739 | /// \param PartialSpec the (uninstantiated) class template partial |
| 1740 | /// specialization that we are instantiating. |
| 1741 | /// |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1742 | /// \returns The instantiated partial specialization, if successful; otherwise, |
| 1743 | /// NULL to indicate an error. |
| 1744 | ClassTemplatePartialSpecializationDecl * |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1745 | TemplateDeclInstantiator::InstantiateClassTemplatePartialSpecialization( |
| 1746 | ClassTemplateDecl *ClassTemplate, |
| 1747 | ClassTemplatePartialSpecializationDecl *PartialSpec) { |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1748 | // Create a local instantiation scope for this class template partial |
| 1749 | // specialization, which will contain the instantiations of the template |
| 1750 | // parameters. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1751 | LocalInstantiationScope Scope(SemaRef); |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 1752 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1753 | // Substitute into the template parameters of the class template partial |
| 1754 | // specialization. |
| 1755 | TemplateParameterList *TempParams = PartialSpec->getTemplateParameters(); |
| 1756 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 1757 | if (!InstParams) |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1758 | return 0; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1759 | |
| 1760 | // Substitute into the template arguments of the class template partial |
| 1761 | // specialization. |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1762 | TemplateArgumentListInfo InstTemplateArgs; // no angle locations |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1763 | if (SemaRef.Subst(PartialSpec->getTemplateArgsAsWritten(), |
| 1764 | PartialSpec->getNumTemplateArgsAsWritten(), |
| 1765 | InstTemplateArgs, TemplateArgs)) |
| 1766 | return 0; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1767 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1768 | // Check that the template argument list is well-formed for this |
| 1769 | // class template. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1770 | llvm::SmallVector<TemplateArgument, 4> Converted; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1771 | if (SemaRef.CheckTemplateArgumentList(ClassTemplate, |
| 1772 | PartialSpec->getLocation(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1773 | InstTemplateArgs, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1774 | false, |
| 1775 | Converted)) |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1776 | return 0; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1777 | |
| 1778 | // Figure out where to insert this class template partial specialization |
| 1779 | // in the member template's set of class template partial specializations. |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1780 | void *InsertPos = 0; |
| 1781 | ClassTemplateSpecializationDecl *PrevDecl |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1782 | = ClassTemplate->findPartialSpecialization(Converted.data(), |
| 1783 | Converted.size(), InsertPos); |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1784 | |
| 1785 | // Build the canonical type that describes the converted template |
| 1786 | // arguments of the class template partial specialization. |
| 1787 | QualType CanonType |
| 1788 | = SemaRef.Context.getTemplateSpecializationType(TemplateName(ClassTemplate), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1789 | Converted.data(), |
| 1790 | Converted.size()); |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1791 | |
| 1792 | // Build the fully-sugared type for this class template |
| 1793 | // specialization as the user wrote in the specialization |
| 1794 | // itself. This means that we'll pretty-print the type retrieved |
| 1795 | // from the specialization's declaration the way that the user |
| 1796 | // actually wrote the specialization, rather than formatting the |
| 1797 | // name based on the "canonical" representation used to store the |
| 1798 | // template arguments in the specialization. |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1799 | TypeSourceInfo *WrittenTy |
| 1800 | = SemaRef.Context.getTemplateSpecializationTypeInfo( |
| 1801 | TemplateName(ClassTemplate), |
| 1802 | PartialSpec->getLocation(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1803 | InstTemplateArgs, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1804 | CanonType); |
| 1805 | |
| 1806 | if (PrevDecl) { |
| 1807 | // We've already seen a partial specialization with the same template |
| 1808 | // parameters and template arguments. This can happen, for example, when |
| 1809 | // substituting the outer template arguments ends up causing two |
| 1810 | // class template partial specializations of a member class template |
| 1811 | // to have identical forms, e.g., |
| 1812 | // |
| 1813 | // template<typename T, typename U> |
| 1814 | // struct Outer { |
| 1815 | // template<typename X, typename Y> struct Inner; |
| 1816 | // template<typename Y> struct Inner<T, Y>; |
| 1817 | // template<typename Y> struct Inner<U, Y>; |
| 1818 | // }; |
| 1819 | // |
| 1820 | // Outer<int, int> outer; // error: the partial specializations of Inner |
| 1821 | // // have the same signature. |
| 1822 | SemaRef.Diag(PartialSpec->getLocation(), diag::err_partial_spec_redeclared) |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1823 | << WrittenTy->getType(); |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1824 | SemaRef.Diag(PrevDecl->getLocation(), diag::note_prev_partial_spec_here) |
| 1825 | << SemaRef.Context.getTypeDeclType(PrevDecl); |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1826 | return 0; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
| 1829 | |
| 1830 | // Create the class template partial specialization declaration. |
| 1831 | ClassTemplatePartialSpecializationDecl *InstPartialSpec |
Douglas Gregor | 13c8577 | 2010-05-06 00:28:52 +0000 | [diff] [blame] | 1832 | = ClassTemplatePartialSpecializationDecl::Create(SemaRef.Context, |
| 1833 | PartialSpec->getTagKind(), |
| 1834 | Owner, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1835 | PartialSpec->getLocation(), |
| 1836 | InstParams, |
| 1837 | ClassTemplate, |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1838 | Converted.data(), |
| 1839 | Converted.size(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1840 | InstTemplateArgs, |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1841 | CanonType, |
Douglas Gregor | dc60c1e | 2010-04-30 05:56:50 +0000 | [diff] [blame] | 1842 | 0, |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 1843 | ClassTemplate->getNextPartialSpecSequenceNumber()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1844 | // Substitute the nested name specifier, if any. |
| 1845 | if (SubstQualifier(PartialSpec, InstPartialSpec)) |
| 1846 | return 0; |
| 1847 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1848 | InstPartialSpec->setInstantiatedFromMember(PartialSpec); |
Douglas Gregor | 4469e8a | 2010-05-19 17:02:24 +0000 | [diff] [blame] | 1849 | InstPartialSpec->setTypeAsWritten(WrittenTy); |
| 1850 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1851 | // Add this partial specialization to the set of class template partial |
| 1852 | // specializations. |
Argyrios Kyrtzidis | cc0b1bc | 2010-07-20 13:59:28 +0000 | [diff] [blame] | 1853 | ClassTemplate->AddPartialSpecialization(InstPartialSpec, InsertPos); |
Douglas Gregor | d65587f | 2010-11-10 19:44:59 +0000 | [diff] [blame] | 1854 | return InstPartialSpec; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1857 | TypeSourceInfo* |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1858 | TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1859 | llvm::SmallVectorImpl<ParmVarDecl *> &Params) { |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1860 | TypeSourceInfo *OldTInfo = D->getTypeSourceInfo(); |
| 1861 | assert(OldTInfo && "substituting function without type source info"); |
| 1862 | assert(Params.empty() && "parameter vector is non-empty at start"); |
John McCall | 6cd3b9f | 2010-04-09 17:38:44 +0000 | [diff] [blame] | 1863 | TypeSourceInfo *NewTInfo |
| 1864 | = SemaRef.SubstFunctionDeclType(OldTInfo, TemplateArgs, |
| 1865 | D->getTypeSpecStartLoc(), |
| 1866 | D->getDeclName()); |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1867 | if (!NewTInfo) |
| 1868 | return 0; |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1869 | |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1870 | if (NewTInfo != OldTInfo) { |
| 1871 | // Get parameters from the new type info. |
Abramo Bagnara | 140a2bd | 2010-12-13 22:27:55 +0000 | [diff] [blame] | 1872 | TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens(); |
Douglas Gregor | 6920cdc | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 1873 | if (FunctionProtoTypeLoc *OldProtoLoc |
| 1874 | = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) { |
Abramo Bagnara | 140a2bd | 2010-12-13 22:27:55 +0000 | [diff] [blame] | 1875 | TypeLoc NewTL = NewTInfo->getTypeLoc().IgnoreParens(); |
Douglas Gregor | 6920cdc | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 1876 | FunctionProtoTypeLoc *NewProtoLoc = cast<FunctionProtoTypeLoc>(&NewTL); |
| 1877 | assert(NewProtoLoc && "Missing prototype?"); |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 1878 | unsigned NewIdx = 0, NumNewParams = NewProtoLoc->getNumArgs(); |
| 1879 | for (unsigned OldIdx = 0, NumOldParams = OldProtoLoc->getNumArgs(); |
| 1880 | OldIdx != NumOldParams; ++OldIdx) { |
| 1881 | ParmVarDecl *OldParam = OldProtoLoc->getArg(OldIdx); |
| 1882 | if (!OldParam->isParameterPack() || |
| 1883 | (NewIdx < NumNewParams && |
| 1884 | NewProtoLoc->getArg(NewIdx)->isParameterPack())) { |
| 1885 | // Simple case: normal parameter, or a parameter pack that's |
| 1886 | // instantiated to a (still-dependent) parameter pack. |
| 1887 | ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++); |
| 1888 | Params.push_back(NewParam); |
| 1889 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam, |
| 1890 | NewParam); |
| 1891 | continue; |
| 1892 | } |
| 1893 | |
| 1894 | // Parameter pack: make the instantiation an argument pack. |
| 1895 | SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack( |
| 1896 | OldParam); |
Douglas Gregor | 21371ea | 2011-01-11 03:14:20 +0000 | [diff] [blame] | 1897 | unsigned NumArgumentsInExpansion |
| 1898 | = SemaRef.getNumArgumentsInExpansion(OldParam->getType(), |
| 1899 | TemplateArgs); |
| 1900 | while (NumArgumentsInExpansion--) { |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 1901 | ParmVarDecl *NewParam = NewProtoLoc->getArg(NewIdx++); |
| 1902 | Params.push_back(NewParam); |
| 1903 | SemaRef.CurrentInstantiationScope->InstantiatedLocalPackArg(OldParam, |
| 1904 | NewParam); |
| 1905 | } |
Douglas Gregor | 6920cdc | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 1906 | } |
Douglas Gregor | 895162d | 2010-04-30 18:55:50 +0000 | [diff] [blame] | 1907 | } |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1908 | } else { |
| 1909 | // The function type itself was not dependent and therefore no |
| 1910 | // substitution occurred. However, we still need to instantiate |
| 1911 | // the function parameters themselves. |
Abramo Bagnara | 140a2bd | 2010-12-13 22:27:55 +0000 | [diff] [blame] | 1912 | TypeLoc OldTL = OldTInfo->getTypeLoc().IgnoreParens(); |
Douglas Gregor | 6920cdc | 2010-05-03 15:32:18 +0000 | [diff] [blame] | 1913 | if (FunctionProtoTypeLoc *OldProtoLoc |
| 1914 | = dyn_cast<FunctionProtoTypeLoc>(&OldTL)) { |
| 1915 | for (unsigned i = 0, i_end = OldProtoLoc->getNumArgs(); i != i_end; ++i) { |
| 1916 | ParmVarDecl *Parm = VisitParmVarDecl(OldProtoLoc->getArg(i)); |
| 1917 | if (!Parm) |
| 1918 | return 0; |
| 1919 | Params.push_back(Parm); |
| 1920 | } |
Douglas Gregor | cb27b0f | 2010-04-12 07:48:19 +0000 | [diff] [blame] | 1921 | } |
| 1922 | } |
John McCall | 21ef0fa | 2010-03-11 09:03:00 +0000 | [diff] [blame] | 1923 | return NewTInfo; |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1926 | /// \brief Initializes the common fields of an instantiation function |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1927 | /// declaration (New) from the corresponding fields of its template (Tmpl). |
| 1928 | /// |
| 1929 | /// \returns true if there was an error |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1930 | bool |
| 1931 | TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1932 | FunctionDecl *Tmpl) { |
| 1933 | if (Tmpl->isDeleted()) |
| 1934 | New->setDeleted(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1935 | |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1936 | // If we are performing substituting explicitly-specified template arguments |
| 1937 | // or deduced template arguments into a function template and we reach this |
| 1938 | // 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] | 1939 | // to keeping the new function template specialization. We therefore |
| 1940 | // convert the active template instantiation for the function template |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1941 | // into a template instantiation for this specific function template |
| 1942 | // specialization, which is not a SFINAE context, so that we diagnose any |
| 1943 | // further errors in the declaration itself. |
| 1944 | typedef Sema::ActiveTemplateInstantiation ActiveInstType; |
| 1945 | ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back(); |
| 1946 | if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || |
| 1947 | ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1948 | if (FunctionTemplateDecl *FunTmpl |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1949 | = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1950 | assert(FunTmpl->getTemplatedDecl() == Tmpl && |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1951 | "Deduction from the wrong function template?"); |
Daniel Dunbar | bcbb8bd | 2009-07-16 22:10:11 +0000 | [diff] [blame] | 1952 | (void) FunTmpl; |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1953 | ActiveInst.Kind = ActiveInstType::TemplateInstantiation; |
| 1954 | ActiveInst.Entity = reinterpret_cast<uintptr_t>(New); |
Douglas Gregor | f35f828 | 2009-11-11 21:54:23 +0000 | [diff] [blame] | 1955 | --SemaRef.NonInstantiationEntries; |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1956 | } |
| 1957 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1958 | |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 1959 | const FunctionProtoType *Proto = Tmpl->getType()->getAs<FunctionProtoType>(); |
| 1960 | assert(Proto && "Function template without prototype?"); |
| 1961 | |
| 1962 | if (Proto->hasExceptionSpec() || Proto->hasAnyExceptionSpec() || |
| 1963 | Proto->getNoReturnAttr()) { |
| 1964 | // The function has an exception specification or a "noreturn" |
| 1965 | // attribute. Substitute into each of the exception types. |
| 1966 | llvm::SmallVector<QualType, 4> Exceptions; |
| 1967 | for (unsigned I = 0, N = Proto->getNumExceptions(); I != N; ++I) { |
| 1968 | // FIXME: Poor location information! |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 1969 | if (const PackExpansionType *PackExpansion |
| 1970 | = Proto->getExceptionType(I)->getAs<PackExpansionType>()) { |
| 1971 | // We have a pack expansion. Instantiate it. |
| 1972 | llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 1973 | SemaRef.collectUnexpandedParameterPacks(PackExpansion->getPattern(), |
| 1974 | Unexpanded); |
| 1975 | assert(!Unexpanded.empty() && |
| 1976 | "Pack expansion without parameter packs?"); |
| 1977 | |
| 1978 | bool Expand = false; |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 1979 | bool RetainExpansion = false; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 1980 | llvm::Optional<unsigned> NumExpansions |
| 1981 | = PackExpansion->getNumExpansions(); |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 1982 | if (SemaRef.CheckParameterPacksForExpansion(New->getLocation(), |
| 1983 | SourceRange(), |
| 1984 | Unexpanded.data(), |
| 1985 | Unexpanded.size(), |
| 1986 | TemplateArgs, |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 1987 | Expand, |
| 1988 | RetainExpansion, |
| 1989 | NumExpansions)) |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 1990 | break; |
| 1991 | |
| 1992 | if (!Expand) { |
| 1993 | // We can't expand this pack expansion into separate arguments yet; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 1994 | // just substitute into the pattern and create a new pack expansion |
| 1995 | // type. |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 1996 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, -1); |
| 1997 | QualType T = SemaRef.SubstType(PackExpansion->getPattern(), |
| 1998 | TemplateArgs, |
| 1999 | New->getLocation(), New->getDeclName()); |
| 2000 | if (T.isNull()) |
| 2001 | break; |
| 2002 | |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2003 | T = SemaRef.Context.getPackExpansionType(T, NumExpansions); |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 2004 | Exceptions.push_back(T); |
| 2005 | continue; |
| 2006 | } |
| 2007 | |
| 2008 | // Substitute into the pack expansion pattern for each template |
| 2009 | bool Invalid = false; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2010 | for (unsigned ArgIdx = 0; ArgIdx != *NumExpansions; ++ArgIdx) { |
Douglas Gregor | b99268b | 2010-12-21 00:52:54 +0000 | [diff] [blame] | 2011 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, ArgIdx); |
| 2012 | |
| 2013 | QualType T = SemaRef.SubstType(PackExpansion->getPattern(), |
| 2014 | TemplateArgs, |
| 2015 | New->getLocation(), New->getDeclName()); |
| 2016 | if (T.isNull()) { |
| 2017 | Invalid = true; |
| 2018 | break; |
| 2019 | } |
| 2020 | |
| 2021 | Exceptions.push_back(T); |
| 2022 | } |
| 2023 | |
| 2024 | if (Invalid) |
| 2025 | break; |
| 2026 | |
| 2027 | continue; |
| 2028 | } |
| 2029 | |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 2030 | QualType T |
| 2031 | = SemaRef.SubstType(Proto->getExceptionType(I), TemplateArgs, |
| 2032 | New->getLocation(), New->getDeclName()); |
| 2033 | if (T.isNull() || |
| 2034 | SemaRef.CheckSpecifiedExceptionType(T, New->getLocation())) |
| 2035 | continue; |
| 2036 | |
| 2037 | Exceptions.push_back(T); |
| 2038 | } |
| 2039 | |
| 2040 | // Rebuild the function type |
| 2041 | |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2042 | FunctionProtoType::ExtProtoInfo EPI = Proto->getExtProtoInfo(); |
| 2043 | EPI.HasExceptionSpec = Proto->hasExceptionSpec(); |
| 2044 | EPI.HasAnyExceptionSpec = Proto->hasAnyExceptionSpec(); |
| 2045 | EPI.NumExceptions = Exceptions.size(); |
| 2046 | EPI.Exceptions = Exceptions.data(); |
| 2047 | EPI.ExtInfo = Proto->getExtInfo(); |
| 2048 | |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 2049 | const FunctionProtoType *NewProto |
| 2050 | = New->getType()->getAs<FunctionProtoType>(); |
| 2051 | assert(NewProto && "Template instantiation without function prototype?"); |
| 2052 | New->setType(SemaRef.Context.getFunctionType(NewProto->getResultType(), |
| 2053 | NewProto->arg_type_begin(), |
| 2054 | NewProto->getNumArgs(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 2055 | EPI)); |
Douglas Gregor | 0ae7b3f | 2009-12-08 17:45:32 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
John McCall | 1d8d1cc | 2010-08-01 02:01:53 +0000 | [diff] [blame] | 2058 | SemaRef.InstantiateAttrs(TemplateArgs, Tmpl, New); |
Douglas Gregor | 7cf84d6 | 2010-06-15 17:05:35 +0000 | [diff] [blame] | 2059 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2060 | return false; |
| 2061 | } |
| 2062 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2063 | /// \brief Initializes common fields of an instantiated method |
| 2064 | /// declaration (New) from the corresponding fields of its template |
| 2065 | /// (Tmpl). |
| 2066 | /// |
| 2067 | /// \returns true if there was an error |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2068 | bool |
| 2069 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2070 | CXXMethodDecl *Tmpl) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2071 | if (InitFunctionInstantiation(New, Tmpl)) |
| 2072 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2073 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2074 | New->setAccess(Tmpl->getAccess()); |
Fariborz Jahanian | e7184df | 2009-12-03 18:44:40 +0000 | [diff] [blame] | 2075 | if (Tmpl->isVirtualAsWritten()) |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 2076 | New->setVirtualAsWritten(true); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 2077 | |
| 2078 | // FIXME: attributes |
| 2079 | // FIXME: New needs a pointer to Tmpl |
| 2080 | return false; |
| 2081 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2082 | |
| 2083 | /// \brief Instantiate the definition of the given function from its |
| 2084 | /// template. |
| 2085 | /// |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2086 | /// \param PointOfInstantiation the point at which the instantiation was |
| 2087 | /// required. Note that this is not precisely a "point of instantiation" |
| 2088 | /// for the function, but it's close. |
| 2089 | /// |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2090 | /// \param Function the already-instantiated declaration of a |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2091 | /// function template specialization or member function of a class template |
| 2092 | /// specialization. |
| 2093 | /// |
| 2094 | /// \param Recursive if true, recursively instantiates any functions that |
| 2095 | /// are required by this instantiation. |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2096 | /// |
| 2097 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 2098 | /// instantiation where the body of the function is required. Complain if |
| 2099 | /// there is no such body. |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 2100 | void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2101 | FunctionDecl *Function, |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2102 | bool Recursive, |
| 2103 | bool DefinitionRequired) { |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2104 | if (Function->isInvalidDecl() || Function->hasBody()) |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 2105 | return; |
| 2106 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2107 | // Never instantiate an explicit specialization. |
| 2108 | if (Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
| 2109 | return; |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 2110 | |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2111 | // Find the function body that we'll be substituting. |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2112 | const FunctionDecl *PatternDecl = Function->getTemplateInstantiationPattern(); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2113 | Stmt *Pattern = 0; |
| 2114 | if (PatternDecl) |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 2115 | Pattern = PatternDecl->getBody(PatternDecl); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2116 | |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2117 | if (!Pattern) { |
| 2118 | if (DefinitionRequired) { |
| 2119 | if (Function->getPrimaryTemplate()) |
| 2120 | Diag(PointOfInstantiation, |
| 2121 | diag::err_explicit_instantiation_undefined_func_template) |
| 2122 | << Function->getPrimaryTemplate(); |
| 2123 | else |
| 2124 | Diag(PointOfInstantiation, |
| 2125 | diag::err_explicit_instantiation_undefined_member) |
| 2126 | << 1 << Function->getDeclName() << Function->getDeclContext(); |
| 2127 | |
| 2128 | if (PatternDecl) |
| 2129 | Diag(PatternDecl->getLocation(), |
| 2130 | diag::note_explicit_instantiation_here); |
Douglas Gregor | cfe833b | 2010-05-17 17:57:54 +0000 | [diff] [blame] | 2131 | Function->setInvalidDecl(); |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 2132 | } else if (Function->getTemplateSpecializationKind() |
| 2133 | == TSK_ExplicitInstantiationDefinition) { |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2134 | PendingInstantiations.push_back( |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 2135 | std::make_pair(Function, PointOfInstantiation)); |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2136 | } |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 2137 | |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2138 | return; |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2139 | } |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 2140 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2141 | // C++0x [temp.explicit]p9: |
| 2142 | // Except for inline functions, other explicit instantiation declarations |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2143 | // have the effect of suppressing the implicit instantiation of the entity |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2144 | // to which they refer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2145 | if (Function->getTemplateSpecializationKind() |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2146 | == TSK_ExplicitInstantiationDeclaration && |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 2147 | !PatternDecl->isInlined()) |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2148 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2149 | |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 2150 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); |
| 2151 | if (Inst) |
Douglas Gregor | e7089b0 | 2010-05-03 23:29:10 +0000 | [diff] [blame] | 2152 | return; |
| 2153 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2154 | // If we're performing recursive template instantiation, create our own |
| 2155 | // queue of pending implicit instantiations that we will instantiate later, |
| 2156 | // while we're still within our own instantiation context. |
Nick Lewycky | 2a5f99e | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 2157 | llvm::SmallVector<VTableUse, 16> SavedVTableUses; |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2158 | std::deque<PendingImplicitInstantiation> SavedPendingInstantiations; |
Nick Lewycky | 2a5f99e | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 2159 | if (Recursive) { |
| 2160 | VTableUses.swap(SavedVTableUses); |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2161 | PendingInstantiations.swap(SavedPendingInstantiations); |
Nick Lewycky | 2a5f99e | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 2162 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2163 | |
Douglas Gregor | 9679caf | 2010-05-12 17:27:19 +0000 | [diff] [blame] | 2164 | EnterExpressionEvaluationContext EvalContext(*this, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2165 | Sema::PotentiallyEvaluated); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2166 | ActOnStartOfFunctionDef(0, Function); |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 2167 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 2168 | // Introduce a new scope where local variable instantiations will be |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 2169 | // recorded, unless we're actually a member function within a local |
| 2170 | // class, in which case we need to merge our results with the parent |
| 2171 | // scope (of the enclosing function). |
| 2172 | bool MergeWithParentScope = false; |
| 2173 | if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Function->getDeclContext())) |
| 2174 | MergeWithParentScope = Rec->isLocalClass(); |
| 2175 | |
| 2176 | LocalInstantiationScope Scope(*this, MergeWithParentScope); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2177 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 2178 | // Introduce the instantiated function parameters into the local |
Peter Collingbourne | 8a6c0f1 | 2010-07-18 16:45:46 +0000 | [diff] [blame] | 2179 | // instantiation scope, and set the parameter names to those used |
| 2180 | // in the template. |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2181 | unsigned FParamIdx = 0; |
Peter Collingbourne | 8a6c0f1 | 2010-07-18 16:45:46 +0000 | [diff] [blame] | 2182 | for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) { |
| 2183 | const ParmVarDecl *PatternParam = PatternDecl->getParamDecl(I); |
Douglas Gregor | 12c9c00 | 2011-01-07 16:43:16 +0000 | [diff] [blame] | 2184 | if (!PatternParam->isParameterPack()) { |
| 2185 | // Simple case: not a parameter pack. |
| 2186 | assert(FParamIdx < Function->getNumParams()); |
| 2187 | ParmVarDecl *FunctionParam = Function->getParamDecl(I); |
| 2188 | FunctionParam->setDeclName(PatternParam->getDeclName()); |
| 2189 | Scope.InstantiatedLocal(PatternParam, FunctionParam); |
| 2190 | ++FParamIdx; |
| 2191 | continue; |
| 2192 | } |
| 2193 | |
| 2194 | // Expand the parameter pack. |
| 2195 | Scope.MakeInstantiatedLocalArgPack(PatternParam); |
| 2196 | for (unsigned NumFParams = Function->getNumParams(); |
| 2197 | FParamIdx < NumFParams; |
| 2198 | ++FParamIdx) { |
| 2199 | ParmVarDecl *FunctionParam = Function->getParamDecl(FParamIdx); |
| 2200 | FunctionParam->setDeclName(PatternParam->getDeclName()); |
| 2201 | Scope.InstantiatedLocalPackArg(PatternParam, FunctionParam); |
| 2202 | } |
Peter Collingbourne | 8a6c0f1 | 2010-07-18 16:45:46 +0000 | [diff] [blame] | 2203 | } |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 2204 | |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 2205 | // Enter the scope of this instantiation. We don't use |
| 2206 | // PushDeclContext because we don't have a scope. |
| 2207 | DeclContext *PreviousContext = CurContext; |
| 2208 | CurContext = Function; |
| 2209 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2210 | MultiLevelTemplateArgumentList TemplateArgs = |
Douglas Gregor | e7089b0 | 2010-05-03 23:29:10 +0000 | [diff] [blame] | 2211 | getTemplateInstantiationArgs(Function, 0, false, PatternDecl); |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2212 | |
| 2213 | // If this is a constructor, instantiate the member initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2214 | if (const CXXConstructorDecl *Ctor = |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2215 | dyn_cast<CXXConstructorDecl>(PatternDecl)) { |
| 2216 | InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor, |
| 2217 | TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2218 | } |
| 2219 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 2220 | // Instantiate the function body. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2221 | StmtResult Body = SubstStmt(Pattern, TemplateArgs); |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 2222 | |
Douglas Gregor | 52604ab | 2009-09-11 21:19:12 +0000 | [diff] [blame] | 2223 | if (Body.isInvalid()) |
| 2224 | Function->setInvalidDecl(); |
| 2225 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2226 | ActOnFinishFunctionBody(Function, Body.get(), |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 2227 | /*IsInstantiation=*/true); |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 2228 | |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 2229 | PerformDependentDiagnostics(PatternDecl, TemplateArgs); |
| 2230 | |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 2231 | CurContext = PreviousContext; |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 2232 | |
| 2233 | DeclGroupRef DG(Function); |
| 2234 | Consumer.HandleTopLevelDecl(DG); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2235 | |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 2236 | // This class may have local implicit instantiations that need to be |
| 2237 | // instantiation within this scope. |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2238 | PerformPendingInstantiations(/*LocalOnly=*/true); |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 2239 | Scope.Exit(); |
| 2240 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2241 | if (Recursive) { |
Nick Lewycky | 2a5f99e | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 2242 | // Define any pending vtables. |
| 2243 | DefineUsedVTables(); |
| 2244 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2245 | // Instantiate any pending implicit instantiations found during the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2246 | // instantiation of this template. |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2247 | PerformPendingInstantiations(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2248 | |
Nick Lewycky | 2a5f99e | 2010-11-25 00:35:20 +0000 | [diff] [blame] | 2249 | // Restore the set of pending vtables. |
| 2250 | VTableUses.swap(SavedVTableUses); |
| 2251 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2252 | // Restore the set of pending implicit instantiations. |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2253 | PendingInstantiations.swap(SavedPendingInstantiations); |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 2254 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | /// \brief Instantiate the definition of the given variable from its |
| 2258 | /// template. |
| 2259 | /// |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2260 | /// \param PointOfInstantiation the point at which the instantiation was |
| 2261 | /// required. Note that this is not precisely a "point of instantiation" |
| 2262 | /// for the function, but it's close. |
| 2263 | /// |
| 2264 | /// \param Var the already-instantiated declaration of a static member |
| 2265 | /// variable of a class template specialization. |
| 2266 | /// |
| 2267 | /// \param Recursive if true, recursively instantiates any functions that |
| 2268 | /// are required by this instantiation. |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2269 | /// |
| 2270 | /// \param DefinitionRequired if true, then we are performing an explicit |
| 2271 | /// instantiation where an out-of-line definition of the member variable |
| 2272 | /// is required. Complain if there is no such definition. |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2273 | void Sema::InstantiateStaticDataMemberDefinition( |
| 2274 | SourceLocation PointOfInstantiation, |
| 2275 | VarDecl *Var, |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2276 | bool Recursive, |
| 2277 | bool DefinitionRequired) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2278 | if (Var->isInvalidDecl()) |
| 2279 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2280 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2281 | // Find the out-of-line definition of this static data member. |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2282 | VarDecl *Def = Var->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2283 | assert(Def && "This data member was not instantiated from a template?"); |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2284 | assert(Def->isStaticDataMember() && "Not a static data member?"); |
| 2285 | Def = Def->getOutOfLineDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2286 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2287 | if (!Def) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2288 | // We did not find an out-of-line definition of this static data member, |
| 2289 | // 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] | 2290 | // instantiate this definition (or provide a specialization for it) in |
| 2291 | // another translation unit. |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2292 | if (DefinitionRequired) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2293 | Def = Var->getInstantiatedFromStaticDataMember(); |
Douglas Gregor | e2d3a3d | 2009-10-15 14:05:49 +0000 | [diff] [blame] | 2294 | Diag(PointOfInstantiation, |
| 2295 | diag::err_explicit_instantiation_undefined_member) |
| 2296 | << 2 << Var->getDeclName() << Var->getDeclContext(); |
| 2297 | Diag(Def->getLocation(), diag::note_explicit_instantiation_here); |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 2298 | } else if (Var->getTemplateSpecializationKind() |
| 2299 | == TSK_ExplicitInstantiationDefinition) { |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2300 | PendingInstantiations.push_back( |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 2301 | std::make_pair(Var, PointOfInstantiation)); |
| 2302 | } |
| 2303 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2304 | return; |
| 2305 | } |
| 2306 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2307 | // Never instantiate an explicit specialization. |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2308 | if (Var->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2309 | return; |
| 2310 | |
| 2311 | // C++0x [temp.explicit]p9: |
| 2312 | // Except for inline functions, other explicit instantiation declarations |
| 2313 | // have the effect of suppressing the implicit instantiation of the entity |
| 2314 | // to which they refer. |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2315 | if (Var->getTemplateSpecializationKind() |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2316 | == TSK_ExplicitInstantiationDeclaration) |
| 2317 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2318 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2319 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
| 2320 | if (Inst) |
| 2321 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2322 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2323 | // If we're performing recursive template instantiation, create our own |
| 2324 | // queue of pending implicit instantiations that we will instantiate later, |
| 2325 | // while we're still within our own instantiation context. |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2326 | std::deque<PendingImplicitInstantiation> SavedPendingInstantiations; |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2327 | if (Recursive) |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2328 | PendingInstantiations.swap(SavedPendingInstantiations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2329 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2330 | // Enter the scope of this instantiation. We don't use |
| 2331 | // PushDeclContext because we don't have a scope. |
| 2332 | DeclContext *PreviousContext = CurContext; |
| 2333 | CurContext = Var->getDeclContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2334 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2335 | VarDecl *OldVar = Var; |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2336 | Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), |
Nico Weber | 6bb4dcb | 2010-11-28 22:53:37 +0000 | [diff] [blame] | 2337 | getTemplateInstantiationArgs(Var))); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2338 | CurContext = PreviousContext; |
| 2339 | |
| 2340 | if (Var) { |
Douglas Gregor | 583f33b | 2009-10-15 18:07:02 +0000 | [diff] [blame] | 2341 | MemberSpecializationInfo *MSInfo = OldVar->getMemberSpecializationInfo(); |
| 2342 | assert(MSInfo && "Missing member specialization information?"); |
| 2343 | Var->setTemplateSpecializationKind(MSInfo->getTemplateSpecializationKind(), |
| 2344 | MSInfo->getPointOfInstantiation()); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2345 | DeclGroupRef DG(Var); |
| 2346 | Consumer.HandleTopLevelDecl(DG); |
| 2347 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2348 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2349 | if (Recursive) { |
| 2350 | // Instantiate any pending implicit instantiations found during the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2351 | // instantiation of this template. |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2352 | PerformPendingInstantiations(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2353 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2354 | // Restore the set of pending implicit instantiations. |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2355 | PendingInstantiations.swap(SavedPendingInstantiations); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2356 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 2357 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2358 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2359 | void |
| 2360 | Sema::InstantiateMemInitializers(CXXConstructorDecl *New, |
| 2361 | const CXXConstructorDecl *Tmpl, |
| 2362 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2363 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2364 | llvm::SmallVector<MemInitTy*, 4> NewInits; |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2365 | bool AnyErrors = false; |
| 2366 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2367 | // Instantiate all the initializers. |
| 2368 | for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(), |
Douglas Gregor | 72f6d67 | 2009-09-01 21:04:42 +0000 | [diff] [blame] | 2369 | InitsEnd = Tmpl->init_end(); |
| 2370 | Inits != InitsEnd; ++Inits) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2371 | CXXCtorInitializer *Init = *Inits; |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2372 | |
Chandler Carruth | 030ef47 | 2010-09-03 21:54:20 +0000 | [diff] [blame] | 2373 | // Only instantiate written initializers, let Sema re-construct implicit |
| 2374 | // ones. |
| 2375 | if (!Init->isWritten()) |
| 2376 | continue; |
| 2377 | |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 2378 | SourceLocation LParenLoc, RParenLoc; |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 2379 | ASTOwningVector<Expr*> NewArgs(*this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2380 | |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2381 | SourceLocation EllipsisLoc; |
| 2382 | |
| 2383 | if (Init->isPackExpansion()) { |
| 2384 | // This is a pack expansion. We should expand it now. |
| 2385 | TypeLoc BaseTL = Init->getBaseClassInfo()->getTypeLoc(); |
| 2386 | llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 2387 | collectUnexpandedParameterPacks(BaseTL, Unexpanded); |
| 2388 | bool ShouldExpand = false; |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2389 | bool RetainExpansion = false; |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2390 | llvm::Optional<unsigned> NumExpansions; |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2391 | if (CheckParameterPacksForExpansion(Init->getEllipsisLoc(), |
| 2392 | BaseTL.getSourceRange(), |
| 2393 | Unexpanded.data(), |
| 2394 | Unexpanded.size(), |
| 2395 | TemplateArgs, ShouldExpand, |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2396 | RetainExpansion, |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2397 | NumExpansions)) { |
| 2398 | AnyErrors = true; |
| 2399 | New->setInvalidDecl(); |
| 2400 | continue; |
| 2401 | } |
| 2402 | assert(ShouldExpand && "Partial instantiation of base initializer?"); |
| 2403 | |
| 2404 | // Loop over all of the arguments in the argument pack(s), |
Douglas Gregor | cded4f6 | 2011-01-14 17:04:44 +0000 | [diff] [blame] | 2405 | for (unsigned I = 0; I != *NumExpansions; ++I) { |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2406 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I); |
| 2407 | |
| 2408 | // Instantiate the initializer. |
| 2409 | if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs, |
| 2410 | LParenLoc, NewArgs, RParenLoc)) { |
| 2411 | AnyErrors = true; |
| 2412 | break; |
| 2413 | } |
| 2414 | |
| 2415 | // Instantiate the base type. |
| 2416 | TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(), |
| 2417 | TemplateArgs, |
| 2418 | Init->getSourceLocation(), |
| 2419 | New->getDeclName()); |
| 2420 | if (!BaseTInfo) { |
| 2421 | AnyErrors = true; |
| 2422 | break; |
| 2423 | } |
| 2424 | |
| 2425 | // Build the initializer. |
| 2426 | MemInitResult NewInit = BuildBaseInitializer(BaseTInfo->getType(), |
| 2427 | BaseTInfo, |
| 2428 | (Expr **)NewArgs.data(), |
| 2429 | NewArgs.size(), |
| 2430 | Init->getLParenLoc(), |
| 2431 | Init->getRParenLoc(), |
| 2432 | New->getParent(), |
| 2433 | SourceLocation()); |
| 2434 | if (NewInit.isInvalid()) { |
| 2435 | AnyErrors = true; |
| 2436 | break; |
| 2437 | } |
| 2438 | |
| 2439 | NewInits.push_back(NewInit.get()); |
| 2440 | NewArgs.clear(); |
| 2441 | } |
| 2442 | |
| 2443 | continue; |
| 2444 | } |
| 2445 | |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 2446 | // Instantiate the initializer. |
| 2447 | if (InstantiateInitializer(*this, Init->getInit(), TemplateArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 2448 | LParenLoc, NewArgs, RParenLoc)) { |
Douglas Gregor | 6b98b2e | 2010-03-02 07:38:39 +0000 | [diff] [blame] | 2449 | AnyErrors = true; |
| 2450 | continue; |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2451 | } |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2452 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2453 | MemInitResult NewInit; |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2454 | if (Init->isBaseInitializer()) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2455 | TypeSourceInfo *BaseTInfo = SubstType(Init->getBaseClassInfo(), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2456 | TemplateArgs, |
| 2457 | Init->getSourceLocation(), |
| 2458 | New->getDeclName()); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2459 | if (!BaseTInfo) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2460 | AnyErrors = true; |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2461 | New->setInvalidDecl(); |
| 2462 | continue; |
| 2463 | } |
| 2464 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2465 | NewInit = BuildBaseInitializer(BaseTInfo->getType(), BaseTInfo, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2466 | (Expr **)NewArgs.data(), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2467 | NewArgs.size(), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2468 | Init->getLParenLoc(), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2469 | Init->getRParenLoc(), |
Douglas Gregor | 3fb9e4b | 2011-01-04 00:32:56 +0000 | [diff] [blame] | 2470 | New->getParent(), |
| 2471 | EllipsisLoc); |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2472 | } else if (Init->isMemberInitializer()) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2473 | FieldDecl *Member = cast<FieldDecl>(FindInstantiatedDecl( |
| 2474 | Init->getMemberLocation(), |
| 2475 | Init->getMember(), |
| 2476 | TemplateArgs)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2477 | |
| 2478 | NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2479 | NewArgs.size(), |
| 2480 | Init->getSourceLocation(), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2481 | Init->getLParenLoc(), |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2482 | Init->getRParenLoc()); |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2483 | } else if (Init->isIndirectMemberInitializer()) { |
| 2484 | IndirectFieldDecl *IndirectMember = |
| 2485 | cast<IndirectFieldDecl>(FindInstantiatedDecl( |
| 2486 | Init->getMemberLocation(), |
| 2487 | Init->getIndirectMember(), TemplateArgs)); |
| 2488 | |
| 2489 | NewInit = BuildMemberInitializer(IndirectMember, (Expr **)NewArgs.data(), |
| 2490 | NewArgs.size(), |
| 2491 | Init->getSourceLocation(), |
| 2492 | Init->getLParenLoc(), |
| 2493 | Init->getRParenLoc()); |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2496 | if (NewInit.isInvalid()) { |
| 2497 | AnyErrors = true; |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2498 | New->setInvalidDecl(); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2499 | } else { |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2500 | // FIXME: It would be nice if ASTOwningVector had a release function. |
| 2501 | NewArgs.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2502 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2503 | NewInits.push_back((MemInitTy *)NewInit.get()); |
| 2504 | } |
| 2505 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2506 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2507 | // Assign all the initializers to the new constructor. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2508 | ActOnMemInitializers(New, |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2509 | /*FIXME: ColonLoc */ |
| 2510 | SourceLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 2511 | NewInits.data(), NewInits.size(), |
| 2512 | AnyErrors); |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2515 | // TODO: this could be templated if the various decl types used the |
| 2516 | // same method name. |
| 2517 | static bool isInstantiationOf(ClassTemplateDecl *Pattern, |
| 2518 | ClassTemplateDecl *Instance) { |
| 2519 | Pattern = Pattern->getCanonicalDecl(); |
| 2520 | |
| 2521 | do { |
| 2522 | Instance = Instance->getCanonicalDecl(); |
| 2523 | if (Pattern == Instance) return true; |
| 2524 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 2525 | } while (Instance); |
| 2526 | |
| 2527 | return false; |
| 2528 | } |
| 2529 | |
Douglas Gregor | 0d69653 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 2530 | static bool isInstantiationOf(FunctionTemplateDecl *Pattern, |
| 2531 | FunctionTemplateDecl *Instance) { |
| 2532 | Pattern = Pattern->getCanonicalDecl(); |
| 2533 | |
| 2534 | do { |
| 2535 | Instance = Instance->getCanonicalDecl(); |
| 2536 | if (Pattern == Instance) return true; |
| 2537 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 2538 | } while (Instance); |
| 2539 | |
| 2540 | return false; |
| 2541 | } |
| 2542 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2543 | static bool |
| 2544 | isInstantiationOf(ClassTemplatePartialSpecializationDecl *Pattern, |
| 2545 | ClassTemplatePartialSpecializationDecl *Instance) { |
| 2546 | Pattern |
| 2547 | = cast<ClassTemplatePartialSpecializationDecl>(Pattern->getCanonicalDecl()); |
| 2548 | do { |
| 2549 | Instance = cast<ClassTemplatePartialSpecializationDecl>( |
| 2550 | Instance->getCanonicalDecl()); |
| 2551 | if (Pattern == Instance) |
| 2552 | return true; |
| 2553 | Instance = Instance->getInstantiatedFromMember(); |
| 2554 | } while (Instance); |
| 2555 | |
| 2556 | return false; |
| 2557 | } |
| 2558 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2559 | static bool isInstantiationOf(CXXRecordDecl *Pattern, |
| 2560 | CXXRecordDecl *Instance) { |
| 2561 | Pattern = Pattern->getCanonicalDecl(); |
| 2562 | |
| 2563 | do { |
| 2564 | Instance = Instance->getCanonicalDecl(); |
| 2565 | if (Pattern == Instance) return true; |
| 2566 | Instance = Instance->getInstantiatedFromMemberClass(); |
| 2567 | } while (Instance); |
| 2568 | |
| 2569 | return false; |
| 2570 | } |
| 2571 | |
| 2572 | static bool isInstantiationOf(FunctionDecl *Pattern, |
| 2573 | FunctionDecl *Instance) { |
| 2574 | Pattern = Pattern->getCanonicalDecl(); |
| 2575 | |
| 2576 | do { |
| 2577 | Instance = Instance->getCanonicalDecl(); |
| 2578 | if (Pattern == Instance) return true; |
| 2579 | Instance = Instance->getInstantiatedFromMemberFunction(); |
| 2580 | } while (Instance); |
| 2581 | |
| 2582 | return false; |
| 2583 | } |
| 2584 | |
| 2585 | static bool isInstantiationOf(EnumDecl *Pattern, |
| 2586 | EnumDecl *Instance) { |
| 2587 | Pattern = Pattern->getCanonicalDecl(); |
| 2588 | |
| 2589 | do { |
| 2590 | Instance = Instance->getCanonicalDecl(); |
| 2591 | if (Pattern == Instance) return true; |
| 2592 | Instance = Instance->getInstantiatedFromMemberEnum(); |
| 2593 | } while (Instance); |
| 2594 | |
| 2595 | return false; |
| 2596 | } |
| 2597 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2598 | static bool isInstantiationOf(UsingShadowDecl *Pattern, |
| 2599 | UsingShadowDecl *Instance, |
| 2600 | ASTContext &C) { |
| 2601 | return C.getInstantiatedFromUsingShadowDecl(Instance) == Pattern; |
| 2602 | } |
| 2603 | |
| 2604 | static bool isInstantiationOf(UsingDecl *Pattern, |
| 2605 | UsingDecl *Instance, |
| 2606 | ASTContext &C) { |
| 2607 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; |
| 2608 | } |
| 2609 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2610 | static bool isInstantiationOf(UnresolvedUsingValueDecl *Pattern, |
| 2611 | UsingDecl *Instance, |
| 2612 | ASTContext &C) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2613 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | static bool isInstantiationOf(UnresolvedUsingTypenameDecl *Pattern, |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 2617 | UsingDecl *Instance, |
| 2618 | ASTContext &C) { |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2619 | return C.getInstantiatedFromUsingDecl(Instance) == Pattern; |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2622 | static bool isInstantiationOfStaticDataMember(VarDecl *Pattern, |
| 2623 | VarDecl *Instance) { |
| 2624 | assert(Instance->isStaticDataMember()); |
| 2625 | |
| 2626 | Pattern = Pattern->getCanonicalDecl(); |
| 2627 | |
| 2628 | do { |
| 2629 | Instance = Instance->getCanonicalDecl(); |
| 2630 | if (Pattern == Instance) return true; |
| 2631 | Instance = Instance->getInstantiatedFromStaticDataMember(); |
| 2632 | } while (Instance); |
| 2633 | |
| 2634 | return false; |
| 2635 | } |
| 2636 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2637 | // Other is the prospective instantiation |
| 2638 | // D is the prospective pattern |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2639 | static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 2640 | if (D->getKind() != Other->getKind()) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2641 | if (UnresolvedUsingTypenameDecl *UUD |
| 2642 | = dyn_cast<UnresolvedUsingTypenameDecl>(D)) { |
| 2643 | if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { |
| 2644 | return isInstantiationOf(UUD, UD, Ctx); |
| 2645 | } |
| 2646 | } |
| 2647 | |
| 2648 | if (UnresolvedUsingValueDecl *UUD |
| 2649 | = dyn_cast<UnresolvedUsingValueDecl>(D)) { |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 2650 | if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { |
| 2651 | return isInstantiationOf(UUD, UD, Ctx); |
| 2652 | } |
| 2653 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2654 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 2655 | return false; |
| 2656 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2657 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2658 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other)) |
| 2659 | return isInstantiationOf(cast<CXXRecordDecl>(D), Record); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2660 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2661 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other)) |
| 2662 | return isInstantiationOf(cast<FunctionDecl>(D), Function); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2663 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2664 | if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other)) |
| 2665 | return isInstantiationOf(cast<EnumDecl>(D), Enum); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2666 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2667 | if (VarDecl *Var = dyn_cast<VarDecl>(Other)) |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2668 | if (Var->isStaticDataMember()) |
| 2669 | return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var); |
| 2670 | |
| 2671 | if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other)) |
| 2672 | return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp); |
Douglas Gregor | a5bf7f1 | 2009-08-28 22:03:51 +0000 | [diff] [blame] | 2673 | |
Douglas Gregor | 0d69653 | 2009-09-28 06:34:35 +0000 | [diff] [blame] | 2674 | if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(Other)) |
| 2675 | return isInstantiationOf(cast<FunctionTemplateDecl>(D), Temp); |
| 2676 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2677 | if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 2678 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Other)) |
| 2679 | return isInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(D), |
| 2680 | PartialSpec); |
| 2681 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 2682 | if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) { |
| 2683 | if (!Field->getDeclName()) { |
| 2684 | // This is an unnamed field. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2685 | return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) == |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 2686 | cast<FieldDecl>(D); |
| 2687 | } |
| 2688 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2689 | |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2690 | if (UsingDecl *Using = dyn_cast<UsingDecl>(Other)) |
| 2691 | return isInstantiationOf(cast<UsingDecl>(D), Using, Ctx); |
| 2692 | |
| 2693 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(Other)) |
| 2694 | return isInstantiationOf(cast<UsingShadowDecl>(D), Shadow, Ctx); |
| 2695 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2696 | return D->getDeclName() && isa<NamedDecl>(Other) && |
| 2697 | D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); |
| 2698 | } |
| 2699 | |
| 2700 | template<typename ForwardIterator> |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2701 | static NamedDecl *findInstantiationOf(ASTContext &Ctx, |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2702 | NamedDecl *D, |
| 2703 | ForwardIterator first, |
| 2704 | ForwardIterator last) { |
| 2705 | for (; first != last; ++first) |
| 2706 | if (isInstantiationOf(Ctx, D, *first)) |
| 2707 | return cast<NamedDecl>(*first); |
| 2708 | |
| 2709 | return 0; |
| 2710 | } |
| 2711 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 2712 | /// \brief Finds the instantiation of the given declaration context |
| 2713 | /// within the current instantiation. |
| 2714 | /// |
| 2715 | /// \returns NULL if there was an error |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 2716 | DeclContext *Sema::FindInstantiatedContext(SourceLocation Loc, DeclContext* DC, |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2717 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 2718 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) { |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 2719 | Decl* ID = FindInstantiatedDecl(Loc, D, TemplateArgs); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 2720 | return cast_or_null<DeclContext>(ID); |
| 2721 | } else return DC; |
| 2722 | } |
| 2723 | |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 2724 | /// \brief Find the instantiation of the given declaration within the |
| 2725 | /// current instantiation. |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2726 | /// |
| 2727 | /// This routine is intended to be used when \p D is a declaration |
| 2728 | /// referenced from within a template, that needs to mapped into the |
| 2729 | /// corresponding declaration within an instantiation. For example, |
| 2730 | /// given: |
| 2731 | /// |
| 2732 | /// \code |
| 2733 | /// template<typename T> |
| 2734 | /// struct X { |
| 2735 | /// enum Kind { |
| 2736 | /// KnownValue = sizeof(T) |
| 2737 | /// }; |
| 2738 | /// |
| 2739 | /// bool getKind() const { return KnownValue; } |
| 2740 | /// }; |
| 2741 | /// |
| 2742 | /// template struct X<int>; |
| 2743 | /// \endcode |
| 2744 | /// |
| 2745 | /// In the instantiation of X<int>::getKind(), we need to map the |
| 2746 | /// EnumConstantDecl for KnownValue (which refers to |
| 2747 | /// X<T>::<Kind>::KnownValue) to its instantiation |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 2748 | /// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs |
| 2749 | /// this mapping from within the instantiation of X<int>. |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 2750 | NamedDecl *Sema::FindInstantiatedDecl(SourceLocation Loc, NamedDecl *D, |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2751 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2752 | DeclContext *ParentDC = D->getDeclContext(); |
Douglas Gregor | 550d9b2 | 2009-10-31 17:21:17 +0000 | [diff] [blame] | 2753 | if (isa<ParmVarDecl>(D) || isa<NonTypeTemplateParmDecl>(D) || |
Douglas Gregor | 6d3e627 | 2010-02-05 19:54:12 +0000 | [diff] [blame] | 2754 | isa<TemplateTypeParmDecl>(D) || isa<TemplateTemplateParmDecl>(D) || |
John McCall | 7667245 | 2010-08-19 23:06:02 +0000 | [diff] [blame] | 2755 | (ParentDC->isFunctionOrMethod() && ParentDC->isDependentContext())) { |
Douglas Gregor | 2bba76b | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 2756 | // D is a local of some kind. Look into the map of local |
| 2757 | // declarations to their instantiations. |
Fariborz Jahanian | 8dd0c56 | 2010-07-13 00:16:40 +0000 | [diff] [blame] | 2758 | return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D)); |
Douglas Gregor | 2bba76b | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 2759 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2760 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2761 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 2762 | if (!Record->isDependentContext()) |
| 2763 | return D; |
| 2764 | |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 2765 | // If the RecordDecl is actually the injected-class-name or a |
| 2766 | // "templated" declaration for a class template, class template |
| 2767 | // partial specialization, or a member class of a class template, |
| 2768 | // substitute into the injected-class-name of the class template |
| 2769 | // or partial specialization to find the new DeclContext. |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2770 | QualType T; |
| 2771 | ClassTemplateDecl *ClassTemplate = Record->getDescribedClassTemplate(); |
| 2772 | |
| 2773 | if (ClassTemplate) { |
Douglas Gregor | 24bae92 | 2010-07-08 18:37:38 +0000 | [diff] [blame] | 2774 | T = ClassTemplate->getInjectedClassNameSpecialization(); |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2775 | } else if (ClassTemplatePartialSpecializationDecl *PartialSpec |
| 2776 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2777 | ClassTemplate = PartialSpec->getSpecializedTemplate(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2778 | |
| 2779 | // If we call SubstType with an InjectedClassNameType here we |
| 2780 | // can end up in an infinite loop. |
| 2781 | T = Context.getTypeDeclType(Record); |
| 2782 | assert(isa<InjectedClassNameType>(T) && |
| 2783 | "type of partial specialization is not an InjectedClassNameType"); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 2784 | T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2785 | } |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2786 | |
| 2787 | if (!T.isNull()) { |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 2788 | // Substitute into the injected-class-name to get the type |
| 2789 | // corresponding to the instantiation we want, which may also be |
| 2790 | // the current instantiation (if we're in a template |
| 2791 | // definition). This substitution should never fail, since we |
| 2792 | // know we can instantiate the injected-class-name or we |
| 2793 | // wouldn't have gotten to the injected-class-name! |
| 2794 | |
| 2795 | // FIXME: Can we use the CurrentInstantiationScope to avoid this |
| 2796 | // extra instantiation in the common case? |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2797 | T = SubstType(T, TemplateArgs, SourceLocation(), DeclarationName()); |
| 2798 | assert(!T.isNull() && "Instantiation of injected-class-name cannot fail."); |
| 2799 | |
| 2800 | if (!T->isDependentType()) { |
| 2801 | assert(T->isRecordType() && "Instantiation must produce a record type"); |
| 2802 | return T->getAs<RecordType>()->getDecl(); |
| 2803 | } |
| 2804 | |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 2805 | // We are performing "partial" template instantiation to create |
| 2806 | // the member declarations for the members of a class template |
| 2807 | // specialization. Therefore, D is actually referring to something |
| 2808 | // in the current instantiation. Look through the current |
| 2809 | // context, which contains actual instantiations, to find the |
| 2810 | // instantiation of the "current instantiation" that D refers |
| 2811 | // to. |
| 2812 | bool SawNonDependentContext = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2813 | for (DeclContext *DC = CurContext; !DC->isFileContext(); |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2814 | DC = DC->getParent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2815 | if (ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 2816 | = dyn_cast<ClassTemplateSpecializationDecl>(DC)) |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2817 | if (isInstantiationOf(ClassTemplate, |
| 2818 | Spec->getSpecializedTemplate())) |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2819 | return Spec; |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 2820 | |
| 2821 | if (!DC->isDependentContext()) |
| 2822 | SawNonDependentContext = true; |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 2825 | // We're performing "instantiation" of a member of the current |
| 2826 | // instantiation while we are type-checking the |
| 2827 | // definition. Compute the declaration context and return that. |
| 2828 | assert(!SawNonDependentContext && |
| 2829 | "No dependent context while instantiating record"); |
| 2830 | DeclContext *DC = computeDeclContext(T); |
| 2831 | assert(DC && |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2832 | "Unable to find declaration for the current instantiation"); |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 2833 | return cast<CXXRecordDecl>(DC); |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2834 | } |
Douglas Gregor | 8b013bd | 2010-02-05 22:40:03 +0000 | [diff] [blame] | 2835 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2836 | // Fall through to deal with other dependent record types (e.g., |
| 2837 | // anonymous unions in class templates). |
| 2838 | } |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 2839 | |
Douglas Gregor | e95b409 | 2009-09-16 18:34:49 +0000 | [diff] [blame] | 2840 | if (!ParentDC->isDependentContext()) |
| 2841 | return D; |
| 2842 | |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 2843 | ParentDC = FindInstantiatedContext(Loc, ParentDC, TemplateArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2844 | if (!ParentDC) |
Douglas Gregor | 44c7384 | 2009-09-01 17:53:10 +0000 | [diff] [blame] | 2845 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2846 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2847 | if (ParentDC != D->getDeclContext()) { |
| 2848 | // We performed some kind of instantiation in the parent context, |
| 2849 | // so now we need to look into the instantiated parent context to |
| 2850 | // find the instantiation of the declaration D. |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 2851 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2852 | // If our context used to be dependent, we may need to instantiate |
| 2853 | // it before performing lookup into that context. |
| 2854 | if (CXXRecordDecl *Spec = dyn_cast<CXXRecordDecl>(ParentDC)) { |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 2855 | if (!Spec->isDependentContext()) { |
| 2856 | QualType T = Context.getTypeDeclType(Spec); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2857 | const RecordType *Tag = T->getAs<RecordType>(); |
| 2858 | assert(Tag && "type of non-dependent record is not a RecordType"); |
| 2859 | if (!Tag->isBeingDefined() && |
| 2860 | RequireCompleteType(Loc, T, diag::err_incomplete_type)) |
| 2861 | return 0; |
Douglas Gregor | a43064c | 2010-11-05 23:22:45 +0000 | [diff] [blame] | 2862 | |
| 2863 | ParentDC = Tag->getDecl(); |
Douglas Gregor | 7c1e98f | 2010-03-01 15:56:25 +0000 | [diff] [blame] | 2864 | } |
| 2865 | } |
| 2866 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2867 | NamedDecl *Result = 0; |
| 2868 | if (D->getDeclName()) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2869 | DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName()); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2870 | Result = findInstantiationOf(Context, D, Found.first, Found.second); |
| 2871 | } else { |
| 2872 | // Since we don't have a name for the entity we're looking for, |
| 2873 | // our only option is to walk through all of the declarations to |
| 2874 | // find that name. This will occur in a few cases: |
| 2875 | // |
| 2876 | // - anonymous struct/union within a template |
| 2877 | // - unnamed class/struct/union/enum within a template |
| 2878 | // |
| 2879 | // FIXME: Find a better way to find these instantiations! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2880 | Result = findInstantiationOf(Context, D, |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2881 | ParentDC->decls_begin(), |
| 2882 | ParentDC->decls_end()); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2883 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2884 | |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2885 | // UsingShadowDecls can instantiate to nothing because of using hiding. |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 2886 | assert((Result || isa<UsingShadowDecl>(D) || D->isInvalidDecl() || |
| 2887 | cast<Decl>(ParentDC)->isInvalidDecl()) |
John McCall | 9f54ad4 | 2009-12-10 09:41:52 +0000 | [diff] [blame] | 2888 | && "Unable to find instantiation of declaration!"); |
| 2889 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2890 | D = Result; |
| 2891 | } |
| 2892 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 2893 | return D; |
| 2894 | } |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2895 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2896 | /// \brief Performs template instantiation for all implicit template |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2897 | /// instantiations we have seen until this point. |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2898 | void Sema::PerformPendingInstantiations(bool LocalOnly) { |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 2899 | while (!PendingLocalImplicitInstantiations.empty() || |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2900 | (!LocalOnly && !PendingInstantiations.empty())) { |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 2901 | PendingImplicitInstantiation Inst; |
| 2902 | |
| 2903 | if (PendingLocalImplicitInstantiations.empty()) { |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 2904 | Inst = PendingInstantiations.front(); |
| 2905 | PendingInstantiations.pop_front(); |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 2906 | } else { |
| 2907 | Inst = PendingLocalImplicitInstantiations.front(); |
| 2908 | PendingLocalImplicitInstantiations.pop_front(); |
| 2909 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2910 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2911 | // Instantiate function definitions |
| 2912 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2913 | PrettyDeclStackTraceEntry CrashInfo(*this, Function, SourceLocation(), |
| 2914 | "instantiating function definition"); |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 2915 | bool DefinitionRequired = Function->getTemplateSpecializationKind() == |
| 2916 | TSK_ExplicitInstantiationDefinition; |
| 2917 | InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true, |
| 2918 | DefinitionRequired); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2919 | continue; |
| 2920 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2921 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2922 | // Instantiate static data member definitions. |
| 2923 | VarDecl *Var = cast<VarDecl>(Inst.first); |
| 2924 | assert(Var->isStaticDataMember() && "Not a static data member?"); |
Anders Carlsson | c17fb7b | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 2925 | |
Chandler Carruth | 291b441 | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 2926 | // Don't try to instantiate declarations if the most recent redeclaration |
| 2927 | // is invalid. |
| 2928 | if (Var->getMostRecentDeclaration()->isInvalidDecl()) |
| 2929 | continue; |
| 2930 | |
| 2931 | // Check if the most recent declaration has changed the specialization kind |
| 2932 | // and removed the need for implicit instantiation. |
| 2933 | switch (Var->getMostRecentDeclaration()->getTemplateSpecializationKind()) { |
| 2934 | case TSK_Undeclared: |
| 2935 | assert(false && "Cannot instantitiate an undeclared specialization."); |
| 2936 | case TSK_ExplicitInstantiationDeclaration: |
Chandler Carruth | 291b441 | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 2937 | case TSK_ExplicitSpecialization: |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 2938 | continue; // No longer need to instantiate this type. |
| 2939 | case TSK_ExplicitInstantiationDefinition: |
| 2940 | // We only need an instantiation if the pending instantiation *is* the |
| 2941 | // explicit instantiation. |
| 2942 | if (Var != Var->getMostRecentDeclaration()) continue; |
Chandler Carruth | 291b441 | 2010-02-13 10:17:50 +0000 | [diff] [blame] | 2943 | case TSK_ImplicitInstantiation: |
| 2944 | break; |
| 2945 | } |
| 2946 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2947 | PrettyDeclStackTraceEntry CrashInfo(*this, Var, Var->getLocation(), |
| 2948 | "instantiating static data member " |
| 2949 | "definition"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2950 | |
Chandler Carruth | 58e390e | 2010-08-25 08:27:02 +0000 | [diff] [blame] | 2951 | bool DefinitionRequired = Var->getTemplateSpecializationKind() == |
| 2952 | TSK_ExplicitInstantiationDefinition; |
| 2953 | InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true, |
| 2954 | DefinitionRequired); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2955 | } |
| 2956 | } |
John McCall | 0c01d18 | 2010-03-24 05:22:00 +0000 | [diff] [blame] | 2957 | |
| 2958 | void Sema::PerformDependentDiagnostics(const DeclContext *Pattern, |
| 2959 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 2960 | for (DeclContext::ddiag_iterator I = Pattern->ddiag_begin(), |
| 2961 | E = Pattern->ddiag_end(); I != E; ++I) { |
| 2962 | DependentDiagnostic *DD = *I; |
| 2963 | |
| 2964 | switch (DD->getKind()) { |
| 2965 | case DependentDiagnostic::Access: |
| 2966 | HandleDependentAccessCheck(*DD, TemplateArgs); |
| 2967 | break; |
| 2968 | } |
| 2969 | } |
| 2970 | } |