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 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 102 | Owner->addDecl(SemaRef.Context, Typedef); |
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, |
| 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; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 127 | SemaRef.CheckVariableDeclaration(Var, 0, Redeclaration); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 128 | Owner->addDecl(SemaRef.Context, Var); |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 129 | |
| 130 | if (D->getInit()) { |
| 131 | OwningExprResult Init |
| 132 | = SemaRef.InstantiateExpr(D->getInit(), TemplateArgs, NumTemplateArgs); |
| 133 | if (Init.isInvalid()) |
| 134 | Var->setInvalidDecl(); |
| 135 | else |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 136 | SemaRef.AddInitializerToDecl(Sema::DeclPtrTy::make(Var), move(Init), |
Douglas Gregor | 3d7a12a | 2009-03-25 23:32:15 +0000 | [diff] [blame] | 137 | D->hasCXXDirectInitializer()); |
| 138 | } |
| 139 | |
| 140 | return Var; |
| 141 | } |
| 142 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 143 | Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { |
| 144 | bool Invalid = false; |
| 145 | QualType T = D->getType(); |
| 146 | if (T->isDependentType()) { |
| 147 | T = SemaRef.InstantiateType(T, TemplateArgs, NumTemplateArgs, |
| 148 | D->getLocation(), |
| 149 | D->getDeclName()); |
| 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) { |
| 168 | OwningExprResult InstantiatedBitWidth |
| 169 | = SemaRef.InstantiateExpr(BitWidth, TemplateArgs, NumTemplateArgs); |
| 170 | if (InstantiatedBitWidth.isInvalid()) { |
| 171 | Invalid = true; |
| 172 | BitWidth = 0; |
| 173 | } else |
| 174 | BitWidth = (Expr *)InstantiatedBitWidth.release(); |
| 175 | } |
| 176 | |
| 177 | FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T, |
| 178 | cast<RecordDecl>(Owner), |
| 179 | D->getLocation(), |
| 180 | D->isMutable(), |
| 181 | BitWidth, |
| 182 | D->getAccess(), |
| 183 | 0); |
| 184 | if (Field) { |
| 185 | if (Invalid) |
| 186 | Field->setInvalidDecl(); |
| 187 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 188 | Owner->addDecl(SemaRef.Context, Field); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | return Field; |
| 192 | } |
| 193 | |
| 194 | Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 195 | Expr *AssertExpr = D->getAssertExpr(); |
| 196 | |
| 197 | OwningExprResult InstantiatedAssertExpr |
| 198 | = SemaRef.InstantiateExpr(AssertExpr, TemplateArgs, NumTemplateArgs); |
| 199 | if (InstantiatedAssertExpr.isInvalid()) |
| 200 | return 0; |
| 201 | |
| 202 | OwningExprResult Message = SemaRef.Clone(D->getMessage()); |
| 203 | Decl *StaticAssert |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 204 | = SemaRef.ActOnStaticAssertDeclaration(D->getLocation(), |
| 205 | move(InstantiatedAssertExpr), |
| 206 | move(Message)).getAs<Decl>(); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 207 | return StaticAssert; |
| 208 | } |
| 209 | |
| 210 | Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { |
| 211 | EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, |
| 212 | D->getLocation(), D->getIdentifier(), |
| 213 | /*PrevDecl=*/0); |
Douglas Gregor | 06c0fec | 2009-03-25 22:00:53 +0000 | [diff] [blame] | 214 | Enum->setAccess(D->getAccess()); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 215 | Owner->addDecl(SemaRef.Context, Enum); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 216 | Enum->startDefinition(); |
| 217 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 218 | llvm::SmallVector<Sema::DeclPtrTy, 16> Enumerators; |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 219 | |
| 220 | EnumConstantDecl *LastEnumConst = 0; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 221 | for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(SemaRef.Context), |
| 222 | ECEnd = D->enumerator_end(SemaRef.Context); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 223 | EC != ECEnd; ++EC) { |
| 224 | // The specified value for the enumerator. |
| 225 | OwningExprResult Value = SemaRef.Owned((Expr *)0); |
| 226 | if (Expr *UninstValue = EC->getInitExpr()) |
| 227 | Value = SemaRef.InstantiateExpr(UninstValue, |
| 228 | TemplateArgs, NumTemplateArgs); |
| 229 | |
| 230 | // Drop the initial value and continue. |
| 231 | bool isInvalid = false; |
| 232 | if (Value.isInvalid()) { |
| 233 | Value = SemaRef.Owned((Expr *)0); |
| 234 | isInvalid = true; |
| 235 | } |
| 236 | |
| 237 | EnumConstantDecl *EnumConst |
| 238 | = SemaRef.CheckEnumConstant(Enum, LastEnumConst, |
| 239 | EC->getLocation(), EC->getIdentifier(), |
| 240 | move(Value)); |
| 241 | |
| 242 | if (isInvalid) { |
| 243 | if (EnumConst) |
| 244 | EnumConst->setInvalidDecl(); |
| 245 | Enum->setInvalidDecl(); |
| 246 | } |
| 247 | |
| 248 | if (EnumConst) { |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 249 | Enum->addDecl(SemaRef.Context, EnumConst); |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 250 | Enumerators.push_back(Sema::DeclPtrTy::make(EnumConst)); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 251 | LastEnumConst = EnumConst; |
| 252 | } |
| 253 | } |
| 254 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 255 | SemaRef.ActOnEnumBody(Enum->getLocation(), Sema::DeclPtrTy::make(Enum), |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 256 | &Enumerators[0], Enumerators.size()); |
| 257 | |
| 258 | return Enum; |
| 259 | } |
| 260 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 261 | Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 262 | assert(false && "EnumConstantDecls can only occur within EnumDecls."); |
| 263 | return 0; |
| 264 | } |
| 265 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 266 | Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 267 | CXXRecordDecl *PrevDecl = 0; |
| 268 | if (D->isInjectedClassName()) |
| 269 | PrevDecl = cast<CXXRecordDecl>(Owner); |
| 270 | |
| 271 | CXXRecordDecl *Record |
| 272 | = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner, |
| 273 | D->getLocation(), D->getIdentifier(), PrevDecl); |
| 274 | Record->setImplicit(D->isImplicit()); |
| 275 | Record->setAccess(D->getAccess()); |
| 276 | |
| 277 | if (!D->isInjectedClassName()) |
| 278 | Record->setInstantiationOfMemberClass(D); |
Douglas Gregor | befc20e | 2009-03-26 00:10:35 +0000 | [diff] [blame] | 279 | else |
| 280 | Record->setDescribedClassTemplate(D->getDescribedClassTemplate()); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 281 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 282 | Owner->addDecl(SemaRef.Context, Record); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 283 | return Record; |
| 284 | } |
| 285 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 286 | Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) { |
| 287 | // Only handle actual methods; we'll deal with constructors, |
| 288 | // destructors, etc. separately. |
| 289 | if (D->getKind() != Decl::CXXMethod) |
| 290 | return 0; |
| 291 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 292 | llvm::SmallVector<ParmVarDecl *, 16> Params; |
| 293 | QualType T = InstantiateFunctionType(D, Params); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 294 | if (T.isNull()) |
| 295 | return 0; |
| 296 | |
| 297 | // Build the instantiated method declaration. |
| 298 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 299 | CXXMethodDecl *Method |
| 300 | = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(), |
| 301 | D->getDeclName(), T, D->isStatic(), |
| 302 | D->isInline()); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 303 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 304 | // Attach the parameters |
| 305 | for (unsigned P = 0; P < Params.size(); ++P) |
| 306 | Params[P]->setOwningFunction(Method); |
| 307 | Method->setParams(SemaRef.Context, &Params[0], Params.size()); |
| 308 | |
| 309 | if (InitMethodInstantiation(Method, D)) |
| 310 | Method->setInvalidDecl(); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 311 | |
| 312 | NamedDecl *PrevDecl |
| 313 | = SemaRef.LookupQualifiedName(Owner, Method->getDeclName(), |
| 314 | Sema::LookupOrdinaryName, true); |
| 315 | // In C++, the previous declaration we find might be a tag type |
| 316 | // (class or enum). In this case, the new declaration will hide the |
| 317 | // tag type. Note that this does does not apply if we're declaring a |
| 318 | // typedef (C++ [dcl.typedef]p4). |
| 319 | if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 320 | PrevDecl = 0; |
| 321 | bool Redeclaration = false; |
| 322 | bool OverloadableAttrRequired = false; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 323 | SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration, |
| 324 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 325 | |
| 326 | if (!Method->isInvalidDecl() || !PrevDecl) |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 327 | Owner->addDecl(SemaRef.Context, Method); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 328 | return Method; |
| 329 | } |
| 330 | |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 331 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
| 332 | llvm::SmallVector<ParmVarDecl *, 16> Params; |
| 333 | QualType T = InstantiateFunctionType(D, Params); |
| 334 | if (T.isNull()) |
| 335 | return 0; |
| 336 | |
| 337 | // Build the instantiated method declaration. |
| 338 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 339 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); |
| 340 | DeclarationName Name |
| 341 | = SemaRef.Context.DeclarationNames.getCXXConstructorName(ClassTy); |
| 342 | CXXConstructorDecl *Constructor |
| 343 | = CXXConstructorDecl::Create(SemaRef.Context, Record, D->getLocation(), |
| 344 | Name, T, D->isExplicit(), D->isInline(), |
| 345 | false); |
| 346 | |
| 347 | // Attach the parameters |
| 348 | for (unsigned P = 0; P < Params.size(); ++P) |
| 349 | Params[P]->setOwningFunction(Constructor); |
| 350 | Constructor->setParams(SemaRef.Context, &Params[0], Params.size()); |
| 351 | |
| 352 | if (InitMethodInstantiation(Constructor, D)) |
| 353 | Constructor->setInvalidDecl(); |
| 354 | |
| 355 | NamedDecl *PrevDecl |
| 356 | = SemaRef.LookupQualifiedName(Owner, Name, Sema::LookupOrdinaryName, true); |
| 357 | |
| 358 | // In C++, the previous declaration we find might be a tag type |
| 359 | // (class or enum). In this case, the new declaration will hide the |
| 360 | // tag type. Note that this does does not apply if we're declaring a |
| 361 | // typedef (C++ [dcl.typedef]p4). |
| 362 | if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 363 | PrevDecl = 0; |
| 364 | bool Redeclaration = false; |
| 365 | bool OverloadableAttrRequired = false; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 366 | SemaRef.CheckFunctionDeclaration(Constructor, PrevDecl, Redeclaration, |
| 367 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 368 | |
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 | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 374 | llvm::SmallVector<ParmVarDecl *, 16> Params; |
| 375 | QualType T = InstantiateFunctionType(D, Params); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 376 | if (T.isNull()) |
| 377 | return 0; |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 378 | assert(Params.size() == 0 && "Destructor with parameters?"); |
| 379 | |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 380 | // Build the instantiated destructor declaration. |
| 381 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 382 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); |
| 383 | CXXDestructorDecl *Destructor |
| 384 | = CXXDestructorDecl::Create(SemaRef.Context, Record, |
| 385 | D->getLocation(), |
| 386 | SemaRef.Context.DeclarationNames.getCXXDestructorName(ClassTy), |
| 387 | T, D->isInline(), false); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 388 | if (InitMethodInstantiation(Destructor, D)) |
| 389 | Destructor->setInvalidDecl(); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 390 | |
| 391 | bool Redeclaration = false; |
| 392 | bool OverloadableAttrRequired = false; |
| 393 | NamedDecl *PrevDecl = 0; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 394 | SemaRef.CheckFunctionDeclaration(Destructor, PrevDecl, Redeclaration, |
| 395 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 396 | Owner->addDecl(SemaRef.Context, Destructor); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 397 | return Destructor; |
| 398 | } |
| 399 | |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 400 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { |
| 401 | llvm::SmallVector<ParmVarDecl *, 16> Params; |
| 402 | QualType T = InstantiateFunctionType(D, Params); |
| 403 | if (T.isNull()) |
| 404 | return 0; |
| 405 | assert(Params.size() == 0 && "Destructor with parameters?"); |
| 406 | |
| 407 | // Build the instantiated conversion declaration. |
| 408 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 409 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); |
| 410 | QualType ConvTy |
| 411 | = SemaRef.Context.getCanonicalType(T->getAsFunctionType()->getResultType()); |
| 412 | CXXConversionDecl *Conversion |
| 413 | = CXXConversionDecl::Create(SemaRef.Context, Record, |
| 414 | D->getLocation(), |
| 415 | SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(ConvTy), |
| 416 | T, D->isInline(), D->isExplicit()); |
| 417 | if (InitMethodInstantiation(Conversion, D)) |
| 418 | Conversion->setInvalidDecl(); |
| 419 | |
| 420 | bool Redeclaration = false; |
| 421 | bool OverloadableAttrRequired = false; |
| 422 | NamedDecl *PrevDecl = 0; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 423 | SemaRef.CheckFunctionDeclaration(Conversion, PrevDecl, Redeclaration, |
| 424 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 425 | Owner->addDecl(SemaRef.Context, Conversion); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 426 | return Conversion; |
| 427 | } |
| 428 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 429 | ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 430 | QualType OrigT = SemaRef.InstantiateType(D->getOriginalType(), TemplateArgs, |
| 431 | NumTemplateArgs, D->getLocation(), |
| 432 | D->getDeclName()); |
| 433 | if (OrigT.isNull()) |
| 434 | return 0; |
| 435 | |
| 436 | QualType T = SemaRef.adjustParameterType(OrigT); |
| 437 | |
| 438 | if (D->getDefaultArg()) { |
| 439 | // FIXME: Leave a marker for "uninstantiated" default |
| 440 | // arguments. They only get instantiated on demand at the call |
| 441 | // site. |
| 442 | unsigned DiagID = SemaRef.Diags.getCustomDiagID(Diagnostic::Warning, |
| 443 | "sorry, dropping default argument during template instantiation"); |
| 444 | SemaRef.Diag(D->getDefaultArg()->getSourceRange().getBegin(), DiagID) |
| 445 | << D->getDefaultArg()->getSourceRange(); |
| 446 | } |
| 447 | |
| 448 | // Allocate the parameter |
| 449 | ParmVarDecl *Param = 0; |
| 450 | if (T == OrigT) |
| 451 | Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 452 | D->getIdentifier(), T, D->getStorageClass(), |
| 453 | 0); |
| 454 | else |
| 455 | Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner, |
| 456 | D->getLocation(), D->getIdentifier(), |
| 457 | T, OrigT, D->getStorageClass(), 0); |
| 458 | |
| 459 | // Note: we don't try to instantiate function parameters until after |
| 460 | // we've instantiated the function's type. Therefore, we don't have |
| 461 | // to check for 'void' parameter types here. |
| 462 | return Param; |
| 463 | } |
| 464 | |
| 465 | Decl * |
| 466 | TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) { |
| 467 | // Since parameter types can decay either before or after |
| 468 | // instantiation, we simply treat OriginalParmVarDecls as |
| 469 | // ParmVarDecls the same way, and create one or the other depending |
| 470 | // on what happens after template instantiation. |
| 471 | return VisitParmVarDecl(D); |
| 472 | } |
| 473 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 474 | Decl *Sema::InstantiateDecl(Decl *D, DeclContext *Owner, |
| 475 | const TemplateArgument *TemplateArgs, |
| 476 | unsigned NumTemplateArgs) { |
| 477 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs, |
| 478 | NumTemplateArgs); |
| 479 | return Instantiator.Visit(D); |
| 480 | } |
| 481 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 482 | /// \brief Instantiates the type of the given function, including |
| 483 | /// instantiating all of the function parameters. |
| 484 | /// |
| 485 | /// \param D The function that we will be instantiated |
| 486 | /// |
| 487 | /// \param Params the instantiated parameter declarations |
| 488 | |
| 489 | /// \returns the instantiated function's type if successfull, a NULL |
| 490 | /// type if there was an error. |
| 491 | QualType |
| 492 | TemplateDeclInstantiator::InstantiateFunctionType(FunctionDecl *D, |
| 493 | llvm::SmallVectorImpl<ParmVarDecl *> &Params) { |
| 494 | bool InvalidDecl = false; |
| 495 | |
| 496 | // Instantiate the function parameters |
| 497 | TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, |
| 498 | TemplateArgs, NumTemplateArgs); |
| 499 | llvm::SmallVector<QualType, 16> ParamTys; |
| 500 | for (FunctionDecl::param_iterator P = D->param_begin(), |
| 501 | PEnd = D->param_end(); |
| 502 | P != PEnd; ++P) { |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 503 | if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) { |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 504 | if (PInst->getType()->isVoidType()) { |
| 505 | SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type); |
| 506 | PInst->setInvalidDecl(); |
| 507 | } |
| 508 | else if (SemaRef.RequireNonAbstractType(PInst->getLocation(), |
| 509 | PInst->getType(), |
| 510 | diag::err_abstract_type_in_decl, |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 511 | Sema::AbstractParamType)) |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 512 | PInst->setInvalidDecl(); |
| 513 | |
| 514 | Params.push_back(PInst); |
| 515 | ParamTys.push_back(PInst->getType()); |
| 516 | |
| 517 | if (PInst->isInvalidDecl()) |
| 518 | InvalidDecl = true; |
| 519 | } else |
| 520 | InvalidDecl = true; |
| 521 | } |
| 522 | |
| 523 | // FIXME: Deallocate dead declarations. |
| 524 | if (InvalidDecl) |
| 525 | return QualType(); |
| 526 | |
| 527 | const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType(); |
| 528 | assert(Proto && "Missing prototype?"); |
| 529 | QualType ResultType |
| 530 | = SemaRef.InstantiateType(Proto->getResultType(), |
| 531 | TemplateArgs, NumTemplateArgs, |
| 532 | D->getLocation(), D->getDeclName()); |
| 533 | if (ResultType.isNull()) |
| 534 | return QualType(); |
| 535 | |
| 536 | return SemaRef.BuildFunctionType(ResultType, &ParamTys[0], ParamTys.size(), |
| 537 | Proto->isVariadic(), Proto->getTypeQuals(), |
| 538 | D->getLocation(), D->getDeclName()); |
| 539 | } |
| 540 | |
| 541 | /// \brief Initializes common fields of an instantiated method |
| 542 | /// declaration (New) from the corresponding fields of its template |
| 543 | /// (Tmpl). |
| 544 | /// |
| 545 | /// \returns true if there was an error |
| 546 | bool |
| 547 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, |
| 548 | CXXMethodDecl *Tmpl) { |
| 549 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 550 | New->setAccess(Tmpl->getAccess()); |
| 551 | if (Tmpl->isVirtual()) { |
| 552 | New->setVirtual(); |
| 553 | Record->setAggregate(false); |
| 554 | Record->setPOD(false); |
| 555 | Record->setPolymorphic(true); |
| 556 | } |
| 557 | if (Tmpl->isDeleted()) |
| 558 | New->setDeleted(); |
| 559 | if (Tmpl->isPure()) { |
| 560 | New->setPure(); |
| 561 | Record->setAbstract(true); |
| 562 | } |
| 563 | |
| 564 | // FIXME: attributes |
| 565 | // FIXME: New needs a pointer to Tmpl |
| 566 | return false; |
| 567 | } |