Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 1 | //===------- SemaTemplateInstantiate.cpp - C++ Template 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. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===/ |
| 12 | |
| 13 | #include "Sema.h" |
| 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/Expr.h" |
| 16 | #include "clang/AST/ExprCXX.h" |
| 17 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 18 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 19 | #include "clang/Parse/DeclSpec.h" |
| 20 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang; |
| 24 | |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===/ |
| 26 | // Template Instantiation Support |
| 27 | //===----------------------------------------------------------------------===/ |
| 28 | |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 29 | Sema::InstantiatingTemplate:: |
| 30 | InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, |
| 31 | ClassTemplateSpecializationDecl *Entity, |
| 32 | SourceRange InstantiationRange) |
| 33 | : SemaRef(SemaRef) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 34 | |
| 35 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 36 | InstantiationRange); |
| 37 | if (!Invalid) { |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 38 | ActiveTemplateInstantiation Inst; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 39 | Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation; |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 40 | Inst.PointOfInstantiation = PointOfInstantiation; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 41 | Inst.Entity = reinterpret_cast<uintptr_t>(Entity); |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 42 | Inst.TemplateArgs = 0; |
| 43 | Inst.NumTemplateArgs = 0; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 44 | Inst.InstantiationRange = InstantiationRange; |
| 45 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 46 | Invalid = false; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
| 51 | SourceLocation PointOfInstantiation, |
| 52 | TemplateDecl *Template, |
| 53 | const TemplateArgument *TemplateArgs, |
| 54 | unsigned NumTemplateArgs, |
| 55 | SourceRange InstantiationRange) |
| 56 | : SemaRef(SemaRef) { |
| 57 | |
| 58 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 59 | InstantiationRange); |
| 60 | if (!Invalid) { |
| 61 | ActiveTemplateInstantiation Inst; |
| 62 | Inst.Kind |
| 63 | = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation; |
| 64 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 65 | Inst.Entity = reinterpret_cast<uintptr_t>(Template); |
| 66 | Inst.TemplateArgs = TemplateArgs; |
| 67 | Inst.NumTemplateArgs = NumTemplateArgs; |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 68 | Inst.InstantiationRange = InstantiationRange; |
| 69 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 70 | Invalid = false; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | Sema::InstantiatingTemplate::~InstantiatingTemplate() { |
| 75 | if (!Invalid) |
| 76 | SemaRef.ActiveTemplateInstantiations.pop_back(); |
| 77 | } |
| 78 | |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 79 | bool Sema::InstantiatingTemplate::CheckInstantiationDepth( |
| 80 | SourceLocation PointOfInstantiation, |
| 81 | SourceRange InstantiationRange) { |
| 82 | if (SemaRef.ActiveTemplateInstantiations.size() |
| 83 | <= SemaRef.getLangOptions().InstantiationDepth) |
| 84 | return false; |
| 85 | |
| 86 | SemaRef.Diag(PointOfInstantiation, |
| 87 | diag::err_template_recursion_depth_exceeded) |
| 88 | << SemaRef.getLangOptions().InstantiationDepth |
| 89 | << InstantiationRange; |
| 90 | SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth) |
| 91 | << SemaRef.getLangOptions().InstantiationDepth; |
| 92 | return true; |
| 93 | } |
| 94 | |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 95 | /// \brief Post-diagnostic hook for printing the instantiation stack. |
| 96 | void Sema::PrintInstantiationStackHook(unsigned, void *Cookie) { |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 97 | Sema &SemaRef = *static_cast<Sema*>(Cookie); |
| 98 | SemaRef.PrintInstantiationStack(); |
| 99 | SemaRef.LastTemplateInstantiationErrorContext |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 100 | = SemaRef.ActiveTemplateInstantiations.back(); |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /// \brief Prints the current instantiation stack through a series of |
| 104 | /// notes. |
| 105 | void Sema::PrintInstantiationStack() { |
| 106 | for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator |
| 107 | Active = ActiveTemplateInstantiations.rbegin(), |
| 108 | ActiveEnd = ActiveTemplateInstantiations.rend(); |
| 109 | Active != ActiveEnd; |
| 110 | ++Active) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 111 | switch (Active->Kind) { |
| 112 | case ActiveTemplateInstantiation::TemplateInstantiation: { |
| 113 | ClassTemplateSpecializationDecl *Spec |
| 114 | = cast<ClassTemplateSpecializationDecl>((Decl*)Active->Entity); |
| 115 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 116 | diag::note_template_class_instantiation_here) |
| 117 | << Context.getTypeDeclType(Spec) |
| 118 | << Active->InstantiationRange; |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: { |
| 123 | TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity); |
| 124 | std::string TemplateArgsStr |
| 125 | = ClassTemplateSpecializationType::PrintTemplateArgumentList( |
| 126 | Active->TemplateArgs, |
| 127 | Active->NumTemplateArgs); |
| 128 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 129 | diag::note_default_arg_instantiation_here) |
| 130 | << (Template->getNameAsString() + TemplateArgsStr) |
| 131 | << Active->InstantiationRange; |
| 132 | break; |
| 133 | } |
| 134 | } |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 138 | //===----------------------------------------------------------------------===/ |
| 139 | // Template Instantiation for Types |
| 140 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 141 | namespace { |
| 142 | class VISIBILITY_HIDDEN TemplateTypeInstantiator { |
| 143 | Sema &SemaRef; |
| 144 | const TemplateArgument *TemplateArgs; |
| 145 | unsigned NumTemplateArgs; |
| 146 | SourceLocation Loc; |
| 147 | DeclarationName Entity; |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 148 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 149 | public: |
| 150 | TemplateTypeInstantiator(Sema &SemaRef, |
| 151 | const TemplateArgument *TemplateArgs, |
| 152 | unsigned NumTemplateArgs, |
| 153 | SourceLocation Loc, |
| 154 | DeclarationName Entity) |
| 155 | : SemaRef(SemaRef), TemplateArgs(TemplateArgs), |
| 156 | NumTemplateArgs(NumTemplateArgs), Loc(Loc), Entity(Entity) { } |
| 157 | |
| 158 | QualType operator()(QualType T) const { return Instantiate(T); } |
| 159 | |
| 160 | QualType Instantiate(QualType T) const; |
| 161 | |
| 162 | // Declare instantiate functions for each type. |
| 163 | #define TYPE(Class, Base) \ |
| 164 | QualType Instantiate##Class##Type(const Class##Type *T, \ |
| 165 | unsigned Quals) const; |
| 166 | #define ABSTRACT_TYPE(Class, Base) |
| 167 | #include "clang/AST/TypeNodes.def" |
| 168 | }; |
| 169 | } |
| 170 | |
| 171 | QualType |
| 172 | TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T, |
| 173 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 174 | // FIXME: Implement this |
| 175 | assert(false && "Cannot instantiate ExtQualType yet"); |
| 176 | return QualType(); |
| 177 | } |
| 178 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 179 | QualType |
| 180 | TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T, |
| 181 | unsigned Quals) const { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 182 | assert(false && "Builtin types are not dependent and cannot be instantiated"); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 183 | return QualType(T, Quals); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 186 | QualType |
| 187 | TemplateTypeInstantiator:: |
| 188 | InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 189 | // FIXME: Implement this |
| 190 | assert(false && "Cannot instantiate FixedWidthIntType yet"); |
| 191 | return QualType(); |
| 192 | } |
| 193 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 194 | QualType |
| 195 | TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T, |
| 196 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 197 | // FIXME: Implement this |
| 198 | assert(false && "Cannot instantiate ComplexType yet"); |
| 199 | return QualType(); |
| 200 | } |
| 201 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 202 | QualType |
| 203 | TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T, |
| 204 | unsigned Quals) const { |
| 205 | QualType PointeeType = Instantiate(T->getPointeeType()); |
| 206 | if (PointeeType.isNull()) |
| 207 | return QualType(); |
| 208 | |
| 209 | return SemaRef.BuildPointerType(PointeeType, Quals, Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 212 | QualType |
| 213 | TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T, |
| 214 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 215 | // FIXME: Implement this |
| 216 | assert(false && "Cannot instantiate BlockPointerType yet"); |
| 217 | return QualType(); |
| 218 | } |
| 219 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 220 | QualType |
| 221 | TemplateTypeInstantiator::InstantiateReferenceType(const ReferenceType *T, |
| 222 | unsigned Quals) const { |
| 223 | QualType ReferentType = Instantiate(T->getPointeeType()); |
| 224 | if (ReferentType.isNull()) |
| 225 | return QualType(); |
| 226 | |
| 227 | return SemaRef.BuildReferenceType(ReferentType, Quals, Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 230 | QualType |
| 231 | TemplateTypeInstantiator:: |
| 232 | InstantiateMemberPointerType(const MemberPointerType *T, |
| 233 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 234 | // FIXME: Implement this |
| 235 | assert(false && "Cannot instantiate MemberPointerType yet"); |
| 236 | return QualType(); |
| 237 | } |
| 238 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 239 | QualType |
| 240 | TemplateTypeInstantiator:: |
| 241 | InstantiateConstantArrayType(const ConstantArrayType *T, |
| 242 | unsigned Quals) const { |
| 243 | QualType ElementType = Instantiate(T->getElementType()); |
| 244 | if (ElementType.isNull()) |
| 245 | return ElementType; |
| 246 | |
| 247 | // Build a temporary integer literal to specify the size for |
| 248 | // BuildArrayType. Since we have already checked the size as part of |
| 249 | // creating the dependent array type in the first place, we know |
| 250 | // there aren't any errors. |
Douglas Gregor | 8d21721 | 2009-03-09 20:07:22 +0000 | [diff] [blame] | 251 | // FIXME: Is IntTy big enough? Maybe not, but LongLongTy causes |
| 252 | // problems that I have yet to investigate. |
| 253 | IntegerLiteral ArraySize(T->getSize(), SemaRef.Context.IntTy, Loc); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 254 | return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(), |
| 255 | &ArraySize, T->getIndexTypeQualifier(), |
| 256 | Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 259 | QualType |
| 260 | TemplateTypeInstantiator:: |
| 261 | InstantiateIncompleteArrayType(const IncompleteArrayType *T, |
| 262 | unsigned Quals) const { |
| 263 | QualType ElementType = Instantiate(T->getElementType()); |
| 264 | if (ElementType.isNull()) |
| 265 | return ElementType; |
| 266 | |
| 267 | return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(), |
| 268 | 0, T->getIndexTypeQualifier(), |
| 269 | Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 272 | QualType |
| 273 | TemplateTypeInstantiator:: |
| 274 | InstantiateVariableArrayType(const VariableArrayType *T, |
| 275 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 276 | // FIXME: Implement this |
| 277 | assert(false && "Cannot instantiate VariableArrayType yet"); |
| 278 | return QualType(); |
| 279 | } |
| 280 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 281 | QualType |
| 282 | TemplateTypeInstantiator:: |
| 283 | InstantiateDependentSizedArrayType(const DependentSizedArrayType *T, |
| 284 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 285 | // FIXME: Implement this |
| 286 | assert(false && "Cannot instantiate DependentSizedArrayType yet"); |
| 287 | return QualType(); |
| 288 | } |
| 289 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 290 | QualType |
| 291 | TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T, |
| 292 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 293 | // FIXME: Implement this |
| 294 | assert(false && "Cannot instantiate VectorType yet"); |
| 295 | return QualType(); |
| 296 | } |
| 297 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 298 | QualType |
| 299 | TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T, |
| 300 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 301 | // FIXME: Implement this |
| 302 | assert(false && "Cannot instantiate ExtVectorType yet"); |
| 303 | return QualType(); |
| 304 | } |
| 305 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 306 | QualType |
| 307 | TemplateTypeInstantiator:: |
| 308 | InstantiateFunctionProtoType(const FunctionProtoType *T, |
| 309 | unsigned Quals) const { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 310 | QualType ResultType = Instantiate(T->getResultType()); |
| 311 | if (ResultType.isNull()) |
| 312 | return ResultType; |
| 313 | |
| 314 | llvm::SmallVector<QualType, 16> ParamTypes; |
| 315 | for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(), |
| 316 | ParamEnd = T->arg_type_end(); |
| 317 | Param != ParamEnd; ++Param) { |
| 318 | QualType P = Instantiate(*Param); |
| 319 | if (P.isNull()) |
| 320 | return P; |
| 321 | |
| 322 | ParamTypes.push_back(P); |
| 323 | } |
| 324 | |
| 325 | return SemaRef.BuildFunctionType(ResultType, &ParamTypes[0], |
| 326 | ParamTypes.size(), |
| 327 | T->isVariadic(), T->getTypeQuals(), |
| 328 | Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 331 | QualType |
| 332 | TemplateTypeInstantiator:: |
| 333 | InstantiateFunctionNoProtoType(const FunctionNoProtoType *T, |
| 334 | unsigned Quals) const { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 335 | assert(false && "Functions without prototypes cannot be dependent."); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 336 | return QualType(); |
| 337 | } |
| 338 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 339 | QualType |
| 340 | TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T, |
| 341 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 342 | // FIXME: Implement this |
| 343 | assert(false && "Cannot instantiate TypedefType yet"); |
| 344 | return QualType(); |
| 345 | } |
| 346 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 347 | QualType |
| 348 | TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T, |
| 349 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 350 | // FIXME: Implement this |
| 351 | assert(false && "Cannot instantiate TypeOfExprType yet"); |
| 352 | return QualType(); |
| 353 | } |
| 354 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 355 | QualType |
| 356 | TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T, |
| 357 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 358 | // FIXME: Implement this |
| 359 | assert(false && "Cannot instantiate TypeOfType yet"); |
| 360 | return QualType(); |
| 361 | } |
| 362 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 363 | QualType |
| 364 | TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T, |
| 365 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 366 | // FIXME: Implement this |
| 367 | assert(false && "Cannot instantiate RecordType yet"); |
| 368 | return QualType(); |
| 369 | } |
| 370 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 371 | QualType |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 372 | TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T, |
| 373 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 374 | // FIXME: Implement this |
| 375 | assert(false && "Cannot instantiate EnumType yet"); |
| 376 | return QualType(); |
| 377 | } |
| 378 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 379 | QualType |
| 380 | TemplateTypeInstantiator:: |
| 381 | InstantiateTemplateTypeParmType(const TemplateTypeParmType *T, |
| 382 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 383 | if (T->getDepth() == 0) { |
| 384 | // Replace the template type parameter with its corresponding |
| 385 | // template argument. |
| 386 | assert(T->getIndex() < NumTemplateArgs && "Wrong # of template args"); |
| 387 | assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type && |
| 388 | "Template argument kind mismatch"); |
| 389 | QualType Result = TemplateArgs[T->getIndex()].getAsType(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 390 | if (Result.isNull() || !Quals) |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 391 | return Result; |
| 392 | |
| 393 | // C++ [dcl.ref]p1: |
| 394 | // [...] Cv-qualified references are ill-formed except when |
| 395 | // the cv-qualifiers are introduced through the use of a |
| 396 | // typedef (7.1.3) or of a template type argument (14.3), in |
| 397 | // which case the cv-qualifiers are ignored. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 398 | if (Quals && Result->isReferenceType()) |
| 399 | Quals = 0; |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 400 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 401 | return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers()); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | // The template type parameter comes from an inner template (e.g., |
| 405 | // the template parameter list of a member template inside the |
| 406 | // template we are instantiating). Create a new template type |
| 407 | // parameter with the template "level" reduced by one. |
| 408 | return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1, |
| 409 | T->getIndex(), |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 410 | T->getName()) |
| 411 | .getQualifiedType(Quals); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 414 | QualType |
| 415 | TemplateTypeInstantiator:: |
| 416 | InstantiateClassTemplateSpecializationType( |
| 417 | const ClassTemplateSpecializationType *T, |
| 418 | unsigned Quals) const { |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 419 | llvm::SmallVector<TemplateArgument, 16> InstantiatedTemplateArgs; |
| 420 | InstantiatedTemplateArgs.reserve(T->getNumArgs()); |
| 421 | for (ClassTemplateSpecializationType::iterator Arg = T->begin(), |
| 422 | ArgEnd = T->end(); |
| 423 | Arg != ArgEnd; ++Arg) { |
| 424 | switch (Arg->getKind()) { |
| 425 | case TemplateArgument::Type: { |
| 426 | QualType T = SemaRef.InstantiateType(Arg->getAsType(), |
| 427 | TemplateArgs, NumTemplateArgs, |
| 428 | Arg->getLocation(), |
| 429 | DeclarationName()); |
| 430 | if (T.isNull()) |
| 431 | return QualType(); |
| 432 | |
| 433 | InstantiatedTemplateArgs.push_back( |
| 434 | TemplateArgument(Arg->getLocation(), T)); |
| 435 | break; |
| 436 | } |
| 437 | |
| 438 | case TemplateArgument::Declaration: |
| 439 | case TemplateArgument::Integral: |
| 440 | InstantiatedTemplateArgs.push_back(*Arg); |
| 441 | break; |
| 442 | |
| 443 | case TemplateArgument::Expression: |
| 444 | assert(false && "Cannot instantiate expressions yet"); |
| 445 | break; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | // FIXME: We're missing the locations of the template name, '<', and |
| 450 | // '>'. |
| 451 | return SemaRef.CheckClassTemplateId(cast<ClassTemplateDecl>(T->getTemplate()), |
| 452 | Loc, |
| 453 | SourceLocation(), |
| 454 | &InstantiatedTemplateArgs[0], |
| 455 | InstantiatedTemplateArgs.size(), |
| 456 | SourceLocation()); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 459 | QualType |
| 460 | TemplateTypeInstantiator:: |
| 461 | InstantiateObjCInterfaceType(const ObjCInterfaceType *T, |
| 462 | unsigned Quals) const { |
| 463 | assert(false && "Objective-C types cannot be dependent"); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 464 | return QualType(); |
| 465 | } |
| 466 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 467 | QualType |
| 468 | TemplateTypeInstantiator:: |
| 469 | InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T, |
| 470 | unsigned Quals) const { |
| 471 | assert(false && "Objective-C types cannot be dependent"); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 472 | return QualType(); |
| 473 | } |
| 474 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 475 | QualType |
| 476 | TemplateTypeInstantiator:: |
| 477 | InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T, |
| 478 | unsigned Quals) const { |
| 479 | assert(false && "Objective-C types cannot be dependent"); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 480 | return QualType(); |
| 481 | } |
| 482 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 483 | QualType |
| 484 | TemplateTypeInstantiator:: |
| 485 | InstantiateObjCQualifiedClassType(const ObjCQualifiedClassType *T, |
| 486 | unsigned Quals) const { |
| 487 | assert(false && "Objective-C types cannot be dependent"); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 488 | return QualType(); |
| 489 | } |
| 490 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 491 | /// \brief The actual implementation of Sema::InstantiateType(). |
| 492 | QualType TemplateTypeInstantiator::Instantiate(QualType T) const { |
| 493 | // If T is not a dependent type, there is nothing to do. |
| 494 | if (!T->isDependentType()) |
| 495 | return T; |
| 496 | |
| 497 | switch (T->getTypeClass()) { |
| 498 | #define TYPE(Class, Base) \ |
| 499 | case Type::Class: \ |
| 500 | return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \ |
| 501 | T.getCVRQualifiers()); |
| 502 | #define ABSTRACT_TYPE(Class, Base) |
| 503 | #include "clang/AST/TypeNodes.def" |
| 504 | } |
| 505 | |
| 506 | assert(false && "Not all types have been decoded for instantiation"); |
| 507 | return QualType(); |
| 508 | } |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 509 | |
| 510 | /// \brief Instantiate the type T with a given set of template arguments. |
| 511 | /// |
| 512 | /// This routine substitutes the given template arguments into the |
| 513 | /// type T and produces the instantiated type. |
| 514 | /// |
| 515 | /// \param T the type into which the template arguments will be |
| 516 | /// substituted. If this type is not dependent, it will be returned |
| 517 | /// immediately. |
| 518 | /// |
| 519 | /// \param TemplateArgs the template arguments that will be |
| 520 | /// substituted for the top-level template parameters within T. |
| 521 | /// |
| 522 | /// \param NumTemplateArgs the number of template arguments provided |
| 523 | /// by TemplateArgs. |
| 524 | /// |
| 525 | /// \param Loc the location in the source code where this substitution |
| 526 | /// is being performed. It will typically be the location of the |
| 527 | /// declarator (if we're instantiating the type of some declaration) |
| 528 | /// or the location of the type in the source code (if, e.g., we're |
| 529 | /// instantiating the type of a cast expression). |
| 530 | /// |
| 531 | /// \param Entity the name of the entity associated with a declaration |
| 532 | /// being instantiated (if any). May be empty to indicate that there |
| 533 | /// is no such entity (if, e.g., this is a type that occurs as part of |
| 534 | /// a cast expression) or that the entity has no name (e.g., an |
| 535 | /// unnamed function parameter). |
| 536 | /// |
| 537 | /// \returns If the instantiation succeeds, the instantiated |
| 538 | /// type. Otherwise, produces diagnostics and returns a NULL type. |
| 539 | QualType Sema::InstantiateType(QualType T, |
| 540 | const TemplateArgument *TemplateArgs, |
| 541 | unsigned NumTemplateArgs, |
| 542 | SourceLocation Loc, DeclarationName Entity) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 543 | assert(!ActiveTemplateInstantiations.empty() && |
| 544 | "Cannot perform an instantiation without some context on the " |
| 545 | "instantiation stack"); |
| 546 | |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 547 | // If T is not a dependent type, there is nothing to do. |
| 548 | if (!T->isDependentType()) |
| 549 | return T; |
| 550 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 551 | TemplateTypeInstantiator Instantiator(*this, TemplateArgs, NumTemplateArgs, |
| 552 | Loc, Entity); |
| 553 | return Instantiator(T); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 554 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 555 | |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 556 | //===----------------------------------------------------------------------===/ |
| 557 | // Template Instantiation for Expressions |
| 558 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 559 | namespace { |
| 560 | class VISIBILITY_HIDDEN TemplateExprInstantiator |
| 561 | : public StmtVisitor<TemplateExprInstantiator, Sema::OwningExprResult> { |
| 562 | Sema &SemaRef; |
| 563 | const TemplateArgument *TemplateArgs; |
| 564 | unsigned NumTemplateArgs; |
| 565 | |
| 566 | public: |
| 567 | TemplateExprInstantiator(Sema &SemaRef, |
| 568 | const TemplateArgument *TemplateArgs, |
| 569 | unsigned NumTemplateArgs) |
| 570 | : SemaRef(SemaRef), TemplateArgs(TemplateArgs), |
| 571 | NumTemplateArgs(NumTemplateArgs) { } |
| 572 | |
| 573 | // FIXME: Once we get closer to completion, replace these |
| 574 | // manually-written declarations with automatically-generated ones |
| 575 | // from clang/AST/StmtNodes.def. |
| 576 | Sema::OwningExprResult VisitIntegerLiteral(IntegerLiteral *E); |
| 577 | Sema::OwningExprResult VisitDeclRefExpr(DeclRefExpr *E); |
| 578 | Sema::OwningExprResult VisitParenExpr(ParenExpr *E); |
Douglas Gregor | df03251 | 2009-03-12 22:46:12 +0000 | [diff] [blame^] | 579 | Sema::OwningExprResult VisitBinaryOperator(BinaryOperator *E); |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 580 | |
| 581 | // Base case. I'm supposed to ignore this. |
Douglas Gregor | df03251 | 2009-03-12 22:46:12 +0000 | [diff] [blame^] | 582 | Sema::OwningExprResult VisitStmt(Stmt *) { |
| 583 | assert(false && "Cannot instantiate this kind of expression"); |
| 584 | return SemaRef.ExprError(); |
| 585 | } |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 586 | }; |
| 587 | } |
| 588 | |
| 589 | Sema::OwningExprResult |
| 590 | TemplateExprInstantiator::VisitIntegerLiteral(IntegerLiteral *E) { |
| 591 | // FIXME: Can't we just re-use the expression node? |
| 592 | return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral(E->getValue(), |
| 593 | E->getType(), |
| 594 | E->getLocation())); |
| 595 | } |
| 596 | |
| 597 | Sema::OwningExprResult |
| 598 | TemplateExprInstantiator::VisitDeclRefExpr(DeclRefExpr *E) { |
| 599 | Decl *D = E->getDecl(); |
| 600 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) { |
| 601 | assert(NTTP->getDepth() == 0 && "No nested templates yet"); |
Douglas Gregor | c971f86 | 2009-03-12 22:20:26 +0000 | [diff] [blame] | 602 | const TemplateArgument &Arg = TemplateArgs[NTTP->getPosition()]; |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 603 | return SemaRef.Owned(new (SemaRef.Context) IntegerLiteral( |
Douglas Gregor | c971f86 | 2009-03-12 22:20:26 +0000 | [diff] [blame] | 604 | *Arg.getAsIntegral(), |
| 605 | Arg.getIntegralType(), |
| 606 | E->getSourceRange().getBegin())); |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 607 | } else |
| 608 | assert(false && "Can't handle arbitrary declaration references"); |
| 609 | |
| 610 | return SemaRef.ExprError(); |
| 611 | } |
| 612 | |
| 613 | Sema::OwningExprResult |
| 614 | TemplateExprInstantiator::VisitParenExpr(ParenExpr *E) { |
| 615 | Sema::OwningExprResult SubExpr |
| 616 | = SemaRef.InstantiateExpr(E->getSubExpr(), TemplateArgs, NumTemplateArgs); |
| 617 | if (SubExpr.isInvalid()) |
| 618 | return SemaRef.ExprError(); |
| 619 | |
| 620 | return SemaRef.Owned(new (SemaRef.Context) ParenExpr( |
| 621 | E->getLParen(), E->getRParen(), |
| 622 | (Expr *)SubExpr.release())); |
| 623 | } |
| 624 | |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 625 | Sema::OwningExprResult |
Douglas Gregor | df03251 | 2009-03-12 22:46:12 +0000 | [diff] [blame^] | 626 | TemplateExprInstantiator::VisitBinaryOperator(BinaryOperator *E) { |
| 627 | Sema::OwningExprResult LHS = Visit(E->getLHS()); |
| 628 | if (LHS.isInvalid()) |
| 629 | return SemaRef.ExprError(); |
| 630 | |
| 631 | Sema::OwningExprResult RHS = Visit(E->getRHS()); |
| 632 | if (RHS.isInvalid()) |
| 633 | return SemaRef.ExprError(); |
| 634 | |
| 635 | Sema::OwningExprResult Result |
| 636 | = SemaRef.CreateBuiltinBinOp(E->getOperatorLoc(), |
| 637 | E->getOpcode(), |
| 638 | (Expr *)LHS.get(), |
| 639 | (Expr *)RHS.get()); |
| 640 | if (Result.isInvalid()) |
| 641 | return SemaRef.ExprError(); |
| 642 | |
| 643 | LHS.release(); |
| 644 | RHS.release(); |
| 645 | return move(Result); |
| 646 | } |
| 647 | |
| 648 | Sema::OwningExprResult |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 649 | Sema::InstantiateExpr(Expr *E, const TemplateArgument *TemplateArgs, |
| 650 | unsigned NumTemplateArgs) { |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 651 | TemplateExprInstantiator Instantiator(*this, TemplateArgs, NumTemplateArgs); |
| 652 | return Instantiator.Visit(E); |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 655 | /// \brief Instantiate the base class specifiers of the given class |
| 656 | /// template specialization. |
| 657 | /// |
| 658 | /// Produces a diagnostic and returns true on error, returns false and |
| 659 | /// attaches the instantiated base classes to the class template |
| 660 | /// specialization if successful. |
| 661 | bool |
| 662 | Sema::InstantiateBaseSpecifiers( |
| 663 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 664 | ClassTemplateDecl *ClassTemplate) { |
| 665 | bool Invalid = false; |
| 666 | llvm::SmallVector<CXXBaseSpecifier*, 8> InstantiatedBases; |
| 667 | for (ClassTemplateSpecializationDecl::base_class_iterator |
| 668 | Base = ClassTemplate->getTemplatedDecl()->bases_begin(), |
| 669 | BaseEnd = ClassTemplate->getTemplatedDecl()->bases_end(); |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 670 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 671 | if (!Base->getType()->isDependentType()) { |
| 672 | // FIXME: Allocate via ASTContext |
| 673 | InstantiatedBases.push_back(new CXXBaseSpecifier(*Base)); |
| 674 | continue; |
| 675 | } |
| 676 | |
| 677 | QualType BaseType = InstantiateType(Base->getType(), |
| 678 | ClassTemplateSpec->getTemplateArgs(), |
| 679 | ClassTemplateSpec->getNumTemplateArgs(), |
| 680 | Base->getSourceRange().getBegin(), |
| 681 | DeclarationName()); |
| 682 | if (BaseType.isNull()) { |
| 683 | Invalid = true; |
| 684 | continue; |
| 685 | } |
| 686 | |
| 687 | if (CXXBaseSpecifier *InstantiatedBase |
| 688 | = CheckBaseSpecifier(ClassTemplateSpec, |
| 689 | Base->getSourceRange(), |
| 690 | Base->isVirtual(), |
| 691 | Base->getAccessSpecifierAsWritten(), |
| 692 | BaseType, |
| 693 | /*FIXME: Not totally accurate */ |
| 694 | Base->getSourceRange().getBegin())) |
| 695 | InstantiatedBases.push_back(InstantiatedBase); |
| 696 | else |
| 697 | Invalid = true; |
| 698 | } |
| 699 | |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 700 | if (!Invalid && |
| 701 | AttachBaseSpecifiers(ClassTemplateSpec, &InstantiatedBases[0], |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 702 | InstantiatedBases.size())) |
| 703 | Invalid = true; |
| 704 | |
| 705 | return Invalid; |
| 706 | } |
| 707 | |
| 708 | bool |
| 709 | Sema::InstantiateClassTemplateSpecialization( |
| 710 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 711 | bool ExplicitInstantiation) { |
| 712 | // Perform the actual instantiation on the canonical declaration. |
| 713 | ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( |
| 714 | Context.getCanonicalDecl(ClassTemplateSpec)); |
| 715 | |
| 716 | // We can only instantiate something that hasn't already been |
| 717 | // instantiated or specialized. Fail without any diagnostics: our |
| 718 | // caller will provide an error message. |
| 719 | if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) |
| 720 | return true; |
| 721 | |
| 722 | // FIXME: Push this class template instantiation onto the |
| 723 | // instantiation stack, checking for recursion that exceeds a |
| 724 | // certain depth. |
| 725 | |
| 726 | // FIXME: Perform class template partial specialization to select |
| 727 | // the best template. |
| 728 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
| 729 | |
| 730 | if (!Template->getTemplatedDecl()->getDefinition(Context)) { |
| 731 | Diag(ClassTemplateSpec->getLocation(), |
| 732 | diag::err_template_implicit_instantiate_undefined) |
| 733 | << Context.getTypeDeclType(ClassTemplateSpec); |
| 734 | Diag(Template->getTemplatedDecl()->getLocation(), |
| 735 | diag::note_template_decl_here); |
| 736 | return true; |
| 737 | } |
| 738 | |
| 739 | // Note that this is an instantiation. |
| 740 | ClassTemplateSpec->setSpecializationKind( |
| 741 | ExplicitInstantiation? TSK_ExplicitInstantiation |
| 742 | : TSK_ImplicitInstantiation); |
| 743 | |
| 744 | |
| 745 | bool Invalid = false; |
| 746 | |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 747 | InstantiatingTemplate Inst(*this, ClassTemplateSpec->getLocation(), |
| 748 | ClassTemplateSpec); |
| 749 | if (Inst) |
| 750 | return true; |
| 751 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 752 | // Enter the scope of this instantiation. We don't use |
| 753 | // PushDeclContext because we don't have a scope. |
| 754 | DeclContext *PreviousContext = CurContext; |
| 755 | CurContext = ClassTemplateSpec; |
| 756 | |
| 757 | // Start the definition of this instantiation. |
| 758 | ClassTemplateSpec->startDefinition(); |
| 759 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 760 | |
| 761 | // Instantiate the base class specifiers. |
| 762 | if (InstantiateBaseSpecifiers(ClassTemplateSpec, Template)) |
| 763 | Invalid = true; |
| 764 | |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 765 | // FIXME: Create the injected-class-name for the |
| 766 | // instantiation. Should this be a typedef or something like it? |
| 767 | |
| 768 | RecordDecl *Pattern = Template->getTemplatedDecl(); |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 769 | llvm::SmallVector<DeclTy *, 32> Fields; |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 770 | for (RecordDecl::decl_iterator Member = Pattern->decls_begin(), |
| 771 | MemberEnd = Pattern->decls_end(); |
| 772 | Member != MemberEnd; ++Member) { |
| 773 | if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(*Member)) { |
| 774 | // FIXME: Simplified instantiation of typedefs needs to be made |
| 775 | // "real". |
| 776 | QualType T = Typedef->getUnderlyingType(); |
| 777 | if (T->isDependentType()) { |
| 778 | T = InstantiateType(T, ClassTemplateSpec->getTemplateArgs(), |
| 779 | ClassTemplateSpec->getNumTemplateArgs(), |
| 780 | Typedef->getLocation(), |
| 781 | Typedef->getDeclName()); |
| 782 | if (T.isNull()) { |
| 783 | Invalid = true; |
| 784 | T = Context.IntTy; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | // Create the new typedef |
| 789 | TypedefDecl *New |
| 790 | = TypedefDecl::Create(Context, ClassTemplateSpec, |
| 791 | Typedef->getLocation(), |
| 792 | Typedef->getIdentifier(), |
| 793 | T); |
| 794 | ClassTemplateSpec->addDecl(New); |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 795 | } |
| 796 | else if (FieldDecl *Field = dyn_cast<FieldDecl>(*Member)) { |
| 797 | // FIXME: Simplified instantiation of fields needs to be made |
| 798 | // "real". |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 799 | bool InvalidDecl = false; |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 800 | QualType T = Field->getType(); |
| 801 | if (T->isDependentType()) { |
| 802 | T = InstantiateType(T, ClassTemplateSpec->getTemplateArgs(), |
| 803 | ClassTemplateSpec->getNumTemplateArgs(), |
| 804 | Field->getLocation(), |
| 805 | Field->getDeclName()); |
| 806 | if (!T.isNull() && T->isFunctionType()) { |
| 807 | // C++ [temp.arg.type]p3: |
| 808 | // If a declaration acquires a function type through a type |
| 809 | // dependent on a template-parameter and this causes a |
| 810 | // declaration that does not use the syntactic form of a |
| 811 | // function declarator to have function type, the program is |
| 812 | // ill-formed. |
| 813 | Diag(Field->getLocation(), diag::err_field_instantiates_to_function) |
| 814 | << T; |
| 815 | T = QualType(); |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 816 | InvalidDecl = true; |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 820 | Expr *BitWidth = Field->getBitWidth(); |
| 821 | if (InvalidDecl) |
| 822 | BitWidth = 0; |
| 823 | if (BitWidth && |
| 824 | (BitWidth->isTypeDependent() || BitWidth->isValueDependent())) { |
| 825 | OwningExprResult InstantiatedBitWidth |
| 826 | = InstantiateExpr(BitWidth, |
| 827 | ClassTemplateSpec->getTemplateArgs(), |
| 828 | ClassTemplateSpec->getNumTemplateArgs()); |
| 829 | if (InstantiatedBitWidth.isInvalid()) { |
| 830 | Invalid = InvalidDecl = true; |
| 831 | BitWidth = 0; |
| 832 | } else |
| 833 | BitWidth = (Expr *)InstantiatedBitWidth.release(); |
| 834 | } |
| 835 | |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 836 | FieldDecl *New = CheckFieldDecl(Field->getDeclName(), T, |
| 837 | ClassTemplateSpec, |
| 838 | Field->getLocation(), |
| 839 | Field->isMutable(), |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 840 | BitWidth, |
Douglas Gregor | 4dd55f5 | 2009-03-11 20:50:30 +0000 | [diff] [blame] | 841 | Field->getAccess(), |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 842 | 0); |
| 843 | if (New) { |
| 844 | ClassTemplateSpec->addDecl(New); |
| 845 | Fields.push_back(New); |
| 846 | |
Douglas Gregor | a0e500d | 2009-03-12 16:53:44 +0000 | [diff] [blame] | 847 | if (InvalidDecl) |
| 848 | New->setInvalidDecl(); |
| 849 | |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 850 | if (New->isInvalidDecl()) |
| 851 | Invalid = true; |
| 852 | } |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | |
Douglas Gregor | 3cf538d | 2009-03-11 18:59:21 +0000 | [diff] [blame] | 856 | // Finish checking fields. |
| 857 | ActOnFields(0, ClassTemplateSpec->getLocation(), ClassTemplateSpec, |
| 858 | &Fields[0], Fields.size(), SourceLocation(), SourceLocation(), |
| 859 | 0); |
| 860 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 861 | // Add any implicitly-declared members that we might need. |
| 862 | AddImplicitlyDeclaredMembersToClass(ClassTemplateSpec); |
| 863 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 864 | // Exit the scope of this instantiation. |
| 865 | CurContext = PreviousContext; |
| 866 | |
| 867 | return Invalid; |
| 868 | } |