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" |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTConsumer.h" |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/Expr.h" |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 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 | 54dabfc | 2009-05-14 23:26:13 +0000 | [diff] [blame] | 28 | /// \brief Retrieve the template argument list that should be used to |
| 29 | /// instantiate the given declaration. |
| 30 | const TemplateArgumentList & |
| 31 | Sema::getTemplateInstantiationArgs(NamedDecl *D) { |
| 32 | if (ClassTemplateSpecializationDecl *Spec |
| 33 | = dyn_cast<ClassTemplateSpecializationDecl>(D)) |
| 34 | return Spec->getTemplateArgs(); |
| 35 | |
| 36 | DeclContext *EnclosingTemplateCtx = D->getDeclContext(); |
| 37 | while (!isa<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx)) { |
| 38 | assert(!EnclosingTemplateCtx->isFileContext() && |
| 39 | "Tried to get the instantiation arguments of a non-template"); |
| 40 | EnclosingTemplateCtx = EnclosingTemplateCtx->getParent(); |
| 41 | } |
| 42 | |
| 43 | ClassTemplateSpecializationDecl *EnclosingTemplate |
| 44 | = cast<ClassTemplateSpecializationDecl>(EnclosingTemplateCtx); |
| 45 | return EnclosingTemplate->getTemplateArgs(); |
| 46 | } |
| 47 | |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 48 | Sema::InstantiatingTemplate:: |
| 49 | InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 50 | Decl *Entity, |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 51 | SourceRange InstantiationRange) |
| 52 | : SemaRef(SemaRef) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 53 | |
| 54 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 55 | InstantiationRange); |
| 56 | if (!Invalid) { |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 57 | ActiveTemplateInstantiation Inst; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 58 | Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation; |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 59 | Inst.PointOfInstantiation = PointOfInstantiation; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 60 | Inst.Entity = reinterpret_cast<uintptr_t>(Entity); |
Douglas Gregor | 313a81d | 2009-03-12 18:36:18 +0000 | [diff] [blame] | 61 | Inst.TemplateArgs = 0; |
| 62 | Inst.NumTemplateArgs = 0; |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 63 | Inst.InstantiationRange = InstantiationRange; |
| 64 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 65 | Invalid = false; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
| 70 | SourceLocation PointOfInstantiation, |
| 71 | TemplateDecl *Template, |
| 72 | const TemplateArgument *TemplateArgs, |
| 73 | unsigned NumTemplateArgs, |
| 74 | SourceRange InstantiationRange) |
| 75 | : SemaRef(SemaRef) { |
| 76 | |
| 77 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 78 | InstantiationRange); |
| 79 | if (!Invalid) { |
| 80 | ActiveTemplateInstantiation Inst; |
| 81 | Inst.Kind |
| 82 | = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation; |
| 83 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 84 | Inst.Entity = reinterpret_cast<uintptr_t>(Template); |
| 85 | Inst.TemplateArgs = TemplateArgs; |
| 86 | Inst.NumTemplateArgs = NumTemplateArgs; |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 87 | Inst.InstantiationRange = InstantiationRange; |
| 88 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 89 | Invalid = false; |
| 90 | } |
| 91 | } |
| 92 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 93 | Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, |
| 94 | SourceLocation PointOfInstantiation, |
| 95 | ClassTemplatePartialSpecializationDecl *PartialSpec, |
| 96 | const TemplateArgument *TemplateArgs, |
| 97 | unsigned NumTemplateArgs, |
| 98 | SourceRange InstantiationRange) |
| 99 | : SemaRef(SemaRef) { |
| 100 | |
| 101 | Invalid = CheckInstantiationDepth(PointOfInstantiation, |
| 102 | InstantiationRange); |
| 103 | if (!Invalid) { |
| 104 | ActiveTemplateInstantiation Inst; |
| 105 | Inst.Kind |
| 106 | = ActiveTemplateInstantiation::PartialSpecDeductionInstantiation; |
| 107 | Inst.PointOfInstantiation = PointOfInstantiation; |
| 108 | Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec); |
| 109 | Inst.TemplateArgs = TemplateArgs; |
| 110 | Inst.NumTemplateArgs = NumTemplateArgs; |
| 111 | Inst.InstantiationRange = InstantiationRange; |
| 112 | SemaRef.ActiveTemplateInstantiations.push_back(Inst); |
| 113 | Invalid = false; |
| 114 | } |
| 115 | } |
| 116 | |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 117 | void Sema::InstantiatingTemplate::Clear() { |
| 118 | if (!Invalid) { |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 119 | SemaRef.ActiveTemplateInstantiations.pop_back(); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 120 | Invalid = true; |
| 121 | } |
Douglas Gregor | 26dce44 | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 124 | bool Sema::InstantiatingTemplate::CheckInstantiationDepth( |
| 125 | SourceLocation PointOfInstantiation, |
| 126 | SourceRange InstantiationRange) { |
| 127 | if (SemaRef.ActiveTemplateInstantiations.size() |
| 128 | <= SemaRef.getLangOptions().InstantiationDepth) |
| 129 | return false; |
| 130 | |
| 131 | SemaRef.Diag(PointOfInstantiation, |
| 132 | diag::err_template_recursion_depth_exceeded) |
| 133 | << SemaRef.getLangOptions().InstantiationDepth |
| 134 | << InstantiationRange; |
| 135 | SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth) |
| 136 | << SemaRef.getLangOptions().InstantiationDepth; |
| 137 | return true; |
| 138 | } |
| 139 | |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 140 | /// \brief Prints the current instantiation stack through a series of |
| 141 | /// notes. |
| 142 | void Sema::PrintInstantiationStack() { |
| 143 | for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator |
| 144 | Active = ActiveTemplateInstantiations.rbegin(), |
| 145 | ActiveEnd = ActiveTemplateInstantiations.rend(); |
| 146 | Active != ActiveEnd; |
| 147 | ++Active) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 148 | switch (Active->Kind) { |
| 149 | case ActiveTemplateInstantiation::TemplateInstantiation: { |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 150 | Decl *D = reinterpret_cast<Decl *>(Active->Entity); |
| 151 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 152 | unsigned DiagID = diag::note_template_member_class_here; |
| 153 | if (isa<ClassTemplateSpecializationDecl>(Record)) |
| 154 | DiagID = diag::note_template_class_instantiation_here; |
| 155 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 156 | DiagID) |
| 157 | << Context.getTypeDeclType(Record) |
| 158 | << Active->InstantiationRange; |
| 159 | } else { |
| 160 | FunctionDecl *Function = cast<FunctionDecl>(D); |
| 161 | unsigned DiagID = diag::note_template_member_function_here; |
| 162 | // FIXME: check for a function template |
| 163 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 164 | DiagID) |
| 165 | << Function |
| 166 | << Active->InstantiationRange; |
| 167 | } |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 168 | break; |
| 169 | } |
| 170 | |
| 171 | case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: { |
| 172 | TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity); |
| 173 | std::string TemplateArgsStr |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 174 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 175 | Active->TemplateArgs, |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 176 | Active->NumTemplateArgs, |
| 177 | Context.PrintingPolicy); |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 178 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 179 | diag::note_default_arg_instantiation_here) |
| 180 | << (Template->getNameAsString() + TemplateArgsStr) |
| 181 | << Active->InstantiationRange; |
| 182 | break; |
| 183 | } |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 184 | |
| 185 | case ActiveTemplateInstantiation::PartialSpecDeductionInstantiation: { |
| 186 | ClassTemplatePartialSpecializationDecl *PartialSpec |
| 187 | = cast<ClassTemplatePartialSpecializationDecl>((Decl *)Active->Entity); |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 188 | // FIXME: The active template instantiation's template arguments |
| 189 | // are interesting, too. We should add something like [with T = |
| 190 | // foo, U = bar, etc.] to the string. |
| 191 | Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr), |
| 192 | diag::note_partial_spec_deduct_instantiation_here) |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame^] | 193 | << Context.getTypeDeclType(PartialSpec) |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 194 | << Active->InstantiationRange; |
| 195 | break; |
| 196 | } |
| 197 | |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 198 | } |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 202 | //===----------------------------------------------------------------------===/ |
| 203 | // Template Instantiation for Types |
| 204 | //===----------------------------------------------------------------------===/ |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 205 | namespace { |
| 206 | class VISIBILITY_HIDDEN TemplateTypeInstantiator { |
| 207 | Sema &SemaRef; |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 208 | const TemplateArgumentList &TemplateArgs; |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 209 | SourceLocation Loc; |
| 210 | DeclarationName Entity; |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 211 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 212 | public: |
| 213 | TemplateTypeInstantiator(Sema &SemaRef, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 214 | const TemplateArgumentList &TemplateArgs, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 215 | SourceLocation Loc, |
| 216 | DeclarationName Entity) |
| 217 | : SemaRef(SemaRef), TemplateArgs(TemplateArgs), |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 218 | Loc(Loc), Entity(Entity) { } |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 219 | |
| 220 | QualType operator()(QualType T) const { return Instantiate(T); } |
| 221 | |
| 222 | QualType Instantiate(QualType T) const; |
| 223 | |
| 224 | // Declare instantiate functions for each type. |
| 225 | #define TYPE(Class, Base) \ |
| 226 | QualType Instantiate##Class##Type(const Class##Type *T, \ |
| 227 | unsigned Quals) const; |
| 228 | #define ABSTRACT_TYPE(Class, Base) |
| 229 | #include "clang/AST/TypeNodes.def" |
| 230 | }; |
| 231 | } |
| 232 | |
| 233 | QualType |
| 234 | TemplateTypeInstantiator::InstantiateExtQualType(const ExtQualType *T, |
| 235 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 236 | // FIXME: Implement this |
| 237 | assert(false && "Cannot instantiate ExtQualType yet"); |
| 238 | return QualType(); |
| 239 | } |
| 240 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 241 | QualType |
| 242 | TemplateTypeInstantiator::InstantiateBuiltinType(const BuiltinType *T, |
| 243 | unsigned Quals) const { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 244 | assert(false && "Builtin types are not dependent and cannot be instantiated"); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 245 | return QualType(T, Quals); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 248 | QualType |
| 249 | TemplateTypeInstantiator:: |
| 250 | InstantiateFixedWidthIntType(const FixedWidthIntType *T, unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 251 | // FIXME: Implement this |
| 252 | assert(false && "Cannot instantiate FixedWidthIntType yet"); |
| 253 | return QualType(); |
| 254 | } |
| 255 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 256 | QualType |
| 257 | TemplateTypeInstantiator::InstantiateComplexType(const ComplexType *T, |
| 258 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 259 | // FIXME: Implement this |
| 260 | assert(false && "Cannot instantiate ComplexType yet"); |
| 261 | return QualType(); |
| 262 | } |
| 263 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 264 | QualType |
| 265 | TemplateTypeInstantiator::InstantiatePointerType(const PointerType *T, |
| 266 | unsigned Quals) const { |
| 267 | QualType PointeeType = Instantiate(T->getPointeeType()); |
| 268 | if (PointeeType.isNull()) |
| 269 | return QualType(); |
| 270 | |
| 271 | return SemaRef.BuildPointerType(PointeeType, Quals, Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 274 | QualType |
| 275 | TemplateTypeInstantiator::InstantiateBlockPointerType(const BlockPointerType *T, |
| 276 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 277 | // FIXME: Implement this |
| 278 | assert(false && "Cannot instantiate BlockPointerType yet"); |
| 279 | return QualType(); |
| 280 | } |
| 281 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 282 | QualType |
| 283 | TemplateTypeInstantiator::InstantiateLValueReferenceType( |
| 284 | const LValueReferenceType *T, unsigned Quals) const { |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 285 | QualType ReferentType = Instantiate(T->getPointeeType()); |
| 286 | if (ReferentType.isNull()) |
| 287 | return QualType(); |
| 288 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 289 | return SemaRef.BuildReferenceType(ReferentType, true, Quals, Loc, Entity); |
| 290 | } |
| 291 | |
| 292 | QualType |
| 293 | TemplateTypeInstantiator::InstantiateRValueReferenceType( |
| 294 | const RValueReferenceType *T, unsigned Quals) const { |
| 295 | QualType ReferentType = Instantiate(T->getPointeeType()); |
| 296 | if (ReferentType.isNull()) |
| 297 | return QualType(); |
| 298 | |
| 299 | return SemaRef.BuildReferenceType(ReferentType, false, Quals, Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 302 | QualType |
| 303 | TemplateTypeInstantiator:: |
| 304 | InstantiateMemberPointerType(const MemberPointerType *T, |
| 305 | unsigned Quals) const { |
Douglas Gregor | 949bf69 | 2009-06-09 22:17:39 +0000 | [diff] [blame] | 306 | QualType PointeeType = Instantiate(T->getPointeeType()); |
| 307 | if (PointeeType.isNull()) |
| 308 | return QualType(); |
| 309 | |
| 310 | QualType ClassType = Instantiate(QualType(T->getClass(), 0)); |
| 311 | if (ClassType.isNull()) |
| 312 | return QualType(); |
| 313 | |
| 314 | return SemaRef.BuildMemberPointerType(PointeeType, ClassType, Quals, Loc, |
| 315 | Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 318 | QualType |
| 319 | TemplateTypeInstantiator:: |
| 320 | InstantiateConstantArrayType(const ConstantArrayType *T, |
| 321 | unsigned Quals) const { |
| 322 | QualType ElementType = Instantiate(T->getElementType()); |
| 323 | if (ElementType.isNull()) |
| 324 | return ElementType; |
| 325 | |
| 326 | // Build a temporary integer literal to specify the size for |
| 327 | // BuildArrayType. Since we have already checked the size as part of |
| 328 | // creating the dependent array type in the first place, we know |
Douglas Gregor | ff66803 | 2009-05-13 18:28:20 +0000 | [diff] [blame] | 329 | // there aren't any errors. However, we do need to determine what |
| 330 | // C++ type to give the size expression. |
| 331 | llvm::APInt Size = T->getSize(); |
| 332 | QualType Types[] = { |
| 333 | SemaRef.Context.UnsignedCharTy, SemaRef.Context.UnsignedShortTy, |
| 334 | SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy, |
| 335 | SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty |
| 336 | }; |
| 337 | const unsigned NumTypes = sizeof(Types) / sizeof(QualType); |
| 338 | QualType SizeType; |
| 339 | for (unsigned I = 0; I != NumTypes; ++I) |
| 340 | if (Size.getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) { |
| 341 | SizeType = Types[I]; |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | if (SizeType.isNull()) |
| 346 | SizeType = SemaRef.Context.getFixedWidthIntType(Size.getBitWidth(), false); |
| 347 | |
| 348 | IntegerLiteral ArraySize(Size, SizeType, Loc); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 349 | return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(), |
| 350 | &ArraySize, T->getIndexTypeQualifier(), |
| 351 | Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 354 | QualType |
| 355 | TemplateTypeInstantiator:: |
| 356 | InstantiateIncompleteArrayType(const IncompleteArrayType *T, |
| 357 | unsigned Quals) const { |
| 358 | QualType ElementType = Instantiate(T->getElementType()); |
| 359 | if (ElementType.isNull()) |
| 360 | return ElementType; |
| 361 | |
| 362 | return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(), |
| 363 | 0, T->getIndexTypeQualifier(), |
| 364 | Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 367 | QualType |
| 368 | TemplateTypeInstantiator:: |
| 369 | InstantiateVariableArrayType(const VariableArrayType *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 VariableArrayType yet"); |
| 373 | return QualType(); |
| 374 | } |
| 375 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 376 | QualType |
| 377 | TemplateTypeInstantiator:: |
| 378 | InstantiateDependentSizedArrayType(const DependentSizedArrayType *T, |
| 379 | unsigned Quals) const { |
Anders Carlsson | 76b1c84 | 2009-03-15 20:12:13 +0000 | [diff] [blame] | 380 | Expr *ArraySize = T->getSizeExpr(); |
| 381 | assert(ArraySize->isValueDependent() && |
| 382 | "dependent sized array types must have value dependent size expr"); |
| 383 | |
| 384 | // Instantiate the element type if needed |
| 385 | QualType ElementType = T->getElementType(); |
| 386 | if (ElementType->isDependentType()) { |
| 387 | ElementType = Instantiate(ElementType); |
| 388 | if (ElementType.isNull()) |
| 389 | return QualType(); |
| 390 | } |
| 391 | |
| 392 | // Instantiate the size expression |
| 393 | Sema::OwningExprResult InstantiatedArraySize = |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 394 | SemaRef.InstantiateExpr(ArraySize, TemplateArgs); |
Anders Carlsson | 76b1c84 | 2009-03-15 20:12:13 +0000 | [diff] [blame] | 395 | if (InstantiatedArraySize.isInvalid()) |
| 396 | return QualType(); |
| 397 | |
| 398 | return SemaRef.BuildArrayType(ElementType, T->getSizeModifier(), |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 399 | InstantiatedArraySize.takeAs<Expr>(), |
Anders Carlsson | 76b1c84 | 2009-03-15 20:12:13 +0000 | [diff] [blame] | 400 | T->getIndexTypeQualifier(), Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 403 | QualType |
| 404 | TemplateTypeInstantiator::InstantiateVectorType(const VectorType *T, |
| 405 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 406 | // FIXME: Implement this |
| 407 | assert(false && "Cannot instantiate VectorType yet"); |
| 408 | return QualType(); |
| 409 | } |
| 410 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 411 | QualType |
| 412 | TemplateTypeInstantiator::InstantiateExtVectorType(const ExtVectorType *T, |
| 413 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 414 | // FIXME: Implement this |
| 415 | assert(false && "Cannot instantiate ExtVectorType yet"); |
| 416 | return QualType(); |
| 417 | } |
| 418 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 419 | QualType |
| 420 | TemplateTypeInstantiator:: |
| 421 | InstantiateFunctionProtoType(const FunctionProtoType *T, |
| 422 | unsigned Quals) const { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 423 | QualType ResultType = Instantiate(T->getResultType()); |
| 424 | if (ResultType.isNull()) |
| 425 | return ResultType; |
| 426 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 427 | llvm::SmallVector<QualType, 4> ParamTypes; |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 428 | for (FunctionProtoType::arg_type_iterator Param = T->arg_type_begin(), |
| 429 | ParamEnd = T->arg_type_end(); |
| 430 | Param != ParamEnd; ++Param) { |
| 431 | QualType P = Instantiate(*Param); |
| 432 | if (P.isNull()) |
| 433 | return P; |
| 434 | |
| 435 | ParamTypes.push_back(P); |
| 436 | } |
| 437 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame^] | 438 | return SemaRef.BuildFunctionType(ResultType, ParamTypes.data(), |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 439 | ParamTypes.size(), |
| 440 | T->isVariadic(), T->getTypeQuals(), |
| 441 | Loc, Entity); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 444 | QualType |
| 445 | TemplateTypeInstantiator:: |
| 446 | InstantiateFunctionNoProtoType(const FunctionNoProtoType *T, |
| 447 | unsigned Quals) const { |
Douglas Gregor | 724651c | 2009-02-28 01:04:19 +0000 | [diff] [blame] | 448 | assert(false && "Functions without prototypes cannot be dependent."); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 449 | return QualType(); |
| 450 | } |
| 451 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 452 | QualType |
| 453 | TemplateTypeInstantiator::InstantiateTypedefType(const TypedefType *T, |
| 454 | unsigned Quals) const { |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 455 | TypedefDecl *Typedef |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 456 | = cast_or_null<TypedefDecl>( |
| 457 | SemaRef.InstantiateCurrentDeclRef(T->getDecl())); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 458 | if (!Typedef) |
| 459 | return QualType(); |
| 460 | |
| 461 | return SemaRef.Context.getTypeDeclType(Typedef); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 464 | QualType |
| 465 | TemplateTypeInstantiator::InstantiateTypeOfExprType(const TypeOfExprType *T, |
| 466 | unsigned Quals) const { |
Douglas Gregor | 5f8bd59 | 2009-05-26 22:09:24 +0000 | [diff] [blame] | 467 | Sema::OwningExprResult E |
| 468 | = SemaRef.InstantiateExpr(T->getUnderlyingExpr(), TemplateArgs); |
| 469 | if (E.isInvalid()) |
| 470 | return QualType(); |
| 471 | |
| 472 | return SemaRef.Context.getTypeOfExprType(E.takeAs<Expr>()); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 475 | QualType |
| 476 | TemplateTypeInstantiator::InstantiateTypeOfType(const TypeOfType *T, |
| 477 | unsigned Quals) const { |
Douglas Gregor | 5f8bd59 | 2009-05-26 22:09:24 +0000 | [diff] [blame] | 478 | QualType Underlying = Instantiate(T->getUnderlyingType()); |
| 479 | if (Underlying.isNull()) |
| 480 | return QualType(); |
| 481 | |
| 482 | return SemaRef.Context.getTypeOfType(Underlying); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 485 | QualType |
| 486 | TemplateTypeInstantiator::InstantiateRecordType(const RecordType *T, |
| 487 | unsigned Quals) const { |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 488 | RecordDecl *Record |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 489 | = cast_or_null<RecordDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl())); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 490 | if (!Record) |
| 491 | return QualType(); |
| 492 | |
| 493 | return SemaRef.Context.getTypeDeclType(Record); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 496 | QualType |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 497 | TemplateTypeInstantiator::InstantiateEnumType(const EnumType *T, |
| 498 | unsigned Quals) const { |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 499 | EnumDecl *Enum |
Douglas Gregor | ed961e7 | 2009-05-27 17:54:46 +0000 | [diff] [blame] | 500 | = cast_or_null<EnumDecl>(SemaRef.InstantiateCurrentDeclRef(T->getDecl())); |
Douglas Gregor | 815215d | 2009-05-27 05:35:12 +0000 | [diff] [blame] | 501 | if (!Enum) |
| 502 | return QualType(); |
| 503 | |
| 504 | return SemaRef.Context.getTypeDeclType(Enum); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 507 | QualType |
| 508 | TemplateTypeInstantiator:: |
| 509 | InstantiateTemplateTypeParmType(const TemplateTypeParmType *T, |
| 510 | unsigned Quals) const { |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 511 | if (T->getDepth() == 0) { |
| 512 | // Replace the template type parameter with its corresponding |
| 513 | // template argument. |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 514 | assert(TemplateArgs[T->getIndex()].getKind() == TemplateArgument::Type && |
| 515 | "Template argument kind mismatch"); |
| 516 | QualType Result = TemplateArgs[T->getIndex()].getAsType(); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 517 | if (Result.isNull() || !Quals) |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 518 | return Result; |
| 519 | |
| 520 | // C++ [dcl.ref]p1: |
| 521 | // [...] Cv-qualified references are ill-formed except when |
| 522 | // the cv-qualifiers are introduced through the use of a |
| 523 | // typedef (7.1.3) or of a template type argument (14.3), in |
| 524 | // which case the cv-qualifiers are ignored. |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 525 | if (Quals && Result->isReferenceType()) |
| 526 | Quals = 0; |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 527 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 528 | return QualType(Result.getTypePtr(), Quals | Result.getCVRQualifiers()); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | // The template type parameter comes from an inner template (e.g., |
| 532 | // the template parameter list of a member template inside the |
| 533 | // template we are instantiating). Create a new template type |
| 534 | // parameter with the template "level" reduced by one. |
| 535 | return SemaRef.Context.getTemplateTypeParmType(T->getDepth() - 1, |
| 536 | T->getIndex(), |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 537 | T->getName()) |
| 538 | .getQualifiedType(Quals); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 541 | QualType |
| 542 | TemplateTypeInstantiator:: |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 543 | InstantiateTemplateSpecializationType( |
| 544 | const TemplateSpecializationType *T, |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 545 | unsigned Quals) const { |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 546 | llvm::SmallVector<TemplateArgument, 4> InstantiatedTemplateArgs; |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 547 | InstantiatedTemplateArgs.reserve(T->getNumArgs()); |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 548 | for (TemplateSpecializationType::iterator Arg = T->begin(), ArgEnd = T->end(); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 549 | Arg != ArgEnd; ++Arg) { |
Douglas Gregor | 9133300 | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 550 | TemplateArgument InstArg = SemaRef.Instantiate(*Arg, TemplateArgs); |
| 551 | if (InstArg.isNull()) |
| 552 | return QualType(); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 553 | |
Douglas Gregor | 9133300 | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 554 | InstantiatedTemplateArgs.push_back(InstArg); |
Douglas Gregor | 40808ce | 2009-03-09 23:48:35 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 557 | // FIXME: We're missing the locations of the template name, '<', and '>'. |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 558 | |
| 559 | TemplateName Name = SemaRef.InstantiateTemplateName(T->getTemplateName(), |
| 560 | Loc, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 561 | TemplateArgs); |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 562 | |
| 563 | return SemaRef.CheckTemplateIdType(Name, Loc, SourceLocation(), |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame^] | 564 | InstantiatedTemplateArgs.data(), |
Douglas Gregor | 7532dc6 | 2009-03-30 22:58:21 +0000 | [diff] [blame] | 565 | InstantiatedTemplateArgs.size(), |
| 566 | SourceLocation()); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 569 | QualType |
| 570 | TemplateTypeInstantiator:: |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 571 | InstantiateQualifiedNameType(const QualifiedNameType *T, |
| 572 | unsigned Quals) const { |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 573 | // When we instantiated a qualified name type, there's no point in |
| 574 | // keeping the qualification around in the instantiated result. So, |
| 575 | // just instantiate the named type. |
| 576 | return (*this)(T->getNamedType()); |
| 577 | } |
| 578 | |
| 579 | QualType |
| 580 | TemplateTypeInstantiator:: |
| 581 | InstantiateTypenameType(const TypenameType *T, unsigned Quals) const { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 582 | if (const TemplateSpecializationType *TemplateId = T->getTemplateId()) { |
| 583 | // When the typename type refers to a template-id, the template-id |
| 584 | // is dependent and has enough information to instantiate the |
| 585 | // result of the typename type. Since we don't care about keeping |
| 586 | // the spelling of the typename type in template instantiations, |
| 587 | // we just instantiate the template-id. |
| 588 | return InstantiateTemplateSpecializationType(TemplateId, Quals); |
| 589 | } |
| 590 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 591 | NestedNameSpecifier *NNS |
| 592 | = SemaRef.InstantiateNestedNameSpecifier(T->getQualifier(), |
| 593 | SourceRange(Loc), |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 594 | TemplateArgs); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 595 | if (!NNS) |
| 596 | return QualType(); |
| 597 | |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 598 | return SemaRef.CheckTypenameType(NNS, *T->getIdentifier(), SourceRange(Loc)); |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | QualType |
| 602 | TemplateTypeInstantiator:: |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 603 | InstantiateObjCInterfaceType(const ObjCInterfaceType *T, |
| 604 | unsigned Quals) const { |
| 605 | assert(false && "Objective-C types cannot be dependent"); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 606 | return QualType(); |
| 607 | } |
| 608 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 609 | QualType |
| 610 | TemplateTypeInstantiator:: |
| 611 | InstantiateObjCQualifiedInterfaceType(const ObjCQualifiedInterfaceType *T, |
| 612 | unsigned Quals) const { |
| 613 | assert(false && "Objective-C types cannot be dependent"); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 614 | return QualType(); |
| 615 | } |
| 616 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 617 | QualType |
| 618 | TemplateTypeInstantiator:: |
| 619 | InstantiateObjCQualifiedIdType(const ObjCQualifiedIdType *T, |
| 620 | unsigned Quals) const { |
| 621 | assert(false && "Objective-C types cannot be dependent"); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 622 | return QualType(); |
| 623 | } |
| 624 | |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 625 | /// \brief The actual implementation of Sema::InstantiateType(). |
| 626 | QualType TemplateTypeInstantiator::Instantiate(QualType T) const { |
| 627 | // If T is not a dependent type, there is nothing to do. |
| 628 | if (!T->isDependentType()) |
| 629 | return T; |
| 630 | |
| 631 | switch (T->getTypeClass()) { |
| 632 | #define TYPE(Class, Base) \ |
| 633 | case Type::Class: \ |
| 634 | return Instantiate##Class##Type(cast<Class##Type>(T.getTypePtr()), \ |
| 635 | T.getCVRQualifiers()); |
| 636 | #define ABSTRACT_TYPE(Class, Base) |
| 637 | #include "clang/AST/TypeNodes.def" |
| 638 | } |
| 639 | |
| 640 | assert(false && "Not all types have been decoded for instantiation"); |
| 641 | return QualType(); |
| 642 | } |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 643 | |
| 644 | /// \brief Instantiate the type T with a given set of template arguments. |
| 645 | /// |
| 646 | /// This routine substitutes the given template arguments into the |
| 647 | /// type T and produces the instantiated type. |
| 648 | /// |
| 649 | /// \param T the type into which the template arguments will be |
| 650 | /// substituted. If this type is not dependent, it will be returned |
| 651 | /// immediately. |
| 652 | /// |
| 653 | /// \param TemplateArgs the template arguments that will be |
| 654 | /// substituted for the top-level template parameters within T. |
| 655 | /// |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 656 | /// \param Loc the location in the source code where this substitution |
| 657 | /// is being performed. It will typically be the location of the |
| 658 | /// declarator (if we're instantiating the type of some declaration) |
| 659 | /// or the location of the type in the source code (if, e.g., we're |
| 660 | /// instantiating the type of a cast expression). |
| 661 | /// |
| 662 | /// \param Entity the name of the entity associated with a declaration |
| 663 | /// being instantiated (if any). May be empty to indicate that there |
| 664 | /// is no such entity (if, e.g., this is a type that occurs as part of |
| 665 | /// a cast expression) or that the entity has no name (e.g., an |
| 666 | /// unnamed function parameter). |
| 667 | /// |
| 668 | /// \returns If the instantiation succeeds, the instantiated |
| 669 | /// type. Otherwise, produces diagnostics and returns a NULL type. |
| 670 | QualType Sema::InstantiateType(QualType T, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 671 | const TemplateArgumentList &TemplateArgs, |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 672 | SourceLocation Loc, DeclarationName Entity) { |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 673 | assert(!ActiveTemplateInstantiations.empty() && |
| 674 | "Cannot perform an instantiation without some context on the " |
| 675 | "instantiation stack"); |
| 676 | |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 677 | // If T is not a dependent type, there is nothing to do. |
| 678 | if (!T->isDependentType()) |
| 679 | return T; |
| 680 | |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 681 | TemplateTypeInstantiator Instantiator(*this, TemplateArgs, Loc, Entity); |
Douglas Gregor | cd281c3 | 2009-02-28 00:25:32 +0000 | [diff] [blame] | 682 | return Instantiator(T); |
Douglas Gregor | 99ebf65 | 2009-02-27 19:31:52 +0000 | [diff] [blame] | 683 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 684 | |
| 685 | /// \brief Instantiate the base class specifiers of the given class |
| 686 | /// template specialization. |
| 687 | /// |
| 688 | /// Produces a diagnostic and returns true on error, returns false and |
| 689 | /// attaches the instantiated base classes to the class template |
| 690 | /// specialization if successful. |
| 691 | bool |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 692 | Sema::InstantiateBaseSpecifiers(CXXRecordDecl *Instantiation, |
| 693 | CXXRecordDecl *Pattern, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 694 | const TemplateArgumentList &TemplateArgs) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 695 | bool Invalid = false; |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 696 | llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases; |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 697 | for (ClassTemplateSpecializationDecl::base_class_iterator |
| 698 | Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end(); |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 699 | Base != BaseEnd; ++Base) { |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 700 | if (!Base->getType()->isDependentType()) { |
| 701 | // FIXME: Allocate via ASTContext |
| 702 | InstantiatedBases.push_back(new CXXBaseSpecifier(*Base)); |
| 703 | continue; |
| 704 | } |
| 705 | |
| 706 | QualType BaseType = InstantiateType(Base->getType(), |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 707 | TemplateArgs, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 708 | Base->getSourceRange().getBegin(), |
| 709 | DeclarationName()); |
| 710 | if (BaseType.isNull()) { |
| 711 | Invalid = true; |
| 712 | continue; |
| 713 | } |
| 714 | |
| 715 | if (CXXBaseSpecifier *InstantiatedBase |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 716 | = CheckBaseSpecifier(Instantiation, |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 717 | Base->getSourceRange(), |
| 718 | Base->isVirtual(), |
| 719 | Base->getAccessSpecifierAsWritten(), |
| 720 | BaseType, |
| 721 | /*FIXME: Not totally accurate */ |
| 722 | Base->getSourceRange().getBegin())) |
| 723 | InstantiatedBases.push_back(InstantiatedBase); |
| 724 | else |
| 725 | Invalid = true; |
| 726 | } |
| 727 | |
Douglas Gregor | 27b152f | 2009-03-10 18:52:44 +0000 | [diff] [blame] | 728 | if (!Invalid && |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 729 | AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(), |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 730 | InstantiatedBases.size())) |
| 731 | Invalid = true; |
| 732 | |
| 733 | return Invalid; |
| 734 | } |
| 735 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 736 | /// \brief Instantiate the definition of a class from a given pattern. |
| 737 | /// |
| 738 | /// \param PointOfInstantiation The point of instantiation within the |
| 739 | /// source code. |
| 740 | /// |
| 741 | /// \param Instantiation is the declaration whose definition is being |
| 742 | /// instantiated. This will be either a class template specialization |
| 743 | /// or a member class of a class template specialization. |
| 744 | /// |
| 745 | /// \param Pattern is the pattern from which the instantiation |
| 746 | /// occurs. This will be either the declaration of a class template or |
| 747 | /// the declaration of a member class of a class template. |
| 748 | /// |
| 749 | /// \param TemplateArgs The template arguments to be substituted into |
| 750 | /// the pattern. |
| 751 | /// |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 752 | /// \returns true if an error occurred, false otherwise. |
| 753 | bool |
| 754 | Sema::InstantiateClass(SourceLocation PointOfInstantiation, |
| 755 | CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern, |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 756 | const TemplateArgumentList &TemplateArgs, |
| 757 | bool ExplicitInstantiation) { |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 758 | bool Invalid = false; |
| 759 | |
| 760 | CXXRecordDecl *PatternDef |
| 761 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition(Context)); |
| 762 | if (!PatternDef) { |
| 763 | if (Pattern == Instantiation->getInstantiatedFromMemberClass()) { |
| 764 | Diag(PointOfInstantiation, |
| 765 | diag::err_implicit_instantiate_member_undefined) |
| 766 | << Context.getTypeDeclType(Instantiation); |
| 767 | Diag(Pattern->getLocation(), diag::note_member_of_template_here); |
| 768 | } else { |
Douglas Gregor | 93dfdb1 | 2009-05-13 00:25:59 +0000 | [diff] [blame] | 769 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
| 770 | << ExplicitInstantiation |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 771 | << Context.getTypeDeclType(Instantiation); |
| 772 | Diag(Pattern->getLocation(), diag::note_template_decl_here); |
| 773 | } |
| 774 | return true; |
| 775 | } |
| 776 | Pattern = PatternDef; |
| 777 | |
Douglas Gregor | d048bb7 | 2009-03-25 21:23:52 +0000 | [diff] [blame] | 778 | InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 779 | if (Inst) |
| 780 | return true; |
| 781 | |
| 782 | // Enter the scope of this instantiation. We don't use |
| 783 | // PushDeclContext because we don't have a scope. |
| 784 | DeclContext *PreviousContext = CurContext; |
| 785 | CurContext = Instantiation; |
| 786 | |
| 787 | // Start the definition of this instantiation. |
| 788 | Instantiation->startDefinition(); |
| 789 | |
| 790 | // Instantiate the base class specifiers. |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 791 | if (InstantiateBaseSpecifiers(Instantiation, Pattern, TemplateArgs)) |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 792 | Invalid = true; |
| 793 | |
Douglas Gregor | 0ca20ac | 2009-05-29 18:27:38 +0000 | [diff] [blame] | 794 | llvm::SmallVector<DeclPtrTy, 4> Fields; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 795 | for (RecordDecl::decl_iterator Member = Pattern->decls_begin(Context), |
| 796 | MemberEnd = Pattern->decls_end(Context); |
| 797 | Member != MemberEnd; ++Member) { |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 798 | Decl *NewMember = InstantiateDecl(*Member, Instantiation, TemplateArgs); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 799 | if (NewMember) { |
| 800 | if (NewMember->isInvalidDecl()) |
| 801 | Invalid = true; |
| 802 | else if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 803 | Fields.push_back(DeclPtrTy::make(Field)); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 804 | } else { |
| 805 | // FIXME: Eventually, a NULL return will mean that one of the |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 806 | // instantiations was a semantic disaster, and we'll want to set Invalid = |
| 807 | // true. For now, we expect to skip some members that we can't yet handle. |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | |
| 811 | // Finish checking fields. |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 812 | ActOnFields(0, Instantiation->getLocation(), DeclPtrTy::make(Instantiation), |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 813 | Fields.data(), Fields.size(), SourceLocation(), SourceLocation(), |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 814 | 0); |
| 815 | |
| 816 | // Add any implicitly-declared members that we might need. |
| 817 | AddImplicitlyDeclaredMembersToClass(Instantiation); |
| 818 | |
| 819 | // Exit the scope of this instantiation. |
| 820 | CurContext = PreviousContext; |
| 821 | |
Douglas Gregor | aba43bb | 2009-05-26 20:50:29 +0000 | [diff] [blame] | 822 | if (!Invalid) |
| 823 | Consumer.HandleTagDeclDefinition(Instantiation); |
| 824 | |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 825 | // If this is an explicit instantiation, instantiate our members, too. |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 826 | if (!Invalid && ExplicitInstantiation) { |
| 827 | Inst.Clear(); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 828 | InstantiateClassMembers(PointOfInstantiation, Instantiation, TemplateArgs); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 829 | } |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 830 | |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 831 | return Invalid; |
| 832 | } |
| 833 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 834 | bool |
| 835 | Sema::InstantiateClassTemplateSpecialization( |
| 836 | ClassTemplateSpecializationDecl *ClassTemplateSpec, |
| 837 | bool ExplicitInstantiation) { |
| 838 | // Perform the actual instantiation on the canonical declaration. |
| 839 | ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( |
| 840 | Context.getCanonicalDecl(ClassTemplateSpec)); |
| 841 | |
| 842 | // We can only instantiate something that hasn't already been |
| 843 | // instantiated or specialized. Fail without any diagnostics: our |
| 844 | // caller will provide an error message. |
| 845 | if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) |
| 846 | return true; |
| 847 | |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 848 | ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 849 | CXXRecordDecl *Pattern = Template->getTemplatedDecl(); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 850 | const TemplateArgumentList *TemplateArgs |
| 851 | = &ClassTemplateSpec->getTemplateArgs(); |
| 852 | |
| 853 | // Determine whether any class template partial specializations |
| 854 | // match the given template arguments. |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 855 | typedef std::pair<ClassTemplatePartialSpecializationDecl *, |
| 856 | TemplateArgumentList *> MatchResult; |
| 857 | llvm::SmallVector<MatchResult, 4> Matched; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 858 | for (llvm::FoldingSet<ClassTemplatePartialSpecializationDecl>::iterator |
| 859 | Partial = Template->getPartialSpecializations().begin(), |
| 860 | PartialEnd = Template->getPartialSpecializations().end(); |
| 861 | Partial != PartialEnd; |
| 862 | ++Partial) { |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 863 | if (TemplateArgumentList *Deduced |
| 864 | = DeduceTemplateArguments(&*Partial, |
| 865 | ClassTemplateSpec->getTemplateArgs())) |
| 866 | Matched.push_back(std::make_pair(&*Partial, Deduced)); |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | if (Matched.size() == 1) { |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 870 | Pattern = Matched[0].first; |
| 871 | TemplateArgs = Matched[0].second; |
Douglas Gregor | c8ab256 | 2009-05-31 09:31:02 +0000 | [diff] [blame] | 872 | } else if (Matched.size() > 1) { |
| 873 | // FIXME: Implement partial ordering of class template partial |
| 874 | // specializations. |
| 875 | Diag(ClassTemplateSpec->getLocation(), |
| 876 | diag::unsup_template_partial_spec_ordering); |
| 877 | } |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 878 | |
| 879 | // Note that this is an instantiation. |
| 880 | ClassTemplateSpec->setSpecializationKind( |
| 881 | ExplicitInstantiation? TSK_ExplicitInstantiation |
| 882 | : TSK_ImplicitInstantiation); |
| 883 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 884 | bool Result = InstantiateClass(ClassTemplateSpec->getLocation(), |
| 885 | ClassTemplateSpec, Pattern, *TemplateArgs, |
| 886 | ExplicitInstantiation); |
| 887 | |
| 888 | for (unsigned I = 0, N = Matched.size(); I != N; ++I) { |
| 889 | // FIXME: Implement TemplateArgumentList::Destroy! |
| 890 | // if (Matched[I].first != Pattern) |
| 891 | // Matched[I].second->Destroy(Context); |
| 892 | } |
| 893 | |
| 894 | return Result; |
Douglas Gregor | 2943aed | 2009-03-03 04:44:36 +0000 | [diff] [blame] | 895 | } |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 896 | |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 897 | /// \brief Instantiate the definitions of all of the member of the |
| 898 | /// given class, which is an instantiation of a class template or a |
| 899 | /// member class of a template. |
| 900 | void |
| 901 | Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation, |
| 902 | CXXRecordDecl *Instantiation, |
| 903 | const TemplateArgumentList &TemplateArgs) { |
| 904 | for (DeclContext::decl_iterator D = Instantiation->decls_begin(Context), |
| 905 | DEnd = Instantiation->decls_end(Context); |
| 906 | D != DEnd; ++D) { |
| 907 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) { |
| 908 | if (!Function->getBody(Context)) |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 909 | InstantiateFunctionDefinition(PointOfInstantiation, Function); |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 910 | } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) { |
| 911 | const VarDecl *Def = 0; |
| 912 | if (!Var->getDefinition(Def)) |
| 913 | InstantiateVariableDefinition(Var); |
| 914 | } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) { |
| 915 | if (!Record->isInjectedClassName() && !Record->getDefinition(Context)) { |
| 916 | assert(Record->getInstantiatedFromMemberClass() && |
| 917 | "Missing instantiated-from-template information"); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 918 | InstantiateClass(PointOfInstantiation, Record, |
Douglas Gregor | a58861f | 2009-05-13 20:28:22 +0000 | [diff] [blame] | 919 | Record->getInstantiatedFromMemberClass(), |
| 920 | TemplateArgs, true); |
| 921 | } |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | /// \brief Instantiate the definitions of all of the members of the |
| 927 | /// given class template specialization, which was named as part of an |
| 928 | /// explicit instantiation. |
| 929 | void Sema::InstantiateClassTemplateSpecializationMembers( |
| 930 | SourceLocation PointOfInstantiation, |
| 931 | ClassTemplateSpecializationDecl *ClassTemplateSpec) { |
| 932 | // C++0x [temp.explicit]p7: |
| 933 | // An explicit instantiation that names a class template |
| 934 | // specialization is an explicit instantion of the same kind |
| 935 | // (declaration or definition) of each of its members (not |
| 936 | // including members inherited from base classes) that has not |
| 937 | // been previously explicitly specialized in the translation unit |
| 938 | // containing the explicit instantiation, except as described |
| 939 | // below. |
| 940 | InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec, |
| 941 | ClassTemplateSpec->getTemplateArgs()); |
| 942 | } |
| 943 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 944 | /// \brief Instantiate a nested-name-specifier. |
| 945 | NestedNameSpecifier * |
| 946 | Sema::InstantiateNestedNameSpecifier(NestedNameSpecifier *NNS, |
| 947 | SourceRange Range, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 948 | const TemplateArgumentList &TemplateArgs) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 949 | // Instantiate the prefix of this nested name specifier. |
| 950 | NestedNameSpecifier *Prefix = NNS->getPrefix(); |
| 951 | if (Prefix) { |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 952 | Prefix = InstantiateNestedNameSpecifier(Prefix, Range, TemplateArgs); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 953 | if (!Prefix) |
| 954 | return 0; |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 957 | switch (NNS->getKind()) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 958 | case NestedNameSpecifier::Identifier: { |
| 959 | assert(Prefix && |
| 960 | "Can't have an identifier nested-name-specifier with no prefix"); |
| 961 | CXXScopeSpec SS; |
| 962 | // FIXME: The source location information is all wrong. |
| 963 | SS.setRange(Range); |
| 964 | SS.setScopeRep(Prefix); |
| 965 | return static_cast<NestedNameSpecifier *>( |
| 966 | ActOnCXXNestedNameSpecifier(0, SS, |
| 967 | Range.getEnd(), |
| 968 | Range.getEnd(), |
Douglas Gregor | 3f5b61c | 2009-05-14 00:28:11 +0000 | [diff] [blame] | 969 | *NNS->getAsIdentifier())); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 970 | break; |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 971 | } |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 972 | |
| 973 | case NestedNameSpecifier::Namespace: |
| 974 | case NestedNameSpecifier::Global: |
| 975 | return NNS; |
| 976 | |
| 977 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 978 | case NestedNameSpecifier::TypeSpec: { |
| 979 | QualType T = QualType(NNS->getAsType(), 0); |
| 980 | if (!T->isDependentType()) |
| 981 | return NNS; |
| 982 | |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 983 | T = InstantiateType(T, TemplateArgs, Range.getBegin(), DeclarationName()); |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 984 | if (T.isNull()) |
| 985 | return 0; |
| 986 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 987 | if (T->isRecordType() || |
| 988 | (getLangOptions().CPlusPlus0x && T->isEnumeralType())) { |
Douglas Gregor | 1734317 | 2009-04-01 00:28:59 +0000 | [diff] [blame] | 989 | assert(T.getCVRQualifiers() == 0 && "Can't get cv-qualifiers here"); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 990 | return NestedNameSpecifier::Create(Context, Prefix, |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 991 | NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate, |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 992 | T.getTypePtr()); |
| 993 | } |
| 994 | |
| 995 | Diag(Range.getBegin(), diag::err_nested_name_spec_non_tag) << T; |
| 996 | return 0; |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 1000 | // Required to silence a GCC warning |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 1001 | return 0; |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1002 | } |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1003 | |
| 1004 | TemplateName |
| 1005 | Sema::InstantiateTemplateName(TemplateName Name, SourceLocation Loc, |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1006 | const TemplateArgumentList &TemplateArgs) { |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1007 | if (TemplateTemplateParmDecl *TTP |
| 1008 | = dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 1009 | Name.getAsTemplateDecl())) { |
| 1010 | assert(TTP->getDepth() == 0 && |
| 1011 | "Cannot reduce depth of a template template parameter"); |
Douglas Gregor | 9bde773 | 2009-03-31 20:22:05 +0000 | [diff] [blame] | 1012 | assert(TemplateArgs[TTP->getPosition()].getAsDecl() && |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1013 | "Wrong kind of template template argument"); |
| 1014 | ClassTemplateDecl *ClassTemplate |
| 1015 | = dyn_cast<ClassTemplateDecl>( |
| 1016 | TemplateArgs[TTP->getPosition()].getAsDecl()); |
Douglas Gregor | 9bde773 | 2009-03-31 20:22:05 +0000 | [diff] [blame] | 1017 | assert(ClassTemplate && "Expected a class template"); |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1018 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) { |
| 1019 | NestedNameSpecifier *NNS |
| 1020 | = InstantiateNestedNameSpecifier(QTN->getQualifier(), |
| 1021 | /*FIXME=*/SourceRange(Loc), |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1022 | TemplateArgs); |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1023 | if (NNS) |
| 1024 | return Context.getQualifiedTemplateName(NNS, |
| 1025 | QTN->hasTemplateKeyword(), |
| 1026 | ClassTemplate); |
| 1027 | } |
| 1028 | |
| 1029 | return TemplateName(ClassTemplate); |
| 1030 | } else if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) { |
| 1031 | NestedNameSpecifier *NNS |
| 1032 | = InstantiateNestedNameSpecifier(DTN->getQualifier(), |
| 1033 | /*FIXME=*/SourceRange(Loc), |
Douglas Gregor | 7e06390 | 2009-05-11 23:53:27 +0000 | [diff] [blame] | 1034 | TemplateArgs); |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1035 | |
| 1036 | if (!NNS) // FIXME: Not the best recovery strategy. |
| 1037 | return Name; |
| 1038 | |
| 1039 | if (NNS->isDependent()) |
| 1040 | return Context.getDependentTemplateName(NNS, DTN->getName()); |
| 1041 | |
| 1042 | // Somewhat redundant with ActOnDependentTemplateName. |
| 1043 | CXXScopeSpec SS; |
| 1044 | SS.setRange(SourceRange(Loc)); |
| 1045 | SS.setScopeRep(NNS); |
| 1046 | TemplateTy Template; |
| 1047 | TemplateNameKind TNK = isTemplateName(*DTN->getName(), 0, Template, &SS); |
| 1048 | if (TNK == TNK_Non_template) { |
| 1049 | Diag(Loc, diag::err_template_kw_refers_to_non_template) |
| 1050 | << DTN->getName(); |
| 1051 | return Name; |
| 1052 | } else if (TNK == TNK_Function_template) { |
| 1053 | Diag(Loc, diag::err_template_kw_refers_to_non_template) |
| 1054 | << DTN->getName(); |
| 1055 | return Name; |
| 1056 | } |
| 1057 | |
| 1058 | return Template.getAsVal<TemplateName>(); |
| 1059 | } |
| 1060 | |
| 1061 | |
| 1062 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1063 | // FIXME: Even if we're referring to a Decl that isn't a template template |
| 1064 | // parameter, we may need to instantiate the outer contexts of that |
| 1065 | // Decl. However, this won't be needed until we implement member templates. |
Douglas Gregor | de650ae | 2009-03-31 18:38:02 +0000 | [diff] [blame] | 1066 | return Name; |
| 1067 | } |
Douglas Gregor | 9133300 | 2009-06-11 00:06:24 +0000 | [diff] [blame] | 1068 | |
| 1069 | TemplateArgument Sema::Instantiate(TemplateArgument Arg, |
| 1070 | const TemplateArgumentList &TemplateArgs) { |
| 1071 | switch (Arg.getKind()) { |
| 1072 | case TemplateArgument::Null: |
| 1073 | assert(false && "Should never have a NULL template argument"); |
| 1074 | break; |
| 1075 | |
| 1076 | case TemplateArgument::Type: { |
| 1077 | QualType T = InstantiateType(Arg.getAsType(), TemplateArgs, |
| 1078 | Arg.getLocation(), DeclarationName()); |
| 1079 | if (T.isNull()) |
| 1080 | return TemplateArgument(); |
| 1081 | |
| 1082 | return TemplateArgument(Arg.getLocation(), T); |
| 1083 | } |
| 1084 | |
| 1085 | case TemplateArgument::Declaration: |
| 1086 | // FIXME: Template instantiation for template template parameters. |
| 1087 | return Arg; |
| 1088 | |
| 1089 | case TemplateArgument::Integral: |
| 1090 | return Arg; |
| 1091 | |
| 1092 | case TemplateArgument::Expression: { |
| 1093 | Sema::OwningExprResult E = InstantiateExpr(Arg.getAsExpr(), TemplateArgs); |
| 1094 | if (E.isInvalid()) |
| 1095 | return TemplateArgument(); |
| 1096 | return TemplateArgument(E.takeAs<Expr>()); |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | assert(false && "Unhandled template argument kind"); |
| 1101 | return TemplateArgument(); |
| 1102 | } |