summaryrefslogtreecommitdiff
path: root/source/slang/slang-check-overload.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2022-07-12 22:45:05 -0700
committerGitHub <noreply@github.com>2022-07-12 22:45:05 -0700
commit049aac1b80143753b4b3449e529024bafe2250be (patch)
treee6734ef240bf7016562a301009add1761caf06f6 /source/slang/slang-check-overload.cpp
parenta6775666c38ccaeb2a991921a08343afa09c659b (diff)
Support `class` types. (#2321)
* Support `class` types. * Ignore class-keyword test * Fix codereview comments and warnings. Co-authored-by: Yong He <yhe@nvidia.com>
Diffstat (limited to 'source/slang/slang-check-overload.cpp')
-rw-r--r--source/slang/slang-check-overload.cpp37
1 files changed, 37 insertions, 0 deletions
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<ConstructorDecl>())
+ {
+ if (auto resultType = as<DeclRefType>(candidate.resultType))
+ {
+ if (resultType->declRef.as<ClassDecl>())
+ {
+ isClassType = true;
+ }
+ }
+ }
+ if (as<NewExpr>(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;