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