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