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. |
| 42 | Decl *VisitTypedefDecl(TypedefDecl *D); |
| 43 | Decl *VisitFieldDecl(FieldDecl *D); |
| 44 | Decl *VisitStaticAssertDecl(StaticAssertDecl *D); |
| 45 | Decl *VisitEnumDecl(EnumDecl *D); |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame^] | 46 | Decl *VisitCXXMethodDecl(CXXMethodDecl *D); |
| 47 | Decl *VisitParmVarDecl(ParmVarDecl *D); |
| 48 | Decl *VisitOriginalParmVarDecl(OriginalParmVarDecl *D); |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 49 | |
| 50 | // Base case. FIXME: Remove once we can instantiate everything. |
| 51 | Decl *VisitDecl(Decl *) { |
| 52 | return 0; |
| 53 | } |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | Decl *TemplateDeclInstantiator::VisitTypedefDecl(TypedefDecl *D) { |
| 58 | bool Invalid = false; |
| 59 | QualType T = D->getUnderlyingType(); |
| 60 | if (T->isDependentType()) { |
| 61 | T = SemaRef.InstantiateType(T, TemplateArgs, NumTemplateArgs, |
| 62 | D->getLocation(), |
| 63 | D->getDeclName()); |
| 64 | if (T.isNull()) { |
| 65 | Invalid = true; |
| 66 | T = SemaRef.Context.IntTy; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Create the new typedef |
| 71 | TypedefDecl *Typedef |
| 72 | = TypedefDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 73 | D->getIdentifier(), T); |
| 74 | if (Invalid) |
| 75 | Typedef->setInvalidDecl(); |
| 76 | |
| 77 | Owner->addDecl(Typedef); |
| 78 | return Typedef; |
| 79 | } |
| 80 | |
| 81 | Decl *TemplateDeclInstantiator::VisitFieldDecl(FieldDecl *D) { |
| 82 | bool Invalid = false; |
| 83 | QualType T = D->getType(); |
| 84 | if (T->isDependentType()) { |
| 85 | T = SemaRef.InstantiateType(T, TemplateArgs, NumTemplateArgs, |
| 86 | D->getLocation(), |
| 87 | D->getDeclName()); |
| 88 | if (!T.isNull() && T->isFunctionType()) { |
| 89 | // C++ [temp.arg.type]p3: |
| 90 | // If a declaration acquires a function type through a type |
| 91 | // dependent on a template-parameter and this causes a |
| 92 | // declaration that does not use the syntactic form of a |
| 93 | // function declarator to have function type, the program is |
| 94 | // ill-formed. |
| 95 | SemaRef.Diag(D->getLocation(), diag::err_field_instantiates_to_function) |
| 96 | << T; |
| 97 | T = QualType(); |
| 98 | Invalid = true; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | Expr *BitWidth = D->getBitWidth(); |
| 103 | if (Invalid) |
| 104 | BitWidth = 0; |
| 105 | else if (BitWidth) { |
| 106 | OwningExprResult InstantiatedBitWidth |
| 107 | = SemaRef.InstantiateExpr(BitWidth, TemplateArgs, NumTemplateArgs); |
| 108 | if (InstantiatedBitWidth.isInvalid()) { |
| 109 | Invalid = true; |
| 110 | BitWidth = 0; |
| 111 | } else |
| 112 | BitWidth = (Expr *)InstantiatedBitWidth.release(); |
| 113 | } |
| 114 | |
| 115 | FieldDecl *Field = SemaRef.CheckFieldDecl(D->getDeclName(), T, |
| 116 | cast<RecordDecl>(Owner), |
| 117 | D->getLocation(), |
| 118 | D->isMutable(), |
| 119 | BitWidth, |
| 120 | D->getAccess(), |
| 121 | 0); |
| 122 | if (Field) { |
| 123 | if (Invalid) |
| 124 | Field->setInvalidDecl(); |
| 125 | |
| 126 | Owner->addDecl(Field); |
| 127 | } |
| 128 | |
| 129 | return Field; |
| 130 | } |
| 131 | |
| 132 | Decl *TemplateDeclInstantiator::VisitStaticAssertDecl(StaticAssertDecl *D) { |
| 133 | Expr *AssertExpr = D->getAssertExpr(); |
| 134 | |
| 135 | OwningExprResult InstantiatedAssertExpr |
| 136 | = SemaRef.InstantiateExpr(AssertExpr, TemplateArgs, NumTemplateArgs); |
| 137 | if (InstantiatedAssertExpr.isInvalid()) |
| 138 | return 0; |
| 139 | |
| 140 | OwningExprResult Message = SemaRef.Clone(D->getMessage()); |
| 141 | Decl *StaticAssert |
| 142 | = (Decl *)SemaRef.ActOnStaticAssertDeclaration(D->getLocation(), |
| 143 | move(InstantiatedAssertExpr), |
| 144 | move(Message)); |
| 145 | return StaticAssert; |
| 146 | } |
| 147 | |
| 148 | Decl *TemplateDeclInstantiator::VisitEnumDecl(EnumDecl *D) { |
| 149 | EnumDecl *Enum = EnumDecl::Create(SemaRef.Context, Owner, |
| 150 | D->getLocation(), D->getIdentifier(), |
| 151 | /*PrevDecl=*/0); |
| 152 | Owner->addDecl(Enum); |
| 153 | Enum->startDefinition(); |
| 154 | |
| 155 | llvm::SmallVector<Sema::DeclTy *, 16> Enumerators; |
| 156 | |
| 157 | EnumConstantDecl *LastEnumConst = 0; |
| 158 | for (EnumDecl::enumerator_iterator EC = D->enumerator_begin(), |
| 159 | ECEnd = D->enumerator_end(); |
| 160 | EC != ECEnd; ++EC) { |
| 161 | // The specified value for the enumerator. |
| 162 | OwningExprResult Value = SemaRef.Owned((Expr *)0); |
| 163 | if (Expr *UninstValue = EC->getInitExpr()) |
| 164 | Value = SemaRef.InstantiateExpr(UninstValue, |
| 165 | TemplateArgs, NumTemplateArgs); |
| 166 | |
| 167 | // Drop the initial value and continue. |
| 168 | bool isInvalid = false; |
| 169 | if (Value.isInvalid()) { |
| 170 | Value = SemaRef.Owned((Expr *)0); |
| 171 | isInvalid = true; |
| 172 | } |
| 173 | |
| 174 | EnumConstantDecl *EnumConst |
| 175 | = SemaRef.CheckEnumConstant(Enum, LastEnumConst, |
| 176 | EC->getLocation(), EC->getIdentifier(), |
| 177 | move(Value)); |
| 178 | |
| 179 | if (isInvalid) { |
| 180 | if (EnumConst) |
| 181 | EnumConst->setInvalidDecl(); |
| 182 | Enum->setInvalidDecl(); |
| 183 | } |
| 184 | |
| 185 | if (EnumConst) { |
| 186 | Enum->addDecl(EnumConst); |
| 187 | Enumerators.push_back(EnumConst); |
| 188 | LastEnumConst = EnumConst; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | SemaRef.ActOnEnumBody(Enum->getLocation(), Enum, |
| 193 | &Enumerators[0], Enumerators.size()); |
| 194 | |
| 195 | return Enum; |
| 196 | } |
| 197 | |
Douglas Gregor | 2dc0e64 | 2009-03-23 23:06:20 +0000 | [diff] [blame^] | 198 | Decl *TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D) { |
| 199 | // Only handle actual methods; we'll deal with constructors, |
| 200 | // destructors, etc. separately. |
| 201 | if (D->getKind() != Decl::CXXMethod) |
| 202 | return 0; |
| 203 | |
| 204 | QualType T = SemaRef.InstantiateType(D->getType(), TemplateArgs, |
| 205 | NumTemplateArgs, D->getLocation(), |
| 206 | D->getDeclName()); |
| 207 | if (T.isNull()) |
| 208 | return 0; |
| 209 | |
| 210 | // Build the instantiated method declaration. |
| 211 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner); |
| 212 | CXXMethodDecl *Method |
| 213 | = CXXMethodDecl::Create(SemaRef.Context, Record, D->getLocation(), |
| 214 | D->getDeclName(), T, D->isStatic(), |
| 215 | D->isInline()); |
| 216 | Method->setAccess(D->getAccess()); |
| 217 | // FIXME: Duplicates some logic in ActOnFunctionDeclarator. |
| 218 | if (D->isVirtual()) { |
| 219 | Method->setVirtual(); |
| 220 | Record->setAggregate(false); |
| 221 | Record->setPOD(false); |
| 222 | Record->setPolymorphic(true); |
| 223 | } |
| 224 | if (D->isDeleted()) |
| 225 | Method->setDeleted(); |
| 226 | if (D->isPure()) { |
| 227 | Method->setPure(); |
| 228 | Record->setAbstract(true); |
| 229 | } |
| 230 | // FIXME: attributes |
| 231 | // FIXME: Method needs a pointer referencing where it came from. |
| 232 | |
| 233 | // Instantiate the function parameters |
| 234 | { |
| 235 | TemplateDeclInstantiator ParamInstantiator(SemaRef, Method, |
| 236 | TemplateArgs, NumTemplateArgs); |
| 237 | llvm::SmallVector<ParmVarDecl *, 16> Params; |
| 238 | for (FunctionDecl::param_iterator P = Method->param_begin(), |
| 239 | PEnd = Method->param_end(); |
| 240 | P != PEnd; ++P) { |
| 241 | if (ParmVarDecl *PInst = (ParmVarDecl *)ParamInstantiator.Visit(*P)) |
| 242 | Params.push_back(PInst); |
| 243 | else |
| 244 | Method->setInvalidDecl(); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | NamedDecl *PrevDecl |
| 249 | = SemaRef.LookupQualifiedName(Owner, Method->getDeclName(), |
| 250 | Sema::LookupOrdinaryName, true); |
| 251 | // In C++, the previous declaration we find might be a tag type |
| 252 | // (class or enum). In this case, the new declaration will hide the |
| 253 | // tag type. Note that this does does not apply if we're declaring a |
| 254 | // typedef (C++ [dcl.typedef]p4). |
| 255 | if (PrevDecl && PrevDecl->getIdentifierNamespace() == Decl::IDNS_Tag) |
| 256 | PrevDecl = 0; |
| 257 | bool Redeclaration = false; |
| 258 | bool OverloadableAttrRequired = false; |
| 259 | if (SemaRef.CheckFunctionDeclaration(Method, PrevDecl, Redeclaration, |
| 260 | /*FIXME:*/OverloadableAttrRequired)) |
| 261 | Method->setInvalidDecl(); |
| 262 | |
| 263 | if (!Method->isInvalidDecl() || !PrevDecl) |
| 264 | Owner->addDecl(Method); |
| 265 | return Method; |
| 266 | } |
| 267 | |
| 268 | Decl *TemplateDeclInstantiator::VisitParmVarDecl(ParmVarDecl *D) { |
| 269 | QualType OrigT = SemaRef.InstantiateType(D->getOriginalType(), TemplateArgs, |
| 270 | NumTemplateArgs, D->getLocation(), |
| 271 | D->getDeclName()); |
| 272 | if (OrigT.isNull()) |
| 273 | return 0; |
| 274 | |
| 275 | QualType T = SemaRef.adjustParameterType(OrigT); |
| 276 | |
| 277 | if (D->getDefaultArg()) { |
| 278 | // FIXME: Leave a marker for "uninstantiated" default |
| 279 | // arguments. They only get instantiated on demand at the call |
| 280 | // site. |
| 281 | unsigned DiagID = SemaRef.Diags.getCustomDiagID(Diagnostic::Warning, |
| 282 | "sorry, dropping default argument during template instantiation"); |
| 283 | SemaRef.Diag(D->getDefaultArg()->getSourceRange().getBegin(), DiagID) |
| 284 | << D->getDefaultArg()->getSourceRange(); |
| 285 | } |
| 286 | |
| 287 | // Allocate the parameter |
| 288 | ParmVarDecl *Param = 0; |
| 289 | if (T == OrigT) |
| 290 | Param = ParmVarDecl::Create(SemaRef.Context, Owner, D->getLocation(), |
| 291 | D->getIdentifier(), T, D->getStorageClass(), |
| 292 | 0); |
| 293 | else |
| 294 | Param = OriginalParmVarDecl::Create(SemaRef.Context, Owner, |
| 295 | D->getLocation(), D->getIdentifier(), |
| 296 | T, OrigT, D->getStorageClass(), 0); |
| 297 | |
| 298 | // Note: we don't try to instantiate function parameters until after |
| 299 | // we've instantiated the function's type. Therefore, we don't have |
| 300 | // to check for 'void' parameter types here. |
| 301 | return Param; |
| 302 | } |
| 303 | |
| 304 | Decl * |
| 305 | TemplateDeclInstantiator::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) { |
| 306 | // Since parameter types can decay either before or after |
| 307 | // instantiation, we simply treat OriginalParmVarDecls as |
| 308 | // ParmVarDecls the same way, and create one or the other depending |
| 309 | // on what happens after template instantiation. |
| 310 | return VisitParmVarDecl(D); |
| 311 | } |
| 312 | |
Douglas Gregor | 8dbc269 | 2009-03-17 21:15:40 +0000 | [diff] [blame] | 313 | Decl *Sema::InstantiateDecl(Decl *D, DeclContext *Owner, |
| 314 | const TemplateArgument *TemplateArgs, |
| 315 | unsigned NumTemplateArgs) { |
| 316 | TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs, |
| 317 | NumTemplateArgs); |
| 318 | return Instantiator.Visit(D); |
| 319 | } |
| 320 | |