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