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 | //===----------------------------------------------------------------------===/ |
| 12 | #include "Sema.h" |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/DeclTemplate.h" |
| 16 | #include "clang/AST/DeclVisitor.h" |
| 17 | #include "clang/AST/Expr.h" |
Anders Carlsson | c17fb7b | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 18 | #include "clang/Basic/PrettyStackTrace.h" |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
| 21 | |
| 22 | using namespace clang; |
| 23 | |
| 24 | namespace { |
| 25 | class VISIBILITY_HIDDEN TemplateDeclInstantiator |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 26 | : public DeclVisitor<TemplateDeclInstantiator, Decl *> { |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 27 | Sema &SemaRef; |
| 28 | DeclContext *Owner; |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 29 | const MultiLevelTemplateArgumentList &TemplateArgs; |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 30 | |
| 31 | public: |
| 32 | typedef Sema::OwningExprResult OwningExprResult; |
| 33 | |
| 34 | TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 35 | const MultiLevelTemplateArgumentList &TemplateArgs) |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 36 | : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { } |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 37 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 38 | // FIXME: Once we get closer to completion, replace these manually-written |
| 39 | // declarations with automatically-generated ones from |
| 40 | // clang/AST/DeclNodes.def. |
Douglas Gregor | 4f722be | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 41 | Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 42 | Decl *VisitNamespaceDecl(NamespaceDecl *D); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 43 | Decl *VisitTypedefDecl(TypedefDecl *D); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 44 | Decl *VisitVarDecl(VarDecl *D); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 45 | Decl *VisitFieldDecl(FieldDecl *D); |
| 46 | Decl *VisitStaticAssertDecl(StaticAssertDecl *D); |
| 47 | Decl *VisitEnumDecl(EnumDecl *D); |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 48 | Decl *VisitEnumConstantDecl(EnumConstantDecl *D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 49 | Decl *VisitFriendDecl(FriendDecl *D); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 50 | Decl *VisitFunctionDecl(FunctionDecl *D); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 51 | Decl *VisitCXXRecordDecl(CXXRecordDecl *D); |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 52 | Decl *VisitCXXMethodDecl(CXXMethodDecl *D, |
| 53 | TemplateParameterList *TemplateParams = 0); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 54 | Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 55 | Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 56 | Decl *VisitCXXConversionDecl(CXXConversionDecl *D); |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 57 | ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 58 | Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 59 | Decl *VisitClassTemplateDecl(ClassTemplateDecl *D); |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 60 | Decl *VisitFunctionTemplateDecl(FunctionTemplateDecl *D); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 61 | Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 62 | Decl *VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D); |
| 63 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 64 | // Base case. FIXME: Remove once we can instantiate everything. |
| 65 | Decl *VisitDecl(Decl *) { |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 66 | assert(false && "Template instantiation of unknown declaration kind!"); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 67 | return 0; |
| 68 | } |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 69 | |
John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 70 | const LangOptions &getLangOptions() { |
| 71 | return SemaRef.getLangOptions(); |
| 72 | } |
| 73 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 74 | // Helper functions for instantiating methods. |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 75 | QualType SubstFunctionType(FunctionDecl *D, |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 76 | llvm::SmallVectorImpl<ParmVarDecl *> &Params); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 77 | bool InitFunctionInstantiation(FunctionDecl *New, FunctionDecl *Tmpl); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 78 | bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 79 | |
| 80 | TemplateParameterList * |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 81 | SubstTemplateParams(TemplateParameterList *List); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 82 | }; |
| 83 | } |
| 84 | |
Douglas Gregor | 4f722be | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 85 | Decl * |
| 86 | TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 87 | assert(false && "Translation units cannot be instantiated"); |
| 88 | return D; |
| 89 | } |
| 90 | |
| 91 | Decl * |
| 92 | TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { |
| 93 | assert(false && "Namespaces cannot be instantiated"); |
| 94 | return D; |
| 95 | } |
| 96 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 97 | Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { |
| 98 | bool Invalid = false; |
| 99 | QualType T = D->getUnderlyingType(); |
| 100 | if (T->isDependentType()) { |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 101 | T = SemaRef.SubstType(T, TemplateArgs, |
| 102 | D->getLocation(), D->getDeclName()); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 103 | if (T.isNull()) { |
| 104 | Invalid = true; |
| 105 | T = SemaRef.Context.IntTy; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Create the new typedef |
| 110 | TypedefDecl *Typedef |
| 111 | = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 112 | D->getIdentifier(), T); |
| 113 | if (Invalid) |
| 114 | Typedef->setInvalidDecl(); |
| 115 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 116 | Owner->addDecl(Typedef); |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 117 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 118 | return Typedef; |
| 119 | } |
| 120 | |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 121 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 122 | // Do substitution on the type of the declaration |
| 123 | QualType T = SemaRef.SubstType(D->getType(), TemplateArgs, |
| 124 | D->getTypeSpecStartLoc(), |
| 125 | D->getDeclName()); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 126 | if (T.isNull()) |
| 127 | return 0; |
| 128 | |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 129 | // Build the instantiated declaration |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 130 | VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner, |
| 131 | D->getLocation(), D->getIdentifier(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 132 | T, D->getDeclaratorInfo(), |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 133 | D->getStorageClass()); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 134 | Var->setThreadSpecified(D->isThreadSpecified()); |
| 135 | Var->setCXXDirectInitializer(D->hasCXXDirectInitializer()); |
| 136 | Var->setDeclaredInCondition(D->isDeclaredInCondition()); |
| 137 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 138 | // If we are instantiating a static data member defined |
| 139 | // out-of-line, the instantiation will have the same lexical |
| 140 | // context (which will be a namespace scope) as the template. |
| 141 | if (D->isOutOfLine()) |
| 142 | Var->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 143 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 144 | // FIXME: In theory, we could have a previous declaration for variables that |
| 145 | // are not static data members. |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 146 | bool Redeclaration = false; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 147 | SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 148 | |
| 149 | if (D->isOutOfLine()) { |
| 150 | D->getLexicalDeclContext()->addDecl(Var); |
| 151 | Owner->makeDeclVisibleInContext(Var); |
| 152 | } else { |
| 153 | Owner->addDecl(Var); |
| 154 | } |
| 155 | |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 156 | if (D->getInit()) { |
| 157 | OwningExprResult Init |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 158 | = SemaRef.SubstExpr(D->getInit(), TemplateArgs); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 159 | if (Init.isInvalid()) |
| 160 | Var->setInvalidDecl(); |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 161 | else if (ParenListExpr *PLE = dyn_cast<ParenListExpr>((Expr *)Init.get())) { |
| 162 | // FIXME: We're faking all of the comma locations, which is suboptimal. |
| 163 | // Do we even need these comma locations? |
| 164 | llvm::SmallVector<SourceLocation, 4> FakeCommaLocs; |
| 165 | if (PLE->getNumExprs() > 0) { |
| 166 | FakeCommaLocs.reserve(PLE->getNumExprs() - 1); |
| 167 | for (unsigned I = 0, N = PLE->getNumExprs() - 1; I != N; ++I) { |
| 168 | Expr *E = PLE->getExpr(I)->Retain(); |
| 169 | FakeCommaLocs.push_back( |
| 170 | SemaRef.PP.getLocForEndOfToken(E->getLocEnd())); |
| 171 | } |
Douglas Gregor | e9f8eb6 | 2009-08-26 23:26:04 +0000 | [diff] [blame] | 172 | PLE->getExpr(PLE->getNumExprs() - 1)->Retain(); |
Douglas Gregor | 83ddad3 | 2009-08-26 21:14:46 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // Add the direct initializer to the declaration. |
| 176 | SemaRef.AddCXXDirectInitializerToDecl(Sema::DeclPtrTy::make(Var), |
| 177 | PLE->getLParenLoc(), |
| 178 | Sema::MultiExprArg(SemaRef, |
| 179 | (void**)PLE->getExprs(), |
| 180 | PLE->getNumExprs()), |
| 181 | FakeCommaLocs.data(), |
| 182 | PLE->getRParenLoc()); |
| 183 | |
| 184 | // When Init is destroyed, it will destroy the instantiated ParenListExpr; |
| 185 | // we've explicitly retained all of its subexpressions already. |
| 186 | } else |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 187 | SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init), |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 188 | D->hasCXXDirectInitializer()); |
Douglas Gregor | 65b9005 | 2009-07-27 17:43:39 +0000 | [diff] [blame] | 189 | } else if (!Var->isStaticDataMember() || Var->isOutOfLine()) |
| 190 | SemaRef.ActOnUninitializedDecl(Sema::DeclPtrTy::make(Var), false); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 191 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 192 | // Link instantiations of static data members back to the template from |
| 193 | // which they were instantiated. |
| 194 | if (Var->isStaticDataMember()) |
| 195 | SemaRef.Context.setInstantiatedFromStaticDataMember(Var, D); |
| 196 | |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 197 | return Var; |
| 198 | } |
| 199 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 200 | Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { |
| 201 | bool Invalid = false; |
| 202 | QualType T = D->getType(); |
| 203 | if (T->isDependentType()) { |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 204 | T = SemaRef.SubstType(T, TemplateArgs, |
| 205 | D->getLocation(), D->getDeclName()); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 206 | if (!T.isNull() && T->isFunctionType()) { |
| 207 | // C++ [temp.arg.type]p3: |
| 208 | // If a declaration acquires a function type through a type |
| 209 | // dependent on a template-parameter and this causes a |
| 210 | // declaration that does not use the syntactic form of a |
| 211 | // function declarator to have function type, the program is |
| 212 | // ill-formed. |
| 213 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
| 214 | << T; |
| 215 | T = QualType(); |
| 216 | Invalid = true; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | Expr *BitWidth = D->getBitWidth(); |
| 221 | if (Invalid) |
| 222 | BitWidth = 0; |
| 223 | else if (BitWidth) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 224 | // The bit-width expression is not potentially evaluated. |
| 225 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated); |
| 226 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 227 | OwningExprResult InstantiatedBitWidth |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 228 | = SemaRef.SubstExpr(BitWidth, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 229 | if (InstantiatedBitWidth.isInvalid()) { |
| 230 | Invalid = true; |
| 231 | BitWidth = 0; |
| 232 | } else |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 233 | BitWidth = InstantiatedBitWidth.takeAs<Expr>(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 237 | D->getDeclaratorInfo(), |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 238 | cast<RecordDecl>(Owner), |
| 239 | D->getLocation(), |
| 240 | D->isMutable(), |
| 241 | BitWidth, |
Steve Naroff | ea218b8 | 2009-07-14 14:58:18 +0000 | [diff] [blame] | 242 | D->getTypeSpecStartLoc(), |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 243 | D->getAccess(), |
| 244 | 0); |
| 245 | if (Field) { |
| 246 | if (Invalid) |
| 247 | Field->setInvalidDecl(); |
| 248 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 249 | if (!Field->getDeclName()) { |
| 250 | // Keep track of where this decl came from. |
| 251 | SemaRef.Context.setInstantiatedFromUnnamedFieldDecl(Field, D); |
| 252 | } |
| 253 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 254 | Owner->addDecl(Field); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | return Field; |
| 258 | } |
| 259 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 260 | Decl *TemplateDeclInstantiator::VisitFriendDecl(FriendDecl *D) { |
| 261 | FriendDecl::FriendUnion FU; |
| 262 | |
| 263 | // Handle friend type expressions by simply substituting template |
| 264 | // parameters into the pattern type. |
| 265 | if (Type *Ty = D->getFriendType()) { |
| 266 | QualType T = SemaRef.SubstType(QualType(Ty,0), TemplateArgs, |
| 267 | D->getLocation(), DeclarationName()); |
| 268 | if (T.isNull()) return 0; |
| 269 | |
| 270 | assert(getLangOptions().CPlusPlus0x || T->isRecordType()); |
| 271 | FU = T.getTypePtr(); |
| 272 | |
| 273 | // Handle everything else by appropriate substitution. |
| 274 | } else { |
| 275 | NamedDecl *ND = D->getFriendDecl(); |
| 276 | assert(ND && "friend decl must be a decl or a type!"); |
| 277 | |
| 278 | Decl *NewND = Visit(ND); |
| 279 | if (!NewND) return 0; |
| 280 | |
| 281 | FU = cast<NamedDecl>(NewND); |
John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 282 | } |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 283 | |
| 284 | FriendDecl *FD = |
| 285 | FriendDecl::Create(SemaRef.Context, Owner, D->getLocation(), FU, |
| 286 | D->getFriendLoc()); |
John McCall | 5fee110 | 2009-08-29 03:50:18 +0000 | [diff] [blame] | 287 | FD->setAccess(AS_public); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 288 | Owner->addDecl(FD); |
| 289 | return FD; |
John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 292 | Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 293 | Expr *AssertExpr = D->getAssertExpr(); |
| 294 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 295 | // The expression in a static assertion is not potentially evaluated. |
| 296 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated); |
| 297 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 298 | OwningExprResult InstantiatedAssertExpr |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 299 | = SemaRef.SubstExpr(AssertExpr, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 300 | if (InstantiatedAssertExpr.isInvalid()) |
| 301 | return 0; |
| 302 | |
Douglas Gregor | 43d9d92 | 2009-08-08 01:41:12 +0000 | [diff] [blame] | 303 | OwningExprResult Message(SemaRef, D->getMessage()); |
| 304 | D->getMessage()->Retain(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 305 | Decl *StaticAssert |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 306 | = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(), |
| 307 | move(InstantiatedAssertExpr), |
| 308 | move(Message)).getAs<Decl>(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 309 | return StaticAssert; |
| 310 | } |
| 311 | |
| 312 | Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { |
| 313 | EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, |
| 314 | D->getLocation(), D->getIdentifier(), |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 315 | D->getTagKeywordLoc(), |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 316 | /*PrevDecl=*/0); |
Douglas Gregor | 8dbc3c6 | 2009-05-27 17:20:35 +0000 | [diff] [blame] | 317 | Enum->setInstantiationOfMemberEnum(D); |
Douglas Gregor | 06c0fec | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 318 | Enum->setAccess(D->getAccess()); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 319 | Owner->addDecl(Enum); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 320 | Enum->startDefinition(); |
| 321 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 322 | llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators; |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 323 | |
| 324 | EnumConstantDecl *LastEnumConst = 0; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 325 | for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(), |
| 326 | ECEnd = D->enumerator_end(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 327 | EC != ECEnd; ++EC) { |
| 328 | // The specified value for the enumerator. |
| 329 | OwningExprResult Value = SemaRef.Owned((Expr *)0); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 330 | if (Expr *UninstValue = EC->getInitExpr()) { |
| 331 | // The enumerator's value expression is not potentially evaluated. |
| 332 | EnterExpressionEvaluationContext Unevaluated(SemaRef, |
| 333 | Action::Unevaluated); |
| 334 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 335 | Value = SemaRef.SubstExpr(UninstValue, TemplateArgs); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 336 | } |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 337 | |
| 338 | // Drop the initial value and continue. |
| 339 | bool isInvalid = false; |
| 340 | if (Value.isInvalid()) { |
| 341 | Value = SemaRef.Owned((Expr *)0); |
| 342 | isInvalid = true; |
| 343 | } |
| 344 | |
| 345 | EnumConstantDecl *EnumConst |
| 346 | = SemaRef.CheckEnumConstant(Enum, LastEnumConst, |
| 347 | EC->getLocation(), EC->getIdentifier(), |
| 348 | move(Value)); |
| 349 | |
| 350 | if (isInvalid) { |
| 351 | if (EnumConst) |
| 352 | EnumConst->setInvalidDecl(); |
| 353 | Enum->setInvalidDecl(); |
| 354 | } |
| 355 | |
| 356 | if (EnumConst) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 357 | Enum->addDecl(EnumConst); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 358 | Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst)); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 359 | LastEnumConst = EnumConst; |
| 360 | } |
| 361 | } |
| 362 | |
Mike Stump | c6e35aa | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 363 | // FIXME: Fixup LBraceLoc and RBraceLoc |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 364 | // FIXME: Empty Scope and AttributeList (required to handle attribute packed). |
Mike Stump | c6e35aa | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 365 | SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(), |
| 366 | Sema::DeclPtrTy::make(Enum), |
Edward O'Callaghan | fee1381 | 2009-08-08 14:36:57 +0000 | [diff] [blame] | 367 | &Enumerators[0], Enumerators.size(), |
| 368 | 0, 0); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 369 | |
| 370 | return Enum; |
| 371 | } |
| 372 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 373 | Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 374 | assert(false && "EnumConstantDecls can only occur within EnumDecls."); |
| 375 | return 0; |
| 376 | } |
| 377 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 378 | Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { |
| 379 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 380 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 381 | if (!InstParams) |
| 382 | return NULL; |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 383 | |
| 384 | CXXRecordDecl *Pattern = D->getTemplatedDecl(); |
| 385 | CXXRecordDecl *RecordInst |
| 386 | = CXXRecordDecl::Create(SemaRef.Context, Pattern->getTagKind(), Owner, |
| 387 | Pattern->getLocation(), Pattern->getIdentifier(), |
| 388 | Pattern->getTagKeywordLoc(), /*PrevDecl=*/ NULL); |
| 389 | |
| 390 | ClassTemplateDecl *Inst |
| 391 | = ClassTemplateDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 392 | D->getIdentifier(), InstParams, RecordInst, 0); |
| 393 | RecordInst->setDescribedClassTemplate(Inst); |
| 394 | Inst->setAccess(D->getAccess()); |
| 395 | Inst->setInstantiatedFromMemberTemplate(D); |
| 396 | |
| 397 | Owner->addDecl(Inst); |
| 398 | return Inst; |
| 399 | } |
| 400 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 401 | Decl * |
| 402 | TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { |
| 403 | TemplateParameterList *TempParams = D->getTemplateParameters(); |
| 404 | TemplateParameterList *InstParams = SubstTemplateParams(TempParams); |
| 405 | if (!InstParams) |
| 406 | return NULL; |
| 407 | |
| 408 | // FIXME: Handle instantiation of nested function templates that aren't |
| 409 | // member function templates. This could happen inside a FriendDecl. |
| 410 | assert(isa<CXXMethodDecl>(D->getTemplatedDecl())); |
| 411 | CXXMethodDecl *InstMethod |
| 412 | = cast_or_null<CXXMethodDecl>( |
| 413 | VisitCXXMethodDecl(cast<CXXMethodDecl>(D->getTemplatedDecl()), |
| 414 | InstParams)); |
| 415 | if (!InstMethod) |
| 416 | return 0; |
| 417 | |
| 418 | // Link the instantiated function template declaration to the function |
| 419 | // template from which it was instantiated. |
| 420 | FunctionTemplateDecl *InstTemplate = InstMethod->getDescribedFunctionTemplate(); |
| 421 | assert(InstTemplate && "VisitCXXMethodDecl didn't create a template!"); |
| 422 | InstTemplate->setInstantiatedFromMemberTemplate(D); |
| 423 | Owner->addDecl(InstTemplate); |
| 424 | return InstTemplate; |
| 425 | } |
| 426 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 427 | Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 428 | CXXRecordDecl *PrevDecl = 0; |
| 429 | if (D->isInjectedClassName()) |
| 430 | PrevDecl = cast<CXXRecordDecl>(Owner); |
| 431 | |
| 432 | CXXRecordDecl *Record |
| 433 | = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner, |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 434 | D->getLocation(), D->getIdentifier(), |
| 435 | D->getTagKeywordLoc(), PrevDecl); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 436 | Record->setImplicit(D->isImplicit()); |
Eli Friedman | eaba1af | 2009-08-27 19:11:42 +0000 | [diff] [blame] | 437 | // FIXME: Check against AS_none is an ugly hack to work around the issue that |
| 438 | // the tag decls introduced by friend class declarations don't have an access |
| 439 | // specifier. Remove once this area of the code gets sorted out. |
| 440 | if (D->getAccess() != AS_none) |
| 441 | Record->setAccess(D->getAccess()); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 442 | if (!D->isInjectedClassName()) |
| 443 | Record->setInstantiationOfMemberClass(D); |
| 444 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 445 | // If the original function was part of a friend declaration, |
| 446 | // inherit its namespace state. |
| 447 | if (Decl::FriendObjectKind FOK = D->getFriendObjectKind()) |
| 448 | Record->setObjectOfFriendDecl(FOK == Decl::FOK_Declared); |
| 449 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 450 | Record->setAnonymousStructOrUnion(D->isAnonymousStructOrUnion()); |
| 451 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 452 | Owner->addDecl(Record); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 453 | return Record; |
| 454 | } |
| 455 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 456 | /// Normal class members are of more specific types and therefore |
| 457 | /// don't make it here. This function serves two purposes: |
| 458 | /// 1) instantiating function templates |
| 459 | /// 2) substituting friend declarations |
| 460 | /// FIXME: preserve function definitions in case #2 |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 461 | Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D) { |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 462 | // Check whether there is already a function template specialization for |
| 463 | // this declaration. |
| 464 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
| 465 | void *InsertPos = 0; |
| 466 | if (FunctionTemplate) { |
| 467 | llvm::FoldingSetNodeID ID; |
| 468 | FunctionTemplateSpecializationInfo::Profile(ID, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 469 | TemplateArgs.getInnermost().getFlatArgumentList(), |
| 470 | TemplateArgs.getInnermost().flat_size(), |
Douglas Gregor | 828e226 | 2009-07-29 16:09:57 +0000 | [diff] [blame] | 471 | SemaRef.Context); |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 472 | |
| 473 | FunctionTemplateSpecializationInfo *Info |
| 474 | = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID, |
| 475 | InsertPos); |
| 476 | |
| 477 | // If we already have a function template specialization, return it. |
| 478 | if (Info) |
| 479 | return Info->Function; |
| 480 | } |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 481 | |
| 482 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 483 | |
| 484 | llvm::SmallVector<ParmVarDecl *, 4> Params; |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 485 | QualType T = SubstFunctionType(D, Params); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 486 | if (T.isNull()) |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 487 | return 0; |
John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 488 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 489 | // Build the instantiated method declaration. |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 490 | DeclContext *DC = SemaRef.FindInstantiatedContext(D->getDeclContext()); |
| 491 | FunctionDecl *Function = |
| 492 | FunctionDecl::Create(SemaRef.Context, DC, D->getLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 493 | D->getDeclName(), T, D->getDeclaratorInfo(), |
| 494 | D->getStorageClass(), |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 495 | D->isInline(), D->hasWrittenPrototype()); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 496 | Function->setLexicalDeclContext(Owner); |
| 497 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 498 | // Attach the parameters |
| 499 | for (unsigned P = 0; P < Params.size(); ++P) |
| 500 | Params[P]->setOwningFunction(Function); |
| 501 | Function->setParams(SemaRef.Context, Params.data(), Params.size()); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 502 | |
| 503 | // If the original function was part of a friend declaration, |
| 504 | // inherit its namespace state and add it to the owner. |
| 505 | if (Decl::FriendObjectKind FOK = D->getFriendObjectKind()) { |
| 506 | bool WasDeclared = (FOK == Decl::FOK_Declared); |
| 507 | Function->setObjectOfFriendDecl(WasDeclared); |
| 508 | if (!Owner->isDependentContext()) |
John McCall | ab88d97 | 2009-08-31 22:39:49 +0000 | [diff] [blame] | 509 | DC->makeDeclVisibleInContext(Function, /* Recoverable = */ false); |
John McCall | f181d8a | 2009-08-29 03:16:09 +0000 | [diff] [blame] | 510 | |
| 511 | Function->setInstantiationOfMemberFunction(D); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 512 | } |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 513 | |
| 514 | if (InitFunctionInstantiation(Function, D)) |
| 515 | Function->setInvalidDecl(); |
| 516 | |
| 517 | bool Redeclaration = false; |
| 518 | bool OverloadableAttrRequired = false; |
| 519 | NamedDecl *PrevDecl = 0; |
| 520 | SemaRef.CheckFunctionDeclaration(Function, PrevDecl, Redeclaration, |
| 521 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 522 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 523 | if (FunctionTemplate) { |
| 524 | // Record this function template specialization. |
| 525 | Function->setFunctionTemplateSpecialization(SemaRef.Context, |
| 526 | FunctionTemplate, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 527 | &TemplateArgs.getInnermost(), |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 528 | InsertPos); |
John McCall | fd810b1 | 2009-08-14 02:03:10 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 531 | return Function; |
| 532 | } |
| 533 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 534 | Decl * |
| 535 | TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D, |
| 536 | TemplateParameterList *TemplateParams) { |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 537 | FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate(); |
| 538 | void *InsertPos = 0; |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 539 | if (FunctionTemplate && !TemplateParams) { |
| 540 | // We are creating a function template specialization from a function |
| 541 | // template. Check whether there is already a function template |
| 542 | // specialization for this particular set of template arguments. |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 543 | llvm::FoldingSetNodeID ID; |
| 544 | FunctionTemplateSpecializationInfo::Profile(ID, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 545 | TemplateArgs.getInnermost().getFlatArgumentList(), |
| 546 | TemplateArgs.getInnermost().flat_size(), |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 547 | SemaRef.Context); |
| 548 | |
| 549 | FunctionTemplateSpecializationInfo *Info |
| 550 | = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID, |
| 551 | InsertPos); |
| 552 | |
| 553 | // If we already have a function template specialization, return it. |
| 554 | if (Info) |
| 555 | return Info->Function; |
| 556 | } |
| 557 | |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 558 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 559 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 560 | llvm::SmallVector<ParmVarDecl *, 4> Params; |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 561 | QualType T = SubstFunctionType(D, Params); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 562 | if (T.isNull()) |
| 563 | return 0; |
| 564 | |
| 565 | // Build the instantiated method declaration. |
| 566 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 567 | CXXMethodDecl *Method = 0; |
| 568 | |
| 569 | DeclarationName Name = D->getDeclName(); |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 570 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 571 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); |
| 572 | Name = SemaRef.Context.DeclarationNames.getCXXConstructorName( |
| 573 | SemaRef.Context.getCanonicalType(ClassTy)); |
| 574 | Method = CXXConstructorDecl::Create(SemaRef.Context, Record, |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 575 | Constructor->getLocation(), |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 576 | Name, T, |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 577 | Constructor->getDeclaratorInfo(), |
| 578 | Constructor->isExplicit(), |
| 579 | Constructor->isInline(), false); |
| 580 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
| 581 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); |
| 582 | Name = SemaRef.Context.DeclarationNames.getCXXDestructorName( |
| 583 | SemaRef.Context.getCanonicalType(ClassTy)); |
| 584 | Method = CXXDestructorDecl::Create(SemaRef.Context, Record, |
| 585 | Destructor->getLocation(), Name, |
| 586 | T, Destructor->isInline(), false); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 587 | } else if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { |
| 588 | CanQualType ConvTy |
| 589 | = SemaRef.Context.getCanonicalType( |
| 590 | T->getAsFunctionType()->getResultType()); |
| 591 | Name = SemaRef.Context.DeclarationNames.getCXXConversionFunctionName( |
| 592 | ConvTy); |
| 593 | Method = CXXConversionDecl::Create(SemaRef.Context, Record, |
| 594 | Conversion->getLocation(), Name, |
| 595 | T, Conversion->getDeclaratorInfo(), |
| 596 | Conversion->isInline(), |
| 597 | Conversion->isExplicit()); |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 598 | } else { |
| 599 | Method = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(), |
| 600 | D->getDeclName(), T, D->getDeclaratorInfo(), |
| 601 | D->isStatic(), D->isInline()); |
| 602 | } |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 603 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 604 | if (TemplateParams) { |
| 605 | // Our resulting instantiation is actually a function template, since we |
| 606 | // are substituting only the outer template parameters. For example, given |
| 607 | // |
| 608 | // template<typename T> |
| 609 | // struct X { |
| 610 | // template<typename U> void f(T, U); |
| 611 | // }; |
| 612 | // |
| 613 | // X<int> x; |
| 614 | // |
| 615 | // We are instantiating the member template "f" within X<int>, which means |
| 616 | // substituting int for T, but leaving "f" as a member function template. |
| 617 | // Build the function template itself. |
| 618 | FunctionTemplate = FunctionTemplateDecl::Create(SemaRef.Context, Record, |
| 619 | Method->getLocation(), |
| 620 | Method->getDeclName(), |
| 621 | TemplateParams, Method); |
| 622 | if (D->isOutOfLine()) |
| 623 | FunctionTemplate->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 624 | Method->setDescribedFunctionTemplate(FunctionTemplate); |
| 625 | } else if (!FunctionTemplate) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 626 | Method->setInstantiationOfMemberFunction(D); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 627 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 628 | // If we are instantiating a member function defined |
| 629 | // out-of-line, the instantiation will have the same lexical |
| 630 | // context (which will be a namespace scope) as the template. |
| 631 | if (D->isOutOfLine()) |
| 632 | Method->setLexicalDeclContext(D->getLexicalDeclContext()); |
| 633 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 634 | // Attach the parameters |
| 635 | for (unsigned P = 0; P < Params.size(); ++P) |
| 636 | Params[P]->setOwningFunction(Method); |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 637 | Method->setParams(SemaRef.Context, Params.data(), Params.size()); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 638 | |
| 639 | if (InitMethodInstantiation(Method, D)) |
| 640 | Method->setInvalidDecl(); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 641 | |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 642 | NamedDecl *PrevDecl = 0; |
| 643 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 644 | if (!FunctionTemplate || TemplateParams) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 645 | PrevDecl = SemaRef.LookupQualifiedName(Owner, Name, |
| 646 | Sema::LookupOrdinaryName, true); |
| 647 | |
| 648 | // In C++, the previous declaration we find might be a tag type |
| 649 | // (class or enum). In this case, the new declaration will hide the |
| 650 | // tag type. Note that this does does not apply if we're declaring a |
| 651 | // typedef (C++ [dcl.typedef]p4). |
| 652 | if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 653 | PrevDecl = 0; |
| 654 | } |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 655 | |
Douglas Gregor | d60e105 | 2009-08-27 16:57:43 +0000 | [diff] [blame] | 656 | if (FunctionTemplate && !TemplateParams) |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 657 | // Record this function template specialization. |
| 658 | Method->setFunctionTemplateSpecialization(SemaRef.Context, |
| 659 | FunctionTemplate, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 660 | &TemplateArgs.getInnermost(), |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 661 | InsertPos); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 662 | |
| 663 | bool Redeclaration = false; |
| 664 | bool OverloadableAttrRequired = false; |
| 665 | SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration, |
| 666 | /*FIXME:*/OverloadableAttrRequired); |
| 667 | |
| 668 | if (!FunctionTemplate && (!Method->isInvalidDecl() || !PrevDecl)) |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 669 | Owner->addDecl(Method); |
| 670 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 671 | return Method; |
| 672 | } |
| 673 | |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 674 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
Douglas Gregor | dec0666 | 2009-08-21 18:42:58 +0000 | [diff] [blame] | 675 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 678 | Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
Douglas Gregor | 17e32f3 | 2009-08-21 22:43:28 +0000 | [diff] [blame] | 679 | return VisitCXXMethodDecl(D); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 682 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 683 | return VisitCXXMethodDecl(D); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 686 | ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 687 | QualType OrigT = SemaRef.SubstType(D->getOriginalType(), TemplateArgs, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 688 | D->getLocation(), D->getDeclName()); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 689 | if (OrigT.isNull()) |
| 690 | return 0; |
| 691 | |
| 692 | QualType T = SemaRef.adjustParameterType(OrigT); |
| 693 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 694 | // Allocate the parameter |
| 695 | ParmVarDecl *Param = 0; |
| 696 | if (T == OrigT) |
| 697 | Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 698 | D->getIdentifier(), T, D->getDeclaratorInfo(), |
| 699 | D->getStorageClass(), 0); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 700 | else |
| 701 | Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner, |
| 702 | D->getLocation(), D->getIdentifier(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 703 | T, D->getDeclaratorInfo(), OrigT, |
| 704 | D->getStorageClass(), 0); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 705 | |
Anders Carlsson | 9351c17 | 2009-08-25 03:18:48 +0000 | [diff] [blame] | 706 | // Mark the default argument as being uninstantiated. |
| 707 | if (Expr *Arg = D->getDefaultArg()) |
| 708 | Param->setUninstantiatedDefaultArg(Arg); |
| 709 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 710 | // Note: we don't try to instantiate function parameters until after |
| 711 | // we've instantiated the function's type. Therefore, we don't have |
| 712 | // to check for 'void' parameter types here. |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 713 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 714 | return Param; |
| 715 | } |
| 716 | |
| 717 | Decl * |
| 718 | TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) { |
| 719 | // Since parameter types can decay either before or after |
| 720 | // instantiation, we simply treat OriginalParmVarDecls as |
| 721 | // ParmVarDecls the same way, and create one or the other depending |
| 722 | // on what happens after template instantiation. |
| 723 | return VisitParmVarDecl(D); |
| 724 | } |
| 725 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 726 | Decl *TemplateDeclInstantiator::VisitTemplateTypeParmDecl( |
| 727 | TemplateTypeParmDecl *D) { |
| 728 | // TODO: don't always clone when decls are refcounted. |
| 729 | const Type* T = D->getTypeForDecl(); |
| 730 | assert(T->isTemplateTypeParmType()); |
| 731 | const TemplateTypeParmType *TTPT = T->getAs<TemplateTypeParmType>(); |
| 732 | |
| 733 | TemplateTypeParmDecl *Inst = |
| 734 | TemplateTypeParmDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 735 | TTPT->getDepth(), TTPT->getIndex(), |
| 736 | TTPT->getName(), |
| 737 | D->wasDeclaredWithTypename(), |
| 738 | D->isParameterPack()); |
| 739 | |
| 740 | if (D->hasDefaultArgument()) { |
| 741 | QualType DefaultPattern = D->getDefaultArgument(); |
| 742 | QualType DefaultInst |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 743 | = SemaRef.SubstType(DefaultPattern, TemplateArgs, |
| 744 | D->getDefaultArgumentLoc(), |
| 745 | D->getDeclName()); |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 746 | |
| 747 | Inst->setDefaultArgument(DefaultInst, |
| 748 | D->getDefaultArgumentLoc(), |
| 749 | D->defaultArgumentWasInherited() /* preserve? */); |
| 750 | } |
| 751 | |
| 752 | return Inst; |
| 753 | } |
| 754 | |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 755 | Decl * |
| 756 | TemplateDeclInstantiator::VisitUnresolvedUsingDecl(UnresolvedUsingDecl *D) { |
| 757 | NestedNameSpecifier *NNS = |
| 758 | SemaRef.SubstNestedNameSpecifier(D->getTargetNestedNameSpecifier(), |
| 759 | D->getTargetNestedNameRange(), |
| 760 | TemplateArgs); |
| 761 | if (!NNS) |
| 762 | return 0; |
| 763 | |
| 764 | CXXScopeSpec SS; |
| 765 | SS.setRange(D->getTargetNestedNameRange()); |
| 766 | SS.setScopeRep(NNS); |
| 767 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 768 | NamedDecl *UD = |
| 769 | SemaRef.BuildUsingDeclaration(D->getLocation(), SS, |
| 770 | D->getTargetNameLocation(), |
| 771 | D->getTargetName(), 0, D->isTypeName()); |
| 772 | if (UD) |
| 773 | SemaRef.Context.setInstantiatedFromUnresolvedUsingDecl(cast<UsingDecl>(UD), |
| 774 | D); |
| 775 | return UD; |
Anders Carlsson | 0dde18e | 2009-08-28 15:18:15 +0000 | [diff] [blame] | 776 | } |
| 777 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 778 | Decl *Sema::SubstDecl(Decl *D, DeclContext *Owner, |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 779 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 780 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 781 | return Instantiator.Visit(D); |
| 782 | } |
| 783 | |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 784 | /// \brief Instantiates a nested template parameter list in the current |
| 785 | /// instantiation context. |
| 786 | /// |
| 787 | /// \param L The parameter list to instantiate |
| 788 | /// |
| 789 | /// \returns NULL if there was an error |
| 790 | TemplateParameterList * |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 791 | TemplateDeclInstantiator::SubstTemplateParams(TemplateParameterList *L) { |
John McCall | e29ba20 | 2009-08-20 01:44:21 +0000 | [diff] [blame] | 792 | // Get errors for all the parameters before bailing out. |
| 793 | bool Invalid = false; |
| 794 | |
| 795 | unsigned N = L->size(); |
| 796 | typedef llvm::SmallVector<Decl*,8> ParamVector; |
| 797 | ParamVector Params; |
| 798 | Params.reserve(N); |
| 799 | for (TemplateParameterList::iterator PI = L->begin(), PE = L->end(); |
| 800 | PI != PE; ++PI) { |
| 801 | Decl *D = Visit(*PI); |
| 802 | Params.push_back(D); |
| 803 | Invalid = Invalid || !D; |
| 804 | } |
| 805 | |
| 806 | // Clean up if we had an error. |
| 807 | if (Invalid) { |
| 808 | for (ParamVector::iterator PI = Params.begin(), PE = Params.end(); |
| 809 | PI != PE; ++PI) |
| 810 | if (*PI) |
| 811 | (*PI)->Destroy(SemaRef.Context); |
| 812 | return NULL; |
| 813 | } |
| 814 | |
| 815 | TemplateParameterList *InstL |
| 816 | = TemplateParameterList::Create(SemaRef.Context, L->getTemplateLoc(), |
| 817 | L->getLAngleLoc(), &Params.front(), N, |
| 818 | L->getRAngleLoc()); |
| 819 | return InstL; |
| 820 | } |
| 821 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 822 | /// \brief Does substitution on the type of the given function, including |
| 823 | /// all of the function parameters. |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 824 | /// |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 825 | /// \param D The function whose type will be the basis of the substitution |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 826 | /// |
| 827 | /// \param Params the instantiated parameter declarations |
| 828 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 829 | /// \returns the instantiated function's type if successful, a NULL |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 830 | /// type if there was an error. |
| 831 | QualType |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 832 | TemplateDeclInstantiator::SubstFunctionType(FunctionDecl *D, |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 833 | llvm::SmallVectorImpl<ParmVarDecl *> &Params) { |
| 834 | bool InvalidDecl = false; |
| 835 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 836 | // Substitute all of the function's formal parameter types. |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 837 | TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs); |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 838 | llvm::SmallVector<QualType, 4> ParamTys; |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 839 | for (FunctionDecl::param_iterator P = D->param_begin(), |
| 840 | PEnd = D->param_end(); |
| 841 | P != PEnd; ++P) { |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 842 | if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) { |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 843 | if (PInst->getType()->isVoidType()) { |
| 844 | SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type); |
| 845 | PInst->setInvalidDecl(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 846 | } else if (SemaRef.RequireNonAbstractType(PInst->getLocation(), |
| 847 | PInst->getType(), |
| 848 | diag::err_abstract_type_in_decl, |
| 849 | Sema::AbstractParamType)) |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 850 | PInst->setInvalidDecl(); |
| 851 | |
| 852 | Params.push_back(PInst); |
| 853 | ParamTys.push_back(PInst->getType()); |
| 854 | |
| 855 | if (PInst->isInvalidDecl()) |
| 856 | InvalidDecl = true; |
| 857 | } else |
| 858 | InvalidDecl = true; |
| 859 | } |
| 860 | |
| 861 | // FIXME: Deallocate dead declarations. |
| 862 | if (InvalidDecl) |
| 863 | return QualType(); |
| 864 | |
| 865 | const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType(); |
| 866 | assert(Proto && "Missing prototype?"); |
| 867 | QualType ResultType |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 868 | = SemaRef.SubstType(Proto->getResultType(), TemplateArgs, |
| 869 | D->getLocation(), D->getDeclName()); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 870 | if (ResultType.isNull()) |
| 871 | return QualType(); |
| 872 | |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 873 | return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(), |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 874 | Proto->isVariadic(), Proto->getTypeQuals(), |
| 875 | D->getLocation(), D->getDeclName()); |
| 876 | } |
| 877 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 878 | /// \brief Initializes the common fields of an instantiation function |
| 879 | /// declaration (New) from the corresponding fields of its template (Tmpl). |
| 880 | /// |
| 881 | /// \returns true if there was an error |
| 882 | bool |
| 883 | TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New, |
| 884 | FunctionDecl *Tmpl) { |
| 885 | if (Tmpl->isDeleted()) |
| 886 | New->setDeleted(); |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 887 | |
| 888 | // If we are performing substituting explicitly-specified template arguments |
| 889 | // or deduced template arguments into a function template and we reach this |
| 890 | // point, we are now past the point where SFINAE applies and have committed |
| 891 | // to keeping the new function template specialization. We therefore |
| 892 | // convert the active template instantiation for the function template |
| 893 | // into a template instantiation for this specific function template |
| 894 | // specialization, which is not a SFINAE context, so that we diagnose any |
| 895 | // further errors in the declaration itself. |
| 896 | typedef Sema::ActiveTemplateInstantiation ActiveInstType; |
| 897 | ActiveInstType &ActiveInst = SemaRef.ActiveTemplateInstantiations.back(); |
| 898 | if (ActiveInst.Kind == ActiveInstType::ExplicitTemplateArgumentSubstitution || |
| 899 | ActiveInst.Kind == ActiveInstType::DeducedTemplateArgumentSubstitution) { |
| 900 | if (FunctionTemplateDecl *FunTmpl |
| 901 | = dyn_cast<FunctionTemplateDecl>((Decl *)ActiveInst.Entity)) { |
| 902 | assert(FunTmpl->getTemplatedDecl() == Tmpl && |
| 903 | "Deduction from the wrong function template?"); |
Daniel Dunbar | bcbb8bd | 2009-07-16 22:10:11 +0000 | [diff] [blame] | 904 | (void) FunTmpl; |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 905 | ActiveInst.Kind = ActiveInstType::TemplateInstantiation; |
| 906 | ActiveInst.Entity = reinterpret_cast<uintptr_t>(New); |
| 907 | } |
| 908 | } |
| 909 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 910 | return false; |
| 911 | } |
| 912 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 913 | /// \brief Initializes common fields of an instantiated method |
| 914 | /// declaration (New) from the corresponding fields of its template |
| 915 | /// (Tmpl). |
| 916 | /// |
| 917 | /// \returns true if there was an error |
| 918 | bool |
| 919 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, |
| 920 | CXXMethodDecl *Tmpl) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 921 | if (InitFunctionInstantiation(New, Tmpl)) |
| 922 | return true; |
| 923 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 924 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 925 | New->setAccess(Tmpl->getAccess()); |
Anders Carlsson | 77b7f1d | 2009-05-14 22:15:41 +0000 | [diff] [blame] | 926 | if (Tmpl->isVirtualAsWritten()) { |
| 927 | New->setVirtualAsWritten(true); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 928 | Record->setAggregate(false); |
| 929 | Record->setPOD(false); |
Eli Friedman | 1d954f6 | 2009-08-15 21:55:26 +0000 | [diff] [blame] | 930 | Record->setEmpty(false); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 931 | Record->setPolymorphic(true); |
| 932 | } |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 933 | if (Tmpl->isPure()) { |
| 934 | New->setPure(); |
| 935 | Record->setAbstract(true); |
| 936 | } |
| 937 | |
| 938 | // FIXME: attributes |
| 939 | // FIXME: New needs a pointer to Tmpl |
| 940 | return false; |
| 941 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 942 | |
| 943 | /// \brief Instantiate the definition of the given function from its |
| 944 | /// template. |
| 945 | /// |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 946 | /// \param PointOfInstantiation the point at which the instantiation was |
| 947 | /// required. Note that this is not precisely a "point of instantiation" |
| 948 | /// for the function, but it's close. |
| 949 | /// |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 950 | /// \param Function the already-instantiated declaration of a |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 951 | /// function template specialization or member function of a class template |
| 952 | /// specialization. |
| 953 | /// |
| 954 | /// \param Recursive if true, recursively instantiates any functions that |
| 955 | /// are required by this instantiation. |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 956 | void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 957 | FunctionDecl *Function, |
| 958 | bool Recursive) { |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 959 | if (Function->isInvalidDecl()) |
| 960 | return; |
| 961 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 962 | assert(!Function->getBody() && "Already instantiated!"); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 963 | |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 964 | // Find the function body that we'll be substituting. |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 965 | const FunctionDecl *PatternDecl = 0; |
Douglas Gregor | 5ec178f | 2009-08-28 21:09:48 +0000 | [diff] [blame] | 966 | if (FunctionTemplateDecl *Primary = Function->getPrimaryTemplate()) { |
| 967 | while (Primary->getInstantiatedFromMemberTemplate()) |
| 968 | Primary = Primary->getInstantiatedFromMemberTemplate(); |
| 969 | |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 970 | PatternDecl = Primary->getTemplatedDecl(); |
Douglas Gregor | 5ec178f | 2009-08-28 21:09:48 +0000 | [diff] [blame] | 971 | } else |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 972 | PatternDecl = Function->getInstantiatedFromMemberFunction(); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 973 | Stmt *Pattern = 0; |
| 974 | if (PatternDecl) |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 975 | Pattern = PatternDecl->getBody(PatternDecl); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 976 | |
| 977 | if (!Pattern) |
| 978 | return; |
| 979 | |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 980 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); |
| 981 | if (Inst) |
| 982 | return; |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 983 | |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 984 | // If we're performing recursive template instantiation, create our own |
| 985 | // queue of pending implicit instantiations that we will instantiate later, |
| 986 | // while we're still within our own instantiation context. |
| 987 | std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations; |
| 988 | if (Recursive) |
| 989 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); |
| 990 | |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 991 | ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function)); |
| 992 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 993 | // Introduce a new scope where local variable instantiations will be |
| 994 | // recorded. |
| 995 | LocalInstantiationScope Scope(*this); |
| 996 | |
| 997 | // Introduce the instantiated function parameters into the local |
| 998 | // instantiation scope. |
| 999 | for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) |
| 1000 | Scope.InstantiatedLocal(PatternDecl->getParamDecl(I), |
| 1001 | Function->getParamDecl(I)); |
| 1002 | |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 1003 | // Enter the scope of this instantiation. We don't use |
| 1004 | // PushDeclContext because we don't have a scope. |
| 1005 | DeclContext *PreviousContext = CurContext; |
| 1006 | CurContext = Function; |
| 1007 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1008 | MultiLevelTemplateArgumentList TemplateArgs = |
| 1009 | getTemplateInstantiationArgs(Function); |
| 1010 | |
| 1011 | // If this is a constructor, instantiate the member initializers. |
| 1012 | if (const CXXConstructorDecl *Ctor = |
| 1013 | dyn_cast<CXXConstructorDecl>(PatternDecl)) { |
| 1014 | InstantiateMemInitializers(cast<CXXConstructorDecl>(Function), Ctor, |
| 1015 | TemplateArgs); |
| 1016 | } |
| 1017 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 1018 | // Instantiate the function body. |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1019 | OwningStmtResult Body = SubstStmt(Pattern, TemplateArgs); |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 1020 | |
| 1021 | ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body), |
| 1022 | /*IsInstantiation=*/true); |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 1023 | |
| 1024 | CurContext = PreviousContext; |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 1025 | |
| 1026 | DeclGroupRef DG(Function); |
| 1027 | Consumer.HandleTopLevelDecl(DG); |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1028 | |
| 1029 | if (Recursive) { |
| 1030 | // Instantiate any pending implicit instantiations found during the |
| 1031 | // instantiation of this template. |
| 1032 | PerformPendingImplicitInstantiations(); |
| 1033 | |
| 1034 | // Restore the set of pending implicit instantiations. |
| 1035 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); |
| 1036 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | /// \brief Instantiate the definition of the given variable from its |
| 1040 | /// template. |
| 1041 | /// |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1042 | /// \param PointOfInstantiation the point at which the instantiation was |
| 1043 | /// required. Note that this is not precisely a "point of instantiation" |
| 1044 | /// for the function, but it's close. |
| 1045 | /// |
| 1046 | /// \param Var the already-instantiated declaration of a static member |
| 1047 | /// variable of a class template specialization. |
| 1048 | /// |
| 1049 | /// \param Recursive if true, recursively instantiates any functions that |
| 1050 | /// are required by this instantiation. |
| 1051 | void Sema::InstantiateStaticDataMemberDefinition( |
| 1052 | SourceLocation PointOfInstantiation, |
| 1053 | VarDecl *Var, |
| 1054 | bool Recursive) { |
| 1055 | if (Var->isInvalidDecl()) |
| 1056 | return; |
| 1057 | |
| 1058 | // Find the out-of-line definition of this static data member. |
| 1059 | // FIXME: Do we have to look for specializations separately? |
| 1060 | VarDecl *Def = Var->getInstantiatedFromStaticDataMember(); |
| 1061 | bool FoundOutOfLineDef = false; |
| 1062 | assert(Def && "This data member was not instantiated from a template?"); |
| 1063 | assert(Def->isStaticDataMember() && "Not a static data member?"); |
| 1064 | for (VarDecl::redecl_iterator RD = Def->redecls_begin(), |
| 1065 | RDEnd = Def->redecls_end(); |
| 1066 | RD != RDEnd; ++RD) { |
| 1067 | if (RD->getLexicalDeclContext()->isFileContext()) { |
| 1068 | Def = *RD; |
| 1069 | FoundOutOfLineDef = true; |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | if (!FoundOutOfLineDef) { |
| 1074 | // We did not find an out-of-line definition of this static data member, |
| 1075 | // so we won't perform any instantiation. Rather, we rely on the user to |
| 1076 | // instantiate this definition (or provide a specialization for it) in |
| 1077 | // another translation unit. |
| 1078 | return; |
| 1079 | } |
| 1080 | |
| 1081 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Var); |
| 1082 | if (Inst) |
| 1083 | return; |
| 1084 | |
| 1085 | // If we're performing recursive template instantiation, create our own |
| 1086 | // queue of pending implicit instantiations that we will instantiate later, |
| 1087 | // while we're still within our own instantiation context. |
| 1088 | std::deque<PendingImplicitInstantiation> SavedPendingImplicitInstantiations; |
| 1089 | if (Recursive) |
| 1090 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); |
| 1091 | |
| 1092 | // Enter the scope of this instantiation. We don't use |
| 1093 | // PushDeclContext because we don't have a scope. |
| 1094 | DeclContext *PreviousContext = CurContext; |
| 1095 | CurContext = Var->getDeclContext(); |
| 1096 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1097 | Var = cast_or_null<VarDecl>(SubstDecl(Def, Var->getDeclContext(), |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1098 | getTemplateInstantiationArgs(Var))); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1099 | |
| 1100 | CurContext = PreviousContext; |
| 1101 | |
| 1102 | if (Var) { |
| 1103 | DeclGroupRef DG(Var); |
| 1104 | Consumer.HandleTopLevelDecl(DG); |
| 1105 | } |
| 1106 | |
| 1107 | if (Recursive) { |
| 1108 | // Instantiate any pending implicit instantiations found during the |
| 1109 | // instantiation of this template. |
| 1110 | PerformPendingImplicitInstantiations(); |
| 1111 | |
| 1112 | // Restore the set of pending implicit instantiations. |
| 1113 | PendingImplicitInstantiations.swap(SavedPendingImplicitInstantiations); |
| 1114 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 1115 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1116 | |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1117 | void |
| 1118 | Sema::InstantiateMemInitializers(CXXConstructorDecl *New, |
| 1119 | const CXXConstructorDecl *Tmpl, |
| 1120 | const MultiLevelTemplateArgumentList &TemplateArgs) { |
| 1121 | |
| 1122 | llvm::SmallVector<MemInitTy*, 4> NewInits; |
| 1123 | |
| 1124 | // Instantiate all the initializers. |
| 1125 | for (CXXConstructorDecl::init_const_iterator Inits = Tmpl->init_begin(), |
| 1126 | InitsEnd = Tmpl->init_end(); Inits != InitsEnd; ++Inits) { |
| 1127 | CXXBaseOrMemberInitializer *Init = *Inits; |
| 1128 | |
| 1129 | ASTOwningVector<&ActionBase::DeleteExpr> NewArgs(*this); |
| 1130 | |
| 1131 | // Instantiate all the arguments. |
| 1132 | for (ExprIterator Args = Init->arg_begin(), ArgsEnd = Init->arg_end(); |
| 1133 | Args != ArgsEnd; ++Args) { |
| 1134 | OwningExprResult NewArg = SubstExpr(*Args, TemplateArgs); |
| 1135 | |
| 1136 | if (NewArg.isInvalid()) |
| 1137 | New->setInvalidDecl(); |
| 1138 | else |
| 1139 | NewArgs.push_back(NewArg.takeAs<Expr>()); |
| 1140 | } |
| 1141 | |
| 1142 | MemInitResult NewInit; |
| 1143 | |
| 1144 | if (Init->isBaseInitializer()) { |
Eli Friedman | c5573a8 | 2009-08-29 22:22:07 +0000 | [diff] [blame] | 1145 | QualType BaseType(Init->getBaseClass(), 0); |
| 1146 | BaseType = SubstType(BaseType, TemplateArgs, Init->getSourceLocation(), |
| 1147 | New->getDeclName()); |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1148 | |
| 1149 | NewInit = BuildBaseInitializer(BaseType, |
| 1150 | (Expr **)NewArgs.data(), |
| 1151 | NewArgs.size(), |
| 1152 | Init->getSourceLocation(), |
| 1153 | Init->getRParenLoc(), |
| 1154 | New->getParent()); |
| 1155 | } else if (Init->isMemberInitializer()) { |
Anders Carlsson | 9988d5d | 2009-09-01 04:31:02 +0000 | [diff] [blame] | 1156 | FieldDecl *Member; |
| 1157 | |
| 1158 | // Is this an anonymous union? |
| 1159 | if (FieldDecl *UnionInit = Init->getAnonUnionMember()) |
Anders Carlsson | cdc83c7 | 2009-09-01 06:22:14 +0000 | [diff] [blame] | 1160 | Member = cast<FieldDecl>(FindInstantiatedDecl(UnionInit)); |
Anders Carlsson | 9988d5d | 2009-09-01 04:31:02 +0000 | [diff] [blame] | 1161 | else |
| 1162 | Member = cast<FieldDecl>(FindInstantiatedDecl(Init->getMember())); |
Anders Carlsson | 0902531 | 2009-08-29 05:16:22 +0000 | [diff] [blame] | 1163 | |
| 1164 | NewInit = BuildMemberInitializer(Member, (Expr **)NewArgs.data(), |
| 1165 | NewArgs.size(), |
| 1166 | Init->getSourceLocation(), |
| 1167 | Init->getRParenLoc()); |
| 1168 | } |
| 1169 | |
| 1170 | if (NewInit.isInvalid()) |
| 1171 | New->setInvalidDecl(); |
| 1172 | else { |
| 1173 | // FIXME: It would be nice if ASTOwningVector had a release function. |
| 1174 | NewArgs.take(); |
| 1175 | |
| 1176 | NewInits.push_back((MemInitTy *)NewInit.get()); |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | // Assign all the initializers to the new constructor. |
| 1181 | ActOnMemInitializers(DeclPtrTy::make(New), |
| 1182 | /*FIXME: ColonLoc */ |
| 1183 | SourceLocation(), |
| 1184 | NewInits.data(), NewInits.size()); |
| 1185 | } |
| 1186 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1187 | // TODO: this could be templated if the various decl types used the |
| 1188 | // same method name. |
| 1189 | static bool isInstantiationOf(ClassTemplateDecl *Pattern, |
| 1190 | ClassTemplateDecl *Instance) { |
| 1191 | Pattern = Pattern->getCanonicalDecl(); |
| 1192 | |
| 1193 | do { |
| 1194 | Instance = Instance->getCanonicalDecl(); |
| 1195 | if (Pattern == Instance) return true; |
| 1196 | Instance = Instance->getInstantiatedFromMemberTemplate(); |
| 1197 | } while (Instance); |
| 1198 | |
| 1199 | return false; |
| 1200 | } |
| 1201 | |
| 1202 | static bool isInstantiationOf(CXXRecordDecl *Pattern, |
| 1203 | CXXRecordDecl *Instance) { |
| 1204 | Pattern = Pattern->getCanonicalDecl(); |
| 1205 | |
| 1206 | do { |
| 1207 | Instance = Instance->getCanonicalDecl(); |
| 1208 | if (Pattern == Instance) return true; |
| 1209 | Instance = Instance->getInstantiatedFromMemberClass(); |
| 1210 | } while (Instance); |
| 1211 | |
| 1212 | return false; |
| 1213 | } |
| 1214 | |
| 1215 | static bool isInstantiationOf(FunctionDecl *Pattern, |
| 1216 | FunctionDecl *Instance) { |
| 1217 | Pattern = Pattern->getCanonicalDecl(); |
| 1218 | |
| 1219 | do { |
| 1220 | Instance = Instance->getCanonicalDecl(); |
| 1221 | if (Pattern == Instance) return true; |
| 1222 | Instance = Instance->getInstantiatedFromMemberFunction(); |
| 1223 | } while (Instance); |
| 1224 | |
| 1225 | return false; |
| 1226 | } |
| 1227 | |
| 1228 | static bool isInstantiationOf(EnumDecl *Pattern, |
| 1229 | EnumDecl *Instance) { |
| 1230 | Pattern = Pattern->getCanonicalDecl(); |
| 1231 | |
| 1232 | do { |
| 1233 | Instance = Instance->getCanonicalDecl(); |
| 1234 | if (Pattern == Instance) return true; |
| 1235 | Instance = Instance->getInstantiatedFromMemberEnum(); |
| 1236 | } while (Instance); |
| 1237 | |
| 1238 | return false; |
| 1239 | } |
| 1240 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1241 | static bool isInstantiationOf(UnresolvedUsingDecl *Pattern, |
| 1242 | UsingDecl *Instance, |
| 1243 | ASTContext &C) { |
| 1244 | return C.getInstantiatedFromUnresolvedUsingDecl(Instance) == Pattern; |
| 1245 | } |
| 1246 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1247 | static bool isInstantiationOfStaticDataMember(VarDecl *Pattern, |
| 1248 | VarDecl *Instance) { |
| 1249 | assert(Instance->isStaticDataMember()); |
| 1250 | |
| 1251 | Pattern = Pattern->getCanonicalDecl(); |
| 1252 | |
| 1253 | do { |
| 1254 | Instance = Instance->getCanonicalDecl(); |
| 1255 | if (Pattern == Instance) return true; |
| 1256 | Instance = Instance->getInstantiatedFromStaticDataMember(); |
| 1257 | } while (Instance); |
| 1258 | |
| 1259 | return false; |
| 1260 | } |
| 1261 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1262 | static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1263 | if (D->getKind() != Other->getKind()) { |
| 1264 | if (UnresolvedUsingDecl *UUD = dyn_cast<UnresolvedUsingDecl>(D)) { |
| 1265 | if (UsingDecl *UD = dyn_cast<UsingDecl>(Other)) { |
| 1266 | return isInstantiationOf(UUD, UD, Ctx); |
| 1267 | } |
| 1268 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1269 | |
Anders Carlsson | 0d8df78 | 2009-08-29 19:37:28 +0000 | [diff] [blame] | 1270 | return false; |
| 1271 | } |
| 1272 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1273 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other)) |
| 1274 | return isInstantiationOf(cast<CXXRecordDecl>(D), Record); |
Douglas Gregor | a5bf7f1 | 2009-08-28 22:03:51 +0000 | [diff] [blame] | 1275 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1276 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other)) |
| 1277 | return isInstantiationOf(cast<FunctionDecl>(D), Function); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1278 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1279 | if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other)) |
| 1280 | return isInstantiationOf(cast<EnumDecl>(D), Enum); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1281 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1282 | if (VarDecl *Var = dyn_cast<VarDecl>(Other)) |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1283 | if (Var->isStaticDataMember()) |
| 1284 | return isInstantiationOfStaticDataMember(cast<VarDecl>(D), Var); |
| 1285 | |
| 1286 | if (ClassTemplateDecl *Temp = dyn_cast<ClassTemplateDecl>(Other)) |
| 1287 | return isInstantiationOf(cast<ClassTemplateDecl>(D), Temp); |
Douglas Gregor | a5bf7f1 | 2009-08-28 22:03:51 +0000 | [diff] [blame] | 1288 | |
Anders Carlsson | d8b285f | 2009-09-01 04:26:58 +0000 | [diff] [blame] | 1289 | if (FieldDecl *Field = dyn_cast<FieldDecl>(Other)) { |
| 1290 | if (!Field->getDeclName()) { |
| 1291 | // This is an unnamed field. |
| 1292 | return Ctx.getInstantiatedFromUnnamedFieldDecl(Field) == |
| 1293 | cast<FieldDecl>(D); |
| 1294 | } |
| 1295 | } |
| 1296 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1297 | return D->getDeclName() && isa<NamedDecl>(Other) && |
| 1298 | D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); |
| 1299 | } |
| 1300 | |
| 1301 | template<typename ForwardIterator> |
| 1302 | static NamedDecl *findInstantiationOf(ASTContext &Ctx, |
| 1303 | NamedDecl *D, |
| 1304 | ForwardIterator first, |
| 1305 | ForwardIterator last) { |
| 1306 | for (; first != last; ++first) |
| 1307 | if (isInstantiationOf(Ctx, D, *first)) |
| 1308 | return cast<NamedDecl>(*first); |
| 1309 | |
| 1310 | return 0; |
| 1311 | } |
| 1312 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1313 | /// \brief Finds the instantiation of the given declaration context |
| 1314 | /// within the current instantiation. |
| 1315 | /// |
| 1316 | /// \returns NULL if there was an error |
| 1317 | DeclContext *Sema::FindInstantiatedContext(DeclContext* DC) { |
| 1318 | if (NamedDecl *D = dyn_cast<NamedDecl>(DC)) { |
| 1319 | Decl* ID = FindInstantiatedDecl(D); |
| 1320 | return cast_or_null<DeclContext>(ID); |
| 1321 | } else return DC; |
| 1322 | } |
| 1323 | |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 1324 | /// \brief Find the instantiation of the given declaration within the |
| 1325 | /// current instantiation. |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1326 | /// |
| 1327 | /// This routine is intended to be used when \p D is a declaration |
| 1328 | /// referenced from within a template, that needs to mapped into the |
| 1329 | /// corresponding declaration within an instantiation. For example, |
| 1330 | /// given: |
| 1331 | /// |
| 1332 | /// \code |
| 1333 | /// template<typename T> |
| 1334 | /// struct X { |
| 1335 | /// enum Kind { |
| 1336 | /// KnownValue = sizeof(T) |
| 1337 | /// }; |
| 1338 | /// |
| 1339 | /// bool getKind() const { return KnownValue; } |
| 1340 | /// }; |
| 1341 | /// |
| 1342 | /// template struct X<int>; |
| 1343 | /// \endcode |
| 1344 | /// |
| 1345 | /// In the instantiation of X<int>::getKind(), we need to map the |
| 1346 | /// EnumConstantDecl for KnownValue (which refers to |
| 1347 | /// X<T>::<Kind>::KnownValue) to its instantiation |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 1348 | /// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs |
| 1349 | /// this mapping from within the instantiation of X<int>. |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1350 | NamedDecl * Sema::FindInstantiatedDecl(NamedDecl *D) { |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1351 | DeclContext *ParentDC = D->getDeclContext(); |
Douglas Gregor | 2bba76b | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 1352 | if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) { |
| 1353 | // D is a local of some kind. Look into the map of local |
| 1354 | // declarations to their instantiations. |
| 1355 | return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D)); |
| 1356 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1357 | |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1358 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 1359 | if (ClassTemplateDecl *ClassTemplate |
| 1360 | = Record->getDescribedClassTemplate()) { |
| 1361 | // When the declaration D was parsed, it referred to the current |
| 1362 | // instantiation. Therefore, look through the current context, |
| 1363 | // which contains actual instantiations, to find the |
| 1364 | // instantiation of the "current instantiation" that D refers |
| 1365 | // to. Alternatively, we could just instantiate the |
| 1366 | // injected-class-name with the current template arguments, but |
| 1367 | // such an instantiation is far more expensive. |
| 1368 | for (DeclContext *DC = CurContext; !DC->isFileContext(); |
| 1369 | DC = DC->getParent()) { |
| 1370 | if (ClassTemplateSpecializationDecl *Spec |
| 1371 | = dyn_cast<ClassTemplateSpecializationDecl>(DC)) |
| 1372 | if (isInstantiationOf(ClassTemplate, Spec->getSpecializedTemplate())) |
| 1373 | return Spec; |
| 1374 | } |
| 1375 | |
| 1376 | assert(false && |
| 1377 | "Unable to find declaration for the current instantiation"); |
| 1378 | } |
| 1379 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 1380 | ParentDC = FindInstantiatedContext(ParentDC); |
| 1381 | if (!ParentDC) return 0; |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1382 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1383 | if (ParentDC != D->getDeclContext()) { |
| 1384 | // We performed some kind of instantiation in the parent context, |
| 1385 | // so now we need to look into the instantiated parent context to |
| 1386 | // find the instantiation of the declaration D. |
| 1387 | NamedDecl *Result = 0; |
| 1388 | if (D->getDeclName()) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1389 | DeclContext::lookup_result Found = ParentDC->lookup(D->getDeclName()); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1390 | Result = findInstantiationOf(Context, D, Found.first, Found.second); |
| 1391 | } else { |
| 1392 | // Since we don't have a name for the entity we're looking for, |
| 1393 | // our only option is to walk through all of the declarations to |
| 1394 | // find that name. This will occur in a few cases: |
| 1395 | // |
| 1396 | // - anonymous struct/union within a template |
| 1397 | // - unnamed class/struct/union/enum within a template |
| 1398 | // |
| 1399 | // FIXME: Find a better way to find these instantiations! |
| 1400 | Result = findInstantiationOf(Context, D, |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1401 | ParentDC->decls_begin(), |
| 1402 | ParentDC->decls_end()); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1403 | } |
John McCall | 52a575a | 2009-08-29 08:11:13 +0000 | [diff] [blame] | 1404 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1405 | assert(Result && "Unable to find instantiation of declaration!"); |
| 1406 | D = Result; |
| 1407 | } |
| 1408 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 1409 | return D; |
| 1410 | } |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1411 | |
| 1412 | /// \brief Performs template instantiation for all implicit template |
| 1413 | /// instantiations we have seen until this point. |
| 1414 | void Sema::PerformPendingImplicitInstantiations() { |
| 1415 | while (!PendingImplicitInstantiations.empty()) { |
| 1416 | PendingImplicitInstantiation Inst = PendingImplicitInstantiations.front(); |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1417 | PendingImplicitInstantiations.pop_front(); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1418 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1419 | // Instantiate function definitions |
| 1420 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Inst.first)) { |
Anders Carlsson | c17fb7b | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 1421 | PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Function), |
| 1422 | Function->getLocation(), *this, |
| 1423 | Context.getSourceManager(), |
| 1424 | "instantiating function definition"); |
| 1425 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1426 | if (!Function->getBody()) |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 1427 | InstantiateFunctionDefinition(/*FIXME:*/Inst.second, Function, true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1428 | continue; |
| 1429 | } |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1430 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1431 | // Instantiate static data member definitions. |
| 1432 | VarDecl *Var = cast<VarDecl>(Inst.first); |
| 1433 | assert(Var->isStaticDataMember() && "Not a static data member?"); |
Anders Carlsson | c17fb7b | 2009-09-01 05:12:24 +0000 | [diff] [blame] | 1434 | |
| 1435 | PrettyStackTraceActionsDecl CrashInfo(DeclPtrTy::make(Var), |
| 1436 | Var->getLocation(), *this, |
| 1437 | Context.getSourceManager(), |
| 1438 | "instantiating static data member " |
| 1439 | "definition"); |
| 1440 | |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1441 | InstantiateStaticDataMemberDefinition(/*FIXME:*/Inst.second, Var, true); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1442 | } |
| 1443 | } |