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