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