Douglas Gregor | 7429654 | 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 | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Compiler.h" |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| 23 | |
Douglas Gregor | fee85d6 | 2009-03-10 18:03:33 +0000 | [diff] [blame^] | 24 | //===----------------------------------------------------------------------===/ |
| 25 | // Template Instantiation Support |
| 26 | //===----------------------------------------------------------------------===/ |
| 27 | |
Douglas Gregor | 375733c | 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) { |
| 33 | if (SemaRef.ActiveTemplateInstantiations.size() |
| 34 | > SemaRef.getLangOptions().InstantiationDepth) { |
| 35 | SemaRef.Diag(PointOfInstantiation, |
| 36 | diag::err_template_recursion_depth_exceeded) |
| 37 | << SemaRef.getLangOptions().InstantiationDepth |
| 38 | << InstantiationRange; |
| 39 | SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth) |
| 40 | << SemaRef.getLangOptions().InstantiationDepth; |
| 41 | Invalid = true; |
| 42 | } else { |
| 43 | ActiveTemplateInstantiation Inst; |
| 44 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 45 | Inst.Entity = Entity; |
| 46 | Inst.InstantiationRange = InstantiationRange; |
| 47 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 48 | Invalid = false; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | Sema::InstantiatingTemplate::~InstantiatingTemplate() { |
| 53 | if (!Invalid) |
| 54 | SemaRef.ActiveTemplateInstantiations.pop_back(); |
| 55 | } |
| 56 | |
Douglas Gregor | fee85d6 | 2009-03-10 18:03:33 +0000 | [diff] [blame^] | 57 | /// \brief Post-diagnostic hook for printing the instantiation stack. |
| 58 | void Sema::PrintInstantiationStackHook(unsigned, void *Cookie) { |
| 59 | static_cast<Sema*>(Cookie)->PrintInstantiationStack(); |
| 60 | } |
| 61 | |
| 62 | /// \brief Prints the current instantiation stack through a series of |
| 63 | /// notes. |
| 64 | void Sema::PrintInstantiationStack() { |
| 65 | for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator |
| 66 | Active = ActiveTemplateInstantiations.rbegin(), |
| 67 | ActiveEnd = ActiveTemplateInstantiations.rend(); |
| 68 | Active != ActiveEnd; |
| 69 | ++Active) { |
| 70 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 71 | diag::note_template_class_instantiation_here) |
| 72 | << Context.getTypeDeclType(Active->Entity) |
| 73 | << Active->InstantiationRange; |
| 74 | } |
| 75 | } |
| 76 | |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 77 | //===----------------------------------------------------------------------===/ |
| 78 | // Template Instantiation for Types |
| 79 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 80 | namespace { |
| 81 | class VISIBILITY_HIDDEN TemplateTypeInstantiator { |
| 82 | Sema &SemaRef; |
| 83 | const TemplateArgument *TemplateArgs; |
| 84 | unsigned NumTemplateArgs; |
| 85 | SourceLocation Loc; |
| 86 | DeclarationName Entity; |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 87 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 88 | public: |
| 89 | TemplateTypeInstantiator(Sema &SemaRef, |
| 90 | const TemplateArgument *TemplateArgs, |
| 91 | unsigned NumTemplateArgs, |
| 92 | SourceLocation Loc, |
| 93 | DeclarationName Entity) |
| 94 | : SemaRef(SemaRef), TemplateArgs(TemplateArgs), |
| 95 | NumTemplateArgs(NumTemplateArgs), Loc(Loc), Entity(Entity) { } |
| 96 | |
| 97 | QualType operator()(QualType T) const { return Instantiate(T); } |
| 98 | |
| 99 | QualType Instantiate(QualType T) const; |
| 100 | |
| 101 | // Declare instantiate functions for each type. |
| 102 | #define TYPE(Class, Base) \ |
| 103 | QualType Instantiate##Class##Type(const Class##Type *T, \ |
| 104 | unsigned Quals) const; |
| 105 | #define ABSTRACT_TYPE(Class, Base) |
| 106 | #include "clang/AST/TypeNodes.def" |
| 107 | }; |
| 108 | } |
| 109 | |
| 110 | QualType |
| 111 | TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T, |
| 112 | unsigned Quals) const { |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 113 | // FIXME: Implement this |
| 114 | assert(false && "Cannot instantiate ExtQualType yet"); |
| 115 | return QualType(); |
| 116 | } |
| 117 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 118 | QualType |
| 119 | TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T, |
| 120 | unsigned Quals) const { |
Douglas Gregor | c0139ca | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 121 | assert(false && "Builtin types are not dependent and cannot be instantiated"); |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 122 | return QualType(T, Quals); |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 125 | QualType |
| 126 | TemplateTypeInstantiator:: |
| 127 | InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const { |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 128 | // FIXME: Implement this |
| 129 | assert(false && "Cannot instantiate FixedWidthIntType yet"); |
| 130 | return QualType(); |
| 131 | } |
| 132 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 133 | QualType |
| 134 | TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T, |
| 135 | unsigned Quals) const { |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 136 | // FIXME: Implement this |
| 137 | assert(false && "Cannot instantiate ComplexType yet"); |
| 138 | return QualType(); |
| 139 | } |
| 140 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 141 | QualType |
| 142 | TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T, |
| 143 | unsigned Quals) const { |
| 144 | QualType PointeeType = Instantiate(T->getPointeeType()); |
| 145 | if (PointeeType.isNull()) |
| 146 | return QualType(); |
| 147 | |
| 148 | return SemaRef.BuildPointerType(PointeeType, Quals, Loc, Entity); |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 151 | QualType |
| 152 | TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T, |
| 153 | unsigned Quals) const { |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 154 | // FIXME: Implement this |
| 155 | assert(false && "Cannot instantiate BlockPointerType yet"); |
| 156 | return QualType(); |
| 157 | } |
| 158 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 159 | QualType |
| 160 | TemplateTypeInstantiator::InstantiateReferenceType(const ReferenceType *T, |
| 161 | unsigned Quals) const { |
| 162 | QualType ReferentType = Instantiate(T->getPointeeType()); |
| 163 | if (ReferentType.isNull()) |
| 164 | return QualType(); |
| 165 | |
| 166 | return SemaRef.BuildReferenceType(ReferentType, Quals, Loc, Entity); |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 169 | QualType |
| 170 | TemplateTypeInstantiator:: |
| 171 | InstantiateMemberPointerType(const MemberPointerType *T, |
| 172 | unsigned Quals) const { |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 173 | // FIXME: Implement this |
| 174 | assert(false && "Cannot instantiate MemberPointerType yet"); |
| 175 | return QualType(); |
| 176 | } |
| 177 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 178 | QualType |
| 179 | TemplateTypeInstantiator:: |
| 180 | InstantiateConstantArrayType(const ConstantArrayType *T, |
| 181 | unsigned Quals) const { |
| 182 | QualType ElementType = Instantiate(T->getElementType()); |
| 183 | if (ElementType.isNull()) |
| 184 | return ElementType; |
| 185 | |
| 186 | // Build a temporary integer literal to specify the size for |
| 187 | // BuildArrayType. Since we have already checked the size as part of |
| 188 | // creating the dependent array type in the first place, we know |
| 189 | // there aren't any errors. |
Douglas Gregor | 448fcd3 | 2009-03-09 20:07:22 +0000 | [diff] [blame] | 190 | // FIXME: Is IntTy big enough? Maybe not, but LongLongTy causes |
| 191 | // problems that I have yet to investigate. |
| 192 | IntegerLiteral ArraySize(T->getSize(), SemaRef.Context.IntTy, Loc); |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 193 | return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(), |
| 194 | &ArraySize, T->getIndexTypeQualifier(), |
| 195 | Loc, Entity); |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 198 | QualType |
| 199 | TemplateTypeInstantiator:: |
| 200 | InstantiateIncompleteArrayType(const IncompleteArrayType *T, |
| 201 | unsigned Quals) const { |
| 202 | QualType ElementType = Instantiate(T->getElementType()); |
| 203 | if (ElementType.isNull()) |
| 204 | return ElementType; |
| 205 | |
| 206 | return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(), |
| 207 | 0, T->getIndexTypeQualifier(), |
| 208 | Loc, Entity); |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 211 | QualType |
| 212 | TemplateTypeInstantiator:: |
| 213 | InstantiateVariableArrayType(const VariableArrayType *T, |
| 214 | unsigned Quals) const { |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 215 | // FIXME: Implement this |
| 216 | assert(false && "Cannot instantiate VariableArrayType yet"); |
| 217 | return QualType(); |
| 218 | } |
| 219 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 220 | QualType |
| 221 | TemplateTypeInstantiator:: |
| 222 | InstantiateDependentSizedArrayType(const DependentSizedArrayType *T, |
| 223 | unsigned Quals) const { |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 224 | // FIXME: Implement this |
| 225 | assert(false && "Cannot instantiate DependentSizedArrayType yet"); |
| 226 | return QualType(); |
| 227 | } |
| 228 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 229 | QualType |
| 230 | TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T, |
| 231 | unsigned Quals) const { |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 232 | // FIXME: Implement this |
| 233 | assert(false && "Cannot instantiate VectorType yet"); |
| 234 | return QualType(); |
| 235 | } |
| 236 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 237 | QualType |
| 238 | TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T, |
| 239 | unsigned Quals) const { |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 240 | // FIXME: Implement this |
| 241 | assert(false && "Cannot instantiate ExtVectorType yet"); |
| 242 | return QualType(); |
| 243 | } |
| 244 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 245 | QualType |
| 246 | TemplateTypeInstantiator:: |
| 247 | InstantiateFunctionProtoType(const FunctionProtoType *T, |
| 248 | unsigned Quals) const { |
Douglas Gregor | c0139ca | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 249 | QualType ResultType = Instantiate(T->getResultType()); |
| 250 | if (ResultType.isNull()) |
| 251 | return ResultType; |
| 252 | |
| 253 | llvm::SmallVector<QualType, 16> ParamTypes; |
| 254 | for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(), |
| 255 | ParamEnd = T->arg_type_end(); |
| 256 | Param != ParamEnd; ++Param) { |
| 257 | QualType P = Instantiate(*Param); |
| 258 | if (P.isNull()) |
| 259 | return P; |
| 260 | |
| 261 | ParamTypes.push_back(P); |
| 262 | } |
| 263 | |
| 264 | return SemaRef.BuildFunctionType(ResultType, &ParamTypes[0], |
| 265 | ParamTypes.size(), |
| 266 | T->isVariadic(), T->getTypeQuals(), |
| 267 | Loc, Entity); |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 270 | QualType |
| 271 | TemplateTypeInstantiator:: |
| 272 | InstantiateFunctionNoProtoType(const FunctionNoProtoType *T, |
| 273 | unsigned Quals) const { |
Douglas Gregor | c0139ca | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 274 | assert(false && "Functions without prototypes cannot be dependent."); |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 275 | return QualType(); |
| 276 | } |
| 277 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 278 | QualType |
| 279 | TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T, |
| 280 | unsigned Quals) const { |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 281 | // FIXME: Implement this |
| 282 | assert(false && "Cannot instantiate TypedefType yet"); |
| 283 | return QualType(); |
| 284 | } |
| 285 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 286 | QualType |
| 287 | TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T, |
| 288 | unsigned Quals) const { |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 289 | // FIXME: Implement this |
| 290 | assert(false && "Cannot instantiate TypeOfExprType yet"); |
| 291 | return QualType(); |
| 292 | } |
| 293 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 294 | QualType |
| 295 | TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T, |
| 296 | unsigned Quals) const { |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 297 | // FIXME: Implement this |
| 298 | assert(false && "Cannot instantiate TypeOfType yet"); |
| 299 | return QualType(); |
| 300 | } |
| 301 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 302 | QualType |
| 303 | TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T, |
| 304 | unsigned Quals) const { |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 305 | // FIXME: Implement this |
| 306 | assert(false && "Cannot instantiate RecordType yet"); |
| 307 | return QualType(); |
| 308 | } |
| 309 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 310 | QualType |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 311 | TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T, |
| 312 | unsigned Quals) const { |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 313 | // FIXME: Implement this |
| 314 | assert(false && "Cannot instantiate EnumType yet"); |
| 315 | return QualType(); |
| 316 | } |
| 317 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 318 | QualType |
| 319 | TemplateTypeInstantiator:: |
| 320 | InstantiateTemplateTypeParmType(const TemplateTypeParmType *T, |
| 321 | unsigned Quals) const { |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 322 | if (T->getDepth() == 0) { |
| 323 | // Replace the template type parameter with its corresponding |
| 324 | // template argument. |
| 325 | assert(T->getIndex() < NumTemplateArgs && "Wrong # of template args"); |
| 326 | assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type && |
| 327 | "Template argument kind mismatch"); |
| 328 | QualType Result = TemplateArgs[T->getIndex()].getAsType(); |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 329 | if (Result.isNull() || !Quals) |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 330 | return Result; |
| 331 | |
| 332 | // C++ [dcl.ref]p1: |
| 333 | // [...] Cv-qualified references are ill-formed except when |
| 334 | // the cv-qualifiers are introduced through the use of a |
| 335 | // typedef (7.1.3) or of a template type argument (14.3), in |
| 336 | // which case the cv-qualifiers are ignored. |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 337 | if (Quals && Result->isReferenceType()) |
| 338 | Quals = 0; |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 339 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 340 | return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers()); |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | // The template type parameter comes from an inner template (e.g., |
| 344 | // the template parameter list of a member template inside the |
| 345 | // template we are instantiating). Create a new template type |
| 346 | // parameter with the template "level" reduced by one. |
| 347 | return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1, |
| 348 | T->getIndex(), |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 349 | T->getName()) |
| 350 | .getQualifiedType(Quals); |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 353 | QualType |
| 354 | TemplateTypeInstantiator:: |
| 355 | InstantiateClassTemplateSpecializationType( |
| 356 | const ClassTemplateSpecializationType *T, |
| 357 | unsigned Quals) const { |
Douglas Gregor | f9ff4b1 | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 358 | llvm::SmallVector<TemplateArgument, 16> InstantiatedTemplateArgs; |
| 359 | InstantiatedTemplateArgs.reserve(T->getNumArgs()); |
| 360 | for (ClassTemplateSpecializationType::iterator Arg = T->begin(), |
| 361 | ArgEnd = T->end(); |
| 362 | Arg != ArgEnd; ++Arg) { |
| 363 | switch (Arg->getKind()) { |
| 364 | case TemplateArgument::Type: { |
| 365 | QualType T = SemaRef.InstantiateType(Arg->getAsType(), |
| 366 | TemplateArgs, NumTemplateArgs, |
| 367 | Arg->getLocation(), |
| 368 | DeclarationName()); |
| 369 | if (T.isNull()) |
| 370 | return QualType(); |
| 371 | |
| 372 | InstantiatedTemplateArgs.push_back( |
| 373 | TemplateArgument(Arg->getLocation(), T)); |
| 374 | break; |
| 375 | } |
| 376 | |
| 377 | case TemplateArgument::Declaration: |
| 378 | case TemplateArgument::Integral: |
| 379 | InstantiatedTemplateArgs.push_back(*Arg); |
| 380 | break; |
| 381 | |
| 382 | case TemplateArgument::Expression: |
| 383 | assert(false && "Cannot instantiate expressions yet"); |
| 384 | break; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | // FIXME: We're missing the locations of the template name, '<', and |
| 389 | // '>'. |
| 390 | return SemaRef.CheckClassTemplateId(cast<ClassTemplateDecl>(T->getTemplate()), |
| 391 | Loc, |
| 392 | SourceLocation(), |
| 393 | &InstantiatedTemplateArgs[0], |
| 394 | InstantiatedTemplateArgs.size(), |
| 395 | SourceLocation()); |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 398 | QualType |
| 399 | TemplateTypeInstantiator:: |
| 400 | InstantiateObjCInterfaceType(const ObjCInterfaceType *T, |
| 401 | unsigned Quals) const { |
| 402 | assert(false && "Objective-C types cannot be dependent"); |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 403 | return QualType(); |
| 404 | } |
| 405 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 406 | QualType |
| 407 | TemplateTypeInstantiator:: |
| 408 | InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T, |
| 409 | unsigned Quals) const { |
| 410 | assert(false && "Objective-C types cannot be dependent"); |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 411 | return QualType(); |
| 412 | } |
| 413 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 414 | QualType |
| 415 | TemplateTypeInstantiator:: |
| 416 | InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T, |
| 417 | unsigned Quals) const { |
| 418 | assert(false && "Objective-C types cannot be dependent"); |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 419 | return QualType(); |
| 420 | } |
| 421 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 422 | QualType |
| 423 | TemplateTypeInstantiator:: |
| 424 | InstantiateObjCQualifiedClassType(const ObjCQualifiedClassType *T, |
| 425 | unsigned Quals) const { |
| 426 | assert(false && "Objective-C types cannot be dependent"); |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 427 | return QualType(); |
| 428 | } |
| 429 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 430 | /// \brief The actual implementation of Sema::InstantiateType(). |
| 431 | QualType TemplateTypeInstantiator::Instantiate(QualType T) const { |
| 432 | // If T is not a dependent type, there is nothing to do. |
| 433 | if (!T->isDependentType()) |
| 434 | return T; |
| 435 | |
| 436 | switch (T->getTypeClass()) { |
| 437 | #define TYPE(Class, Base) \ |
| 438 | case Type::Class: \ |
| 439 | return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \ |
| 440 | T.getCVRQualifiers()); |
| 441 | #define ABSTRACT_TYPE(Class, Base) |
| 442 | #include "clang/AST/TypeNodes.def" |
| 443 | } |
| 444 | |
| 445 | assert(false && "Not all types have been decoded for instantiation"); |
| 446 | return QualType(); |
| 447 | } |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 448 | |
| 449 | /// \brief Instantiate the type T with a given set of template arguments. |
| 450 | /// |
| 451 | /// This routine substitutes the given template arguments into the |
| 452 | /// type T and produces the instantiated type. |
| 453 | /// |
| 454 | /// \param T the type into which the template arguments will be |
| 455 | /// substituted. If this type is not dependent, it will be returned |
| 456 | /// immediately. |
| 457 | /// |
| 458 | /// \param TemplateArgs the template arguments that will be |
| 459 | /// substituted for the top-level template parameters within T. |
| 460 | /// |
| 461 | /// \param NumTemplateArgs the number of template arguments provided |
| 462 | /// by TemplateArgs. |
| 463 | /// |
| 464 | /// \param Loc the location in the source code where this substitution |
| 465 | /// is being performed. It will typically be the location of the |
| 466 | /// declarator (if we're instantiating the type of some declaration) |
| 467 | /// or the location of the type in the source code (if, e.g., we're |
| 468 | /// instantiating the type of a cast expression). |
| 469 | /// |
| 470 | /// \param Entity the name of the entity associated with a declaration |
| 471 | /// being instantiated (if any). May be empty to indicate that there |
| 472 | /// is no such entity (if, e.g., this is a type that occurs as part of |
| 473 | /// a cast expression) or that the entity has no name (e.g., an |
| 474 | /// unnamed function parameter). |
| 475 | /// |
| 476 | /// \returns If the instantiation succeeds, the instantiated |
| 477 | /// type. Otherwise, produces diagnostics and returns a NULL type. |
| 478 | QualType Sema::InstantiateType(QualType T, |
| 479 | const TemplateArgument *TemplateArgs, |
| 480 | unsigned NumTemplateArgs, |
| 481 | SourceLocation Loc, DeclarationName Entity) { |
| 482 | // If T is not a dependent type, there is nothing to do. |
| 483 | if (!T->isDependentType()) |
| 484 | return T; |
| 485 | |
Douglas Gregor | f57dcd0 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 486 | TemplateTypeInstantiator Instantiator(*this, TemplateArgs, NumTemplateArgs, |
| 487 | Loc, Entity); |
| 488 | return Instantiator(T); |
Douglas Gregor | 7429654 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 489 | } |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 490 | |
| 491 | /// \brief Instantiate the base class specifiers of the given class |
| 492 | /// template specialization. |
| 493 | /// |
| 494 | /// Produces a diagnostic and returns true on error, returns false and |
| 495 | /// attaches the instantiated base classes to the class template |
| 496 | /// specialization if successful. |
| 497 | bool |
| 498 | Sema::InstantiateBaseSpecifiers( |
| 499 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 500 | ClassTemplateDecl *ClassTemplate) { |
| 501 | bool Invalid = false; |
| 502 | llvm::SmallVector<CXXBaseSpecifier*, 8> InstantiatedBases; |
| 503 | for (ClassTemplateSpecializationDecl::base_class_iterator |
| 504 | Base = ClassTemplate->getTemplatedDecl()->bases_begin(), |
| 505 | BaseEnd = ClassTemplate->getTemplatedDecl()->bases_end(); |
| 506 | Base != BaseEnd && !Invalid; ++Base) { |
| 507 | if (!Base->getType()->isDependentType()) { |
| 508 | // FIXME: Allocate via ASTContext |
| 509 | InstantiatedBases.push_back(new CXXBaseSpecifier(*Base)); |
| 510 | continue; |
| 511 | } |
| 512 | |
| 513 | QualType BaseType = InstantiateType(Base->getType(), |
| 514 | ClassTemplateSpec->getTemplateArgs(), |
| 515 | ClassTemplateSpec->getNumTemplateArgs(), |
| 516 | Base->getSourceRange().getBegin(), |
| 517 | DeclarationName()); |
| 518 | if (BaseType.isNull()) { |
| 519 | Invalid = true; |
| 520 | continue; |
| 521 | } |
| 522 | |
| 523 | if (CXXBaseSpecifier *InstantiatedBase |
| 524 | = CheckBaseSpecifier(ClassTemplateSpec, |
| 525 | Base->getSourceRange(), |
| 526 | Base->isVirtual(), |
| 527 | Base->getAccessSpecifierAsWritten(), |
| 528 | BaseType, |
| 529 | /*FIXME: Not totally accurate */ |
| 530 | Base->getSourceRange().getBegin())) |
| 531 | InstantiatedBases.push_back(InstantiatedBase); |
| 532 | else |
| 533 | Invalid = true; |
| 534 | } |
| 535 | |
| 536 | if (AttachBaseSpecifiers(ClassTemplateSpec, &InstantiatedBases[0], |
| 537 | InstantiatedBases.size())) |
| 538 | Invalid = true; |
| 539 | |
| 540 | return Invalid; |
| 541 | } |
| 542 | |
| 543 | bool |
| 544 | Sema::InstantiateClassTemplateSpecialization( |
| 545 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 546 | bool ExplicitInstantiation) { |
| 547 | // Perform the actual instantiation on the canonical declaration. |
| 548 | ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( |
| 549 | Context.getCanonicalDecl(ClassTemplateSpec)); |
| 550 | |
| 551 | // We can only instantiate something that hasn't already been |
| 552 | // instantiated or specialized. Fail without any diagnostics: our |
| 553 | // caller will provide an error message. |
| 554 | if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) |
| 555 | return true; |
| 556 | |
| 557 | // FIXME: Push this class template instantiation onto the |
| 558 | // instantiation stack, checking for recursion that exceeds a |
| 559 | // certain depth. |
| 560 | |
| 561 | // FIXME: Perform class template partial specialization to select |
| 562 | // the best template. |
| 563 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
| 564 | |
| 565 | if (!Template->getTemplatedDecl()->getDefinition(Context)) { |
| 566 | Diag(ClassTemplateSpec->getLocation(), |
| 567 | diag::err_template_implicit_instantiate_undefined) |
| 568 | << Context.getTypeDeclType(ClassTemplateSpec); |
| 569 | Diag(Template->getTemplatedDecl()->getLocation(), |
| 570 | diag::note_template_decl_here); |
| 571 | return true; |
| 572 | } |
| 573 | |
| 574 | // Note that this is an instantiation. |
| 575 | ClassTemplateSpec->setSpecializationKind( |
| 576 | ExplicitInstantiation? TSK_ExplicitInstantiation |
| 577 | : TSK_ImplicitInstantiation); |
| 578 | |
| 579 | |
| 580 | bool Invalid = false; |
| 581 | |
Douglas Gregor | 375733c | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 582 | InstantiatingTemplate Inst(*this, ClassTemplateSpec->getLocation(), |
| 583 | ClassTemplateSpec); |
| 584 | if (Inst) |
| 585 | return true; |
| 586 | |
Douglas Gregor | ed3a398 | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 587 | // Enter the scope of this instantiation. We don't use |
| 588 | // PushDeclContext because we don't have a scope. |
| 589 | DeclContext *PreviousContext = CurContext; |
| 590 | CurContext = ClassTemplateSpec; |
| 591 | |
| 592 | // Start the definition of this instantiation. |
| 593 | ClassTemplateSpec->startDefinition(); |
| 594 | |
| 595 | // FIXME: Create the injected-class-name for the |
| 596 | // instantiation. Should this be a typedef or something like it? |
| 597 | |
| 598 | // Instantiate the base class specifiers. |
| 599 | if (InstantiateBaseSpecifiers(ClassTemplateSpec, Template)) |
| 600 | Invalid = true; |
| 601 | |
| 602 | // FIXME: Instantiate all of the members. |
| 603 | |
| 604 | // Add any implicitly-declared members that we might need. |
| 605 | AddImplicitlyDeclaredMembersToClass(ClassTemplateSpec); |
| 606 | |
| 607 | // Finish the definition of this instantiation. |
| 608 | // FIXME: ActOnFields does more checking, which we'll eventually need. |
| 609 | ClassTemplateSpec->completeDefinition(Context); |
| 610 | |
| 611 | // Exit the scope of this instantiation. |
| 612 | CurContext = PreviousContext; |
| 613 | |
| 614 | return Invalid; |
| 615 | } |