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