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" |
| 13 | #include "clang/AST/ASTContext.h" |
| 14 | #include "clang/AST/DeclTemplate.h" |
| 15 | #include "clang/AST/DeclVisitor.h" |
| 16 | #include "clang/AST/Expr.h" |
| 17 | #include "llvm/Support/Compiler.h" |
| 18 | |
| 19 | using namespace clang; |
| 20 | |
| 21 | namespace { |
| 22 | class VISIBILITY_HIDDEN TemplateDeclInstantiator |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 23 | : public DeclVisitor<TemplateDeclInstantiator, Decl *> { |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 24 | Sema &SemaRef; |
| 25 | DeclContext *Owner; |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 26 | const TemplateArgumentList &TemplateArgs; |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 27 | |
| 28 | public: |
| 29 | typedef Sema::OwningExprResult OwningExprResult; |
| 30 | |
| 31 | TemplateDeclInstantiator(Sema &SemaRef, DeclContext *Owner, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 32 | const TemplateArgumentList &TemplateArgs) |
| 33 | : SemaRef(SemaRef), Owner(Owner), TemplateArgs(TemplateArgs) { } |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 34 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 35 | // FIXME: Once we get closer to completion, replace these manually-written |
| 36 | // declarations with automatically-generated ones from |
| 37 | // clang/AST/DeclNodes.def. |
Douglas Gregor | 4f722be | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 38 | Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D); |
| 39 | Decl *VisitNamespaceDecl(NamespaceDecl *D); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 40 | Decl *VisitTypedefDecl(TypedefDecl *D); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 41 | Decl *VisitVarDecl(VarDecl *D); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 42 | Decl *VisitFieldDecl(FieldDecl *D); |
| 43 | Decl *VisitStaticAssertDecl(StaticAssertDecl *D); |
| 44 | Decl *VisitEnumDecl(EnumDecl *D); |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 45 | Decl *VisitEnumConstantDecl(EnumConstantDecl *D); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 46 | Decl *VisitCXXRecordDecl(CXXRecordDecl *D); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 47 | Decl *VisitCXXMethodDecl(CXXMethodDecl *D); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 48 | Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 49 | Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 50 | Decl *VisitCXXConversionDecl(CXXConversionDecl *D); |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 51 | ParmVarDecl *VisitParmVarDecl(ParmVarDecl *D); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 52 | Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 53 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 54 | // Base case. FIXME: Remove once we can instantiate everything. |
| 55 | Decl *VisitDecl(Decl *) { |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 56 | assert(false && "Template instantiation of unknown declaration kind!"); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 57 | return 0; |
| 58 | } |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 59 | |
| 60 | // Helper functions for instantiating methods. |
| 61 | QualType InstantiateFunctionType(FunctionDecl *D, |
| 62 | llvm::SmallVectorImpl<ParmVarDecl *> &Params); |
| 63 | bool InitMethodInstantiation(CXXMethodDecl *New, CXXMethodDecl *Tmpl); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 64 | }; |
| 65 | } |
| 66 | |
Douglas Gregor | 4f722be | 2009-03-25 15:45:12 +0000 | [diff] [blame] | 67 | Decl * |
| 68 | TemplateDeclInstantiator::VisitTranslationUnitDecl(TranslationUnitDecl *D) { |
| 69 | assert(false && "Translation units cannot be instantiated"); |
| 70 | return D; |
| 71 | } |
| 72 | |
| 73 | Decl * |
| 74 | TemplateDeclInstantiator::VisitNamespaceDecl(NamespaceDecl *D) { |
| 75 | assert(false && "Namespaces cannot be instantiated"); |
| 76 | return D; |
| 77 | } |
| 78 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 79 | Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { |
| 80 | bool Invalid = false; |
| 81 | QualType T = D->getUnderlyingType(); |
| 82 | if (T->isDependentType()) { |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 83 | T = SemaRef.InstantiateType(T, TemplateArgs, |
| 84 | D->getLocation(), D->getDeclName()); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 85 | if (T.isNull()) { |
| 86 | Invalid = true; |
| 87 | T = SemaRef.Context.IntTy; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Create the new typedef |
| 92 | TypedefDecl *Typedef |
| 93 | = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 94 | D->getIdentifier(), T); |
| 95 | if (Invalid) |
| 96 | Typedef->setInvalidDecl(); |
| 97 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 98 | Owner->addDecl(SemaRef.Context, Typedef); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 99 | return Typedef; |
| 100 | } |
| 101 | |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 102 | Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { |
| 103 | // Instantiate the type of the declaration |
| 104 | QualType T = SemaRef.InstantiateType(D->getType(), TemplateArgs, |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 105 | D->getTypeSpecStartLoc(), |
| 106 | D->getDeclName()); |
| 107 | if (T.isNull()) |
| 108 | return 0; |
| 109 | |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 110 | // Build the instantiated declaration |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 111 | VarDecl *Var = VarDecl::Create(SemaRef.Context, Owner, |
| 112 | D->getLocation(), D->getIdentifier(), |
| 113 | T, D->getStorageClass(), |
| 114 | D->getTypeSpecStartLoc()); |
| 115 | Var->setThreadSpecified(D->isThreadSpecified()); |
| 116 | Var->setCXXDirectInitializer(D->hasCXXDirectInitializer()); |
| 117 | Var->setDeclaredInCondition(D->isDeclaredInCondition()); |
| 118 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 119 | // FIXME: In theory, we could have a previous declaration for variables that |
| 120 | // are not static data members. |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 121 | bool Redeclaration = false; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 122 | SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 123 | Owner->addDecl(SemaRef.Context, Var); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 124 | |
| 125 | if (D->getInit()) { |
| 126 | OwningExprResult Init |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 127 | = SemaRef.InstantiateExpr(D->getInit(), TemplateArgs); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 128 | if (Init.isInvalid()) |
| 129 | Var->setInvalidDecl(); |
| 130 | else |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 131 | SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init), |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 132 | D->hasCXXDirectInitializer()); |
| 133 | } |
| 134 | |
| 135 | return Var; |
| 136 | } |
| 137 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 138 | Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { |
| 139 | bool Invalid = false; |
| 140 | QualType T = D->getType(); |
| 141 | if (T->isDependentType()) { |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 142 | T = SemaRef.InstantiateType(T, TemplateArgs, |
| 143 | D->getLocation(), D->getDeclName()); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 144 | if (!T.isNull() && T->isFunctionType()) { |
| 145 | // C++ [temp.arg.type]p3: |
| 146 | // If a declaration acquires a function type through a type |
| 147 | // dependent on a template-parameter and this causes a |
| 148 | // declaration that does not use the syntactic form of a |
| 149 | // function declarator to have function type, the program is |
| 150 | // ill-formed. |
| 151 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
| 152 | << T; |
| 153 | T = QualType(); |
| 154 | Invalid = true; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | Expr *BitWidth = D->getBitWidth(); |
| 159 | if (Invalid) |
| 160 | BitWidth = 0; |
| 161 | else if (BitWidth) { |
| 162 | OwningExprResult InstantiatedBitWidth |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 163 | = SemaRef.InstantiateExpr(BitWidth, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 164 | if (InstantiatedBitWidth.isInvalid()) { |
| 165 | Invalid = true; |
| 166 | BitWidth = 0; |
| 167 | } else |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 168 | BitWidth = InstantiatedBitWidth.takeAs<Expr>(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T, |
| 172 | cast<RecordDecl>(Owner), |
| 173 | D->getLocation(), |
| 174 | D->isMutable(), |
| 175 | BitWidth, |
| 176 | D->getAccess(), |
| 177 | 0); |
| 178 | if (Field) { |
| 179 | if (Invalid) |
| 180 | Field->setInvalidDecl(); |
| 181 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 182 | Owner->addDecl(SemaRef.Context, Field); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | return Field; |
| 186 | } |
| 187 | |
| 188 | Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 189 | Expr *AssertExpr = D->getAssertExpr(); |
| 190 | |
| 191 | OwningExprResult InstantiatedAssertExpr |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 192 | = SemaRef.InstantiateExpr(AssertExpr, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 193 | if (InstantiatedAssertExpr.isInvalid()) |
| 194 | return 0; |
| 195 | |
| 196 | OwningExprResult Message = SemaRef.Clone(D->getMessage()); |
| 197 | Decl *StaticAssert |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 198 | = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(), |
| 199 | move(InstantiatedAssertExpr), |
| 200 | move(Message)).getAs<Decl>(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 201 | return StaticAssert; |
| 202 | } |
| 203 | |
| 204 | Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { |
| 205 | EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, |
| 206 | D->getLocation(), D->getIdentifier(), |
| 207 | /*PrevDecl=*/0); |
Douglas Gregor | 06c0fec | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 208 | Enum->setAccess(D->getAccess()); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 209 | Owner->addDecl(SemaRef.Context, Enum); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 210 | Enum->startDefinition(); |
| 211 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 212 | llvm::SmallVector<Sema::DeclPtrTy, 16> Enumerators; |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 213 | |
| 214 | EnumConstantDecl *LastEnumConst = 0; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 215 | for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(SemaRef.Context), |
| 216 | ECEnd = D->enumerator_end(SemaRef.Context); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 217 | EC != ECEnd; ++EC) { |
| 218 | // The specified value for the enumerator. |
| 219 | OwningExprResult Value = SemaRef.Owned((Expr *)0); |
| 220 | if (Expr *UninstValue = EC->getInitExpr()) |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 221 | Value = SemaRef.InstantiateExpr(UninstValue, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 222 | |
| 223 | // Drop the initial value and continue. |
| 224 | bool isInvalid = false; |
| 225 | if (Value.isInvalid()) { |
| 226 | Value = SemaRef.Owned((Expr *)0); |
| 227 | isInvalid = true; |
| 228 | } |
| 229 | |
| 230 | EnumConstantDecl *EnumConst |
| 231 | = SemaRef.CheckEnumConstant(Enum, LastEnumConst, |
| 232 | EC->getLocation(), EC->getIdentifier(), |
| 233 | move(Value)); |
| 234 | |
| 235 | if (isInvalid) { |
| 236 | if (EnumConst) |
| 237 | EnumConst->setInvalidDecl(); |
| 238 | Enum->setInvalidDecl(); |
| 239 | } |
| 240 | |
| 241 | if (EnumConst) { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 242 | Enum->addDecl(SemaRef.Context, EnumConst); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 243 | Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst)); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 244 | LastEnumConst = EnumConst; |
| 245 | } |
| 246 | } |
| 247 | |
Mike Stump | c6e35aa | 2009-05-16 07:06:02 +0000 | [diff] [blame] | 248 | // FIXME: Fixup LBraceLoc and RBraceLoc |
| 249 | SemaRef.ActOnEnumBody(Enum->getLocation(), SourceLocation(), SourceLocation(), |
| 250 | Sema::DeclPtrTy::make(Enum), |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 251 | &Enumerators[0], Enumerators.size()); |
| 252 | |
| 253 | return Enum; |
| 254 | } |
| 255 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 256 | Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 257 | assert(false && "EnumConstantDecls can only occur within EnumDecls."); |
| 258 | return 0; |
| 259 | } |
| 260 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 261 | Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 262 | CXXRecordDecl *PrevDecl = 0; |
| 263 | if (D->isInjectedClassName()) |
| 264 | PrevDecl = cast<CXXRecordDecl>(Owner); |
| 265 | |
| 266 | CXXRecordDecl *Record |
| 267 | = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner, |
| 268 | D->getLocation(), D->getIdentifier(), PrevDecl); |
| 269 | Record->setImplicit(D->isImplicit()); |
| 270 | Record->setAccess(D->getAccess()); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 271 | if (!D->isInjectedClassName()) |
| 272 | Record->setInstantiationOfMemberClass(D); |
| 273 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 274 | Owner->addDecl(SemaRef.Context, Record); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 275 | return Record; |
| 276 | } |
| 277 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 278 | Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) { |
| 279 | // Only handle actual methods; we'll deal with constructors, |
| 280 | // destructors, etc. separately. |
| 281 | if (D->getKind() != Decl::CXXMethod) |
| 282 | return 0; |
| 283 | |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 284 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 285 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 286 | llvm::SmallVector<ParmVarDecl *, 16> Params; |
| 287 | QualType T = InstantiateFunctionType(D, Params); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 288 | if (T.isNull()) |
| 289 | return 0; |
| 290 | |
| 291 | // Build the instantiated method declaration. |
| 292 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 293 | CXXMethodDecl *Method |
| 294 | = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(), |
| 295 | D->getDeclName(), T, D->isStatic(), |
| 296 | D->isInline()); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 297 | Method->setInstantiationOfMemberFunction(D); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 298 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 299 | // Attach the parameters |
| 300 | for (unsigned P = 0; P < Params.size(); ++P) |
| 301 | Params[P]->setOwningFunction(Method); |
| 302 | Method->setParams(SemaRef.Context, &Params[0], Params.size()); |
| 303 | |
| 304 | if (InitMethodInstantiation(Method, D)) |
| 305 | Method->setInvalidDecl(); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 306 | |
| 307 | NamedDecl *PrevDecl |
| 308 | = SemaRef.LookupQualifiedName(Owner, Method->getDeclName(), |
| 309 | Sema::LookupOrdinaryName, true); |
| 310 | // In C++, the previous declaration we find might be a tag type |
| 311 | // (class or enum). In this case, the new declaration will hide the |
| 312 | // tag type. Note that this does does not apply if we're declaring a |
| 313 | // typedef (C++ [dcl.typedef]p4). |
| 314 | if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 315 | PrevDecl = 0; |
| 316 | bool Redeclaration = false; |
| 317 | bool OverloadableAttrRequired = false; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 318 | SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration, |
| 319 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 320 | |
| 321 | if (!Method->isInvalidDecl() || !PrevDecl) |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 322 | Owner->addDecl(SemaRef.Context, Method); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 323 | return Method; |
| 324 | } |
| 325 | |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 326 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 327 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 328 | |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 329 | llvm::SmallVector<ParmVarDecl *, 16> Params; |
| 330 | QualType T = InstantiateFunctionType(D, Params); |
| 331 | if (T.isNull()) |
| 332 | return 0; |
| 333 | |
| 334 | // Build the instantiated method declaration. |
| 335 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 336 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); |
| 337 | DeclarationName Name |
Douglas Gregor | 49f25ec | 2009-05-15 21:18:27 +0000 | [diff] [blame] | 338 | = SemaRef.Context.DeclarationNames.getCXXConstructorName( |
| 339 | SemaRef.Context.getCanonicalType(ClassTy)); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 340 | CXXConstructorDecl *Constructor |
| 341 | = CXXConstructorDecl::Create(SemaRef.Context, Record, D->getLocation(), |
| 342 | Name, T, D->isExplicit(), D->isInline(), |
| 343 | false); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 344 | Constructor->setInstantiationOfMemberFunction(D); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 345 | |
| 346 | // Attach the parameters |
| 347 | for (unsigned P = 0; P < Params.size(); ++P) |
| 348 | Params[P]->setOwningFunction(Constructor); |
| 349 | Constructor->setParams(SemaRef.Context, &Params[0], Params.size()); |
| 350 | |
| 351 | if (InitMethodInstantiation(Constructor, D)) |
| 352 | Constructor->setInvalidDecl(); |
| 353 | |
| 354 | NamedDecl *PrevDecl |
| 355 | = SemaRef.LookupQualifiedName(Owner, Name, Sema::LookupOrdinaryName, true); |
| 356 | |
| 357 | // In C++, the previous declaration we find might be a tag type |
| 358 | // (class or enum). In this case, the new declaration will hide the |
| 359 | // tag type. Note that this does does not apply if we're declaring a |
| 360 | // typedef (C++ [dcl.typedef]p4). |
| 361 | if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 362 | PrevDecl = 0; |
| 363 | bool Redeclaration = false; |
| 364 | bool OverloadableAttrRequired = false; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 365 | SemaRef.CheckFunctionDeclaration(Constructor, PrevDecl, Redeclaration, |
| 366 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 367 | |
Douglas Gregor | 49f25ec | 2009-05-15 21:18:27 +0000 | [diff] [blame] | 368 | Record->addedConstructor(SemaRef.Context, Constructor); |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 369 | Owner->addDecl(SemaRef.Context, Constructor); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 370 | return Constructor; |
| 371 | } |
| 372 | |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 373 | Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 374 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 375 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 376 | llvm::SmallVector<ParmVarDecl *, 16> Params; |
| 377 | QualType T = InstantiateFunctionType(D, Params); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 378 | if (T.isNull()) |
| 379 | return 0; |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 380 | assert(Params.size() == 0 && "Destructor with parameters?"); |
| 381 | |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 382 | // Build the instantiated destructor declaration. |
| 383 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
Douglas Gregor | 49f25ec | 2009-05-15 21:18:27 +0000 | [diff] [blame] | 384 | QualType ClassTy = |
| 385 | SemaRef.Context.getCanonicalType(SemaRef.Context.getTypeDeclType(Record)); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 386 | CXXDestructorDecl *Destructor |
| 387 | = CXXDestructorDecl::Create(SemaRef.Context, Record, |
| 388 | D->getLocation(), |
| 389 | SemaRef.Context.DeclarationNames.getCXXDestructorName(ClassTy), |
| 390 | T, D->isInline(), false); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 391 | Destructor->setInstantiationOfMemberFunction(D); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 392 | if (InitMethodInstantiation(Destructor, D)) |
| 393 | Destructor->setInvalidDecl(); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 394 | |
| 395 | bool Redeclaration = false; |
| 396 | bool OverloadableAttrRequired = false; |
| 397 | NamedDecl *PrevDecl = 0; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 398 | SemaRef.CheckFunctionDeclaration(Destructor, PrevDecl, Redeclaration, |
| 399 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 400 | Owner->addDecl(SemaRef.Context, Destructor); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 401 | return Destructor; |
| 402 | } |
| 403 | |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 404 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 405 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 406 | |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 407 | llvm::SmallVector<ParmVarDecl *, 16> Params; |
| 408 | QualType T = InstantiateFunctionType(D, Params); |
| 409 | if (T.isNull()) |
| 410 | return 0; |
| 411 | assert(Params.size() == 0 && "Destructor with parameters?"); |
| 412 | |
| 413 | // Build the instantiated conversion declaration. |
| 414 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 415 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); |
| 416 | QualType ConvTy |
| 417 | = SemaRef.Context.getCanonicalType(T->getAsFunctionType()->getResultType()); |
| 418 | CXXConversionDecl *Conversion |
| 419 | = CXXConversionDecl::Create(SemaRef.Context, Record, |
| 420 | D->getLocation(), |
| 421 | SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(ConvTy), |
| 422 | T, D->isInline(), D->isExplicit()); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 423 | Conversion->setInstantiationOfMemberFunction(D); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 424 | if (InitMethodInstantiation(Conversion, D)) |
| 425 | Conversion->setInvalidDecl(); |
| 426 | |
| 427 | bool Redeclaration = false; |
| 428 | bool OverloadableAttrRequired = false; |
| 429 | NamedDecl *PrevDecl = 0; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 430 | SemaRef.CheckFunctionDeclaration(Conversion, PrevDecl, Redeclaration, |
| 431 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 432 | Owner->addDecl(SemaRef.Context, Conversion); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 433 | return Conversion; |
| 434 | } |
| 435 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 436 | ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 437 | QualType OrigT = SemaRef.InstantiateType(D->getOriginalType(), TemplateArgs, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 438 | D->getLocation(), D->getDeclName()); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 439 | if (OrigT.isNull()) |
| 440 | return 0; |
| 441 | |
| 442 | QualType T = SemaRef.adjustParameterType(OrigT); |
| 443 | |
| 444 | if (D->getDefaultArg()) { |
| 445 | // FIXME: Leave a marker for "uninstantiated" default |
| 446 | // arguments. They only get instantiated on demand at the call |
| 447 | // site. |
| 448 | unsigned DiagID = SemaRef.Diags.getCustomDiagID(Diagnostic::Warning, |
| 449 | "sorry, dropping default argument during template instantiation"); |
| 450 | SemaRef.Diag(D->getDefaultArg()->getSourceRange().getBegin(), DiagID) |
| 451 | << D->getDefaultArg()->getSourceRange(); |
| 452 | } |
| 453 | |
| 454 | // Allocate the parameter |
| 455 | ParmVarDecl *Param = 0; |
| 456 | if (T == OrigT) |
| 457 | Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 458 | D->getIdentifier(), T, D->getStorageClass(), |
| 459 | 0); |
| 460 | else |
| 461 | Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner, |
| 462 | D->getLocation(), D->getIdentifier(), |
| 463 | T, OrigT, D->getStorageClass(), 0); |
| 464 | |
| 465 | // Note: we don't try to instantiate function parameters until after |
| 466 | // we've instantiated the function's type. Therefore, we don't have |
| 467 | // to check for 'void' parameter types here. |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 468 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 469 | return Param; |
| 470 | } |
| 471 | |
| 472 | Decl * |
| 473 | TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) { |
| 474 | // Since parameter types can decay either before or after |
| 475 | // instantiation, we simply treat OriginalParmVarDecls as |
| 476 | // ParmVarDecls the same way, and create one or the other depending |
| 477 | // on what happens after template instantiation. |
| 478 | return VisitParmVarDecl(D); |
| 479 | } |
| 480 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 481 | Decl *Sema::InstantiateDecl(Decl *D, DeclContext *Owner, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 482 | const TemplateArgumentList &TemplateArgs) { |
| 483 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 484 | return Instantiator.Visit(D); |
| 485 | } |
| 486 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 487 | /// \brief Instantiates the type of the given function, including |
| 488 | /// instantiating all of the function parameters. |
| 489 | /// |
| 490 | /// \param D The function that we will be instantiated |
| 491 | /// |
| 492 | /// \param Params the instantiated parameter declarations |
| 493 | |
| 494 | /// \returns the instantiated function's type if successfull, a NULL |
| 495 | /// type if there was an error. |
| 496 | QualType |
| 497 | TemplateDeclInstantiator::InstantiateFunctionType(FunctionDecl *D, |
| 498 | llvm::SmallVectorImpl<ParmVarDecl *> &Params) { |
| 499 | bool InvalidDecl = false; |
| 500 | |
| 501 | // Instantiate the function parameters |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 502 | TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 503 | llvm::SmallVector<QualType, 16> ParamTys; |
| 504 | for (FunctionDecl::param_iterator P = D->param_begin(), |
| 505 | PEnd = D->param_end(); |
| 506 | P != PEnd; ++P) { |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 507 | if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) { |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 508 | if (PInst->getType()->isVoidType()) { |
| 509 | SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type); |
| 510 | PInst->setInvalidDecl(); |
| 511 | } |
| 512 | else if (SemaRef.RequireNonAbstractType(PInst->getLocation(), |
| 513 | PInst->getType(), |
| 514 | diag::err_abstract_type_in_decl, |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 515 | Sema::AbstractParamType)) |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 516 | PInst->setInvalidDecl(); |
| 517 | |
| 518 | Params.push_back(PInst); |
| 519 | ParamTys.push_back(PInst->getType()); |
| 520 | |
| 521 | if (PInst->isInvalidDecl()) |
| 522 | InvalidDecl = true; |
| 523 | } else |
| 524 | InvalidDecl = true; |
| 525 | } |
| 526 | |
| 527 | // FIXME: Deallocate dead declarations. |
| 528 | if (InvalidDecl) |
| 529 | return QualType(); |
| 530 | |
| 531 | const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType(); |
| 532 | assert(Proto && "Missing prototype?"); |
| 533 | QualType ResultType |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 534 | = SemaRef.InstantiateType(Proto->getResultType(), TemplateArgs, |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 535 | D->getLocation(), D->getDeclName()); |
| 536 | if (ResultType.isNull()) |
| 537 | return QualType(); |
| 538 | |
| 539 | return SemaRef.BuildFunctionType(ResultType, &ParamTys[0], ParamTys.size(), |
| 540 | Proto->isVariadic(), Proto->getTypeQuals(), |
| 541 | D->getLocation(), D->getDeclName()); |
| 542 | } |
| 543 | |
| 544 | /// \brief Initializes common fields of an instantiated method |
| 545 | /// declaration (New) from the corresponding fields of its template |
| 546 | /// (Tmpl). |
| 547 | /// |
| 548 | /// \returns true if there was an error |
| 549 | bool |
| 550 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, |
| 551 | CXXMethodDecl *Tmpl) { |
| 552 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 553 | New->setAccess(Tmpl->getAccess()); |
Anders Carlsson | 77b7f1d | 2009-05-14 22:15:41 +0000 | [diff] [blame] | 554 | if (Tmpl->isVirtualAsWritten()) { |
| 555 | New->setVirtualAsWritten(true); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 556 | Record->setAggregate(false); |
| 557 | Record->setPOD(false); |
| 558 | Record->setPolymorphic(true); |
| 559 | } |
| 560 | if (Tmpl->isDeleted()) |
| 561 | New->setDeleted(); |
| 562 | if (Tmpl->isPure()) { |
| 563 | New->setPure(); |
| 564 | Record->setAbstract(true); |
| 565 | } |
| 566 | |
| 567 | // FIXME: attributes |
| 568 | // FIXME: New needs a pointer to Tmpl |
| 569 | return false; |
| 570 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 571 | |
| 572 | /// \brief Instantiate the definition of the given function from its |
| 573 | /// template. |
| 574 | /// |
| 575 | /// \param Function the already-instantiated declaration of a |
| 576 | /// function. |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 577 | void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, |
| 578 | FunctionDecl *Function) { |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 579 | // FIXME: make this work for function template specializations, too. |
| 580 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 581 | if (Function->isInvalidDecl()) |
| 582 | return; |
| 583 | |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 584 | // Find the function body that we'll be substituting. |
| 585 | const FunctionDecl *PatternDecl |
| 586 | = Function->getInstantiatedFromMemberFunction(); |
| 587 | Stmt *Pattern = 0; |
| 588 | if (PatternDecl) |
| 589 | Pattern = PatternDecl->getBody(Context, PatternDecl); |
| 590 | |
| 591 | if (!Pattern) |
| 592 | return; |
| 593 | |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 594 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Function); |
| 595 | if (Inst) |
| 596 | return; |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 597 | |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 598 | ActOnStartOfFunctionDef(0, DeclPtrTy::make(Function)); |
| 599 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 600 | // Introduce a new scope where local variable instantiations will be |
| 601 | // recorded. |
| 602 | LocalInstantiationScope Scope(*this); |
| 603 | |
| 604 | // Introduce the instantiated function parameters into the local |
| 605 | // instantiation scope. |
| 606 | for (unsigned I = 0, N = PatternDecl->getNumParams(); I != N; ++I) |
| 607 | Scope.InstantiatedLocal(PatternDecl->getParamDecl(I), |
| 608 | Function->getParamDecl(I)); |
| 609 | |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 610 | // Enter the scope of this instantiation. We don't use |
| 611 | // PushDeclContext because we don't have a scope. |
| 612 | DeclContext *PreviousContext = CurContext; |
| 613 | CurContext = Function; |
| 614 | |
Douglas Gregor | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 615 | // Instantiate the function body. |
| 616 | OwningStmtResult Body |
| 617 | = InstantiateStmt(Pattern, getTemplateInstantiationArgs(Function)); |
Douglas Gregor | e2c31ff | 2009-05-15 17:59:04 +0000 | [diff] [blame] | 618 | |
| 619 | ActOnFinishFunctionBody(DeclPtrTy::make(Function), move(Body), |
| 620 | /*IsInstantiation=*/true); |
Douglas Gregor | b9f1b8d | 2009-05-15 00:01:03 +0000 | [diff] [blame] | 621 | |
| 622 | CurContext = PreviousContext; |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | /// \brief Instantiate the definition of the given variable from its |
| 626 | /// template. |
| 627 | /// |
| 628 | /// \param Var the already-instantiated declaration of a variable. |
| 629 | void Sema::InstantiateVariableDefinition(VarDecl *Var) { |
| 630 | // FIXME: Implement this! |
| 631 | } |