From 049aac1b80143753b4b3449e529024bafe2250be Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 12 Jul 2022 22:45:05 -0700 Subject: Support `class` types. (#2321) * Support `class` types. * Ignore class-keyword test * Fix codereview comments and warnings. Co-authored-by: Yong He --- source/slang/slang-check-overload.cpp | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (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 bd27c7df2..be351a78f 100644 --- a/source/slang/slang-check-overload.cpp +++ b/source/slang/slang-check-overload.cpp @@ -60,6 +60,40 @@ namespace Slang return counts; } + bool SemanticsVisitor::TryCheckOverloadCandidateClassNewMatchUp(OverloadResolveContext& context, OverloadCandidate const& candidate) + { + // Check that a constructor call to a class type must be in a `new` expr, and a `new` expr + // is only used to construct a class. + bool isClassType = false; + bool isNewExpr = false; + if (auto ctorDeclRef = candidate.item.declRef.as()) + { + if (auto resultType = as(candidate.resultType)) + { + if (resultType->declRef.as()) + { + isClassType = true; + } + } + } + if (as(context.originalExpr)) + { + isNewExpr = true; + } + + if (isNewExpr && !isClassType) + { + getSink()->diagnose(context.originalExpr, Diagnostics::newCanOnlyBeUsedToInitializeAClass); + return false; + } + if (!isNewExpr && isClassType) + { + getSink()->diagnose(context.originalExpr, Diagnostics::classCanOnlyBeInitializedWithNew); + return false; + } + return true; + } + bool SemanticsVisitor::TryCheckOverloadCandidateArity( OverloadResolveContext& context, OverloadCandidate const& candidate) @@ -572,6 +606,9 @@ namespace Slang context.mode = OverloadResolveContext::Mode::ForReal; + if (!TryCheckOverloadCandidateClassNewMatchUp(context, candidate)) + goto error; + if (!TryCheckOverloadCandidateArity(context, candidate)) goto error; -- cgit v1.2.3