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 | |
| 35 | // FIXME: Once we get closer to completion, replace these |
| 36 | // manually-written declarations with automatically-generated ones |
| 37 | // from 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 | |
| 110 | // Build the instantiataed declaration |
| 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 | |
| 119 | // FIXME: In theory, we could have a previous declaration for |
| 120 | // variables that are not static data members. |
| 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 | |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 248 | SemaRef.ActOnEnumBody(Enum->getLocation(), Sema::DeclPtrTy::make(Enum), |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 249 | &Enumerators[0], Enumerators.size()); |
| 250 | |
| 251 | return Enum; |
| 252 | } |
| 253 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 254 | Decl *TemplateDeclInstantiator::VisitEnumConstantDecl(EnumConstantDecl *D) { |
| 255 | assert(false && "EnumConstantDecls can only occur within EnumDecls."); |
| 256 | return 0; |
| 257 | } |
| 258 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 259 | Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { |
| 260 | CXXRecordDecl *PrevDecl = 0; |
| 261 | if (D->isInjectedClassName()) |
| 262 | PrevDecl = cast<CXXRecordDecl>(Owner); |
| 263 | |
| 264 | CXXRecordDecl *Record |
| 265 | = CXXRecordDecl::Create(SemaRef.Context, D->getTagKind(), Owner, |
| 266 | D->getLocation(), D->getIdentifier(), PrevDecl); |
| 267 | Record->setImplicit(D->isImplicit()); |
| 268 | Record->setAccess(D->getAccess()); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 269 | if (!D->isInjectedClassName()) |
| 270 | Record->setInstantiationOfMemberClass(D); |
| 271 | |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 272 | Owner->addDecl(SemaRef.Context, Record); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 273 | return Record; |
| 274 | } |
| 275 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 276 | Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) { |
| 277 | // Only handle actual methods; we'll deal with constructors, |
| 278 | // destructors, etc. separately. |
| 279 | if (D->getKind() != Decl::CXXMethod) |
| 280 | return 0; |
| 281 | |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 282 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 283 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 284 | llvm::SmallVector<ParmVarDecl *, 16> Params; |
| 285 | QualType T = InstantiateFunctionType(D, Params); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 286 | if (T.isNull()) |
| 287 | return 0; |
| 288 | |
| 289 | // Build the instantiated method declaration. |
| 290 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 291 | CXXMethodDecl *Method |
| 292 | = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(), |
| 293 | D->getDeclName(), T, D->isStatic(), |
| 294 | D->isInline()); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 295 | Method->setInstantiationOfMemberFunction(D); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 296 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 297 | // Attach the parameters |
| 298 | for (unsigned P = 0; P < Params.size(); ++P) |
| 299 | Params[P]->setOwningFunction(Method); |
| 300 | Method->setParams(SemaRef.Context, &Params[0], Params.size()); |
| 301 | |
| 302 | if (InitMethodInstantiation(Method, D)) |
| 303 | Method->setInvalidDecl(); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 304 | |
| 305 | NamedDecl *PrevDecl |
| 306 | = SemaRef.LookupQualifiedName(Owner, Method->getDeclName(), |
| 307 | Sema::LookupOrdinaryName, true); |
| 308 | // In C++, the previous declaration we find might be a tag type |
| 309 | // (class or enum). In this case, the new declaration will hide the |
| 310 | // tag type. Note that this does does not apply if we're declaring a |
| 311 | // typedef (C++ [dcl.typedef]p4). |
| 312 | if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 313 | PrevDecl = 0; |
| 314 | bool Redeclaration = false; |
| 315 | bool OverloadableAttrRequired = false; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 316 | SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration, |
| 317 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 318 | |
| 319 | if (!Method->isInvalidDecl() || !PrevDecl) |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 320 | Owner->addDecl(SemaRef.Context, Method); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 321 | return Method; |
| 322 | } |
| 323 | |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 324 | Decl *TemplateDeclInstantiator::VisitCXXConstructorDecl(CXXConstructorDecl *D) { |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 325 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 326 | |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 327 | llvm::SmallVector<ParmVarDecl *, 16> Params; |
| 328 | QualType T = InstantiateFunctionType(D, Params); |
| 329 | if (T.isNull()) |
| 330 | return 0; |
| 331 | |
| 332 | // Build the instantiated method declaration. |
| 333 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 334 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); |
| 335 | DeclarationName Name |
| 336 | = SemaRef.Context.DeclarationNames.getCXXConstructorName(ClassTy); |
| 337 | CXXConstructorDecl *Constructor |
| 338 | = CXXConstructorDecl::Create(SemaRef.Context, Record, D->getLocation(), |
| 339 | Name, T, D->isExplicit(), D->isInline(), |
| 340 | false); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 341 | Constructor->setInstantiationOfMemberFunction(D); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 342 | |
| 343 | // Attach the parameters |
| 344 | for (unsigned P = 0; P < Params.size(); ++P) |
| 345 | Params[P]->setOwningFunction(Constructor); |
| 346 | Constructor->setParams(SemaRef.Context, &Params[0], Params.size()); |
| 347 | |
| 348 | if (InitMethodInstantiation(Constructor, D)) |
| 349 | Constructor->setInvalidDecl(); |
| 350 | |
| 351 | NamedDecl *PrevDecl |
| 352 | = SemaRef.LookupQualifiedName(Owner, Name, Sema::LookupOrdinaryName, true); |
| 353 | |
| 354 | // In C++, the previous declaration we find might be a tag type |
| 355 | // (class or enum). In this case, the new declaration will hide the |
| 356 | // tag type. Note that this does does not apply if we're declaring a |
| 357 | // typedef (C++ [dcl.typedef]p4). |
| 358 | if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 359 | PrevDecl = 0; |
| 360 | bool Redeclaration = false; |
| 361 | bool OverloadableAttrRequired = false; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 362 | SemaRef.CheckFunctionDeclaration(Constructor, PrevDecl, Redeclaration, |
| 363 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 364 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 365 | Owner->addDecl(SemaRef.Context, Constructor); |
Douglas Gregor | 615c5d4 | 2009-03-24 16:43:20 +0000 | [diff] [blame] | 366 | return Constructor; |
| 367 | } |
| 368 | |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 369 | Decl *TemplateDeclInstantiator::VisitCXXDestructorDecl(CXXDestructorDecl *D) { |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 370 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 371 | |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 372 | llvm::SmallVector<ParmVarDecl *, 16> Params; |
| 373 | QualType T = InstantiateFunctionType(D, Params); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 374 | if (T.isNull()) |
| 375 | return 0; |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 376 | assert(Params.size() == 0 && "Destructor with parameters?"); |
| 377 | |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 378 | // Build the instantiated destructor declaration. |
| 379 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 380 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); |
| 381 | CXXDestructorDecl *Destructor |
| 382 | = CXXDestructorDecl::Create(SemaRef.Context, Record, |
| 383 | D->getLocation(), |
| 384 | SemaRef.Context.DeclarationNames.getCXXDestructorName(ClassTy), |
| 385 | T, D->isInline(), false); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 386 | Destructor->setInstantiationOfMemberFunction(D); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 387 | if (InitMethodInstantiation(Destructor, D)) |
| 388 | Destructor->setInvalidDecl(); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 389 | |
| 390 | bool Redeclaration = false; |
| 391 | bool OverloadableAttrRequired = false; |
| 392 | NamedDecl *PrevDecl = 0; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 393 | SemaRef.CheckFunctionDeclaration(Destructor, PrevDecl, Redeclaration, |
| 394 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 395 | Owner->addDecl(SemaRef.Context, Destructor); |
Douglas Gregor | 03b2b07 | 2009-03-24 00:15:49 +0000 | [diff] [blame] | 396 | return Destructor; |
| 397 | } |
| 398 | |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 399 | Decl *TemplateDeclInstantiator::VisitCXXConversionDecl(CXXConversionDecl *D) { |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 400 | Sema::LocalInstantiationScope Scope(SemaRef); |
| 401 | |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 402 | llvm::SmallVector<ParmVarDecl *, 16> Params; |
| 403 | QualType T = InstantiateFunctionType(D, Params); |
| 404 | if (T.isNull()) |
| 405 | return 0; |
| 406 | assert(Params.size() == 0 && "Destructor with parameters?"); |
| 407 | |
| 408 | // Build the instantiated conversion declaration. |
| 409 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 410 | QualType ClassTy = SemaRef.Context.getTypeDeclType(Record); |
| 411 | QualType ConvTy |
| 412 | = SemaRef.Context.getCanonicalType(T->getAsFunctionType()->getResultType()); |
| 413 | CXXConversionDecl *Conversion |
| 414 | = CXXConversionDecl::Create(SemaRef.Context, Record, |
| 415 | D->getLocation(), |
| 416 | SemaRef.Context.DeclarationNames.getCXXConversionFunctionName(ConvTy), |
| 417 | T, D->isInline(), D->isExplicit()); |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 418 | Conversion->setInstantiationOfMemberFunction(D); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 419 | if (InitMethodInstantiation(Conversion, D)) |
| 420 | Conversion->setInvalidDecl(); |
| 421 | |
| 422 | bool Redeclaration = false; |
| 423 | bool OverloadableAttrRequired = false; |
| 424 | NamedDecl *PrevDecl = 0; |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 425 | SemaRef.CheckFunctionDeclaration(Conversion, PrevDecl, Redeclaration, |
| 426 | /*FIXME:*/OverloadableAttrRequired); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 427 | Owner->addDecl(SemaRef.Context, Conversion); |
Douglas Gregor | bb969ed | 2009-03-25 00:34:44 +0000 | [diff] [blame] | 428 | return Conversion; |
| 429 | } |
| 430 | |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 431 | ParmVarDecl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 432 | QualType OrigT = SemaRef.InstantiateType(D->getOriginalType(), TemplateArgs, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 433 | D->getLocation(), D->getDeclName()); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 434 | if (OrigT.isNull()) |
| 435 | return 0; |
| 436 | |
| 437 | QualType T = SemaRef.adjustParameterType(OrigT); |
| 438 | |
| 439 | if (D->getDefaultArg()) { |
| 440 | // FIXME: Leave a marker for "uninstantiated" default |
| 441 | // arguments. They only get instantiated on demand at the call |
| 442 | // site. |
| 443 | unsigned DiagID = SemaRef.Diags.getCustomDiagID(Diagnostic::Warning, |
| 444 | "sorry, dropping default argument during template instantiation"); |
| 445 | SemaRef.Diag(D->getDefaultArg()->getSourceRange().getBegin(), DiagID) |
| 446 | << D->getDefaultArg()->getSourceRange(); |
| 447 | } |
| 448 | |
| 449 | // Allocate the parameter |
| 450 | ParmVarDecl *Param = 0; |
| 451 | if (T == OrigT) |
| 452 | Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 453 | D->getIdentifier(), T, D->getStorageClass(), |
| 454 | 0); |
| 455 | else |
| 456 | Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner, |
| 457 | D->getLocation(), D->getIdentifier(), |
| 458 | T, OrigT, D->getStorageClass(), 0); |
| 459 | |
| 460 | // Note: we don't try to instantiate function parameters until after |
| 461 | // we've instantiated the function's type. Therefore, we don't have |
| 462 | // to check for 'void' parameter types here. |
Douglas Gregor | 48dd19b | 2009-05-14 21:44:34 +0000 | [diff] [blame] | 463 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(D, Param); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame] | 464 | return Param; |
| 465 | } |
| 466 | |
| 467 | Decl * |
| 468 | TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) { |
| 469 | // Since parameter types can decay either before or after |
| 470 | // instantiation, we simply treat OriginalParmVarDecls as |
| 471 | // ParmVarDecls the same way, and create one or the other depending |
| 472 | // on what happens after template instantiation. |
| 473 | return VisitParmVarDecl(D); |
| 474 | } |
| 475 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 476 | Decl *Sema::InstantiateDecl(Decl *D, DeclContext *Owner, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 477 | const TemplateArgumentList &TemplateArgs) { |
| 478 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 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 |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 497 | TemplateDeclInstantiator ParamInstantiator(SemaRef, 0, TemplateArgs); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 498 | llvm::SmallVector<QualType, 16> ParamTys; |
| 499 | for (FunctionDecl::param_iterator P = D->param_begin(), |
| 500 | PEnd = D->param_end(); |
| 501 | P != PEnd; ++P) { |
Douglas Gregor | 6477b69 | 2009-03-25 15:04:13 +0000 | [diff] [blame] | 502 | if (ParmVarDecl *PInst = ParamInstantiator.VisitParmVarDecl(*P)) { |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 503 | if (PInst->getType()->isVoidType()) { |
| 504 | SemaRef.Diag(PInst->getLocation(), diag::err_param_with_void_type); |
| 505 | PInst->setInvalidDecl(); |
| 506 | } |
| 507 | else if (SemaRef.RequireNonAbstractType(PInst->getLocation(), |
| 508 | PInst->getType(), |
| 509 | diag::err_abstract_type_in_decl, |
Anders Carlsson | 8211eff | 2009-03-24 01:19:16 +0000 | [diff] [blame] | 510 | Sema::AbstractParamType)) |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 511 | PInst->setInvalidDecl(); |
| 512 | |
| 513 | Params.push_back(PInst); |
| 514 | ParamTys.push_back(PInst->getType()); |
| 515 | |
| 516 | if (PInst->isInvalidDecl()) |
| 517 | InvalidDecl = true; |
| 518 | } else |
| 519 | InvalidDecl = true; |
| 520 | } |
| 521 | |
| 522 | // FIXME: Deallocate dead declarations. |
| 523 | if (InvalidDecl) |
| 524 | return QualType(); |
| 525 | |
| 526 | const FunctionProtoType *Proto = D->getType()->getAsFunctionProtoType(); |
| 527 | assert(Proto && "Missing prototype?"); |
| 528 | QualType ResultType |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 529 | = SemaRef.InstantiateType(Proto->getResultType(), TemplateArgs, |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 530 | D->getLocation(), D->getDeclName()); |
| 531 | if (ResultType.isNull()) |
| 532 | return QualType(); |
| 533 | |
| 534 | return SemaRef.BuildFunctionType(ResultType, &ParamTys[0], ParamTys.size(), |
| 535 | Proto->isVariadic(), Proto->getTypeQuals(), |
| 536 | D->getLocation(), D->getDeclName()); |
| 537 | } |
| 538 | |
| 539 | /// \brief Initializes common fields of an instantiated method |
| 540 | /// declaration (New) from the corresponding fields of its template |
| 541 | /// (Tmpl). |
| 542 | /// |
| 543 | /// \returns true if there was an error |
| 544 | bool |
| 545 | TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New, |
| 546 | CXXMethodDecl *Tmpl) { |
| 547 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 548 | New->setAccess(Tmpl->getAccess()); |
Anders Carlsson | 77b7f1d | 2009-05-14 22:15:41 +0000 | [diff] [blame^] | 549 | if (Tmpl->isVirtualAsWritten()) { |
| 550 | New->setVirtualAsWritten(true); |
Douglas Gregor | 5545e16 | 2009-03-24 00:38:23 +0000 | [diff] [blame] | 551 | Record->setAggregate(false); |
| 552 | Record->setPOD(false); |
| 553 | Record->setPolymorphic(true); |
| 554 | } |
| 555 | if (Tmpl->isDeleted()) |
| 556 | New->setDeleted(); |
| 557 | if (Tmpl->isPure()) { |
| 558 | New->setPure(); |
| 559 | Record->setAbstract(true); |
| 560 | } |
| 561 | |
| 562 | // FIXME: attributes |
| 563 | // FIXME: New needs a pointer to Tmpl |
| 564 | return false; |
| 565 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 566 | |
| 567 | /// \brief Instantiate the definition of the given function from its |
| 568 | /// template. |
| 569 | /// |
| 570 | /// \param Function the already-instantiated declaration of a |
| 571 | /// function. |
| 572 | void Sema::InstantiateFunctionDefinition(FunctionDecl *Function) { |
Douglas Gregor | 1eee0e7 | 2009-05-14 21:06:31 +0000 | [diff] [blame] | 573 | // FIXME: make this work for function template specializations, too. |
| 574 | |
| 575 | // Find the function body that we'll be substituting. |
| 576 | const FunctionDecl *PatternDecl |
| 577 | = Function->getInstantiatedFromMemberFunction(); |
| 578 | Stmt *Pattern = 0; |
| 579 | if (PatternDecl) |
| 580 | Pattern = PatternDecl->getBody(Context, PatternDecl); |
| 581 | |
| 582 | if (!Pattern) |
| 583 | return; |
| 584 | |
| 585 | // FIXME: instantiate the pattern |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | /// \brief Instantiate the definition of the given variable from its |
| 589 | /// template. |
| 590 | /// |
| 591 | /// \param Var the already-instantiated declaration of a variable. |
| 592 | void Sema::InstantiateVariableDefinition(VarDecl *Var) { |
| 593 | // FIXME: Implement this! |
| 594 | } |