| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | #include "TreeTransform.h" |
| 12 | #include "clang/AST/ASTConsumer.h" |
| 13 | #include "clang/AST/ASTContext.h" |
| 14 | #include "clang/AST/DeclFriend.h" |
| 15 | #include "clang/AST/DeclTemplate.h" |
| 16 | #include "clang/AST/Expr.h" |
| 17 | #include "clang/AST/ExprCXX.h" |
| 18 | #include "clang/AST/RecursiveASTVisitor.h" |
| 19 | #include "clang/AST/TypeVisitor.h" |
| 20 | #include "clang/Basic/Builtins.h" |
| 21 | #include "clang/Basic/LangOptions.h" |
| 22 | #include "clang/Basic/PartialDiagnostic.h" |
| 23 | #include "clang/Basic/TargetInfo.h" |
| 24 | #include "clang/Sema/DeclSpec.h" |
| 25 | #include "clang/Sema/Lookup.h" |
| 26 | #include "clang/Sema/ParsedTemplate.h" |
| 27 | #include "clang/Sema/Scope.h" |
| 28 | #include "clang/Sema/SemaInternal.h" |
| 29 | #include "clang/Sema/Template.h" |
| 30 | #include "clang/Sema/TemplateDeduction.h" |
| 31 | #include "llvm/ADT/SmallBitVector.h" |
| 32 | #include "llvm/ADT/SmallString.h" |
| 33 | #include "llvm/ADT/StringExtras.h" |
| 34 | |
| 35 | #include <iterator> |
| 36 | using namespace clang; |
| 37 | using namespace sema; |
| 38 | |
| 39 | |
| 40 | SourceRange |
| 41 | clang::getTemplateParamsRange(TemplateParameterList const * const *Ps, |
| 42 | unsigned N) { |
| 43 | if (!N) return SourceRange(); |
| 44 | return SourceRange(Ps[0]->getTemplateLoc(), Ps[N-1]->getRAngleLoc()); |
| 45 | } |
| 46 | |
| 47 | namespace clang { |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | static Expr *formAssociatedConstraints(TemplateParameterList *Params, |
| 57 | FunctionDecl *FD); |
| 58 | } |
| 59 | |
| 60 | static Expr *clang::formAssociatedConstraints(TemplateParameterList *Params, |
| 61 | FunctionDecl *FD) { |
| 62 | |
| 63 | (0) . __assert_fail ("!FD && \"Cannot collect constraints from function declaration yet.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 63, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!FD && "Cannot collect constraints from function declaration yet."); |
| 64 | return Params->getRequiresClause(); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | NamedDecl *Sema::getAsTemplateNameDecl(NamedDecl *D, |
| 74 | bool AllowFunctionTemplates, |
| 75 | bool AllowDependent) { |
| 76 | D = D->getUnderlyingDecl(); |
| 77 | |
| 78 | if (isa<TemplateDecl>(D)) { |
| 79 | if (!AllowFunctionTemplates && isa<FunctionTemplateDecl>(D)) |
| 80 | return nullptr; |
| 81 | |
| 82 | return D; |
| 83 | } |
| 84 | |
| 85 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | if (Record->isInjectedClassName()) { |
| 98 | Record = cast<CXXRecordDecl>(Record->getDeclContext()); |
| 99 | if (Record->getDescribedClassTemplate()) |
| 100 | return Record->getDescribedClassTemplate(); |
| 101 | |
| 102 | if (ClassTemplateSpecializationDecl *Spec |
| 103 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) |
| 104 | return Spec->getSpecializedTemplate(); |
| 105 | } |
| 106 | |
| 107 | return nullptr; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | if (AllowDependent && isa<UnresolvedUsingValueDecl>(D)) |
| 114 | return D; |
| 115 | |
| 116 | return nullptr; |
| 117 | } |
| 118 | |
| 119 | void Sema::FilterAcceptableTemplateNames(LookupResult &R, |
| 120 | bool AllowFunctionTemplates, |
| 121 | bool AllowDependent) { |
| 122 | LookupResult::Filter filter = R.makeFilter(); |
| 123 | while (filter.hasNext()) { |
| 124 | NamedDecl *Orig = filter.next(); |
| 125 | if (!getAsTemplateNameDecl(Orig, AllowFunctionTemplates, AllowDependent)) |
| 126 | filter.erase(); |
| 127 | } |
| 128 | filter.done(); |
| 129 | } |
| 130 | |
| 131 | bool Sema::hasAnyAcceptableTemplateNames(LookupResult &R, |
| 132 | bool AllowFunctionTemplates, |
| 133 | bool AllowDependent) { |
| 134 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); I != IEnd; ++I) |
| 135 | if (getAsTemplateNameDecl(*I, AllowFunctionTemplates, AllowDependent)) |
| 136 | return true; |
| 137 | |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | TemplateNameKind Sema::isTemplateName(Scope *S, |
| 142 | CXXScopeSpec &SS, |
| 143 | bool hasTemplateKeyword, |
| 144 | const UnqualifiedId &Name, |
| 145 | ParsedType ObjectTypePtr, |
| 146 | bool EnteringContext, |
| 147 | TemplateTy &TemplateResult, |
| 148 | bool &MemberOfUnknownSpecialization) { |
| 149 | (0) . __assert_fail ("getLangOpts().CPlusPlus && \"No template names in C!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 149, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(getLangOpts().CPlusPlus && "No template names in C!"); |
| 150 | |
| 151 | DeclarationName TName; |
| 152 | MemberOfUnknownSpecialization = false; |
| 153 | |
| 154 | switch (Name.getKind()) { |
| 155 | case UnqualifiedIdKind::IK_Identifier: |
| 156 | TName = DeclarationName(Name.Identifier); |
| 157 | break; |
| 158 | |
| 159 | case UnqualifiedIdKind::IK_OperatorFunctionId: |
| 160 | TName = Context.DeclarationNames.getCXXOperatorName( |
| 161 | Name.OperatorFunctionId.Operator); |
| 162 | break; |
| 163 | |
| 164 | case UnqualifiedIdKind::IK_LiteralOperatorId: |
| 165 | TName = Context.DeclarationNames.getCXXLiteralOperatorName(Name.Identifier); |
| 166 | break; |
| 167 | |
| 168 | default: |
| 169 | return TNK_Non_template; |
| 170 | } |
| 171 | |
| 172 | QualType ObjectType = ObjectTypePtr.get(); |
| 173 | |
| 174 | LookupResult R(*this, TName, Name.getBeginLoc(), LookupOrdinaryName); |
| 175 | if (LookupTemplateName(R, S, SS, ObjectType, EnteringContext, |
| 176 | MemberOfUnknownSpecialization)) |
| 177 | return TNK_Non_template; |
| 178 | if (R.empty()) return TNK_Non_template; |
| 179 | |
| 180 | NamedDecl *D = nullptr; |
| 181 | if (R.isAmbiguous()) { |
| 182 | |
| 183 | |
| 184 | bool AnyFunctionTemplates = false; |
| 185 | for (NamedDecl *FoundD : R) { |
| 186 | if (NamedDecl *FoundTemplate = getAsTemplateNameDecl(FoundD)) { |
| 187 | if (isa<FunctionTemplateDecl>(FoundTemplate)) |
| 188 | AnyFunctionTemplates = true; |
| 189 | else { |
| 190 | D = FoundTemplate; |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
| 198 | if (!D && !AnyFunctionTemplates) { |
| 199 | R.suppressDiagnostics(); |
| 200 | return TNK_Non_template; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | |
| 205 | if (!D) |
| 206 | FilterAcceptableTemplateNames(R); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | TemplateName Template; |
| 214 | TemplateNameKind TemplateKind; |
| 215 | |
| 216 | unsigned ResultCount = R.end() - R.begin(); |
| 217 | if (!D && ResultCount > 1) { |
| 218 | |
| 219 | |
| 220 | Template = Context.getOverloadedTemplateName(R.begin(), R.end()); |
| 221 | TemplateKind = TNK_Function_template; |
| 222 | |
| 223 | |
| 224 | R.suppressDiagnostics(); |
| 225 | } else { |
| 226 | if (!D) { |
| 227 | D = getAsTemplateNameDecl(*R.begin()); |
| 228 | (0) . __assert_fail ("D && \"unambiguous result is not a template name\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 228, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(D && "unambiguous result is not a template name"); |
| 229 | } |
| 230 | |
| 231 | if (isa<UnresolvedUsingValueDecl>(D)) { |
| 232 | |
| 233 | MemberOfUnknownSpecialization = true; |
| 234 | return TNK_Non_template; |
| 235 | } |
| 236 | |
| 237 | TemplateDecl *TD = cast<TemplateDecl>(D); |
| 238 | |
| 239 | if (SS.isSet() && !SS.isInvalid()) { |
| 240 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
| 241 | Template = Context.getQualifiedTemplateName(Qualifier, |
| 242 | hasTemplateKeyword, TD); |
| 243 | } else { |
| 244 | Template = TemplateName(TD); |
| 245 | } |
| 246 | |
| 247 | if (isa<FunctionTemplateDecl>(TD)) { |
| 248 | TemplateKind = TNK_Function_template; |
| 249 | |
| 250 | |
| 251 | R.suppressDiagnostics(); |
| 252 | } else { |
| 253 | (TD) || isa(TD) || isa(TD) || isa(TD) || isa(TD)", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 255, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isa<ClassTemplateDecl>(TD) || isa<TemplateTemplateParmDecl>(TD) || |
| 254 | (TD) || isa(TD) || isa(TD) || isa(TD) || isa(TD)", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 255, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> isa<TypeAliasTemplateDecl>(TD) || isa<VarTemplateDecl>(TD) || |
| 255 | (TD) || isa(TD) || isa(TD) || isa(TD) || isa(TD)", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 255, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> isa<BuiltinTemplateDecl>(TD)); |
| 256 | TemplateKind = |
| 257 | isa<VarTemplateDecl>(TD) ? TNK_Var_template : TNK_Type_template; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | TemplateResult = TemplateTy::make(Template); |
| 262 | return TemplateKind; |
| 263 | } |
| 264 | |
| 265 | bool Sema::isDeductionGuideName(Scope *S, const IdentifierInfo &Name, |
| 266 | SourceLocation NameLoc, |
| 267 | ParsedTemplateTy *Template) { |
| 268 | CXXScopeSpec SS; |
| 269 | bool MemberOfUnknownSpecialization = false; |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | LookupResult R(*this, DeclarationName(&Name), NameLoc, LookupOrdinaryName); |
| 275 | if (LookupTemplateName(R, S, SS, QualType(), |
| 276 | false, |
| 277 | MemberOfUnknownSpecialization)) |
| 278 | return false; |
| 279 | |
| 280 | if (R.empty()) return false; |
| 281 | if (R.isAmbiguous()) { |
| 282 | |
| 283 | R.suppressDiagnostics(); |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | |
| 288 | |
| 289 | TemplateDecl *TD = R.getAsSingle<TemplateDecl>(); |
| 290 | if (!TD || !getAsTypeTemplateDecl(TD)) |
| 291 | return false; |
| 292 | |
| 293 | if (Template) |
| 294 | *Template = TemplateTy::make(TemplateName(TD)); |
| 295 | return true; |
| 296 | } |
| 297 | |
| 298 | bool Sema::DiagnoseUnknownTemplateName(const IdentifierInfo &II, |
| 299 | SourceLocation IILoc, |
| 300 | Scope *S, |
| 301 | const CXXScopeSpec *SS, |
| 302 | TemplateTy &SuggestedTemplate, |
| 303 | TemplateNameKind &SuggestedKind) { |
| 304 | |
| 305 | |
| 306 | |
| 307 | if (!SS || !SS->isSet() || !isDependentScopeSpecifier(*SS) || |
| 308 | computeDeclContext(*SS)) |
| 309 | return false; |
| 310 | |
| 311 | |
| 312 | |
| 313 | NestedNameSpecifier *Qualifier = (NestedNameSpecifier*)SS->getScopeRep(); |
| 314 | Diag(IILoc, diag::err_template_kw_missing) |
| 315 | << Qualifier << II.getName() |
| 316 | << FixItHint::CreateInsertion(IILoc, "template "); |
| 317 | SuggestedTemplate |
| 318 | = TemplateTy::make(Context.getDependentTemplateName(Qualifier, &II)); |
| 319 | SuggestedKind = TNK_Dependent_template_name; |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | bool Sema::LookupTemplateName(LookupResult &Found, |
| 324 | Scope *S, CXXScopeSpec &SS, |
| 325 | QualType ObjectType, |
| 326 | bool EnteringContext, |
| 327 | bool &MemberOfUnknownSpecialization, |
| 328 | SourceLocation TemplateKWLoc) { |
| 329 | Found.setTemplateNameLookup(true); |
| 330 | |
| 331 | |
| 332 | MemberOfUnknownSpecialization = false; |
| 333 | DeclContext *LookupCtx = nullptr; |
| 334 | bool IsDependent = false; |
| 335 | if (!ObjectType.isNull()) { |
| 336 | |
| 337 | |
| 338 | (0) . __assert_fail ("!SS.isSet() && \"ObjectType and scope specifier cannot coexist\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 338, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!SS.isSet() && "ObjectType and scope specifier cannot coexist"); |
| 339 | LookupCtx = computeDeclContext(ObjectType); |
| 340 | IsDependent = !LookupCtx; |
| 341 | (0) . __assert_fail ("(IsDependent || !ObjectType->isIncompleteType() || ObjectType->castAs()->isBeingDefined()) && \"Caller should have completed object type\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 343, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((IsDependent || !ObjectType->isIncompleteType() || |
| 342 | (0) . __assert_fail ("(IsDependent || !ObjectType->isIncompleteType() || ObjectType->castAs()->isBeingDefined()) && \"Caller should have completed object type\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 343, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> ObjectType->castAs<TagType>()->isBeingDefined()) && |
| 343 | (0) . __assert_fail ("(IsDependent || !ObjectType->isIncompleteType() || ObjectType->castAs()->isBeingDefined()) && \"Caller should have completed object type\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 343, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Caller should have completed object type"); |
| 344 | |
| 345 | |
| 346 | if (ObjectType->isObjCObjectOrInterfaceType()) { |
| 347 | Found.clear(); |
| 348 | return false; |
| 349 | } |
| 350 | } else if (SS.isSet()) { |
| 351 | |
| 352 | |
| 353 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 354 | IsDependent = !LookupCtx; |
| 355 | |
| 356 | |
| 357 | if (LookupCtx && RequireCompleteDeclContext(SS, LookupCtx)) |
| 358 | return true; |
| 359 | } |
| 360 | |
| 361 | bool ObjectTypeSearchedInScope = false; |
| 362 | bool AllowFunctionTemplatesInLookup = true; |
| 363 | if (LookupCtx) { |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | LookupQualifiedName(Found, LookupCtx); |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | IsDependent |= Found.wasNotFoundInCurrentInstantiation(); |
| 379 | } |
| 380 | |
| 381 | if (!SS.isSet() && (ObjectType.isNull() || Found.empty())) { |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | if (S) |
| 392 | LookupName(Found, S); |
| 393 | |
| 394 | if (!ObjectType.isNull()) { |
| 395 | |
| 396 | |
| 397 | |
| 398 | AllowFunctionTemplatesInLookup = false; |
| 399 | ObjectTypeSearchedInScope = true; |
| 400 | } |
| 401 | |
| 402 | IsDependent |= Found.wasNotFoundInCurrentInstantiation(); |
| 403 | } |
| 404 | |
| 405 | if (Found.isAmbiguous()) |
| 406 | return false; |
| 407 | |
| 408 | if (Found.empty() && !IsDependent) { |
| 409 | |
| 410 | DeclarationName Name = Found.getLookupName(); |
| 411 | Found.clear(); |
| 412 | |
| 413 | DefaultFilterCCC FilterCCC{}; |
| 414 | FilterCCC.WantTypeSpecifiers = false; |
| 415 | FilterCCC.WantExpressionKeywords = false; |
| 416 | FilterCCC.WantRemainingKeywords = false; |
| 417 | FilterCCC.WantCXXNamedCasts = true; |
| 418 | if (TypoCorrection Corrected = |
| 419 | CorrectTypo(Found.getLookupNameInfo(), Found.getLookupKind(), S, |
| 420 | &SS, FilterCCC, CTK_ErrorRecovery, LookupCtx)) { |
| 421 | Found.setLookupName(Corrected.getCorrection()); |
| 422 | if (auto *ND = Corrected.getFoundDecl()) |
| 423 | Found.addDecl(ND); |
| 424 | FilterAcceptableTemplateNames(Found); |
| 425 | if (Found.isAmbiguous()) { |
| 426 | Found.clear(); |
| 427 | } else if (!Found.empty()) { |
| 428 | if (LookupCtx) { |
| 429 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 430 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
| 431 | Name.getAsString() == CorrectedStr; |
| 432 | diagnoseTypo(Corrected, PDiag(diag::err_no_member_template_suggest) |
| 433 | << Name << LookupCtx << DroppedSpecifier |
| 434 | << SS.getRange()); |
| 435 | } else { |
| 436 | diagnoseTypo(Corrected, PDiag(diag::err_no_template_suggest) << Name); |
| 437 | } |
| 438 | } |
| 439 | } else { |
| 440 | Found.setLookupName(Name); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | NamedDecl *ExampleLookupResult = |
| 445 | Found.empty() ? nullptr : Found.getRepresentativeDecl(); |
| 446 | FilterAcceptableTemplateNames(Found, AllowFunctionTemplatesInLookup); |
| 447 | if (Found.empty()) { |
| 448 | if (IsDependent) { |
| 449 | MemberOfUnknownSpecialization = true; |
| 450 | return false; |
| 451 | } |
| 452 | |
| 453 | |
| 454 | |
| 455 | if (ExampleLookupResult && TemplateKWLoc.isValid()) { |
| 456 | Diag(Found.getNameLoc(), diag::err_template_kw_refers_to_non_template) |
| 457 | << Found.getLookupName() << SS.getRange(); |
| 458 | Diag(ExampleLookupResult->getUnderlyingDecl()->getLocation(), |
| 459 | diag::note_template_kw_refers_to_non_template) |
| 460 | << Found.getLookupName(); |
| 461 | return true; |
| 462 | } |
| 463 | |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | if (S && !ObjectType.isNull() && !ObjectTypeSearchedInScope && |
| 468 | !getLangOpts().CPlusPlus11) { |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | LookupResult FoundOuter(*this, Found.getLookupName(), Found.getNameLoc(), |
| 476 | LookupOrdinaryName); |
| 477 | FoundOuter.setTemplateNameLookup(true); |
| 478 | LookupName(FoundOuter, S); |
| 479 | |
| 480 | |
| 481 | FilterAcceptableTemplateNames(FoundOuter, ); |
| 482 | |
| 483 | NamedDecl *OuterTemplate; |
| 484 | if (FoundOuter.empty()) { |
| 485 | |
| 486 | |
| 487 | } else if (FoundOuter.isAmbiguous() || !FoundOuter.isSingleResult() || |
| 488 | !(OuterTemplate = |
| 489 | getAsTemplateNameDecl(FoundOuter.getFoundDecl()))) { |
| 490 | |
| 491 | |
| 492 | |
| 493 | FoundOuter.clear(); |
| 494 | } else if (!Found.isSuppressingDiagnostics()) { |
| 495 | |
| 496 | |
| 497 | |
| 498 | if (!Found.isSingleResult() || |
| 499 | getAsTemplateNameDecl(Found.getFoundDecl())->getCanonicalDecl() != |
| 500 | OuterTemplate->getCanonicalDecl()) { |
| 501 | Diag(Found.getNameLoc(), |
| 502 | diag::ext_nested_name_member_ref_lookup_ambiguous) |
| 503 | << Found.getLookupName() |
| 504 | << ObjectType; |
| 505 | Diag(Found.getRepresentativeDecl()->getLocation(), |
| 506 | diag::note_ambig_member_ref_object_type) |
| 507 | << ObjectType; |
| 508 | Diag(FoundOuter.getFoundDecl()->getLocation(), |
| 509 | diag::note_ambig_member_ref_scope); |
| 510 | |
| 511 | |
| 512 | |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | return false; |
| 518 | } |
| 519 | |
| 520 | void Sema::diagnoseExprIntendedAsTemplateName(Scope *S, ExprResult TemplateName, |
| 521 | SourceLocation Less, |
| 522 | SourceLocation Greater) { |
| 523 | if (TemplateName.isInvalid()) |
| 524 | return; |
| 525 | |
| 526 | DeclarationNameInfo NameInfo; |
| 527 | CXXScopeSpec SS; |
| 528 | LookupNameKind LookupKind; |
| 529 | |
| 530 | DeclContext *LookupCtx = nullptr; |
| 531 | NamedDecl *Found = nullptr; |
| 532 | bool MissingTemplateKeyword = false; |
| 533 | |
| 534 | |
| 535 | if (auto *DRE = dyn_cast<DeclRefExpr>(TemplateName.get())) { |
| 536 | NameInfo = DRE->getNameInfo(); |
| 537 | SS.Adopt(DRE->getQualifierLoc()); |
| 538 | LookupKind = LookupOrdinaryName; |
| 539 | Found = DRE->getFoundDecl(); |
| 540 | } else if (auto *ME = dyn_cast<MemberExpr>(TemplateName.get())) { |
| 541 | NameInfo = ME->getMemberNameInfo(); |
| 542 | SS.Adopt(ME->getQualifierLoc()); |
| 543 | LookupKind = LookupMemberName; |
| 544 | LookupCtx = ME->getBase()->getType()->getAsCXXRecordDecl(); |
| 545 | Found = ME->getMemberDecl(); |
| 546 | } else if (auto *DSDRE = |
| 547 | dyn_cast<DependentScopeDeclRefExpr>(TemplateName.get())) { |
| 548 | NameInfo = DSDRE->getNameInfo(); |
| 549 | SS.Adopt(DSDRE->getQualifierLoc()); |
| 550 | MissingTemplateKeyword = true; |
| 551 | } else if (auto *DSME = |
| 552 | dyn_cast<CXXDependentScopeMemberExpr>(TemplateName.get())) { |
| 553 | NameInfo = DSME->getMemberNameInfo(); |
| 554 | SS.Adopt(DSME->getQualifierLoc()); |
| 555 | MissingTemplateKeyword = true; |
| 556 | } else { |
| 557 | llvm_unreachable("unexpected kind of potential template name"); |
| 558 | } |
| 559 | |
| 560 | |
| 561 | |
| 562 | if (MissingTemplateKeyword) { |
| 563 | Diag(NameInfo.getBeginLoc(), diag::err_template_kw_missing) |
| 564 | << "" << NameInfo.getName().getAsString() << SourceRange(Less, Greater); |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | |
| 569 | struct TemplateCandidateFilter : CorrectionCandidateCallback { |
| 570 | Sema &S; |
| 571 | TemplateCandidateFilter(Sema &S) : S(S) { |
| 572 | WantTypeSpecifiers = false; |
| 573 | WantExpressionKeywords = false; |
| 574 | WantRemainingKeywords = false; |
| 575 | WantCXXNamedCasts = true; |
| 576 | }; |
| 577 | bool ValidateCandidate(const TypoCorrection &Candidate) override { |
| 578 | if (auto *ND = Candidate.getCorrectionDecl()) |
| 579 | return S.getAsTemplateNameDecl(ND); |
| 580 | return Candidate.isKeyword(); |
| 581 | } |
| 582 | |
| 583 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
| 584 | return llvm::make_unique<TemplateCandidateFilter>(*this); |
| 585 | } |
| 586 | }; |
| 587 | |
| 588 | DeclarationName Name = NameInfo.getName(); |
| 589 | TemplateCandidateFilter CCC(*this); |
| 590 | if (TypoCorrection Corrected = CorrectTypo(NameInfo, LookupKind, S, &SS, CCC, |
| 591 | CTK_ErrorRecovery, LookupCtx)) { |
| 592 | auto *ND = Corrected.getFoundDecl(); |
| 593 | if (ND) |
| 594 | ND = getAsTemplateNameDecl(ND); |
| 595 | if (ND || Corrected.isKeyword()) { |
| 596 | if (LookupCtx) { |
| 597 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 598 | bool DroppedSpecifier = Corrected.WillReplaceSpecifier() && |
| 599 | Name.getAsString() == CorrectedStr; |
| 600 | diagnoseTypo(Corrected, |
| 601 | PDiag(diag::err_non_template_in_member_template_id_suggest) |
| 602 | << Name << LookupCtx << DroppedSpecifier |
| 603 | << SS.getRange(), false); |
| 604 | } else { |
| 605 | diagnoseTypo(Corrected, |
| 606 | PDiag(diag::err_non_template_in_template_id_suggest) |
| 607 | << Name, false); |
| 608 | } |
| 609 | if (Found) |
| 610 | Diag(Found->getLocation(), |
| 611 | diag::note_non_template_in_template_id_found); |
| 612 | return; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | Diag(NameInfo.getLoc(), diag::err_non_template_in_template_id) |
| 617 | << Name << SourceRange(Less, Greater); |
| 618 | if (Found) |
| 619 | Diag(Found->getLocation(), diag::note_non_template_in_template_id_found); |
| 620 | } |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | ExprResult |
| 626 | Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, |
| 627 | SourceLocation TemplateKWLoc, |
| 628 | const DeclarationNameInfo &NameInfo, |
| 629 | bool isAddressOfOperand, |
| 630 | const TemplateArgumentListInfo *TemplateArgs) { |
| 631 | DeclContext *DC = getFunctionLevelDeclContext(); |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
| 643 | |
| 644 | bool MightBeCxx11UnevalField = |
| 645 | getLangOpts().CPlusPlus11 && isUnevaluatedContext(); |
| 646 | |
| 647 | |
| 648 | bool IsEnum = false; |
| 649 | if (NestedNameSpecifier *NNS = SS.getScopeRep()) |
| 650 | IsEnum = dyn_cast_or_null<EnumType>(NNS->getAsType()); |
| 651 | |
| 652 | if (!MightBeCxx11UnevalField && !isAddressOfOperand && !IsEnum && |
| 653 | isa<CXXMethodDecl>(DC) && cast<CXXMethodDecl>(DC)->isInstance()) { |
| 654 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(); |
| 655 | |
| 656 | |
| 657 | |
| 658 | NamedDecl *FirstQualifierInScope = nullptr; |
| 659 | |
| 660 | return CXXDependentScopeMemberExpr::Create( |
| 661 | Context, nullptr, ThisType, true, |
| 662 | SourceLocation(), SS.getWithLocInContext(Context), TemplateKWLoc, |
| 663 | FirstQualifierInScope, NameInfo, TemplateArgs); |
| 664 | } |
| 665 | |
| 666 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
| 667 | } |
| 668 | |
| 669 | ExprResult |
| 670 | Sema::BuildDependentDeclRefExpr(const CXXScopeSpec &SS, |
| 671 | SourceLocation TemplateKWLoc, |
| 672 | const DeclarationNameInfo &NameInfo, |
| 673 | const TemplateArgumentListInfo *TemplateArgs) { |
| 674 | return DependentScopeDeclRefExpr::Create( |
| 675 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 676 | TemplateArgs); |
| 677 | } |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | bool Sema::DiagnoseUninstantiableTemplate(SourceLocation PointOfInstantiation, |
| 683 | NamedDecl *Instantiation, |
| 684 | bool InstantiatedFromMember, |
| 685 | const NamedDecl *Pattern, |
| 686 | const NamedDecl *PatternDef, |
| 687 | TemplateSpecializationKind TSK, |
| 688 | bool Complain ) { |
| 689 | (Instantiation) || isa(Instantiation) || isa(Instantiation)", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 690, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isa<TagDecl>(Instantiation) || isa<FunctionDecl>(Instantiation) || |
| 690 | (Instantiation) || isa(Instantiation) || isa(Instantiation)", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 690, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> isa<VarDecl>(Instantiation)); |
| 691 | |
| 692 | bool IsEntityBeingDefined = false; |
| 693 | if (const TagDecl *TD = dyn_cast_or_null<TagDecl>(PatternDef)) |
| 694 | IsEntityBeingDefined = TD->isBeingDefined(); |
| 695 | |
| 696 | if (PatternDef && !IsEntityBeingDefined) { |
| 697 | NamedDecl *SuggestedDef = nullptr; |
| 698 | if (!hasVisibleDefinition(const_cast<NamedDecl*>(PatternDef), &SuggestedDef, |
| 699 | )) { |
| 700 | |
| 701 | bool Recover = Complain && !isSFINAEContext(); |
| 702 | if (Complain) |
| 703 | diagnoseMissingImport(PointOfInstantiation, SuggestedDef, |
| 704 | Sema::MissingImportKind::Definition, Recover); |
| 705 | return !Recover; |
| 706 | } |
| 707 | return false; |
| 708 | } |
| 709 | |
| 710 | if (!Complain || (PatternDef && PatternDef->isInvalidDecl())) |
| 711 | return true; |
| 712 | |
| 713 | llvm::Optional<unsigned> Note; |
| 714 | QualType InstantiationTy; |
| 715 | if (TagDecl *TD = dyn_cast<TagDecl>(Instantiation)) |
| 716 | InstantiationTy = Context.getTypeDeclType(TD); |
| 717 | if (PatternDef) { |
| 718 | Diag(PointOfInstantiation, |
| 719 | diag::err_template_instantiate_within_definition) |
| 720 | << (TSK != TSK_ImplicitInstantiation) |
| 721 | << InstantiationTy; |
| 722 | |
| 723 | |
| 724 | Instantiation->setInvalidDecl(); |
| 725 | } else if (InstantiatedFromMember) { |
| 726 | if (isa<FunctionDecl>(Instantiation)) { |
| 727 | Diag(PointOfInstantiation, |
| 728 | diag::err_explicit_instantiation_undefined_member) |
| 729 | << 1 << Instantiation->getDeclName() |
| 730 | << Instantiation->getDeclContext(); |
| 731 | Note = diag::note_explicit_instantiation_here; |
| 732 | } else { |
| 733 | (0) . __assert_fail ("isa(Instantiation) && \"Must be a TagDecl!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 733, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isa<TagDecl>(Instantiation) && "Must be a TagDecl!"); |
| 734 | Diag(PointOfInstantiation, |
| 735 | diag::err_implicit_instantiate_member_undefined) |
| 736 | << InstantiationTy; |
| 737 | Note = diag::note_member_declared_at; |
| 738 | } |
| 739 | } else { |
| 740 | if (isa<FunctionDecl>(Instantiation)) { |
| 741 | Diag(PointOfInstantiation, |
| 742 | diag::err_explicit_instantiation_undefined_func_template) |
| 743 | << Pattern; |
| 744 | Note = diag::note_explicit_instantiation_here; |
| 745 | } else if (isa<TagDecl>(Instantiation)) { |
| 746 | Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) |
| 747 | << (TSK != TSK_ImplicitInstantiation) |
| 748 | << InstantiationTy; |
| 749 | Note = diag::note_template_decl_here; |
| 750 | } else { |
| 751 | (0) . __assert_fail ("isa(Instantiation) && \"Must be a VarDecl!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 751, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isa<VarDecl>(Instantiation) && "Must be a VarDecl!"); |
| 752 | if (isa<VarTemplateSpecializationDecl>(Instantiation)) { |
| 753 | Diag(PointOfInstantiation, |
| 754 | diag::err_explicit_instantiation_undefined_var_template) |
| 755 | << Instantiation; |
| 756 | Instantiation->setInvalidDecl(); |
| 757 | } else |
| 758 | Diag(PointOfInstantiation, |
| 759 | diag::err_explicit_instantiation_undefined_member) |
| 760 | << 2 << Instantiation->getDeclName() |
| 761 | << Instantiation->getDeclContext(); |
| 762 | Note = diag::note_explicit_instantiation_here; |
| 763 | } |
| 764 | } |
| 765 | if (Note) |
| 766 | Diag(Pattern->getLocation(), Note.getValue()); |
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | if (TSK == TSK_ExplicitInstantiationDeclaration) |
| 773 | Instantiation->setInvalidDecl(); |
| 774 | return true; |
| 775 | } |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | void Sema::DiagnoseTemplateParameterShadow(SourceLocation Loc, Decl *PrevDecl) { |
| 782 | (0) . __assert_fail ("PrevDecl->isTemplateParameter() && \"Not a template parameter\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 782, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(PrevDecl->isTemplateParameter() && "Not a template parameter"); |
| 783 | |
| 784 | |
| 785 | if (getLangOpts().MicrosoftExt) |
| 786 | return; |
| 787 | |
| 788 | |
| 789 | |
| 790 | |
| 791 | Diag(Loc, diag::err_template_param_shadow) |
| 792 | << cast<NamedDecl>(PrevDecl)->getDeclName(); |
| 793 | Diag(PrevDecl->getLocation(), diag::note_template_param_here); |
| 794 | } |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | TemplateDecl *Sema::AdjustDeclIfTemplate(Decl *&D) { |
| 800 | if (TemplateDecl *Temp = dyn_cast_or_null<TemplateDecl>(D)) { |
| 801 | D = Temp->getTemplatedDecl(); |
| 802 | return Temp; |
| 803 | } |
| 804 | return nullptr; |
| 805 | } |
| 806 | |
| 807 | ParsedTemplateArgument ParsedTemplateArgument::getTemplatePackExpansion( |
| 808 | SourceLocation EllipsisLoc) const { |
| 809 | (0) . __assert_fail ("Kind == Template && \"Only template template arguments can be pack expansions here\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 810, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Kind == Template && |
| 810 | (0) . __assert_fail ("Kind == Template && \"Only template template arguments can be pack expansions here\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 810, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Only template template arguments can be pack expansions here"); |
| 811 | (0) . __assert_fail ("getAsTemplate().get().containsUnexpandedParameterPack() && \"Template template argument pack expansion without packs\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 812, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(getAsTemplate().get().containsUnexpandedParameterPack() && |
| 812 | (0) . __assert_fail ("getAsTemplate().get().containsUnexpandedParameterPack() && \"Template template argument pack expansion without packs\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 812, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Template template argument pack expansion without packs"); |
| 813 | ParsedTemplateArgument Result(*this); |
| 814 | Result.EllipsisLoc = EllipsisLoc; |
| 815 | return Result; |
| 816 | } |
| 817 | |
| 818 | static TemplateArgumentLoc translateTemplateArgument(Sema &SemaRef, |
| 819 | const ParsedTemplateArgument &Arg) { |
| 820 | |
| 821 | switch (Arg.getKind()) { |
| 822 | case ParsedTemplateArgument::Type: { |
| 823 | TypeSourceInfo *DI; |
| 824 | QualType T = SemaRef.GetTypeFromParser(Arg.getAsType(), &DI); |
| 825 | if (!DI) |
| 826 | DI = SemaRef.Context.getTrivialTypeSourceInfo(T, Arg.getLocation()); |
| 827 | return TemplateArgumentLoc(TemplateArgument(T), DI); |
| 828 | } |
| 829 | |
| 830 | case ParsedTemplateArgument::NonType: { |
| 831 | Expr *E = static_cast<Expr *>(Arg.getAsExpr()); |
| 832 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 833 | } |
| 834 | |
| 835 | case ParsedTemplateArgument::Template: { |
| 836 | TemplateName Template = Arg.getAsTemplate().get(); |
| 837 | TemplateArgument TArg; |
| 838 | if (Arg.getEllipsisLoc().isValid()) |
| 839 | TArg = TemplateArgument(Template, Optional<unsigned int>()); |
| 840 | else |
| 841 | TArg = Template; |
| 842 | return TemplateArgumentLoc(TArg, |
| 843 | Arg.getScopeSpec().getWithLocInContext( |
| 844 | SemaRef.Context), |
| 845 | Arg.getLocation(), |
| 846 | Arg.getEllipsisLoc()); |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | llvm_unreachable("Unhandled parsed template argument"); |
| 851 | } |
| 852 | |
| 853 | |
| 854 | |
| 855 | void Sema::translateTemplateArguments(const ASTTemplateArgsPtr &TemplateArgsIn, |
| 856 | TemplateArgumentListInfo &TemplateArgs) { |
| 857 | for (unsigned I = 0, Last = TemplateArgsIn.size(); I != Last; ++I) |
| 858 | TemplateArgs.addArgument(translateTemplateArgument(*this, |
| 859 | TemplateArgsIn[I])); |
| 860 | } |
| 861 | |
| 862 | static void maybeDiagnoseTemplateParameterShadow(Sema &SemaRef, Scope *S, |
| 863 | SourceLocation Loc, |
| 864 | IdentifierInfo *Name) { |
| 865 | NamedDecl *PrevDecl = SemaRef.LookupSingleName( |
| 866 | S, Name, Loc, Sema::LookupOrdinaryName, Sema::ForVisibleRedeclaration); |
| 867 | if (PrevDecl && PrevDecl->isTemplateParameter()) |
| 868 | SemaRef.DiagnoseTemplateParameterShadow(Loc, PrevDecl); |
| 869 | } |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | |
| 875 | ParsedTemplateArgument Sema::ActOnTemplateTypeArgument(TypeResult ParsedType) { |
| 876 | TypeSourceInfo *TInfo; |
| 877 | QualType T = GetTypeFromParser(ParsedType.get(), &TInfo); |
| 878 | if (T.isNull()) |
| 879 | return ParsedTemplateArgument(); |
| 880 | (0) . __assert_fail ("TInfo && \"template argument with no location\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 880, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(TInfo && "template argument with no location"); |
| 881 | |
| 882 | |
| 883 | |
| 884 | if (getLangOpts().CPlusPlus17) { |
| 885 | TypeLoc TL = TInfo->getTypeLoc(); |
| 886 | SourceLocation EllipsisLoc; |
| 887 | if (auto PET = TL.getAs<PackExpansionTypeLoc>()) { |
| 888 | EllipsisLoc = PET.getEllipsisLoc(); |
| 889 | TL = PET.getPatternLoc(); |
| 890 | } |
| 891 | |
| 892 | CXXScopeSpec SS; |
| 893 | if (auto ET = TL.getAs<ElaboratedTypeLoc>()) { |
| 894 | SS.Adopt(ET.getQualifierLoc()); |
| 895 | TL = ET.getNamedTypeLoc(); |
| 896 | } |
| 897 | |
| 898 | if (auto DTST = TL.getAs<DeducedTemplateSpecializationTypeLoc>()) { |
| 899 | TemplateName Name = DTST.getTypePtr()->getTemplateName(); |
| 900 | if (SS.isSet()) |
| 901 | Name = Context.getQualifiedTemplateName(SS.getScopeRep(), |
| 902 | false, |
| 903 | Name.getAsTemplateDecl()); |
| 904 | ParsedTemplateArgument Result(SS, TemplateTy::make(Name), |
| 905 | DTST.getTemplateNameLoc()); |
| 906 | if (EllipsisLoc.isValid()) |
| 907 | Result = Result.getTemplatePackExpansion(EllipsisLoc); |
| 908 | return Result; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | |
| 916 | return ParsedTemplateArgument(ParsedTemplateArgument::Type, |
| 917 | ParsedType.get().getAsOpaquePtr(), |
| 918 | TInfo->getTypeLoc().getBeginLoc()); |
| 919 | } |
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | |
| 929 | |
| 930 | NamedDecl *Sema::ActOnTypeParameter(Scope *S, bool Typename, |
| 931 | SourceLocation EllipsisLoc, |
| 932 | SourceLocation KeyLoc, |
| 933 | IdentifierInfo *ParamName, |
| 934 | SourceLocation ParamNameLoc, |
| 935 | unsigned Depth, unsigned Position, |
| 936 | SourceLocation EqualLoc, |
| 937 | ParsedType DefaultArg) { |
| 938 | (0) . __assert_fail ("S->isTemplateParamScope() && \"Template type parameter not in template parameter scope!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 939, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(S->isTemplateParamScope() && |
| 939 | (0) . __assert_fail ("S->isTemplateParamScope() && \"Template type parameter not in template parameter scope!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 939, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Template type parameter not in template parameter scope!"); |
| 940 | |
| 941 | SourceLocation Loc = ParamNameLoc; |
| 942 | if (!ParamName) |
| 943 | Loc = KeyLoc; |
| 944 | |
| 945 | bool IsParameterPack = EllipsisLoc.isValid(); |
| 946 | TemplateTypeParmDecl *Param |
| 947 | = TemplateTypeParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 948 | KeyLoc, Loc, Depth, Position, ParamName, |
| 949 | Typename, IsParameterPack); |
| 950 | Param->setAccess(AS_public); |
| 951 | |
| 952 | if (ParamName) { |
| 953 | maybeDiagnoseTemplateParameterShadow(*this, S, ParamNameLoc, ParamName); |
| 954 | |
| 955 | |
| 956 | S->AddDecl(Param); |
| 957 | IdResolver.AddDecl(Param); |
| 958 | } |
| 959 | |
| 960 | |
| 961 | |
| 962 | |
| 963 | if (DefaultArg && IsParameterPack) { |
| 964 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 965 | DefaultArg = nullptr; |
| 966 | } |
| 967 | |
| 968 | |
| 969 | if (DefaultArg) { |
| 970 | TypeSourceInfo *DefaultTInfo; |
| 971 | GetTypeFromParser(DefaultArg, &DefaultTInfo); |
| 972 | |
| 973 | (0) . __assert_fail ("DefaultTInfo && \"expected source information for type\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 973, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(DefaultTInfo && "expected source information for type"); |
| 974 | |
| 975 | |
| 976 | if (DiagnoseUnexpandedParameterPack(Loc, DefaultTInfo, |
| 977 | UPPC_DefaultArgument)) |
| 978 | return Param; |
| 979 | |
| 980 | |
| 981 | if (CheckTemplateArgument(Param, DefaultTInfo)) { |
| 982 | Param->setInvalidDecl(); |
| 983 | return Param; |
| 984 | } |
| 985 | |
| 986 | Param->setDefaultArgument(DefaultTInfo); |
| 987 | } |
| 988 | |
| 989 | return Param; |
| 990 | } |
| 991 | |
| 992 | |
| 993 | |
| 994 | |
| 995 | |
| 996 | |
| 997 | QualType Sema::CheckNonTypeTemplateParameterType(TypeSourceInfo *&TSI, |
| 998 | SourceLocation Loc) { |
| 999 | if (TSI->getType()->isUndeducedType()) { |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | |
| 1004 | |
| 1005 | TSI = SubstAutoTypeSourceInfo(TSI, Context.DependentTy); |
| 1006 | } |
| 1007 | |
| 1008 | return CheckNonTypeTemplateParameterType(TSI->getType(), Loc); |
| 1009 | } |
| 1010 | |
| 1011 | QualType Sema::CheckNonTypeTemplateParameterType(QualType T, |
| 1012 | SourceLocation Loc) { |
| 1013 | |
| 1014 | |
| 1015 | if (T->isVariablyModifiedType()) { |
| 1016 | Diag(Loc, diag::err_variably_modified_nontype_template_param) |
| 1017 | << T; |
| 1018 | return QualType(); |
| 1019 | } |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | if (T->isIntegralOrEnumerationType() || |
| 1028 | |
| 1029 | T->isPointerType() || |
| 1030 | |
| 1031 | T->isReferenceType() || |
| 1032 | |
| 1033 | T->isMemberPointerType() || |
| 1034 | |
| 1035 | T->isNullPtrType() || |
| 1036 | |
| 1037 | |
| 1038 | T->isDependentType() || |
| 1039 | |
| 1040 | T->isUndeducedType()) { |
| 1041 | |
| 1042 | |
| 1043 | return T.getUnqualifiedType(); |
| 1044 | } |
| 1045 | |
| 1046 | |
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | else if (T->isArrayType() || T->isFunctionType()) |
| 1052 | return Context.getDecayedType(T); |
| 1053 | |
| 1054 | Diag(Loc, diag::err_template_nontype_parm_bad_type) |
| 1055 | << T; |
| 1056 | |
| 1057 | return QualType(); |
| 1058 | } |
| 1059 | |
| 1060 | NamedDecl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D, |
| 1061 | unsigned Depth, |
| 1062 | unsigned Position, |
| 1063 | SourceLocation EqualLoc, |
| 1064 | Expr *Default) { |
| 1065 | TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S); |
| 1066 | |
| 1067 | |
| 1068 | auto CheckValidDeclSpecifiers = [this, &D] { |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | |
| 1074 | |
| 1075 | |
| 1076 | |
| 1077 | |
| 1078 | |
| 1079 | |
| 1080 | const DeclSpec &DS = D.getDeclSpec(); |
| 1081 | auto EmitDiag = [this](SourceLocation Loc) { |
| 1082 | Diag(Loc, diag::err_invalid_decl_specifier_in_nontype_parm) |
| 1083 | << FixItHint::CreateRemoval(Loc); |
| 1084 | }; |
| 1085 | if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) |
| 1086 | EmitDiag(DS.getStorageClassSpecLoc()); |
| 1087 | |
| 1088 | if (DS.getThreadStorageClassSpec() != TSCS_unspecified) |
| 1089 | EmitDiag(DS.getThreadStorageClassSpecLoc()); |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | |
| 1094 | |
| 1095 | if (DS.isInlineSpecified()) |
| 1096 | EmitDiag(DS.getInlineSpecLoc()); |
| 1097 | |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | |
| 1102 | |
| 1103 | if (DS.isConstexprSpecified()) |
| 1104 | EmitDiag(DS.getConstexprSpecLoc()); |
| 1105 | |
| 1106 | |
| 1107 | |
| 1108 | |
| 1109 | if (DS.isVirtualSpecified()) |
| 1110 | EmitDiag(DS.getVirtualSpecLoc()); |
| 1111 | |
| 1112 | if (DS.isExplicitSpecified()) |
| 1113 | EmitDiag(DS.getExplicitSpecLoc()); |
| 1114 | |
| 1115 | if (DS.isNoreturnSpecified()) |
| 1116 | EmitDiag(DS.getNoreturnSpecLoc()); |
| 1117 | }; |
| 1118 | |
| 1119 | CheckValidDeclSpecifiers(); |
| 1120 | |
| 1121 | if (TInfo->getType()->isUndeducedType()) { |
| 1122 | Diag(D.getIdentifierLoc(), |
| 1123 | diag::warn_cxx14_compat_template_nontype_parm_auto_type) |
| 1124 | << QualType(TInfo->getType()->getContainedAutoType(), 0); |
| 1125 | } |
| 1126 | |
| 1127 | (0) . __assert_fail ("S->isTemplateParamScope() && \"Non-type template parameter not in template parameter scope!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 1128, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(S->isTemplateParamScope() && |
| 1128 | (0) . __assert_fail ("S->isTemplateParamScope() && \"Non-type template parameter not in template parameter scope!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 1128, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Non-type template parameter not in template parameter scope!"); |
| 1129 | bool Invalid = false; |
| 1130 | |
| 1131 | QualType T = CheckNonTypeTemplateParameterType(TInfo, D.getIdentifierLoc()); |
| 1132 | if (T.isNull()) { |
| 1133 | T = Context.IntTy; |
| 1134 | Invalid = true; |
| 1135 | } |
| 1136 | |
| 1137 | IdentifierInfo *ParamName = D.getIdentifier(); |
| 1138 | bool IsParameterPack = D.hasEllipsis(); |
| 1139 | NonTypeTemplateParmDecl *Param = NonTypeTemplateParmDecl::Create( |
| 1140 | Context, Context.getTranslationUnitDecl(), D.getBeginLoc(), |
| 1141 | D.getIdentifierLoc(), Depth, Position, ParamName, T, IsParameterPack, |
| 1142 | TInfo); |
| 1143 | Param->setAccess(AS_public); |
| 1144 | |
| 1145 | if (Invalid) |
| 1146 | Param->setInvalidDecl(); |
| 1147 | |
| 1148 | if (ParamName) { |
| 1149 | maybeDiagnoseTemplateParameterShadow(*this, S, D.getIdentifierLoc(), |
| 1150 | ParamName); |
| 1151 | |
| 1152 | |
| 1153 | S->AddDecl(Param); |
| 1154 | IdResolver.AddDecl(Param); |
| 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 | if (Default && IsParameterPack) { |
| 1161 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 1162 | Default = nullptr; |
| 1163 | } |
| 1164 | |
| 1165 | |
| 1166 | if (Default) { |
| 1167 | |
| 1168 | if (DiagnoseUnexpandedParameterPack(Default, UPPC_DefaultArgument)) |
| 1169 | return Param; |
| 1170 | |
| 1171 | TemplateArgument Converted; |
| 1172 | ExprResult DefaultRes = |
| 1173 | CheckTemplateArgument(Param, Param->getType(), Default, Converted); |
| 1174 | if (DefaultRes.isInvalid()) { |
| 1175 | Param->setInvalidDecl(); |
| 1176 | return Param; |
| 1177 | } |
| 1178 | Default = DefaultRes.get(); |
| 1179 | |
| 1180 | Param->setDefaultArgument(Default); |
| 1181 | } |
| 1182 | |
| 1183 | return Param; |
| 1184 | } |
| 1185 | |
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | NamedDecl *Sema::ActOnTemplateTemplateParameter(Scope* S, |
| 1190 | SourceLocation TmpLoc, |
| 1191 | TemplateParameterList *Params, |
| 1192 | SourceLocation EllipsisLoc, |
| 1193 | IdentifierInfo *Name, |
| 1194 | SourceLocation NameLoc, |
| 1195 | unsigned Depth, |
| 1196 | unsigned Position, |
| 1197 | SourceLocation EqualLoc, |
| 1198 | ParsedTemplateArgument Default) { |
| 1199 | (0) . __assert_fail ("S->isTemplateParamScope() && \"Template template parameter not in template parameter scope!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 1200, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(S->isTemplateParamScope() && |
| 1200 | (0) . __assert_fail ("S->isTemplateParamScope() && \"Template template parameter not in template parameter scope!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 1200, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Template template parameter not in template parameter scope!"); |
| 1201 | |
| 1202 | |
| 1203 | bool IsParameterPack = EllipsisLoc.isValid(); |
| 1204 | TemplateTemplateParmDecl *Param = |
| 1205 | TemplateTemplateParmDecl::Create(Context, Context.getTranslationUnitDecl(), |
| 1206 | NameLoc.isInvalid()? TmpLoc : NameLoc, |
| 1207 | Depth, Position, IsParameterPack, |
| 1208 | Name, Params); |
| 1209 | Param->setAccess(AS_public); |
| 1210 | |
| 1211 | |
| 1212 | |
| 1213 | if (Name) { |
| 1214 | maybeDiagnoseTemplateParameterShadow(*this, S, NameLoc, Name); |
| 1215 | |
| 1216 | S->AddDecl(Param); |
| 1217 | IdResolver.AddDecl(Param); |
| 1218 | } |
| 1219 | |
| 1220 | if (Params->size() == 0) { |
| 1221 | Diag(Param->getLocation(), diag::err_template_template_parm_no_parms) |
| 1222 | << SourceRange(Params->getLAngleLoc(), Params->getRAngleLoc()); |
| 1223 | Param->setInvalidDecl(); |
| 1224 | } |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 | |
| 1229 | if (IsParameterPack && !Default.isInvalid()) { |
| 1230 | Diag(EqualLoc, diag::err_template_param_pack_default_arg); |
| 1231 | Default = ParsedTemplateArgument(); |
| 1232 | } |
| 1233 | |
| 1234 | if (!Default.isInvalid()) { |
| 1235 | |
| 1236 | |
| 1237 | |
| 1238 | |
| 1239 | |
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | TemplateArgumentLoc DefaultArg = translateTemplateArgument(*this, Default); |
| 1244 | if (DefaultArg.getArgument().getAsTemplate().isNull()) { |
| 1245 | Diag(DefaultArg.getLocation(), diag::err_template_arg_not_valid_template) |
| 1246 | << DefaultArg.getSourceRange(); |
| 1247 | return Param; |
| 1248 | } |
| 1249 | |
| 1250 | |
| 1251 | if (DiagnoseUnexpandedParameterPack(DefaultArg.getLocation(), |
| 1252 | DefaultArg.getArgument().getAsTemplate(), |
| 1253 | UPPC_DefaultArgument)) |
| 1254 | return Param; |
| 1255 | |
| 1256 | Param->setDefaultArgument(Context, DefaultArg); |
| 1257 | } |
| 1258 | |
| 1259 | return Param; |
| 1260 | } |
| 1261 | |
| 1262 | |
| 1263 | |
| 1264 | |
| 1265 | TemplateParameterList * |
| 1266 | Sema::ActOnTemplateParameterList(unsigned Depth, |
| 1267 | SourceLocation ExportLoc, |
| 1268 | SourceLocation TemplateLoc, |
| 1269 | SourceLocation LAngleLoc, |
| 1270 | ArrayRef<NamedDecl *> Params, |
| 1271 | SourceLocation RAngleLoc, |
| 1272 | Expr *RequiresClause) { |
| 1273 | if (ExportLoc.isValid()) |
| 1274 | Diag(ExportLoc, diag::warn_template_export_unsupported); |
| 1275 | |
| 1276 | return TemplateParameterList::Create( |
| 1277 | Context, TemplateLoc, LAngleLoc, |
| 1278 | llvm::makeArrayRef(Params.data(), Params.size()), |
| 1279 | RAngleLoc, RequiresClause); |
| 1280 | } |
| 1281 | |
| 1282 | static void SetNestedNameSpecifier(Sema &S, TagDecl *T, |
| 1283 | const CXXScopeSpec &SS) { |
| 1284 | if (SS.isSet()) |
| 1285 | T->setQualifierInfo(SS.getWithLocInContext(S.Context)); |
| 1286 | } |
| 1287 | |
| 1288 | DeclResult Sema::CheckClassTemplate( |
| 1289 | Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc, |
| 1290 | CXXScopeSpec &SS, IdentifierInfo *Name, SourceLocation NameLoc, |
| 1291 | const ParsedAttributesView &Attr, TemplateParameterList *TemplateParams, |
| 1292 | AccessSpecifier AS, SourceLocation ModulePrivateLoc, |
| 1293 | SourceLocation FriendLoc, unsigned NumOuterTemplateParamLists, |
| 1294 | TemplateParameterList **OuterTemplateParamLists, SkipBodyInfo *SkipBody) { |
| 1295 | (0) . __assert_fail ("TemplateParams && TemplateParams->size() > 0 && \"No template parameters\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 1296, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(TemplateParams && TemplateParams->size() > 0 && |
| 1296 | (0) . __assert_fail ("TemplateParams && TemplateParams->size() > 0 && \"No template parameters\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 1296, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "No template parameters"); |
| 1297 | (0) . __assert_fail ("TUK != TUK_Reference && \"Can only declare or define class templates\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 1297, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(TUK != TUK_Reference && "Can only declare or define class templates"); |
| 1298 | bool Invalid = false; |
| 1299 | |
| 1300 | |
| 1301 | if (CheckTemplateDeclScope(S, TemplateParams)) |
| 1302 | return true; |
| 1303 | |
| 1304 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 1305 | (0) . __assert_fail ("Kind != TTK_Enum && \"can't build template of enumerated type\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 1305, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Kind != TTK_Enum && "can't build template of enumerated type"); |
| 1306 | |
| 1307 | |
| 1308 | if (!Name) { |
| 1309 | Diag(KWLoc, diag::err_template_unnamed_class); |
| 1310 | return true; |
| 1311 | } |
| 1312 | |
| 1313 | |
| 1314 | |
| 1315 | |
| 1316 | DeclContext *SemanticContext; |
| 1317 | LookupResult Previous(*this, Name, NameLoc, |
| 1318 | (SS.isEmpty() && TUK == TUK_Friend) |
| 1319 | ? LookupTagName : LookupOrdinaryName, |
| 1320 | forRedeclarationInCurContext()); |
| 1321 | if (SS.isNotEmpty() && !SS.isInvalid()) { |
| 1322 | SemanticContext = computeDeclContext(SS, true); |
| 1323 | if (!SemanticContext) { |
| 1324 | |
| 1325 | |
| 1326 | |
| 1327 | Diag(NameLoc, TUK == TUK_Friend |
| 1328 | ? diag::warn_template_qualified_friend_ignored |
| 1329 | : diag::err_template_qualified_declarator_no_match) |
| 1330 | << SS.getScopeRep() << SS.getRange(); |
| 1331 | return TUK != TUK_Friend; |
| 1332 | } |
| 1333 | |
| 1334 | if (RequireCompleteDeclContext(SS, SemanticContext)) |
| 1335 | return true; |
| 1336 | |
| 1337 | |
| 1338 | |
| 1339 | |
| 1340 | if (SemanticContext->isDependentContext()) { |
| 1341 | ContextRAII SavedContext(*this, SemanticContext); |
| 1342 | if (RebuildTemplateParamsInCurrentInstantiation(TemplateParams)) |
| 1343 | Invalid = true; |
| 1344 | } else if (TUK != TUK_Friend && TUK != TUK_Reference) |
| 1345 | diagnoseQualifiedDeclaration(SS, SemanticContext, Name, NameLoc, false); |
| 1346 | |
| 1347 | LookupQualifiedName(Previous, SemanticContext); |
| 1348 | } else { |
| 1349 | SemanticContext = CurContext; |
| 1350 | |
| 1351 | |
| 1352 | |
| 1353 | |
| 1354 | |
| 1355 | if (TUK != TUK_Friend && |
| 1356 | DiagnoseClassNameShadow(SemanticContext, |
| 1357 | DeclarationNameInfo(Name, NameLoc))) |
| 1358 | return true; |
| 1359 | |
| 1360 | LookupName(Previous, S); |
| 1361 | } |
| 1362 | |
| 1363 | if (Previous.isAmbiguous()) |
| 1364 | return true; |
| 1365 | |
| 1366 | NamedDecl *PrevDecl = nullptr; |
| 1367 | if (Previous.begin() != Previous.end()) |
| 1368 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
| 1369 | |
| 1370 | if (PrevDecl && PrevDecl->isTemplateParameter()) { |
| 1371 | |
| 1372 | DiagnoseTemplateParameterShadow(NameLoc, PrevDecl); |
| 1373 | |
| 1374 | PrevDecl = nullptr; |
| 1375 | } |
| 1376 | |
| 1377 | |
| 1378 | |
| 1379 | ClassTemplateDecl *PrevClassTemplate = |
| 1380 | dyn_cast_or_null<ClassTemplateDecl>(PrevDecl); |
| 1381 | |
| 1382 | |
| 1383 | |
| 1384 | |
| 1385 | if (!PrevClassTemplate && PrevDecl && isa<CXXRecordDecl>(PrevDecl) && |
| 1386 | cast<CXXRecordDecl>(PrevDecl)->isInjectedClassName()) { |
| 1387 | PrevDecl = cast<CXXRecordDecl>(PrevDecl->getDeclContext()); |
| 1388 | PrevClassTemplate |
| 1389 | = cast<CXXRecordDecl>(PrevDecl)->getDescribedClassTemplate(); |
| 1390 | if (!PrevClassTemplate && isa<ClassTemplateSpecializationDecl>(PrevDecl)) { |
| 1391 | PrevClassTemplate |
| 1392 | = cast<ClassTemplateSpecializationDecl>(PrevDecl) |
| 1393 | ->getSpecializedTemplate(); |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | if (TUK == TUK_Friend) { |
| 1398 | |
| 1399 | |
| 1400 | |
| 1401 | |
| 1402 | |
| 1403 | if (!SS.isSet()) { |
| 1404 | DeclContext *OutermostContext = CurContext; |
| 1405 | while (!OutermostContext->isFileContext()) |
| 1406 | OutermostContext = OutermostContext->getLookupParent(); |
| 1407 | |
| 1408 | if (PrevDecl && |
| 1409 | (OutermostContext->Equals(PrevDecl->getDeclContext()) || |
| 1410 | OutermostContext->Encloses(PrevDecl->getDeclContext()))) { |
| 1411 | SemanticContext = PrevDecl->getDeclContext(); |
| 1412 | } else { |
| 1413 | |
| 1414 | |
| 1415 | |
| 1416 | PrevDecl = PrevClassTemplate = nullptr; |
| 1417 | SemanticContext = OutermostContext; |
| 1418 | |
| 1419 | |
| 1420 | |
| 1421 | Previous.clear(LookupOrdinaryName); |
| 1422 | DeclContext *LookupContext = SemanticContext; |
| 1423 | while (LookupContext->isTransparentContext()) |
| 1424 | LookupContext = LookupContext->getLookupParent(); |
| 1425 | LookupQualifiedName(Previous, LookupContext); |
| 1426 | |
| 1427 | if (Previous.isAmbiguous()) |
| 1428 | return true; |
| 1429 | |
| 1430 | if (Previous.begin() != Previous.end()) |
| 1431 | PrevDecl = (*Previous.begin())->getUnderlyingDecl(); |
| 1432 | } |
| 1433 | } |
| 1434 | } else if (PrevDecl && |
| 1435 | !isDeclInScope(Previous.getRepresentativeDecl(), SemanticContext, |
| 1436 | S, SS.isValid())) |
| 1437 | PrevDecl = PrevClassTemplate = nullptr; |
| 1438 | |
| 1439 | if (auto *Shadow = dyn_cast_or_null<UsingShadowDecl>( |
| 1440 | PrevDecl ? Previous.getRepresentativeDecl() : nullptr)) { |
| 1441 | if (SS.isEmpty() && |
| 1442 | !(PrevClassTemplate && |
| 1443 | PrevClassTemplate->getDeclContext()->getRedeclContext()->Equals( |
| 1444 | SemanticContext->getRedeclContext()))) { |
| 1445 | Diag(KWLoc, diag::err_using_decl_conflict_reverse); |
| 1446 | Diag(Shadow->getTargetDecl()->getLocation(), |
| 1447 | diag::note_using_decl_target); |
| 1448 | Diag(Shadow->getUsingDecl()->getLocation(), diag::note_using_decl) << 0; |
| 1449 | |
| 1450 | PrevDecl = PrevClassTemplate = nullptr; |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | |
| 1455 | Expr *const CurAC = formAssociatedConstraints(TemplateParams, nullptr); |
| 1456 | |
| 1457 | if (PrevClassTemplate) { |
| 1458 | |
| 1459 | |
| 1460 | |
| 1461 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 1462 | !TemplateParameterListsAreEqual(TemplateParams, |
| 1463 | PrevClassTemplate->getTemplateParameters(), |
| 1464 | , |
| 1465 | TPL_TemplateMatch)) |
| 1466 | return true; |
| 1467 | |
| 1468 | |
| 1469 | const Expr *const PrevAC = PrevClassTemplate->getAssociatedConstraints(); |
| 1470 | const bool RedeclACMismatch = [&] { |
| 1471 | if (!(CurAC || PrevAC)) |
| 1472 | return false; |
| 1473 | if (CurAC && PrevAC) { |
| 1474 | llvm::FoldingSetNodeID CurACInfo, PrevACInfo; |
| 1475 | CurAC->Profile(CurACInfo, Context, ); |
| 1476 | PrevAC->Profile(PrevACInfo, Context, ); |
| 1477 | if (CurACInfo == PrevACInfo) |
| 1478 | return false; |
| 1479 | } |
| 1480 | return true; |
| 1481 | }(); |
| 1482 | |
| 1483 | if (RedeclACMismatch) { |
| 1484 | Diag(CurAC ? CurAC->getBeginLoc() : NameLoc, |
| 1485 | diag::err_template_different_associated_constraints); |
| 1486 | Diag(PrevAC ? PrevAC->getBeginLoc() : PrevClassTemplate->getLocation(), |
| 1487 | diag::note_template_prev_declaration) |
| 1488 | << 0; |
| 1489 | return true; |
| 1490 | } |
| 1491 | |
| 1492 | |
| 1493 | |
| 1494 | |
| 1495 | |
| 1496 | |
| 1497 | RecordDecl *PrevRecordDecl = PrevClassTemplate->getTemplatedDecl(); |
| 1498 | if (!isAcceptableTagRedeclaration(PrevRecordDecl, Kind, |
| 1499 | TUK == TUK_Definition, KWLoc, Name)) { |
| 1500 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
| 1501 | << Name |
| 1502 | << FixItHint::CreateReplacement(KWLoc, PrevRecordDecl->getKindName()); |
| 1503 | Diag(PrevRecordDecl->getLocation(), diag::note_previous_use); |
| 1504 | Kind = PrevRecordDecl->getTagKind(); |
| 1505 | } |
| 1506 | |
| 1507 | |
| 1508 | if (TUK == TUK_Definition) { |
| 1509 | if (TagDecl *Def = PrevRecordDecl->getDefinition()) { |
| 1510 | |
| 1511 | |
| 1512 | NamedDecl *Hidden = nullptr; |
| 1513 | if (SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
| 1514 | SkipBody->ShouldSkip = true; |
| 1515 | SkipBody->Previous = Def; |
| 1516 | auto *Tmpl = cast<CXXRecordDecl>(Hidden)->getDescribedClassTemplate(); |
| 1517 | (0) . __assert_fail ("Tmpl && \"original definition of a class template is not a \" \"class template?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 1518, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Tmpl && "original definition of a class template is not a " |
| 1518 | (0) . __assert_fail ("Tmpl && \"original definition of a class template is not a \" \"class template?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 1518, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "class template?"); |
| 1519 | makeMergedDefinitionVisible(Hidden); |
| 1520 | makeMergedDefinitionVisible(Tmpl); |
| 1521 | } else { |
| 1522 | Diag(NameLoc, diag::err_redefinition) << Name; |
| 1523 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 1524 | |
| 1525 | |
| 1526 | return true; |
| 1527 | } |
| 1528 | } |
| 1529 | } |
| 1530 | } else if (PrevDecl) { |
| 1531 | |
| 1532 | |
| 1533 | |
| 1534 | |
| 1535 | |
| 1536 | Diag(NameLoc, diag::err_redefinition_different_kind) << Name; |
| 1537 | Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| 1538 | return true; |
| 1539 | } |
| 1540 | |
| 1541 | |
| 1542 | |
| 1543 | |
| 1544 | |
| 1545 | if (!(TUK == TUK_Friend && CurContext->isDependentContext()) && |
| 1546 | CheckTemplateParameterList( |
| 1547 | TemplateParams, |
| 1548 | PrevClassTemplate |
| 1549 | ? PrevClassTemplate->getMostRecentDecl()->getTemplateParameters() |
| 1550 | : nullptr, |
| 1551 | (SS.isSet() && SemanticContext && SemanticContext->isRecord() && |
| 1552 | SemanticContext->isDependentContext()) |
| 1553 | ? TPC_ClassTemplateMember |
| 1554 | : TUK == TUK_Friend ? TPC_FriendClassTemplate : TPC_ClassTemplate, |
| 1555 | SkipBody)) |
| 1556 | Invalid = true; |
| 1557 | |
| 1558 | if (SS.isSet()) { |
| 1559 | |
| 1560 | |
| 1561 | if (!SS.isInvalid() && !Invalid && !PrevClassTemplate) { |
| 1562 | Diag(NameLoc, TUK == TUK_Friend ? diag::err_friend_decl_does_not_match |
| 1563 | : diag::err_member_decl_does_not_match) |
| 1564 | << Name << SemanticContext << << SS.getRange(); |
| 1565 | Invalid = true; |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | |
| 1570 | |
| 1571 | |
| 1572 | |
| 1573 | |
| 1574 | bool ShouldAddRedecl |
| 1575 | = !(TUK == TUK_Friend && CurContext->isDependentContext()); |
| 1576 | |
| 1577 | CXXRecordDecl *NewClass = |
| 1578 | CXXRecordDecl::Create(Context, Kind, SemanticContext, KWLoc, NameLoc, Name, |
| 1579 | PrevClassTemplate && ShouldAddRedecl ? |
| 1580 | PrevClassTemplate->getTemplatedDecl() : nullptr, |
| 1581 | ); |
| 1582 | SetNestedNameSpecifier(*this, NewClass, SS); |
| 1583 | if (NumOuterTemplateParamLists > 0) |
| 1584 | NewClass->setTemplateParameterListsInfo( |
| 1585 | Context, llvm::makeArrayRef(OuterTemplateParamLists, |
| 1586 | NumOuterTemplateParamLists)); |
| 1587 | |
| 1588 | |
| 1589 | |
| 1590 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) { |
| 1591 | AddAlignmentAttributesForRecord(NewClass); |
| 1592 | AddMsStructLayoutForRecord(NewClass); |
| 1593 | } |
| 1594 | |
| 1595 | |
| 1596 | |
| 1597 | Expr *const ACtoAttach = |
| 1598 | PrevClassTemplate && ShouldAddRedecl ? nullptr : CurAC; |
| 1599 | |
| 1600 | ClassTemplateDecl *NewTemplate |
| 1601 | = ClassTemplateDecl::Create(Context, SemanticContext, NameLoc, |
| 1602 | DeclarationName(Name), TemplateParams, |
| 1603 | NewClass, ACtoAttach); |
| 1604 | |
| 1605 | if (ShouldAddRedecl) |
| 1606 | NewTemplate->setPreviousDecl(PrevClassTemplate); |
| 1607 | |
| 1608 | NewClass->setDescribedClassTemplate(NewTemplate); |
| 1609 | |
| 1610 | if (ModulePrivateLoc.isValid()) |
| 1611 | NewTemplate->setModulePrivate(); |
| 1612 | |
| 1613 | |
| 1614 | QualType T = NewTemplate->getInjectedClassNameSpecialization(); |
| 1615 | T = Context.getInjectedClassNameType(NewClass, T); |
| 1616 | (0) . __assert_fail ("T->isDependentType() && \"Class template type is not dependent?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 1616, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(T->isDependentType() && "Class template type is not dependent?"); |
| 1617 | (void)T; |
| 1618 | |
| 1619 | |
| 1620 | |
| 1621 | if (PrevClassTemplate && |
| 1622 | PrevClassTemplate->getInstantiatedFromMemberTemplate()) |
| 1623 | PrevClassTemplate->setMemberSpecialization(); |
| 1624 | |
| 1625 | |
| 1626 | if (!Invalid && TUK != TUK_Friend && NewTemplate->getDeclContext()->isRecord()) |
| 1627 | SetMemberAccessSpecifier(NewTemplate, PrevClassTemplate, AS); |
| 1628 | |
| 1629 | |
| 1630 | NewClass->setLexicalDeclContext(CurContext); |
| 1631 | NewTemplate->setLexicalDeclContext(CurContext); |
| 1632 | |
| 1633 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) |
| 1634 | NewClass->startDefinition(); |
| 1635 | |
| 1636 | ProcessDeclAttributeList(S, NewClass, Attr); |
| 1637 | |
| 1638 | if (PrevClassTemplate) |
| 1639 | mergeDeclAttributes(NewClass, PrevClassTemplate->getTemplatedDecl()); |
| 1640 | |
| 1641 | AddPushedVisibilityAttribute(NewClass); |
| 1642 | |
| 1643 | if (TUK != TUK_Friend) { |
| 1644 | |
| 1645 | Scope *Outer = S; |
| 1646 | while ((Outer->getFlags() & Scope::TemplateParamScope) != 0) |
| 1647 | Outer = Outer->getParent(); |
| 1648 | PushOnScopeChains(NewTemplate, Outer); |
| 1649 | } else { |
| 1650 | if (PrevClassTemplate && PrevClassTemplate->getAccess() != AS_none) { |
| 1651 | NewTemplate->setAccess(PrevClassTemplate->getAccess()); |
| 1652 | NewClass->setAccess(PrevClassTemplate->getAccess()); |
| 1653 | } |
| 1654 | |
| 1655 | NewTemplate->setObjectOfFriendDecl(); |
| 1656 | |
| 1657 | |
| 1658 | if (!CurContext->isDependentContext()) { |
| 1659 | DeclContext *DC = SemanticContext->getRedeclContext(); |
| 1660 | DC->makeDeclVisibleInContext(NewTemplate); |
| 1661 | if (Scope *EnclosingScope = getScopeForDeclContext(S, DC)) |
| 1662 | PushOnScopeChains(NewTemplate, EnclosingScope, |
| 1663 | false); |
| 1664 | } |
| 1665 | |
| 1666 | FriendDecl *Friend = FriendDecl::Create( |
| 1667 | Context, CurContext, NewClass->getLocation(), NewTemplate, FriendLoc); |
| 1668 | Friend->setAccess(AS_public); |
| 1669 | CurContext->addDecl(Friend); |
| 1670 | } |
| 1671 | |
| 1672 | if (PrevClassTemplate) |
| 1673 | CheckRedeclarationModuleOwnership(NewTemplate, PrevClassTemplate); |
| 1674 | |
| 1675 | if (Invalid) { |
| 1676 | NewTemplate->setInvalidDecl(); |
| 1677 | NewClass->setInvalidDecl(); |
| 1678 | } |
| 1679 | |
| 1680 | ActOnDocumentableDecl(NewTemplate); |
| 1681 | |
| 1682 | if (SkipBody && SkipBody->ShouldSkip) |
| 1683 | return SkipBody->Previous; |
| 1684 | |
| 1685 | return NewTemplate; |
| 1686 | } |
| 1687 | |
| 1688 | namespace { |
| 1689 | |
| 1690 | |
| 1691 | class |
| 1692 | : public TreeTransform<ExtractTypeForDeductionGuide> { |
| 1693 | public: |
| 1694 | typedef TreeTransform<ExtractTypeForDeductionGuide> ; |
| 1695 | ExtractTypeForDeductionGuide(Sema &SemaRef) : Base(SemaRef) {} |
| 1696 | |
| 1697 | TypeSourceInfo *(TypeSourceInfo *TSI) { return TransformType(TSI); } |
| 1698 | |
| 1699 | QualType (TypeLocBuilder &TLB, TypedefTypeLoc TL) { |
| 1700 | return TransformType( |
| 1701 | TLB, |
| 1702 | TL.getTypedefNameDecl()->getTypeSourceInfo()->getTypeLoc()); |
| 1703 | } |
| 1704 | }; |
| 1705 | |
| 1706 | |
| 1707 | |
| 1708 | struct ConvertConstructorToDeductionGuideTransform { |
| 1709 | ConvertConstructorToDeductionGuideTransform(Sema &S, |
| 1710 | ClassTemplateDecl *Template) |
| 1711 | : SemaRef(S), Template(Template) {} |
| 1712 | |
| 1713 | Sema &SemaRef; |
| 1714 | ClassTemplateDecl *Template; |
| 1715 | |
| 1716 | DeclContext *DC = Template->getDeclContext(); |
| 1717 | CXXRecordDecl *Primary = Template->getTemplatedDecl(); |
| 1718 | DeclarationName DeductionGuideName = |
| 1719 | SemaRef.Context.DeclarationNames.getCXXDeductionGuideName(Template); |
| 1720 | |
| 1721 | QualType DeducedType = SemaRef.Context.getTypeDeclType(Primary); |
| 1722 | |
| 1723 | |
| 1724 | |
| 1725 | unsigned Depth1IndexAdjustment = Template->getTemplateParameters()->size(); |
| 1726 | |
| 1727 | |
| 1728 | NamedDecl *transformConstructor(FunctionTemplateDecl *FTD, |
| 1729 | CXXConstructorDecl *CD) { |
| 1730 | SmallVector<TemplateArgument, 16> SubstArgs; |
| 1731 | |
| 1732 | LocalInstantiationScope Scope(SemaRef); |
| 1733 | |
| 1734 | |
| 1735 | |
| 1736 | |
| 1737 | |
| 1738 | |
| 1739 | |
| 1740 | |
| 1741 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 1742 | if (FTD) { |
| 1743 | TemplateParameterList *InnerParams = FTD->getTemplateParameters(); |
| 1744 | SmallVector<NamedDecl *, 16> AllParams; |
| 1745 | AllParams.reserve(TemplateParams->size() + InnerParams->size()); |
| 1746 | AllParams.insert(AllParams.begin(), |
| 1747 | TemplateParams->begin(), TemplateParams->end()); |
| 1748 | SubstArgs.reserve(InnerParams->size()); |
| 1749 | |
| 1750 | |
| 1751 | |
| 1752 | for (NamedDecl *Param : *InnerParams) { |
| 1753 | MultiLevelTemplateArgumentList Args; |
| 1754 | Args.addOuterTemplateArguments(SubstArgs); |
| 1755 | Args.addOuterRetainedLevel(); |
| 1756 | NamedDecl *NewParam = transformTemplateParameter(Param, Args); |
| 1757 | if (!NewParam) |
| 1758 | return nullptr; |
| 1759 | AllParams.push_back(NewParam); |
| 1760 | SubstArgs.push_back(SemaRef.Context.getCanonicalTemplateArgument( |
| 1761 | SemaRef.Context.getInjectedTemplateArg(NewParam))); |
| 1762 | } |
| 1763 | TemplateParams = TemplateParameterList::Create( |
| 1764 | SemaRef.Context, InnerParams->getTemplateLoc(), |
| 1765 | InnerParams->getLAngleLoc(), AllParams, InnerParams->getRAngleLoc(), |
| 1766 | nullptr); |
| 1767 | } |
| 1768 | |
| 1769 | |
| 1770 | |
| 1771 | |
| 1772 | MultiLevelTemplateArgumentList Args; |
| 1773 | if (FTD) { |
| 1774 | Args.addOuterTemplateArguments(SubstArgs); |
| 1775 | Args.addOuterRetainedLevel(); |
| 1776 | } |
| 1777 | |
| 1778 | FunctionProtoTypeLoc FPTL = CD->getTypeSourceInfo()->getTypeLoc() |
| 1779 | .getAsAdjusted<FunctionProtoTypeLoc>(); |
| 1780 | (0) . __assert_fail ("FPTL && \"no prototype for constructor declaration\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 1780, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(FPTL && "no prototype for constructor declaration"); |
| 1781 | |
| 1782 | |
| 1783 | |
| 1784 | |
| 1785 | TypeLocBuilder TLB; |
| 1786 | SmallVector<ParmVarDecl*, 8> Params; |
| 1787 | QualType NewType = transformFunctionProtoType(TLB, FPTL, Params, Args); |
| 1788 | if (NewType.isNull()) |
| 1789 | return nullptr; |
| 1790 | TypeSourceInfo *NewTInfo = TLB.getTypeSourceInfo(SemaRef.Context, NewType); |
| 1791 | |
| 1792 | return buildDeductionGuide(TemplateParams, CD->isExplicit(), NewTInfo, |
| 1793 | CD->getBeginLoc(), CD->getLocation(), |
| 1794 | CD->getEndLoc()); |
| 1795 | } |
| 1796 | |
| 1797 | |
| 1798 | NamedDecl *buildSimpleDeductionGuide(MutableArrayRef<QualType> ParamTypes) { |
| 1799 | SourceLocation Loc = Template->getLocation(); |
| 1800 | |
| 1801 | |
| 1802 | FunctionProtoType::ExtProtoInfo EPI; |
| 1803 | EPI.HasTrailingReturn = true; |
| 1804 | QualType Result = SemaRef.BuildFunctionType(DeducedType, ParamTypes, Loc, |
| 1805 | DeductionGuideName, EPI); |
| 1806 | TypeSourceInfo *TSI = SemaRef.Context.getTrivialTypeSourceInfo(Result, Loc); |
| 1807 | |
| 1808 | FunctionProtoTypeLoc FPTL = |
| 1809 | TSI->getTypeLoc().castAs<FunctionProtoTypeLoc>(); |
| 1810 | |
| 1811 | |
| 1812 | SmallVector<ParmVarDecl*, 4> Params; |
| 1813 | for (auto T : ParamTypes) { |
| 1814 | ParmVarDecl *NewParam = ParmVarDecl::Create( |
| 1815 | SemaRef.Context, DC, Loc, Loc, nullptr, T, |
| 1816 | SemaRef.Context.getTrivialTypeSourceInfo(T, Loc), SC_None, nullptr); |
| 1817 | NewParam->setScopeInfo(0, Params.size()); |
| 1818 | FPTL.setParam(Params.size(), NewParam); |
| 1819 | Params.push_back(NewParam); |
| 1820 | } |
| 1821 | |
| 1822 | return buildDeductionGuide(Template->getTemplateParameters(), false, TSI, |
| 1823 | Loc, Loc, Loc); |
| 1824 | } |
| 1825 | |
| 1826 | private: |
| 1827 | |
| 1828 | |
| 1829 | |
| 1830 | NamedDecl *transformTemplateParameter(NamedDecl *TemplateParam, |
| 1831 | MultiLevelTemplateArgumentList &Args) { |
| 1832 | if (auto *TTP = dyn_cast<TemplateTypeParmDecl>(TemplateParam)) { |
| 1833 | |
| 1834 | |
| 1835 | auto *NewTTP = TemplateTypeParmDecl::Create( |
| 1836 | SemaRef.Context, DC, TTP->getBeginLoc(), TTP->getLocation(), |
| 1837 | 0, Depth1IndexAdjustment + TTP->getIndex(), |
| 1838 | TTP->getIdentifier(), TTP->wasDeclaredWithTypename(), |
| 1839 | TTP->isParameterPack()); |
| 1840 | if (TTP->hasDefaultArgument()) { |
| 1841 | TypeSourceInfo *InstantiatedDefaultArg = |
| 1842 | SemaRef.SubstType(TTP->getDefaultArgumentInfo(), Args, |
| 1843 | TTP->getDefaultArgumentLoc(), TTP->getDeclName()); |
| 1844 | if (InstantiatedDefaultArg) |
| 1845 | NewTTP->setDefaultArgument(InstantiatedDefaultArg); |
| 1846 | } |
| 1847 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(TemplateParam, |
| 1848 | NewTTP); |
| 1849 | return NewTTP; |
| 1850 | } |
| 1851 | |
| 1852 | if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(TemplateParam)) |
| 1853 | return transformTemplateParameterImpl(TTP, Args); |
| 1854 | |
| 1855 | return transformTemplateParameterImpl( |
| 1856 | cast<NonTypeTemplateParmDecl>(TemplateParam), Args); |
| 1857 | } |
| 1858 | template<typename TemplateParmDecl> |
| 1859 | TemplateParmDecl * |
| 1860 | transformTemplateParameterImpl(TemplateParmDecl *OldParam, |
| 1861 | MultiLevelTemplateArgumentList &Args) { |
| 1862 | |
| 1863 | |
| 1864 | auto *NewParam = |
| 1865 | cast_or_null<TemplateParmDecl>(SemaRef.SubstDecl(OldParam, DC, Args)); |
| 1866 | (0) . __assert_fail ("NewParam->getDepth() == 0 && \"unexpected template param depth\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 1866, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(NewParam->getDepth() == 0 && "unexpected template param depth"); |
| 1867 | NewParam->setPosition(NewParam->getPosition() + Depth1IndexAdjustment); |
| 1868 | return NewParam; |
| 1869 | } |
| 1870 | |
| 1871 | QualType transformFunctionProtoType(TypeLocBuilder &TLB, |
| 1872 | FunctionProtoTypeLoc TL, |
| 1873 | SmallVectorImpl<ParmVarDecl*> &Params, |
| 1874 | MultiLevelTemplateArgumentList &Args) { |
| 1875 | SmallVector<QualType, 4> ParamTypes; |
| 1876 | const FunctionProtoType *T = TL.getTypePtr(); |
| 1877 | |
| 1878 | |
| 1879 | for (auto *OldParam : TL.getParams()) { |
| 1880 | ParmVarDecl *NewParam = transformFunctionTypeParam(OldParam, Args); |
| 1881 | if (!NewParam) |
| 1882 | return QualType(); |
| 1883 | ParamTypes.push_back(NewParam->getType()); |
| 1884 | Params.push_back(NewParam); |
| 1885 | } |
| 1886 | |
| 1887 | |
| 1888 | |
| 1889 | |
| 1890 | |
| 1891 | |
| 1892 | |
| 1893 | |
| 1894 | |
| 1895 | |
| 1896 | QualType ReturnType = DeducedType; |
| 1897 | TLB.pushTypeSpec(ReturnType).setNameLoc(Primary->getLocation()); |
| 1898 | |
| 1899 | |
| 1900 | |
| 1901 | FunctionProtoType::ExtProtoInfo EPI; |
| 1902 | EPI.Variadic = T->isVariadic(); |
| 1903 | EPI.HasTrailingReturn = true; |
| 1904 | |
| 1905 | QualType Result = SemaRef.BuildFunctionType( |
| 1906 | ReturnType, ParamTypes, TL.getBeginLoc(), DeductionGuideName, EPI); |
| 1907 | if (Result.isNull()) |
| 1908 | return QualType(); |
| 1909 | |
| 1910 | FunctionProtoTypeLoc NewTL = TLB.push<FunctionProtoTypeLoc>(Result); |
| 1911 | NewTL.setLocalRangeBegin(TL.getLocalRangeBegin()); |
| 1912 | NewTL.setLParenLoc(TL.getLParenLoc()); |
| 1913 | NewTL.setRParenLoc(TL.getRParenLoc()); |
| 1914 | NewTL.setExceptionSpecRange(SourceRange()); |
| 1915 | NewTL.setLocalRangeEnd(TL.getLocalRangeEnd()); |
| 1916 | for (unsigned I = 0, E = NewTL.getNumParams(); I != E; ++I) |
| 1917 | NewTL.setParam(I, Params[I]); |
| 1918 | |
| 1919 | return Result; |
| 1920 | } |
| 1921 | |
| 1922 | ParmVarDecl * |
| 1923 | transformFunctionTypeParam(ParmVarDecl *OldParam, |
| 1924 | MultiLevelTemplateArgumentList &Args) { |
| 1925 | TypeSourceInfo *OldDI = OldParam->getTypeSourceInfo(); |
| 1926 | TypeSourceInfo *NewDI; |
| 1927 | if (auto PackTL = OldDI->getTypeLoc().getAs<PackExpansionTypeLoc>()) { |
| 1928 | |
| 1929 | Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(SemaRef, 0); |
| 1930 | NewDI = |
| 1931 | SemaRef.SubstType(PackTL.getPatternLoc(), Args, |
| 1932 | OldParam->getLocation(), OldParam->getDeclName()); |
| 1933 | if (!NewDI) return nullptr; |
| 1934 | NewDI = |
| 1935 | SemaRef.CheckPackExpansion(NewDI, PackTL.getEllipsisLoc(), |
| 1936 | PackTL.getTypePtr()->getNumExpansions()); |
| 1937 | } else |
| 1938 | NewDI = SemaRef.SubstType(OldDI, Args, OldParam->getLocation(), |
| 1939 | OldParam->getDeclName()); |
| 1940 | if (!NewDI) |
| 1941 | return nullptr; |
| 1942 | |
| 1943 | |
| 1944 | |
| 1945 | |
| 1946 | |
| 1947 | NewDI = ExtractTypeForDeductionGuide(SemaRef).transform(NewDI); |
| 1948 | |
| 1949 | |
| 1950 | |
| 1951 | ExprResult NewDefArg; |
| 1952 | if (OldParam->hasDefaultArg()) { |
| 1953 | NewDefArg = SemaRef.SubstExpr(OldParam->getDefaultArg(), Args); |
| 1954 | if (NewDefArg.isInvalid()) |
| 1955 | return nullptr; |
| 1956 | } |
| 1957 | |
| 1958 | ParmVarDecl *NewParam = ParmVarDecl::Create(SemaRef.Context, DC, |
| 1959 | OldParam->getInnerLocStart(), |
| 1960 | OldParam->getLocation(), |
| 1961 | OldParam->getIdentifier(), |
| 1962 | NewDI->getType(), |
| 1963 | NewDI, |
| 1964 | OldParam->getStorageClass(), |
| 1965 | NewDefArg.get()); |
| 1966 | NewParam->setScopeInfo(OldParam->getFunctionScopeDepth(), |
| 1967 | OldParam->getFunctionScopeIndex()); |
| 1968 | SemaRef.CurrentInstantiationScope->InstantiatedLocal(OldParam, NewParam); |
| 1969 | return NewParam; |
| 1970 | } |
| 1971 | |
| 1972 | NamedDecl *buildDeductionGuide(TemplateParameterList *TemplateParams, |
| 1973 | bool Explicit, TypeSourceInfo *TInfo, |
| 1974 | SourceLocation LocStart, SourceLocation Loc, |
| 1975 | SourceLocation LocEnd) { |
| 1976 | DeclarationNameInfo Name(DeductionGuideName, Loc); |
| 1977 | ArrayRef<ParmVarDecl *> Params = |
| 1978 | TInfo->getTypeLoc().castAs<FunctionProtoTypeLoc>().getParams(); |
| 1979 | |
| 1980 | |
| 1981 | auto *Guide = |
| 1982 | CXXDeductionGuideDecl::Create(SemaRef.Context, DC, LocStart, Explicit, |
| 1983 | Name, TInfo->getType(), TInfo, LocEnd); |
| 1984 | Guide->setImplicit(); |
| 1985 | Guide->setParams(Params); |
| 1986 | |
| 1987 | for (auto *Param : Params) |
| 1988 | Param->setDeclContext(Guide); |
| 1989 | |
| 1990 | auto *GuideTemplate = FunctionTemplateDecl::Create( |
| 1991 | SemaRef.Context, DC, Loc, DeductionGuideName, TemplateParams, Guide); |
| 1992 | GuideTemplate->setImplicit(); |
| 1993 | Guide->setDescribedFunctionTemplate(GuideTemplate); |
| 1994 | |
| 1995 | if (isa<CXXRecordDecl>(DC)) { |
| 1996 | Guide->setAccess(AS_public); |
| 1997 | GuideTemplate->setAccess(AS_public); |
| 1998 | } |
| 1999 | |
| 2000 | DC->addDecl(GuideTemplate); |
| 2001 | return GuideTemplate; |
| 2002 | } |
| 2003 | }; |
| 2004 | } |
| 2005 | |
| 2006 | void Sema::DeclareImplicitDeductionGuides(TemplateDecl *Template, |
| 2007 | SourceLocation Loc) { |
| 2008 | DeclContext *DC = Template->getDeclContext(); |
| 2009 | if (DC->isDependentContext()) |
| 2010 | return; |
| 2011 | |
| 2012 | ConvertConstructorToDeductionGuideTransform Transform( |
| 2013 | *this, cast<ClassTemplateDecl>(Template)); |
| 2014 | if (!isCompleteType(Loc, Transform.DeducedType)) |
| 2015 | return; |
| 2016 | |
| 2017 | |
| 2018 | |
| 2019 | auto Existing = DC->lookup(Transform.DeductionGuideName); |
| 2020 | for (auto *D : Existing) |
| 2021 | if (D->isImplicit()) |
| 2022 | return; |
| 2023 | |
| 2024 | |
| 2025 | |
| 2026 | ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1); |
| 2027 | |
| 2028 | |
| 2029 | |
| 2030 | |
| 2031 | InstantiatingTemplate BuildingDeductionGuides(*this, Loc, Template); |
| 2032 | if (BuildingDeductionGuides.isInvalid()) |
| 2033 | return; |
| 2034 | |
| 2035 | |
| 2036 | |
| 2037 | |
| 2038 | |
| 2039 | bool AddedAny = false; |
| 2040 | for (NamedDecl *D : LookupConstructors(Transform.Primary)) { |
| 2041 | D = D->getUnderlyingDecl(); |
| 2042 | if (D->isInvalidDecl() || D->isImplicit()) |
| 2043 | continue; |
| 2044 | D = cast<NamedDecl>(D->getCanonicalDecl()); |
| 2045 | |
| 2046 | auto *FTD = dyn_cast<FunctionTemplateDecl>(D); |
| 2047 | auto *CD = |
| 2048 | dyn_cast_or_null<CXXConstructorDecl>(FTD ? FTD->getTemplatedDecl() : D); |
| 2049 | |
| 2050 | |
| 2051 | if (!CD || (!FTD && CD->isFunctionTemplateSpecialization())) |
| 2052 | continue; |
| 2053 | |
| 2054 | Transform.transformConstructor(FTD, CD); |
| 2055 | AddedAny = true; |
| 2056 | } |
| 2057 | |
| 2058 | |
| 2059 | |
| 2060 | |
| 2061 | |
| 2062 | if (!AddedAny) |
| 2063 | Transform.buildSimpleDeductionGuide(None); |
| 2064 | |
| 2065 | |
| 2066 | |
| 2067 | cast<CXXDeductionGuideDecl>( |
| 2068 | cast<FunctionTemplateDecl>( |
| 2069 | Transform.buildSimpleDeductionGuide(Transform.DeducedType)) |
| 2070 | ->getTemplatedDecl()) |
| 2071 | ->setIsCopyDeductionCandidate(); |
| 2072 | } |
| 2073 | |
| 2074 | |
| 2075 | |
| 2076 | |
| 2077 | |
| 2078 | static bool DiagnoseDefaultTemplateArgument(Sema &S, |
| 2079 | Sema::TemplateParamListContext TPC, |
| 2080 | SourceLocation ParamLoc, |
| 2081 | SourceRange DefArgRange) { |
| 2082 | switch (TPC) { |
| 2083 | case Sema::TPC_ClassTemplate: |
| 2084 | case Sema::TPC_VarTemplate: |
| 2085 | case Sema::TPC_TypeAliasTemplate: |
| 2086 | return false; |
| 2087 | |
| 2088 | case Sema::TPC_FunctionTemplate: |
| 2089 | case Sema::TPC_FriendFunctionTemplateDefinition: |
| 2090 | |
| 2091 | |
| 2092 | |
| 2093 | |
| 2094 | |
| 2095 | |
| 2096 | |
| 2097 | |
| 2098 | S.Diag(ParamLoc, S.getLangOpts().CPlusPlus11 ? |
| 2099 | diag::warn_cxx98_compat_template_parameter_default_in_function_template |
| 2100 | : diag::ext_template_parameter_default_in_function_template) |
| 2101 | << DefArgRange; |
| 2102 | return false; |
| 2103 | |
| 2104 | case Sema::TPC_ClassTemplateMember: |
| 2105 | |
| 2106 | |
| 2107 | |
| 2108 | |
| 2109 | S.Diag(ParamLoc, diag::err_template_parameter_default_template_member) |
| 2110 | << DefArgRange; |
| 2111 | return true; |
| 2112 | |
| 2113 | case Sema::TPC_FriendClassTemplate: |
| 2114 | case Sema::TPC_FriendFunctionTemplate: |
| 2115 | |
| 2116 | |
| 2117 | |
| 2118 | S.Diag(ParamLoc, diag::err_template_parameter_default_friend_template) |
| 2119 | << DefArgRange; |
| 2120 | return true; |
| 2121 | |
| 2122 | |
| 2123 | |
| 2124 | |
| 2125 | } |
| 2126 | |
| 2127 | llvm_unreachable("Invalid TemplateParamListContext!"); |
| 2128 | } |
| 2129 | |
| 2130 | |
| 2131 | |
| 2132 | static bool DiagnoseUnexpandedParameterPacks(Sema &S, |
| 2133 | TemplateTemplateParmDecl *TTP) { |
| 2134 | |
| 2135 | |
| 2136 | if (TTP->isParameterPack()) |
| 2137 | return false; |
| 2138 | |
| 2139 | TemplateParameterList *Params = TTP->getTemplateParameters(); |
| 2140 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 2141 | NamedDecl *P = Params->getParam(I); |
| 2142 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
| 2143 | if (!NTTP->isParameterPack() && |
| 2144 | S.DiagnoseUnexpandedParameterPack(NTTP->getLocation(), |
| 2145 | NTTP->getTypeSourceInfo(), |
| 2146 | Sema::UPPC_NonTypeTemplateParameterType)) |
| 2147 | return true; |
| 2148 | |
| 2149 | continue; |
| 2150 | } |
| 2151 | |
| 2152 | if (TemplateTemplateParmDecl *InnerTTP |
| 2153 | = dyn_cast<TemplateTemplateParmDecl>(P)) |
| 2154 | if (DiagnoseUnexpandedParameterPacks(S, InnerTTP)) |
| 2155 | return true; |
| 2156 | } |
| 2157 | |
| 2158 | return false; |
| 2159 | } |
| 2160 | |
| 2161 | |
| 2162 | |
| 2163 | |
| 2164 | |
| 2165 | |
| 2166 | |
| 2167 | |
| 2168 | |
| 2169 | |
| 2170 | |
| 2171 | |
| 2172 | |
| 2173 | |
| 2174 | |
| 2175 | |
| 2176 | |
| 2177 | |
| 2178 | |
| 2179 | |
| 2180 | |
| 2181 | |
| 2182 | |
| 2183 | |
| 2184 | |
| 2185 | |
| 2186 | |
| 2187 | |
| 2188 | |
| 2189 | bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, |
| 2190 | TemplateParameterList *OldParams, |
| 2191 | TemplateParamListContext TPC, |
| 2192 | SkipBodyInfo *SkipBody) { |
| 2193 | bool Invalid = false; |
| 2194 | |
| 2195 | |
| 2196 | |
| 2197 | |
| 2198 | |
| 2199 | |
| 2200 | |
| 2201 | bool SawDefaultArgument = false; |
| 2202 | SourceLocation PreviousDefaultArgLoc; |
| 2203 | |
| 2204 | |
| 2205 | TemplateParameterList::iterator OldParam = NewParams->end(); |
| 2206 | if (OldParams) |
| 2207 | OldParam = OldParams->begin(); |
| 2208 | |
| 2209 | bool RemoveDefaultArguments = false; |
| 2210 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 2211 | NewParamEnd = NewParams->end(); |
| 2212 | NewParam != NewParamEnd; ++NewParam) { |
| 2213 | |
| 2214 | bool RedundantDefaultArg = false; |
| 2215 | SourceLocation OldDefaultLoc; |
| 2216 | SourceLocation NewDefaultLoc; |
| 2217 | |
| 2218 | |
| 2219 | bool MissingDefaultArg = false; |
| 2220 | |
| 2221 | |
| 2222 | bool SawParameterPack = false; |
| 2223 | |
| 2224 | if (TemplateTypeParmDecl *NewTypeParm |
| 2225 | = dyn_cast<TemplateTypeParmDecl>(*NewParam)) { |
| 2226 | |
| 2227 | if (NewTypeParm->hasDefaultArgument() && |
| 2228 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 2229 | NewTypeParm->getLocation(), |
| 2230 | NewTypeParm->getDefaultArgumentInfo()->getTypeLoc() |
| 2231 | .getSourceRange())) |
| 2232 | NewTypeParm->removeDefaultArgument(); |
| 2233 | |
| 2234 | |
| 2235 | TemplateTypeParmDecl *OldTypeParm |
| 2236 | = OldParams? cast<TemplateTypeParmDecl>(*OldParam) : nullptr; |
| 2237 | if (NewTypeParm->isParameterPack()) { |
| 2238 | (0) . __assert_fail ("!NewTypeParm->hasDefaultArgument() && \"Parameter packs can't have a default argument!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 2239, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!NewTypeParm->hasDefaultArgument() && |
| 2239 | (0) . __assert_fail ("!NewTypeParm->hasDefaultArgument() && \"Parameter packs can't have a default argument!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 2239, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Parameter packs can't have a default argument!"); |
| 2240 | SawParameterPack = true; |
| 2241 | } else if (OldTypeParm && hasVisibleDefaultArgument(OldTypeParm) && |
| 2242 | NewTypeParm->hasDefaultArgument() && |
| 2243 | (!SkipBody || !SkipBody->ShouldSkip)) { |
| 2244 | OldDefaultLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 2245 | NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 2246 | SawDefaultArgument = true; |
| 2247 | RedundantDefaultArg = true; |
| 2248 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 2249 | } else if (OldTypeParm && OldTypeParm->hasDefaultArgument()) { |
| 2250 | |
| 2251 | |
| 2252 | NewTypeParm->setInheritedDefaultArgument(Context, OldTypeParm); |
| 2253 | PreviousDefaultArgLoc = OldTypeParm->getDefaultArgumentLoc(); |
| 2254 | } else if (NewTypeParm->hasDefaultArgument()) { |
| 2255 | SawDefaultArgument = true; |
| 2256 | PreviousDefaultArgLoc = NewTypeParm->getDefaultArgumentLoc(); |
| 2257 | } else if (SawDefaultArgument) |
| 2258 | MissingDefaultArg = true; |
| 2259 | } else if (NonTypeTemplateParmDecl *NewNonTypeParm |
| 2260 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) { |
| 2261 | |
| 2262 | if (!NewNonTypeParm->isParameterPack() && |
| 2263 | DiagnoseUnexpandedParameterPack(NewNonTypeParm->getLocation(), |
| 2264 | NewNonTypeParm->getTypeSourceInfo(), |
| 2265 | UPPC_NonTypeTemplateParameterType)) { |
| 2266 | Invalid = true; |
| 2267 | continue; |
| 2268 | } |
| 2269 | |
| 2270 | |
| 2271 | if (NewNonTypeParm->hasDefaultArgument() && |
| 2272 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 2273 | NewNonTypeParm->getLocation(), |
| 2274 | NewNonTypeParm->getDefaultArgument()->getSourceRange())) { |
| 2275 | NewNonTypeParm->removeDefaultArgument(); |
| 2276 | } |
| 2277 | |
| 2278 | |
| 2279 | NonTypeTemplateParmDecl *OldNonTypeParm |
| 2280 | = OldParams? cast<NonTypeTemplateParmDecl>(*OldParam) : nullptr; |
| 2281 | if (NewNonTypeParm->isParameterPack()) { |
| 2282 | (0) . __assert_fail ("!NewNonTypeParm->hasDefaultArgument() && \"Parameter packs can't have a default argument!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 2283, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!NewNonTypeParm->hasDefaultArgument() && |
| 2283 | (0) . __assert_fail ("!NewNonTypeParm->hasDefaultArgument() && \"Parameter packs can't have a default argument!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 2283, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Parameter packs can't have a default argument!"); |
| 2284 | if (!NewNonTypeParm->isPackExpansion()) |
| 2285 | SawParameterPack = true; |
| 2286 | } else if (OldNonTypeParm && hasVisibleDefaultArgument(OldNonTypeParm) && |
| 2287 | NewNonTypeParm->hasDefaultArgument() && |
| 2288 | (!SkipBody || !SkipBody->ShouldSkip)) { |
| 2289 | OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 2290 | NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 2291 | SawDefaultArgument = true; |
| 2292 | RedundantDefaultArg = true; |
| 2293 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 2294 | } else if (OldNonTypeParm && OldNonTypeParm->hasDefaultArgument()) { |
| 2295 | |
| 2296 | |
| 2297 | NewNonTypeParm->setInheritedDefaultArgument(Context, OldNonTypeParm); |
| 2298 | PreviousDefaultArgLoc = OldNonTypeParm->getDefaultArgumentLoc(); |
| 2299 | } else if (NewNonTypeParm->hasDefaultArgument()) { |
| 2300 | SawDefaultArgument = true; |
| 2301 | PreviousDefaultArgLoc = NewNonTypeParm->getDefaultArgumentLoc(); |
| 2302 | } else if (SawDefaultArgument) |
| 2303 | MissingDefaultArg = true; |
| 2304 | } else { |
| 2305 | TemplateTemplateParmDecl *NewTemplateParm |
| 2306 | = cast<TemplateTemplateParmDecl>(*NewParam); |
| 2307 | |
| 2308 | |
| 2309 | if (::DiagnoseUnexpandedParameterPacks(*this, NewTemplateParm)) { |
| 2310 | Invalid = true; |
| 2311 | continue; |
| 2312 | } |
| 2313 | |
| 2314 | |
| 2315 | if (NewTemplateParm->hasDefaultArgument() && |
| 2316 | DiagnoseDefaultTemplateArgument(*this, TPC, |
| 2317 | NewTemplateParm->getLocation(), |
| 2318 | NewTemplateParm->getDefaultArgument().getSourceRange())) |
| 2319 | NewTemplateParm->removeDefaultArgument(); |
| 2320 | |
| 2321 | |
| 2322 | TemplateTemplateParmDecl *OldTemplateParm |
| 2323 | = OldParams? cast<TemplateTemplateParmDecl>(*OldParam) : nullptr; |
| 2324 | if (NewTemplateParm->isParameterPack()) { |
| 2325 | (0) . __assert_fail ("!NewTemplateParm->hasDefaultArgument() && \"Parameter packs can't have a default argument!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 2326, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!NewTemplateParm->hasDefaultArgument() && |
| 2326 | (0) . __assert_fail ("!NewTemplateParm->hasDefaultArgument() && \"Parameter packs can't have a default argument!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 2326, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Parameter packs can't have a default argument!"); |
| 2327 | if (!NewTemplateParm->isPackExpansion()) |
| 2328 | SawParameterPack = true; |
| 2329 | } else if (OldTemplateParm && |
| 2330 | hasVisibleDefaultArgument(OldTemplateParm) && |
| 2331 | NewTemplateParm->hasDefaultArgument() && |
| 2332 | (!SkipBody || !SkipBody->ShouldSkip)) { |
| 2333 | OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); |
| 2334 | NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); |
| 2335 | SawDefaultArgument = true; |
| 2336 | RedundantDefaultArg = true; |
| 2337 | PreviousDefaultArgLoc = NewDefaultLoc; |
| 2338 | } else if (OldTemplateParm && OldTemplateParm->hasDefaultArgument()) { |
| 2339 | |
| 2340 | |
| 2341 | NewTemplateParm->setInheritedDefaultArgument(Context, OldTemplateParm); |
| 2342 | PreviousDefaultArgLoc |
| 2343 | = OldTemplateParm->getDefaultArgument().getLocation(); |
| 2344 | } else if (NewTemplateParm->hasDefaultArgument()) { |
| 2345 | SawDefaultArgument = true; |
| 2346 | PreviousDefaultArgLoc |
| 2347 | = NewTemplateParm->getDefaultArgument().getLocation(); |
| 2348 | } else if (SawDefaultArgument) |
| 2349 | MissingDefaultArg = true; |
| 2350 | } |
| 2351 | |
| 2352 | |
| 2353 | |
| 2354 | |
| 2355 | if (SawParameterPack && (NewParam + 1) != NewParamEnd && |
| 2356 | (TPC == TPC_ClassTemplate || TPC == TPC_VarTemplate || |
| 2357 | TPC == TPC_TypeAliasTemplate)) { |
| 2358 | Diag((*NewParam)->getLocation(), |
| 2359 | diag::err_template_param_pack_must_be_last_template_parameter); |
| 2360 | Invalid = true; |
| 2361 | } |
| 2362 | |
| 2363 | if (RedundantDefaultArg) { |
| 2364 | |
| 2365 | |
| 2366 | |
| 2367 | Diag(NewDefaultLoc, diag::err_template_param_default_arg_redefinition); |
| 2368 | Diag(OldDefaultLoc, diag::note_template_param_prev_default_arg); |
| 2369 | Invalid = true; |
| 2370 | } else if (MissingDefaultArg && TPC != TPC_FunctionTemplate) { |
| 2371 | |
| 2372 | |
| 2373 | |
| 2374 | |
| 2375 | |
| 2376 | Diag((*NewParam)->getLocation(), |
| 2377 | diag::err_template_param_default_arg_missing); |
| 2378 | Diag(PreviousDefaultArgLoc, diag::note_template_param_prev_default_arg); |
| 2379 | Invalid = true; |
| 2380 | RemoveDefaultArguments = true; |
| 2381 | } |
| 2382 | |
| 2383 | |
| 2384 | |
| 2385 | if (OldParams) |
| 2386 | ++OldParam; |
| 2387 | } |
| 2388 | |
| 2389 | |
| 2390 | |
| 2391 | if (RemoveDefaultArguments) { |
| 2392 | for (TemplateParameterList::iterator NewParam = NewParams->begin(), |
| 2393 | NewParamEnd = NewParams->end(); |
| 2394 | NewParam != NewParamEnd; ++NewParam) { |
| 2395 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*NewParam)) |
| 2396 | TTP->removeDefaultArgument(); |
| 2397 | else if (NonTypeTemplateParmDecl *NTTP |
| 2398 | = dyn_cast<NonTypeTemplateParmDecl>(*NewParam)) |
| 2399 | NTTP->removeDefaultArgument(); |
| 2400 | else |
| 2401 | cast<TemplateTemplateParmDecl>(*NewParam)->removeDefaultArgument(); |
| 2402 | } |
| 2403 | } |
| 2404 | |
| 2405 | return Invalid; |
| 2406 | } |
| 2407 | |
| 2408 | namespace { |
| 2409 | |
| 2410 | |
| 2411 | |
| 2412 | struct DependencyChecker : RecursiveASTVisitor<DependencyChecker> { |
| 2413 | typedef RecursiveASTVisitor<DependencyChecker> super; |
| 2414 | |
| 2415 | unsigned Depth; |
| 2416 | |
| 2417 | |
| 2418 | |
| 2419 | |
| 2420 | |
| 2421 | bool IgnoreNonTypeDependent; |
| 2422 | |
| 2423 | bool Match; |
| 2424 | SourceLocation MatchLoc; |
| 2425 | |
| 2426 | DependencyChecker(unsigned Depth, bool IgnoreNonTypeDependent) |
| 2427 | : Depth(Depth), IgnoreNonTypeDependent(IgnoreNonTypeDependent), |
| 2428 | Match(false) {} |
| 2429 | |
| 2430 | DependencyChecker(TemplateParameterList *Params, bool IgnoreNonTypeDependent) |
| 2431 | : IgnoreNonTypeDependent(IgnoreNonTypeDependent), Match(false) { |
| 2432 | NamedDecl *ND = Params->getParam(0); |
| 2433 | if (TemplateTypeParmDecl *PD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 2434 | Depth = PD->getDepth(); |
| 2435 | } else if (NonTypeTemplateParmDecl *PD = |
| 2436 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 2437 | Depth = PD->getDepth(); |
| 2438 | } else { |
| 2439 | Depth = cast<TemplateTemplateParmDecl>(ND)->getDepth(); |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | bool Matches(unsigned ParmDepth, SourceLocation Loc = SourceLocation()) { |
| 2444 | if (ParmDepth >= Depth) { |
| 2445 | Match = true; |
| 2446 | MatchLoc = Loc; |
| 2447 | return true; |
| 2448 | } |
| 2449 | return false; |
| 2450 | } |
| 2451 | |
| 2452 | bool TraverseStmt(Stmt *S, DataRecursionQueue *Q = nullptr) { |
| 2453 | |
| 2454 | |
| 2455 | |
| 2456 | |
| 2457 | if (auto *E = dyn_cast_or_null<Expr>(S)) |
| 2458 | if (IgnoreNonTypeDependent && !E->isTypeDependent()) |
| 2459 | return true; |
| 2460 | return super::TraverseStmt(S, Q); |
| 2461 | } |
| 2462 | |
| 2463 | bool TraverseTypeLoc(TypeLoc TL) { |
| 2464 | if (IgnoreNonTypeDependent && !TL.isNull() && |
| 2465 | !TL.getType()->isDependentType()) |
| 2466 | return true; |
| 2467 | return super::TraverseTypeLoc(TL); |
| 2468 | } |
| 2469 | |
| 2470 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) { |
| 2471 | return !Matches(TL.getTypePtr()->getDepth(), TL.getNameLoc()); |
| 2472 | } |
| 2473 | |
| 2474 | bool VisitTemplateTypeParmType(const TemplateTypeParmType *T) { |
| 2475 | |
| 2476 | return IgnoreNonTypeDependent || !Matches(T->getDepth()); |
| 2477 | } |
| 2478 | |
| 2479 | bool TraverseTemplateName(TemplateName N) { |
| 2480 | if (TemplateTemplateParmDecl *PD = |
| 2481 | dyn_cast_or_null<TemplateTemplateParmDecl>(N.getAsTemplateDecl())) |
| 2482 | if (Matches(PD->getDepth())) |
| 2483 | return false; |
| 2484 | return super::TraverseTemplateName(N); |
| 2485 | } |
| 2486 | |
| 2487 | bool VisitDeclRefExpr(DeclRefExpr *E) { |
| 2488 | if (NonTypeTemplateParmDecl *PD = |
| 2489 | dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) |
| 2490 | if (Matches(PD->getDepth(), E->getExprLoc())) |
| 2491 | return false; |
| 2492 | return super::VisitDeclRefExpr(E); |
| 2493 | } |
| 2494 | |
| 2495 | bool VisitSubstTemplateTypeParmType(const SubstTemplateTypeParmType *T) { |
| 2496 | return TraverseType(T->getReplacementType()); |
| 2497 | } |
| 2498 | |
| 2499 | bool |
| 2500 | VisitSubstTemplateTypeParmPackType(const SubstTemplateTypeParmPackType *T) { |
| 2501 | return TraverseTemplateArgument(T->getArgumentPack()); |
| 2502 | } |
| 2503 | |
| 2504 | bool TraverseInjectedClassNameType(const InjectedClassNameType *T) { |
| 2505 | return TraverseType(T->getInjectedSpecializationType()); |
| 2506 | } |
| 2507 | }; |
| 2508 | } |
| 2509 | |
| 2510 | |
| 2511 | |
| 2512 | static bool |
| 2513 | DependsOnTemplateParameters(QualType T, TemplateParameterList *Params) { |
| 2514 | DependencyChecker Checker(Params, ); |
| 2515 | Checker.TraverseType(T); |
| 2516 | return Checker.Match; |
| 2517 | } |
| 2518 | |
| 2519 | |
| 2520 | |
| 2521 | static SourceRange getRangeOfTypeInNestedNameSpecifier(ASTContext &Context, |
| 2522 | QualType T, |
| 2523 | const CXXScopeSpec &SS) { |
| 2524 | NestedNameSpecifierLoc NNSLoc(SS.getScopeRep(), SS.location_data()); |
| 2525 | while (NestedNameSpecifier *NNS = NNSLoc.getNestedNameSpecifier()) { |
| 2526 | if (const Type *CurType = NNS->getAsType()) { |
| 2527 | if (Context.hasSameUnqualifiedType(T, QualType(CurType, 0))) |
| 2528 | return NNSLoc.getTypeLoc().getSourceRange(); |
| 2529 | } else |
| 2530 | break; |
| 2531 | |
| 2532 | NNSLoc = NNSLoc.getPrefix(); |
| 2533 | } |
| 2534 | |
| 2535 | return SourceRange(); |
| 2536 | } |
| 2537 | |
| 2538 | |
| 2539 | |
| 2540 | |
| 2541 | |
| 2542 | |
| 2543 | |
| 2544 | |
| 2545 | |
| 2546 | |
| 2547 | |
| 2548 | |
| 2549 | |
| 2550 | |
| 2551 | |
| 2552 | |
| 2553 | |
| 2554 | |
| 2555 | |
| 2556 | |
| 2557 | |
| 2558 | |
| 2559 | |
| 2560 | |
| 2561 | |
| 2562 | |
| 2563 | |
| 2564 | |
| 2565 | |
| 2566 | |
| 2567 | |
| 2568 | |
| 2569 | |
| 2570 | |
| 2571 | TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( |
| 2572 | SourceLocation DeclStartLoc, SourceLocation DeclLoc, const CXXScopeSpec &SS, |
| 2573 | TemplateIdAnnotation *TemplateId, |
| 2574 | ArrayRef<TemplateParameterList *> ParamLists, bool IsFriend, |
| 2575 | bool &IsMemberSpecialization, bool &Invalid) { |
| 2576 | IsMemberSpecialization = false; |
| 2577 | Invalid = false; |
| 2578 | |
| 2579 | |
| 2580 | |
| 2581 | |
| 2582 | SmallVector<QualType, 4> NestedTypes; |
| 2583 | QualType T; |
| 2584 | if (SS.getScopeRep()) { |
| 2585 | if (CXXRecordDecl *Record |
| 2586 | = dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, true))) |
| 2587 | T = Context.getTypeDeclType(Record); |
| 2588 | else |
| 2589 | T = QualType(SS.getScopeRep()->getAsType(), 0); |
| 2590 | } |
| 2591 | |
| 2592 | |
| 2593 | |
| 2594 | |
| 2595 | SourceLocation ExplicitSpecLoc; |
| 2596 | |
| 2597 | while (!T.isNull()) { |
| 2598 | NestedTypes.push_back(T); |
| 2599 | |
| 2600 | |
| 2601 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 2602 | |
| 2603 | if (ClassTemplateSpecializationDecl *Spec |
| 2604 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 2605 | if (!isa<ClassTemplatePartialSpecializationDecl>(Spec) && |
| 2606 | Spec->getSpecializationKind() == TSK_ExplicitSpecialization) { |
| 2607 | ExplicitSpecLoc = Spec->getLocation(); |
| 2608 | break; |
| 2609 | } |
| 2610 | } else if (Record->getTemplateSpecializationKind() |
| 2611 | == TSK_ExplicitSpecialization) { |
| 2612 | ExplicitSpecLoc = Record->getLocation(); |
| 2613 | break; |
| 2614 | } |
| 2615 | |
| 2616 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Record->getParent())) |
| 2617 | T = Context.getTypeDeclType(Parent); |
| 2618 | else |
| 2619 | T = QualType(); |
| 2620 | continue; |
| 2621 | } |
| 2622 | |
| 2623 | if (const TemplateSpecializationType *TST |
| 2624 | = T->getAs<TemplateSpecializationType>()) { |
| 2625 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 2626 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Template->getDeclContext())) |
| 2627 | T = Context.getTypeDeclType(Parent); |
| 2628 | else |
| 2629 | T = QualType(); |
| 2630 | continue; |
| 2631 | } |
| 2632 | } |
| 2633 | |
| 2634 | |
| 2635 | if (const DependentTemplateSpecializationType *DependentTST |
| 2636 | = T->getAs<DependentTemplateSpecializationType>()) { |
| 2637 | if (NestedNameSpecifier *NNS = DependentTST->getQualifier()) |
| 2638 | T = QualType(NNS->getAsType(), 0); |
| 2639 | else |
| 2640 | T = QualType(); |
| 2641 | continue; |
| 2642 | } |
| 2643 | |
| 2644 | |
| 2645 | if (const DependentNameType *DependentName = T->getAs<DependentNameType>()){ |
| 2646 | if (NestedNameSpecifier *NNS = DependentName->getQualifier()) |
| 2647 | T = QualType(NNS->getAsType(), 0); |
| 2648 | else |
| 2649 | T = QualType(); |
| 2650 | continue; |
| 2651 | } |
| 2652 | |
| 2653 | |
| 2654 | if (const EnumType *EnumT = T->getAs<EnumType>()) { |
| 2655 | |
| 2656 | |
| 2657 | EnumDecl *Enum = EnumT->getDecl(); |
| 2658 | |
| 2659 | |
| 2660 | if (TypeDecl *Parent = dyn_cast<TypeDecl>(Enum->getParent())) |
| 2661 | T = Context.getTypeDeclType(Parent); |
| 2662 | else |
| 2663 | T = QualType(); |
| 2664 | continue; |
| 2665 | } |
| 2666 | |
| 2667 | T = QualType(); |
| 2668 | } |
| 2669 | |
| 2670 | |
| 2671 | std::reverse(NestedTypes.begin(), NestedTypes.end()); |
| 2672 | |
| 2673 | |
| 2674 | |
| 2675 | |
| 2676 | |
| 2677 | |
| 2678 | |
| 2679 | bool SawNonEmptyTemplateParameterList = false; |
| 2680 | |
| 2681 | auto CheckExplicitSpecialization = [&](SourceRange Range, bool Recovery) { |
| 2682 | if (SawNonEmptyTemplateParameterList) { |
| 2683 | Diag(DeclLoc, diag::err_specialize_member_of_template) |
| 2684 | << !Recovery << Range; |
| 2685 | Invalid = true; |
| 2686 | IsMemberSpecialization = false; |
| 2687 | return true; |
| 2688 | } |
| 2689 | |
| 2690 | return false; |
| 2691 | }; |
| 2692 | |
| 2693 | auto DiagnoseMissingExplicitSpecialization = [&] (SourceRange Range) { |
| 2694 | |
| 2695 | if (CheckExplicitSpecialization(Range, true)) |
| 2696 | return true; |
| 2697 | |
| 2698 | |
| 2699 | SourceLocation ExpectedTemplateLoc; |
| 2700 | if (!ParamLists.empty()) |
| 2701 | ExpectedTemplateLoc = ParamLists[0]->getTemplateLoc(); |
| 2702 | else |
| 2703 | ExpectedTemplateLoc = DeclStartLoc; |
| 2704 | |
| 2705 | Diag(DeclLoc, diag::err_template_spec_needs_header) |
| 2706 | << Range |
| 2707 | << FixItHint::CreateInsertion(ExpectedTemplateLoc, "template<> "); |
| 2708 | return false; |
| 2709 | }; |
| 2710 | |
| 2711 | unsigned ParamIdx = 0; |
| 2712 | for (unsigned TypeIdx = 0, NumTypes = NestedTypes.size(); TypeIdx != NumTypes; |
| 2713 | ++TypeIdx) { |
| 2714 | T = NestedTypes[TypeIdx]; |
| 2715 | |
| 2716 | |
| 2717 | bool = false; |
| 2718 | |
| 2719 | |
| 2720 | bool = false; |
| 2721 | |
| 2722 | |
| 2723 | |
| 2724 | TemplateParameterList *ExpectedTemplateParams = nullptr; |
| 2725 | |
| 2726 | |
| 2727 | |
| 2728 | |
| 2729 | |
| 2730 | |
| 2731 | if (CXXRecordDecl *Record = T->getAsCXXRecordDecl()) { |
| 2732 | if (ClassTemplatePartialSpecializationDecl *Partial |
| 2733 | = dyn_cast<ClassTemplatePartialSpecializationDecl>(Record)) { |
| 2734 | ExpectedTemplateParams = Partial->getTemplateParameters(); |
| 2735 | NeedNonemptyTemplateHeader = true; |
| 2736 | } else if (Record->isDependentType()) { |
| 2737 | if (Record->getDescribedClassTemplate()) { |
| 2738 | ExpectedTemplateParams = Record->getDescribedClassTemplate() |
| 2739 | ->getTemplateParameters(); |
| 2740 | NeedNonemptyTemplateHeader = true; |
| 2741 | } |
| 2742 | } else if (ClassTemplateSpecializationDecl *Spec |
| 2743 | = dyn_cast<ClassTemplateSpecializationDecl>(Record)) { |
| 2744 | |
| 2745 | |
| 2746 | |
| 2747 | |
| 2748 | if (Spec->getSpecializationKind() != TSK_ExplicitSpecialization) |
| 2749 | NeedEmptyTemplateHeader = true; |
| 2750 | else |
| 2751 | continue; |
| 2752 | } else if (Record->getTemplateSpecializationKind()) { |
| 2753 | if (Record->getTemplateSpecializationKind() |
| 2754 | != TSK_ExplicitSpecialization && |
| 2755 | TypeIdx == NumTypes - 1) |
| 2756 | IsMemberSpecialization = true; |
| 2757 | |
| 2758 | continue; |
| 2759 | } |
| 2760 | } else if (const TemplateSpecializationType *TST |
| 2761 | = T->getAs<TemplateSpecializationType>()) { |
| 2762 | if (TemplateDecl *Template = TST->getTemplateName().getAsTemplateDecl()) { |
| 2763 | ExpectedTemplateParams = Template->getTemplateParameters(); |
| 2764 | NeedNonemptyTemplateHeader = true; |
| 2765 | } |
| 2766 | } else if (T->getAs<DependentTemplateSpecializationType>()) { |
| 2767 | |
| 2768 | |
| 2769 | NeedNonemptyTemplateHeader = false; |
| 2770 | } |
| 2771 | |
| 2772 | |
| 2773 | |
| 2774 | |
| 2775 | |
| 2776 | |
| 2777 | |
| 2778 | |
| 2779 | if (ParamIdx < ParamLists.size()) { |
| 2780 | if (ParamLists[ParamIdx]->size() == 0) { |
| 2781 | if (CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2782 | false)) |
| 2783 | return nullptr; |
| 2784 | } else |
| 2785 | SawNonEmptyTemplateParameterList = true; |
| 2786 | } |
| 2787 | |
| 2788 | if (NeedEmptyTemplateHeader) { |
| 2789 | |
| 2790 | |
| 2791 | if (TypeIdx == NumTypes - 1) |
| 2792 | IsMemberSpecialization = true; |
| 2793 | |
| 2794 | if (ParamIdx < ParamLists.size()) { |
| 2795 | if (ParamLists[ParamIdx]->size() > 0) { |
| 2796 | |
| 2797 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 2798 | diag::err_template_param_list_matches_nontemplate) |
| 2799 | << T |
| 2800 | << SourceRange(ParamLists[ParamIdx]->getLAngleLoc(), |
| 2801 | ParamLists[ParamIdx]->getRAngleLoc()) |
| 2802 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2803 | Invalid = true; |
| 2804 | return nullptr; |
| 2805 | } |
| 2806 | |
| 2807 | |
| 2808 | ++ParamIdx; |
| 2809 | continue; |
| 2810 | } |
| 2811 | |
| 2812 | if (!IsFriend) |
| 2813 | if (DiagnoseMissingExplicitSpecialization( |
| 2814 | getRangeOfTypeInNestedNameSpecifier(Context, T, SS))) |
| 2815 | return nullptr; |
| 2816 | |
| 2817 | continue; |
| 2818 | } |
| 2819 | |
| 2820 | if (NeedNonemptyTemplateHeader) { |
| 2821 | |
| 2822 | |
| 2823 | |
| 2824 | |
| 2825 | if (IsFriend && T->isDependentType()) { |
| 2826 | if (ParamIdx < ParamLists.size() && |
| 2827 | DependsOnTemplateParameters(T, ParamLists[ParamIdx])) |
| 2828 | ExpectedTemplateParams = nullptr; |
| 2829 | else |
| 2830 | continue; |
| 2831 | } |
| 2832 | |
| 2833 | if (ParamIdx < ParamLists.size()) { |
| 2834 | |
| 2835 | if (ExpectedTemplateParams && |
| 2836 | !TemplateParameterListsAreEqual(ParamLists[ParamIdx], |
| 2837 | ExpectedTemplateParams, |
| 2838 | true, TPL_TemplateMatch)) |
| 2839 | Invalid = true; |
| 2840 | |
| 2841 | if (!Invalid && |
| 2842 | CheckTemplateParameterList(ParamLists[ParamIdx], nullptr, |
| 2843 | TPC_ClassTemplateMember)) |
| 2844 | Invalid = true; |
| 2845 | |
| 2846 | ++ParamIdx; |
| 2847 | continue; |
| 2848 | } |
| 2849 | |
| 2850 | Diag(DeclLoc, diag::err_template_spec_needs_template_parameters) |
| 2851 | << T |
| 2852 | << getRangeOfTypeInNestedNameSpecifier(Context, T, SS); |
| 2853 | Invalid = true; |
| 2854 | continue; |
| 2855 | } |
| 2856 | } |
| 2857 | |
| 2858 | |
| 2859 | |
| 2860 | |
| 2861 | if (ParamIdx >= ParamLists.size()) { |
| 2862 | if (TemplateId && !IsFriend) { |
| 2863 | |
| 2864 | |
| 2865 | DiagnoseMissingExplicitSpecialization(SourceRange(TemplateId->LAngleLoc, |
| 2866 | TemplateId->RAngleLoc)); |
| 2867 | |
| 2868 | |
| 2869 | return TemplateParameterList::Create(Context, SourceLocation(), |
| 2870 | SourceLocation(), None, |
| 2871 | SourceLocation(), nullptr); |
| 2872 | } |
| 2873 | |
| 2874 | return nullptr; |
| 2875 | } |
| 2876 | |
| 2877 | |
| 2878 | if (ParamIdx < ParamLists.size() - 1) { |
| 2879 | bool = false; |
| 2880 | bool = true; |
| 2881 | for (unsigned I = ParamIdx, E = ParamLists.size() - 1; I != E; ++I) { |
| 2882 | if (ParamLists[I]->size() == 0) |
| 2883 | HasAnyExplicitSpecHeader = true; |
| 2884 | else |
| 2885 | AllExplicitSpecHeaders = false; |
| 2886 | } |
| 2887 | |
| 2888 | Diag(ParamLists[ParamIdx]->getTemplateLoc(), |
| 2889 | AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers |
| 2890 | : diag::err_template_spec_extra_headers) |
| 2891 | << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), |
| 2892 | ParamLists[ParamLists.size() - 2]->getRAngleLoc()); |
| 2893 | |
| 2894 | |
| 2895 | |
| 2896 | |
| 2897 | if (ExplicitSpecLoc.isValid() && HasAnyExplicitSpecHeader) |
| 2898 | Diag(ExplicitSpecLoc, |
| 2899 | diag::note_explicit_template_spec_does_not_need_header) |
| 2900 | << NestedTypes.back(); |
| 2901 | |
| 2902 | |
| 2903 | |
| 2904 | |
| 2905 | if (!AllExplicitSpecHeaders) |
| 2906 | Invalid = true; |
| 2907 | } |
| 2908 | |
| 2909 | |
| 2910 | |
| 2911 | |
| 2912 | |
| 2913 | |
| 2914 | |
| 2915 | |
| 2916 | if (ParamLists.back()->size() == 0 && |
| 2917 | CheckExplicitSpecialization(ParamLists[ParamIdx]->getSourceRange(), |
| 2918 | false)) |
| 2919 | return nullptr; |
| 2920 | |
| 2921 | |
| 2922 | |
| 2923 | return ParamLists.back(); |
| 2924 | } |
| 2925 | |
| 2926 | void Sema::NoteAllFoundTemplates(TemplateName Name) { |
| 2927 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2928 | Diag(Template->getLocation(), diag::note_template_declared_here) |
| 2929 | << (isa<FunctionTemplateDecl>(Template) |
| 2930 | ? 0 |
| 2931 | : isa<ClassTemplateDecl>(Template) |
| 2932 | ? 1 |
| 2933 | : isa<VarTemplateDecl>(Template) |
| 2934 | ? 2 |
| 2935 | : isa<TypeAliasTemplateDecl>(Template) ? 3 : 4) |
| 2936 | << Template->getDeclName(); |
| 2937 | return; |
| 2938 | } |
| 2939 | |
| 2940 | if (OverloadedTemplateStorage *OST = Name.getAsOverloadedTemplate()) { |
| 2941 | for (OverloadedTemplateStorage::iterator I = OST->begin(), |
| 2942 | IEnd = OST->end(); |
| 2943 | I != IEnd; ++I) |
| 2944 | Diag((*I)->getLocation(), diag::note_template_declared_here) |
| 2945 | << 0 << (*I)->getDeclName(); |
| 2946 | |
| 2947 | return; |
| 2948 | } |
| 2949 | } |
| 2950 | |
| 2951 | static QualType |
| 2952 | checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD, |
| 2953 | const SmallVectorImpl<TemplateArgument> &Converted, |
| 2954 | SourceLocation TemplateLoc, |
| 2955 | TemplateArgumentListInfo &TemplateArgs) { |
| 2956 | ASTContext &Context = SemaRef.getASTContext(); |
| 2957 | switch (BTD->getBuiltinTemplateKind()) { |
| 2958 | case BTK__make_integer_seq: { |
| 2959 | |
| 2960 | |
| 2961 | |
| 2962 | |
| 2963 | |
| 2964 | if (!Converted[1].getAsType()->isIntegralType(Context)) { |
| 2965 | SemaRef.Diag(TemplateArgs[1].getLocation(), |
| 2966 | diag::err_integer_sequence_integral_element_type); |
| 2967 | return QualType(); |
| 2968 | } |
| 2969 | |
| 2970 | |
| 2971 | |
| 2972 | TemplateArgument NumArgsArg = Converted[2]; |
| 2973 | llvm::APSInt NumArgs = NumArgsArg.getAsIntegral(); |
| 2974 | if (NumArgs < 0) { |
| 2975 | SemaRef.Diag(TemplateArgs[2].getLocation(), |
| 2976 | diag::err_integer_sequence_negative_length); |
| 2977 | return QualType(); |
| 2978 | } |
| 2979 | |
| 2980 | QualType ArgTy = NumArgsArg.getIntegralType(); |
| 2981 | TemplateArgumentListInfo SyntheticTemplateArgs; |
| 2982 | |
| 2983 | |
| 2984 | SyntheticTemplateArgs.addArgument(TemplateArgs[1]); |
| 2985 | |
| 2986 | for (llvm::APSInt I(NumArgs.getBitWidth(), NumArgs.isUnsigned()); |
| 2987 | I < NumArgs; ++I) { |
| 2988 | TemplateArgument TA(Context, I, ArgTy); |
| 2989 | SyntheticTemplateArgs.addArgument(SemaRef.getTrivialTemplateArgumentLoc( |
| 2990 | TA, ArgTy, TemplateArgs[2].getLocation())); |
| 2991 | } |
| 2992 | |
| 2993 | |
| 2994 | return SemaRef.CheckTemplateIdType(Converted[0].getAsTemplate(), |
| 2995 | TemplateLoc, SyntheticTemplateArgs); |
| 2996 | } |
| 2997 | |
| 2998 | case BTK__type_pack_element: |
| 2999 | |
| 3000 | |
| 3001 | |
| 3002 | (0) . __assert_fail ("Converted.size() == 2 && \"__type_pack_element should be given an index and a parameter pack\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 3003, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Converted.size() == 2 && |
| 3003 | (0) . __assert_fail ("Converted.size() == 2 && \"__type_pack_element should be given an index and a parameter pack\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 3003, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "__type_pack_element should be given an index and a parameter pack"); |
| 3004 | |
| 3005 | |
| 3006 | TemplateArgument IndexArg = Converted[0], Ts = Converted[1]; |
| 3007 | llvm::APSInt Index = IndexArg.getAsIntegral(); |
| 3008 | (0) . __assert_fail ("Index >= 0 && \"the index used with __type_pack_element should be of \" \"type std..size_t, and hence be non-negative\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 3009, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Index >= 0 && "the index used with __type_pack_element should be of " |
| 3009 | (0) . __assert_fail ("Index >= 0 && \"the index used with __type_pack_element should be of \" \"type std..size_t, and hence be non-negative\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 3009, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "type std::size_t, and hence be non-negative"); |
| 3010 | if (Index >= Ts.pack_size()) { |
| 3011 | SemaRef.Diag(TemplateArgs[0].getLocation(), |
| 3012 | diag::err_type_pack_element_out_of_bounds); |
| 3013 | return QualType(); |
| 3014 | } |
| 3015 | |
| 3016 | |
| 3017 | auto Nth = std::next(Ts.pack_begin(), Index.getExtValue()); |
| 3018 | return Nth->getAsType(); |
| 3019 | } |
| 3020 | llvm_unreachable("unexpected BuiltinTemplateDecl!"); |
| 3021 | } |
| 3022 | |
| 3023 | |
| 3024 | static bool isEnableIfAliasTemplate(TypeAliasTemplateDecl *AliasTemplate) { |
| 3025 | return AliasTemplate->getName().equals("enable_if_t"); |
| 3026 | } |
| 3027 | |
| 3028 | |
| 3029 | |
| 3030 | |
| 3031 | |
| 3032 | |
| 3033 | |
| 3034 | static void collectConjunctionTerms(Expr *Clause, |
| 3035 | SmallVectorImpl<Expr *> &Terms) { |
| 3036 | if (auto BinOp = dyn_cast<BinaryOperator>(Clause->IgnoreParenImpCasts())) { |
| 3037 | if (BinOp->getOpcode() == BO_LAnd) { |
| 3038 | collectConjunctionTerms(BinOp->getLHS(), Terms); |
| 3039 | collectConjunctionTerms(BinOp->getRHS(), Terms); |
| 3040 | } |
| 3041 | |
| 3042 | return; |
| 3043 | } |
| 3044 | |
| 3045 | Terms.push_back(Clause); |
| 3046 | } |
| 3047 | |
| 3048 | |
| 3049 | |
| 3050 | |
| 3051 | static Expr *lookThroughRangesV3Condition(Preprocessor &PP, Expr *Cond) { |
| 3052 | |
| 3053 | auto *BinOp = dyn_cast<BinaryOperator>(Cond->IgnoreParenImpCasts()); |
| 3054 | if (!BinOp) return Cond; |
| 3055 | |
| 3056 | if (BinOp->getOpcode() != BO_LOr) return Cond; |
| 3057 | |
| 3058 | |
| 3059 | Expr *LHS = BinOp->getLHS(); |
| 3060 | auto *InnerBinOp = dyn_cast<BinaryOperator>(LHS->IgnoreParenImpCasts()); |
| 3061 | if (!InnerBinOp) return Cond; |
| 3062 | |
| 3063 | if (InnerBinOp->getOpcode() != BO_EQ || |
| 3064 | !isa<IntegerLiteral>(InnerBinOp->getRHS())) |
| 3065 | return Cond; |
| 3066 | |
| 3067 | |
| 3068 | |
| 3069 | |
| 3070 | SourceLocation Loc = InnerBinOp->getExprLoc(); |
| 3071 | if (!Loc.isMacroID()) return Cond; |
| 3072 | |
| 3073 | StringRef MacroName = PP.getImmediateMacroName(Loc); |
| 3074 | if (MacroName == "CONCEPT_REQUIRES" || MacroName == "CONCEPT_REQUIRES_") |
| 3075 | return BinOp->getRHS(); |
| 3076 | |
| 3077 | return Cond; |
| 3078 | } |
| 3079 | |
| 3080 | namespace { |
| 3081 | |
| 3082 | |
| 3083 | |
| 3084 | |
| 3085 | class FailedBooleanConditionPrinterHelper : public PrinterHelper { |
| 3086 | public: |
| 3087 | explicit FailedBooleanConditionPrinterHelper(const PrintingPolicy &P) |
| 3088 | : Policy(P) {} |
| 3089 | |
| 3090 | bool handledStmt(Stmt *E, raw_ostream &OS) override { |
| 3091 | const auto *DR = dyn_cast<DeclRefExpr>(E); |
| 3092 | if (DR && DR->getQualifier()) { |
| 3093 | |
| 3094 | |
| 3095 | DR->getQualifier()->print(OS, Policy, true); |
| 3096 | |
| 3097 | const ValueDecl *VD = DR->getDecl(); |
| 3098 | OS << VD->getName(); |
| 3099 | if (const auto *IV = dyn_cast<VarTemplateSpecializationDecl>(VD)) { |
| 3100 | |
| 3101 | printTemplateArgumentList(OS, IV->getTemplateArgs().asArray(), Policy); |
| 3102 | } |
| 3103 | return true; |
| 3104 | } |
| 3105 | return false; |
| 3106 | } |
| 3107 | |
| 3108 | private: |
| 3109 | const PrintingPolicy Policy; |
| 3110 | }; |
| 3111 | |
| 3112 | } |
| 3113 | |
| 3114 | std::pair<Expr *, std::string> |
| 3115 | Sema::findFailedBooleanCondition(Expr *Cond) { |
| 3116 | Cond = lookThroughRangesV3Condition(PP, Cond); |
| 3117 | |
| 3118 | |
| 3119 | SmallVector<Expr *, 4> Terms; |
| 3120 | collectConjunctionTerms(Cond, Terms); |
| 3121 | |
| 3122 | |
| 3123 | Expr *FailedCond = nullptr; |
| 3124 | for (Expr *Term : Terms) { |
| 3125 | Expr *TermAsWritten = Term->IgnoreParenImpCasts(); |
| 3126 | |
| 3127 | |
| 3128 | if (isa<CXXBoolLiteralExpr>(TermAsWritten) || |
| 3129 | isa<IntegerLiteral>(TermAsWritten)) |
| 3130 | continue; |
| 3131 | |
| 3132 | |
| 3133 | |
| 3134 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 3135 | *this, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 3136 | |
| 3137 | bool Succeeded; |
| 3138 | if (Term->EvaluateAsBooleanCondition(Succeeded, Context) && |
| 3139 | !Succeeded) { |
| 3140 | FailedCond = TermAsWritten; |
| 3141 | break; |
| 3142 | } |
| 3143 | } |
| 3144 | if (!FailedCond) |
| 3145 | FailedCond = Cond->IgnoreParenImpCasts(); |
| 3146 | |
| 3147 | std::string Description; |
| 3148 | { |
| 3149 | llvm::raw_string_ostream Out(Description); |
| 3150 | PrintingPolicy Policy = getPrintingPolicy(); |
| 3151 | Policy.PrintCanonicalTypes = true; |
| 3152 | FailedBooleanConditionPrinterHelper Helper(Policy); |
| 3153 | FailedCond->printPretty(Out, &Helper, Policy, 0, "\n", nullptr); |
| 3154 | } |
| 3155 | return { FailedCond, Description }; |
| 3156 | } |
| 3157 | |
| 3158 | QualType Sema::CheckTemplateIdType(TemplateName Name, |
| 3159 | SourceLocation TemplateLoc, |
| 3160 | TemplateArgumentListInfo &TemplateArgs) { |
| 3161 | DependentTemplateName *DTN |
| 3162 | = Name.getUnderlying().getAsDependentTemplateName(); |
| 3163 | if (DTN && DTN->isIdentifier()) |
| 3164 | |
| 3165 | |
| 3166 | |
| 3167 | |
| 3168 | return Context.getDependentTemplateSpecializationType(ETK_None, |
| 3169 | DTN->getQualifier(), |
| 3170 | DTN->getIdentifier(), |
| 3171 | TemplateArgs); |
| 3172 | |
| 3173 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 3174 | if (!Template || isa<FunctionTemplateDecl>(Template) || |
| 3175 | isa<VarTemplateDecl>(Template)) { |
| 3176 | |
| 3177 | |
| 3178 | if (Name.getAsSubstTemplateTemplateParmPack()) |
| 3179 | return Context.getTemplateSpecializationType(Name, TemplateArgs); |
| 3180 | |
| 3181 | Diag(TemplateLoc, diag::err_template_id_not_a_type) |
| 3182 | << Name; |
| 3183 | NoteAllFoundTemplates(Name); |
| 3184 | return QualType(); |
| 3185 | } |
| 3186 | |
| 3187 | |
| 3188 | |
| 3189 | SmallVector<TemplateArgument, 4> Converted; |
| 3190 | if (CheckTemplateArgumentList(Template, TemplateLoc, TemplateArgs, |
| 3191 | false, Converted)) |
| 3192 | return QualType(); |
| 3193 | |
| 3194 | QualType CanonType; |
| 3195 | |
| 3196 | bool InstantiationDependent = false; |
| 3197 | if (TypeAliasTemplateDecl *AliasTemplate = |
| 3198 | dyn_cast<TypeAliasTemplateDecl>(Template)) { |
| 3199 | |
| 3200 | TypeAliasDecl *Pattern = AliasTemplate->getTemplatedDecl(); |
| 3201 | if (Pattern->isInvalidDecl()) |
| 3202 | return QualType(); |
| 3203 | |
| 3204 | TemplateArgumentList StackTemplateArgs(TemplateArgumentList::OnStack, |
| 3205 | Converted); |
| 3206 | |
| 3207 | |
| 3208 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3209 | TemplateArgLists.addOuterTemplateArguments(&StackTemplateArgs); |
| 3210 | unsigned Depth = AliasTemplate->getTemplateParameters()->getDepth(); |
| 3211 | for (unsigned I = 0; I < Depth; ++I) |
| 3212 | TemplateArgLists.addOuterTemplateArguments(None); |
| 3213 | |
| 3214 | LocalInstantiationScope Scope(*this); |
| 3215 | InstantiatingTemplate Inst(*this, TemplateLoc, Template); |
| 3216 | if (Inst.isInvalid()) |
| 3217 | return QualType(); |
| 3218 | |
| 3219 | CanonType = SubstType(Pattern->getUnderlyingType(), |
| 3220 | TemplateArgLists, AliasTemplate->getLocation(), |
| 3221 | AliasTemplate->getDeclName()); |
| 3222 | if (CanonType.isNull()) { |
| 3223 | |
| 3224 | |
| 3225 | |
| 3226 | if (isEnableIfAliasTemplate(AliasTemplate)) { |
| 3227 | if (auto DeductionInfo = isSFINAEContext()) { |
| 3228 | if (*DeductionInfo && |
| 3229 | (*DeductionInfo)->hasSFINAEDiagnostic() && |
| 3230 | (*DeductionInfo)->peekSFINAEDiagnostic().second.getDiagID() == |
| 3231 | diag::err_typename_nested_not_found_enable_if && |
| 3232 | TemplateArgs[0].getArgument().getKind() |
| 3233 | == TemplateArgument::Expression) { |
| 3234 | Expr *FailedCond; |
| 3235 | std::string FailedDescription; |
| 3236 | std::tie(FailedCond, FailedDescription) = |
| 3237 | findFailedBooleanCondition(TemplateArgs[0].getSourceExpression()); |
| 3238 | |
| 3239 | |
| 3240 | PartialDiagnosticAt OldDiag = |
| 3241 | {SourceLocation(), PartialDiagnostic::NullDiagnostic()}; |
| 3242 | (*DeductionInfo)->takeSFINAEDiagnostic(OldDiag); |
| 3243 | |
| 3244 | |
| 3245 | |
| 3246 | (*DeductionInfo)->addSFINAEDiagnostic( |
| 3247 | OldDiag.first, |
| 3248 | PDiag(diag::err_typename_nested_not_found_requirement) |
| 3249 | << FailedDescription |
| 3250 | << FailedCond->getSourceRange()); |
| 3251 | } |
| 3252 | } |
| 3253 | } |
| 3254 | |
| 3255 | return QualType(); |
| 3256 | } |
| 3257 | } else if (Name.isDependent() || |
| 3258 | TemplateSpecializationType::anyDependentTemplateArguments( |
| 3259 | TemplateArgs, InstantiationDependent)) { |
| 3260 | |
| 3261 | |
| 3262 | |
| 3263 | |
| 3264 | |
| 3265 | |
| 3266 | |
| 3267 | CanonType = Context.getCanonicalTemplateSpecializationType(Name, Converted); |
| 3268 | |
| 3269 | |
| 3270 | |
| 3271 | |
| 3272 | |
| 3273 | |
| 3274 | |
| 3275 | if (isa<ClassTemplateDecl>(Template)) { |
| 3276 | for (DeclContext *Ctx = CurContext; Ctx; Ctx = Ctx->getLookupParent()) { |
| 3277 | |
| 3278 | if (Ctx->isFileContext()) break; |
| 3279 | |
| 3280 | |
| 3281 | CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(Ctx); |
| 3282 | if (!Record) continue; |
| 3283 | |
| 3284 | |
| 3285 | |
| 3286 | if (!isa<ClassTemplatePartialSpecializationDecl>(Record) && |
| 3287 | !Record->getDescribedClassTemplate()) |
| 3288 | continue; |
| 3289 | |
| 3290 | |
| 3291 | |
| 3292 | QualType ICNT = Context.getTypeDeclType(Record); |
| 3293 | QualType Injected = cast<InjectedClassNameType>(ICNT) |
| 3294 | ->getInjectedSpecializationType(); |
| 3295 | |
| 3296 | if (CanonType != Injected->getCanonicalTypeInternal()) |
| 3297 | continue; |
| 3298 | |
| 3299 | |
| 3300 | |
| 3301 | assert(ICNT.isCanonical()); |
| 3302 | CanonType = ICNT; |
| 3303 | break; |
| 3304 | } |
| 3305 | } |
| 3306 | } else if (ClassTemplateDecl *ClassTemplate |
| 3307 | = dyn_cast<ClassTemplateDecl>(Template)) { |
| 3308 | |
| 3309 | |
| 3310 | void *InsertPos = nullptr; |
| 3311 | ClassTemplateSpecializationDecl *Decl |
| 3312 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
| 3313 | if (!Decl) { |
| 3314 | |
| 3315 | |
| 3316 | |
| 3317 | Decl = ClassTemplateSpecializationDecl::Create( |
| 3318 | Context, ClassTemplate->getTemplatedDecl()->getTagKind(), |
| 3319 | ClassTemplate->getDeclContext(), |
| 3320 | ClassTemplate->getTemplatedDecl()->getBeginLoc(), |
| 3321 | ClassTemplate->getLocation(), ClassTemplate, Converted, nullptr); |
| 3322 | ClassTemplate->AddSpecialization(Decl, InsertPos); |
| 3323 | if (ClassTemplate->isOutOfLine()) |
| 3324 | Decl->setLexicalDeclContext(ClassTemplate->getLexicalDeclContext()); |
| 3325 | } |
| 3326 | |
| 3327 | if (Decl->getSpecializationKind() == TSK_Undeclared) { |
| 3328 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 3329 | TemplateArgLists.addOuterTemplateArguments(Converted); |
| 3330 | InstantiateAttrsForDecl(TemplateArgLists, ClassTemplate->getTemplatedDecl(), |
| 3331 | Decl); |
| 3332 | } |
| 3333 | |
| 3334 | |
| 3335 | (void)DiagnoseUseOfDecl(Decl, TemplateLoc); |
| 3336 | |
| 3337 | CanonType = Context.getTypeDeclType(Decl); |
| 3338 | (0) . __assert_fail ("isa(CanonType) && \"type of non-dependent specialization is not a RecordType\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 3339, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isa<RecordType>(CanonType) && |
| 3339 | (0) . __assert_fail ("isa(CanonType) && \"type of non-dependent specialization is not a RecordType\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 3339, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "type of non-dependent specialization is not a RecordType"); |
| 3340 | } else if (auto *BTD = dyn_cast<BuiltinTemplateDecl>(Template)) { |
| 3341 | CanonType = checkBuiltinTemplateIdType(*this, BTD, Converted, TemplateLoc, |
| 3342 | TemplateArgs); |
| 3343 | } |
| 3344 | |
| 3345 | |
| 3346 | |
| 3347 | |
| 3348 | return Context.getTemplateSpecializationType(Name, TemplateArgs, CanonType); |
| 3349 | } |
| 3350 | |
| 3351 | TypeResult |
| 3352 | Sema::ActOnTemplateIdType(CXXScopeSpec &SS, SourceLocation TemplateKWLoc, |
| 3353 | TemplateTy TemplateD, IdentifierInfo *TemplateII, |
| 3354 | SourceLocation TemplateIILoc, |
| 3355 | SourceLocation LAngleLoc, |
| 3356 | ASTTemplateArgsPtr TemplateArgsIn, |
| 3357 | SourceLocation RAngleLoc, |
| 3358 | bool IsCtorOrDtorName, bool IsClassName) { |
| 3359 | if (SS.isInvalid()) |
| 3360 | return true; |
| 3361 | |
| 3362 | if (!IsCtorOrDtorName && !IsClassName && SS.isSet()) { |
| 3363 | DeclContext *LookupCtx = computeDeclContext(SS, ); |
| 3364 | |
| 3365 | |
| 3366 | |
| 3367 | |
| 3368 | |
| 3369 | |
| 3370 | |
| 3371 | if (!LookupCtx && isDependentScopeSpecifier(SS)) { |
| 3372 | Diag(SS.getBeginLoc(), diag::err_typename_missing_template) |
| 3373 | << SS.getScopeRep() << TemplateII->getName(); |
| 3374 | |
| 3375 | |
| 3376 | |
| 3377 | |
| 3378 | return ActOnTypenameType(nullptr, SourceLocation(), SS, TemplateKWLoc, |
| 3379 | TemplateD, TemplateII, TemplateIILoc, LAngleLoc, |
| 3380 | TemplateArgsIn, RAngleLoc); |
| 3381 | } |
| 3382 | |
| 3383 | |
| 3384 | |
| 3385 | |
| 3386 | |
| 3387 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(LookupCtx); |
| 3388 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
| 3389 | Diag(TemplateIILoc, |
| 3390 | TemplateKWLoc.isInvalid() |
| 3391 | ? diag::err_out_of_line_qualified_id_type_names_constructor |
| 3392 | : diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 3393 | << TemplateII << 0 |
| 3394 | << 1 ; |
| 3395 | } |
| 3396 | } |
| 3397 | |
| 3398 | TemplateName Template = TemplateD.get(); |
| 3399 | |
| 3400 | |
| 3401 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 3402 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 3403 | |
| 3404 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 3405 | QualType T |
| 3406 | = Context.getDependentTemplateSpecializationType(ETK_None, |
| 3407 | DTN->getQualifier(), |
| 3408 | DTN->getIdentifier(), |
| 3409 | TemplateArgs); |
| 3410 | |
| 3411 | TypeLocBuilder TLB; |
| 3412 | DependentTemplateSpecializationTypeLoc SpecTL |
| 3413 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 3414 | SpecTL.setElaboratedKeywordLoc(SourceLocation()); |
| 3415 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3416 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 3417 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
| 3418 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3419 | SpecTL.setRAngleLoc(RAngleLoc); |
| 3420 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 3421 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 3422 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 3423 | } |
| 3424 | |
| 3425 | QualType Result = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
| 3426 | if (Result.isNull()) |
| 3427 | return true; |
| 3428 | |
| 3429 | |
| 3430 | TypeLocBuilder TLB; |
| 3431 | TemplateSpecializationTypeLoc SpecTL |
| 3432 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 3433 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 3434 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
| 3435 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3436 | SpecTL.setRAngleLoc(RAngleLoc); |
| 3437 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 3438 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
| 3439 | |
| 3440 | |
| 3441 | |
| 3442 | |
| 3443 | if (SS.isNotEmpty() && !IsCtorOrDtorName) { |
| 3444 | |
| 3445 | Result = Context.getElaboratedType(ETK_None, SS.getScopeRep(), Result); |
| 3446 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 3447 | ElabTL.setElaboratedKeywordLoc(SourceLocation()); |
| 3448 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3449 | } |
| 3450 | |
| 3451 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
| 3452 | } |
| 3453 | |
| 3454 | TypeResult Sema::ActOnTagTemplateIdType(TagUseKind TUK, |
| 3455 | TypeSpecifierType TagSpec, |
| 3456 | SourceLocation TagLoc, |
| 3457 | CXXScopeSpec &SS, |
| 3458 | SourceLocation TemplateKWLoc, |
| 3459 | TemplateTy TemplateD, |
| 3460 | SourceLocation TemplateLoc, |
| 3461 | SourceLocation LAngleLoc, |
| 3462 | ASTTemplateArgsPtr TemplateArgsIn, |
| 3463 | SourceLocation RAngleLoc) { |
| 3464 | TemplateName Template = TemplateD.get(); |
| 3465 | |
| 3466 | |
| 3467 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 3468 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 3469 | |
| 3470 | |
| 3471 | TagTypeKind TagKind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 3472 | ElaboratedTypeKeyword Keyword |
| 3473 | = TypeWithKeyword::getKeywordForTagTypeKind(TagKind); |
| 3474 | |
| 3475 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 3476 | QualType T = Context.getDependentTemplateSpecializationType(Keyword, |
| 3477 | DTN->getQualifier(), |
| 3478 | DTN->getIdentifier(), |
| 3479 | TemplateArgs); |
| 3480 | |
| 3481 | |
| 3482 | TypeLocBuilder TLB; |
| 3483 | DependentTemplateSpecializationTypeLoc SpecTL |
| 3484 | = TLB.push<DependentTemplateSpecializationTypeLoc>(T); |
| 3485 | SpecTL.setElaboratedKeywordLoc(TagLoc); |
| 3486 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3487 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 3488 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 3489 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3490 | SpecTL.setRAngleLoc(RAngleLoc); |
| 3491 | for (unsigned I = 0, N = SpecTL.getNumArgs(); I != N; ++I) |
| 3492 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 3493 | return CreateParsedType(T, TLB.getTypeSourceInfo(Context, T)); |
| 3494 | } |
| 3495 | |
| 3496 | if (TypeAliasTemplateDecl *TAT = |
| 3497 | dyn_cast_or_null<TypeAliasTemplateDecl>(Template.getAsTemplateDecl())) { |
| 3498 | |
| 3499 | |
| 3500 | |
| 3501 | |
| 3502 | Diag(TemplateLoc, diag::err_tag_reference_non_tag) |
| 3503 | << TAT << NTK_TypeAliasTemplate << TagKind; |
| 3504 | Diag(TAT->getLocation(), diag::note_declared_at); |
| 3505 | } |
| 3506 | |
| 3507 | QualType Result = CheckTemplateIdType(Template, TemplateLoc, TemplateArgs); |
| 3508 | if (Result.isNull()) |
| 3509 | return TypeResult(true); |
| 3510 | |
| 3511 | |
| 3512 | if (const RecordType *RT = Result->getAs<RecordType>()) { |
| 3513 | RecordDecl *D = RT->getDecl(); |
| 3514 | |
| 3515 | IdentifierInfo *Id = D->getIdentifier(); |
| 3516 | (0) . __assert_fail ("Id && \"templated class must have an identifier\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 3516, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Id && "templated class must have an identifier"); |
| 3517 | |
| 3518 | if (!isAcceptableTagRedeclaration(D, TagKind, TUK == TUK_Definition, |
| 3519 | TagLoc, Id)) { |
| 3520 | Diag(TagLoc, diag::err_use_with_wrong_tag) |
| 3521 | << Result |
| 3522 | << FixItHint::CreateReplacement(SourceRange(TagLoc), D->getKindName()); |
| 3523 | Diag(D->getLocation(), diag::note_previous_use); |
| 3524 | } |
| 3525 | } |
| 3526 | |
| 3527 | |
| 3528 | TypeLocBuilder TLB; |
| 3529 | TemplateSpecializationTypeLoc SpecTL |
| 3530 | = TLB.push<TemplateSpecializationTypeLoc>(Result); |
| 3531 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 3532 | SpecTL.setTemplateNameLoc(TemplateLoc); |
| 3533 | SpecTL.setLAngleLoc(LAngleLoc); |
| 3534 | SpecTL.setRAngleLoc(RAngleLoc); |
| 3535 | for (unsigned i = 0, e = SpecTL.getNumArgs(); i != e; ++i) |
| 3536 | SpecTL.setArgLocInfo(i, TemplateArgs[i].getLocInfo()); |
| 3537 | |
| 3538 | |
| 3539 | |
| 3540 | Result = Context.getElaboratedType(Keyword, SS.getScopeRep(), Result); |
| 3541 | ElaboratedTypeLoc ElabTL = TLB.push<ElaboratedTypeLoc>(Result); |
| 3542 | ElabTL.setElaboratedKeywordLoc(TagLoc); |
| 3543 | ElabTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 3544 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
| 3545 | } |
| 3546 | |
| 3547 | static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized, |
| 3548 | NamedDecl *PrevDecl, |
| 3549 | SourceLocation Loc, |
| 3550 | bool IsPartialSpecialization); |
| 3551 | |
| 3552 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D); |
| 3553 | |
| 3554 | static bool isTemplateArgumentTemplateParameter( |
| 3555 | const TemplateArgument &Arg, unsigned Depth, unsigned Index) { |
| 3556 | switch (Arg.getKind()) { |
| 3557 | case TemplateArgument::Null: |
| 3558 | case TemplateArgument::NullPtr: |
| 3559 | case TemplateArgument::Integral: |
| 3560 | case TemplateArgument::Declaration: |
| 3561 | case TemplateArgument::Pack: |
| 3562 | case TemplateArgument::TemplateExpansion: |
| 3563 | return false; |
| 3564 | |
| 3565 | case TemplateArgument::Type: { |
| 3566 | QualType Type = Arg.getAsType(); |
| 3567 | const TemplateTypeParmType *TPT = |
| 3568 | Arg.getAsType()->getAs<TemplateTypeParmType>(); |
| 3569 | return TPT && !Type.hasQualifiers() && |
| 3570 | TPT->getDepth() == Depth && TPT->getIndex() == Index; |
| 3571 | } |
| 3572 | |
| 3573 | case TemplateArgument::Expression: { |
| 3574 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg.getAsExpr()); |
| 3575 | if (!DRE || !DRE->getDecl()) |
| 3576 | return false; |
| 3577 | const NonTypeTemplateParmDecl *NTTP = |
| 3578 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 3579 | return NTTP && NTTP->getDepth() == Depth && NTTP->getIndex() == Index; |
| 3580 | } |
| 3581 | |
| 3582 | case TemplateArgument::Template: |
| 3583 | const TemplateTemplateParmDecl *TTP = |
| 3584 | dyn_cast_or_null<TemplateTemplateParmDecl>( |
| 3585 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()); |
| 3586 | return TTP && TTP->getDepth() == Depth && TTP->getIndex() == Index; |
| 3587 | } |
| 3588 | llvm_unreachable("unexpected kind of template argument"); |
| 3589 | } |
| 3590 | |
| 3591 | static bool isSameAsPrimaryTemplate(TemplateParameterList *Params, |
| 3592 | ArrayRef<TemplateArgument> Args) { |
| 3593 | if (Params->size() != Args.size()) |
| 3594 | return false; |
| 3595 | |
| 3596 | unsigned Depth = Params->getDepth(); |
| 3597 | |
| 3598 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 3599 | TemplateArgument Arg = Args[I]; |
| 3600 | |
| 3601 | |
| 3602 | |
| 3603 | if (Params->getParam(I)->isParameterPack()) { |
| 3604 | if (Arg.getKind() != TemplateArgument::Pack || Arg.pack_size() != 1 || |
| 3605 | !Arg.pack_begin()->isPackExpansion()) |
| 3606 | return false; |
| 3607 | Arg = Arg.pack_begin()->getPackExpansionPattern(); |
| 3608 | } |
| 3609 | |
| 3610 | if (!isTemplateArgumentTemplateParameter(Arg, Depth, I)) |
| 3611 | return false; |
| 3612 | } |
| 3613 | |
| 3614 | return true; |
| 3615 | } |
| 3616 | |
| 3617 | |
| 3618 | static TemplateArgumentListInfo |
| 3619 | makeTemplateArgumentListInfo(Sema &S, TemplateIdAnnotation &TemplateId) { |
| 3620 | TemplateArgumentListInfo TemplateArgs(TemplateId.LAngleLoc, |
| 3621 | TemplateId.RAngleLoc); |
| 3622 | ASTTemplateArgsPtr TemplateArgsPtr(TemplateId.getTemplateArgs(), |
| 3623 | TemplateId.NumArgs); |
| 3624 | S.translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
| 3625 | return TemplateArgs; |
| 3626 | } |
| 3627 | |
| 3628 | template<typename PartialSpecDecl> |
| 3629 | static void checkMoreSpecializedThanPrimary(Sema &S, PartialSpecDecl *Partial) { |
| 3630 | if (Partial->getDeclContext()->isDependentContext()) |
| 3631 | return; |
| 3632 | |
| 3633 | |
| 3634 | |
| 3635 | TemplateDeductionInfo Info(Partial->getLocation()); |
| 3636 | if (S.isMoreSpecializedThanPrimary(Partial, Info)) |
| 3637 | return; |
| 3638 | |
| 3639 | auto *Template = Partial->getSpecializedTemplate(); |
| 3640 | S.Diag(Partial->getLocation(), |
| 3641 | diag::ext_partial_spec_not_more_specialized_than_primary) |
| 3642 | << isa<VarTemplateDecl>(Template); |
| 3643 | |
| 3644 | if (Info.hasSFINAEDiagnostic()) { |
| 3645 | PartialDiagnosticAt Diag = {SourceLocation(), |
| 3646 | PartialDiagnostic::NullDiagnostic()}; |
| 3647 | Info.takeSFINAEDiagnostic(Diag); |
| 3648 | SmallString<128> SFINAEArgString; |
| 3649 | Diag.second.EmitToString(S.getDiagnostics(), SFINAEArgString); |
| 3650 | S.Diag(Diag.first, |
| 3651 | diag::note_partial_spec_not_more_specialized_than_primary) |
| 3652 | << SFINAEArgString; |
| 3653 | } |
| 3654 | |
| 3655 | S.Diag(Template->getLocation(), diag::note_template_decl_here); |
| 3656 | } |
| 3657 | |
| 3658 | static void |
| 3659 | noteNonDeducibleParameters(Sema &S, TemplateParameterList *TemplateParams, |
| 3660 | const llvm::SmallBitVector &DeducibleParams) { |
| 3661 | for (unsigned I = 0, N = DeducibleParams.size(); I != N; ++I) { |
| 3662 | if (!DeducibleParams[I]) { |
| 3663 | NamedDecl *Param = TemplateParams->getParam(I); |
| 3664 | if (Param->getDeclName()) |
| 3665 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
| 3666 | << Param->getDeclName(); |
| 3667 | else |
| 3668 | S.Diag(Param->getLocation(), diag::note_non_deducible_parameter) |
| 3669 | << "(anonymous)"; |
| 3670 | } |
| 3671 | } |
| 3672 | } |
| 3673 | |
| 3674 | |
| 3675 | template<typename PartialSpecDecl> |
| 3676 | static void checkTemplatePartialSpecialization(Sema &S, |
| 3677 | PartialSpecDecl *Partial) { |
| 3678 | |
| 3679 | |
| 3680 | |
| 3681 | checkMoreSpecializedThanPrimary(S, Partial); |
| 3682 | |
| 3683 | |
| 3684 | |
| 3685 | |
| 3686 | |
| 3687 | |
| 3688 | |
| 3689 | |
| 3690 | auto *TemplateParams = Partial->getTemplateParameters(); |
| 3691 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 3692 | S.MarkUsedTemplateParameters(Partial->getTemplateArgs(), true, |
| 3693 | TemplateParams->getDepth(), DeducibleParams); |
| 3694 | |
| 3695 | if (!DeducibleParams.all()) { |
| 3696 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
| 3697 | S.Diag(Partial->getLocation(), diag::ext_partial_specs_not_deducible) |
| 3698 | << isa<VarTemplatePartialSpecializationDecl>(Partial) |
| 3699 | << (NumNonDeducible > 1) |
| 3700 | << SourceRange(Partial->getLocation(), |
| 3701 | Partial->getTemplateArgsAsWritten()->RAngleLoc); |
| 3702 | noteNonDeducibleParameters(S, TemplateParams, DeducibleParams); |
| 3703 | } |
| 3704 | } |
| 3705 | |
| 3706 | void Sema::CheckTemplatePartialSpecialization( |
| 3707 | ClassTemplatePartialSpecializationDecl *Partial) { |
| 3708 | checkTemplatePartialSpecialization(*this, Partial); |
| 3709 | } |
| 3710 | |
| 3711 | void Sema::CheckTemplatePartialSpecialization( |
| 3712 | VarTemplatePartialSpecializationDecl *Partial) { |
| 3713 | checkTemplatePartialSpecialization(*this, Partial); |
| 3714 | } |
| 3715 | |
| 3716 | void Sema::CheckDeductionGuideTemplate(FunctionTemplateDecl *TD) { |
| 3717 | |
| 3718 | |
| 3719 | |
| 3720 | |
| 3721 | auto *TemplateParams = TD->getTemplateParameters(); |
| 3722 | llvm::SmallBitVector DeducibleParams(TemplateParams->size()); |
| 3723 | MarkDeducedTemplateParameters(TD, DeducibleParams); |
| 3724 | for (unsigned I = 0; I != TemplateParams->size(); ++I) { |
| 3725 | |
| 3726 | auto *Param = TemplateParams->getParam(I); |
| 3727 | if (Param->isParameterPack() || hasVisibleDefaultArgument(Param)) |
| 3728 | DeducibleParams[I] = true; |
| 3729 | } |
| 3730 | |
| 3731 | if (!DeducibleParams.all()) { |
| 3732 | unsigned NumNonDeducible = DeducibleParams.size() - DeducibleParams.count(); |
| 3733 | Diag(TD->getLocation(), diag::err_deduction_guide_template_not_deducible) |
| 3734 | << (NumNonDeducible > 1); |
| 3735 | noteNonDeducibleParameters(*this, TemplateParams, DeducibleParams); |
| 3736 | } |
| 3737 | } |
| 3738 | |
| 3739 | DeclResult Sema::ActOnVarTemplateSpecialization( |
| 3740 | Scope *S, Declarator &D, TypeSourceInfo *DI, SourceLocation TemplateKWLoc, |
| 3741 | TemplateParameterList *TemplateParams, StorageClass SC, |
| 3742 | bool IsPartialSpecialization) { |
| 3743 | |
| 3744 | (0) . __assert_fail ("D.getName().getKind() == UnqualifiedIdKind..IK_TemplateId && \"Variable template specialization is declared with a template it.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 3745, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId && |
| 3745 | (0) . __assert_fail ("D.getName().getKind() == UnqualifiedIdKind..IK_TemplateId && \"Variable template specialization is declared with a template it.\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 3745, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Variable template specialization is declared with a template it."); |
| 3746 | |
| 3747 | TemplateIdAnnotation *TemplateId = D.getName().TemplateId; |
| 3748 | TemplateArgumentListInfo TemplateArgs = |
| 3749 | makeTemplateArgumentListInfo(*this, *TemplateId); |
| 3750 | SourceLocation TemplateNameLoc = D.getIdentifierLoc(); |
| 3751 | SourceLocation LAngleLoc = TemplateId->LAngleLoc; |
| 3752 | SourceLocation RAngleLoc = TemplateId->RAngleLoc; |
| 3753 | |
| 3754 | TemplateName Name = TemplateId->Template.get(); |
| 3755 | |
| 3756 | |
| 3757 | VarTemplateDecl *VarTemplate = |
| 3758 | dyn_cast_or_null<VarTemplateDecl>(Name.getAsTemplateDecl()); |
| 3759 | if (!VarTemplate) { |
| 3760 | NamedDecl *FnTemplate; |
| 3761 | if (auto *OTS = Name.getAsOverloadedTemplate()) |
| 3762 | FnTemplate = *OTS->begin(); |
| 3763 | else |
| 3764 | FnTemplate = dyn_cast_or_null<FunctionTemplateDecl>(Name.getAsTemplateDecl()); |
| 3765 | if (FnTemplate) |
| 3766 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template_but_method) |
| 3767 | << FnTemplate->getDeclName(); |
| 3768 | return Diag(D.getIdentifierLoc(), diag::err_var_spec_no_template) |
| 3769 | << IsPartialSpecialization; |
| 3770 | } |
| 3771 | |
| 3772 | |
| 3773 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 3774 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 3775 | UPPC_PartialSpecialization)) |
| 3776 | return true; |
| 3777 | |
| 3778 | |
| 3779 | |
| 3780 | SmallVector<TemplateArgument, 4> Converted; |
| 3781 | if (CheckTemplateArgumentList(VarTemplate, TemplateNameLoc, TemplateArgs, |
| 3782 | false, Converted)) |
| 3783 | return true; |
| 3784 | |
| 3785 | |
| 3786 | |
| 3787 | if (IsPartialSpecialization) { |
| 3788 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, VarTemplate, |
| 3789 | TemplateArgs.size(), Converted)) |
| 3790 | return true; |
| 3791 | |
| 3792 | |
| 3793 | |
| 3794 | bool InstantiationDependent; |
| 3795 | if (!Name.isDependent() && |
| 3796 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 3797 | TemplateArgs.arguments(), |
| 3798 | InstantiationDependent)) { |
| 3799 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 3800 | << VarTemplate->getDeclName(); |
| 3801 | IsPartialSpecialization = false; |
| 3802 | } |
| 3803 | |
| 3804 | if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(), |
| 3805 | Converted)) { |
| 3806 | |
| 3807 | |
| 3808 | |
| 3809 | |
| 3810 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 3811 | << 1 |
| 3812 | << (SC != SC_Extern && !CurContext->isRecord()) |
| 3813 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 3814 | |
| 3815 | |
| 3816 | return true; |
| 3817 | } |
| 3818 | } |
| 3819 | |
| 3820 | void *InsertPos = nullptr; |
| 3821 | VarTemplateSpecializationDecl *PrevDecl = nullptr; |
| 3822 | |
| 3823 | if (IsPartialSpecialization) |
| 3824 | |
| 3825 | PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos); |
| 3826 | else |
| 3827 | PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos); |
| 3828 | |
| 3829 | VarTemplateSpecializationDecl *Specialization = nullptr; |
| 3830 | |
| 3831 | |
| 3832 | |
| 3833 | if (CheckTemplateSpecializationScope(*this, VarTemplate, PrevDecl, |
| 3834 | TemplateNameLoc, |
| 3835 | IsPartialSpecialization)) |
| 3836 | return true; |
| 3837 | |
| 3838 | if (PrevDecl && PrevDecl->getSpecializationKind() == TSK_Undeclared) { |
| 3839 | |
| 3840 | |
| 3841 | |
| 3842 | |
| 3843 | Specialization = PrevDecl; |
| 3844 | Specialization->setLocation(TemplateNameLoc); |
| 3845 | PrevDecl = nullptr; |
| 3846 | } else if (IsPartialSpecialization) { |
| 3847 | |
| 3848 | VarTemplatePartialSpecializationDecl *PrevPartial = |
| 3849 | cast_or_null<VarTemplatePartialSpecializationDecl>(PrevDecl); |
| 3850 | VarTemplatePartialSpecializationDecl *Partial = |
| 3851 | VarTemplatePartialSpecializationDecl::Create( |
| 3852 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, |
| 3853 | TemplateNameLoc, TemplateParams, VarTemplate, DI->getType(), DI, SC, |
| 3854 | Converted, TemplateArgs); |
| 3855 | |
| 3856 | if (!PrevPartial) |
| 3857 | VarTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 3858 | Specialization = Partial; |
| 3859 | |
| 3860 | |
| 3861 | |
| 3862 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 3863 | PrevPartial->setMemberSpecialization(); |
| 3864 | |
| 3865 | CheckTemplatePartialSpecialization(Partial); |
| 3866 | } else { |
| 3867 | |
| 3868 | |
| 3869 | Specialization = VarTemplateSpecializationDecl::Create( |
| 3870 | Context, VarTemplate->getDeclContext(), TemplateKWLoc, TemplateNameLoc, |
| 3871 | VarTemplate, DI->getType(), DI, SC, Converted); |
| 3872 | Specialization->setTemplateArgsInfo(TemplateArgs); |
| 3873 | |
| 3874 | if (!PrevDecl) |
| 3875 | VarTemplate->AddSpecialization(Specialization, InsertPos); |
| 3876 | } |
| 3877 | |
| 3878 | |
| 3879 | |
| 3880 | |
| 3881 | |
| 3882 | |
| 3883 | |
| 3884 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 3885 | bool Okay = false; |
| 3886 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 3887 | |
| 3888 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 3889 | Okay = true; |
| 3890 | break; |
| 3891 | } |
| 3892 | } |
| 3893 | |
| 3894 | if (!Okay) { |
| 3895 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 3896 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 3897 | << Name << Range; |
| 3898 | |
| 3899 | Diag(PrevDecl->getPointOfInstantiation(), |
| 3900 | diag::note_instantiation_required_here) |
| 3901 | << (PrevDecl->getTemplateSpecializationKind() != |
| 3902 | TSK_ImplicitInstantiation); |
| 3903 | return true; |
| 3904 | } |
| 3905 | } |
| 3906 | |
| 3907 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 3908 | Specialization->setLexicalDeclContext(CurContext); |
| 3909 | |
| 3910 | |
| 3911 | |
| 3912 | |
| 3913 | CurContext->addDecl(Specialization); |
| 3914 | |
| 3915 | |
| 3916 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 3917 | |
| 3918 | if (PrevDecl) { |
| 3919 | |
| 3920 | |
| 3921 | LookupResult PrevSpec(*this, GetNameForDeclarator(D), LookupOrdinaryName, |
| 3922 | forRedeclarationInCurContext()); |
| 3923 | PrevSpec.addDecl(PrevDecl); |
| 3924 | D.setRedeclaration(CheckVariableDeclaration(Specialization, PrevSpec)); |
| 3925 | } else if (Specialization->isStaticDataMember() && |
| 3926 | Specialization->isOutOfLine()) { |
| 3927 | Specialization->setAccess(VarTemplate->getAccess()); |
| 3928 | } |
| 3929 | |
| 3930 | |
| 3931 | |
| 3932 | if (Specialization->isStaticDataMember()) |
| 3933 | Specialization->setInstantiationOfStaticDataMember( |
| 3934 | VarTemplate->getTemplatedDecl(), |
| 3935 | Specialization->getSpecializationKind()); |
| 3936 | |
| 3937 | return Specialization; |
| 3938 | } |
| 3939 | |
| 3940 | namespace { |
| 3941 | |
| 3942 | |
| 3943 | struct PartialSpecMatchResult { |
| 3944 | VarTemplatePartialSpecializationDecl *Partial; |
| 3945 | TemplateArgumentList *Args; |
| 3946 | }; |
| 3947 | } |
| 3948 | |
| 3949 | DeclResult |
| 3950 | Sema::CheckVarTemplateId(VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 3951 | SourceLocation TemplateNameLoc, |
| 3952 | const TemplateArgumentListInfo &TemplateArgs) { |
| 3953 | (0) . __assert_fail ("Template && \"A variable template id without template?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 3953, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Template && "A variable template id without template?"); |
| 3954 | |
| 3955 | |
| 3956 | SmallVector<TemplateArgument, 4> Converted; |
| 3957 | if (CheckTemplateArgumentList( |
| 3958 | Template, TemplateNameLoc, |
| 3959 | const_cast<TemplateArgumentListInfo &>(TemplateArgs), false, |
| 3960 | Converted)) |
| 3961 | return true; |
| 3962 | |
| 3963 | |
| 3964 | |
| 3965 | void *InsertPos = nullptr; |
| 3966 | if (VarTemplateSpecializationDecl *Spec = Template->findSpecialization( |
| 3967 | Converted, InsertPos)) { |
| 3968 | checkSpecializationVisibility(TemplateNameLoc, Spec); |
| 3969 | |
| 3970 | return Spec; |
| 3971 | } |
| 3972 | |
| 3973 | |
| 3974 | |
| 3975 | |
| 3976 | |
| 3977 | VarDecl *InstantiationPattern = Template->getTemplatedDecl(); |
| 3978 | TemplateArgumentList TemplateArgList(TemplateArgumentList::OnStack, |
| 3979 | Converted); |
| 3980 | TemplateArgumentList *InstantiationArgs = &TemplateArgList; |
| 3981 | bool AmbiguousPartialSpec = false; |
| 3982 | typedef PartialSpecMatchResult MatchResult; |
| 3983 | SmallVector<MatchResult, 4> Matched; |
| 3984 | SourceLocation PointOfInstantiation = TemplateNameLoc; |
| 3985 | TemplateSpecCandidateSet FailedCandidates(PointOfInstantiation, |
| 3986 | ); |
| 3987 | |
| 3988 | |
| 3989 | |
| 3990 | |
| 3991 | |
| 3992 | |
| 3993 | |
| 3994 | |
| 3995 | |
| 3996 | |
| 3997 | bool InstantiationDependent = false; |
| 3998 | if (!TemplateSpecializationType::anyDependentTemplateArguments( |
| 3999 | TemplateArgs, InstantiationDependent)) { |
| 4000 | |
| 4001 | SmallVector<VarTemplatePartialSpecializationDecl *, 4> PartialSpecs; |
| 4002 | Template->getPartialSpecializations(PartialSpecs); |
| 4003 | |
| 4004 | for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { |
| 4005 | VarTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; |
| 4006 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 4007 | |
| 4008 | if (TemplateDeductionResult Result = |
| 4009 | DeduceTemplateArguments(Partial, TemplateArgList, Info)) { |
| 4010 | |
| 4011 | |
| 4012 | FailedCandidates.addCandidate().set( |
| 4013 | DeclAccessPair::make(Template, AS_public), Partial, |
| 4014 | MakeDeductionFailureInfo(Context, Result, Info)); |
| 4015 | (void)Result; |
| 4016 | } else { |
| 4017 | Matched.push_back(PartialSpecMatchResult()); |
| 4018 | Matched.back().Partial = Partial; |
| 4019 | Matched.back().Args = Info.take(); |
| 4020 | } |
| 4021 | } |
| 4022 | |
| 4023 | if (Matched.size() >= 1) { |
| 4024 | SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); |
| 4025 | if (Matched.size() == 1) { |
| 4026 | |
| 4027 | |
| 4028 | |
| 4029 | } else { |
| 4030 | |
| 4031 | |
| 4032 | |
| 4033 | |
| 4034 | |
| 4035 | |
| 4036 | |
| 4037 | for (SmallVector<MatchResult, 4>::iterator P = Best + 1, |
| 4038 | PEnd = Matched.end(); |
| 4039 | P != PEnd; ++P) { |
| 4040 | if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, |
| 4041 | PointOfInstantiation) == |
| 4042 | P->Partial) |
| 4043 | Best = P; |
| 4044 | } |
| 4045 | |
| 4046 | |
| 4047 | |
| 4048 | for (SmallVector<MatchResult, 4>::iterator P = Matched.begin(), |
| 4049 | PEnd = Matched.end(); |
| 4050 | P != PEnd; ++P) { |
| 4051 | if (P != Best && getMoreSpecializedPartialSpecialization( |
| 4052 | P->Partial, Best->Partial, |
| 4053 | PointOfInstantiation) != Best->Partial) { |
| 4054 | AmbiguousPartialSpec = true; |
| 4055 | break; |
| 4056 | } |
| 4057 | } |
| 4058 | } |
| 4059 | |
| 4060 | |
| 4061 | InstantiationPattern = Best->Partial; |
| 4062 | InstantiationArgs = Best->Args; |
| 4063 | } else { |
| 4064 | |
| 4065 | |
| 4066 | |
| 4067 | } |
| 4068 | } |
| 4069 | |
| 4070 | |
| 4071 | |
| 4072 | |
| 4073 | |
| 4074 | VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( |
| 4075 | Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, |
| 4076 | Converted, TemplateNameLoc, InsertPos ); |
| 4077 | if (!Decl) |
| 4078 | return true; |
| 4079 | |
| 4080 | if (AmbiguousPartialSpec) { |
| 4081 | |
| 4082 | Decl->setInvalidDecl(); |
| 4083 | Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) |
| 4084 | << Decl; |
| 4085 | |
| 4086 | |
| 4087 | for (MatchResult P : Matched) |
| 4088 | Diag(P.Partial->getLocation(), diag::note_partial_spec_match) |
| 4089 | << getTemplateArgumentBindingsText(P.Partial->getTemplateParameters(), |
| 4090 | *P.Args); |
| 4091 | return true; |
| 4092 | } |
| 4093 | |
| 4094 | if (VarTemplatePartialSpecializationDecl *D = |
| 4095 | dyn_cast<VarTemplatePartialSpecializationDecl>(InstantiationPattern)) |
| 4096 | Decl->setInstantiationOf(D, InstantiationArgs); |
| 4097 | |
| 4098 | checkSpecializationVisibility(TemplateNameLoc, Decl); |
| 4099 | |
| 4100 | (0) . __assert_fail ("Decl && \"No variable template specialization?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 4100, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Decl && "No variable template specialization?"); |
| 4101 | return Decl; |
| 4102 | } |
| 4103 | |
| 4104 | ExprResult |
| 4105 | Sema::CheckVarTemplateId(const CXXScopeSpec &SS, |
| 4106 | const DeclarationNameInfo &NameInfo, |
| 4107 | VarTemplateDecl *Template, SourceLocation TemplateLoc, |
| 4108 | const TemplateArgumentListInfo *TemplateArgs) { |
| 4109 | |
| 4110 | DeclResult Decl = CheckVarTemplateId(Template, TemplateLoc, NameInfo.getLoc(), |
| 4111 | *TemplateArgs); |
| 4112 | if (Decl.isInvalid()) |
| 4113 | return ExprError(); |
| 4114 | |
| 4115 | VarDecl *Var = cast<VarDecl>(Decl.get()); |
| 4116 | if (!Var->getTemplateSpecializationKind()) |
| 4117 | Var->setTemplateSpecializationKind(TSK_ImplicitInstantiation, |
| 4118 | NameInfo.getLoc()); |
| 4119 | |
| 4120 | |
| 4121 | return BuildDeclarationNameExpr(SS, NameInfo, Var, |
| 4122 | , TemplateArgs); |
| 4123 | } |
| 4124 | |
| 4125 | void Sema::diagnoseMissingTemplateArguments(TemplateName Name, |
| 4126 | SourceLocation Loc) { |
| 4127 | Diag(Loc, diag::err_template_missing_args) |
| 4128 | << (int)getTemplateNameKindForDiagnostics(Name) << Name; |
| 4129 | if (TemplateDecl *TD = Name.getAsTemplateDecl()) { |
| 4130 | Diag(TD->getLocation(), diag::note_template_decl_here) |
| 4131 | << TD->getTemplateParameters()->getSourceRange(); |
| 4132 | } |
| 4133 | } |
| 4134 | |
| 4135 | ExprResult Sema::BuildTemplateIdExpr(const CXXScopeSpec &SS, |
| 4136 | SourceLocation TemplateKWLoc, |
| 4137 | LookupResult &R, |
| 4138 | bool RequiresADL, |
| 4139 | const TemplateArgumentListInfo *TemplateArgs) { |
| 4140 | |
| 4141 | |
| 4142 | |
| 4143 | |
| 4144 | |
| 4145 | |
| 4146 | |
| 4147 | |
| 4148 | |
| 4149 | |
| 4150 | |
| 4151 | (0) . __assert_fail ("!R.empty() && \"empty lookup results when building templateid\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 4151, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!R.empty() && "empty lookup results when building templateid"); |
| 4152 | (0) . __assert_fail ("!R.isAmbiguous() && \"ambiguous lookup when building templateid\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 4152, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!R.isAmbiguous() && "ambiguous lookup when building templateid"); |
| 4153 | |
| 4154 | |
| 4155 | if (auto *TD = R.getAsSingle<TemplateDecl>()) { |
| 4156 | if (!TemplateArgs && !isa<FunctionTemplateDecl>(TD)) { |
| 4157 | diagnoseMissingTemplateArguments(TemplateName(TD), R.getNameLoc()); |
| 4158 | return ExprError(); |
| 4159 | } |
| 4160 | } |
| 4161 | |
| 4162 | auto AnyDependentArguments = [&]() -> bool { |
| 4163 | bool InstantiationDependent; |
| 4164 | return TemplateArgs && |
| 4165 | TemplateSpecializationType::anyDependentTemplateArguments( |
| 4166 | *TemplateArgs, InstantiationDependent); |
| 4167 | }; |
| 4168 | |
| 4169 | |
| 4170 | if (R.getAsSingle<VarTemplateDecl>() && !AnyDependentArguments()) { |
| 4171 | return CheckVarTemplateId(SS, R.getLookupNameInfo(), |
| 4172 | R.getAsSingle<VarTemplateDecl>(), |
| 4173 | TemplateKWLoc, TemplateArgs); |
| 4174 | } |
| 4175 | |
| 4176 | |
| 4177 | R.suppressDiagnostics(); |
| 4178 | |
| 4179 | UnresolvedLookupExpr *ULE |
| 4180 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
| 4181 | SS.getWithLocInContext(Context), |
| 4182 | TemplateKWLoc, |
| 4183 | R.getLookupNameInfo(), |
| 4184 | RequiresADL, TemplateArgs, |
| 4185 | R.begin(), R.end()); |
| 4186 | |
| 4187 | return ULE; |
| 4188 | } |
| 4189 | |
| 4190 | |
| 4191 | ExprResult |
| 4192 | Sema::BuildQualifiedTemplateIdExpr(CXXScopeSpec &SS, |
| 4193 | SourceLocation TemplateKWLoc, |
| 4194 | const DeclarationNameInfo &NameInfo, |
| 4195 | const TemplateArgumentListInfo *TemplateArgs) { |
| 4196 | |
| 4197 | assert(TemplateArgs || TemplateKWLoc.isValid()); |
| 4198 | DeclContext *DC; |
| 4199 | if (!(DC = computeDeclContext(SS, false)) || |
| 4200 | DC->isDependentContext() || |
| 4201 | RequireCompleteDeclContext(SS, DC)) |
| 4202 | return BuildDependentDeclRefExpr(SS, TemplateKWLoc, NameInfo, TemplateArgs); |
| 4203 | |
| 4204 | bool MemberOfUnknownSpecialization; |
| 4205 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
| 4206 | if (LookupTemplateName(R, (Scope *)nullptr, SS, QualType(), |
| 4207 | , MemberOfUnknownSpecialization, |
| 4208 | TemplateKWLoc)) |
| 4209 | return ExprError(); |
| 4210 | |
| 4211 | if (R.isAmbiguous()) |
| 4212 | return ExprError(); |
| 4213 | |
| 4214 | if (R.empty()) { |
| 4215 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 4216 | << NameInfo.getName() << DC << SS.getRange(); |
| 4217 | return ExprError(); |
| 4218 | } |
| 4219 | |
| 4220 | if (ClassTemplateDecl *Temp = R.getAsSingle<ClassTemplateDecl>()) { |
| 4221 | Diag(NameInfo.getLoc(), diag::err_template_kw_refers_to_class_template) |
| 4222 | << SS.getScopeRep() |
| 4223 | << NameInfo.getName().getAsString() << SS.getRange(); |
| 4224 | Diag(Temp->getLocation(), diag::note_referenced_class_template); |
| 4225 | return ExprError(); |
| 4226 | } |
| 4227 | |
| 4228 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, false, TemplateArgs); |
| 4229 | } |
| 4230 | |
| 4231 | |
| 4232 | |
| 4233 | |
| 4234 | |
| 4235 | |
| 4236 | |
| 4237 | |
| 4238 | TemplateNameKind Sema::ActOnDependentTemplateName(Scope *S, |
| 4239 | CXXScopeSpec &SS, |
| 4240 | SourceLocation TemplateKWLoc, |
| 4241 | const UnqualifiedId &Name, |
| 4242 | ParsedType ObjectType, |
| 4243 | bool EnteringContext, |
| 4244 | TemplateTy &Result, |
| 4245 | bool AllowInjectedClassName) { |
| 4246 | if (TemplateKWLoc.isValid() && S && !S->getTemplateParamParent()) |
| 4247 | Diag(TemplateKWLoc, |
| 4248 | getLangOpts().CPlusPlus11 ? |
| 4249 | diag::warn_cxx98_compat_template_outside_of_template : |
| 4250 | diag::ext_template_outside_of_template) |
| 4251 | << FixItHint::CreateRemoval(TemplateKWLoc); |
| 4252 | |
| 4253 | DeclContext *LookupCtx = nullptr; |
| 4254 | if (SS.isSet()) |
| 4255 | LookupCtx = computeDeclContext(SS, EnteringContext); |
| 4256 | if (!LookupCtx && ObjectType) |
| 4257 | LookupCtx = computeDeclContext(ObjectType.get()); |
| 4258 | if (LookupCtx) { |
| 4259 | |
| 4260 | |
| 4261 | |
| 4262 | |
| 4263 | |
| 4264 | |
| 4265 | |
| 4266 | |
| 4267 | |
| 4268 | |
| 4269 | |
| 4270 | |
| 4271 | |
| 4272 | |
| 4273 | |
| 4274 | |
| 4275 | bool MemberOfUnknownSpecialization; |
| 4276 | TemplateNameKind TNK = isTemplateName(S, SS, TemplateKWLoc.isValid(), Name, |
| 4277 | ObjectType, EnteringContext, Result, |
| 4278 | MemberOfUnknownSpecialization); |
| 4279 | if (TNK == TNK_Non_template && MemberOfUnknownSpecialization) { |
| 4280 | |
| 4281 | } else if (TNK == TNK_Non_template) { |
| 4282 | |
| 4283 | |
| 4284 | |
| 4285 | DeclarationNameInfo DNI = GetNameFromUnqualifiedId(Name); |
| 4286 | LookupResult R(*this, DNI.getName(), Name.getBeginLoc(), |
| 4287 | LookupOrdinaryName); |
| 4288 | bool MOUS; |
| 4289 | if (!LookupTemplateName(R, S, SS, ObjectType.get(), EnteringContext, |
| 4290 | MOUS, TemplateKWLoc) && !R.isAmbiguous()) |
| 4291 | Diag(Name.getBeginLoc(), diag::err_no_member) |
| 4292 | << DNI.getName() << LookupCtx << SS.getRange(); |
| 4293 | return TNK_Non_template; |
| 4294 | } else { |
| 4295 | |
| 4296 | auto *LookupRD = dyn_cast<CXXRecordDecl>(LookupCtx); |
| 4297 | if (!AllowInjectedClassName && SS.isSet() && LookupRD && |
| 4298 | Name.getKind() == UnqualifiedIdKind::IK_Identifier && |
| 4299 | Name.Identifier && LookupRD->getIdentifier() == Name.Identifier) { |
| 4300 | |
| 4301 | |
| 4302 | |
| 4303 | |
| 4304 | |
| 4305 | |
| 4306 | |
| 4307 | |
| 4308 | |
| 4309 | Diag(Name.getBeginLoc(), |
| 4310 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 4311 | << Name.Identifier |
| 4312 | << 0 |
| 4313 | << 1 ; |
| 4314 | } |
| 4315 | return TNK; |
| 4316 | } |
| 4317 | } |
| 4318 | |
| 4319 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
| 4320 | |
| 4321 | switch (Name.getKind()) { |
| 4322 | case UnqualifiedIdKind::IK_Identifier: |
| 4323 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 4324 | Name.Identifier)); |
| 4325 | return TNK_Dependent_template_name; |
| 4326 | |
| 4327 | case UnqualifiedIdKind::IK_OperatorFunctionId: |
| 4328 | Result = TemplateTy::make(Context.getDependentTemplateName(Qualifier, |
| 4329 | Name.OperatorFunctionId.Operator)); |
| 4330 | return TNK_Function_template; |
| 4331 | |
| 4332 | case UnqualifiedIdKind::IK_LiteralOperatorId: |
| 4333 | llvm_unreachable("literal operator id cannot have a dependent scope"); |
| 4334 | |
| 4335 | default: |
| 4336 | break; |
| 4337 | } |
| 4338 | |
| 4339 | Diag(Name.getBeginLoc(), diag::err_template_kw_refers_to_non_template) |
| 4340 | << GetNameFromUnqualifiedId(Name).getName() << Name.getSourceRange() |
| 4341 | << TemplateKWLoc; |
| 4342 | return TNK_Non_template; |
| 4343 | } |
| 4344 | |
| 4345 | bool Sema::CheckTemplateTypeArgument(TemplateTypeParmDecl *Param, |
| 4346 | TemplateArgumentLoc &AL, |
| 4347 | SmallVectorImpl<TemplateArgument> &Converted) { |
| 4348 | const TemplateArgument &Arg = AL.getArgument(); |
| 4349 | QualType ArgType; |
| 4350 | TypeSourceInfo *TSI = nullptr; |
| 4351 | |
| 4352 | |
| 4353 | switch(Arg.getKind()) { |
| 4354 | case TemplateArgument::Type: |
| 4355 | |
| 4356 | |
| 4357 | |
| 4358 | ArgType = Arg.getAsType(); |
| 4359 | TSI = AL.getTypeSourceInfo(); |
| 4360 | break; |
| 4361 | case TemplateArgument::Template: |
| 4362 | case TemplateArgument::TemplateExpansion: { |
| 4363 | |
| 4364 | |
| 4365 | SourceRange SR = AL.getSourceRange(); |
| 4366 | TemplateName Name = Arg.getAsTemplateOrTemplatePattern(); |
| 4367 | diagnoseMissingTemplateArguments(Name, SR.getEnd()); |
| 4368 | return true; |
| 4369 | } |
| 4370 | case TemplateArgument::Expression: { |
| 4371 | |
| 4372 | |
| 4373 | CXXScopeSpec SS; |
| 4374 | DeclarationNameInfo NameInfo; |
| 4375 | |
| 4376 | if (DeclRefExpr *ArgExpr = dyn_cast<DeclRefExpr>(Arg.getAsExpr())) { |
| 4377 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 4378 | NameInfo = ArgExpr->getNameInfo(); |
| 4379 | } else if (DependentScopeDeclRefExpr *ArgExpr = |
| 4380 | dyn_cast<DependentScopeDeclRefExpr>(Arg.getAsExpr())) { |
| 4381 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 4382 | NameInfo = ArgExpr->getNameInfo(); |
| 4383 | } else if (CXXDependentScopeMemberExpr *ArgExpr = |
| 4384 | dyn_cast<CXXDependentScopeMemberExpr>(Arg.getAsExpr())) { |
| 4385 | if (ArgExpr->isImplicitAccess()) { |
| 4386 | SS.Adopt(ArgExpr->getQualifierLoc()); |
| 4387 | NameInfo = ArgExpr->getMemberNameInfo(); |
| 4388 | } |
| 4389 | } |
| 4390 | |
| 4391 | if (auto *II = NameInfo.getName().getAsIdentifierInfo()) { |
| 4392 | LookupResult Result(*this, NameInfo, LookupOrdinaryName); |
| 4393 | LookupParsedName(Result, CurScope, &SS); |
| 4394 | |
| 4395 | if (Result.getAsSingle<TypeDecl>() || |
| 4396 | Result.getResultKind() == |
| 4397 | LookupResult::NotFoundInCurrentInstantiation) { |
| 4398 | |
| 4399 | SourceLocation Loc = AL.getSourceRange().getBegin(); |
| 4400 | Diag(Loc, getLangOpts().MSVCCompat |
| 4401 | ? diag::ext_ms_template_type_arg_missing_typename |
| 4402 | : diag::err_template_arg_must_be_type_suggest) |
| 4403 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 4404 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4405 | |
| 4406 | |
| 4407 | |
| 4408 | ArgType = |
| 4409 | Context.getDependentNameType(ETK_Typename, SS.getScopeRep(), II); |
| 4410 | TypeLocBuilder TLB; |
| 4411 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(ArgType); |
| 4412 | TL.setElaboratedKeywordLoc(SourceLocation()); |
| 4413 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 4414 | TL.setNameLoc(NameInfo.getLoc()); |
| 4415 | TSI = TLB.getTypeSourceInfo(Context, ArgType); |
| 4416 | |
| 4417 | |
| 4418 | |
| 4419 | AL = TemplateArgumentLoc(TemplateArgument(ArgType), |
| 4420 | TemplateArgumentLocInfo(TSI)); |
| 4421 | |
| 4422 | break; |
| 4423 | } |
| 4424 | } |
| 4425 | |
| 4426 | LLVM_FALLTHROUGH; |
| 4427 | } |
| 4428 | default: { |
| 4429 | |
| 4430 | |
| 4431 | SourceRange SR = AL.getSourceRange(); |
| 4432 | Diag(SR.getBegin(), diag::err_template_arg_must_be_type) << SR; |
| 4433 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4434 | |
| 4435 | return true; |
| 4436 | } |
| 4437 | } |
| 4438 | |
| 4439 | if (CheckTemplateArgument(Param, TSI)) |
| 4440 | return true; |
| 4441 | |
| 4442 | |
| 4443 | ArgType = Context.getCanonicalType(ArgType); |
| 4444 | |
| 4445 | |
| 4446 | |
| 4447 | |
| 4448 | if (getLangOpts().ObjCAutoRefCount && |
| 4449 | ArgType->isObjCLifetimeType() && |
| 4450 | !ArgType.getObjCLifetime()) { |
| 4451 | Qualifiers Qs; |
| 4452 | Qs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 4453 | ArgType = Context.getQualifiedType(ArgType, Qs); |
| 4454 | } |
| 4455 | |
| 4456 | Converted.push_back(TemplateArgument(ArgType)); |
| 4457 | return false; |
| 4458 | } |
| 4459 | |
| 4460 | |
| 4461 | |
| 4462 | |
| 4463 | |
| 4464 | |
| 4465 | |
| 4466 | |
| 4467 | |
| 4468 | |
| 4469 | |
| 4470 | |
| 4471 | |
| 4472 | |
| 4473 | |
| 4474 | |
| 4475 | |
| 4476 | |
| 4477 | |
| 4478 | |
| 4479 | |
| 4480 | |
| 4481 | static TypeSourceInfo * |
| 4482 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 4483 | TemplateDecl *Template, |
| 4484 | SourceLocation TemplateLoc, |
| 4485 | SourceLocation RAngleLoc, |
| 4486 | TemplateTypeParmDecl *Param, |
| 4487 | SmallVectorImpl<TemplateArgument> &Converted) { |
| 4488 | TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); |
| 4489 | |
| 4490 | |
| 4491 | |
| 4492 | if (ArgType->getType()->isInstantiationDependentType()) { |
| 4493 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 4494 | Param, Template, Converted, |
| 4495 | SourceRange(TemplateLoc, RAngleLoc)); |
| 4496 | if (Inst.isInvalid()) |
| 4497 | return nullptr; |
| 4498 | |
| 4499 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
| 4500 | |
| 4501 | |
| 4502 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4503 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 4504 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 4505 | TemplateArgLists.addOuterTemplateArguments(None); |
| 4506 | |
| 4507 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
| 4508 | ArgType = |
| 4509 | SemaRef.SubstType(ArgType, TemplateArgLists, |
| 4510 | Param->getDefaultArgumentLoc(), Param->getDeclName()); |
| 4511 | } |
| 4512 | |
| 4513 | return ArgType; |
| 4514 | } |
| 4515 | |
| 4516 | |
| 4517 | |
| 4518 | |
| 4519 | |
| 4520 | |
| 4521 | |
| 4522 | |
| 4523 | |
| 4524 | |
| 4525 | |
| 4526 | |
| 4527 | |
| 4528 | |
| 4529 | |
| 4530 | |
| 4531 | |
| 4532 | |
| 4533 | |
| 4534 | |
| 4535 | |
| 4536 | |
| 4537 | |
| 4538 | static ExprResult |
| 4539 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 4540 | TemplateDecl *Template, |
| 4541 | SourceLocation TemplateLoc, |
| 4542 | SourceLocation RAngleLoc, |
| 4543 | NonTypeTemplateParmDecl *Param, |
| 4544 | SmallVectorImpl<TemplateArgument> &Converted) { |
| 4545 | Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, |
| 4546 | Param, Template, Converted, |
| 4547 | SourceRange(TemplateLoc, RAngleLoc)); |
| 4548 | if (Inst.isInvalid()) |
| 4549 | return ExprError(); |
| 4550 | |
| 4551 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
| 4552 | |
| 4553 | |
| 4554 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4555 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 4556 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 4557 | TemplateArgLists.addOuterTemplateArguments(None); |
| 4558 | |
| 4559 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 4560 | SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 4561 | return SemaRef.SubstExpr(Param->getDefaultArgument(), TemplateArgLists); |
| 4562 | } |
| 4563 | |
| 4564 | |
| 4565 | |
| 4566 | |
| 4567 | |
| 4568 | |
| 4569 | |
| 4570 | |
| 4571 | |
| 4572 | |
| 4573 | |
| 4574 | |
| 4575 | |
| 4576 | |
| 4577 | |
| 4578 | |
| 4579 | |
| 4580 | |
| 4581 | |
| 4582 | |
| 4583 | |
| 4584 | |
| 4585 | |
| 4586 | |
| 4587 | |
| 4588 | |
| 4589 | static TemplateName |
| 4590 | SubstDefaultTemplateArgument(Sema &SemaRef, |
| 4591 | TemplateDecl *Template, |
| 4592 | SourceLocation TemplateLoc, |
| 4593 | SourceLocation RAngleLoc, |
| 4594 | TemplateTemplateParmDecl *Param, |
| 4595 | SmallVectorImpl<TemplateArgument> &Converted, |
| 4596 | NestedNameSpecifierLoc &QualifierLoc) { |
| 4597 | Sema::InstantiatingTemplate Inst( |
| 4598 | SemaRef, TemplateLoc, TemplateParameter(Param), Template, Converted, |
| 4599 | SourceRange(TemplateLoc, RAngleLoc)); |
| 4600 | if (Inst.isInvalid()) |
| 4601 | return TemplateName(); |
| 4602 | |
| 4603 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
| 4604 | |
| 4605 | |
| 4606 | MultiLevelTemplateArgumentList TemplateArgLists; |
| 4607 | TemplateArgLists.addOuterTemplateArguments(&TemplateArgs); |
| 4608 | for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) |
| 4609 | TemplateArgLists.addOuterTemplateArguments(None); |
| 4610 | |
| 4611 | Sema::ContextRAII SavedContext(SemaRef, Template->getDeclContext()); |
| 4612 | |
| 4613 | QualifierLoc = Param->getDefaultArgument().getTemplateQualifierLoc(); |
| 4614 | if (QualifierLoc) { |
| 4615 | QualifierLoc = |
| 4616 | SemaRef.SubstNestedNameSpecifierLoc(QualifierLoc, TemplateArgLists); |
| 4617 | if (!QualifierLoc) |
| 4618 | return TemplateName(); |
| 4619 | } |
| 4620 | |
| 4621 | return SemaRef.SubstTemplateName( |
| 4622 | QualifierLoc, |
| 4623 | Param->getDefaultArgument().getArgument().getAsTemplate(), |
| 4624 | Param->getDefaultArgument().getTemplateNameLoc(), |
| 4625 | TemplateArgLists); |
| 4626 | } |
| 4627 | |
| 4628 | |
| 4629 | |
| 4630 | |
| 4631 | TemplateArgumentLoc |
| 4632 | Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, |
| 4633 | SourceLocation TemplateLoc, |
| 4634 | SourceLocation RAngleLoc, |
| 4635 | Decl *Param, |
| 4636 | SmallVectorImpl<TemplateArgument> |
| 4637 | &Converted, |
| 4638 | bool &HasDefaultArg) { |
| 4639 | HasDefaultArg = false; |
| 4640 | |
| 4641 | if (TemplateTypeParmDecl *TypeParm = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 4642 | if (!hasVisibleDefaultArgument(TypeParm)) |
| 4643 | return TemplateArgumentLoc(); |
| 4644 | |
| 4645 | HasDefaultArg = true; |
| 4646 | TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, |
| 4647 | TemplateLoc, |
| 4648 | RAngleLoc, |
| 4649 | TypeParm, |
| 4650 | Converted); |
| 4651 | if (DI) |
| 4652 | return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); |
| 4653 | |
| 4654 | return TemplateArgumentLoc(); |
| 4655 | } |
| 4656 | |
| 4657 | if (NonTypeTemplateParmDecl *NonTypeParm |
| 4658 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4659 | if (!hasVisibleDefaultArgument(NonTypeParm)) |
| 4660 | return TemplateArgumentLoc(); |
| 4661 | |
| 4662 | HasDefaultArg = true; |
| 4663 | ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, |
| 4664 | TemplateLoc, |
| 4665 | RAngleLoc, |
| 4666 | NonTypeParm, |
| 4667 | Converted); |
| 4668 | if (Arg.isInvalid()) |
| 4669 | return TemplateArgumentLoc(); |
| 4670 | |
| 4671 | Expr *ArgE = Arg.getAs<Expr>(); |
| 4672 | return TemplateArgumentLoc(TemplateArgument(ArgE), ArgE); |
| 4673 | } |
| 4674 | |
| 4675 | TemplateTemplateParmDecl *TempTempParm |
| 4676 | = cast<TemplateTemplateParmDecl>(Param); |
| 4677 | if (!hasVisibleDefaultArgument(TempTempParm)) |
| 4678 | return TemplateArgumentLoc(); |
| 4679 | |
| 4680 | HasDefaultArg = true; |
| 4681 | NestedNameSpecifierLoc QualifierLoc; |
| 4682 | TemplateName TName = SubstDefaultTemplateArgument(*this, Template, |
| 4683 | TemplateLoc, |
| 4684 | RAngleLoc, |
| 4685 | TempTempParm, |
| 4686 | Converted, |
| 4687 | QualifierLoc); |
| 4688 | if (TName.isNull()) |
| 4689 | return TemplateArgumentLoc(); |
| 4690 | |
| 4691 | return TemplateArgumentLoc(TemplateArgument(TName), |
| 4692 | TempTempParm->getDefaultArgument().getTemplateQualifierLoc(), |
| 4693 | TempTempParm->getDefaultArgument().getTemplateNameLoc()); |
| 4694 | } |
| 4695 | |
| 4696 | |
| 4697 | |
| 4698 | |
| 4699 | static TemplateArgumentLoc convertTypeTemplateArgumentToTemplate(TypeLoc TLoc) { |
| 4700 | |
| 4701 | NestedNameSpecifierLoc QualLoc; |
| 4702 | if (auto ETLoc = TLoc.getAs<ElaboratedTypeLoc>()) { |
| 4703 | if (ETLoc.getTypePtr()->getKeyword() != ETK_None) |
| 4704 | return TemplateArgumentLoc(); |
| 4705 | |
| 4706 | QualLoc = ETLoc.getQualifierLoc(); |
| 4707 | TLoc = ETLoc.getNamedTypeLoc(); |
| 4708 | } |
| 4709 | |
| 4710 | |
| 4711 | |
| 4712 | if (auto InjLoc = TLoc.getAs<InjectedClassNameTypeLoc>()) |
| 4713 | return TemplateArgumentLoc(InjLoc.getTypePtr()->getTemplateName(), |
| 4714 | QualLoc, InjLoc.getNameLoc()); |
| 4715 | |
| 4716 | |
| 4717 | |
| 4718 | |
| 4719 | |
| 4720 | if (auto RecLoc = TLoc.getAs<RecordTypeLoc>()) |
| 4721 | if (auto *CTSD = |
| 4722 | dyn_cast<ClassTemplateSpecializationDecl>(RecLoc.getDecl())) |
| 4723 | return TemplateArgumentLoc(TemplateName(CTSD->getSpecializedTemplate()), |
| 4724 | QualLoc, RecLoc.getNameLoc()); |
| 4725 | |
| 4726 | return TemplateArgumentLoc(); |
| 4727 | } |
| 4728 | |
| 4729 | |
| 4730 | |
| 4731 | |
| 4732 | |
| 4733 | |
| 4734 | |
| 4735 | |
| 4736 | |
| 4737 | |
| 4738 | |
| 4739 | |
| 4740 | |
| 4741 | |
| 4742 | |
| 4743 | |
| 4744 | |
| 4745 | |
| 4746 | |
| 4747 | |
| 4748 | |
| 4749 | |
| 4750 | |
| 4751 | |
| 4752 | |
| 4753 | |
| 4754 | |
| 4755 | bool Sema::CheckTemplateArgument(NamedDecl *Param, |
| 4756 | TemplateArgumentLoc &Arg, |
| 4757 | NamedDecl *Template, |
| 4758 | SourceLocation TemplateLoc, |
| 4759 | SourceLocation RAngleLoc, |
| 4760 | unsigned ArgumentPackIndex, |
| 4761 | SmallVectorImpl<TemplateArgument> &Converted, |
| 4762 | CheckTemplateArgumentKind CTAK) { |
| 4763 | |
| 4764 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 4765 | return CheckTemplateTypeArgument(TTP, Arg, Converted); |
| 4766 | |
| 4767 | |
| 4768 | if (NonTypeTemplateParmDecl *NTTP =dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 4769 | |
| 4770 | |
| 4771 | |
| 4772 | QualType NTTPType = NTTP->getType(); |
| 4773 | if (NTTP->isParameterPack() && NTTP->isExpandedParameterPack()) |
| 4774 | NTTPType = NTTP->getExpansionType(ArgumentPackIndex); |
| 4775 | |
| 4776 | |
| 4777 | |
| 4778 | if (NTTPType->isDependentType() && |
| 4779 | !isa<TemplateTemplateParmDecl>(Template) && |
| 4780 | !Template->getDeclContext()->isDependentContext()) { |
| 4781 | |
| 4782 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 4783 | NTTP, Converted, |
| 4784 | SourceRange(TemplateLoc, RAngleLoc)); |
| 4785 | if (Inst.isInvalid()) |
| 4786 | return true; |
| 4787 | |
| 4788 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 4789 | Converted); |
| 4790 | NTTPType = SubstType(NTTPType, |
| 4791 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 4792 | NTTP->getLocation(), |
| 4793 | NTTP->getDeclName()); |
| 4794 | |
| 4795 | |
| 4796 | if (!NTTPType.isNull()) |
| 4797 | NTTPType = CheckNonTypeTemplateParameterType(NTTPType, |
| 4798 | NTTP->getLocation()); |
| 4799 | if (NTTPType.isNull()) |
| 4800 | return true; |
| 4801 | } |
| 4802 | |
| 4803 | switch (Arg.getArgument().getKind()) { |
| 4804 | case TemplateArgument::Null: |
| 4805 | llvm_unreachable("Should never see a NULL template argument here"); |
| 4806 | |
| 4807 | case TemplateArgument::Expression: { |
| 4808 | TemplateArgument Result; |
| 4809 | unsigned CurSFINAEErrors = NumSFINAEErrors; |
| 4810 | ExprResult Res = |
| 4811 | CheckTemplateArgument(NTTP, NTTPType, Arg.getArgument().getAsExpr(), |
| 4812 | Result, CTAK); |
| 4813 | if (Res.isInvalid()) |
| 4814 | return true; |
| 4815 | |
| 4816 | if (CurSFINAEErrors < NumSFINAEErrors) |
| 4817 | return true; |
| 4818 | |
| 4819 | |
| 4820 | |
| 4821 | if (Res.get() != Arg.getArgument().getAsExpr()) { |
| 4822 | TemplateArgument TA(Res.get()); |
| 4823 | Arg = TemplateArgumentLoc(TA, Res.get()); |
| 4824 | } |
| 4825 | |
| 4826 | Converted.push_back(Result); |
| 4827 | break; |
| 4828 | } |
| 4829 | |
| 4830 | case TemplateArgument::Declaration: |
| 4831 | case TemplateArgument::Integral: |
| 4832 | case TemplateArgument::NullPtr: |
| 4833 | |
| 4834 | |
| 4835 | Converted.push_back(Arg.getArgument()); |
| 4836 | break; |
| 4837 | |
| 4838 | case TemplateArgument::Template: |
| 4839 | case TemplateArgument::TemplateExpansion: |
| 4840 | |
| 4841 | |
| 4842 | if (DependentTemplateName *DTN |
| 4843 | = Arg.getArgument().getAsTemplateOrTemplatePattern() |
| 4844 | .getAsDependentTemplateName()) { |
| 4845 | |
| 4846 | |
| 4847 | |
| 4848 | |
| 4849 | |
| 4850 | DeclarationNameInfo NameInfo(DTN->getIdentifier(), |
| 4851 | Arg.getTemplateNameLoc()); |
| 4852 | |
| 4853 | CXXScopeSpec SS; |
| 4854 | SS.Adopt(Arg.getTemplateQualifierLoc()); |
| 4855 | |
| 4856 | |
| 4857 | |
| 4858 | SourceLocation TemplateKWLoc; |
| 4859 | ExprResult E = DependentScopeDeclRefExpr::Create( |
| 4860 | Context, SS.getWithLocInContext(Context), TemplateKWLoc, NameInfo, |
| 4861 | nullptr); |
| 4862 | |
| 4863 | |
| 4864 | |
| 4865 | if (Arg.getArgument().getKind() == TemplateArgument::TemplateExpansion){ |
| 4866 | E = ActOnPackExpansion(E.get(), Arg.getTemplateEllipsisLoc()); |
| 4867 | if (E.isInvalid()) |
| 4868 | return true; |
| 4869 | } |
| 4870 | |
| 4871 | TemplateArgument Result; |
| 4872 | E = CheckTemplateArgument(NTTP, NTTPType, E.get(), Result); |
| 4873 | if (E.isInvalid()) |
| 4874 | return true; |
| 4875 | |
| 4876 | Converted.push_back(Result); |
| 4877 | break; |
| 4878 | } |
| 4879 | |
| 4880 | |
| 4881 | |
| 4882 | |
| 4883 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_expr) |
| 4884 | << Arg.getSourceRange(); |
| 4885 | |
| 4886 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4887 | return true; |
| 4888 | |
| 4889 | case TemplateArgument::Type: { |
| 4890 | |
| 4891 | |
| 4892 | |
| 4893 | |
| 4894 | |
| 4895 | |
| 4896 | |
| 4897 | |
| 4898 | |
| 4899 | |
| 4900 | QualType T = Arg.getArgument().getAsType(); |
| 4901 | SourceRange SR = Arg.getSourceRange(); |
| 4902 | if (T->isFunctionType()) |
| 4903 | Diag(SR.getBegin(), diag::err_template_arg_nontype_ambig) << SR << T; |
| 4904 | else |
| 4905 | Diag(SR.getBegin(), diag::err_template_arg_must_be_expr) << SR; |
| 4906 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 4907 | return true; |
| 4908 | } |
| 4909 | |
| 4910 | case TemplateArgument::Pack: |
| 4911 | llvm_unreachable("Caller must expand template argument packs"); |
| 4912 | } |
| 4913 | |
| 4914 | return false; |
| 4915 | } |
| 4916 | |
| 4917 | |
| 4918 | |
| 4919 | TemplateTemplateParmDecl *TempParm = cast<TemplateTemplateParmDecl>(Param); |
| 4920 | |
| 4921 | TemplateParameterList *Params = TempParm->getTemplateParameters(); |
| 4922 | if (TempParm->isExpandedParameterPack()) |
| 4923 | Params = TempParm->getExpansionTemplateParameters(ArgumentPackIndex); |
| 4924 | |
| 4925 | |
| 4926 | |
| 4927 | |
| 4928 | |
| 4929 | |
| 4930 | { |
| 4931 | |
| 4932 | LocalInstantiationScope Scope(*this); |
| 4933 | InstantiatingTemplate Inst(*this, TemplateLoc, Template, |
| 4934 | TempParm, Converted, |
| 4935 | SourceRange(TemplateLoc, RAngleLoc)); |
| 4936 | if (Inst.isInvalid()) |
| 4937 | return true; |
| 4938 | |
| 4939 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); |
| 4940 | Params = SubstTemplateParams(Params, CurContext, |
| 4941 | MultiLevelTemplateArgumentList(TemplateArgs)); |
| 4942 | if (!Params) |
| 4943 | return true; |
| 4944 | } |
| 4945 | |
| 4946 | |
| 4947 | |
| 4948 | |
| 4949 | |
| 4950 | if (Arg.getArgument().getKind() == TemplateArgument::Type) { |
| 4951 | TemplateArgumentLoc ConvertedArg = convertTypeTemplateArgumentToTemplate( |
| 4952 | Arg.getTypeSourceInfo()->getTypeLoc()); |
| 4953 | if (!ConvertedArg.getArgument().isNull()) |
| 4954 | Arg = ConvertedArg; |
| 4955 | } |
| 4956 | |
| 4957 | switch (Arg.getArgument().getKind()) { |
| 4958 | case TemplateArgument::Null: |
| 4959 | llvm_unreachable("Should never see a NULL template argument here"); |
| 4960 | |
| 4961 | case TemplateArgument::Template: |
| 4962 | case TemplateArgument::TemplateExpansion: |
| 4963 | if (CheckTemplateTemplateArgument(Params, Arg)) |
| 4964 | return true; |
| 4965 | |
| 4966 | Converted.push_back(Arg.getArgument()); |
| 4967 | break; |
| 4968 | |
| 4969 | case TemplateArgument::Expression: |
| 4970 | case TemplateArgument::Type: |
| 4971 | |
| 4972 | |
| 4973 | Diag(Arg.getLocation(), diag::err_template_arg_must_be_template) |
| 4974 | << getLangOpts().CPlusPlus11; |
| 4975 | return true; |
| 4976 | |
| 4977 | case TemplateArgument::Declaration: |
| 4978 | llvm_unreachable("Declaration argument with template template parameter"); |
| 4979 | case TemplateArgument::Integral: |
| 4980 | llvm_unreachable("Integral argument with template template parameter"); |
| 4981 | case TemplateArgument::NullPtr: |
| 4982 | llvm_unreachable("Null pointer argument with template template parameter"); |
| 4983 | |
| 4984 | case TemplateArgument::Pack: |
| 4985 | llvm_unreachable("Caller must expand template argument packs"); |
| 4986 | } |
| 4987 | |
| 4988 | return false; |
| 4989 | } |
| 4990 | |
| 4991 | |
| 4992 | |
| 4993 | |
| 4994 | |
| 4995 | |
| 4996 | |
| 4997 | |
| 4998 | |
| 4999 | |
| 5000 | |
| 5001 | |
| 5002 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
| 5003 | if (NonTypeTemplateParmDecl *NTTP |
| 5004 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 5005 | if (NTTP->isExpandedParameterPack()) |
| 5006 | return NTTP->getNumExpansionTypes(); |
| 5007 | } |
| 5008 | |
| 5009 | if (TemplateTemplateParmDecl *TTP |
| 5010 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 5011 | if (TTP->isExpandedParameterPack()) |
| 5012 | return TTP->getNumExpansionTemplateParameters(); |
| 5013 | } |
| 5014 | |
| 5015 | return None; |
| 5016 | } |
| 5017 | |
| 5018 | |
| 5019 | template<typename TemplateParmDecl> |
| 5020 | static bool diagnoseMissingArgument(Sema &S, SourceLocation Loc, |
| 5021 | TemplateDecl *TD, |
| 5022 | const TemplateParmDecl *D, |
| 5023 | TemplateArgumentListInfo &Args) { |
| 5024 | |
| 5025 | |
| 5026 | D = cast<TemplateParmDecl>(cast<TemplateDecl>(TD->getMostRecentDecl()) |
| 5027 | ->getTemplateParameters() |
| 5028 | ->getParam(D->getIndex())); |
| 5029 | |
| 5030 | |
| 5031 | |
| 5032 | llvm::SmallVector<Module*, 8> Modules; |
| 5033 | if (D->hasDefaultArgument() && !S.hasVisibleDefaultArgument(D, &Modules)) { |
| 5034 | S.diagnoseMissingImport(Loc, cast<NamedDecl>(TD), |
| 5035 | D->getDefaultArgumentLoc(), Modules, |
| 5036 | Sema::MissingImportKind::DefaultArgument, |
| 5037 | ); |
| 5038 | return true; |
| 5039 | } |
| 5040 | |
| 5041 | |
| 5042 | |
| 5043 | |
| 5044 | TemplateParameterList *Params = TD->getTemplateParameters(); |
| 5045 | |
| 5046 | S.Diag(Loc, diag::err_template_arg_list_different_arity) |
| 5047 | << |
| 5048 | << (int)S.getTemplateNameKindForDiagnostics(TemplateName(TD)) |
| 5049 | << TD; |
| 5050 | S.Diag(TD->getLocation(), diag::note_template_decl_here) |
| 5051 | << Params->getSourceRange(); |
| 5052 | return true; |
| 5053 | } |
| 5054 | |
| 5055 | |
| 5056 | |
| 5057 | bool Sema::CheckTemplateArgumentList( |
| 5058 | TemplateDecl *Template, SourceLocation TemplateLoc, |
| 5059 | TemplateArgumentListInfo &TemplateArgs, bool PartialTemplateArgs, |
| 5060 | SmallVectorImpl<TemplateArgument> &Converted, |
| 5061 | bool UpdateArgsWithConversions) { |
| 5062 | |
| 5063 | |
| 5064 | |
| 5065 | TemplateArgumentListInfo NewArgs = TemplateArgs; |
| 5066 | |
| 5067 | |
| 5068 | |
| 5069 | |
| 5070 | TemplateParameterList *Params = |
| 5071 | cast<TemplateDecl>(Template->getMostRecentDecl()) |
| 5072 | ->getTemplateParameters(); |
| 5073 | |
| 5074 | SourceLocation RAngleLoc = NewArgs.getRAngleLoc(); |
| 5075 | |
| 5076 | |
| 5077 | |
| 5078 | |
| 5079 | |
| 5080 | |
| 5081 | bool isTemplateTemplateParameter = isa<TemplateTemplateParmDecl>(Template); |
| 5082 | SmallVector<TemplateArgument, 2> ArgumentPack; |
| 5083 | unsigned ArgIdx = 0, NumArgs = NewArgs.size(); |
| 5084 | LocalInstantiationScope InstScope(*this, true); |
| 5085 | for (TemplateParameterList::iterator Param = Params->begin(), |
| 5086 | ParamEnd = Params->end(); |
| 5087 | Param != ParamEnd; ) { |
| 5088 | |
| 5089 | |
| 5090 | if (Optional<unsigned> Expansions = getExpandedPackSize(*Param)) { |
| 5091 | if (*Expansions == ArgumentPack.size()) { |
| 5092 | |
| 5093 | |
| 5094 | Converted.push_back( |
| 5095 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
| 5096 | ArgumentPack.clear(); |
| 5097 | |
| 5098 | |
| 5099 | ++Param; |
| 5100 | continue; |
| 5101 | } else if (ArgIdx == NumArgs && !PartialTemplateArgs) { |
| 5102 | |
| 5103 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 5104 | << |
| 5105 | << (int)getTemplateNameKindForDiagnostics(TemplateName(Template)) |
| 5106 | << Template; |
| 5107 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 5108 | << Params->getSourceRange(); |
| 5109 | return true; |
| 5110 | } |
| 5111 | } |
| 5112 | |
| 5113 | if (ArgIdx < NumArgs) { |
| 5114 | |
| 5115 | if (CheckTemplateArgument(*Param, NewArgs[ArgIdx], Template, |
| 5116 | TemplateLoc, RAngleLoc, |
| 5117 | ArgumentPack.size(), Converted)) |
| 5118 | return true; |
| 5119 | |
| 5120 | bool PackExpansionIntoNonPack = |
| 5121 | NewArgs[ArgIdx].getArgument().isPackExpansion() && |
| 5122 | (!(*Param)->isTemplateParameterPack() || getExpandedPackSize(*Param)); |
| 5123 | if (PackExpansionIntoNonPack && isa<TypeAliasTemplateDecl>(Template)) { |
| 5124 | |
| 5125 | |
| 5126 | |
| 5127 | Diag(NewArgs[ArgIdx].getLocation(), |
| 5128 | diag::err_alias_template_expansion_into_fixed_list) |
| 5129 | << NewArgs[ArgIdx].getSourceRange(); |
| 5130 | Diag((*Param)->getLocation(), diag::note_template_param_here); |
| 5131 | return true; |
| 5132 | } |
| 5133 | |
| 5134 | |
| 5135 | ++ArgIdx; |
| 5136 | |
| 5137 | if ((*Param)->isTemplateParameterPack()) { |
| 5138 | |
| 5139 | |
| 5140 | |
| 5141 | |
| 5142 | ArgumentPack.push_back(Converted.pop_back_val()); |
| 5143 | } else { |
| 5144 | |
| 5145 | ++Param; |
| 5146 | } |
| 5147 | |
| 5148 | |
| 5149 | |
| 5150 | |
| 5151 | if (PackExpansionIntoNonPack) { |
| 5152 | if (!ArgumentPack.empty()) { |
| 5153 | |
| 5154 | |
| 5155 | Converted.insert(Converted.end(), |
| 5156 | ArgumentPack.begin(), ArgumentPack.end()); |
| 5157 | ArgumentPack.clear(); |
| 5158 | } |
| 5159 | |
| 5160 | while (ArgIdx < NumArgs) { |
| 5161 | Converted.push_back(NewArgs[ArgIdx].getArgument()); |
| 5162 | ++ArgIdx; |
| 5163 | } |
| 5164 | |
| 5165 | return false; |
| 5166 | } |
| 5167 | |
| 5168 | continue; |
| 5169 | } |
| 5170 | |
| 5171 | |
| 5172 | if (PartialTemplateArgs) { |
| 5173 | if ((*Param)->isTemplateParameterPack() && !ArgumentPack.empty()) |
| 5174 | Converted.push_back( |
| 5175 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
| 5176 | |
| 5177 | return false; |
| 5178 | } |
| 5179 | |
| 5180 | |
| 5181 | |
| 5182 | if ((*Param)->isTemplateParameterPack()) { |
| 5183 | (0) . __assert_fail ("!getExpandedPackSize(*Param) && \"Should have dealt with this already\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 5184, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!getExpandedPackSize(*Param) && |
| 5184 | (0) . __assert_fail ("!getExpandedPackSize(*Param) && \"Should have dealt with this already\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 5184, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Should have dealt with this already"); |
| 5185 | |
| 5186 | |
| 5187 | |
| 5188 | |
| 5189 | if (Param + 1 != ParamEnd) |
| 5190 | return true; |
| 5191 | |
| 5192 | Converted.push_back( |
| 5193 | TemplateArgument::CreatePackCopy(Context, ArgumentPack)); |
| 5194 | ArgumentPack.clear(); |
| 5195 | |
| 5196 | ++Param; |
| 5197 | continue; |
| 5198 | } |
| 5199 | |
| 5200 | |
| 5201 | TemplateArgumentLoc Arg; |
| 5202 | |
| 5203 | |
| 5204 | |
| 5205 | |
| 5206 | |
| 5207 | |
| 5208 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(*Param)) { |
| 5209 | if (!hasVisibleDefaultArgument(TTP)) |
| 5210 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TTP, |
| 5211 | NewArgs); |
| 5212 | |
| 5213 | TypeSourceInfo *ArgType = SubstDefaultTemplateArgument(*this, |
| 5214 | Template, |
| 5215 | TemplateLoc, |
| 5216 | RAngleLoc, |
| 5217 | TTP, |
| 5218 | Converted); |
| 5219 | if (!ArgType) |
| 5220 | return true; |
| 5221 | |
| 5222 | Arg = TemplateArgumentLoc(TemplateArgument(ArgType->getType()), |
| 5223 | ArgType); |
| 5224 | } else if (NonTypeTemplateParmDecl *NTTP |
| 5225 | = dyn_cast<NonTypeTemplateParmDecl>(*Param)) { |
| 5226 | if (!hasVisibleDefaultArgument(NTTP)) |
| 5227 | return diagnoseMissingArgument(*this, TemplateLoc, Template, NTTP, |
| 5228 | NewArgs); |
| 5229 | |
| 5230 | ExprResult E = SubstDefaultTemplateArgument(*this, Template, |
| 5231 | TemplateLoc, |
| 5232 | RAngleLoc, |
| 5233 | NTTP, |
| 5234 | Converted); |
| 5235 | if (E.isInvalid()) |
| 5236 | return true; |
| 5237 | |
| 5238 | Expr *Ex = E.getAs<Expr>(); |
| 5239 | Arg = TemplateArgumentLoc(TemplateArgument(Ex), Ex); |
| 5240 | } else { |
| 5241 | TemplateTemplateParmDecl *TempParm |
| 5242 | = cast<TemplateTemplateParmDecl>(*Param); |
| 5243 | |
| 5244 | if (!hasVisibleDefaultArgument(TempParm)) |
| 5245 | return diagnoseMissingArgument(*this, TemplateLoc, Template, TempParm, |
| 5246 | NewArgs); |
| 5247 | |
| 5248 | NestedNameSpecifierLoc QualifierLoc; |
| 5249 | TemplateName Name = SubstDefaultTemplateArgument(*this, Template, |
| 5250 | TemplateLoc, |
| 5251 | RAngleLoc, |
| 5252 | TempParm, |
| 5253 | Converted, |
| 5254 | QualifierLoc); |
| 5255 | if (Name.isNull()) |
| 5256 | return true; |
| 5257 | |
| 5258 | Arg = TemplateArgumentLoc(TemplateArgument(Name), QualifierLoc, |
| 5259 | TempParm->getDefaultArgument().getTemplateNameLoc()); |
| 5260 | } |
| 5261 | |
| 5262 | |
| 5263 | |
| 5264 | |
| 5265 | |
| 5266 | InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, Converted, |
| 5267 | SourceRange(TemplateLoc, RAngleLoc)); |
| 5268 | if (Inst.isInvalid()) |
| 5269 | return true; |
| 5270 | |
| 5271 | |
| 5272 | if (CheckTemplateArgument(*Param, Arg, Template, TemplateLoc, |
| 5273 | RAngleLoc, 0, Converted)) |
| 5274 | return true; |
| 5275 | |
| 5276 | |
| 5277 | |
| 5278 | |
| 5279 | if (isTemplateTemplateParameter) |
| 5280 | NewArgs.addArgument(Arg); |
| 5281 | |
| 5282 | |
| 5283 | ++Param; |
| 5284 | ++ArgIdx; |
| 5285 | } |
| 5286 | |
| 5287 | |
| 5288 | |
| 5289 | |
| 5290 | |
| 5291 | if (ArgIdx < NumArgs && CurrentInstantiationScope && |
| 5292 | CurrentInstantiationScope->getPartiallySubstitutedPack()) { |
| 5293 | while (ArgIdx < NumArgs && NewArgs[ArgIdx].getArgument().isPackExpansion()) |
| 5294 | Converted.push_back(NewArgs[ArgIdx++].getArgument()); |
| 5295 | } |
| 5296 | |
| 5297 | |
| 5298 | |
| 5299 | if (ArgIdx < NumArgs) { |
| 5300 | Diag(TemplateLoc, diag::err_template_arg_list_different_arity) |
| 5301 | << |
| 5302 | << (int)getTemplateNameKindForDiagnostics(TemplateName(Template)) |
| 5303 | << Template |
| 5304 | << SourceRange(NewArgs[ArgIdx].getLocation(), NewArgs.getRAngleLoc()); |
| 5305 | Diag(Template->getLocation(), diag::note_template_decl_here) |
| 5306 | << Params->getSourceRange(); |
| 5307 | return true; |
| 5308 | } |
| 5309 | |
| 5310 | |
| 5311 | |
| 5312 | if (UpdateArgsWithConversions) |
| 5313 | TemplateArgs = std::move(NewArgs); |
| 5314 | |
| 5315 | return false; |
| 5316 | } |
| 5317 | |
| 5318 | namespace { |
| 5319 | class UnnamedLocalNoLinkageFinder |
| 5320 | : public TypeVisitor<UnnamedLocalNoLinkageFinder, bool> |
| 5321 | { |
| 5322 | Sema &S; |
| 5323 | SourceRange SR; |
| 5324 | |
| 5325 | typedef TypeVisitor<UnnamedLocalNoLinkageFinder, bool> inherited; |
| 5326 | |
| 5327 | public: |
| 5328 | UnnamedLocalNoLinkageFinder(Sema &S, SourceRange SR) : S(S), SR(SR) { } |
| 5329 | |
| 5330 | bool Visit(QualType T) { |
| 5331 | return T.isNull() ? false : inherited::Visit(T.getTypePtr()); |
| 5332 | } |
| 5333 | |
| 5334 | #define TYPE(Class, Parent) \ |
| 5335 | bool Visit##Class##Type(const Class##Type *); |
| 5336 | #define ABSTRACT_TYPE(Class, Parent) \ |
| 5337 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 5338 | #define NON_CANONICAL_TYPE(Class, Parent) \ |
| 5339 | bool Visit##Class##Type(const Class##Type *) { return false; } |
| 5340 | #include "clang/AST/TypeNodes.def" |
| 5341 | |
| 5342 | bool VisitTagDecl(const TagDecl *Tag); |
| 5343 | bool VisitNestedNameSpecifier(NestedNameSpecifier *NNS); |
| 5344 | }; |
| 5345 | } |
| 5346 | |
| 5347 | bool UnnamedLocalNoLinkageFinder::VisitBuiltinType(const BuiltinType*) { |
| 5348 | return false; |
| 5349 | } |
| 5350 | |
| 5351 | bool UnnamedLocalNoLinkageFinder::VisitComplexType(const ComplexType* T) { |
| 5352 | return Visit(T->getElementType()); |
| 5353 | } |
| 5354 | |
| 5355 | bool UnnamedLocalNoLinkageFinder::VisitPointerType(const PointerType* T) { |
| 5356 | return Visit(T->getPointeeType()); |
| 5357 | } |
| 5358 | |
| 5359 | bool UnnamedLocalNoLinkageFinder::VisitBlockPointerType( |
| 5360 | const BlockPointerType* T) { |
| 5361 | return Visit(T->getPointeeType()); |
| 5362 | } |
| 5363 | |
| 5364 | bool UnnamedLocalNoLinkageFinder::VisitLValueReferenceType( |
| 5365 | const LValueReferenceType* T) { |
| 5366 | return Visit(T->getPointeeType()); |
| 5367 | } |
| 5368 | |
| 5369 | bool UnnamedLocalNoLinkageFinder::VisitRValueReferenceType( |
| 5370 | const RValueReferenceType* T) { |
| 5371 | return Visit(T->getPointeeType()); |
| 5372 | } |
| 5373 | |
| 5374 | bool UnnamedLocalNoLinkageFinder::VisitMemberPointerType( |
| 5375 | const MemberPointerType* T) { |
| 5376 | return Visit(T->getPointeeType()) || Visit(QualType(T->getClass(), 0)); |
| 5377 | } |
| 5378 | |
| 5379 | bool UnnamedLocalNoLinkageFinder::VisitConstantArrayType( |
| 5380 | const ConstantArrayType* T) { |
| 5381 | return Visit(T->getElementType()); |
| 5382 | } |
| 5383 | |
| 5384 | bool UnnamedLocalNoLinkageFinder::VisitIncompleteArrayType( |
| 5385 | const IncompleteArrayType* T) { |
| 5386 | return Visit(T->getElementType()); |
| 5387 | } |
| 5388 | |
| 5389 | bool UnnamedLocalNoLinkageFinder::VisitVariableArrayType( |
| 5390 | const VariableArrayType* T) { |
| 5391 | return Visit(T->getElementType()); |
| 5392 | } |
| 5393 | |
| 5394 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedArrayType( |
| 5395 | const DependentSizedArrayType* T) { |
| 5396 | return Visit(T->getElementType()); |
| 5397 | } |
| 5398 | |
| 5399 | bool UnnamedLocalNoLinkageFinder::VisitDependentSizedExtVectorType( |
| 5400 | const DependentSizedExtVectorType* T) { |
| 5401 | return Visit(T->getElementType()); |
| 5402 | } |
| 5403 | |
| 5404 | bool UnnamedLocalNoLinkageFinder::VisitDependentAddressSpaceType( |
| 5405 | const DependentAddressSpaceType *T) { |
| 5406 | return Visit(T->getPointeeType()); |
| 5407 | } |
| 5408 | |
| 5409 | bool UnnamedLocalNoLinkageFinder::VisitVectorType(const VectorType* T) { |
| 5410 | return Visit(T->getElementType()); |
| 5411 | } |
| 5412 | |
| 5413 | bool UnnamedLocalNoLinkageFinder::VisitDependentVectorType( |
| 5414 | const DependentVectorType *T) { |
| 5415 | return Visit(T->getElementType()); |
| 5416 | } |
| 5417 | |
| 5418 | bool UnnamedLocalNoLinkageFinder::VisitExtVectorType(const ExtVectorType* T) { |
| 5419 | return Visit(T->getElementType()); |
| 5420 | } |
| 5421 | |
| 5422 | bool UnnamedLocalNoLinkageFinder::VisitFunctionProtoType( |
| 5423 | const FunctionProtoType* T) { |
| 5424 | for (const auto &A : T->param_types()) { |
| 5425 | if (Visit(A)) |
| 5426 | return true; |
| 5427 | } |
| 5428 | |
| 5429 | return Visit(T->getReturnType()); |
| 5430 | } |
| 5431 | |
| 5432 | bool UnnamedLocalNoLinkageFinder::VisitFunctionNoProtoType( |
| 5433 | const FunctionNoProtoType* T) { |
| 5434 | return Visit(T->getReturnType()); |
| 5435 | } |
| 5436 | |
| 5437 | bool UnnamedLocalNoLinkageFinder::VisitUnresolvedUsingType( |
| 5438 | const UnresolvedUsingType*) { |
| 5439 | return false; |
| 5440 | } |
| 5441 | |
| 5442 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfExprType(const TypeOfExprType*) { |
| 5443 | return false; |
| 5444 | } |
| 5445 | |
| 5446 | bool UnnamedLocalNoLinkageFinder::VisitTypeOfType(const TypeOfType* T) { |
| 5447 | return Visit(T->getUnderlyingType()); |
| 5448 | } |
| 5449 | |
| 5450 | bool UnnamedLocalNoLinkageFinder::VisitDecltypeType(const DecltypeType*) { |
| 5451 | return false; |
| 5452 | } |
| 5453 | |
| 5454 | bool UnnamedLocalNoLinkageFinder::VisitUnaryTransformType( |
| 5455 | const UnaryTransformType*) { |
| 5456 | return false; |
| 5457 | } |
| 5458 | |
| 5459 | bool UnnamedLocalNoLinkageFinder::VisitAutoType(const AutoType *T) { |
| 5460 | return Visit(T->getDeducedType()); |
| 5461 | } |
| 5462 | |
| 5463 | bool UnnamedLocalNoLinkageFinder::VisitDeducedTemplateSpecializationType( |
| 5464 | const DeducedTemplateSpecializationType *T) { |
| 5465 | return Visit(T->getDeducedType()); |
| 5466 | } |
| 5467 | |
| 5468 | bool UnnamedLocalNoLinkageFinder::VisitRecordType(const RecordType* T) { |
| 5469 | return VisitTagDecl(T->getDecl()); |
| 5470 | } |
| 5471 | |
| 5472 | bool UnnamedLocalNoLinkageFinder::VisitEnumType(const EnumType* T) { |
| 5473 | return VisitTagDecl(T->getDecl()); |
| 5474 | } |
| 5475 | |
| 5476 | bool UnnamedLocalNoLinkageFinder::VisitTemplateTypeParmType( |
| 5477 | const TemplateTypeParmType*) { |
| 5478 | return false; |
| 5479 | } |
| 5480 | |
| 5481 | bool UnnamedLocalNoLinkageFinder::VisitSubstTemplateTypeParmPackType( |
| 5482 | const SubstTemplateTypeParmPackType *) { |
| 5483 | return false; |
| 5484 | } |
| 5485 | |
| 5486 | bool UnnamedLocalNoLinkageFinder::VisitTemplateSpecializationType( |
| 5487 | const TemplateSpecializationType*) { |
| 5488 | return false; |
| 5489 | } |
| 5490 | |
| 5491 | bool UnnamedLocalNoLinkageFinder::VisitInjectedClassNameType( |
| 5492 | const InjectedClassNameType* T) { |
| 5493 | return VisitTagDecl(T->getDecl()); |
| 5494 | } |
| 5495 | |
| 5496 | bool UnnamedLocalNoLinkageFinder::VisitDependentNameType( |
| 5497 | const DependentNameType* T) { |
| 5498 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 5499 | } |
| 5500 | |
| 5501 | bool UnnamedLocalNoLinkageFinder::VisitDependentTemplateSpecializationType( |
| 5502 | const DependentTemplateSpecializationType* T) { |
| 5503 | return VisitNestedNameSpecifier(T->getQualifier()); |
| 5504 | } |
| 5505 | |
| 5506 | bool UnnamedLocalNoLinkageFinder::VisitPackExpansionType( |
| 5507 | const PackExpansionType* T) { |
| 5508 | return Visit(T->getPattern()); |
| 5509 | } |
| 5510 | |
| 5511 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectType(const ObjCObjectType *) { |
| 5512 | return false; |
| 5513 | } |
| 5514 | |
| 5515 | bool UnnamedLocalNoLinkageFinder::VisitObjCInterfaceType( |
| 5516 | const ObjCInterfaceType *) { |
| 5517 | return false; |
| 5518 | } |
| 5519 | |
| 5520 | bool UnnamedLocalNoLinkageFinder::VisitObjCObjectPointerType( |
| 5521 | const ObjCObjectPointerType *) { |
| 5522 | return false; |
| 5523 | } |
| 5524 | |
| 5525 | bool UnnamedLocalNoLinkageFinder::VisitAtomicType(const AtomicType* T) { |
| 5526 | return Visit(T->getValueType()); |
| 5527 | } |
| 5528 | |
| 5529 | bool UnnamedLocalNoLinkageFinder::VisitPipeType(const PipeType* T) { |
| 5530 | return false; |
| 5531 | } |
| 5532 | |
| 5533 | bool UnnamedLocalNoLinkageFinder::VisitTagDecl(const TagDecl *Tag) { |
| 5534 | if (Tag->getDeclContext()->isFunctionOrMethod()) { |
| 5535 | S.Diag(SR.getBegin(), |
| 5536 | S.getLangOpts().CPlusPlus11 ? |
| 5537 | diag::warn_cxx98_compat_template_arg_local_type : |
| 5538 | diag::ext_template_arg_local_type) |
| 5539 | << S.Context.getTypeDeclType(Tag) << SR; |
| 5540 | return true; |
| 5541 | } |
| 5542 | |
| 5543 | if (!Tag->hasNameForLinkage()) { |
| 5544 | S.Diag(SR.getBegin(), |
| 5545 | S.getLangOpts().CPlusPlus11 ? |
| 5546 | diag::warn_cxx98_compat_template_arg_unnamed_type : |
| 5547 | diag::ext_template_arg_unnamed_type) << SR; |
| 5548 | S.Diag(Tag->getLocation(), diag::note_template_unnamed_type_here); |
| 5549 | return true; |
| 5550 | } |
| 5551 | |
| 5552 | return false; |
| 5553 | } |
| 5554 | |
| 5555 | bool UnnamedLocalNoLinkageFinder::VisitNestedNameSpecifier( |
| 5556 | NestedNameSpecifier *NNS) { |
| 5557 | if (NNS->getPrefix() && VisitNestedNameSpecifier(NNS->getPrefix())) |
| 5558 | return true; |
| 5559 | |
| 5560 | switch (NNS->getKind()) { |
| 5561 | case NestedNameSpecifier::Identifier: |
| 5562 | case NestedNameSpecifier::Namespace: |
| 5563 | case NestedNameSpecifier::NamespaceAlias: |
| 5564 | case NestedNameSpecifier::Global: |
| 5565 | case NestedNameSpecifier::Super: |
| 5566 | return false; |
| 5567 | |
| 5568 | case NestedNameSpecifier::TypeSpec: |
| 5569 | case NestedNameSpecifier::TypeSpecWithTemplate: |
| 5570 | return Visit(QualType(NNS->getAsType(), 0)); |
| 5571 | } |
| 5572 | llvm_unreachable("Invalid NestedNameSpecifier::Kind!"); |
| 5573 | } |
| 5574 | |
| 5575 | |
| 5576 | |
| 5577 | |
| 5578 | |
| 5579 | |
| 5580 | bool Sema::CheckTemplateArgument(TemplateTypeParmDecl *Param, |
| 5581 | TypeSourceInfo *ArgInfo) { |
| 5582 | (0) . __assert_fail ("ArgInfo && \"invalid TypeSourceInfo\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 5582, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ArgInfo && "invalid TypeSourceInfo"); |
| 5583 | QualType Arg = ArgInfo->getType(); |
| 5584 | SourceRange SR = ArgInfo->getTypeLoc().getSourceRange(); |
| 5585 | |
| 5586 | if (Arg->isVariablyModifiedType()) { |
| 5587 | return Diag(SR.getBegin(), diag::err_variably_modified_template_arg) << Arg; |
| 5588 | } else if (Context.hasSameUnqualifiedType(Arg, Context.OverloadTy)) { |
| 5589 | return Diag(SR.getBegin(), diag::err_template_arg_overload_type) << SR; |
| 5590 | } |
| 5591 | |
| 5592 | |
| 5593 | |
| 5594 | |
| 5595 | |
| 5596 | |
| 5597 | |
| 5598 | |
| 5599 | if (LangOpts.CPlusPlus11 || Arg->hasUnnamedOrLocalType()) { |
| 5600 | UnnamedLocalNoLinkageFinder Finder(*this, SR); |
| 5601 | (void)Finder.Visit(Context.getCanonicalType(Arg)); |
| 5602 | } |
| 5603 | |
| 5604 | return false; |
| 5605 | } |
| 5606 | |
| 5607 | enum NullPointerValueKind { |
| 5608 | NPV_NotNullPointer, |
| 5609 | NPV_NullPointer, |
| 5610 | NPV_Error |
| 5611 | }; |
| 5612 | |
| 5613 | |
| 5614 | |
| 5615 | static NullPointerValueKind |
| 5616 | isNullPointerValueTemplateArgument(Sema &S, NonTypeTemplateParmDecl *Param, |
| 5617 | QualType ParamType, Expr *Arg, |
| 5618 | Decl *Entity = nullptr) { |
| 5619 | if (Arg->isValueDependent() || Arg->isTypeDependent()) |
| 5620 | return NPV_NotNullPointer; |
| 5621 | |
| 5622 | |
| 5623 | |
| 5624 | if (Entity && Entity->hasAttr<DLLImportAttr>()) |
| 5625 | return NPV_NotNullPointer; |
| 5626 | |
| 5627 | if (!S.isCompleteType(Arg->getExprLoc(), ParamType)) |
| 5628 | llvm_unreachable( |
| 5629 | "Incomplete parameter type in isNullPointerValueTemplateArgument!"); |
| 5630 | |
| 5631 | if (!S.getLangOpts().CPlusPlus11) |
| 5632 | return NPV_NotNullPointer; |
| 5633 | |
| 5634 | |
| 5635 | ExprResult ArgRV = S.DefaultFunctionArrayConversion(Arg); |
| 5636 | if (ArgRV.isInvalid()) |
| 5637 | return NPV_Error; |
| 5638 | Arg = ArgRV.get(); |
| 5639 | |
| 5640 | Expr::EvalResult EvalResult; |
| 5641 | SmallVector<PartialDiagnosticAt, 8> Notes; |
| 5642 | EvalResult.Diag = &Notes; |
| 5643 | if (!Arg->EvaluateAsRValue(EvalResult, S.Context) || |
| 5644 | EvalResult.HasSideEffects) { |
| 5645 | SourceLocation DiagLoc = Arg->getExprLoc(); |
| 5646 | |
| 5647 | |
| 5648 | |
| 5649 | |
| 5650 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 5651 | diag::note_invalid_subexpr_in_const_expr) { |
| 5652 | DiagLoc = Notes[0].first; |
| 5653 | Notes.clear(); |
| 5654 | } |
| 5655 | |
| 5656 | S.Diag(DiagLoc, diag::err_template_arg_not_address_constant) |
| 5657 | << Arg->getType() << Arg->getSourceRange(); |
| 5658 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 5659 | S.Diag(Notes[I].first, Notes[I].second); |
| 5660 | |
| 5661 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5662 | return NPV_Error; |
| 5663 | } |
| 5664 | |
| 5665 | |
| 5666 | |
| 5667 | if (Arg->getType()->isNullPtrType()) |
| 5668 | return NPV_NullPointer; |
| 5669 | |
| 5670 | |
| 5671 | |
| 5672 | |
| 5673 | if ((EvalResult.Val.isLValue() && !EvalResult.Val.getLValueBase()) || |
| 5674 | (EvalResult.Val.isMemberPointer() && |
| 5675 | !EvalResult.Val.getMemberPointerDecl())) { |
| 5676 | |
| 5677 | bool ObjCLifetimeConversion; |
| 5678 | if (S.Context.hasSameUnqualifiedType(Arg->getType(), ParamType) || |
| 5679 | S.IsQualificationConversion(Arg->getType(), ParamType, false, |
| 5680 | ObjCLifetimeConversion)) |
| 5681 | return NPV_NullPointer; |
| 5682 | |
| 5683 | |
| 5684 | |
| 5685 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_wrongtype_null_constant) |
| 5686 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 5687 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5688 | return NPV_NullPointer; |
| 5689 | } |
| 5690 | |
| 5691 | |
| 5692 | |
| 5693 | if (Arg->isNullPointerConstant(S.Context, Expr::NPC_NeverValueDependent)) { |
| 5694 | std::string Code = "static_cast<" + ParamType.getAsString() + ">("; |
| 5695 | S.Diag(Arg->getExprLoc(), diag::err_template_arg_untyped_null_constant) |
| 5696 | << ParamType << FixItHint::CreateInsertion(Arg->getBeginLoc(), Code) |
| 5697 | << FixItHint::CreateInsertion(S.getLocForEndOfToken(Arg->getEndLoc()), |
| 5698 | ")"); |
| 5699 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5700 | return NPV_NullPointer; |
| 5701 | } |
| 5702 | |
| 5703 | |
| 5704 | |
| 5705 | |
| 5706 | return NPV_NotNullPointer; |
| 5707 | } |
| 5708 | |
| 5709 | |
| 5710 | |
| 5711 | static bool CheckTemplateArgumentIsCompatibleWithParameter( |
| 5712 | Sema &S, NonTypeTemplateParmDecl *Param, QualType ParamType, Expr *ArgIn, |
| 5713 | Expr *Arg, QualType ArgType) { |
| 5714 | bool ObjCLifetimeConversion; |
| 5715 | if (ParamType->isPointerType() && |
| 5716 | !ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType() && |
| 5717 | S.IsQualificationConversion(ArgType, ParamType, false, |
| 5718 | ObjCLifetimeConversion)) { |
| 5719 | |
| 5720 | |
| 5721 | } else { |
| 5722 | if (const ReferenceType *ParamRef = ParamType->getAs<ReferenceType>()) { |
| 5723 | if (!ParamRef->getPointeeType()->isFunctionType()) { |
| 5724 | |
| 5725 | |
| 5726 | |
| 5727 | |
| 5728 | |
| 5729 | |
| 5730 | |
| 5731 | |
| 5732 | |
| 5733 | unsigned ParamQuals = ParamRef->getPointeeType().getCVRQualifiers(); |
| 5734 | unsigned ArgQuals = ArgType.getCVRQualifiers(); |
| 5735 | |
| 5736 | if ((ParamQuals | ArgQuals) != ParamQuals) { |
| 5737 | S.Diag(Arg->getBeginLoc(), |
| 5738 | diag::err_template_arg_ref_bind_ignores_quals) |
| 5739 | << ParamType << Arg->getType() << Arg->getSourceRange(); |
| 5740 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5741 | return true; |
| 5742 | } |
| 5743 | } |
| 5744 | } |
| 5745 | |
| 5746 | |
| 5747 | |
| 5748 | |
| 5749 | if (!S.Context.hasSameUnqualifiedType(ArgType, |
| 5750 | ParamType.getNonReferenceType())) { |
| 5751 | |
| 5752 | if (ParamType->isReferenceType()) |
| 5753 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_no_ref_bind) |
| 5754 | << ParamType << ArgIn->getType() << Arg->getSourceRange(); |
| 5755 | else |
| 5756 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_convertible) |
| 5757 | << ArgIn->getType() << ParamType << Arg->getSourceRange(); |
| 5758 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5759 | return true; |
| 5760 | } |
| 5761 | } |
| 5762 | |
| 5763 | return false; |
| 5764 | } |
| 5765 | |
| 5766 | |
| 5767 | |
| 5768 | static bool |
| 5769 | CheckTemplateArgumentAddressOfObjectOrFunction(Sema &S, |
| 5770 | NonTypeTemplateParmDecl *Param, |
| 5771 | QualType ParamType, |
| 5772 | Expr *ArgIn, |
| 5773 | TemplateArgument &Converted) { |
| 5774 | bool Invalid = false; |
| 5775 | Expr *Arg = ArgIn; |
| 5776 | QualType ArgType = Arg->getType(); |
| 5777 | |
| 5778 | bool AddressTaken = false; |
| 5779 | SourceLocation AddrOpLoc; |
| 5780 | if (S.getLangOpts().MicrosoftExt) { |
| 5781 | |
| 5782 | |
| 5783 | Arg = Arg->IgnoreParenCasts(); |
| 5784 | |
| 5785 | bool ExtWarnMSTemplateArg = false; |
| 5786 | UnaryOperatorKind FirstOpKind; |
| 5787 | SourceLocation FirstOpLoc; |
| 5788 | while (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 5789 | UnaryOperatorKind UnOpKind = UnOp->getOpcode(); |
| 5790 | if (UnOpKind == UO_Deref) |
| 5791 | ExtWarnMSTemplateArg = true; |
| 5792 | if (UnOpKind == UO_AddrOf || UnOpKind == UO_Deref) { |
| 5793 | Arg = UnOp->getSubExpr()->IgnoreParenCasts(); |
| 5794 | if (!AddrOpLoc.isValid()) { |
| 5795 | FirstOpKind = UnOpKind; |
| 5796 | FirstOpLoc = UnOp->getOperatorLoc(); |
| 5797 | } |
| 5798 | } else |
| 5799 | break; |
| 5800 | } |
| 5801 | if (FirstOpLoc.isValid()) { |
| 5802 | if (ExtWarnMSTemplateArg) |
| 5803 | S.Diag(ArgIn->getBeginLoc(), diag::ext_ms_deref_template_argument) |
| 5804 | << ArgIn->getSourceRange(); |
| 5805 | |
| 5806 | if (FirstOpKind == UO_AddrOf) |
| 5807 | AddressTaken = true; |
| 5808 | else if (Arg->getType()->isPointerType()) { |
| 5809 | |
| 5810 | |
| 5811 | assert(FirstOpKind == UO_Deref); |
| 5812 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) |
| 5813 | << Arg->getSourceRange(); |
| 5814 | } |
| 5815 | } |
| 5816 | } else { |
| 5817 | |
| 5818 | Arg = Arg->IgnoreImpCasts(); |
| 5819 | |
| 5820 | |
| 5821 | |
| 5822 | |
| 5823 | |
| 5824 | |
| 5825 | |
| 5826 | |
| 5827 | |
| 5828 | |
| 5829 | |
| 5830 | |
| 5831 | |
| 5832 | |
| 5833 | |
| 5834 | bool = false; |
| 5835 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 5836 | if (!Invalid && !ExtraParens) { |
| 5837 | S.Diag(Arg->getBeginLoc(), |
| 5838 | S.getLangOpts().CPlusPlus11 |
| 5839 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 5840 | : diag::ext_template_arg_extra_parens) |
| 5841 | << Arg->getSourceRange(); |
| 5842 | ExtraParens = true; |
| 5843 | } |
| 5844 | |
| 5845 | Arg = Parens->getSubExpr(); |
| 5846 | } |
| 5847 | |
| 5848 | while (SubstNonTypeTemplateParmExpr *subst = |
| 5849 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 5850 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 5851 | |
| 5852 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 5853 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 5854 | Arg = UnOp->getSubExpr(); |
| 5855 | AddressTaken = true; |
| 5856 | AddrOpLoc = UnOp->getOperatorLoc(); |
| 5857 | } |
| 5858 | } |
| 5859 | |
| 5860 | while (SubstNonTypeTemplateParmExpr *subst = |
| 5861 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 5862 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 5863 | } |
| 5864 | |
| 5865 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Arg); |
| 5866 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 5867 | |
| 5868 | |
| 5869 | if (ParamType->isPointerType() || ParamType->isNullPtrType()) { |
| 5870 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, ArgIn, |
| 5871 | Entity)) { |
| 5872 | case NPV_NullPointer: |
| 5873 | S.Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
| 5874 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 5875 | ); |
| 5876 | return false; |
| 5877 | |
| 5878 | case NPV_Error: |
| 5879 | return true; |
| 5880 | |
| 5881 | case NPV_NotNullPointer: |
| 5882 | break; |
| 5883 | } |
| 5884 | } |
| 5885 | |
| 5886 | |
| 5887 | |
| 5888 | if (Arg->isValueDependent()) { |
| 5889 | Converted = TemplateArgument(ArgIn); |
| 5890 | return false; |
| 5891 | } |
| 5892 | |
| 5893 | if (isa<CXXUuidofExpr>(Arg)) { |
| 5894 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, |
| 5895 | ArgIn, Arg, ArgType)) |
| 5896 | return true; |
| 5897 | |
| 5898 | Converted = TemplateArgument(ArgIn); |
| 5899 | return false; |
| 5900 | } |
| 5901 | |
| 5902 | if (!DRE) { |
| 5903 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) |
| 5904 | << Arg->getSourceRange(); |
| 5905 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5906 | return true; |
| 5907 | } |
| 5908 | |
| 5909 | |
| 5910 | if (isa<FieldDecl>(Entity) || isa<IndirectFieldDecl>(Entity)) { |
| 5911 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_field) |
| 5912 | << Entity << Arg->getSourceRange(); |
| 5913 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5914 | return true; |
| 5915 | } |
| 5916 | |
| 5917 | |
| 5918 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Entity)) { |
| 5919 | if (!Method->isStatic()) { |
| 5920 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_method) |
| 5921 | << Method << Arg->getSourceRange(); |
| 5922 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5923 | return true; |
| 5924 | } |
| 5925 | } |
| 5926 | |
| 5927 | FunctionDecl *Func = dyn_cast<FunctionDecl>(Entity); |
| 5928 | VarDecl *Var = dyn_cast<VarDecl>(Entity); |
| 5929 | |
| 5930 | |
| 5931 | if (!Func && !Var) { |
| 5932 | |
| 5933 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_object_or_func) |
| 5934 | << Arg->getSourceRange(); |
| 5935 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 5936 | return true; |
| 5937 | } |
| 5938 | |
| 5939 | |
| 5940 | if (Entity->getFormalLinkage() == InternalLinkage) { |
| 5941 | S.Diag(Arg->getBeginLoc(), |
| 5942 | S.getLangOpts().CPlusPlus11 |
| 5943 | ? diag::warn_cxx98_compat_template_arg_object_internal |
| 5944 | : diag::ext_template_arg_object_internal) |
| 5945 | << !Func << Entity << Arg->getSourceRange(); |
| 5946 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 5947 | << !Func; |
| 5948 | } else if (!Entity->hasLinkage()) { |
| 5949 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_object_no_linkage) |
| 5950 | << !Func << Entity << Arg->getSourceRange(); |
| 5951 | S.Diag(Entity->getLocation(), diag::note_template_arg_internal_object) |
| 5952 | << !Func; |
| 5953 | return true; |
| 5954 | } |
| 5955 | |
| 5956 | if (Func) { |
| 5957 | |
| 5958 | if (ParamType->isPointerType() && !AddressTaken) |
| 5959 | ArgType = S.Context.getPointerType(Func->getType()); |
| 5960 | else if (AddressTaken && ParamType->isReferenceType()) { |
| 5961 | |
| 5962 | |
| 5963 | |
| 5964 | if (!S.Context.hasSameUnqualifiedType(Func->getType(), |
| 5965 | ParamType.getNonReferenceType())) { |
| 5966 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5967 | << ParamType; |
| 5968 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5969 | return true; |
| 5970 | } |
| 5971 | |
| 5972 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 5973 | << ParamType |
| 5974 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 5975 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5976 | |
| 5977 | ArgType = Func->getType(); |
| 5978 | } |
| 5979 | } else { |
| 5980 | |
| 5981 | if (Var->getType()->isReferenceType()) { |
| 5982 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_reference_var) |
| 5983 | << Var->getType() << Arg->getSourceRange(); |
| 5984 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 5985 | return true; |
| 5986 | } |
| 5987 | |
| 5988 | |
| 5989 | if (Var->getTLSKind()) { |
| 5990 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_thread_local) |
| 5991 | << Arg->getSourceRange(); |
| 5992 | S.Diag(Var->getLocation(), diag::note_template_arg_refers_here); |
| 5993 | return true; |
| 5994 | } |
| 5995 | |
| 5996 | |
| 5997 | |
| 5998 | if (ParamType->isReferenceType()) { |
| 5999 | if (AddressTaken) { |
| 6000 | |
| 6001 | |
| 6002 | |
| 6003 | if (!S.Context.hasSameUnqualifiedType(Var->getType(), |
| 6004 | ParamType.getNonReferenceType())) { |
| 6005 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 6006 | << ParamType; |
| 6007 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6008 | return true; |
| 6009 | } |
| 6010 | |
| 6011 | S.Diag(AddrOpLoc, diag::err_template_arg_address_of_non_pointer) |
| 6012 | << ParamType |
| 6013 | << FixItHint::CreateRemoval(AddrOpLoc); |
| 6014 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6015 | |
| 6016 | ArgType = Var->getType(); |
| 6017 | } |
| 6018 | } else if (!AddressTaken && ParamType->isPointerType()) { |
| 6019 | if (Var->getType()->isArrayType()) { |
| 6020 | |
| 6021 | ArgType = S.Context.getArrayDecayedType(Var->getType()); |
| 6022 | } else { |
| 6023 | |
| 6024 | |
| 6025 | |
| 6026 | ArgType = S.Context.getPointerType(Var->getType()); |
| 6027 | if (!S.Context.hasSameUnqualifiedType(ArgType, ParamType)) { |
| 6028 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_address_of) |
| 6029 | << ParamType; |
| 6030 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6031 | return true; |
| 6032 | } |
| 6033 | |
| 6034 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_address_of) |
| 6035 | << ParamType << FixItHint::CreateInsertion(Arg->getBeginLoc(), "&"); |
| 6036 | |
| 6037 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6038 | } |
| 6039 | } |
| 6040 | } |
| 6041 | |
| 6042 | if (CheckTemplateArgumentIsCompatibleWithParameter(S, Param, ParamType, ArgIn, |
| 6043 | Arg, ArgType)) |
| 6044 | return true; |
| 6045 | |
| 6046 | |
| 6047 | Converted = |
| 6048 | TemplateArgument(cast<ValueDecl>(Entity->getCanonicalDecl()), ParamType); |
| 6049 | S.MarkAnyDeclReferenced(Arg->getBeginLoc(), Entity, false); |
| 6050 | return false; |
| 6051 | } |
| 6052 | |
| 6053 | |
| 6054 | |
| 6055 | static bool CheckTemplateArgumentPointerToMember(Sema &S, |
| 6056 | NonTypeTemplateParmDecl *Param, |
| 6057 | QualType ParamType, |
| 6058 | Expr *&ResultArg, |
| 6059 | TemplateArgument &Converted) { |
| 6060 | bool Invalid = false; |
| 6061 | |
| 6062 | Expr *Arg = ResultArg; |
| 6063 | bool ObjCLifetimeConversion; |
| 6064 | |
| 6065 | |
| 6066 | |
| 6067 | |
| 6068 | |
| 6069 | |
| 6070 | |
| 6071 | DeclRefExpr *DRE = nullptr; |
| 6072 | |
| 6073 | |
| 6074 | |
| 6075 | bool = false; |
| 6076 | while (ParenExpr *Parens = dyn_cast<ParenExpr>(Arg)) { |
| 6077 | if (!Invalid && !ExtraParens) { |
| 6078 | S.Diag(Arg->getBeginLoc(), |
| 6079 | S.getLangOpts().CPlusPlus11 |
| 6080 | ? diag::warn_cxx98_compat_template_arg_extra_parens |
| 6081 | : diag::ext_template_arg_extra_parens) |
| 6082 | << Arg->getSourceRange(); |
| 6083 | ExtraParens = true; |
| 6084 | } |
| 6085 | |
| 6086 | Arg = Parens->getSubExpr(); |
| 6087 | } |
| 6088 | |
| 6089 | while (SubstNonTypeTemplateParmExpr *subst = |
| 6090 | dyn_cast<SubstNonTypeTemplateParmExpr>(Arg)) |
| 6091 | Arg = subst->getReplacement()->IgnoreImpCasts(); |
| 6092 | |
| 6093 | |
| 6094 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 6095 | if (UnOp->getOpcode() == UO_AddrOf) { |
| 6096 | DRE = dyn_cast<DeclRefExpr>(UnOp->getSubExpr()); |
| 6097 | if (DRE && !DRE->getQualifier()) |
| 6098 | DRE = nullptr; |
| 6099 | } |
| 6100 | } |
| 6101 | |
| 6102 | else if ((DRE = dyn_cast<DeclRefExpr>(Arg))) { |
| 6103 | ValueDecl *VD = DRE->getDecl(); |
| 6104 | if (VD->getType()->isMemberPointerType()) { |
| 6105 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
| 6106 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 6107 | Converted = TemplateArgument(Arg); |
| 6108 | } else { |
| 6109 | VD = cast<ValueDecl>(VD->getCanonicalDecl()); |
| 6110 | Converted = TemplateArgument(VD, ParamType); |
| 6111 | } |
| 6112 | return Invalid; |
| 6113 | } |
| 6114 | } |
| 6115 | |
| 6116 | DRE = nullptr; |
| 6117 | } |
| 6118 | |
| 6119 | ValueDecl *Entity = DRE ? DRE->getDecl() : nullptr; |
| 6120 | |
| 6121 | |
| 6122 | switch (isNullPointerValueTemplateArgument(S, Param, ParamType, ResultArg, |
| 6123 | Entity)) { |
| 6124 | case NPV_Error: |
| 6125 | return true; |
| 6126 | case NPV_NullPointer: |
| 6127 | S.Diag(ResultArg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
| 6128 | Converted = TemplateArgument(S.Context.getCanonicalType(ParamType), |
| 6129 | ); |
| 6130 | return false; |
| 6131 | case NPV_NotNullPointer: |
| 6132 | break; |
| 6133 | } |
| 6134 | |
| 6135 | if (S.IsQualificationConversion(ResultArg->getType(), |
| 6136 | ParamType.getNonReferenceType(), false, |
| 6137 | ObjCLifetimeConversion)) { |
| 6138 | ResultArg = S.ImpCastExprToType(ResultArg, ParamType, CK_NoOp, |
| 6139 | ResultArg->getValueKind()) |
| 6140 | .get(); |
| 6141 | } else if (!S.Context.hasSameUnqualifiedType( |
| 6142 | ResultArg->getType(), ParamType.getNonReferenceType())) { |
| 6143 | |
| 6144 | S.Diag(ResultArg->getBeginLoc(), diag::err_template_arg_not_convertible) |
| 6145 | << ResultArg->getType() << ParamType << ResultArg->getSourceRange(); |
| 6146 | S.Diag(Param->getLocation(), diag::note_template_param_here); |
| 6147 | return true; |
| 6148 | } |
| 6149 | |
| 6150 | if (!DRE) |
| 6151 | return S.Diag(Arg->getBeginLoc(), |
| 6152 | diag::err_template_arg_not_pointer_to_member_form) |
| 6153 | << Arg->getSourceRange(); |
| 6154 | |
| 6155 | if (isa<FieldDecl>(DRE->getDecl()) || |
| 6156 | isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 6157 | isa<CXXMethodDecl>(DRE->getDecl())) { |
| 6158 | (0) . __assert_fail ("(isa(DRE->getDecl()) || isa(DRE->getDecl()) || !cast(DRE->getDecl())->isStatic()) && \"Only non-static member pointers can make it here\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6161, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((isa<FieldDecl>(DRE->getDecl()) || |
| 6159 | (0) . __assert_fail ("(isa(DRE->getDecl()) || isa(DRE->getDecl()) || !cast(DRE->getDecl())->isStatic()) && \"Only non-static member pointers can make it here\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6161, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> isa<IndirectFieldDecl>(DRE->getDecl()) || |
| 6160 | (0) . __assert_fail ("(isa(DRE->getDecl()) || isa(DRE->getDecl()) || !cast(DRE->getDecl())->isStatic()) && \"Only non-static member pointers can make it here\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6161, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> !cast<CXXMethodDecl>(DRE->getDecl())->isStatic()) && |
| 6161 | (0) . __assert_fail ("(isa(DRE->getDecl()) || isa(DRE->getDecl()) || !cast(DRE->getDecl())->isStatic()) && \"Only non-static member pointers can make it here\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6161, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Only non-static member pointers can make it here"); |
| 6162 | |
| 6163 | |
| 6164 | |
| 6165 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 6166 | Converted = TemplateArgument(Arg); |
| 6167 | } else { |
| 6168 | ValueDecl *D = cast<ValueDecl>(DRE->getDecl()->getCanonicalDecl()); |
| 6169 | Converted = TemplateArgument(D, ParamType); |
| 6170 | } |
| 6171 | return Invalid; |
| 6172 | } |
| 6173 | |
| 6174 | |
| 6175 | S.Diag(Arg->getBeginLoc(), diag::err_template_arg_not_pointer_to_member_form) |
| 6176 | << Arg->getSourceRange(); |
| 6177 | S.Diag(DRE->getDecl()->getLocation(), diag::note_template_arg_refers_here); |
| 6178 | return true; |
| 6179 | } |
| 6180 | |
| 6181 | |
| 6182 | |
| 6183 | |
| 6184 | |
| 6185 | |
| 6186 | |
| 6187 | |
| 6188 | ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, |
| 6189 | QualType ParamType, Expr *Arg, |
| 6190 | TemplateArgument &Converted, |
| 6191 | CheckTemplateArgumentKind CTAK) { |
| 6192 | SourceLocation StartLoc = Arg->getBeginLoc(); |
| 6193 | |
| 6194 | |
| 6195 | if (getLangOpts().CPlusPlus17 && ParamType->isUndeducedType()) { |
| 6196 | |
| 6197 | |
| 6198 | |
| 6199 | |
| 6200 | |
| 6201 | if (CTAK == CTAK_Deduced && Arg->isTypeDependent()) { |
| 6202 | auto *AT = dyn_cast<AutoType>(ParamType); |
| 6203 | if (AT && AT->isDecltypeAuto()) { |
| 6204 | Converted = TemplateArgument(Arg); |
| 6205 | return Arg; |
| 6206 | } |
| 6207 | } |
| 6208 | |
| 6209 | |
| 6210 | |
| 6211 | |
| 6212 | Optional<unsigned> Depth; |
| 6213 | if (CTAK != CTAK_Specified) |
| 6214 | Depth = Param->getDepth() + 1; |
| 6215 | if (DeduceAutoType( |
| 6216 | Context.getTrivialTypeSourceInfo(ParamType, Param->getLocation()), |
| 6217 | Arg, ParamType, Depth) == DAR_Failed) { |
| 6218 | Diag(Arg->getExprLoc(), |
| 6219 | diag::err_non_type_template_parm_type_deduction_failure) |
| 6220 | << Param->getDeclName() << Param->getType() << Arg->getType() |
| 6221 | << Arg->getSourceRange(); |
| 6222 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6223 | return ExprError(); |
| 6224 | } |
| 6225 | |
| 6226 | |
| 6227 | |
| 6228 | |
| 6229 | ParamType = CheckNonTypeTemplateParameterType(ParamType, Arg->getExprLoc()); |
| 6230 | if (ParamType.isNull()) { |
| 6231 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6232 | return ExprError(); |
| 6233 | } |
| 6234 | } |
| 6235 | |
| 6236 | |
| 6237 | (0) . __assert_fail ("!ParamType.hasQualifiers() && \"non-type template parameter type cannot be qualified\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6238, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!ParamType.hasQualifiers() && |
| 6238 | (0) . __assert_fail ("!ParamType.hasQualifiers() && \"non-type template parameter type cannot be qualified\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6238, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "non-type template parameter type cannot be qualified"); |
| 6239 | |
| 6240 | if (CTAK == CTAK_Deduced && |
| 6241 | !Context.hasSameType(ParamType.getNonLValueExprType(Context), |
| 6242 | Arg->getType())) { |
| 6243 | |
| 6244 | |
| 6245 | |
| 6246 | |
| 6247 | |
| 6248 | |
| 6249 | |
| 6250 | if ((ParamType->isDependentType() || Arg->isTypeDependent()) && |
| 6251 | !Arg->getType()->getContainedAutoType()) { |
| 6252 | Converted = TemplateArgument(Arg); |
| 6253 | return Arg; |
| 6254 | } |
| 6255 | |
| 6256 | |
| 6257 | |
| 6258 | |
| 6259 | Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) |
| 6260 | << Arg->getType() |
| 6261 | << ParamType.getUnqualifiedType(); |
| 6262 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6263 | return ExprError(); |
| 6264 | } |
| 6265 | |
| 6266 | |
| 6267 | |
| 6268 | if (ParamType->isDependentType() || Arg->isTypeDependent()) { |
| 6269 | |
| 6270 | Converted = TemplateArgument(Arg); |
| 6271 | return Arg; |
| 6272 | } |
| 6273 | |
| 6274 | |
| 6275 | |
| 6276 | EnterExpressionEvaluationContext ConstantEvaluated( |
| 6277 | *this, Sema::ExpressionEvaluationContext::ConstantEvaluated); |
| 6278 | |
| 6279 | if (getLangOpts().CPlusPlus17) { |
| 6280 | |
| 6281 | |
| 6282 | |
| 6283 | APValue Value; |
| 6284 | ExprResult ArgResult = CheckConvertedConstantExpression( |
| 6285 | Arg, ParamType, Value, CCEK_TemplateArg); |
| 6286 | if (ArgResult.isInvalid()) |
| 6287 | return ExprError(); |
| 6288 | |
| 6289 | |
| 6290 | |
| 6291 | if (ArgResult.get()->isValueDependent()) { |
| 6292 | Converted = TemplateArgument(ArgResult.get()); |
| 6293 | return ArgResult; |
| 6294 | } |
| 6295 | |
| 6296 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
| 6297 | |
| 6298 | |
| 6299 | switch (Value.getKind()) { |
| 6300 | case APValue::Uninitialized: |
| 6301 | isNullPtrType()", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6301, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ParamType->isNullPtrType()); |
| 6302 | Converted = TemplateArgument(CanonParamType, ); |
| 6303 | break; |
| 6304 | case APValue::Int: |
| 6305 | isIntegralOrEnumerationType()", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6305, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ParamType->isIntegralOrEnumerationType()); |
| 6306 | Converted = TemplateArgument(Context, Value.getInt(), CanonParamType); |
| 6307 | break; |
| 6308 | case APValue::MemberPointer: { |
| 6309 | isMemberPointerType()", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6309, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ParamType->isMemberPointerType()); |
| 6310 | |
| 6311 | |
| 6312 | if (!Value.getMemberPointerPath().empty()) { |
| 6313 | Diag(Arg->getBeginLoc(), |
| 6314 | diag::err_template_arg_member_ptr_base_derived_not_supported) |
| 6315 | << Value.getMemberPointerDecl() << ParamType |
| 6316 | << Arg->getSourceRange(); |
| 6317 | return ExprError(); |
| 6318 | } |
| 6319 | |
| 6320 | auto *VD = const_cast<ValueDecl*>(Value.getMemberPointerDecl()); |
| 6321 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 6322 | : TemplateArgument(CanonParamType, ); |
| 6323 | break; |
| 6324 | } |
| 6325 | case APValue::LValue: { |
| 6326 | |
| 6327 | |
| 6328 | isPointerType() || ParamType->isReferenceType() || ParamType->isNullPtrType()", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6329, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ParamType->isPointerType() || ParamType->isReferenceType() || |
| 6329 | isPointerType() || ParamType->isReferenceType() || ParamType->isNullPtrType()", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6329, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> ParamType->isNullPtrType()); |
| 6330 | |
| 6331 | |
| 6332 | |
| 6333 | |
| 6334 | if (auto *E = Value.getLValueBase().dyn_cast<const Expr*>()) { |
| 6335 | if (isa<CXXUuidofExpr>(E)) { |
| 6336 | Converted = TemplateArgument(ArgResult.get()->IgnoreImpCasts()); |
| 6337 | break; |
| 6338 | } |
| 6339 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_decl_ref) |
| 6340 | << Arg->getSourceRange(); |
| 6341 | return ExprError(); |
| 6342 | } |
| 6343 | auto *VD = const_cast<ValueDecl *>( |
| 6344 | Value.getLValueBase().dyn_cast<const ValueDecl *>()); |
| 6345 | |
| 6346 | if (Value.hasLValuePath() && Value.getLValuePath().size() == 1 && |
| 6347 | VD && VD->getType()->isArrayType() && |
| 6348 | Value.getLValuePath()[0].ArrayIndex == 0 && |
| 6349 | !Value.isLValueOnePastTheEnd() && ParamType->isPointerType()) { |
| 6350 | |
| 6351 | |
| 6352 | |
| 6353 | } else if (!Value.hasLValuePath() || Value.getLValuePath().size() || |
| 6354 | Value.isLValueOnePastTheEnd()) { |
| 6355 | Diag(StartLoc, diag::err_non_type_template_arg_subobject) |
| 6356 | << Value.getAsString(Context, ParamType); |
| 6357 | return ExprError(); |
| 6358 | } |
| 6359 | (0) . __assert_fail ("(VD || !ParamType->isReferenceType()) && \"null reference should not be a constant expression\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6360, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((VD || !ParamType->isReferenceType()) && |
| 6360 | (0) . __assert_fail ("(VD || !ParamType->isReferenceType()) && \"null reference should not be a constant expression\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6360, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "null reference should not be a constant expression"); |
| 6361 | (0) . __assert_fail ("(!VD || !ParamType->isNullPtrType()) && \"non-null value of type nullptr_t?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6362, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((!VD || !ParamType->isNullPtrType()) && |
| 6362 | (0) . __assert_fail ("(!VD || !ParamType->isNullPtrType()) && \"non-null value of type nullptr_t?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6362, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "non-null value of type nullptr_t?"); |
| 6363 | Converted = VD ? TemplateArgument(VD, CanonParamType) |
| 6364 | : TemplateArgument(CanonParamType, ); |
| 6365 | break; |
| 6366 | } |
| 6367 | case APValue::AddrLabelDiff: |
| 6368 | return Diag(StartLoc, diag::err_non_type_template_arg_addr_label_diff); |
| 6369 | case APValue::FixedPoint: |
| 6370 | case APValue::Float: |
| 6371 | case APValue::ComplexInt: |
| 6372 | case APValue::ComplexFloat: |
| 6373 | case APValue::Vector: |
| 6374 | case APValue::Array: |
| 6375 | case APValue::Struct: |
| 6376 | case APValue::Union: |
| 6377 | llvm_unreachable("invalid kind for template argument"); |
| 6378 | } |
| 6379 | |
| 6380 | return ArgResult.get(); |
| 6381 | } |
| 6382 | |
| 6383 | |
| 6384 | |
| 6385 | |
| 6386 | |
| 6387 | |
| 6388 | |
| 6389 | if (ParamType->isIntegralOrEnumerationType()) { |
| 6390 | |
| 6391 | |
| 6392 | |
| 6393 | |
| 6394 | |
| 6395 | |
| 6396 | |
| 6397 | |
| 6398 | |
| 6399 | |
| 6400 | if (getLangOpts().CPlusPlus11) { |
| 6401 | |
| 6402 | |
| 6403 | |
| 6404 | |
| 6405 | |
| 6406 | |
| 6407 | |
| 6408 | llvm::APSInt Value; |
| 6409 | ExprResult ArgResult = |
| 6410 | CheckConvertedConstantExpression(Arg, ParamType, Value, |
| 6411 | CCEK_TemplateArg); |
| 6412 | if (ArgResult.isInvalid()) |
| 6413 | return ExprError(); |
| 6414 | |
| 6415 | |
| 6416 | if (ArgResult.get()->isValueDependent()) { |
| 6417 | Converted = TemplateArgument(ArgResult.get()); |
| 6418 | return ArgResult; |
| 6419 | } |
| 6420 | |
| 6421 | |
| 6422 | |
| 6423 | |
| 6424 | QualType IntegerType = ParamType; |
| 6425 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 6426 | IntegerType = Enum->getDecl()->getIntegerType(); |
| 6427 | Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); |
| 6428 | |
| 6429 | Converted = TemplateArgument(Context, Value, |
| 6430 | Context.getCanonicalType(ParamType)); |
| 6431 | return ArgResult; |
| 6432 | } |
| 6433 | |
| 6434 | ExprResult ArgResult = DefaultLvalueConversion(Arg); |
| 6435 | if (ArgResult.isInvalid()) |
| 6436 | return ExprError(); |
| 6437 | Arg = ArgResult.get(); |
| 6438 | |
| 6439 | QualType ArgType = Arg->getType(); |
| 6440 | |
| 6441 | |
| 6442 | |
| 6443 | |
| 6444 | |
| 6445 | |
| 6446 | |
| 6447 | |
| 6448 | llvm::APSInt Value; |
| 6449 | if (!ArgType->isIntegralOrEnumerationType()) { |
| 6450 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_integral_or_enumeral) |
| 6451 | << ArgType << Arg->getSourceRange(); |
| 6452 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6453 | return ExprError(); |
| 6454 | } else if (!Arg->isValueDependent()) { |
| 6455 | class TmplArgICEDiagnoser : public VerifyICEDiagnoser { |
| 6456 | QualType T; |
| 6457 | |
| 6458 | public: |
| 6459 | TmplArgICEDiagnoser(QualType T) : T(T) { } |
| 6460 | |
| 6461 | void diagnoseNotICE(Sema &S, SourceLocation Loc, |
| 6462 | SourceRange SR) override { |
| 6463 | S.Diag(Loc, diag::err_template_arg_not_ice) << T << SR; |
| 6464 | } |
| 6465 | } Diagnoser(ArgType); |
| 6466 | |
| 6467 | Arg = VerifyIntegerConstantExpression(Arg, &Value, Diagnoser, |
| 6468 | false).get(); |
| 6469 | if (!Arg) |
| 6470 | return ExprError(); |
| 6471 | } |
| 6472 | |
| 6473 | |
| 6474 | |
| 6475 | ArgType = ArgType.getUnqualifiedType(); |
| 6476 | |
| 6477 | |
| 6478 | if (Context.hasSameType(ParamType, ArgType)) { |
| 6479 | |
| 6480 | } else if (ParamType->isBooleanType()) { |
| 6481 | |
| 6482 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).get(); |
| 6483 | } else if (IsIntegralPromotion(Arg, ArgType, ParamType) || |
| 6484 | !ParamType->isEnumeralType()) { |
| 6485 | |
| 6486 | Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralCast).get(); |
| 6487 | } else { |
| 6488 | |
| 6489 | Diag(Arg->getBeginLoc(), diag::err_template_arg_not_convertible) |
| 6490 | << Arg->getType() << ParamType << Arg->getSourceRange(); |
| 6491 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6492 | return ExprError(); |
| 6493 | } |
| 6494 | |
| 6495 | |
| 6496 | |
| 6497 | |
| 6498 | if (Arg->isValueDependent()) { |
| 6499 | |
| 6500 | |
| 6501 | Converted = TemplateArgument(Arg); |
| 6502 | return Arg; |
| 6503 | } |
| 6504 | |
| 6505 | QualType IntegerType = Context.getCanonicalType(ParamType); |
| 6506 | if (const EnumType *Enum = IntegerType->getAs<EnumType>()) |
| 6507 | IntegerType = Context.getCanonicalType(Enum->getDecl()->getIntegerType()); |
| 6508 | |
| 6509 | if (ParamType->isBooleanType()) { |
| 6510 | |
| 6511 | Value = Value != 0; |
| 6512 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 6513 | if (Value.getBitWidth() != AllowedBits) |
| 6514 | Value = Value.extOrTrunc(AllowedBits); |
| 6515 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
| 6516 | } else { |
| 6517 | llvm::APSInt OldValue = Value; |
| 6518 | |
| 6519 | |
| 6520 | |
| 6521 | unsigned AllowedBits = Context.getTypeSize(IntegerType); |
| 6522 | if (Value.getBitWidth() != AllowedBits) |
| 6523 | Value = Value.extOrTrunc(AllowedBits); |
| 6524 | Value.setIsSigned(IntegerType->isSignedIntegerOrEnumerationType()); |
| 6525 | |
| 6526 | |
| 6527 | if (IntegerType->isUnsignedIntegerOrEnumerationType() |
| 6528 | && (OldValue.isSigned() && OldValue.isNegative())) { |
| 6529 | Diag(Arg->getBeginLoc(), diag::warn_template_arg_negative) |
| 6530 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 6531 | << Arg->getSourceRange(); |
| 6532 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6533 | } |
| 6534 | |
| 6535 | |
| 6536 | unsigned RequiredBits; |
| 6537 | if (IntegerType->isUnsignedIntegerOrEnumerationType()) |
| 6538 | RequiredBits = OldValue.getActiveBits(); |
| 6539 | else if (OldValue.isUnsigned()) |
| 6540 | RequiredBits = OldValue.getActiveBits() + 1; |
| 6541 | else |
| 6542 | RequiredBits = OldValue.getMinSignedBits(); |
| 6543 | if (RequiredBits > AllowedBits) { |
| 6544 | Diag(Arg->getBeginLoc(), diag::warn_template_arg_too_large) |
| 6545 | << OldValue.toString(10) << Value.toString(10) << Param->getType() |
| 6546 | << Arg->getSourceRange(); |
| 6547 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6548 | } |
| 6549 | } |
| 6550 | |
| 6551 | Converted = TemplateArgument(Context, Value, |
| 6552 | ParamType->isEnumeralType() |
| 6553 | ? Context.getCanonicalType(ParamType) |
| 6554 | : IntegerType); |
| 6555 | return Arg; |
| 6556 | } |
| 6557 | |
| 6558 | QualType ArgType = Arg->getType(); |
| 6559 | DeclAccessPair FoundResult; |
| 6560 | |
| 6561 | |
| 6562 | |
| 6563 | if ( |
| 6564 | |
| 6565 | |
| 6566 | |
| 6567 | |
| 6568 | (ParamType->isPointerType() && |
| 6569 | ParamType->getAs<PointerType>()->getPointeeType()->isFunctionType()) || |
| 6570 | |
| 6571 | |
| 6572 | |
| 6573 | |
| 6574 | (ParamType->isReferenceType() && |
| 6575 | ParamType->getAs<ReferenceType>()->getPointeeType()->isFunctionType()) || |
| 6576 | |
| 6577 | |
| 6578 | |
| 6579 | |
| 6580 | |
| 6581 | (ParamType->isMemberPointerType() && |
| 6582 | ParamType->getAs<MemberPointerType>()->getPointeeType() |
| 6583 | ->isFunctionType())) { |
| 6584 | |
| 6585 | if (Arg->getType() == Context.OverloadTy) { |
| 6586 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, ParamType, |
| 6587 | true, |
| 6588 | FoundResult)) { |
| 6589 | if (DiagnoseUseOfDecl(Fn, Arg->getBeginLoc())) |
| 6590 | return ExprError(); |
| 6591 | |
| 6592 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 6593 | ArgType = Arg->getType(); |
| 6594 | } else |
| 6595 | return ExprError(); |
| 6596 | } |
| 6597 | |
| 6598 | if (!ParamType->isMemberPointerType()) { |
| 6599 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6600 | ParamType, |
| 6601 | Arg, Converted)) |
| 6602 | return ExprError(); |
| 6603 | return Arg; |
| 6604 | } |
| 6605 | |
| 6606 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 6607 | Converted)) |
| 6608 | return ExprError(); |
| 6609 | return Arg; |
| 6610 | } |
| 6611 | |
| 6612 | if (ParamType->isPointerType()) { |
| 6613 | |
| 6614 | |
| 6615 | |
| 6616 | |
| 6617 | (0) . __assert_fail ("ParamType->getPointeeType()->isIncompleteOrObjectType() && \"Only object pointers allowed here\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6618, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ParamType->getPointeeType()->isIncompleteOrObjectType() && |
| 6618 | (0) . __assert_fail ("ParamType->getPointeeType()->isIncompleteOrObjectType() && \"Only object pointers allowed here\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6618, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Only object pointers allowed here"); |
| 6619 | |
| 6620 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6621 | ParamType, |
| 6622 | Arg, Converted)) |
| 6623 | return ExprError(); |
| 6624 | return Arg; |
| 6625 | } |
| 6626 | |
| 6627 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
| 6628 | |
| 6629 | |
| 6630 | |
| 6631 | |
| 6632 | |
| 6633 | |
| 6634 | (0) . __assert_fail ("ParamRefType->getPointeeType()->isIncompleteOrObjectType() && \"Only object references allowed here\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6635, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ParamRefType->getPointeeType()->isIncompleteOrObjectType() && |
| 6635 | (0) . __assert_fail ("ParamRefType->getPointeeType()->isIncompleteOrObjectType() && \"Only object references allowed here\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6635, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Only object references allowed here"); |
| 6636 | |
| 6637 | if (Arg->getType() == Context.OverloadTy) { |
| 6638 | if (FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(Arg, |
| 6639 | ParamRefType->getPointeeType(), |
| 6640 | true, |
| 6641 | FoundResult)) { |
| 6642 | if (DiagnoseUseOfDecl(Fn, Arg->getBeginLoc())) |
| 6643 | return ExprError(); |
| 6644 | |
| 6645 | Arg = FixOverloadedFunctionReference(Arg, FoundResult, Fn); |
| 6646 | ArgType = Arg->getType(); |
| 6647 | } else |
| 6648 | return ExprError(); |
| 6649 | } |
| 6650 | |
| 6651 | if (CheckTemplateArgumentAddressOfObjectOrFunction(*this, Param, |
| 6652 | ParamType, |
| 6653 | Arg, Converted)) |
| 6654 | return ExprError(); |
| 6655 | return Arg; |
| 6656 | } |
| 6657 | |
| 6658 | |
| 6659 | if (ParamType->isNullPtrType()) { |
| 6660 | if (Arg->isTypeDependent() || Arg->isValueDependent()) { |
| 6661 | Converted = TemplateArgument(Arg); |
| 6662 | return Arg; |
| 6663 | } |
| 6664 | |
| 6665 | switch (isNullPointerValueTemplateArgument(*this, Param, ParamType, Arg)) { |
| 6666 | case NPV_NotNullPointer: |
| 6667 | Diag(Arg->getExprLoc(), diag::err_template_arg_not_convertible) |
| 6668 | << Arg->getType() << ParamType; |
| 6669 | Diag(Param->getLocation(), diag::note_template_param_here); |
| 6670 | return ExprError(); |
| 6671 | |
| 6672 | case NPV_Error: |
| 6673 | return ExprError(); |
| 6674 | |
| 6675 | case NPV_NullPointer: |
| 6676 | Diag(Arg->getExprLoc(), diag::warn_cxx98_compat_template_arg_null); |
| 6677 | Converted = TemplateArgument(Context.getCanonicalType(ParamType), |
| 6678 | ); |
| 6679 | return Arg; |
| 6680 | } |
| 6681 | } |
| 6682 | |
| 6683 | |
| 6684 | |
| 6685 | (0) . __assert_fail ("ParamType->isMemberPointerType() && \"Only pointers to members remain\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6685, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ParamType->isMemberPointerType() && "Only pointers to members remain"); |
| 6686 | |
| 6687 | if (CheckTemplateArgumentPointerToMember(*this, Param, ParamType, Arg, |
| 6688 | Converted)) |
| 6689 | return ExprError(); |
| 6690 | return Arg; |
| 6691 | } |
| 6692 | |
| 6693 | static void DiagnoseTemplateParameterListArityMismatch( |
| 6694 | Sema &S, TemplateParameterList *New, TemplateParameterList *Old, |
| 6695 | Sema::TemplateParameterListEqualKind Kind, SourceLocation TemplateArgLoc); |
| 6696 | |
| 6697 | |
| 6698 | |
| 6699 | |
| 6700 | |
| 6701 | |
| 6702 | bool Sema::CheckTemplateTemplateArgument(TemplateParameterList *Params, |
| 6703 | TemplateArgumentLoc &Arg) { |
| 6704 | TemplateName Name = Arg.getArgument().getAsTemplateOrTemplatePattern(); |
| 6705 | TemplateDecl *Template = Name.getAsTemplateDecl(); |
| 6706 | if (!Template) { |
| 6707 | |
| 6708 | (0) . __assert_fail ("Name.isDependent() && \"Non-dependent template isn't a declaration?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6708, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Name.isDependent() && "Non-dependent template isn't a declaration?"); |
| 6709 | return false; |
| 6710 | } |
| 6711 | |
| 6712 | if (Template->isInvalidDecl()) |
| 6713 | return true; |
| 6714 | |
| 6715 | |
| 6716 | |
| 6717 | |
| 6718 | |
| 6719 | |
| 6720 | |
| 6721 | |
| 6722 | |
| 6723 | |
| 6724 | |
| 6725 | |
| 6726 | |
| 6727 | if (!isa<ClassTemplateDecl>(Template) && |
| 6728 | !isa<TemplateTemplateParmDecl>(Template) && |
| 6729 | !isa<TypeAliasTemplateDecl>(Template) && |
| 6730 | !isa<BuiltinTemplateDecl>(Template)) { |
| 6731 | (0) . __assert_fail ("isa(Template) && \"Only function templates are possible here\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6732, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isa<FunctionTemplateDecl>(Template) && |
| 6732 | (0) . __assert_fail ("isa(Template) && \"Only function templates are possible here\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6732, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Only function templates are possible here"); |
| 6733 | Diag(Arg.getLocation(), diag::err_template_arg_not_valid_template); |
| 6734 | Diag(Template->getLocation(), diag::note_template_arg_refers_here_func) |
| 6735 | << Template; |
| 6736 | } |
| 6737 | |
| 6738 | |
| 6739 | |
| 6740 | |
| 6741 | if (getLangOpts().RelaxedTemplateTemplateArgs) { |
| 6742 | |
| 6743 | |
| 6744 | |
| 6745 | |
| 6746 | if (TemplateParameterListsAreEqual( |
| 6747 | Template->getTemplateParameters(), Params, false, |
| 6748 | TPL_TemplateTemplateArgumentMatch, Arg.getLocation())) |
| 6749 | return false; |
| 6750 | |
| 6751 | if (isTemplateTemplateParameterAtLeastAsSpecializedAs(Params, Template, |
| 6752 | Arg.getLocation())) |
| 6753 | return false; |
| 6754 | |
| 6755 | } |
| 6756 | |
| 6757 | return !TemplateParameterListsAreEqual(Template->getTemplateParameters(), |
| 6758 | Params, |
| 6759 | true, |
| 6760 | TPL_TemplateTemplateArgumentMatch, |
| 6761 | Arg.getLocation()); |
| 6762 | } |
| 6763 | |
| 6764 | |
| 6765 | |
| 6766 | |
| 6767 | |
| 6768 | ExprResult |
| 6769 | Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument &Arg, |
| 6770 | QualType ParamType, |
| 6771 | SourceLocation Loc) { |
| 6772 | |
| 6773 | |
| 6774 | |
| 6775 | |
| 6776 | |
| 6777 | if (ParamType->isArrayType()) |
| 6778 | ParamType = Context.getArrayDecayedType(ParamType); |
| 6779 | else if (ParamType->isFunctionType()) |
| 6780 | ParamType = Context.getPointerType(ParamType); |
| 6781 | |
| 6782 | |
| 6783 | |
| 6784 | if (Arg.getKind() == TemplateArgument::NullPtr) { |
| 6785 | return ImpCastExprToType( |
| 6786 | new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc), |
| 6787 | ParamType, |
| 6788 | ParamType->getAs<MemberPointerType>() |
| 6789 | ? CK_NullToMemberPointer |
| 6790 | : CK_NullToPointer); |
| 6791 | } |
| 6792 | (0) . __assert_fail ("Arg.getKind() == TemplateArgument..Declaration && \"Only declaration template arguments permitted here\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6793, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Arg.getKind() == TemplateArgument::Declaration && |
| 6793 | (0) . __assert_fail ("Arg.getKind() == TemplateArgument..Declaration && \"Only declaration template arguments permitted here\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6793, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Only declaration template arguments permitted here"); |
| 6794 | |
| 6795 | ValueDecl *VD = Arg.getAsDecl(); |
| 6796 | |
| 6797 | if (VD->getDeclContext()->isRecord() && |
| 6798 | (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) || |
| 6799 | isa<IndirectFieldDecl>(VD))) { |
| 6800 | |
| 6801 | |
| 6802 | |
| 6803 | |
| 6804 | |
| 6805 | if (ParamType->isMemberPointerType()) { |
| 6806 | QualType ClassType |
| 6807 | = Context.getTypeDeclType(cast<RecordDecl>(VD->getDeclContext())); |
| 6808 | NestedNameSpecifier *Qualifier |
| 6809 | = NestedNameSpecifier::Create(Context, nullptr, false, |
| 6810 | ClassType.getTypePtr()); |
| 6811 | CXXScopeSpec SS; |
| 6812 | SS.MakeTrivial(Context, Qualifier, Loc); |
| 6813 | |
| 6814 | |
| 6815 | |
| 6816 | |
| 6817 | ExprValueKind VK = VK_LValue; |
| 6818 | if (isa<CXXMethodDecl>(VD) && cast<CXXMethodDecl>(VD)->isInstance()) |
| 6819 | VK = VK_RValue; |
| 6820 | |
| 6821 | ExprResult RefExpr = BuildDeclRefExpr(VD, |
| 6822 | VD->getType().getNonReferenceType(), |
| 6823 | VK, |
| 6824 | Loc, |
| 6825 | &SS); |
| 6826 | if (RefExpr.isInvalid()) |
| 6827 | return ExprError(); |
| 6828 | |
| 6829 | RefExpr = CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
| 6830 | |
| 6831 | |
| 6832 | |
| 6833 | |
| 6834 | bool ObjCLifetimeConversion; |
| 6835 | if (IsQualificationConversion(((Expr*) RefExpr.get())->getType(), |
| 6836 | ParamType.getUnqualifiedType(), false, |
| 6837 | ObjCLifetimeConversion)) |
| 6838 | RefExpr = ImpCastExprToType(RefExpr.get(), ParamType.getUnqualifiedType(), CK_NoOp); |
| 6839 | |
| 6840 | getType(), ParamType.getUnqualifiedType())", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6842, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!RefExpr.isInvalid() && |
| 6841 | getType(), ParamType.getUnqualifiedType())", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6842, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> Context.hasSameType(((Expr*) RefExpr.get())->getType(), |
| 6842 | getType(), ParamType.getUnqualifiedType())", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6842, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> ParamType.getUnqualifiedType())); |
| 6843 | return RefExpr; |
| 6844 | } |
| 6845 | } |
| 6846 | |
| 6847 | QualType T = VD->getType().getNonReferenceType(); |
| 6848 | |
| 6849 | if (ParamType->isPointerType()) { |
| 6850 | |
| 6851 | |
| 6852 | ExprResult RefExpr = BuildDeclRefExpr(VD, T, VK_LValue, Loc); |
| 6853 | if (RefExpr.isInvalid()) |
| 6854 | return ExprError(); |
| 6855 | |
| 6856 | if (!Context.hasSameUnqualifiedType(ParamType->getPointeeType(), T) && |
| 6857 | (T->isFunctionType() || T->isArrayType())) { |
| 6858 | |
| 6859 | RefExpr = DefaultFunctionArrayConversion(RefExpr.get()); |
| 6860 | if (RefExpr.isInvalid()) |
| 6861 | return ExprError(); |
| 6862 | |
| 6863 | return RefExpr; |
| 6864 | } |
| 6865 | |
| 6866 | |
| 6867 | return CreateBuiltinUnaryOp(Loc, UO_AddrOf, RefExpr.get()); |
| 6868 | } |
| 6869 | |
| 6870 | ExprValueKind VK = VK_RValue; |
| 6871 | |
| 6872 | |
| 6873 | |
| 6874 | |
| 6875 | if (const ReferenceType *TargetRef = ParamType->getAs<ReferenceType>()) { |
| 6876 | VK = VK_LValue; |
| 6877 | T = Context.getQualifiedType(T, |
| 6878 | TargetRef->getPointeeType().getQualifiers()); |
| 6879 | } else if (isa<FunctionDecl>(VD)) { |
| 6880 | |
| 6881 | VK = VK_LValue; |
| 6882 | } |
| 6883 | |
| 6884 | return BuildDeclRefExpr(VD, T, VK, Loc); |
| 6885 | } |
| 6886 | |
| 6887 | |
| 6888 | |
| 6889 | |
| 6890 | |
| 6891 | |
| 6892 | |
| 6893 | |
| 6894 | ExprResult |
| 6895 | Sema::BuildExpressionFromIntegralTemplateArgument(const TemplateArgument &Arg, |
| 6896 | SourceLocation Loc) { |
| 6897 | (0) . __assert_fail ("Arg.getKind() == TemplateArgument..Integral && \"Operation is only valid for integral template arguments\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6898, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Arg.getKind() == TemplateArgument::Integral && |
| 6898 | (0) . __assert_fail ("Arg.getKind() == TemplateArgument..Integral && \"Operation is only valid for integral template arguments\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 6898, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Operation is only valid for integral template arguments"); |
| 6899 | QualType OrigT = Arg.getIntegralType(); |
| 6900 | |
| 6901 | |
| 6902 | |
| 6903 | |
| 6904 | |
| 6905 | |
| 6906 | QualType T = OrigT; |
| 6907 | if (const EnumType *ET = OrigT->getAs<EnumType>()) |
| 6908 | T = ET->getDecl()->getIntegerType(); |
| 6909 | |
| 6910 | Expr *E; |
| 6911 | if (T->isAnyCharacterType()) { |
| 6912 | CharacterLiteral::CharacterKind Kind; |
| 6913 | if (T->isWideCharType()) |
| 6914 | Kind = CharacterLiteral::Wide; |
| 6915 | else if (T->isChar8Type() && getLangOpts().Char8) |
| 6916 | Kind = CharacterLiteral::UTF8; |
| 6917 | else if (T->isChar16Type()) |
| 6918 | Kind = CharacterLiteral::UTF16; |
| 6919 | else if (T->isChar32Type()) |
| 6920 | Kind = CharacterLiteral::UTF32; |
| 6921 | else |
| 6922 | Kind = CharacterLiteral::Ascii; |
| 6923 | |
| 6924 | E = new (Context) CharacterLiteral(Arg.getAsIntegral().getZExtValue(), |
| 6925 | Kind, T, Loc); |
| 6926 | } else if (T->isBooleanType()) { |
| 6927 | E = new (Context) CXXBoolLiteralExpr(Arg.getAsIntegral().getBoolValue(), |
| 6928 | T, Loc); |
| 6929 | } else if (T->isNullPtrType()) { |
| 6930 | E = new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc); |
| 6931 | } else { |
| 6932 | E = IntegerLiteral::Create(Context, Arg.getAsIntegral(), T, Loc); |
| 6933 | } |
| 6934 | |
| 6935 | if (OrigT->isEnumeralType()) { |
| 6936 | |
| 6937 | |
| 6938 | E = CStyleCastExpr::Create(Context, OrigT, VK_RValue, CK_IntegralCast, E, |
| 6939 | nullptr, |
| 6940 | Context.getTrivialTypeSourceInfo(OrigT, Loc), |
| 6941 | Loc, Loc); |
| 6942 | } |
| 6943 | |
| 6944 | return E; |
| 6945 | } |
| 6946 | |
| 6947 | |
| 6948 | static bool MatchTemplateParameterKind(Sema &S, NamedDecl *New, NamedDecl *Old, |
| 6949 | bool Complain, |
| 6950 | Sema::TemplateParameterListEqualKind Kind, |
| 6951 | SourceLocation TemplateArgLoc) { |
| 6952 | |
| 6953 | if (Old->getKind() != New->getKind()) { |
| 6954 | if (Complain) { |
| 6955 | unsigned NextDiag = diag::err_template_param_different_kind; |
| 6956 | if (TemplateArgLoc.isValid()) { |
| 6957 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 6958 | NextDiag = diag::note_template_param_different_kind; |
| 6959 | } |
| 6960 | S.Diag(New->getLocation(), NextDiag) |
| 6961 | << (Kind != Sema::TPL_TemplateMatch); |
| 6962 | S.Diag(Old->getLocation(), diag::note_template_prev_declaration) |
| 6963 | << (Kind != Sema::TPL_TemplateMatch); |
| 6964 | } |
| 6965 | |
| 6966 | return false; |
| 6967 | } |
| 6968 | |
| 6969 | |
| 6970 | |
| 6971 | |
| 6972 | |
| 6973 | if (Old->isTemplateParameterPack() != New->isTemplateParameterPack() && |
| 6974 | !(Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 6975 | Old->isTemplateParameterPack())) { |
| 6976 | if (Complain) { |
| 6977 | unsigned NextDiag = diag::err_template_parameter_pack_non_pack; |
| 6978 | if (TemplateArgLoc.isValid()) { |
| 6979 | S.Diag(TemplateArgLoc, |
| 6980 | diag::err_template_arg_template_params_mismatch); |
| 6981 | NextDiag = diag::note_template_parameter_pack_non_pack; |
| 6982 | } |
| 6983 | |
| 6984 | unsigned ParamKind = isa<TemplateTypeParmDecl>(New)? 0 |
| 6985 | : isa<NonTypeTemplateParmDecl>(New)? 1 |
| 6986 | : 2; |
| 6987 | S.Diag(New->getLocation(), NextDiag) |
| 6988 | << ParamKind << New->isParameterPack(); |
| 6989 | S.Diag(Old->getLocation(), diag::note_template_parameter_pack_here) |
| 6990 | << ParamKind << Old->isParameterPack(); |
| 6991 | } |
| 6992 | |
| 6993 | return false; |
| 6994 | } |
| 6995 | |
| 6996 | |
| 6997 | if (NonTypeTemplateParmDecl *OldNTTP |
| 6998 | = dyn_cast<NonTypeTemplateParmDecl>(Old)) { |
| 6999 | NonTypeTemplateParmDecl *NewNTTP = cast<NonTypeTemplateParmDecl>(New); |
| 7000 | |
| 7001 | |
| 7002 | |
| 7003 | |
| 7004 | |
| 7005 | if (Kind == Sema::TPL_TemplateTemplateArgumentMatch && |
| 7006 | (OldNTTP->getType()->isDependentType() || |
| 7007 | NewNTTP->getType()->isDependentType())) |
| 7008 | return true; |
| 7009 | |
| 7010 | if (!S.Context.hasSameType(OldNTTP->getType(), NewNTTP->getType())) { |
| 7011 | if (Complain) { |
| 7012 | unsigned NextDiag = diag::err_template_nontype_parm_different_type; |
| 7013 | if (TemplateArgLoc.isValid()) { |
| 7014 | S.Diag(TemplateArgLoc, |
| 7015 | diag::err_template_arg_template_params_mismatch); |
| 7016 | NextDiag = diag::note_template_nontype_parm_different_type; |
| 7017 | } |
| 7018 | S.Diag(NewNTTP->getLocation(), NextDiag) |
| 7019 | << NewNTTP->getType() |
| 7020 | << (Kind != Sema::TPL_TemplateMatch); |
| 7021 | S.Diag(OldNTTP->getLocation(), |
| 7022 | diag::note_template_nontype_parm_prev_declaration) |
| 7023 | << OldNTTP->getType(); |
| 7024 | } |
| 7025 | |
| 7026 | return false; |
| 7027 | } |
| 7028 | |
| 7029 | return true; |
| 7030 | } |
| 7031 | |
| 7032 | |
| 7033 | |
| 7034 | |
| 7035 | if (TemplateTemplateParmDecl *OldTTP |
| 7036 | = dyn_cast<TemplateTemplateParmDecl>(Old)) { |
| 7037 | TemplateTemplateParmDecl *NewTTP = cast<TemplateTemplateParmDecl>(New); |
| 7038 | return S.TemplateParameterListsAreEqual(NewTTP->getTemplateParameters(), |
| 7039 | OldTTP->getTemplateParameters(), |
| 7040 | Complain, |
| 7041 | (Kind == Sema::TPL_TemplateMatch |
| 7042 | ? Sema::TPL_TemplateTemplateParmMatch |
| 7043 | : Kind), |
| 7044 | TemplateArgLoc); |
| 7045 | } |
| 7046 | |
| 7047 | return true; |
| 7048 | } |
| 7049 | |
| 7050 | |
| 7051 | |
| 7052 | static |
| 7053 | void DiagnoseTemplateParameterListArityMismatch(Sema &S, |
| 7054 | TemplateParameterList *New, |
| 7055 | TemplateParameterList *Old, |
| 7056 | Sema::TemplateParameterListEqualKind Kind, |
| 7057 | SourceLocation TemplateArgLoc) { |
| 7058 | unsigned NextDiag = diag::err_template_param_list_different_arity; |
| 7059 | if (TemplateArgLoc.isValid()) { |
| 7060 | S.Diag(TemplateArgLoc, diag::err_template_arg_template_params_mismatch); |
| 7061 | NextDiag = diag::note_template_param_list_different_arity; |
| 7062 | } |
| 7063 | S.Diag(New->getTemplateLoc(), NextDiag) |
| 7064 | << (New->size() > Old->size()) |
| 7065 | << (Kind != Sema::TPL_TemplateMatch) |
| 7066 | << SourceRange(New->getTemplateLoc(), New->getRAngleLoc()); |
| 7067 | S.Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration) |
| 7068 | << (Kind != Sema::TPL_TemplateMatch) |
| 7069 | << SourceRange(Old->getTemplateLoc(), Old->getRAngleLoc()); |
| 7070 | } |
| 7071 | |
| 7072 | |
| 7073 | |
| 7074 | |
| 7075 | |
| 7076 | |
| 7077 | |
| 7078 | |
| 7079 | |
| 7080 | |
| 7081 | |
| 7082 | |
| 7083 | |
| 7084 | |
| 7085 | |
| 7086 | |
| 7087 | |
| 7088 | |
| 7089 | |
| 7090 | |
| 7091 | |
| 7092 | |
| 7093 | |
| 7094 | |
| 7095 | bool |
| 7096 | Sema::TemplateParameterListsAreEqual(TemplateParameterList *New, |
| 7097 | TemplateParameterList *Old, |
| 7098 | bool Complain, |
| 7099 | TemplateParameterListEqualKind Kind, |
| 7100 | SourceLocation TemplateArgLoc) { |
| 7101 | if (Old->size() != New->size() && Kind != TPL_TemplateTemplateArgumentMatch) { |
| 7102 | if (Complain) |
| 7103 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 7104 | TemplateArgLoc); |
| 7105 | |
| 7106 | return false; |
| 7107 | } |
| 7108 | |
| 7109 | |
| 7110 | |
| 7111 | |
| 7112 | |
| 7113 | |
| 7114 | |
| 7115 | TemplateParameterList::iterator NewParm = New->begin(); |
| 7116 | TemplateParameterList::iterator NewParmEnd = New->end(); |
| 7117 | for (TemplateParameterList::iterator OldParm = Old->begin(), |
| 7118 | OldParmEnd = Old->end(); |
| 7119 | OldParm != OldParmEnd; ++OldParm) { |
| 7120 | if (Kind != TPL_TemplateTemplateArgumentMatch || |
| 7121 | !(*OldParm)->isTemplateParameterPack()) { |
| 7122 | if (NewParm == NewParmEnd) { |
| 7123 | if (Complain) |
| 7124 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 7125 | TemplateArgLoc); |
| 7126 | |
| 7127 | return false; |
| 7128 | } |
| 7129 | |
| 7130 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 7131 | Kind, TemplateArgLoc)) |
| 7132 | return false; |
| 7133 | |
| 7134 | ++NewParm; |
| 7135 | continue; |
| 7136 | } |
| 7137 | |
| 7138 | |
| 7139 | |
| 7140 | |
| 7141 | |
| 7142 | |
| 7143 | |
| 7144 | |
| 7145 | for (; NewParm != NewParmEnd; ++NewParm) { |
| 7146 | if (!MatchTemplateParameterKind(*this, *NewParm, *OldParm, Complain, |
| 7147 | Kind, TemplateArgLoc)) |
| 7148 | return false; |
| 7149 | } |
| 7150 | } |
| 7151 | |
| 7152 | |
| 7153 | if (NewParm != NewParmEnd) { |
| 7154 | if (Complain) |
| 7155 | DiagnoseTemplateParameterListArityMismatch(*this, New, Old, Kind, |
| 7156 | TemplateArgLoc); |
| 7157 | |
| 7158 | return false; |
| 7159 | } |
| 7160 | |
| 7161 | return true; |
| 7162 | } |
| 7163 | |
| 7164 | |
| 7165 | |
| 7166 | |
| 7167 | |
| 7168 | bool |
| 7169 | Sema::CheckTemplateDeclScope(Scope *S, TemplateParameterList *TemplateParams) { |
| 7170 | if (!S) |
| 7171 | return false; |
| 7172 | |
| 7173 | |
| 7174 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 7175 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 7176 | S = S->getParent(); |
| 7177 | |
| 7178 | |
| 7179 | |
| 7180 | DeclContext *Ctx = S->getEntity(); |
| 7181 | if (Ctx && Ctx->isExternCContext()) { |
| 7182 | Diag(TemplateParams->getTemplateLoc(), diag::err_template_linkage) |
| 7183 | << TemplateParams->getSourceRange(); |
| 7184 | if (const LinkageSpecDecl *LSD = Ctx->getExternCContext()) |
| 7185 | Diag(LSD->getExternLoc(), diag::note_extern_c_begins_here); |
| 7186 | return true; |
| 7187 | } |
| 7188 | Ctx = Ctx->getRedeclContext(); |
| 7189 | |
| 7190 | |
| 7191 | |
| 7192 | |
| 7193 | if (Ctx) { |
| 7194 | if (Ctx->isFileContext()) |
| 7195 | return false; |
| 7196 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Ctx)) { |
| 7197 | |
| 7198 | |
| 7199 | if (RD->isLocalClass()) |
| 7200 | return Diag(TemplateParams->getTemplateLoc(), |
| 7201 | diag::err_template_inside_local_class) |
| 7202 | << TemplateParams->getSourceRange(); |
| 7203 | else |
| 7204 | return false; |
| 7205 | } |
| 7206 | } |
| 7207 | |
| 7208 | return Diag(TemplateParams->getTemplateLoc(), |
| 7209 | diag::err_template_outside_namespace_or_class_scope) |
| 7210 | << TemplateParams->getSourceRange(); |
| 7211 | } |
| 7212 | |
| 7213 | |
| 7214 | |
| 7215 | static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D) { |
| 7216 | if (!D) |
| 7217 | return TSK_Undeclared; |
| 7218 | |
| 7219 | if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) |
| 7220 | return Record->getTemplateSpecializationKind(); |
| 7221 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) |
| 7222 | return Function->getTemplateSpecializationKind(); |
| 7223 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) |
| 7224 | return Var->getTemplateSpecializationKind(); |
| 7225 | |
| 7226 | return TSK_Undeclared; |
| 7227 | } |
| 7228 | |
| 7229 | |
| 7230 | |
| 7231 | |
| 7232 | |
| 7233 | |
| 7234 | |
| 7235 | |
| 7236 | |
| 7237 | |
| 7238 | |
| 7239 | |
| 7240 | |
| 7241 | |
| 7242 | |
| 7243 | |
| 7244 | |
| 7245 | |
| 7246 | |
| 7247 | |
| 7248 | |
| 7249 | |
| 7250 | |
| 7251 | |
| 7252 | |
| 7253 | static bool CheckTemplateSpecializationScope(Sema &S, |
| 7254 | NamedDecl *Specialized, |
| 7255 | NamedDecl *PrevDecl, |
| 7256 | SourceLocation Loc, |
| 7257 | bool IsPartialSpecialization) { |
| 7258 | |
| 7259 | |
| 7260 | int EntityKind = 0; |
| 7261 | if (isa<ClassTemplateDecl>(Specialized)) |
| 7262 | EntityKind = IsPartialSpecialization? 1 : 0; |
| 7263 | else if (isa<VarTemplateDecl>(Specialized)) |
| 7264 | EntityKind = IsPartialSpecialization ? 3 : 2; |
| 7265 | else if (isa<FunctionTemplateDecl>(Specialized)) |
| 7266 | EntityKind = 4; |
| 7267 | else if (isa<CXXMethodDecl>(Specialized)) |
| 7268 | EntityKind = 5; |
| 7269 | else if (isa<VarDecl>(Specialized)) |
| 7270 | EntityKind = 6; |
| 7271 | else if (isa<RecordDecl>(Specialized)) |
| 7272 | EntityKind = 7; |
| 7273 | else if (isa<EnumDecl>(Specialized) && S.getLangOpts().CPlusPlus11) |
| 7274 | EntityKind = 8; |
| 7275 | else { |
| 7276 | S.Diag(Loc, diag::err_template_spec_unknown_kind) |
| 7277 | << S.getLangOpts().CPlusPlus11; |
| 7278 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 7279 | return true; |
| 7280 | } |
| 7281 | |
| 7282 | |
| 7283 | |
| 7284 | |
| 7285 | if (S.CurContext->getRedeclContext()->isFunctionOrMethod()) { |
| 7286 | S.Diag(Loc, diag::err_template_spec_decl_function_scope) |
| 7287 | << Specialized; |
| 7288 | return true; |
| 7289 | } |
| 7290 | |
| 7291 | |
| 7292 | |
| 7293 | |
| 7294 | DeclContext *SpecializedContext = |
| 7295 | Specialized->getDeclContext()->getRedeclContext(); |
| 7296 | DeclContext *DC = S.CurContext->getRedeclContext(); |
| 7297 | |
| 7298 | |
| 7299 | |
| 7300 | if (!(DC->isFileContext() ? DC->Encloses(SpecializedContext) |
| 7301 | : DC->Equals(SpecializedContext))) { |
| 7302 | if (isa<TranslationUnitDecl>(SpecializedContext)) |
| 7303 | S.Diag(Loc, diag::err_template_spec_redecl_global_scope) |
| 7304 | << EntityKind << Specialized; |
| 7305 | else { |
| 7306 | auto *ND = cast<NamedDecl>(SpecializedContext); |
| 7307 | int Diag = diag::err_template_spec_redecl_out_of_scope; |
| 7308 | if (S.getLangOpts().MicrosoftExt && !DC->isRecord()) |
| 7309 | Diag = diag::ext_ms_template_spec_redecl_out_of_scope; |
| 7310 | S.Diag(Loc, Diag) << EntityKind << Specialized |
| 7311 | << ND << isa<CXXRecordDecl>(ND); |
| 7312 | } |
| 7313 | |
| 7314 | S.Diag(Specialized->getLocation(), diag::note_specialized_entity); |
| 7315 | |
| 7316 | |
| 7317 | |
| 7318 | if (DC->isRecord()) |
| 7319 | return true; |
| 7320 | } |
| 7321 | |
| 7322 | return false; |
| 7323 | } |
| 7324 | |
| 7325 | static SourceRange findTemplateParameterInType(unsigned Depth, Expr *E) { |
| 7326 | if (!E->isTypeDependent()) |
| 7327 | return SourceLocation(); |
| 7328 | DependencyChecker Checker(Depth, ); |
| 7329 | Checker.TraverseStmt(E); |
| 7330 | if (Checker.MatchLoc.isInvalid()) |
| 7331 | return E->getSourceRange(); |
| 7332 | return Checker.MatchLoc; |
| 7333 | } |
| 7334 | |
| 7335 | static SourceRange findTemplateParameter(unsigned Depth, TypeLoc TL) { |
| 7336 | if (!TL.getType()->isDependentType()) |
| 7337 | return SourceLocation(); |
| 7338 | DependencyChecker Checker(Depth, ); |
| 7339 | Checker.TraverseTypeLoc(TL); |
| 7340 | if (Checker.MatchLoc.isInvalid()) |
| 7341 | return TL.getSourceRange(); |
| 7342 | return Checker.MatchLoc; |
| 7343 | } |
| 7344 | |
| 7345 | |
| 7346 | |
| 7347 | static bool CheckNonTypeTemplatePartialSpecializationArgs( |
| 7348 | Sema &S, SourceLocation TemplateNameLoc, NonTypeTemplateParmDecl *Param, |
| 7349 | const TemplateArgument *Args, unsigned NumArgs, bool IsDefaultArgument) { |
| 7350 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 7351 | if (Args[I].getKind() == TemplateArgument::Pack) { |
| 7352 | if (CheckNonTypeTemplatePartialSpecializationArgs( |
| 7353 | S, TemplateNameLoc, Param, Args[I].pack_begin(), |
| 7354 | Args[I].pack_size(), IsDefaultArgument)) |
| 7355 | return true; |
| 7356 | |
| 7357 | continue; |
| 7358 | } |
| 7359 | |
| 7360 | if (Args[I].getKind() != TemplateArgument::Expression) |
| 7361 | continue; |
| 7362 | |
| 7363 | Expr *ArgExpr = Args[I].getAsExpr(); |
| 7364 | |
| 7365 | |
| 7366 | if (PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(ArgExpr)) |
| 7367 | ArgExpr = Expansion->getPattern(); |
| 7368 | |
| 7369 | |
| 7370 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(ArgExpr)) |
| 7371 | ArgExpr = ICE->getSubExpr(); |
| 7372 | |
| 7373 | |
| 7374 | |
| 7375 | |
| 7376 | |
| 7377 | |
| 7378 | |
| 7379 | |
| 7380 | |
| 7381 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ArgExpr)) |
| 7382 | if (isa<NonTypeTemplateParmDecl>(DRE->getDecl())) |
| 7383 | continue; |
| 7384 | |
| 7385 | |
| 7386 | |
| 7387 | |
| 7388 | |
| 7389 | |
| 7390 | |
| 7391 | |
| 7392 | |
| 7393 | |
| 7394 | |
| 7395 | |
| 7396 | |
| 7397 | |
| 7398 | |
| 7399 | |
| 7400 | SourceRange ParamUseRange = |
| 7401 | findTemplateParameterInType(Param->getDepth(), ArgExpr); |
| 7402 | if (ParamUseRange.isValid()) { |
| 7403 | if (IsDefaultArgument) { |
| 7404 | S.Diag(TemplateNameLoc, |
| 7405 | diag::err_dependent_non_type_arg_in_partial_spec); |
| 7406 | S.Diag(ParamUseRange.getBegin(), |
| 7407 | diag::note_dependent_non_type_default_arg_in_partial_spec) |
| 7408 | << ParamUseRange; |
| 7409 | } else { |
| 7410 | S.Diag(ParamUseRange.getBegin(), |
| 7411 | diag::err_dependent_non_type_arg_in_partial_spec) |
| 7412 | << ParamUseRange; |
| 7413 | } |
| 7414 | return true; |
| 7415 | } |
| 7416 | |
| 7417 | ParamUseRange = findTemplateParameter( |
| 7418 | Param->getDepth(), Param->getTypeSourceInfo()->getTypeLoc()); |
| 7419 | if (ParamUseRange.isValid()) { |
| 7420 | S.Diag(IsDefaultArgument ? TemplateNameLoc : ArgExpr->getBeginLoc(), |
| 7421 | diag::err_dependent_typed_non_type_arg_in_partial_spec) |
| 7422 | << Param->getType(); |
| 7423 | S.Diag(Param->getLocation(), diag::note_template_param_here) |
| 7424 | << (IsDefaultArgument ? ParamUseRange : SourceRange()) |
| 7425 | << ParamUseRange; |
| 7426 | return true; |
| 7427 | } |
| 7428 | } |
| 7429 | |
| 7430 | return false; |
| 7431 | } |
| 7432 | |
| 7433 | |
| 7434 | |
| 7435 | |
| 7436 | |
| 7437 | |
| 7438 | |
| 7439 | |
| 7440 | |
| 7441 | |
| 7442 | |
| 7443 | |
| 7444 | bool Sema::CheckTemplatePartialSpecializationArgs( |
| 7445 | SourceLocation TemplateNameLoc, TemplateDecl *PrimaryTemplate, |
| 7446 | unsigned NumExplicit, ArrayRef<TemplateArgument> TemplateArgs) { |
| 7447 | |
| 7448 | |
| 7449 | if (PrimaryTemplate->getDeclContext()->isDependentContext()) |
| 7450 | return false; |
| 7451 | |
| 7452 | TemplateParameterList *TemplateParams = |
| 7453 | PrimaryTemplate->getTemplateParameters(); |
| 7454 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 7455 | NonTypeTemplateParmDecl *Param |
| 7456 | = dyn_cast<NonTypeTemplateParmDecl>(TemplateParams->getParam(I)); |
| 7457 | if (!Param) |
| 7458 | continue; |
| 7459 | |
| 7460 | if (CheckNonTypeTemplatePartialSpecializationArgs(*this, TemplateNameLoc, |
| 7461 | Param, &TemplateArgs[I], |
| 7462 | 1, I >= NumExplicit)) |
| 7463 | return true; |
| 7464 | } |
| 7465 | |
| 7466 | return false; |
| 7467 | } |
| 7468 | |
| 7469 | DeclResult Sema::ActOnClassTemplateSpecialization( |
| 7470 | Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc, |
| 7471 | SourceLocation ModulePrivateLoc, TemplateIdAnnotation &TemplateId, |
| 7472 | const ParsedAttributesView &Attr, |
| 7473 | MultiTemplateParamsArg TemplateParameterLists, SkipBodyInfo *SkipBody) { |
| 7474 | (0) . __assert_fail ("TUK != TUK_Reference && \"References are not specializations\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 7474, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(TUK != TUK_Reference && "References are not specializations"); |
| 7475 | |
| 7476 | CXXScopeSpec &SS = TemplateId.SS; |
| 7477 | |
| 7478 | |
| 7479 | |
| 7480 | SourceLocation TemplateKWLoc = TemplateParameterLists.size() > 0 |
| 7481 | ? TemplateParameterLists[0]->getTemplateLoc() : KWLoc; |
| 7482 | SourceLocation TemplateNameLoc = TemplateId.TemplateNameLoc; |
| 7483 | SourceLocation LAngleLoc = TemplateId.LAngleLoc; |
| 7484 | SourceLocation RAngleLoc = TemplateId.RAngleLoc; |
| 7485 | |
| 7486 | |
| 7487 | TemplateName Name = TemplateId.Template.get(); |
| 7488 | ClassTemplateDecl *ClassTemplate |
| 7489 | = dyn_cast_or_null<ClassTemplateDecl>(Name.getAsTemplateDecl()); |
| 7490 | |
| 7491 | if (!ClassTemplate) { |
| 7492 | Diag(TemplateNameLoc, diag::err_not_class_template_specialization) |
| 7493 | << (Name.getAsTemplateDecl() && |
| 7494 | isa<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())); |
| 7495 | return true; |
| 7496 | } |
| 7497 | |
| 7498 | bool isMemberSpecialization = false; |
| 7499 | bool isPartialSpecialization = false; |
| 7500 | |
| 7501 | |
| 7502 | |
| 7503 | |
| 7504 | |
| 7505 | bool Invalid = false; |
| 7506 | TemplateParameterList *TemplateParams = |
| 7507 | MatchTemplateParametersToScopeSpecifier( |
| 7508 | KWLoc, TemplateNameLoc, SS, &TemplateId, |
| 7509 | TemplateParameterLists, TUK == TUK_Friend, isMemberSpecialization, |
| 7510 | Invalid); |
| 7511 | if (Invalid) |
| 7512 | return true; |
| 7513 | |
| 7514 | if (TemplateParams && TemplateParams->size() > 0) { |
| 7515 | isPartialSpecialization = true; |
| 7516 | |
| 7517 | if (TUK == TUK_Friend) { |
| 7518 | Diag(KWLoc, diag::err_partial_specialization_friend) |
| 7519 | << SourceRange(LAngleLoc, RAngleLoc); |
| 7520 | return true; |
| 7521 | } |
| 7522 | |
| 7523 | |
| 7524 | |
| 7525 | |
| 7526 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 7527 | Decl *Param = TemplateParams->getParam(I); |
| 7528 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) { |
| 7529 | if (TTP->hasDefaultArgument()) { |
| 7530 | Diag(TTP->getDefaultArgumentLoc(), |
| 7531 | diag::err_default_arg_in_partial_spec); |
| 7532 | TTP->removeDefaultArgument(); |
| 7533 | } |
| 7534 | } else if (NonTypeTemplateParmDecl *NTTP |
| 7535 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 7536 | if (Expr *DefArg = NTTP->getDefaultArgument()) { |
| 7537 | Diag(NTTP->getDefaultArgumentLoc(), |
| 7538 | diag::err_default_arg_in_partial_spec) |
| 7539 | << DefArg->getSourceRange(); |
| 7540 | NTTP->removeDefaultArgument(); |
| 7541 | } |
| 7542 | } else { |
| 7543 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(Param); |
| 7544 | if (TTP->hasDefaultArgument()) { |
| 7545 | Diag(TTP->getDefaultArgument().getLocation(), |
| 7546 | diag::err_default_arg_in_partial_spec) |
| 7547 | << TTP->getDefaultArgument().getSourceRange(); |
| 7548 | TTP->removeDefaultArgument(); |
| 7549 | } |
| 7550 | } |
| 7551 | } |
| 7552 | } else if (TemplateParams) { |
| 7553 | if (TUK == TUK_Friend) |
| 7554 | Diag(KWLoc, diag::err_template_spec_friend) |
| 7555 | << FixItHint::CreateRemoval( |
| 7556 | SourceRange(TemplateParams->getTemplateLoc(), |
| 7557 | TemplateParams->getRAngleLoc())) |
| 7558 | << SourceRange(LAngleLoc, RAngleLoc); |
| 7559 | } else { |
| 7560 | ' for this decl") ? static_cast (0) . __assert_fail ("TUK == TUK_Friend && \"should have a 'template<>' for this decl\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 7560, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(TUK == TUK_Friend && "should have a 'template<>' for this decl"); |
| 7561 | } |
| 7562 | |
| 7563 | |
| 7564 | |
| 7565 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 7566 | (0) . __assert_fail ("Kind != TTK_Enum && \"Invalid enum tag in class template spec!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 7566, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Kind != TTK_Enum && "Invalid enum tag in class template spec!"); |
| 7567 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
| 7568 | Kind, TUK == TUK_Definition, KWLoc, |
| 7569 | ClassTemplate->getIdentifier())) { |
| 7570 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
| 7571 | << ClassTemplate |
| 7572 | << FixItHint::CreateReplacement(KWLoc, |
| 7573 | ClassTemplate->getTemplatedDecl()->getKindName()); |
| 7574 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
| 7575 | diag::note_previous_use); |
| 7576 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 7577 | } |
| 7578 | |
| 7579 | |
| 7580 | TemplateArgumentListInfo TemplateArgs = |
| 7581 | makeTemplateArgumentListInfo(*this, TemplateId); |
| 7582 | |
| 7583 | |
| 7584 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 7585 | if (DiagnoseUnexpandedParameterPack(TemplateArgs[I], |
| 7586 | UPPC_PartialSpecialization)) |
| 7587 | return true; |
| 7588 | |
| 7589 | |
| 7590 | |
| 7591 | SmallVector<TemplateArgument, 4> Converted; |
| 7592 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 7593 | TemplateArgs, false, Converted)) |
| 7594 | return true; |
| 7595 | |
| 7596 | |
| 7597 | |
| 7598 | if (isPartialSpecialization) { |
| 7599 | if (CheckTemplatePartialSpecializationArgs(TemplateNameLoc, ClassTemplate, |
| 7600 | TemplateArgs.size(), Converted)) |
| 7601 | return true; |
| 7602 | |
| 7603 | |
| 7604 | |
| 7605 | bool InstantiationDependent; |
| 7606 | if (!Name.isDependent() && |
| 7607 | !TemplateSpecializationType::anyDependentTemplateArguments( |
| 7608 | TemplateArgs.arguments(), InstantiationDependent)) { |
| 7609 | Diag(TemplateNameLoc, diag::err_partial_spec_fully_specialized) |
| 7610 | << ClassTemplate->getDeclName(); |
| 7611 | isPartialSpecialization = false; |
| 7612 | } |
| 7613 | } |
| 7614 | |
| 7615 | void *InsertPos = nullptr; |
| 7616 | ClassTemplateSpecializationDecl *PrevDecl = nullptr; |
| 7617 | |
| 7618 | if (isPartialSpecialization) |
| 7619 | |
| 7620 | PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos); |
| 7621 | else |
| 7622 | PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos); |
| 7623 | |
| 7624 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
| 7625 | |
| 7626 | |
| 7627 | |
| 7628 | if (TUK != TUK_Friend && |
| 7629 | CheckTemplateSpecializationScope(*this, ClassTemplate, PrevDecl, |
| 7630 | TemplateNameLoc, |
| 7631 | isPartialSpecialization)) |
| 7632 | return true; |
| 7633 | |
| 7634 | |
| 7635 | QualType CanonType; |
| 7636 | if (isPartialSpecialization) { |
| 7637 | |
| 7638 | |
| 7639 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 7640 | CanonType = Context.getTemplateSpecializationType(CanonTemplate, |
| 7641 | Converted); |
| 7642 | |
| 7643 | if (Context.hasSameType(CanonType, |
| 7644 | ClassTemplate->getInjectedClassNameSpecialization())) { |
| 7645 | |
| 7646 | |
| 7647 | |
| 7648 | |
| 7649 | |
| 7650 | |
| 7651 | |
| 7652 | Diag(TemplateNameLoc, diag::err_partial_spec_args_match_primary_template) |
| 7653 | << << (TUK == TUK_Definition) |
| 7654 | << FixItHint::CreateRemoval(SourceRange(LAngleLoc, RAngleLoc)); |
| 7655 | return CheckClassTemplate(S, TagSpec, TUK, KWLoc, SS, |
| 7656 | ClassTemplate->getIdentifier(), |
| 7657 | TemplateNameLoc, |
| 7658 | Attr, |
| 7659 | TemplateParams, |
| 7660 | AS_none, SourceLocation(), |
| 7661 | SourceLocation(), |
| 7662 | TemplateParameterLists.size() - 1, |
| 7663 | TemplateParameterLists.data()); |
| 7664 | } |
| 7665 | |
| 7666 | |
| 7667 | ClassTemplatePartialSpecializationDecl *PrevPartial |
| 7668 | = cast_or_null<ClassTemplatePartialSpecializationDecl>(PrevDecl); |
| 7669 | ClassTemplatePartialSpecializationDecl *Partial |
| 7670 | = ClassTemplatePartialSpecializationDecl::Create(Context, Kind, |
| 7671 | ClassTemplate->getDeclContext(), |
| 7672 | KWLoc, TemplateNameLoc, |
| 7673 | TemplateParams, |
| 7674 | ClassTemplate, |
| 7675 | Converted, |
| 7676 | TemplateArgs, |
| 7677 | CanonType, |
| 7678 | PrevPartial); |
| 7679 | SetNestedNameSpecifier(*this, Partial, SS); |
| 7680 | if (TemplateParameterLists.size() > 1 && SS.isSet()) { |
| 7681 | Partial->setTemplateParameterListsInfo( |
| 7682 | Context, TemplateParameterLists.drop_back(1)); |
| 7683 | } |
| 7684 | |
| 7685 | if (!PrevPartial) |
| 7686 | ClassTemplate->AddPartialSpecialization(Partial, InsertPos); |
| 7687 | Specialization = Partial; |
| 7688 | |
| 7689 | |
| 7690 | |
| 7691 | if (PrevPartial && PrevPartial->getInstantiatedFromMember()) |
| 7692 | PrevPartial->setMemberSpecialization(); |
| 7693 | |
| 7694 | CheckTemplatePartialSpecialization(Partial); |
| 7695 | } else { |
| 7696 | |
| 7697 | |
| 7698 | Specialization |
| 7699 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
| 7700 | ClassTemplate->getDeclContext(), |
| 7701 | KWLoc, TemplateNameLoc, |
| 7702 | ClassTemplate, |
| 7703 | Converted, |
| 7704 | PrevDecl); |
| 7705 | SetNestedNameSpecifier(*this, Specialization, SS); |
| 7706 | if (TemplateParameterLists.size() > 0) { |
| 7707 | Specialization->setTemplateParameterListsInfo(Context, |
| 7708 | TemplateParameterLists); |
| 7709 | } |
| 7710 | |
| 7711 | if (!PrevDecl) |
| 7712 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
| 7713 | |
| 7714 | if (CurContext->isDependentContext()) { |
| 7715 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 7716 | CanonType = Context.getTemplateSpecializationType( |
| 7717 | CanonTemplate, Converted); |
| 7718 | } else { |
| 7719 | CanonType = Context.getTypeDeclType(Specialization); |
| 7720 | } |
| 7721 | } |
| 7722 | |
| 7723 | |
| 7724 | |
| 7725 | |
| 7726 | |
| 7727 | |
| 7728 | |
| 7729 | if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) { |
| 7730 | bool Okay = false; |
| 7731 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 7732 | |
| 7733 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 7734 | Okay = true; |
| 7735 | break; |
| 7736 | } |
| 7737 | } |
| 7738 | |
| 7739 | if (!Okay) { |
| 7740 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 7741 | Diag(TemplateNameLoc, diag::err_specialization_after_instantiation) |
| 7742 | << Context.getTypeDeclType(Specialization) << Range; |
| 7743 | |
| 7744 | Diag(PrevDecl->getPointOfInstantiation(), |
| 7745 | diag::note_instantiation_required_here) |
| 7746 | << (PrevDecl->getTemplateSpecializationKind() |
| 7747 | != TSK_ImplicitInstantiation); |
| 7748 | return true; |
| 7749 | } |
| 7750 | } |
| 7751 | |
| 7752 | |
| 7753 | if (TUK != TUK_Friend) |
| 7754 | Specialization->setSpecializationKind(TSK_ExplicitSpecialization); |
| 7755 | |
| 7756 | |
| 7757 | if (TUK == TUK_Definition) { |
| 7758 | RecordDecl *Def = Specialization->getDefinition(); |
| 7759 | NamedDecl *Hidden = nullptr; |
| 7760 | if (Def && SkipBody && !hasVisibleDefinition(Def, &Hidden)) { |
| 7761 | SkipBody->ShouldSkip = true; |
| 7762 | SkipBody->Previous = Def; |
| 7763 | makeMergedDefinitionVisible(Hidden); |
| 7764 | } else if (Def) { |
| 7765 | SourceRange Range(TemplateNameLoc, RAngleLoc); |
| 7766 | Diag(TemplateNameLoc, diag::err_redefinition) << Specialization << Range; |
| 7767 | Diag(Def->getLocation(), diag::note_previous_definition); |
| 7768 | Specialization->setInvalidDecl(); |
| 7769 | return true; |
| 7770 | } |
| 7771 | } |
| 7772 | |
| 7773 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 7774 | |
| 7775 | |
| 7776 | |
| 7777 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) { |
| 7778 | AddAlignmentAttributesForRecord(Specialization); |
| 7779 | AddMsStructLayoutForRecord(Specialization); |
| 7780 | } |
| 7781 | |
| 7782 | if (ModulePrivateLoc.isValid()) |
| 7783 | Diag(Specialization->getLocation(), diag::err_module_private_specialization) |
| 7784 | << (isPartialSpecialization? 1 : 0) |
| 7785 | << FixItHint::CreateRemoval(ModulePrivateLoc); |
| 7786 | |
| 7787 | |
| 7788 | |
| 7789 | |
| 7790 | |
| 7791 | |
| 7792 | |
| 7793 | |
| 7794 | TypeSourceInfo *WrittenTy |
| 7795 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 7796 | TemplateArgs, CanonType); |
| 7797 | if (TUK != TUK_Friend) { |
| 7798 | Specialization->setTypeAsWritten(WrittenTy); |
| 7799 | Specialization->setTemplateKeywordLoc(TemplateKWLoc); |
| 7800 | } |
| 7801 | |
| 7802 | |
| 7803 | |
| 7804 | |
| 7805 | |
| 7806 | |
| 7807 | |
| 7808 | |
| 7809 | |
| 7810 | Specialization->setLexicalDeclContext(CurContext); |
| 7811 | |
| 7812 | |
| 7813 | if (TUK == TUK_Definition && (!SkipBody || !SkipBody->ShouldSkip)) |
| 7814 | Specialization->startDefinition(); |
| 7815 | |
| 7816 | if (TUK == TUK_Friend) { |
| 7817 | FriendDecl *Friend = FriendDecl::Create(Context, CurContext, |
| 7818 | TemplateNameLoc, |
| 7819 | WrittenTy, |
| 7820 | KWLoc); |
| 7821 | Friend->setAccess(AS_public); |
| 7822 | CurContext->addDecl(Friend); |
| 7823 | } else { |
| 7824 | |
| 7825 | |
| 7826 | |
| 7827 | CurContext->addDecl(Specialization); |
| 7828 | } |
| 7829 | |
| 7830 | if (SkipBody && SkipBody->ShouldSkip) |
| 7831 | return SkipBody->Previous; |
| 7832 | |
| 7833 | return Specialization; |
| 7834 | } |
| 7835 | |
| 7836 | Decl *Sema::ActOnTemplateDeclarator(Scope *S, |
| 7837 | MultiTemplateParamsArg TemplateParameterLists, |
| 7838 | Declarator &D) { |
| 7839 | Decl *NewDecl = HandleDeclarator(S, D, TemplateParameterLists); |
| 7840 | ActOnDocumentableDecl(NewDecl); |
| 7841 | return NewDecl; |
| 7842 | } |
| 7843 | |
| 7844 | |
| 7845 | |
| 7846 | static void StripImplicitInstantiation(NamedDecl *D) { |
| 7847 | D->dropAttr<DLLImportAttr>(); |
| 7848 | D->dropAttr<DLLExportAttr>(); |
| 7849 | |
| 7850 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 7851 | FD->setInlineSpecified(false); |
| 7852 | } |
| 7853 | |
| 7854 | |
| 7855 | |
| 7856 | static SourceLocation DiagLocForExplicitInstantiation( |
| 7857 | NamedDecl* D, SourceLocation PointOfInstantiation) { |
| 7858 | |
| 7859 | |
| 7860 | |
| 7861 | SourceLocation PrevDiagLoc = PointOfInstantiation; |
| 7862 | for (Decl *Prev = D; Prev && !PrevDiagLoc.isValid(); |
| 7863 | Prev = Prev->getPreviousDecl()) { |
| 7864 | PrevDiagLoc = Prev->getLocation(); |
| 7865 | } |
| 7866 | (0) . __assert_fail ("PrevDiagLoc.isValid() && \"Explicit instantiation without point of instantiation?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 7867, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(PrevDiagLoc.isValid() && |
| 7867 | (0) . __assert_fail ("PrevDiagLoc.isValid() && \"Explicit instantiation without point of instantiation?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 7867, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Explicit instantiation without point of instantiation?"); |
| 7868 | return PrevDiagLoc; |
| 7869 | } |
| 7870 | |
| 7871 | |
| 7872 | |
| 7873 | |
| 7874 | |
| 7875 | |
| 7876 | |
| 7877 | |
| 7878 | |
| 7879 | |
| 7880 | |
| 7881 | |
| 7882 | |
| 7883 | |
| 7884 | |
| 7885 | |
| 7886 | |
| 7887 | |
| 7888 | |
| 7889 | |
| 7890 | |
| 7891 | |
| 7892 | |
| 7893 | bool |
| 7894 | Sema::CheckSpecializationInstantiationRedecl(SourceLocation NewLoc, |
| 7895 | TemplateSpecializationKind NewTSK, |
| 7896 | NamedDecl *PrevDecl, |
| 7897 | TemplateSpecializationKind PrevTSK, |
| 7898 | SourceLocation PrevPointOfInstantiation, |
| 7899 | bool &HasNoEffect) { |
| 7900 | HasNoEffect = false; |
| 7901 | |
| 7902 | switch (NewTSK) { |
| 7903 | case TSK_Undeclared: |
| 7904 | case TSK_ImplicitInstantiation: |
| 7905 | (0) . __assert_fail ("(PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && \"previous declaration must be implicit!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 7907, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert( |
| 7906 | (0) . __assert_fail ("(PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && \"previous declaration must be implicit!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 7907, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> (PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && |
| 7907 | (0) . __assert_fail ("(PrevTSK == TSK_Undeclared || PrevTSK == TSK_ImplicitInstantiation) && \"previous declaration must be implicit!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 7907, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "previous declaration must be implicit!"); |
| 7908 | return false; |
| 7909 | |
| 7910 | case TSK_ExplicitSpecialization: |
| 7911 | switch (PrevTSK) { |
| 7912 | case TSK_Undeclared: |
| 7913 | case TSK_ExplicitSpecialization: |
| 7914 | |
| 7915 | |
| 7916 | |
| 7917 | return false; |
| 7918 | |
| 7919 | case TSK_ImplicitInstantiation: |
| 7920 | if (PrevPointOfInstantiation.isInvalid()) { |
| 7921 | |
| 7922 | |
| 7923 | StripImplicitInstantiation(PrevDecl); |
| 7924 | return false; |
| 7925 | } |
| 7926 | |
| 7927 | LLVM_FALLTHROUGH; |
| 7928 | |
| 7929 | case TSK_ExplicitInstantiationDeclaration: |
| 7930 | case TSK_ExplicitInstantiationDefinition: |
| 7931 | (0) . __assert_fail ("(PrevTSK == TSK_ImplicitInstantiation || PrevPointOfInstantiation.isValid()) && \"Explicit instantiation without point of instantiation?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 7933, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((PrevTSK == TSK_ImplicitInstantiation || |
| 7932 | (0) . __assert_fail ("(PrevTSK == TSK_ImplicitInstantiation || PrevPointOfInstantiation.isValid()) && \"Explicit instantiation without point of instantiation?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 7933, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> PrevPointOfInstantiation.isValid()) && |
| 7933 | (0) . __assert_fail ("(PrevTSK == TSK_ImplicitInstantiation || PrevPointOfInstantiation.isValid()) && \"Explicit instantiation without point of instantiation?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 7933, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Explicit instantiation without point of instantiation?"); |
| 7934 | |
| 7935 | |
| 7936 | |
| 7937 | |
| 7938 | |
| 7939 | |
| 7940 | |
| 7941 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 7942 | |
| 7943 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) |
| 7944 | return false; |
| 7945 | } |
| 7946 | |
| 7947 | Diag(NewLoc, diag::err_specialization_after_instantiation) |
| 7948 | << PrevDecl; |
| 7949 | Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here) |
| 7950 | << (PrevTSK != TSK_ImplicitInstantiation); |
| 7951 | |
| 7952 | return true; |
| 7953 | } |
| 7954 | llvm_unreachable("The switch over PrevTSK must be exhaustive."); |
| 7955 | |
| 7956 | case TSK_ExplicitInstantiationDeclaration: |
| 7957 | switch (PrevTSK) { |
| 7958 | case TSK_ExplicitInstantiationDeclaration: |
| 7959 | |
| 7960 | HasNoEffect = true; |
| 7961 | return false; |
| 7962 | |
| 7963 | case TSK_Undeclared: |
| 7964 | case TSK_ImplicitInstantiation: |
| 7965 | |
| 7966 | |
| 7967 | return false; |
| 7968 | |
| 7969 | case TSK_ExplicitSpecialization: |
| 7970 | |
| 7971 | |
| 7972 | |
| 7973 | |
| 7974 | |
| 7975 | HasNoEffect = true; |
| 7976 | return false; |
| 7977 | |
| 7978 | case TSK_ExplicitInstantiationDefinition: |
| 7979 | |
| 7980 | |
| 7981 | |
| 7982 | |
| 7983 | Diag(NewLoc, |
| 7984 | diag::err_explicit_instantiation_declaration_after_definition); |
| 7985 | |
| 7986 | |
| 7987 | |
| 7988 | |
| 7989 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 7990 | diag::note_explicit_instantiation_definition_here); |
| 7991 | HasNoEffect = true; |
| 7992 | return false; |
| 7993 | } |
| 7994 | llvm_unreachable("Unexpected TemplateSpecializationKind!"); |
| 7995 | |
| 7996 | case TSK_ExplicitInstantiationDefinition: |
| 7997 | switch (PrevTSK) { |
| 7998 | case TSK_Undeclared: |
| 7999 | case TSK_ImplicitInstantiation: |
| 8000 | |
| 8001 | |
| 8002 | return false; |
| 8003 | |
| 8004 | case TSK_ExplicitSpecialization: |
| 8005 | |
| 8006 | |
| 8007 | |
| 8008 | |
| 8009 | |
| 8010 | Diag(NewLoc, diag::warn_explicit_instantiation_after_specialization) |
| 8011 | << PrevDecl; |
| 8012 | Diag(PrevDecl->getLocation(), |
| 8013 | diag::note_previous_template_specialization); |
| 8014 | HasNoEffect = true; |
| 8015 | return false; |
| 8016 | |
| 8017 | case TSK_ExplicitInstantiationDeclaration: |
| 8018 | |
| 8019 | |
| 8020 | |
| 8021 | |
| 8022 | |
| 8023 | |
| 8024 | |
| 8025 | |
| 8026 | for (Decl *Prev = PrevDecl; Prev; Prev = Prev->getPreviousDecl()) { |
| 8027 | |
| 8028 | if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) { |
| 8029 | HasNoEffect = true; |
| 8030 | break; |
| 8031 | } |
| 8032 | } |
| 8033 | |
| 8034 | return false; |
| 8035 | |
| 8036 | case TSK_ExplicitInstantiationDefinition: |
| 8037 | |
| 8038 | |
| 8039 | |
| 8040 | |
| 8041 | |
| 8042 | |
| 8043 | Diag(NewLoc, (getLangOpts().MSVCCompat) |
| 8044 | ? diag::ext_explicit_instantiation_duplicate |
| 8045 | : diag::err_explicit_instantiation_duplicate) |
| 8046 | << PrevDecl; |
| 8047 | Diag(DiagLocForExplicitInstantiation(PrevDecl, PrevPointOfInstantiation), |
| 8048 | diag::note_previous_explicit_instantiation); |
| 8049 | HasNoEffect = true; |
| 8050 | return false; |
| 8051 | } |
| 8052 | } |
| 8053 | |
| 8054 | llvm_unreachable("Missing specialization/instantiation case?"); |
| 8055 | } |
| 8056 | |
| 8057 | |
| 8058 | |
| 8059 | |
| 8060 | |
| 8061 | |
| 8062 | |
| 8063 | |
| 8064 | |
| 8065 | |
| 8066 | |
| 8067 | |
| 8068 | |
| 8069 | |
| 8070 | |
| 8071 | |
| 8072 | bool |
| 8073 | Sema::CheckDependentFunctionTemplateSpecialization(FunctionDecl *FD, |
| 8074 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
| 8075 | LookupResult &Previous) { |
| 8076 | |
| 8077 | |
| 8078 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
| 8079 | LookupResult::Filter F = Previous.makeFilter(); |
| 8080 | enum DiscardReason { NotAFunctionTemplate, NotAMemberOfEnclosing }; |
| 8081 | SmallVector<std::pair<DiscardReason, Decl *>, 8> DiscardedCandidates; |
| 8082 | while (F.hasNext()) { |
| 8083 | NamedDecl *D = F.next()->getUnderlyingDecl(); |
| 8084 | if (!isa<FunctionTemplateDecl>(D)) { |
| 8085 | F.erase(); |
| 8086 | DiscardedCandidates.push_back(std::make_pair(NotAFunctionTemplate, D)); |
| 8087 | continue; |
| 8088 | } |
| 8089 | |
| 8090 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 8091 | D->getDeclContext()->getRedeclContext())) { |
| 8092 | F.erase(); |
| 8093 | DiscardedCandidates.push_back(std::make_pair(NotAMemberOfEnclosing, D)); |
| 8094 | continue; |
| 8095 | } |
| 8096 | } |
| 8097 | F.done(); |
| 8098 | |
| 8099 | if (Previous.empty()) { |
| 8100 | Diag(FD->getLocation(), |
| 8101 | diag::err_dependent_function_template_spec_no_match); |
| 8102 | for (auto &P : DiscardedCandidates) |
| 8103 | Diag(P.second->getLocation(), |
| 8104 | diag::note_dependent_function_template_spec_discard_reason) |
| 8105 | << P.first; |
| 8106 | return true; |
| 8107 | } |
| 8108 | |
| 8109 | FD->setDependentTemplateSpecialization(Context, Previous.asUnresolvedSet(), |
| 8110 | ExplicitTemplateArgs); |
| 8111 | return false; |
| 8112 | } |
| 8113 | |
| 8114 | |
| 8115 | |
| 8116 | |
| 8117 | |
| 8118 | |
| 8119 | |
| 8120 | |
| 8121 | |
| 8122 | |
| 8123 | |
| 8124 | |
| 8125 | |
| 8126 | |
| 8127 | |
| 8128 | |
| 8129 | |
| 8130 | |
| 8131 | |
| 8132 | |
| 8133 | |
| 8134 | |
| 8135 | |
| 8136 | bool Sema::CheckFunctionTemplateSpecialization( |
| 8137 | FunctionDecl *FD, TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 8138 | LookupResult &Previous, bool QualifiedFriend) { |
| 8139 | |
| 8140 | |
| 8141 | UnresolvedSet<8> Candidates; |
| 8142 | TemplateSpecCandidateSet FailedCandidates(FD->getLocation(), |
| 8143 | ); |
| 8144 | |
| 8145 | llvm::SmallDenseMap<FunctionDecl *, TemplateArgumentListInfo, 8> |
| 8146 | ConvertedTemplateArgs; |
| 8147 | |
| 8148 | DeclContext *FDLookupContext = FD->getDeclContext()->getRedeclContext(); |
| 8149 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 8150 | I != E; ++I) { |
| 8151 | NamedDecl *Ovl = (*I)->getUnderlyingDecl(); |
| 8152 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Ovl)) { |
| 8153 | |
| 8154 | |
| 8155 | if (!FDLookupContext->InEnclosingNamespaceSetOf( |
| 8156 | Ovl->getDeclContext()->getRedeclContext())) |
| 8157 | continue; |
| 8158 | |
| 8159 | |
| 8160 | |
| 8161 | |
| 8162 | |
| 8163 | |
| 8164 | QualType FT = FD->getType(); |
| 8165 | if (FD->isConstexpr()) { |
| 8166 | CXXMethodDecl *OldMD = |
| 8167 | dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 8168 | if (OldMD && OldMD->isConst()) { |
| 8169 | const FunctionProtoType *FPT = FT->castAs<FunctionProtoType>(); |
| 8170 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8171 | EPI.TypeQuals.addConst(); |
| 8172 | FT = Context.getFunctionType(FPT->getReturnType(), |
| 8173 | FPT->getParamTypes(), EPI); |
| 8174 | } |
| 8175 | } |
| 8176 | |
| 8177 | TemplateArgumentListInfo Args; |
| 8178 | if (ExplicitTemplateArgs) |
| 8179 | Args = *ExplicitTemplateArgs; |
| 8180 | |
| 8181 | |
| 8182 | |
| 8183 | |
| 8184 | |
| 8185 | |
| 8186 | |
| 8187 | |
| 8188 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 8189 | FunctionDecl *Specialization = nullptr; |
| 8190 | if (TemplateDeductionResult TDK = DeduceTemplateArguments( |
| 8191 | cast<FunctionTemplateDecl>(FunTmpl->getFirstDecl()), |
| 8192 | ExplicitTemplateArgs ? &Args : nullptr, FT, Specialization, |
| 8193 | Info)) { |
| 8194 | |
| 8195 | |
| 8196 | FailedCandidates.addCandidate().set( |
| 8197 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 8198 | MakeDeductionFailureInfo(Context, TDK, Info)); |
| 8199 | (void)TDK; |
| 8200 | continue; |
| 8201 | } |
| 8202 | |
| 8203 | |
| 8204 | |
| 8205 | |
| 8206 | |
| 8207 | |
| 8208 | if (LangOpts.CUDA && |
| 8209 | IdentifyCUDATarget(Specialization, |
| 8210 | true) != |
| 8211 | IdentifyCUDATarget(FD, true)) { |
| 8212 | FailedCandidates.addCandidate().set( |
| 8213 | I.getPair(), FunTmpl->getTemplatedDecl(), |
| 8214 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 8215 | continue; |
| 8216 | } |
| 8217 | |
| 8218 | |
| 8219 | if (ExplicitTemplateArgs) |
| 8220 | ConvertedTemplateArgs[Specialization] = std::move(Args); |
| 8221 | Candidates.addDecl(Specialization, I.getAccess()); |
| 8222 | } |
| 8223 | } |
| 8224 | |
| 8225 | |
| 8226 | |
| 8227 | |
| 8228 | if (QualifiedFriend && Candidates.empty()) { |
| 8229 | Diag(FD->getLocation(), diag::err_qualified_friend_no_match) |
| 8230 | << FD->getDeclName() << FDLookupContext; |
| 8231 | |
| 8232 | |
| 8233 | for (auto *OldND : Previous) { |
| 8234 | if (auto *OldFD = dyn_cast<FunctionDecl>(OldND->getUnderlyingDecl())) |
| 8235 | NoteOverloadCandidate(OldND, OldFD, FD->getType(), false); |
| 8236 | } |
| 8237 | FailedCandidates.NoteCandidates(*this, FD->getLocation()); |
| 8238 | return true; |
| 8239 | } |
| 8240 | |
| 8241 | |
| 8242 | UnresolvedSetIterator Result = getMostSpecialized( |
| 8243 | Candidates.begin(), Candidates.end(), FailedCandidates, FD->getLocation(), |
| 8244 | PDiag(diag::err_function_template_spec_no_match) << FD->getDeclName(), |
| 8245 | PDiag(diag::err_function_template_spec_ambiguous) |
| 8246 | << FD->getDeclName() << (ExplicitTemplateArgs != nullptr), |
| 8247 | PDiag(diag::note_function_template_spec_matched)); |
| 8248 | |
| 8249 | if (Result == Candidates.end()) |
| 8250 | return true; |
| 8251 | |
| 8252 | |
| 8253 | FunctionDecl *Specialization = cast<FunctionDecl>(*Result); |
| 8254 | |
| 8255 | FunctionTemplateSpecializationInfo *SpecInfo |
| 8256 | = Specialization->getTemplateSpecializationInfo(); |
| 8257 | (0) . __assert_fail ("SpecInfo && \"Function template specialization info missing?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 8257, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(SpecInfo && "Function template specialization info missing?"); |
| 8258 | |
| 8259 | |
| 8260 | |
| 8261 | TemplateSpecializationKind TSK = SpecInfo->getTemplateSpecializationKind(); |
| 8262 | if (TSK == TSK_Undeclared || TSK == TSK_ImplicitInstantiation) { |
| 8263 | Specialization->setLocation(FD->getLocation()); |
| 8264 | Specialization->setLexicalDeclContext(FD->getLexicalDeclContext()); |
| 8265 | |
| 8266 | |
| 8267 | |
| 8268 | |
| 8269 | |
| 8270 | |
| 8271 | Specialization->setConstexpr(FD->isConstexpr()); |
| 8272 | } |
| 8273 | |
| 8274 | |
| 8275 | |
| 8276 | |
| 8277 | |
| 8278 | |
| 8279 | bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None); |
| 8280 | |
| 8281 | |
| 8282 | if (!isFriend && |
| 8283 | CheckTemplateSpecializationScope(*this, |
| 8284 | Specialization->getPrimaryTemplate(), |
| 8285 | Specialization, FD->getLocation(), |
| 8286 | false)) |
| 8287 | return true; |
| 8288 | |
| 8289 | |
| 8290 | |
| 8291 | |
| 8292 | |
| 8293 | |
| 8294 | |
| 8295 | bool HasNoEffect = false; |
| 8296 | if (!isFriend && |
| 8297 | CheckSpecializationInstantiationRedecl(FD->getLocation(), |
| 8298 | TSK_ExplicitSpecialization, |
| 8299 | Specialization, |
| 8300 | SpecInfo->getTemplateSpecializationKind(), |
| 8301 | SpecInfo->getPointOfInstantiation(), |
| 8302 | HasNoEffect)) |
| 8303 | return true; |
| 8304 | |
| 8305 | |
| 8306 | |
| 8307 | if (!isFriend) { |
| 8308 | |
| 8309 | |
| 8310 | |
| 8311 | |
| 8312 | |
| 8313 | |
| 8314 | |
| 8315 | |
| 8316 | if (Specialization->isDeleted() && !SpecInfo->isExplicitSpecialization() && |
| 8317 | !Specialization->getCanonicalDecl()->isReferenced()) { |
| 8318 | |
| 8319 | (0) . __assert_fail ("Specialization->getCanonicalDecl() == Specialization && \"This must be the only existing declaration of this specialization\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 8321, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert( |
| 8320 | (0) . __assert_fail ("Specialization->getCanonicalDecl() == Specialization && \"This must be the only existing declaration of this specialization\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 8321, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> Specialization->getCanonicalDecl() == Specialization && |
| 8321 | (0) . __assert_fail ("Specialization->getCanonicalDecl() == Specialization && \"This must be the only existing declaration of this specialization\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 8321, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "This must be the only existing declaration of this specialization"); |
| 8322 | |
| 8323 | Specialization->setDeletedAsWritten(false); |
| 8324 | } |
| 8325 | |
| 8326 | SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
| 8327 | MarkUnusedFileScopedDecl(Specialization); |
| 8328 | } |
| 8329 | |
| 8330 | |
| 8331 | |
| 8332 | |
| 8333 | |
| 8334 | const TemplateArgumentList* TemplArgs = new (Context) |
| 8335 | TemplateArgumentList(Specialization->getTemplateSpecializationArgs()); |
| 8336 | FD->setFunctionTemplateSpecialization( |
| 8337 | Specialization->getPrimaryTemplate(), TemplArgs, , |
| 8338 | SpecInfo->getTemplateSpecializationKind(), |
| 8339 | ExplicitTemplateArgs ? &ConvertedTemplateArgs[Specialization] : nullptr); |
| 8340 | |
| 8341 | |
| 8342 | |
| 8343 | |
| 8344 | |
| 8345 | |
| 8346 | if (LangOpts.CUDA) |
| 8347 | inheritCUDATargetAttrs(FD, *Specialization->getPrimaryTemplate()); |
| 8348 | |
| 8349 | |
| 8350 | |
| 8351 | Previous.clear(); |
| 8352 | Previous.addDecl(Specialization); |
| 8353 | return false; |
| 8354 | } |
| 8355 | |
| 8356 | |
| 8357 | |
| 8358 | |
| 8359 | |
| 8360 | |
| 8361 | |
| 8362 | |
| 8363 | |
| 8364 | |
| 8365 | |
| 8366 | |
| 8367 | |
| 8368 | |
| 8369 | |
| 8370 | bool |
| 8371 | Sema::CheckMemberSpecialization(NamedDecl *Member, LookupResult &Previous) { |
| 8372 | (0) . __assert_fail ("!isa(Member) && \"Only for non-template members\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 8372, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!isa<TemplateDecl>(Member) && "Only for non-template members"); |
| 8373 | |
| 8374 | |
| 8375 | NamedDecl *FoundInstantiation = nullptr; |
| 8376 | NamedDecl *Instantiation = nullptr; |
| 8377 | NamedDecl *InstantiatedFrom = nullptr; |
| 8378 | MemberSpecializationInfo *MSInfo = nullptr; |
| 8379 | |
| 8380 | if (Previous.empty()) { |
| 8381 | |
| 8382 | } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Member)) { |
| 8383 | for (LookupResult::iterator I = Previous.begin(), E = Previous.end(); |
| 8384 | I != E; ++I) { |
| 8385 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 8386 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 8387 | QualType Adjusted = Function->getType(); |
| 8388 | if (!hasExplicitCallingConv(Adjusted)) |
| 8389 | Adjusted = adjustCCAndNoReturn(Adjusted, Method->getType()); |
| 8390 | |
| 8391 | |
| 8392 | if (Context.hasSameType(Adjusted, Method->getType())) { |
| 8393 | FoundInstantiation = *I; |
| 8394 | Instantiation = Method; |
| 8395 | InstantiatedFrom = Method->getInstantiatedFromMemberFunction(); |
| 8396 | MSInfo = Method->getMemberSpecializationInfo(); |
| 8397 | break; |
| 8398 | } |
| 8399 | } |
| 8400 | } |
| 8401 | } else if (isa<VarDecl>(Member)) { |
| 8402 | VarDecl *PrevVar; |
| 8403 | if (Previous.isSingleResult() && |
| 8404 | (PrevVar = dyn_cast<VarDecl>(Previous.getFoundDecl()))) |
| 8405 | if (PrevVar->isStaticDataMember()) { |
| 8406 | FoundInstantiation = Previous.getRepresentativeDecl(); |
| 8407 | Instantiation = PrevVar; |
| 8408 | InstantiatedFrom = PrevVar->getInstantiatedFromStaticDataMember(); |
| 8409 | MSInfo = PrevVar->getMemberSpecializationInfo(); |
| 8410 | } |
| 8411 | } else if (isa<RecordDecl>(Member)) { |
| 8412 | CXXRecordDecl *PrevRecord; |
| 8413 | if (Previous.isSingleResult() && |
| 8414 | (PrevRecord = dyn_cast<CXXRecordDecl>(Previous.getFoundDecl()))) { |
| 8415 | FoundInstantiation = Previous.getRepresentativeDecl(); |
| 8416 | Instantiation = PrevRecord; |
| 8417 | InstantiatedFrom = PrevRecord->getInstantiatedFromMemberClass(); |
| 8418 | MSInfo = PrevRecord->getMemberSpecializationInfo(); |
| 8419 | } |
| 8420 | } else if (isa<EnumDecl>(Member)) { |
| 8421 | EnumDecl *PrevEnum; |
| 8422 | if (Previous.isSingleResult() && |
| 8423 | (PrevEnum = dyn_cast<EnumDecl>(Previous.getFoundDecl()))) { |
| 8424 | FoundInstantiation = Previous.getRepresentativeDecl(); |
| 8425 | Instantiation = PrevEnum; |
| 8426 | InstantiatedFrom = PrevEnum->getInstantiatedFromMemberEnum(); |
| 8427 | MSInfo = PrevEnum->getMemberSpecializationInfo(); |
| 8428 | } |
| 8429 | } |
| 8430 | |
| 8431 | if (!Instantiation) { |
| 8432 | |
| 8433 | |
| 8434 | |
| 8435 | return false; |
| 8436 | } |
| 8437 | |
| 8438 | |
| 8439 | |
| 8440 | |
| 8441 | |
| 8442 | |
| 8443 | if (Member->getFriendObjectKind() != Decl::FOK_None) { |
| 8444 | |
| 8445 | if (InstantiatedFrom && isa<CXXMethodDecl>(Member)) { |
| 8446 | cast<CXXMethodDecl>(Member)->setInstantiationOfMemberFunction( |
| 8447 | cast<CXXMethodDecl>(InstantiatedFrom), |
| 8448 | cast<CXXMethodDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 8449 | } else if (InstantiatedFrom && isa<CXXRecordDecl>(Member)) { |
| 8450 | cast<CXXRecordDecl>(Member)->setInstantiationOfMemberClass( |
| 8451 | cast<CXXRecordDecl>(InstantiatedFrom), |
| 8452 | cast<CXXRecordDecl>(Instantiation)->getTemplateSpecializationKind()); |
| 8453 | } |
| 8454 | |
| 8455 | Previous.clear(); |
| 8456 | Previous.addDecl(FoundInstantiation); |
| 8457 | return false; |
| 8458 | } |
| 8459 | |
| 8460 | |
| 8461 | if (!InstantiatedFrom) { |
| 8462 | Diag(Member->getLocation(), diag::err_spec_member_not_instantiated) |
| 8463 | << Member; |
| 8464 | Diag(Instantiation->getLocation(), diag::note_specialized_decl); |
| 8465 | return true; |
| 8466 | } |
| 8467 | |
| 8468 | |
| 8469 | |
| 8470 | |
| 8471 | |
| 8472 | |
| 8473 | |
| 8474 | (0) . __assert_fail ("MSInfo && \"Member specialization info missing?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 8474, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(MSInfo && "Member specialization info missing?"); |
| 8475 | |
| 8476 | bool HasNoEffect = false; |
| 8477 | if (CheckSpecializationInstantiationRedecl(Member->getLocation(), |
| 8478 | TSK_ExplicitSpecialization, |
| 8479 | Instantiation, |
| 8480 | MSInfo->getTemplateSpecializationKind(), |
| 8481 | MSInfo->getPointOfInstantiation(), |
| 8482 | HasNoEffect)) |
| 8483 | return true; |
| 8484 | |
| 8485 | |
| 8486 | if (CheckTemplateSpecializationScope(*this, |
| 8487 | InstantiatedFrom, |
| 8488 | Instantiation, Member->getLocation(), |
| 8489 | false)) |
| 8490 | return true; |
| 8491 | |
| 8492 | |
| 8493 | |
| 8494 | if (auto *MemberFunction = dyn_cast<FunctionDecl>(Member)) { |
| 8495 | FunctionDecl *InstantiationFunction = cast<FunctionDecl>(Instantiation); |
| 8496 | if (InstantiationFunction->getTemplateSpecializationKind() == |
| 8497 | TSK_ImplicitInstantiation) { |
| 8498 | |
| 8499 | |
| 8500 | if (InstantiationFunction->isDeleted()) { |
| 8501 | |
| 8502 | getCanonicalDecl() == InstantiationFunction", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 8503, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(InstantiationFunction->getCanonicalDecl() == |
| 8503 | getCanonicalDecl() == InstantiationFunction", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 8503, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> InstantiationFunction); |
| 8504 | |
| 8505 | InstantiationFunction->setDeletedAsWritten(false); |
| 8506 | } |
| 8507 | } |
| 8508 | |
| 8509 | MemberFunction->setInstantiationOfMemberFunction( |
| 8510 | cast<CXXMethodDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
| 8511 | } else if (auto *MemberVar = dyn_cast<VarDecl>(Member)) { |
| 8512 | MemberVar->setInstantiationOfStaticDataMember( |
| 8513 | cast<VarDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
| 8514 | } else if (auto *MemberClass = dyn_cast<CXXRecordDecl>(Member)) { |
| 8515 | MemberClass->setInstantiationOfMemberClass( |
| 8516 | cast<CXXRecordDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
| 8517 | } else if (auto *MemberEnum = dyn_cast<EnumDecl>(Member)) { |
| 8518 | MemberEnum->setInstantiationOfMemberEnum( |
| 8519 | cast<EnumDecl>(InstantiatedFrom), TSK_ExplicitSpecialization); |
| 8520 | } else { |
| 8521 | llvm_unreachable("unknown member specialization kind"); |
| 8522 | } |
| 8523 | |
| 8524 | |
| 8525 | |
| 8526 | Previous.clear(); |
| 8527 | Previous.addDecl(FoundInstantiation); |
| 8528 | return false; |
| 8529 | } |
| 8530 | |
| 8531 | |
| 8532 | |
| 8533 | |
| 8534 | |
| 8535 | |
| 8536 | template<typename DeclT> |
| 8537 | static void completeMemberSpecializationImpl(Sema &S, DeclT *OrigD, |
| 8538 | SourceLocation Loc) { |
| 8539 | if (OrigD->getTemplateSpecializationKind() != TSK_ImplicitInstantiation) |
| 8540 | return; |
| 8541 | |
| 8542 | |
| 8543 | |
| 8544 | |
| 8545 | |
| 8546 | OrigD->setTemplateSpecializationKind(TSK_ExplicitSpecialization); |
| 8547 | OrigD->setLocation(Loc); |
| 8548 | } |
| 8549 | |
| 8550 | void Sema::CompleteMemberSpecialization(NamedDecl *Member, |
| 8551 | LookupResult &Previous) { |
| 8552 | NamedDecl *Instantiation = cast<NamedDecl>(Member->getCanonicalDecl()); |
| 8553 | if (Instantiation == Member) |
| 8554 | return; |
| 8555 | |
| 8556 | if (auto *Function = dyn_cast<CXXMethodDecl>(Instantiation)) |
| 8557 | completeMemberSpecializationImpl(*this, Function, Member->getLocation()); |
| 8558 | else if (auto *Var = dyn_cast<VarDecl>(Instantiation)) |
| 8559 | completeMemberSpecializationImpl(*this, Var, Member->getLocation()); |
| 8560 | else if (auto *Record = dyn_cast<CXXRecordDecl>(Instantiation)) |
| 8561 | completeMemberSpecializationImpl(*this, Record, Member->getLocation()); |
| 8562 | else if (auto *Enum = dyn_cast<EnumDecl>(Instantiation)) |
| 8563 | completeMemberSpecializationImpl(*this, Enum, Member->getLocation()); |
| 8564 | else |
| 8565 | llvm_unreachable("unknown member specialization kind"); |
| 8566 | } |
| 8567 | |
| 8568 | |
| 8569 | |
| 8570 | |
| 8571 | static bool CheckExplicitInstantiationScope(Sema &S, NamedDecl *D, |
| 8572 | SourceLocation InstLoc, |
| 8573 | bool WasQualifiedName) { |
| 8574 | DeclContext *OrigContext= D->getDeclContext()->getEnclosingNamespaceContext(); |
| 8575 | DeclContext *CurContext = S.CurContext->getRedeclContext(); |
| 8576 | |
| 8577 | if (CurContext->isRecord()) { |
| 8578 | S.Diag(InstLoc, diag::err_explicit_instantiation_in_class) |
| 8579 | << D; |
| 8580 | return true; |
| 8581 | } |
| 8582 | |
| 8583 | |
| 8584 | |
| 8585 | |
| 8586 | |
| 8587 | |
| 8588 | |
| 8589 | |
| 8590 | |
| 8591 | if (WasQualifiedName) { |
| 8592 | if (CurContext->Encloses(OrigContext)) |
| 8593 | return false; |
| 8594 | } else { |
| 8595 | if (CurContext->InEnclosingNamespaceSetOf(OrigContext)) |
| 8596 | return false; |
| 8597 | } |
| 8598 | |
| 8599 | if (NamespaceDecl *NS = dyn_cast<NamespaceDecl>(OrigContext)) { |
| 8600 | if (WasQualifiedName) |
| 8601 | S.Diag(InstLoc, |
| 8602 | S.getLangOpts().CPlusPlus11? |
| 8603 | diag::err_explicit_instantiation_out_of_scope : |
| 8604 | diag::warn_explicit_instantiation_out_of_scope_0x) |
| 8605 | << D << NS; |
| 8606 | else |
| 8607 | S.Diag(InstLoc, |
| 8608 | S.getLangOpts().CPlusPlus11? |
| 8609 | diag::err_explicit_instantiation_unqualified_wrong_namespace : |
| 8610 | diag::warn_explicit_instantiation_unqualified_wrong_namespace_0x) |
| 8611 | << D << NS; |
| 8612 | } else |
| 8613 | S.Diag(InstLoc, |
| 8614 | S.getLangOpts().CPlusPlus11? |
| 8615 | diag::err_explicit_instantiation_must_be_global : |
| 8616 | diag::warn_explicit_instantiation_must_be_global_0x) |
| 8617 | << D; |
| 8618 | S.Diag(D->getLocation(), diag::note_explicit_instantiation_here); |
| 8619 | return false; |
| 8620 | } |
| 8621 | |
| 8622 | |
| 8623 | static bool ScopeSpecifierHasTemplateId(const CXXScopeSpec &SS) { |
| 8624 | if (!SS.isSet()) |
| 8625 | return false; |
| 8626 | |
| 8627 | |
| 8628 | |
| 8629 | |
| 8630 | |
| 8631 | |
| 8632 | |
| 8633 | |
| 8634 | for (NestedNameSpecifier *NNS = SS.getScopeRep(); NNS; |
| 8635 | NNS = NNS->getPrefix()) |
| 8636 | if (const Type *T = NNS->getAsType()) |
| 8637 | if (isa<TemplateSpecializationType>(T)) |
| 8638 | return true; |
| 8639 | |
| 8640 | return false; |
| 8641 | } |
| 8642 | |
| 8643 | |
| 8644 | |
| 8645 | static void dllExportImportClassTemplateSpecialization( |
| 8646 | Sema &S, ClassTemplateSpecializationDecl *Def) { |
| 8647 | auto *A = cast_or_null<InheritableAttr>(getDLLAttr(Def)); |
| 8648 | (0) . __assert_fail ("A && \"dllExportImportClassTemplateSpecialization called \" \"on Def without dllexport or dllimport\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 8649, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(A && "dllExportImportClassTemplateSpecialization called " |
| 8649 | (0) . __assert_fail ("A && \"dllExportImportClassTemplateSpecialization called \" \"on Def without dllexport or dllimport\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 8649, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "on Def without dllexport or dllimport"); |
| 8650 | |
| 8651 | |
| 8652 | |
| 8653 | (0) . __assert_fail ("S.DelayedDllExportClasses.empty() && \"delayed exports present at explicit instantiation\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 8654, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(S.DelayedDllExportClasses.empty() && |
| 8654 | (0) . __assert_fail ("S.DelayedDllExportClasses.empty() && \"delayed exports present at explicit instantiation\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 8654, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "delayed exports present at explicit instantiation"); |
| 8655 | S.checkClassLevelDLLAttribute(Def); |
| 8656 | |
| 8657 | |
| 8658 | for (auto &B : Def->bases()) { |
| 8659 | if (auto *BT = dyn_cast_or_null<ClassTemplateSpecializationDecl>( |
| 8660 | B.getType()->getAsCXXRecordDecl())) |
| 8661 | S.propagateDLLAttrToBaseClassTemplate(Def, A, BT, B.getBeginLoc()); |
| 8662 | } |
| 8663 | |
| 8664 | S.referenceDLLExportedClassMethods(); |
| 8665 | } |
| 8666 | |
| 8667 | |
| 8668 | DeclResult Sema::ActOnExplicitInstantiation( |
| 8669 | Scope *S, SourceLocation ExternLoc, SourceLocation TemplateLoc, |
| 8670 | unsigned TagSpec, SourceLocation KWLoc, const CXXScopeSpec &SS, |
| 8671 | TemplateTy TemplateD, SourceLocation TemplateNameLoc, |
| 8672 | SourceLocation LAngleLoc, ASTTemplateArgsPtr TemplateArgsIn, |
| 8673 | SourceLocation RAngleLoc, const ParsedAttributesView &Attr) { |
| 8674 | |
| 8675 | TemplateName Name = TemplateD.get(); |
| 8676 | TemplateDecl *TD = Name.getAsTemplateDecl(); |
| 8677 | |
| 8678 | |
| 8679 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 8680 | (0) . __assert_fail ("Kind != TTK_Enum && \"Invalid enum tag in class template explicit instantiation!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 8681, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Kind != TTK_Enum && |
| 8681 | (0) . __assert_fail ("Kind != TTK_Enum && \"Invalid enum tag in class template explicit instantiation!\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 8681, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Invalid enum tag in class template explicit instantiation!"); |
| 8682 | |
| 8683 | ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(TD); |
| 8684 | |
| 8685 | if (!ClassTemplate) { |
| 8686 | NonTagKind NTK = getNonTagTypeDeclKind(TD, Kind); |
| 8687 | Diag(TemplateNameLoc, diag::err_tag_reference_non_tag) << TD << NTK << Kind; |
| 8688 | Diag(TD->getLocation(), diag::note_previous_use); |
| 8689 | return true; |
| 8690 | } |
| 8691 | |
| 8692 | if (!isAcceptableTagRedeclaration(ClassTemplate->getTemplatedDecl(), |
| 8693 | Kind, , KWLoc, |
| 8694 | ClassTemplate->getIdentifier())) { |
| 8695 | Diag(KWLoc, diag::err_use_with_wrong_tag) |
| 8696 | << ClassTemplate |
| 8697 | << FixItHint::CreateReplacement(KWLoc, |
| 8698 | ClassTemplate->getTemplatedDecl()->getKindName()); |
| 8699 | Diag(ClassTemplate->getTemplatedDecl()->getLocation(), |
| 8700 | diag::note_previous_use); |
| 8701 | Kind = ClassTemplate->getTemplatedDecl()->getTagKind(); |
| 8702 | } |
| 8703 | |
| 8704 | |
| 8705 | |
| 8706 | |
| 8707 | |
| 8708 | TemplateSpecializationKind TSK = ExternLoc.isInvalid() |
| 8709 | ? TSK_ExplicitInstantiationDefinition |
| 8710 | : TSK_ExplicitInstantiationDeclaration; |
| 8711 | |
| 8712 | if (TSK == TSK_ExplicitInstantiationDeclaration) { |
| 8713 | |
| 8714 | for (const ParsedAttr &AL : Attr) { |
| 8715 | if (AL.getKind() == ParsedAttr::AT_DLLExport) { |
| 8716 | Diag(ExternLoc, |
| 8717 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 8718 | Diag(AL.getLoc(), diag::note_attribute); |
| 8719 | break; |
| 8720 | } |
| 8721 | } |
| 8722 | |
| 8723 | if (auto *A = ClassTemplate->getTemplatedDecl()->getAttr<DLLExportAttr>()) { |
| 8724 | Diag(ExternLoc, |
| 8725 | diag::warn_attribute_dllexport_explicit_instantiation_decl); |
| 8726 | Diag(A->getLocation(), diag::note_attribute); |
| 8727 | } |
| 8728 | } |
| 8729 | |
| 8730 | |
| 8731 | |
| 8732 | bool DLLImportExplicitInstantiationDef = false; |
| 8733 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 8734 | Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 8735 | |
| 8736 | bool DLLImport = |
| 8737 | ClassTemplate->getTemplatedDecl()->getAttr<DLLImportAttr>(); |
| 8738 | for (const ParsedAttr &AL : Attr) { |
| 8739 | if (AL.getKind() == ParsedAttr::AT_DLLImport) |
| 8740 | DLLImport = true; |
| 8741 | if (AL.getKind() == ParsedAttr::AT_DLLExport) { |
| 8742 | |
| 8743 | DLLImport = false; |
| 8744 | break; |
| 8745 | } |
| 8746 | } |
| 8747 | if (DLLImport) { |
| 8748 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 8749 | DLLImportExplicitInstantiationDef = true; |
| 8750 | } |
| 8751 | } |
| 8752 | |
| 8753 | |
| 8754 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 8755 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 8756 | |
| 8757 | |
| 8758 | |
| 8759 | SmallVector<TemplateArgument, 4> Converted; |
| 8760 | if (CheckTemplateArgumentList(ClassTemplate, TemplateNameLoc, |
| 8761 | TemplateArgs, false, Converted)) |
| 8762 | return true; |
| 8763 | |
| 8764 | |
| 8765 | |
| 8766 | void *InsertPos = nullptr; |
| 8767 | ClassTemplateSpecializationDecl *PrevDecl |
| 8768 | = ClassTemplate->findSpecialization(Converted, InsertPos); |
| 8769 | |
| 8770 | TemplateSpecializationKind PrevDecl_TSK |
| 8771 | = PrevDecl ? PrevDecl->getTemplateSpecializationKind() : TSK_Undeclared; |
| 8772 | |
| 8773 | |
| 8774 | |
| 8775 | |
| 8776 | |
| 8777 | |
| 8778 | if (CheckExplicitInstantiationScope(*this, ClassTemplate, TemplateNameLoc, |
| 8779 | SS.isSet())) |
| 8780 | return true; |
| 8781 | |
| 8782 | ClassTemplateSpecializationDecl *Specialization = nullptr; |
| 8783 | |
| 8784 | bool HasNoEffect = false; |
| 8785 | if (PrevDecl) { |
| 8786 | if (CheckSpecializationInstantiationRedecl(TemplateNameLoc, TSK, |
| 8787 | PrevDecl, PrevDecl_TSK, |
| 8788 | PrevDecl->getPointOfInstantiation(), |
| 8789 | HasNoEffect)) |
| 8790 | return PrevDecl; |
| 8791 | |
| 8792 | |
| 8793 | |
| 8794 | |
| 8795 | if (PrevDecl_TSK == TSK_ImplicitInstantiation || |
| 8796 | PrevDecl_TSK == TSK_Undeclared) { |
| 8797 | |
| 8798 | |
| 8799 | |
| 8800 | |
| 8801 | |
| 8802 | Specialization = PrevDecl; |
| 8803 | Specialization->setLocation(TemplateNameLoc); |
| 8804 | PrevDecl = nullptr; |
| 8805 | } |
| 8806 | |
| 8807 | if (PrevDecl_TSK == TSK_ExplicitInstantiationDeclaration && |
| 8808 | DLLImportExplicitInstantiationDef) { |
| 8809 | |
| 8810 | HasNoEffect = false; |
| 8811 | } |
| 8812 | } |
| 8813 | |
| 8814 | if (!Specialization) { |
| 8815 | |
| 8816 | |
| 8817 | Specialization |
| 8818 | = ClassTemplateSpecializationDecl::Create(Context, Kind, |
| 8819 | ClassTemplate->getDeclContext(), |
| 8820 | KWLoc, TemplateNameLoc, |
| 8821 | ClassTemplate, |
| 8822 | Converted, |
| 8823 | PrevDecl); |
| 8824 | SetNestedNameSpecifier(*this, Specialization, SS); |
| 8825 | |
| 8826 | if (!HasNoEffect && !PrevDecl) { |
| 8827 | |
| 8828 | ClassTemplate->AddSpecialization(Specialization, InsertPos); |
| 8829 | } |
| 8830 | } |
| 8831 | |
| 8832 | |
| 8833 | |
| 8834 | |
| 8835 | |
| 8836 | |
| 8837 | |
| 8838 | |
| 8839 | TypeSourceInfo *WrittenTy |
| 8840 | = Context.getTemplateSpecializationTypeInfo(Name, TemplateNameLoc, |
| 8841 | TemplateArgs, |
| 8842 | Context.getTypeDeclType(Specialization)); |
| 8843 | Specialization->setTypeAsWritten(WrittenTy); |
| 8844 | |
| 8845 | |
| 8846 | Specialization->setExternLoc(ExternLoc); |
| 8847 | Specialization->setTemplateKeywordLoc(TemplateLoc); |
| 8848 | Specialization->setBraceRange(SourceRange()); |
| 8849 | |
| 8850 | bool PreviouslyDLLExported = Specialization->hasAttr<DLLExportAttr>(); |
| 8851 | ProcessDeclAttributeList(S, Specialization, Attr); |
| 8852 | |
| 8853 | |
| 8854 | |
| 8855 | |
| 8856 | Specialization->setLexicalDeclContext(CurContext); |
| 8857 | CurContext->addDecl(Specialization); |
| 8858 | |
| 8859 | |
| 8860 | if (HasNoEffect) { |
| 8861 | |
| 8862 | Specialization->setTemplateSpecializationKind(TSK); |
| 8863 | return Specialization; |
| 8864 | } |
| 8865 | |
| 8866 | |
| 8867 | |
| 8868 | |
| 8869 | |
| 8870 | |
| 8871 | |
| 8872 | |
| 8873 | ClassTemplateSpecializationDecl *Def |
| 8874 | = cast_or_null<ClassTemplateSpecializationDecl>( |
| 8875 | Specialization->getDefinition()); |
| 8876 | if (!Def) |
| 8877 | InstantiateClassTemplateSpecialization(TemplateNameLoc, Specialization, TSK); |
| 8878 | else if (TSK == TSK_ExplicitInstantiationDefinition) { |
| 8879 | MarkVTableUsed(TemplateNameLoc, Specialization, true); |
| 8880 | Specialization->setPointOfInstantiation(Def->getPointOfInstantiation()); |
| 8881 | } |
| 8882 | |
| 8883 | |
| 8884 | Def = cast_or_null<ClassTemplateSpecializationDecl>( |
| 8885 | Specialization->getDefinition()); |
| 8886 | if (Def) { |
| 8887 | TemplateSpecializationKind Old_TSK = Def->getTemplateSpecializationKind(); |
| 8888 | |
| 8889 | |
| 8890 | if (Old_TSK == TSK_ExplicitInstantiationDeclaration && |
| 8891 | (TSK == TSK_ExplicitInstantiationDefinition || |
| 8892 | DLLImportExplicitInstantiationDef)) { |
| 8893 | |
| 8894 | Def->setTemplateSpecializationKind(TSK); |
| 8895 | |
| 8896 | if (!getDLLAttr(Def) && getDLLAttr(Specialization) && |
| 8897 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 8898 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { |
| 8899 | |
| 8900 | |
| 8901 | |
| 8902 | auto *A = cast<InheritableAttr>( |
| 8903 | getDLLAttr(Specialization)->clone(getASTContext())); |
| 8904 | A->setInherited(true); |
| 8905 | Def->addAttr(A); |
| 8906 | dllExportImportClassTemplateSpecialization(*this, Def); |
| 8907 | } |
| 8908 | } |
| 8909 | |
| 8910 | |
| 8911 | |
| 8912 | bool NewlyDLLExported = |
| 8913 | !PreviouslyDLLExported && Specialization->hasAttr<DLLExportAttr>(); |
| 8914 | if (Old_TSK == TSK_ImplicitInstantiation && NewlyDLLExported && |
| 8915 | (Context.getTargetInfo().getCXXABI().isMicrosoft() || |
| 8916 | Context.getTargetInfo().getTriple().isWindowsItaniumEnvironment())) { |
| 8917 | |
| 8918 | |
| 8919 | |
| 8920 | |
| 8921 | |
| 8922 | |
| 8923 | |
| 8924 | |
| 8925 | |
| 8926 | |
| 8927 | |
| 8928 | |
| 8929 | (0) . __assert_fail ("Def == Specialization && \"Def and Specialization should match for implicit instantiation\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 8930, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Def == Specialization && |
| 8930 | (0) . __assert_fail ("Def == Specialization && \"Def and Specialization should match for implicit instantiation\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 8930, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Def and Specialization should match for implicit instantiation"); |
| 8931 | dllExportImportClassTemplateSpecialization(*this, Def); |
| 8932 | } |
| 8933 | |
| 8934 | |
| 8935 | |
| 8936 | Specialization->setTemplateSpecializationKind(TSK); |
| 8937 | InstantiateClassTemplateSpecializationMembers(TemplateNameLoc, Def, TSK); |
| 8938 | } else { |
| 8939 | |
| 8940 | |
| 8941 | Specialization->setTemplateSpecializationKind(TSK); |
| 8942 | } |
| 8943 | |
| 8944 | return Specialization; |
| 8945 | } |
| 8946 | |
| 8947 | |
| 8948 | DeclResult |
| 8949 | Sema::ActOnExplicitInstantiation(Scope *S, SourceLocation ExternLoc, |
| 8950 | SourceLocation TemplateLoc, unsigned TagSpec, |
| 8951 | SourceLocation KWLoc, CXXScopeSpec &SS, |
| 8952 | IdentifierInfo *Name, SourceLocation NameLoc, |
| 8953 | const ParsedAttributesView &Attr) { |
| 8954 | |
| 8955 | bool Owned = false; |
| 8956 | bool IsDependent = false; |
| 8957 | Decl *TagD = ActOnTag(S, TagSpec, Sema::TUK_Reference, |
| 8958 | KWLoc, SS, Name, NameLoc, Attr, AS_none, |
| 8959 | (), |
| 8960 | MultiTemplateParamsArg(), Owned, IsDependent, |
| 8961 | SourceLocation(), false, TypeResult(), |
| 8962 | , |
| 8963 | ); |
| 8964 | (0) . __assert_fail ("!IsDependent && \"explicit instantiation of dependent name not yet handled\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 8964, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!IsDependent && "explicit instantiation of dependent name not yet handled"); |
| 8965 | |
| 8966 | if (!TagD) |
| 8967 | return true; |
| 8968 | |
| 8969 | TagDecl *Tag = cast<TagDecl>(TagD); |
| 8970 | (0) . __assert_fail ("!Tag->isEnum() && \"shouldn't see enumerations here\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 8970, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Tag->isEnum() && "shouldn't see enumerations here"); |
| 8971 | |
| 8972 | if (Tag->isInvalidDecl()) |
| 8973 | return true; |
| 8974 | |
| 8975 | CXXRecordDecl *Record = cast<CXXRecordDecl>(Tag); |
| 8976 | CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); |
| 8977 | if (!Pattern) { |
| 8978 | Diag(TemplateLoc, diag::err_explicit_instantiation_nontemplate_type) |
| 8979 | << Context.getTypeDeclType(Record); |
| 8980 | Diag(Record->getLocation(), diag::note_nontemplate_decl_here); |
| 8981 | return true; |
| 8982 | } |
| 8983 | |
| 8984 | |
| 8985 | |
| 8986 | |
| 8987 | |
| 8988 | |
| 8989 | |
| 8990 | if (!ScopeSpecifierHasTemplateId(SS)) |
| 8991 | Diag(TemplateLoc, diag::ext_explicit_instantiation_without_qualified_id) |
| 8992 | << Record << SS.getRange(); |
| 8993 | |
| 8994 | |
| 8995 | |
| 8996 | |
| 8997 | |
| 8998 | TemplateSpecializationKind TSK |
| 8999 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 9000 | : TSK_ExplicitInstantiationDeclaration; |
| 9001 | |
| 9002 | |
| 9003 | |
| 9004 | |
| 9005 | |
| 9006 | |
| 9007 | CheckExplicitInstantiationScope(*this, Record, NameLoc, true); |
| 9008 | |
| 9009 | |
| 9010 | CXXRecordDecl *PrevDecl |
| 9011 | = cast_or_null<CXXRecordDecl>(Record->getPreviousDecl()); |
| 9012 | if (!PrevDecl && Record->getDefinition()) |
| 9013 | PrevDecl = Record; |
| 9014 | if (PrevDecl) { |
| 9015 | MemberSpecializationInfo *MSInfo = PrevDecl->getMemberSpecializationInfo(); |
| 9016 | bool HasNoEffect = false; |
| 9017 | (0) . __assert_fail ("MSInfo && \"No member specialization information?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 9017, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(MSInfo && "No member specialization information?"); |
| 9018 | if (CheckSpecializationInstantiationRedecl(TemplateLoc, TSK, |
| 9019 | PrevDecl, |
| 9020 | MSInfo->getTemplateSpecializationKind(), |
| 9021 | MSInfo->getPointOfInstantiation(), |
| 9022 | HasNoEffect)) |
| 9023 | return true; |
| 9024 | if (HasNoEffect) |
| 9025 | return TagD; |
| 9026 | } |
| 9027 | |
| 9028 | CXXRecordDecl *RecordDef |
| 9029 | = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
| 9030 | if (!RecordDef) { |
| 9031 | |
| 9032 | |
| 9033 | |
| 9034 | CXXRecordDecl *Def |
| 9035 | = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); |
| 9036 | if (!Def) { |
| 9037 | Diag(TemplateLoc, diag::err_explicit_instantiation_undefined_member) |
| 9038 | << 0 << Record->getDeclName() << Record->getDeclContext(); |
| 9039 | Diag(Pattern->getLocation(), diag::note_forward_declaration) |
| 9040 | << Pattern; |
| 9041 | return true; |
| 9042 | } else { |
| 9043 | if (InstantiateClass(NameLoc, Record, Def, |
| 9044 | getTemplateInstantiationArgs(Record), |
| 9045 | TSK)) |
| 9046 | return true; |
| 9047 | |
| 9048 | RecordDef = cast_or_null<CXXRecordDecl>(Record->getDefinition()); |
| 9049 | if (!RecordDef) |
| 9050 | return true; |
| 9051 | } |
| 9052 | } |
| 9053 | |
| 9054 | |
| 9055 | InstantiateClassMembers(NameLoc, RecordDef, |
| 9056 | getTemplateInstantiationArgs(Record), TSK); |
| 9057 | |
| 9058 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 9059 | MarkVTableUsed(NameLoc, RecordDef, true); |
| 9060 | |
| 9061 | |
| 9062 | |
| 9063 | |
| 9064 | |
| 9065 | return TagD; |
| 9066 | } |
| 9067 | |
| 9068 | DeclResult Sema::ActOnExplicitInstantiation(Scope *S, |
| 9069 | SourceLocation ExternLoc, |
| 9070 | SourceLocation TemplateLoc, |
| 9071 | Declarator &D) { |
| 9072 | |
| 9073 | |
| 9074 | DeclarationNameInfo NameInfo = GetNameForDeclarator(D); |
| 9075 | DeclarationName Name = NameInfo.getName(); |
| 9076 | if (!Name) { |
| 9077 | if (!D.isInvalidType()) |
| 9078 | Diag(D.getDeclSpec().getBeginLoc(), |
| 9079 | diag::err_explicit_instantiation_requires_name) |
| 9080 | << D.getDeclSpec().getSourceRange() << D.getSourceRange(); |
| 9081 | |
| 9082 | return true; |
| 9083 | } |
| 9084 | |
| 9085 | |
| 9086 | |
| 9087 | while ((S->getFlags() & Scope::DeclScope) == 0 || |
| 9088 | (S->getFlags() & Scope::TemplateParamScope) != 0) |
| 9089 | S = S->getParent(); |
| 9090 | |
| 9091 | |
| 9092 | TypeSourceInfo *T = GetTypeForDeclarator(D, S); |
| 9093 | QualType R = T->getType(); |
| 9094 | if (R.isNull()) |
| 9095 | return true; |
| 9096 | |
| 9097 | |
| 9098 | |
| 9099 | |
| 9100 | if (D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 9101 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_of_typedef) |
| 9102 | << Name; |
| 9103 | return true; |
| 9104 | } else if (D.getDeclSpec().getStorageClassSpec() |
| 9105 | != DeclSpec::SCS_unspecified) { |
| 9106 | |
| 9107 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_storage_class) |
| 9108 | << FixItHint::CreateRemoval(D.getDeclSpec().getStorageClassSpecLoc()); |
| 9109 | |
| 9110 | D.getMutableDeclSpec().ClearStorageClassSpecs(); |
| 9111 | } |
| 9112 | |
| 9113 | |
| 9114 | |
| 9115 | |
| 9116 | |
| 9117 | |
| 9118 | if (D.getDeclSpec().isInlineSpecified()) |
| 9119 | Diag(D.getDeclSpec().getInlineSpecLoc(), |
| 9120 | getLangOpts().CPlusPlus11 ? |
| 9121 | diag::err_explicit_instantiation_inline : |
| 9122 | diag::warn_explicit_instantiation_inline_0x) |
| 9123 | << FixItHint::CreateRemoval(D.getDeclSpec().getInlineSpecLoc()); |
| 9124 | if (D.getDeclSpec().isConstexprSpecified() && R->isFunctionType()) |
| 9125 | |
| 9126 | |
| 9127 | Diag(D.getDeclSpec().getConstexprSpecLoc(), |
| 9128 | diag::err_explicit_instantiation_constexpr); |
| 9129 | |
| 9130 | |
| 9131 | |
| 9132 | if (Name.getNameKind() == DeclarationName::CXXDeductionGuideName) { |
| 9133 | Diag(D.getDeclSpec().getBeginLoc(), diag::err_deduction_guide_specialized) |
| 9134 | << 0; |
| 9135 | return true; |
| 9136 | } |
| 9137 | |
| 9138 | |
| 9139 | |
| 9140 | |
| 9141 | |
| 9142 | TemplateSpecializationKind TSK |
| 9143 | = ExternLoc.isInvalid()? TSK_ExplicitInstantiationDefinition |
| 9144 | : TSK_ExplicitInstantiationDeclaration; |
| 9145 | |
| 9146 | LookupResult Previous(*this, NameInfo, LookupOrdinaryName); |
| 9147 | LookupParsedName(Previous, S, &D.getCXXScopeSpec()); |
| 9148 | |
| 9149 | if (!R->isFunctionType()) { |
| 9150 | |
| 9151 | |
| 9152 | |
| 9153 | |
| 9154 | |
| 9155 | |
| 9156 | |
| 9157 | if (Previous.isAmbiguous()) |
| 9158 | return true; |
| 9159 | |
| 9160 | VarDecl *Prev = Previous.getAsSingle<VarDecl>(); |
| 9161 | VarTemplateDecl *PrevTemplate = Previous.getAsSingle<VarTemplateDecl>(); |
| 9162 | |
| 9163 | if (!PrevTemplate) { |
| 9164 | if (!Prev || !Prev->isStaticDataMember()) { |
| 9165 | |
| 9166 | Diag(D.getIdentifierLoc(), diag::err_explicit_instantiation_not_known) |
| 9167 | << Name; |
| 9168 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 9169 | P != PEnd; ++P) |
| 9170 | Diag((*P)->getLocation(), diag::note_explicit_instantiation_here); |
| 9171 | return true; |
| 9172 | } |
| 9173 | |
| 9174 | if (!Prev->getInstantiatedFromStaticDataMember()) { |
| 9175 | |
| 9176 | Diag(D.getIdentifierLoc(), |
| 9177 | diag::err_explicit_instantiation_data_member_not_instantiated) |
| 9178 | << Prev; |
| 9179 | Diag(Prev->getLocation(), diag::note_explicit_instantiation_here); |
| 9180 | |
| 9181 | return true; |
| 9182 | } |
| 9183 | } else { |
| 9184 | |
| 9185 | |
| 9186 | |
| 9187 | |
| 9188 | |
| 9189 | |
| 9190 | |
| 9191 | if (R->isUndeducedType()) { |
| 9192 | Diag(T->getTypeLoc().getBeginLoc(), |
| 9193 | diag::err_auto_not_allowed_var_inst); |
| 9194 | return true; |
| 9195 | } |
| 9196 | |
| 9197 | if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) { |
| 9198 | |
| 9199 | |
| 9200 | |
| 9201 | Diag(D.getIdentifierLoc(), |
| 9202 | diag::err_explicit_instantiation_without_template_id) |
| 9203 | << PrevTemplate; |
| 9204 | Diag(PrevTemplate->getLocation(), |
| 9205 | diag::note_explicit_instantiation_here); |
| 9206 | return true; |
| 9207 | } |
| 9208 | |
| 9209 | |
| 9210 | TemplateArgumentListInfo TemplateArgs = |
| 9211 | makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
| 9212 | |
| 9213 | DeclResult Res = CheckVarTemplateId(PrevTemplate, TemplateLoc, |
| 9214 | D.getIdentifierLoc(), TemplateArgs); |
| 9215 | if (Res.isInvalid()) |
| 9216 | return true; |
| 9217 | |
| 9218 | |
| 9219 | |
| 9220 | Prev = cast<VarDecl>(Res.get()); |
| 9221 | } |
| 9222 | |
| 9223 | |
| 9224 | |
| 9225 | |
| 9226 | |
| 9227 | |
| 9228 | |
| 9229 | |
| 9230 | |
| 9231 | |
| 9232 | |
| 9233 | if (!ScopeSpecifierHasTemplateId(D.getCXXScopeSpec()) && !PrevTemplate) |
| 9234 | Diag(D.getIdentifierLoc(), |
| 9235 | diag::ext_explicit_instantiation_without_qualified_id) |
| 9236 | << Prev << D.getCXXScopeSpec().getRange(); |
| 9237 | |
| 9238 | |
| 9239 | CheckExplicitInstantiationScope(*this, Prev, D.getIdentifierLoc(), true); |
| 9240 | |
| 9241 | |
| 9242 | TemplateSpecializationKind PrevTSK = Prev->getTemplateSpecializationKind(); |
| 9243 | SourceLocation POI = Prev->getPointOfInstantiation(); |
| 9244 | bool HasNoEffect = false; |
| 9245 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, Prev, |
| 9246 | PrevTSK, POI, HasNoEffect)) |
| 9247 | return true; |
| 9248 | |
| 9249 | if (!HasNoEffect) { |
| 9250 | |
| 9251 | Prev->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 9252 | |
| 9253 | ProcessDeclAttributeList(S, Prev, D.getDeclSpec().getAttributes()); |
| 9254 | if (TSK == TSK_ExplicitInstantiationDefinition) |
| 9255 | InstantiateVariableDefinition(D.getIdentifierLoc(), Prev); |
| 9256 | } |
| 9257 | |
| 9258 | |
| 9259 | if (PrevTemplate && Prev && !Context.hasSameType(Prev->getType(), R)) { |
| 9260 | Diag(T->getTypeLoc().getBeginLoc(), |
| 9261 | diag::err_invalid_var_template_spec_type) |
| 9262 | << 0 << PrevTemplate << R << Prev->getType(); |
| 9263 | Diag(PrevTemplate->getLocation(), diag::note_template_declared_here) |
| 9264 | << 2 << PrevTemplate->getDeclName(); |
| 9265 | return true; |
| 9266 | } |
| 9267 | |
| 9268 | |
| 9269 | return (Decl*) nullptr; |
| 9270 | } |
| 9271 | |
| 9272 | |
| 9273 | |
| 9274 | bool HasExplicitTemplateArgs = false; |
| 9275 | TemplateArgumentListInfo TemplateArgs; |
| 9276 | if (D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId) { |
| 9277 | TemplateArgs = makeTemplateArgumentListInfo(*this, *D.getName().TemplateId); |
| 9278 | HasExplicitTemplateArgs = true; |
| 9279 | } |
| 9280 | |
| 9281 | |
| 9282 | |
| 9283 | |
| 9284 | |
| 9285 | |
| 9286 | UnresolvedSet<8> TemplateMatches; |
| 9287 | FunctionDecl *NonTemplateMatch = nullptr; |
| 9288 | TemplateSpecCandidateSet FailedCandidates(D.getIdentifierLoc()); |
| 9289 | for (LookupResult::iterator P = Previous.begin(), PEnd = Previous.end(); |
| 9290 | P != PEnd; ++P) { |
| 9291 | NamedDecl *Prev = *P; |
| 9292 | if (!HasExplicitTemplateArgs) { |
| 9293 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Prev)) { |
| 9294 | QualType Adjusted = adjustCCAndNoReturn(R, Method->getType(), |
| 9295 | ); |
| 9296 | if (Context.hasSameUnqualifiedType(Method->getType(), Adjusted)) { |
| 9297 | if (Method->getPrimaryTemplate()) { |
| 9298 | TemplateMatches.addDecl(Method, P.getAccess()); |
| 9299 | } else { |
| 9300 | |
| 9301 | (0) . __assert_fail ("!NonTemplateMatch && \"Multiple NonTemplateMatches\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 9301, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!NonTemplateMatch && "Multiple NonTemplateMatches"); |
| 9302 | NonTemplateMatch = Method; |
| 9303 | } |
| 9304 | } |
| 9305 | } |
| 9306 | } |
| 9307 | |
| 9308 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(Prev); |
| 9309 | if (!FunTmpl) |
| 9310 | continue; |
| 9311 | |
| 9312 | TemplateDeductionInfo Info(FailedCandidates.getLocation()); |
| 9313 | FunctionDecl *Specialization = nullptr; |
| 9314 | if (TemplateDeductionResult TDK |
| 9315 | = DeduceTemplateArguments(FunTmpl, |
| 9316 | (HasExplicitTemplateArgs ? &TemplateArgs |
| 9317 | : nullptr), |
| 9318 | R, Specialization, Info)) { |
| 9319 | |
| 9320 | FailedCandidates.addCandidate() |
| 9321 | .set(P.getPair(), FunTmpl->getTemplatedDecl(), |
| 9322 | MakeDeductionFailureInfo(Context, TDK, Info)); |
| 9323 | (void)TDK; |
| 9324 | continue; |
| 9325 | } |
| 9326 | |
| 9327 | |
| 9328 | |
| 9329 | |
| 9330 | |
| 9331 | |
| 9332 | if (LangOpts.CUDA && |
| 9333 | IdentifyCUDATarget(Specialization, |
| 9334 | true) != |
| 9335 | IdentifyCUDATarget(D.getDeclSpec().getAttributes())) { |
| 9336 | FailedCandidates.addCandidate().set( |
| 9337 | P.getPair(), FunTmpl->getTemplatedDecl(), |
| 9338 | MakeDeductionFailureInfo(Context, TDK_CUDATargetMismatch, Info)); |
| 9339 | continue; |
| 9340 | } |
| 9341 | |
| 9342 | TemplateMatches.addDecl(Specialization, P.getAccess()); |
| 9343 | } |
| 9344 | |
| 9345 | FunctionDecl *Specialization = NonTemplateMatch; |
| 9346 | if (!Specialization) { |
| 9347 | |
| 9348 | UnresolvedSetIterator Result = getMostSpecialized( |
| 9349 | TemplateMatches.begin(), TemplateMatches.end(), FailedCandidates, |
| 9350 | D.getIdentifierLoc(), |
| 9351 | PDiag(diag::err_explicit_instantiation_not_known) << Name, |
| 9352 | PDiag(diag::err_explicit_instantiation_ambiguous) << Name, |
| 9353 | PDiag(diag::note_explicit_instantiation_candidate)); |
| 9354 | |
| 9355 | if (Result == TemplateMatches.end()) |
| 9356 | return true; |
| 9357 | |
| 9358 | |
| 9359 | Specialization = cast<FunctionDecl>(*Result); |
| 9360 | } |
| 9361 | |
| 9362 | |
| 9363 | |
| 9364 | |
| 9365 | |
| 9366 | |
| 9367 | |
| 9368 | if (auto *FPT = R->getAs<FunctionProtoType>()) |
| 9369 | if (FPT->hasExceptionSpec()) { |
| 9370 | unsigned DiagID = |
| 9371 | diag::err_mismatched_exception_spec_explicit_instantiation; |
| 9372 | if (getLangOpts().MicrosoftExt) |
| 9373 | DiagID = diag::ext_mismatched_exception_spec_explicit_instantiation; |
| 9374 | bool Result = CheckEquivalentExceptionSpec( |
| 9375 | PDiag(DiagID) << Specialization->getType(), |
| 9376 | PDiag(diag::note_explicit_instantiation_here), |
| 9377 | Specialization->getType()->getAs<FunctionProtoType>(), |
| 9378 | Specialization->getLocation(), FPT, D.getBeginLoc()); |
| 9379 | |
| 9380 | |
| 9381 | if (!getLangOpts().MicrosoftExt && Result) |
| 9382 | return true; |
| 9383 | } |
| 9384 | |
| 9385 | if (Specialization->getTemplateSpecializationKind() == TSK_Undeclared) { |
| 9386 | Diag(D.getIdentifierLoc(), |
| 9387 | diag::err_explicit_instantiation_member_function_not_instantiated) |
| 9388 | << Specialization |
| 9389 | << (Specialization->getTemplateSpecializationKind() == |
| 9390 | TSK_ExplicitSpecialization); |
| 9391 | Diag(Specialization->getLocation(), diag::note_explicit_instantiation_here); |
| 9392 | return true; |
| 9393 | } |
| 9394 | |
| 9395 | FunctionDecl *PrevDecl = Specialization->getPreviousDecl(); |
| 9396 | if (!PrevDecl && Specialization->isThisDeclarationADefinition()) |
| 9397 | PrevDecl = Specialization; |
| 9398 | |
| 9399 | if (PrevDecl) { |
| 9400 | bool HasNoEffect = false; |
| 9401 | if (CheckSpecializationInstantiationRedecl(D.getIdentifierLoc(), TSK, |
| 9402 | PrevDecl, |
| 9403 | PrevDecl->getTemplateSpecializationKind(), |
| 9404 | PrevDecl->getPointOfInstantiation(), |
| 9405 | HasNoEffect)) |
| 9406 | return true; |
| 9407 | |
| 9408 | |
| 9409 | |
| 9410 | if (HasNoEffect) |
| 9411 | return (Decl*) nullptr; |
| 9412 | } |
| 9413 | |
| 9414 | ProcessDeclAttributeList(S, Specialization, D.getDeclSpec().getAttributes()); |
| 9415 | |
| 9416 | |
| 9417 | |
| 9418 | if (TSK == TSK_ExplicitInstantiationDefinition && |
| 9419 | Specialization->hasAttr<DLLImportAttr>() && |
| 9420 | Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| 9421 | TSK = TSK_ExplicitInstantiationDeclaration; |
| 9422 | |
| 9423 | Specialization->setTemplateSpecializationKind(TSK, D.getIdentifierLoc()); |
| 9424 | |
| 9425 | if (Specialization->isDefined()) { |
| 9426 | |
| 9427 | |
| 9428 | Consumer.HandleTopLevelDecl(DeclGroupRef(Specialization)); |
| 9429 | } else if (TSK == TSK_ExplicitInstantiationDefinition) |
| 9430 | InstantiateFunctionDefinition(D.getIdentifierLoc(), Specialization); |
| 9431 | |
| 9432 | |
| 9433 | |
| 9434 | |
| 9435 | |
| 9436 | |
| 9437 | |
| 9438 | |
| 9439 | FunctionTemplateDecl *FunTmpl = Specialization->getPrimaryTemplate(); |
| 9440 | if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId && !FunTmpl && |
| 9441 | D.getCXXScopeSpec().isSet() && |
| 9442 | !ScopeSpecifierHasTemplateId(D.getCXXScopeSpec())) |
| 9443 | Diag(D.getIdentifierLoc(), |
| 9444 | diag::ext_explicit_instantiation_without_qualified_id) |
| 9445 | << Specialization << D.getCXXScopeSpec().getRange(); |
| 9446 | |
| 9447 | CheckExplicitInstantiationScope(*this, |
| 9448 | FunTmpl? (NamedDecl *)FunTmpl |
| 9449 | : Specialization->getInstantiatedFromMemberFunction(), |
| 9450 | D.getIdentifierLoc(), |
| 9451 | D.getCXXScopeSpec().isSet()); |
| 9452 | |
| 9453 | |
| 9454 | return (Decl*) nullptr; |
| 9455 | } |
| 9456 | |
| 9457 | TypeResult |
| 9458 | Sema::ActOnDependentTag(Scope *S, unsigned TagSpec, TagUseKind TUK, |
| 9459 | const CXXScopeSpec &SS, IdentifierInfo *Name, |
| 9460 | SourceLocation TagLoc, SourceLocation NameLoc) { |
| 9461 | |
| 9462 | (0) . __assert_fail ("Name && \"Expected a name in a dependent tag\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 9462, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Name && "Expected a name in a dependent tag"); |
| 9463 | |
| 9464 | NestedNameSpecifier *NNS = SS.getScopeRep(); |
| 9465 | if (!NNS) |
| 9466 | return true; |
| 9467 | |
| 9468 | TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForTypeSpec(TagSpec); |
| 9469 | |
| 9470 | if (TUK == TUK_Declaration || TUK == TUK_Definition) { |
| 9471 | Diag(NameLoc, diag::err_dependent_tag_decl) |
| 9472 | << (TUK == TUK_Definition) << Kind << SS.getRange(); |
| 9473 | return true; |
| 9474 | } |
| 9475 | |
| 9476 | |
| 9477 | ElaboratedTypeKeyword Kwd = TypeWithKeyword::getKeywordForTagTypeKind(Kind); |
| 9478 | QualType Result = Context.getDependentNameType(Kwd, NNS, Name); |
| 9479 | |
| 9480 | |
| 9481 | TypeLocBuilder TLB; |
| 9482 | DependentNameTypeLoc TL = TLB.push<DependentNameTypeLoc>(Result); |
| 9483 | TL.setElaboratedKeywordLoc(TagLoc); |
| 9484 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 9485 | TL.setNameLoc(NameLoc); |
| 9486 | return CreateParsedType(Result, TLB.getTypeSourceInfo(Context, Result)); |
| 9487 | } |
| 9488 | |
| 9489 | TypeResult |
| 9490 | Sema::ActOnTypenameType(Scope *S, SourceLocation TypenameLoc, |
| 9491 | const CXXScopeSpec &SS, const IdentifierInfo &II, |
| 9492 | SourceLocation IdLoc) { |
| 9493 | if (SS.isInvalid()) |
| 9494 | return true; |
| 9495 | |
| 9496 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 9497 | Diag(TypenameLoc, |
| 9498 | getLangOpts().CPlusPlus11 ? |
| 9499 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 9500 | diag::ext_typename_outside_of_template) |
| 9501 | << FixItHint::CreateRemoval(TypenameLoc); |
| 9502 | |
| 9503 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
| 9504 | QualType T = CheckTypenameType(TypenameLoc.isValid()? ETK_Typename : ETK_None, |
| 9505 | TypenameLoc, QualifierLoc, II, IdLoc); |
| 9506 | if (T.isNull()) |
| 9507 | return true; |
| 9508 | |
| 9509 | TypeSourceInfo *TSI = Context.CreateTypeSourceInfo(T); |
| 9510 | if (isa<DependentNameType>(T)) { |
| 9511 | DependentNameTypeLoc TL = TSI->getTypeLoc().castAs<DependentNameTypeLoc>(); |
| 9512 | TL.setElaboratedKeywordLoc(TypenameLoc); |
| 9513 | TL.setQualifierLoc(QualifierLoc); |
| 9514 | TL.setNameLoc(IdLoc); |
| 9515 | } else { |
| 9516 | ElaboratedTypeLoc TL = TSI->getTypeLoc().castAs<ElaboratedTypeLoc>(); |
| 9517 | TL.setElaboratedKeywordLoc(TypenameLoc); |
| 9518 | TL.setQualifierLoc(QualifierLoc); |
| 9519 | TL.getNamedTypeLoc().castAs<TypeSpecTypeLoc>().setNameLoc(IdLoc); |
| 9520 | } |
| 9521 | |
| 9522 | return CreateParsedType(T, TSI); |
| 9523 | } |
| 9524 | |
| 9525 | TypeResult |
| 9526 | Sema::ActOnTypenameType(Scope *S, |
| 9527 | SourceLocation TypenameLoc, |
| 9528 | const CXXScopeSpec &SS, |
| 9529 | SourceLocation TemplateKWLoc, |
| 9530 | TemplateTy TemplateIn, |
| 9531 | IdentifierInfo *TemplateII, |
| 9532 | SourceLocation TemplateIILoc, |
| 9533 | SourceLocation LAngleLoc, |
| 9534 | ASTTemplateArgsPtr TemplateArgsIn, |
| 9535 | SourceLocation RAngleLoc) { |
| 9536 | if (TypenameLoc.isValid() && S && !S->getTemplateParamParent()) |
| 9537 | Diag(TypenameLoc, |
| 9538 | getLangOpts().CPlusPlus11 ? |
| 9539 | diag::warn_cxx98_compat_typename_outside_of_template : |
| 9540 | diag::ext_typename_outside_of_template) |
| 9541 | << FixItHint::CreateRemoval(TypenameLoc); |
| 9542 | |
| 9543 | |
| 9544 | |
| 9545 | if (TypenameLoc.isValid()) { |
| 9546 | auto *LookupRD = |
| 9547 | dyn_cast_or_null<CXXRecordDecl>(computeDeclContext(SS, false)); |
| 9548 | if (LookupRD && LookupRD->getIdentifier() == TemplateII) { |
| 9549 | Diag(TemplateIILoc, |
| 9550 | diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 9551 | << TemplateII << 0 |
| 9552 | << (TemplateKWLoc.isValid() ? 1 : 0 ); |
| 9553 | } |
| 9554 | } |
| 9555 | |
| 9556 | |
| 9557 | TemplateArgumentListInfo TemplateArgs(LAngleLoc, RAngleLoc); |
| 9558 | translateTemplateArguments(TemplateArgsIn, TemplateArgs); |
| 9559 | |
| 9560 | TemplateName Template = TemplateIn.get(); |
| 9561 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) { |
| 9562 | |
| 9563 | (0) . __assert_fail ("DTN && \"dependent template has non-dependent name?\"", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 9563, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(DTN && "dependent template has non-dependent name?"); |
| 9564 | getQualifier() == SS.getScopeRep()", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 9564, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(DTN->getQualifier() == SS.getScopeRep()); |
| 9565 | QualType T = Context.getDependentTemplateSpecializationType(ETK_Typename, |
| 9566 | DTN->getQualifier(), |
| 9567 | DTN->getIdentifier(), |
| 9568 | TemplateArgs); |
| 9569 | |
| 9570 | |
| 9571 | TypeLocBuilder Builder; |
| 9572 | DependentTemplateSpecializationTypeLoc SpecTL |
| 9573 | = Builder.push<DependentTemplateSpecializationTypeLoc>(T); |
| 9574 | SpecTL.setElaboratedKeywordLoc(TypenameLoc); |
| 9575 | SpecTL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 9576 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 9577 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
| 9578 | SpecTL.setLAngleLoc(LAngleLoc); |
| 9579 | SpecTL.setRAngleLoc(RAngleLoc); |
| 9580 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 9581 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 9582 | return CreateParsedType(T, Builder.getTypeSourceInfo(Context, T)); |
| 9583 | } |
| 9584 | |
| 9585 | QualType T = CheckTemplateIdType(Template, TemplateIILoc, TemplateArgs); |
| 9586 | if (T.isNull()) |
| 9587 | return true; |
| 9588 | |
| 9589 | |
| 9590 | TypeLocBuilder Builder; |
| 9591 | TemplateSpecializationTypeLoc SpecTL |
| 9592 | = Builder.push<TemplateSpecializationTypeLoc>(T); |
| 9593 | SpecTL.setTemplateKeywordLoc(TemplateKWLoc); |
| 9594 | SpecTL.setTemplateNameLoc(TemplateIILoc); |
| 9595 | SpecTL.setLAngleLoc(LAngleLoc); |
| 9596 | SpecTL.setRAngleLoc(RAngleLoc); |
| 9597 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 9598 | SpecTL.setArgLocInfo(I, TemplateArgs[I].getLocInfo()); |
| 9599 | |
| 9600 | T = Context.getElaboratedType(ETK_Typename, SS.getScopeRep(), T); |
| 9601 | ElaboratedTypeLoc TL = Builder.push<ElaboratedTypeLoc>(T); |
| 9602 | TL.setElaboratedKeywordLoc(TypenameLoc); |
| 9603 | TL.setQualifierLoc(SS.getWithLocInContext(Context)); |
| 9604 | |
| 9605 | TypeSourceInfo *TSI = Builder.getTypeSourceInfo(Context, T); |
| 9606 | return CreateParsedType(T, TSI); |
| 9607 | } |
| 9608 | |
| 9609 | |
| 9610 | |
| 9611 | |
| 9612 | static bool isEnableIf(NestedNameSpecifierLoc NNS, const IdentifierInfo &II, |
| 9613 | SourceRange &CondRange, Expr *&Cond) { |
| 9614 | |
| 9615 | if (!II.isStr("type")) |
| 9616 | return false; |
| 9617 | |
| 9618 | |
| 9619 | if (!NNS || !NNS.getNestedNameSpecifier()->getAsType()) |
| 9620 | return false; |
| 9621 | TypeLoc EnableIfTy = NNS.getTypeLoc(); |
| 9622 | TemplateSpecializationTypeLoc EnableIfTSTLoc = |
| 9623 | EnableIfTy.getAs<TemplateSpecializationTypeLoc>(); |
| 9624 | if (!EnableIfTSTLoc || EnableIfTSTLoc.getNumArgs() == 0) |
| 9625 | return false; |
| 9626 | const TemplateSpecializationType *EnableIfTST = EnableIfTSTLoc.getTypePtr(); |
| 9627 | |
| 9628 | |
| 9629 | const TemplateDecl *EnableIfDecl = |
| 9630 | EnableIfTST->getTemplateName().getAsTemplateDecl(); |
| 9631 | if (!EnableIfDecl || EnableIfTST->isIncompleteType()) |
| 9632 | return false; |
| 9633 | |
| 9634 | |
| 9635 | const IdentifierInfo *EnableIfII = |
| 9636 | EnableIfDecl->getDeclName().getAsIdentifierInfo(); |
| 9637 | if (!EnableIfII || !EnableIfII->isStr("enable_if")) |
| 9638 | return false; |
| 9639 | |
| 9640 | |
| 9641 | CondRange = EnableIfTSTLoc.getArgLoc(0).getSourceRange(); |
| 9642 | |
| 9643 | |
| 9644 | Cond = nullptr; |
| 9645 | if (EnableIfTSTLoc.getArgLoc(0).getArgument().getKind() |
| 9646 | != TemplateArgument::Expression) |
| 9647 | return true; |
| 9648 | |
| 9649 | Cond = EnableIfTSTLoc.getArgLoc(0).getSourceExpression(); |
| 9650 | |
| 9651 | |
| 9652 | if (isa<CXXBoolLiteralExpr>(Cond->IgnoreParenCasts())) |
| 9653 | Cond = nullptr; |
| 9654 | |
| 9655 | return true; |
| 9656 | } |
| 9657 | |
| 9658 | |
| 9659 | |
| 9660 | QualType |
| 9661 | Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword, |
| 9662 | SourceLocation KeywordLoc, |
| 9663 | NestedNameSpecifierLoc QualifierLoc, |
| 9664 | const IdentifierInfo &II, |
| 9665 | SourceLocation IILoc) { |
| 9666 | CXXScopeSpec SS; |
| 9667 | SS.Adopt(QualifierLoc); |
| 9668 | |
| 9669 | DeclContext *Ctx = computeDeclContext(SS); |
| 9670 | if (!Ctx) { |
| 9671 | |
| 9672 | |
| 9673 | isDependent()", "/home/seafit/code_projects/clang_source/clang/lib/Sema/SemaTemplate.cpp", 9673, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(QualifierLoc.getNestedNameSpecifier()->isDependent()); |
| 9674 | return Context.getDependentNameType(Keyword, |
| 9675 | QualifierLoc.getNestedNameSpecifier(), |
| 9676 | &II); |
| 9677 | } |
| 9678 | |
| 9679 | |
| 9680 | |
| 9681 | |
| 9682 | |
| 9683 | |
| 9684 | |
| 9685 | if (RequireCompleteDeclContext(SS, Ctx)) |
| 9686 | return QualType(); |
| 9687 | |
| 9688 | DeclarationName Name(&II); |
| 9689 | LookupResult Result(*this, Name, IILoc, LookupOrdinaryName); |
| 9690 | LookupQualifiedName(Result, Ctx, SS); |
| 9691 | unsigned DiagID = 0; |
| 9692 | Decl *Referenced = nullptr; |
| 9693 | switch (Result.getResultKind()) { |
| 9694 | case LookupResult::NotFound: { |
| 9695 | |
| 9696 | |
| 9697 | SourceRange CondRange; |
| 9698 | Expr *Cond = nullptr; |
| 9699 | if (isEnableIf(QualifierLoc, II, CondRange, Cond)) { |
| 9700 | |
| 9701 | |
| 9702 | if (Cond) { |
| 9703 | Expr *FailedCond; |
| 9704 | std::string FailedDescription; |
| 9705 | std::tie(FailedCond, FailedDescription) = |
| 9706 | findFailedBooleanCondition(Cond); |
| 9707 | |
| 9708 | Diag(FailedCond->getExprLoc(), |
| 9709 | diag::err_typename_nested_not_found_requirement) |
| 9710 | << FailedDescription |
| 9711 | << FailedCond->getSourceRange(); |
| 9712 | return QualType(); |
| 9713 | } |
| 9714 | |
| 9715 | Diag(CondRange.getBegin(), diag::err_typename_nested_not_found_enable_if) |
| 9716 | << Ctx << CondRange; |
| 9717 | return QualType(); |
| 9718 | } |
| 9719 | |
| 9720 | DiagID = diag::err_typename_nested_not_found; |
| 9721 | break; |
| 9722 | } |
| 9723 | |
| 9724 | case LookupResult::FoundUnresolvedValue: { |
| 9725 | |
| 9726 | |
| 9727 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
| 9728 | IILoc); |
| 9729 | Diag(IILoc, diag::err_typename_refers_to_using_value_decl) |
| 9730 | << Name << Ctx << FullRange; |
| 9731 | if (UnresolvedUsingValueDecl *Using |
| 9732 | = dyn_cast<UnresolvedUsingValueDecl>(Result.getRepresentativeDecl())){ |
| 9733 | SourceLocation Loc = Using->getQualifierLoc().getBeginLoc(); |
| 9734 | Diag(Loc, diag::note_using_value_decl_missing_typename) |
| 9735 | << FixItHint::CreateInsertion(Loc, "typename "); |
| 9736 | } |
| 9737 | } |
| 9738 | |
| 9739 | |
| 9740 | LLVM_FALLTHROUGH; |
| 9741 | |
| 9742 | case LookupResult::NotFoundInCurrentInstantiation: |
| 9743 | |
| 9744 | return Context.getDependentNameType(Keyword, |
| 9745 | QualifierLoc.getNestedNameSpecifier(), |
| 9746 | &II); |
| 9747 | |
| 9748 | case LookupResult::Found: |
| 9749 | if (TypeDecl *Type = dyn_cast<TypeDecl>(Result.getFoundDecl())) { |
| 9750 | |
| 9751 | |
| 9752 | |
| 9753 | |
| 9754 | |
| 9755 | |
| 9756 | |
| 9757 | |
| 9758 | |
| 9759 | |
| 9760 | |
| 9761 | |
| 9762 | |
| 9763 | |
| 9764 | auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(Ctx); |
| 9765 | auto *FoundRD = dyn_cast<CXXRecordDecl>(Type); |
| 9766 | if (Keyword == ETK_Typename && LookupRD && FoundRD && |
| 9767 | FoundRD->isInjectedClassName() && |
| 9768 | declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent()))) |
| 9769 | Diag(IILoc, diag::ext_out_of_line_qualified_id_type_names_constructor) |
| 9770 | << &II << 1 << 0 ; |
| 9771 | |
| 9772 | |
| 9773 | |
| 9774 | MarkAnyDeclReferenced(Type->getLocation(), Type, ); |
| 9775 | return Context.getElaboratedType(Keyword, |
| 9776 | QualifierLoc.getNestedNameSpecifier(), |
| 9777 | Context.getTypeDeclType(Type)); |
| 9778 | } |
| 9779 | |
| 9780 | |
| 9781 | |
| 9782 | |
| 9783 | |
| 9784 | if (getLangOpts().CPlusPlus17) { |
| 9785 | if (auto *TD = getAsTypeTemplateDecl(Result.getFoundDecl())) { |
| 9786 | return Context.getElaboratedType( |
| 9787 | Keyword, QualifierLoc.getNestedNameSpecifier(), |
| 9788 | Context.getDeducedTemplateSpecializationType(TemplateName(TD), |
| 9789 | QualType(), false)); |
| 9790 | } |
| 9791 | } |
| 9792 | |
| 9793 | DiagID = diag::err_typename_nested_not_type; |
| 9794 | Referenced = Result.getFoundDecl(); |
| 9795 | break; |
| 9796 | |
| 9797 | case LookupResult::FoundOverloaded: |
| 9798 | DiagID = diag::err_typename_nested_not_type; |
| 9799 | Referenced = *Result.begin(); |
| 9800 | break; |
| 9801 | |
| 9802 | case LookupResult::Ambiguous: |
| 9803 | return QualType(); |
| 9804 | } |
| 9805 | |
| 9806 | |
| 9807 | |
| 9808 | SourceRange FullRange(KeywordLoc.isValid() ? KeywordLoc : SS.getBeginLoc(), |
| 9809 | IILoc); |
| 9810 | Diag(IILoc, DiagID) << FullRange << Name << Ctx; |
| 9811 | if (Referenced) |
| 9812 | Diag(Referenced->getLocation(), diag::note_typename_refers_here) |
| 9813 | << Name; |
| 9814 | return QualType(); |
| 9815 | } |
| 9816 | |
| 9817 | namespace { |
| 9818 | |
| 9819 | class CurrentInstantiationRebuilder |
| 9820 | : public TreeTransform<CurrentInstantiationRebuilder> { |
| 9821 | SourceLocation Loc; |
| 9822 | DeclarationName Entity; |
| 9823 | |
| 9824 | public: |
| 9825 | typedef TreeTransform<CurrentInstantiationRebuilder> inherited; |
| 9826 | |
| 9827 | CurrentInstantiationRebuilder(Sema &SemaRef, |
| 9828 | SourceLocation Loc, |
| 9829 | DeclarationName Entity) |
| 9830 | : TreeTransform<CurrentInstantiationRebuilder>(SemaRef), |
| 9831 | Loc(Loc), Entity(Entity) { } |
| 9832 | |
| 9833 | |
| 9834 | |
| 9835 | |
| 9836 | |
| 9837 | |
| 9838 | bool AlreadyTransformed(QualType T) { |
| 9839 | return T.isNull() || !T->isDependentType(); |
| 9840 | } |
| 9841 | |
| 9842 | |
| 9843 | |
| 9844 | SourceLocation getBaseLocation() { return Loc; } |
| 9845 | |
| 9846 | |
| 9847 | DeclarationName getBaseEntity() { return Entity; } |
| 9848 | |
| 9849 | |
| 9850 | |
| 9851 | void setBase(SourceLocation Loc, DeclarationName Entity) { |
| 9852 | this->Loc = Loc; |
| 9853 | this->Entity = Entity; |
| 9854 | } |
| 9855 | |
| 9856 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 9857 | |
| 9858 | return E; |
| 9859 | } |
| 9860 | }; |
| 9861 | } |
| 9862 | |
| 9863 | |
| 9864 | |
| 9865 | |
| 9866 | |
| 9867 | |
| 9868 | |
| 9869 | |
| 9870 | |
| 9871 | |
| 9872 | |
| 9873 | |
| 9874 | |
| 9875 | |
| 9876 | |
| 9877 | |
| 9878 | |
| 9879 | |
| 9880 | |
| 9881 | |
| 9882 | |
| 9883 | |
| 9884 | |
| 9885 | |
| 9886 | |
| 9887 | |
| 9888 | |
| 9889 | TypeSourceInfo *Sema::RebuildTypeInCurrentInstantiation(TypeSourceInfo *T, |
| 9890 | SourceLocation Loc, |
| 9891 | DeclarationName Name) { |
| 9892 | if (!T || !T->getType()->isDependentType()) |
| 9893 | return T; |
| 9894 | |
| 9895 | CurrentInstantiationRebuilder Rebuilder(*this, Loc, Name); |
| 9896 | return Rebuilder.TransformType(T); |
| 9897 | } |
| 9898 | |
| 9899 | ExprResult Sema::RebuildExprInCurrentInstantiation(Expr *E) { |
| 9900 | CurrentInstantiationRebuilder Rebuilder(*this, E->getExprLoc(), |
| 9901 | DeclarationName()); |
| 9902 | return Rebuilder.TransformExpr(E); |
| 9903 | } |
| 9904 | |
| 9905 | bool Sema::RebuildNestedNameSpecifierInCurrentInstantiation(CXXScopeSpec &SS) { |
| 9906 | if (SS.isInvalid()) |
| 9907 | return true; |
| 9908 | |
| 9909 | NestedNameSpecifierLoc QualifierLoc = SS.getWithLocInContext(Context); |
| 9910 | CurrentInstantiationRebuilder Rebuilder(*this, SS.getRange().getBegin(), |
| 9911 | DeclarationName()); |
| 9912 | NestedNameSpecifierLoc Rebuilt |
| 9913 | = Rebuilder.TransformNestedNameSpecifierLoc(QualifierLoc); |
| 9914 | if (!Rebuilt) |
| 9915 | return true; |
| 9916 | |
| 9917 | SS.Adopt(Rebuilt); |
| 9918 | return false; |
| 9919 | } |
| 9920 | |
| 9921 | |
| 9922 | |
| 9923 | bool Sema::RebuildTemplateParamsInCurrentInstantiation( |
| 9924 | TemplateParameterList *Params) { |
| 9925 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 9926 | Decl *Param = Params->getParam(I); |
| 9927 | |
| 9928 | |
| 9929 | if (isa<TemplateTypeParmDecl>(Param)) |
| 9930 | continue; |
| 9931 | |
| 9932 | |
| 9933 | if (TemplateTemplateParmDecl *TTP |
| 9934 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 9935 | if (RebuildTemplateParamsInCurrentInstantiation( |
| 9936 | TTP->getTemplateParameters())) |
| 9937 | return true; |
| 9938 | |
| 9939 | continue; |
| 9940 | } |
| 9941 | |
| 9942 | |
| 9943 | NonTypeTemplateParmDecl *NTTP = cast<NonTypeTemplateParmDecl>(Param); |
| 9944 | TypeSourceInfo *NewTSI |
| 9945 | = RebuildTypeInCurrentInstantiation(NTTP->getTypeSourceInfo(), |
| 9946 | NTTP->getLocation(), |
| 9947 | NTTP->getDeclName()); |
| 9948 | if (!NewTSI) |
| 9949 | return true; |
| 9950 | |
| 9951 | if (NewTSI->getType()->isUndeducedType()) { |
| 9952 | |
| 9953 | |
| 9954 | |
| 9955 | |
| 9956 | |
| 9957 | NewTSI = SubstAutoTypeSourceInfo(NewTSI, Context.DependentTy); |
| 9958 | } |
| 9959 | |
| 9960 | if (NewTSI != NTTP->getTypeSourceInfo()) { |
| 9961 | NTTP->setTypeSourceInfo(NewTSI); |
| 9962 | NTTP->setType(NewTSI->getType()); |
| 9963 | } |
| 9964 | } |
| 9965 | |
| 9966 | return false; |
| 9967 | } |
| 9968 | |
| 9969 | |
| 9970 | |
| 9971 | std::string |
| 9972 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 9973 | const TemplateArgumentList &Args) { |
| 9974 | return getTemplateArgumentBindingsText(Params, Args.data(), Args.size()); |
| 9975 | } |
| 9976 | |
| 9977 | std::string |
| 9978 | Sema::getTemplateArgumentBindingsText(const TemplateParameterList *Params, |
| 9979 | const TemplateArgument *Args, |
| 9980 | unsigned NumArgs) { |
| 9981 | SmallString<128> Str; |
| 9982 | llvm::raw_svector_ostream Out(Str); |
| 9983 | |
| 9984 | if (!Params || Params->size() == 0 || NumArgs == 0) |
| 9985 | return std::string(); |
| 9986 | |
| 9987 | for (unsigned I = 0, N = Params->size(); I != N; ++I) { |
| 9988 | if (I >= NumArgs) |
| 9989 | break; |
| 9990 | |
| 9991 | if (I == 0) |
| 9992 | Out << "[with "; |
| 9993 | else |
| 9994 | Out << ", "; |
| 9995 | |
| 9996 | if (const IdentifierInfo *Id = Params->getParam(I)->getIdentifier()) { |
| 9997 | Out << Id->getName(); |
| 9998 | } else { |
| 9999 | Out << '$' << I; |
| 10000 | } |
| 10001 | |
| 10002 | Out << " = "; |
| 10003 | Args[I].print(getPrintingPolicy(), Out); |
| 10004 | } |
| 10005 | |
| 10006 | Out << ']'; |
| 10007 | return Out.str(); |
| 10008 | } |
| 10009 | |
| 10010 | void Sema::MarkAsLateParsedTemplate(FunctionDecl *FD, Decl *FnD, |
| 10011 | CachedTokens &Toks) { |
| 10012 | if (!FD) |
| 10013 | return; |
| 10014 | |
| 10015 | auto LPT = llvm::make_unique<LateParsedTemplate>(); |
| 10016 | |
| 10017 | |
| 10018 | LPT->Toks.swap(Toks); |
| 10019 | LPT->D = FnD; |
| 10020 | LateParsedTemplateMap.insert(std::make_pair(FD, std::move(LPT))); |
| 10021 | |
| 10022 | FD->setLateTemplateParsed(true); |
| 10023 | } |
| 10024 | |
| 10025 | void Sema::UnmarkAsLateParsedTemplate(FunctionDecl *FD) { |
| 10026 | if (!FD) |
| 10027 | return; |
| 10028 | FD->setLateTemplateParsed(false); |
| 10029 | } |
| 10030 | |
| 10031 | bool Sema::IsInsideALocalClassWithinATemplateFunction() { |
| 10032 | DeclContext *DC = CurContext; |
| 10033 | |
| 10034 | while (DC) { |
| 10035 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(CurContext)) { |
| 10036 | const FunctionDecl *FD = RD->isLocalClass(); |
| 10037 | return (FD && FD->getTemplatedKind() != FunctionDecl::TK_NonTemplate); |
| 10038 | } else if (DC->isTranslationUnit() || DC->isNamespace()) |
| 10039 | return false; |
| 10040 | |
| 10041 | DC = DC->getParent(); |
| 10042 | } |
| 10043 | return false; |
| 10044 | } |
| 10045 | |
| 10046 | namespace { |
| 10047 | |
| 10048 | |
| 10049 | |
| 10050 | |
| 10051 | |
| 10052 | |
| 10053 | |
| 10054 | |
| 10055 | |
| 10056 | |
| 10057 | |
| 10058 | |
| 10059 | |
| 10060 | |
| 10061 | |
| 10062 | |
| 10063 | |
| 10064 | class ExplicitSpecializationVisibilityChecker { |
| 10065 | Sema &S; |
| 10066 | SourceLocation Loc; |
| 10067 | llvm::SmallVector<Module *, 8> Modules; |
| 10068 | |
| 10069 | public: |
| 10070 | ExplicitSpecializationVisibilityChecker(Sema &S, SourceLocation Loc) |
| 10071 | : S(S), Loc(Loc) {} |
| 10072 | |
| 10073 | void check(NamedDecl *ND) { |
| 10074 | if (auto *FD = dyn_cast<FunctionDecl>(ND)) |
| 10075 | return checkImpl(FD); |
| 10076 | if (auto *RD = dyn_cast<CXXRecordDecl>(ND)) |
| 10077 | return checkImpl(RD); |
| 10078 | if (auto *VD = dyn_cast<VarDecl>(ND)) |
| 10079 | return checkImpl(VD); |
| 10080 | if (auto *ED = dyn_cast<EnumDecl>(ND)) |
| 10081 | return checkImpl(ED); |
| 10082 | } |
| 10083 | |
| 10084 | private: |
| 10085 | void diagnose(NamedDecl *D, bool IsPartialSpec) { |
| 10086 | auto Kind = IsPartialSpec ? Sema::MissingImportKind::PartialSpecialization |
| 10087 | : Sema::MissingImportKind::ExplicitSpecialization; |
| 10088 | const bool Recover = true; |
| 10089 | |
| 10090 | |
| 10091 | |
| 10092 | |
| 10093 | if (Modules.empty()) |
| 10094 | S.diagnoseMissingImport(Loc, D, Kind, Recover); |
| 10095 | else |
| 10096 | S.diagnoseMissingImport(Loc, D, D->getLocation(), Modules, Kind, Recover); |
| 10097 | } |
| 10098 | |
| 10099 | |
| 10100 | |
| 10101 | |
| 10102 | |
| 10103 | |
| 10104 | |
| 10105 | |
| 10106 | |
| 10107 | |
| 10108 | |
| 10109 | |
| 10110 | |
| 10111 | template<typename SpecDecl> |
| 10112 | void checkImpl(SpecDecl *Spec) { |
| 10113 | bool IsHiddenExplicitSpecialization = false; |
| 10114 | if (Spec->getTemplateSpecializationKind() == TSK_ExplicitSpecialization) { |
| 10115 | IsHiddenExplicitSpecialization = |
| 10116 | Spec->getMemberSpecializationInfo() |
| 10117 | ? !S.hasVisibleMemberSpecialization(Spec, &Modules) |
| 10118 | : !S.hasVisibleExplicitSpecialization(Spec, &Modules); |
| 10119 | } else { |
| 10120 | checkInstantiated(Spec); |
| 10121 | } |
| 10122 | |
| 10123 | if (IsHiddenExplicitSpecialization) |
| 10124 | diagnose(Spec->getMostRecentDecl(), false); |
| 10125 | } |
| 10126 | |
| 10127 | void checkInstantiated(FunctionDecl *FD) { |
| 10128 | if (auto *TD = FD->getPrimaryTemplate()) |
| 10129 | checkTemplate(TD); |
| 10130 | } |
| 10131 | |
| 10132 | void checkInstantiated(CXXRecordDecl *RD) { |
| 10133 | auto *SD = dyn_cast<ClassTemplateSpecializationDecl>(RD); |
| 10134 | if (!SD) |
| 10135 | return; |
| 10136 | |
| 10137 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 10138 | if (auto *TD = From.dyn_cast<ClassTemplateDecl *>()) |
| 10139 | checkTemplate(TD); |
| 10140 | else if (auto *TD = |
| 10141 | From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
| 10142 | if (!S.hasVisibleDeclaration(TD)) |
| 10143 | diagnose(TD, true); |
| 10144 | checkTemplate(TD); |
| 10145 | } |
| 10146 | } |
| 10147 | |
| 10148 | void checkInstantiated(VarDecl *RD) { |
| 10149 | auto *SD = dyn_cast<VarTemplateSpecializationDecl>(RD); |
| 10150 | if (!SD) |
| 10151 | return; |
| 10152 | |
| 10153 | auto From = SD->getSpecializedTemplateOrPartial(); |
| 10154 | if (auto *TD = From.dyn_cast<VarTemplateDecl *>()) |
| 10155 | checkTemplate(TD); |
| 10156 | else if (auto *TD = |
| 10157 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
| 10158 | if (!S.hasVisibleDeclaration(TD)) |
| 10159 | diagnose(TD, true); |
| 10160 | checkTemplate(TD); |
| 10161 | } |
| 10162 | } |
| 10163 | |
| 10164 | void checkInstantiated(EnumDecl *FD) {} |
| 10165 | |
| 10166 | template<typename TemplDecl> |
| 10167 | void checkTemplate(TemplDecl *TD) { |
| 10168 | if (TD->isMemberSpecialization()) { |
| 10169 | if (!S.hasVisibleMemberSpecialization(TD, &Modules)) |
| 10170 | diagnose(TD->getMostRecentDecl(), false); |
| 10171 | } |
| 10172 | } |
| 10173 | }; |
| 10174 | } |
| 10175 | |
| 10176 | void Sema::checkSpecializationVisibility(SourceLocation Loc, NamedDecl *Spec) { |
| 10177 | if (!getLangOpts().Modules) |
| 10178 | return; |
| 10179 | |
| 10180 | ExplicitSpecializationVisibilityChecker(*this, Loc).check(Spec); |
| 10181 | } |
| 10182 | |
| 10183 | |
| 10184 | |
| 10185 | void Sema::checkPartialSpecializationVisibility(SourceLocation Loc, |
| 10186 | NamedDecl *Spec) { |
| 10187 | llvm::SmallVector<Module *, 8> Modules; |
| 10188 | if (!hasVisibleDeclaration(Spec, &Modules)) |
| 10189 | diagnoseMissingImport(Loc, Spec, Spec->getLocation(), Modules, |
| 10190 | MissingImportKind::PartialSpecialization, |
| 10191 | ); |
| 10192 | } |
| 10193 | |