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