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