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