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