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