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