From 11111e5733b189127dc2c4934d67693b9bc6e764 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 6 Dec 2023 12:05:07 -0800 Subject: Support visibility control and default to `internal`. (#3380) * Support visibility control and default to `internal`. * Fix wip. * Fixes. * Fix. * Fix test. * Add legacy language detection and compatibility for existing code. * Add doc. --------- Co-authored-by: Yong He --- source/slang/slang-check-overload.cpp | 62 ++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 19 deletions(-) (limited to 'source/slang/slang-check-overload.cpp') diff --git a/source/slang/slang-check-overload.cpp b/source/slang/slang-check-overload.cpp index d7d29a4e1..2912a79b0 100644 --- a/source/slang/slang-check-overload.cpp +++ b/source/slang/slang-check-overload.cpp @@ -190,6 +190,33 @@ namespace Slang } } + bool SemanticsVisitor::TryCheckOverloadCandidateVisibility(OverloadResolveContext& context, OverloadCandidate const& candidate) + { + // Always succeeds when we are trying out constructors. + if (context.mode == OverloadResolveContext::Mode::JustTrying) + { + if (as(candidate.item.declRef)) + return true; + } + + if (!context.sourceScope) + return true; + + if (!candidate.item.declRef) + return true; + + if (!isDeclVisibleFromScope(candidate.item.declRef, context.sourceScope)) + { + if (context.mode == OverloadResolveContext::Mode::ForReal) + { + getSink()->diagnose(context.loc, Diagnostics::declIsNotVisible, candidate.item.declRef); + } + return false; + } + + return true; + } + bool SemanticsVisitor::TryCheckGenericOverloadCandidateTypes( OverloadResolveContext& context, OverloadCandidate& candidate) @@ -704,6 +731,10 @@ namespace Slang if (!TryCheckOverloadCandidateConstraints(context, candidate)) return; + candidate.status = OverloadCandidate::Status::VisibilityChecked; + if (!TryCheckOverloadCandidateVisibility(context, candidate)) + return; + candidate.status = OverloadCandidate::Status::Applicable; } @@ -777,6 +808,9 @@ namespace Slang if (!TryCheckOverloadCandidateConstraints(context, candidate)) goto error; + if (!TryCheckOverloadCandidateVisibility(context, candidate)) + goto error; + { Expr* baseExpr; switch(candidate.flavor) @@ -887,7 +921,6 @@ namespace Slang } else { - SLANG_DIAGNOSE_UNEXPECTED(getSink(), context.loc, "no original expression for overload result"); return nullptr; } } @@ -1511,6 +1544,7 @@ namespace Slang this, getName("$init"), type, + context.sourceScope, LookupMask::Default, LookupOptions::NoDeref); @@ -1885,7 +1919,7 @@ namespace Slang context.argCount = expr->arguments.getCount(); context.args = expr->arguments.getBuffer(); context.loc = expr->loc; - + context.sourceScope = m_outerScope; context.baseExpr = GetBaseExpr(funcExpr); // TODO: We should have a special case here where an `InvokeExpr` @@ -1975,26 +2009,16 @@ namespace Slang Index candidateCount = context.bestCandidates.getCount(); Index maxCandidatesToPrint = 10; // don't show too many candidates at once... Index candidateIndex = 0; + context.bestCandidates.sort([](const OverloadCandidate& c1, const OverloadCandidate& c2) { return c1.status < c2.status; }); + for (auto candidate : context.bestCandidates) { String declString = ASTPrinter::getDeclSignatureString(candidate.item, m_astBuilder); -// declString = declString + "[" + String(candidate.conversionCostSum) + "]"; - -#if 0 - // Debugging: ensure that we don't consider multiple declarations of the same operation - if (auto decl = as(candidate.item.declRef.decl)) - { - char buffer[1024]; - sprintf_s(buffer, sizeof(buffer), "[this:%p, primary:%p, next:%p]", - decl, - decl->primaryDecl, - decl->nextDecl); - declString.append(buffer); - } -#endif - - getSink()->diagnose(candidate.item.declRef, Diagnostics::overloadCandidate, declString); + if (candidate.status == OverloadCandidate::Status::VisibilityChecked) + getSink()->diagnose(candidate.item.declRef, Diagnostics::invisibleOverloadCandidate, declString); + else + getSink()->diagnose(candidate.item.declRef, Diagnostics::overloadCandidate, declString); candidateIndex++; if (candidateIndex == maxCandidatesToPrint) @@ -2126,7 +2150,7 @@ namespace Slang context.argCount = args.getCount(); context.args = args.getBuffer(); context.loc = genericAppExpr->loc; - + context.sourceScope = m_outerScope; context.baseExpr = GetBaseExpr(baseExpr); AddGenericOverloadCandidates(baseExpr, context); -- cgit v1.2.3