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); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 47 | Decl *VisitCXXRecordDecl(CXXRecordDecl *D); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 48 | Decl *VisitCXXMethodDecl(CXXMethodDecl *D); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 49 | Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 50 | Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 51 | Decl *VisitCXXConversionDecl(CXXConversionDecl *D); |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 52 | ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 53 | Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 54 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 55 | // Base case. FIXME: Remove once we can instantiate everything. |
| 56 | Decl *VisitDecl(Decl *) { |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 57 | assert(false && "Template instantiation of unknown declaration kind!"); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 58 | return 0; |
| 59 | } |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 60 | |
| 61 | // Helper functions for instantiating methods. |
| 62 | QualType InstantiateFunctionType(FunctionDecl *D, |
| 63 | llvm::SmallVectorImpl<ParmVarDecl *> &Params); |
| 64 | bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 65 | }; |
| 66 | } |
| 67 | |
Douglas Gregor | 4f722be | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 68 | Decl * |
| 69 | TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 70 | assert(false && "Translation units cannot be instantiated"); |
| 71 | return D; |
| 72 | } |
| 73 | |
| 74 | Decl * |
| 75 | TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { |
| 76 | assert(false && "Namespaces cannot be instantiated"); |
| 77 | return D; |
| 78 | } |
| 79 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 80 | Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { |
| 81 | bool Invalid = false; |
| 82 | QualType T = D->getUnderlyingType(); |
| 83 | if (T->isDependentType()) { |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 84 | T = SemaRef.InstantiateType(T, TemplateArgs, |
| 85 | D->getLocation(), D->getDeclName()); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 86 | if (T.isNull()) { |
| 87 | Invalid = true; |
| 88 | T = SemaRef.Context.IntTy; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Create the new typedef |
| 93 | TypedefDecl *Typedef |
| 94 | = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 95 | D->getIdentifier(), T); |
| 96 | if (Invalid) |
| 97 | Typedef->setInvalidDecl(); |
| 98 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 99 | Owner->addDecl(SemaRef.Context, Typedef); |
Douglas Gregor | bc22163 | 2009-05-28 16:34:51 +0000 | [diff] [blame] | 100 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 101 | return Typedef; |
| 102 | } |
| 103 | |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 104 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { |
| 105 | // Instantiate the type of the declaration |
| 106 | QualType T = SemaRef.InstantiateType(D->getType(), TemplateArgs, |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 107 | D->getTypeSpecStartLoc(), |
| 108 | D->getDeclName()); |
| 109 | if (T.isNull()) |
| 110 | return 0; |
| 111 | |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 112 | // Build the instantiated declaration |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 113 | VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner, |
| 114 | D->getLocation(), D->getIdentifier(), |
| 115 | T, D->getStorageClass(), |
| 116 | D->getTypeSpecStartLoc()); |
| 117 | Var->setThreadSpecified(D->isThreadSpecified()); |
| 118 | Var->setCXXDirectInitializer(D->hasCXXDirectInitializer()); |
| 119 | Var->setDeclaredInCondition(D->isDeclaredInCondition()); |
| 120 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 121 | // FIXME: In theory, we could have a previous declaration for variables that |
| 122 | // are not static data members. |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 123 | bool Redeclaration = false; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 124 | SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 125 | Owner->addDecl(SemaRef.Context, Var); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 126 | |
| 127 | if (D->getInit()) { |
| 128 | OwningExprResult Init |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 129 | = SemaRef.InstantiateExpr(D->getInit(), TemplateArgs); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 130 | if (Init.isInvalid()) |
| 131 | Var->setInvalidDecl(); |
| 132 | else |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 133 | SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init), |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 134 | D->hasCXXDirectInitializer()); |
Douglas Gregor | d308e62 | 2009-05-18 20:51:54 +0000 | [diff] [blame] | 135 | } else { |
| 136 | // FIXME: Call ActOnUninitializedDecl? (Not always) |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | return Var; |
| 140 | } |
| 141 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 142 | Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { |
| 143 | bool Invalid = false; |
| 144 | QualType T = D->getType(); |
| 145 | if (T->isDependentType()) { |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 146 | T = SemaRef.InstantiateType(T, TemplateArgs, |
| 147 | D->getLocation(), D->getDeclName()); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 148 | if (!T.isNull() && T->isFunctionType()) { |
| 149 | // C++ [temp.arg.type]p3: |
| 150 | // If a declaration acquires a function type through a type |
| 151 | // dependent on a template-parameter and this causes a |
| 152 | // declaration that does not use the syntactic form of a |
| 153 | // function declarator to have function type, the program is |
| 154 | // ill-formed. |
| 155 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
| 156 | << T; |
| 157 | T = QualType(); |
| 158 | Invalid = true; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | Expr *BitWidth = D->getBitWidth(); |
| 163 | if (Invalid) |
| 164 | BitWidth = 0; |
| 165 | else if (BitWidth) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 166 | // The bit-width expression is not potentially evaluated. |
| 167 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated); |
| 168 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 169 | OwningExprResult InstantiatedBitWidth |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 170 | = SemaRef.InstantiateExpr(BitWidth, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 171 | if (InstantiatedBitWidth.isInvalid()) { |
| 172 | Invalid = true; |
| 173 | BitWidth = 0; |
| 174 | } else |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 175 | BitWidth = InstantiatedBitWidth.takeAs<Expr>(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T, |
| 179 | cast<RecordDecl>(Owner), |
| 180 | D->getLocation(), |
| 181 | D->isMutable(), |
| 182 | BitWidth, |
| 183 | D->getAccess(), |
| 184 | 0); |
| 185 | if (Field) { |
| 186 | if (Invalid) |
| 187 | Field->setInvalidDecl(); |
| 188 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 189 | Owner->addDecl(SemaRef.Context, Field); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | return Field; |
| 193 | } |
| 194 | |
| 195 | Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 196 | Expr *AssertExpr = D->getAssertExpr(); |
| 197 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 198 | // The expression in a static assertion is not potentially evaluated. |
| 199 | EnterExpressionEvaluationContext Unevaluated(SemaRef, Action::Unevaluated); |
| 200 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 201 | OwningExprResult InstantiatedAssertExpr |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 202 | = SemaRef.InstantiateExpr(AssertExpr, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 203 | if (InstantiatedAssertExpr.isInvalid()) |
| 204 | return 0; |
| 205 | |
| 206 | OwningExprResult Message = SemaRef.Clone(D->getMessage()); |
| 207 | Decl *StaticAssert |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 208 | = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(), |
| 209 | move(InstantiatedAssertExpr), |
| 210 | move(Message)).getAs<Decl>(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 211 | return StaticAssert; |
| 212 | } |
| 213 | |
| 214 | Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { |
| 215 | EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, |
| 216 | D->getLocation(), D->getIdentifier(), |
| 217 | /*PrevDecl=*/0); |
Douglas Gregor | 8dbc3c6 | 2009-05-27 17:20:35 +0000 | [diff] [blame] | 218 | Enum->setInstantiationOfMemberEnum(D); |
Douglas Gregor | 06c0fec | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 219 | Enum->setAccess(D->getAccess()); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 220 | Owner->addDecl(SemaRef.Context, Enum); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 221 | Enum->startDefinition(); |
| 222 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 223 | llvm::SmallVector<Sema::DeclPtrTy, 4> Enumerators; |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 224 | |
| 225 | EnumConstantDecl *LastEnumConst = 0; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 226 | for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(SemaRef.Context), |
| 227 | ECEnd = D->enumerator_end(SemaRef.Context); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 228 | EC != ECEnd; ++EC) { |
| 229 | // The specified value for the enumerator. |
| 230 | OwningExprResult Value = SemaRef.Owned((Expr *)0); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 231 | if (Expr *UninstValue = EC->getInitExpr()) { |
| 232 | // The enumerator's value expression is not potentially evaluated. |
| 233 | EnterExpressionEvaluationContext Unevaluated(SemaRef, |
| 234 | Action::Unevaluated); |
| 235 | |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 236 | Value = SemaRef.InstantiateExpr(UninstValue, TemplateArgs); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 237 | } |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 238 | |
| 239 | // Drop the initial value and continue. |
| 240 | bool isInvalid = false; |
| 241 | if (Value.isInvalid()) { |
| 242 | Value = SemaRef.Owned((Expr *)0); |
| 243 | isInvalid = true; |
| 244 | } |
| 245 | |
| 246 | EnumConstantDecl *EnumConst |
| 247 | = SemaRef.CheckEnumConstant(Enum, LastEnumConst, |
| 248 | EC->getLocation(), EC->getIdentifier(), |
| 249 | move(Value)); |
| 250 | |
| 251 | if (isInvalid) { |
| 252 | if (EnumConst) |
| 253 | EnumConst->setInvalidDecl(); |
| 254 | Enum->setInvalidDecl(); |
| 255 | } |
| 256 | |
| 257 | if (EnumConst) { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 258 | Enum->addDecl(SemaRef.Context, EnumConst); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 259 | Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst)); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 260 | LastEnumConst = EnumConst; |
| 261 | } |
| 262 | } |
| 263 | |
Mike Stump | c6e35aa | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 264 | // FIXME: Fixup LBraceLoc and RBraceLoc |
| 265 | SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(), |
| 266 | Sema::DeclPtrTy::make(Enum), |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 267 | &Enumerators[0], Enumerators.size()); |
| 268 | |
| 269 | return Enum; |
| 270 | } |
| 271 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 272 | Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 273 | assert(false && "EnumConstantDecls can only occur within EnumDecls."); |
| 274 | return 0; |
| 275 | } |
| 276 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 277 | Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 278 | CXXRecordDecl *PrevDecl = 0; |
| 279 | if (D->isInjectedClassName()) |
| 280 | PrevDecl = cast<CXXRecordDecl>(Owner); |
| 281 | |
| 282 | CXXRecordDecl *Record |
| 283 | = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner, |
| 284 | D->getLocation(), D->getIdentifier(), PrevDecl); |
| 285 | Record->setImplicit(D->isImplicit()); |
| 286 | Record->setAccess(D->getAccess()); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 287 | if (!D->isInjectedClassName()) |
| 288 | Record->setInstantiationOfMemberClass(D); |
| 289 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 290 | Owner->addDecl(SemaRef.Context, Record); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 291 | return Record; |
| 292 | } |
| 293 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 294 | Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) { |
| 295 | // Only handle actual methods; we'll deal with constructors, |
| 296 | // destructors, etc. separately. |
| 297 | if (D->getKind() != Decl::CXXMethod) |
| 298 | return 0; |
| 299 | |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 300 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 301 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 302 | llvm::SmallVector<ParmVarDecl *, 4> Params; |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 303 | QualType T = InstantiateFunctionType(D, Params); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 304 | if (T.isNull()) |
| 305 | return 0; |
| 306 | |
| 307 | // Build the instantiated method declaration. |
| 308 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 309 | CXXMethodDecl *Method |
| 310 | = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(), |
| 311 | D->getDeclName(), T, D->isStatic(), |
| 312 | D->isInline()); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 313 | Method->setInstantiationOfMemberFunction(D); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 314 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 315 | // Attach the parameters |
| 316 | for (unsigned P = 0; P < Params.size(); ++P) |
| 317 | Params[P]->setOwningFunction(Method); |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 318 | Method->setParams(SemaRef.Context, Params.data(), Params.size()); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 319 | |
| 320 | if (InitMethodInstantiation(Method, D)) |
| 321 | Method->setInvalidDecl(); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 322 | |
| 323 | NamedDecl *PrevDecl |
| 324 | = SemaRef.LookupQualifiedName(Owner, Method->getDeclName(), |
| 325 | Sema::LookupOrdinaryName, true); |
| 326 | // In C++, the previous declaration we find might be a tag type |
| 327 | // (class or enum). In this case, the new declaration will hide the |
| 328 | // tag type. Note that this does does not apply if we're declaring a |
| 329 | // typedef (C++ [dcl.typedef]p4). |
| 330 | if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 331 | PrevDecl = 0; |
| 332 | bool Redeclaration = false; |
| 333 | bool OverloadableAttrRequired = false; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 334 | SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration, |
| 335 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 336 | |
| 337 | if (!Method->isInvalidDecl() || !PrevDecl) |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 338 | Owner->addDecl(SemaRef.Context, Method); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 339 | return Method; |
| 340 | } |
| 341 | |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 342 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 343 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 344 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 345 | llvm::SmallVector<ParmVarDecl *, 4> Params; |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 346 | QualType T = InstantiateFunctionType(D, Params); |
| 347 | if (T.isNull()) |
| 348 | return 0; |
| 349 | |
| 350 | // Build the instantiated method declaration. |
| 351 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 352 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); |
| 353 | DeclarationName Name |
Douglas Gregor | 49f25ec | 2009-05-15 21:18:27 +0000 | [diff] [blame] | 354 | = SemaRef.Context.DeclarationNames.getCXXConstructorName( |
| 355 | SemaRef.Context.getCanonicalType(ClassTy)); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 356 | CXXConstructorDecl *Constructor |
| 357 | = CXXConstructorDecl::Create(SemaRef.Context, Record, D->getLocation(), |
| 358 | Name, T, D->isExplicit(), D->isInline(), |
| 359 | false); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 360 | Constructor->setInstantiationOfMemberFunction(D); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 361 | |
| 362 | // Attach the parameters |
| 363 | for (unsigned P = 0; P < Params.size(); ++P) |
| 364 | Params[P]->setOwningFunction(Constructor); |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 365 | Constructor->setParams(SemaRef.Context, Params.data(), Params.size()); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 366 | |
| 367 | if (InitMethodInstantiation(Constructor, D)) |
| 368 | Constructor->setInvalidDecl(); |
| 369 | |
| 370 | NamedDecl *PrevDecl |
| 371 | = SemaRef.LookupQualifiedName(Owner, Name, Sema::LookupOrdinaryName, true); |
| 372 | |
| 373 | // In C++, the previous declaration we find might be a tag type |
| 374 | // (class or enum). In this case, the new declaration will hide the |
| 375 | // tag type. Note that this does does not apply if we're declaring a |
| 376 | // typedef (C++ [dcl.typedef]p4). |
| 377 | if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 378 | PrevDecl = 0; |
| 379 | bool Redeclaration = false; |
| 380 | bool OverloadableAttrRequired = false; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 381 | SemaRef.CheckFunctionDeclaration(Constructor, PrevDecl, Redeclaration, |
| 382 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 383 | |
Douglas Gregor | 49f25ec | 2009-05-15 21:18:27 +0000 | [diff] [blame] | 384 | Record->addedConstructor(SemaRef.Context, Constructor); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 385 | Owner->addDecl(SemaRef.Context, Constructor); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 386 | return Constructor; |
| 387 | } |
| 388 | |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 389 | Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 390 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 391 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 392 | llvm::SmallVector<ParmVarDecl *, 4> Params; |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 393 | QualType T = InstantiateFunctionType(D, Params); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 394 | if (T.isNull()) |
| 395 | return 0; |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 396 | assert(Params.size() == 0 && "Destructor with parameters?"); |
| 397 | |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 398 | // Build the instantiated destructor declaration. |
| 399 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
Douglas Gregor | 49f25ec | 2009-05-15 21:18:27 +0000 | [diff] [blame] | 400 | QualType ClassTy = |
| 401 | SemaRef.Context.getCanonicalType(SemaRef.Context.getTypeDeclType(Record)); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 402 | CXXDestructorDecl *Destructor |
| 403 | = CXXDestructorDecl::Create(SemaRef.Context, Record, |
| 404 | D->getLocation(), |
| 405 | SemaRef.Context.DeclarationNames.getCXXDestructorName(ClassTy), |
| 406 | T, D->isInline(), false); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 407 | Destructor->setInstantiationOfMemberFunction(D); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 408 | if (InitMethodInstantiation(Destructor, D)) |
| 409 | Destructor->setInvalidDecl(); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 410 | |
| 411 | bool Redeclaration = false; |
| 412 | bool OverloadableAttrRequired = false; |
| 413 | NamedDecl *PrevDecl = 0; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 414 | SemaRef.CheckFunctionDeclaration(Destructor, PrevDecl, Redeclaration, |
| 415 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 416 | Owner->addDecl(SemaRef.Context, Destructor); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 417 | return Destructor; |
| 418 | } |
| 419 | |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 420 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 421 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 422 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 423 | llvm::SmallVector<ParmVarDecl *, 4> Params; |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 424 | QualType T = InstantiateFunctionType(D, Params); |
| 425 | if (T.isNull()) |
| 426 | return 0; |
| 427 | assert(Params.size() == 0 && "Destructor with parameters?"); |
| 428 | |
| 429 | // Build the instantiated conversion declaration. |
| 430 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 431 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); |
| 432 | QualType ConvTy |
| 433 | = SemaRef.Context.getCanonicalType(T->getAsFunctionType()->getResultType()); |
| 434 | CXXConversionDecl *Conversion |
| 435 | = CXXConversionDecl::Create(SemaRef.Context, Record, |
| 436 | D->getLocation(), |
| 437 | SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(ConvTy), |
| 438 | T, D->isInline(), D->isExplicit()); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 439 | Conversion->setInstantiationOfMemberFunction(D); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 440 | if (InitMethodInstantiation(Conversion, D)) |
| 441 | Conversion->setInvalidDecl(); |
| 442 | |
| 443 | bool Redeclaration = false; |
| 444 | bool OverloadableAttrRequired = false; |
| 445 | NamedDecl *PrevDecl = 0; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 446 | SemaRef.CheckFunctionDeclaration(Conversion, PrevDecl, Redeclaration, |
| 447 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 448 | Owner->addDecl(SemaRef.Context, Conversion); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 449 | return Conversion; |
| 450 | } |
| 451 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 452 | ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 453 | QualType OrigT = SemaRef.InstantiateType(D->getOriginalType(), TemplateArgs, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 454 | D->getLocation(), D->getDeclName()); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 455 | if (OrigT.isNull()) |
| 456 | return 0; |
| 457 | |
| 458 | QualType T = SemaRef.adjustParameterType(OrigT); |
| 459 | |
| 460 | if (D->getDefaultArg()) { |
| 461 | // FIXME: Leave a marker for "uninstantiated" default |
| 462 | // arguments. They only get instantiated on demand at the call |
| 463 | // site. |
| 464 | unsigned DiagID = SemaRef.Diags.getCustomDiagID(Diagnostic::Warning, |
| 465 | "sorry, dropping default argument during template instantiation"); |
| 466 | SemaRef.Diag(D->getDefaultArg()->getSourceRange().getBegin(), DiagID) |
| 467 | << D->getDefaultArg()->getSourceRange(); |
| 468 | } |
| 469 | |
| 470 | // Allocate the parameter |
| 471 | ParmVarDecl *Param = 0; |
| 472 | if (T == OrigT) |
| 473 | Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 474 | D->getIdentifier(), T, D->getStorageClass(), |
| 475 | 0); |
| 476 | else |
| 477 | Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner, |
| 478 | D->getLocation(), D->getIdentifier(), |
| 479 | T, OrigT, D->getStorageClass(), 0); |
| 480 | |
| 481 | // Note: we don't try to instantiate function parameters until after |
| 482 | // we've instantiated the function's type. Therefore, we don't have |
| 483 | // to check for 'void' parameter types here. |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 484 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 485 | return Param; |
| 486 | } |
| 487 | |
| 488 | Decl * |
| 489 | TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) { |
| 490 | // Since parameter types can decay either before or after |
| 491 | // instantiation, we simply treat OriginalParmVarDecls as |
| 492 | // ParmVarDecls the same way, and create one or the other depending |
| 493 | // on what happens after template instantiation. |
| 494 | return VisitParmVarDecl(D); |
| 495 | } |
| 496 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 497 | Decl *Sema::InstantiateDecl(Decl *D, DeclContext *Owner, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 498 | const TemplateArgumentList &TemplateArgs) { |
| 499 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 500 | return Instantiator.Visit(D); |
| 501 | } |
| 502 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 503 | /// \brief Instantiates the type of the given function, including |
| 504 | /// instantiating all of the function parameters. |
| 505 | /// |
| 506 | /// \param D The function that we will be instantiated |
| 507 | /// |
| 508 | /// \param Params the instantiated parameter declarations |
| 509 | |
| 510 | /// \returns the instantiated function's type if successfull, a NULL |
| 511 | /// type if there was an error. |
| 512 | QualType |
| 513 | TemplateDeclInstantiator::InstantiateFunctionType(FunctionDecl *D, |
| 514 | llvm::SmallVectorImpl<ParmVarDecl *> &Params) { |
| 515 | bool InvalidDecl = false; |
| 516 | |
| 517 | // Instantiate the function parameters |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 518 | TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs); |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 519 | llvm::SmallVector<QualType, 4> ParamTys; |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 520 | for (FunctionDecl::param_iterator P = D->param_begin(), |
| 521 | PEnd = D->param_end(); |
| 522 | P != PEnd; ++P) { |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 523 | if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) { |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 524 | if (PInst->getType()->isVoidType()) { |
| 525 | SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type); |
| 526 | PInst->setInvalidDecl(); |
| 527 | } |
| 528 | else if (SemaRef.RequireNonAbstractType(PInst->getLocation(), |
| 529 | PInst->getType(), |
| 530 | diag::err_abstract_type_in_decl, |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 531 | Sema::AbstractParamType)) |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 532 | PInst->setInvalidDecl(); |
| 533 | |
| 534 | Params.push_back(PInst); |
| 535 | ParamTys.push_back(PInst->getType()); |
| 536 | |
| 537 | if (PInst->isInvalidDecl()) |
| 538 | InvalidDecl = true; |
| 539 | } else |
| 540 | InvalidDecl = true; |
| 541 | } |
| 542 | |
| 543 | // FIXME: Deallocate dead declarations. |
| 544 | if (InvalidDecl) |
| 545 | return QualType(); |
| 546 | |
| 547 | const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType(); |
| 548 | assert(Proto && "Missing prototype?"); |
| 549 | QualType ResultType |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 550 | = SemaRef.InstantiateType(Proto->getResultType(), TemplateArgs, |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 551 | D->getLocation(), D->getDeclName()); |
| 552 | if (ResultType.isNull()) |
| 553 | return QualType(); |
| 554 | |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 555 | return SemaRef.BuildFunctionType(ResultType, ParamTys.data(), ParamTys.size(), |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 556 | Proto->isVariadic(), Proto->getTypeQuals(), |
| 557 | D->getLocation(), D->getDeclName()); |
| 558 | } |
| 559 | |
| 560 | /// \brief Initializes common fields of an instantiated method |
| 561 | /// declaration (New) from the corresponding fields of its template |
| 562 | /// (Tmpl). |
| 563 | /// |
| 564 | /// \returns true if there was an error |
| 565 | bool |
| 566 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, |
| 567 | CXXMethodDecl *Tmpl) { |
| 568 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 569 | New->setAccess(Tmpl->getAccess()); |
Anders Carlsson | 77b7f1d | 2009-05-14 22:15:41 +0000 | [diff] [blame] | 570 | if (Tmpl->isVirtualAsWritten()) { |
| 571 | New->setVirtualAsWritten(true); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 572 | Record->setAggregate(false); |
| 573 | Record->setPOD(false); |
| 574 | Record->setPolymorphic(true); |
| 575 | } |
| 576 | if (Tmpl->isDeleted()) |
| 577 | New->setDeleted(); |
| 578 | if (Tmpl->isPure()) { |
| 579 | New->setPure(); |
| 580 | Record->setAbstract(true); |
| 581 | } |
| 582 | |
| 583 | // FIXME: attributes |
| 584 | // FIXME: New needs a pointer to Tmpl |
| 585 | return false; |
| 586 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 587 | |
| 588 | /// \brief Instantiate the definition of the given function from its |
| 589 | /// template. |
| 590 | /// |
| 591 | /// \param Function the already-instantiated declaration of a |
| 592 | /// function. |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 593 | void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, |
| 594 | FunctionDecl *Function) { |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 595 | // FIXME: make this work for function template specializations, too. |
| 596 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 597 | if (Function->isInvalidDecl()) |
| 598 | return; |
| 599 | |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 600 | // Find the function body that we'll be substituting. |
| 601 | const FunctionDecl *PatternDecl |
| 602 | = Function->getInstantiatedFromMemberFunction(); |
| 603 | Stmt *Pattern = 0; |
| 604 | if (PatternDecl) |
| 605 | Pattern = PatternDecl->getBody(Context, PatternDecl); |
| 606 | |
| 607 | if (!Pattern) |
| 608 | return; |
| 609 | |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 610 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); |
| 611 | if (Inst) |
| 612 | return; |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 613 | |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 614 | ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function)); |
| 615 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 616 | // Introduce a new scope where local variable instantiations will be |
| 617 | // recorded. |
| 618 | LocalInstantiationScope Scope(*this); |
| 619 | |
| 620 | // Introduce the instantiated function parameters into the local |
| 621 | // instantiation scope. |
| 622 | for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) |
| 623 | Scope.InstantiatedLocal(PatternDecl->getParamDecl(I), |
| 624 | Function->getParamDecl(I)); |
| 625 | |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 626 | // Enter the scope of this instantiation. We don't use |
| 627 | // PushDeclContext because we don't have a scope. |
| 628 | DeclContext *PreviousContext = CurContext; |
| 629 | CurContext = Function; |
| 630 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 631 | // Instantiate the function body. |
| 632 | OwningStmtResult Body |
| 633 | = InstantiateStmt(Pattern, getTemplateInstantiationArgs(Function)); |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 634 | |
| 635 | ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body), |
| 636 | /*IsInstantiation=*/true); |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 637 | |
| 638 | CurContext = PreviousContext; |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 639 | |
| 640 | DeclGroupRef DG(Function); |
| 641 | Consumer.HandleTopLevelDecl(DG); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | /// \brief Instantiate the definition of the given variable from its |
| 645 | /// template. |
| 646 | /// |
| 647 | /// \param Var the already-instantiated declaration of a variable. |
| 648 | void Sema::InstantiateVariableDefinition(VarDecl *Var) { |
| 649 | // FIXME: Implement this! |
| 650 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 651 | |
| 652 | static bool isInstantiationOf(ASTContext &Ctx, NamedDecl *D, Decl *Other) { |
| 653 | if (D->getKind() != Other->getKind()) |
| 654 | return false; |
| 655 | |
| 656 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Other)) |
| 657 | return Ctx.getCanonicalDecl(Record->getInstantiatedFromMemberClass()) |
| 658 | == Ctx.getCanonicalDecl(D); |
| 659 | |
| 660 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Other)) |
| 661 | return Ctx.getCanonicalDecl(Function->getInstantiatedFromMemberFunction()) |
| 662 | == Ctx.getCanonicalDecl(D); |
| 663 | |
Douglas Gregor | 8dbc3c6 | 2009-05-27 17:20:35 +0000 | [diff] [blame] | 664 | if (EnumDecl *Enum = dyn_cast<EnumDecl>(Other)) |
| 665 | return Ctx.getCanonicalDecl(Enum->getInstantiatedFromMemberEnum()) |
| 666 | == Ctx.getCanonicalDecl(D); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 667 | |
| 668 | // FIXME: How can we find instantiations of anonymous unions? |
| 669 | |
| 670 | return D->getDeclName() && isa<NamedDecl>(Other) && |
| 671 | D->getDeclName() == cast<NamedDecl>(Other)->getDeclName(); |
| 672 | } |
| 673 | |
| 674 | template<typename ForwardIterator> |
| 675 | static NamedDecl *findInstantiationOf(ASTContext &Ctx, |
| 676 | NamedDecl *D, |
| 677 | ForwardIterator first, |
| 678 | ForwardIterator last) { |
| 679 | for (; first != last; ++first) |
| 680 | if (isInstantiationOf(Ctx, D, *first)) |
| 681 | return cast<NamedDecl>(*first); |
| 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 686 | /// \brief Find the instantiation of the given declaration within the |
| 687 | /// current instantiation. |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 688 | /// |
| 689 | /// This routine is intended to be used when \p D is a declaration |
| 690 | /// referenced from within a template, that needs to mapped into the |
| 691 | /// corresponding declaration within an instantiation. For example, |
| 692 | /// given: |
| 693 | /// |
| 694 | /// \code |
| 695 | /// template<typename T> |
| 696 | /// struct X { |
| 697 | /// enum Kind { |
| 698 | /// KnownValue = sizeof(T) |
| 699 | /// }; |
| 700 | /// |
| 701 | /// bool getKind() const { return KnownValue; } |
| 702 | /// }; |
| 703 | /// |
| 704 | /// template struct X<int>; |
| 705 | /// \endcode |
| 706 | /// |
| 707 | /// In the instantiation of X<int>::getKind(), we need to map the |
| 708 | /// EnumConstantDecl for KnownValue (which refers to |
| 709 | /// X<T>::<Kind>::KnownValue) to its instantiation |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 710 | /// (X<int>::<Kind>::KnownValue). InstantiateCurrentDeclRef() performs |
| 711 | /// this mapping from within the instantiation of X<int>. |
| 712 | NamedDecl * Sema::InstantiateCurrentDeclRef(NamedDecl *D) { |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 713 | DeclContext *ParentDC = D->getDeclContext(); |
Douglas Gregor | 2bba76b | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 714 | if (isa<ParmVarDecl>(D) || ParentDC->isFunctionOrMethod()) { |
| 715 | // D is a local of some kind. Look into the map of local |
| 716 | // declarations to their instantiations. |
| 717 | return cast<NamedDecl>(CurrentInstantiationScope->getInstantiationOf(D)); |
| 718 | } |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 719 | |
Douglas Gregor | 2bba76b | 2009-05-27 17:07:49 +0000 | [diff] [blame] | 720 | if (NamedDecl *ParentDecl = dyn_cast<NamedDecl>(ParentDC)) { |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 721 | ParentDecl = InstantiateCurrentDeclRef(ParentDecl); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 722 | if (!ParentDecl) |
| 723 | return 0; |
| 724 | |
| 725 | ParentDC = cast<DeclContext>(ParentDecl); |
| 726 | } |
| 727 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 728 | if (ParentDC != D->getDeclContext()) { |
| 729 | // We performed some kind of instantiation in the parent context, |
| 730 | // so now we need to look into the instantiated parent context to |
| 731 | // find the instantiation of the declaration D. |
| 732 | NamedDecl *Result = 0; |
| 733 | if (D->getDeclName()) { |
| 734 | DeclContext::lookup_result Found |
| 735 | = ParentDC->lookup(Context, D->getDeclName()); |
| 736 | Result = findInstantiationOf(Context, D, Found.first, Found.second); |
| 737 | } else { |
| 738 | // Since we don't have a name for the entity we're looking for, |
| 739 | // our only option is to walk through all of the declarations to |
| 740 | // find that name. This will occur in a few cases: |
| 741 | // |
| 742 | // - anonymous struct/union within a template |
| 743 | // - unnamed class/struct/union/enum within a template |
| 744 | // |
| 745 | // FIXME: Find a better way to find these instantiations! |
| 746 | Result = findInstantiationOf(Context, D, |
| 747 | ParentDC->decls_begin(Context), |
| 748 | ParentDC->decls_end(Context)); |
| 749 | } |
| 750 | assert(Result && "Unable to find instantiation of declaration!"); |
| 751 | D = Result; |
| 752 | } |
| 753 | |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 754 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 755 | if (ClassTemplateDecl *ClassTemplate |
| 756 | = Record->getDescribedClassTemplate()) { |
| 757 | // When the declaration D was parsed, it referred to the current |
| 758 | // instantiation. Therefore, look through the current context, |
| 759 | // which contains actual instantiations, to find the |
| 760 | // instantiation of the "current instantiation" that D refers |
| 761 | // to. Alternatively, we could just instantiate the |
| 762 | // injected-class-name with the current template arguments, but |
| 763 | // such an instantiation is far more expensive. |
| 764 | for (DeclContext *DC = CurContext; !DC->isFileContext(); |
| 765 | DC = DC->getParent()) { |
| 766 | if (ClassTemplateSpecializationDecl *Spec |
| 767 | = dyn_cast<ClassTemplateSpecializationDecl>(DC)) |
| 768 | if (Context.getCanonicalDecl(Spec->getSpecializedTemplate()) |
| 769 | == Context.getCanonicalDecl(ClassTemplate)) |
| 770 | return Spec; |
| 771 | } |
| 772 | |
| 773 | assert(false && |
| 774 | "Unable to find declaration for the current instantiation"); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | return D; |
| 778 | } |