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