summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-check-expr.cpp
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2024-08-27 18:48:41 -0700
committerGitHub <noreply@github.com>2024-08-27 18:48:41 -0700
commit4f6f827e26ffcb9b850ef8a8b7f7b4beb5addb7a (patch)
treee8f20e798866df7e10067ce5b7ae22f9dc57ff84 /source/slang/slang-check-expr.cpp
parentfbaa444d890f58fabc5933b0c28048d2c5d862c0 (diff)
Add functor syntax support. (#4926)
Diffstat (limited to 'source/slang/slang-check-expr.cpp')
-rw-r--r--source/slang/slang-check-expr.cpp49
1 files changed, 44 insertions, 5 deletions
diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp
index 3072c3257..500407e26 100644
--- a/source/slang/slang-check-expr.cpp
+++ b/source/slang/slang-check-expr.cpp
@@ -2693,6 +2693,42 @@ namespace Slang
return newExpr;
expr->functionExpr = CheckTerm(expr->functionExpr);
+
+ if (auto baseType = as<DeclRefType>(expr->functionExpr->type))
+ {
+ // If callee is a value of DeclRefType, then it is a functor.
+ // We need to look for `operator()` member within the type and
+ // call that instead.
+ auto operatorName = getName("()");
+
+ bool needDeref = false;
+ expr->functionExpr = maybeInsertImplicitOpForMemberBase(expr->functionExpr, needDeref);
+
+ LookupResult lookupResult = lookUpMember(
+ m_astBuilder,
+ this,
+ operatorName,
+ expr->functionExpr->type,
+ m_outerScope,
+ LookupMask::Default,
+ LookupOptions::NoDeref);
+ bool diagnosed = false;
+ lookupResult = filterLookupResultByVisibilityAndDiagnose(lookupResult, expr->loc, diagnosed);
+ if (!lookupResult.isValid())
+ {
+ if (!diagnosed)
+ getSink()->diagnose(expr, Diagnostics::callOperatorNotFound, baseType);
+ return CreateErrorExpr(expr);
+ }
+ auto callFuncExpr = createLookupResultExpr(
+ operatorName,
+ lookupResult,
+ expr->functionExpr,
+ expr->loc,
+ expr->functionExpr);
+ expr->functionExpr = callFuncExpr;
+ }
+
m_treatAsDifferentiableExpr = treatAsDifferentiableExpr;
// If we are in a differentiable function, register differential witness tables involved in
@@ -4400,12 +4436,8 @@ namespace Slang
return expr;
}
- Expr* SemanticsVisitor::checkBaseForMemberExpr(Expr* inBaseExpr, bool& outNeedDeref)
+ Expr* SemanticsVisitor::maybeInsertImplicitOpForMemberBase(Expr* baseExpr, bool& outNeedDeref)
{
- auto baseExpr = inBaseExpr;
-
- baseExpr = CheckTerm(baseExpr);
-
auto derefExpr = MaybeDereference(baseExpr);
if (derefExpr != baseExpr)
@@ -4459,6 +4491,13 @@ namespace Slang
return baseExpr;
}
+ Expr* SemanticsVisitor::checkBaseForMemberExpr(Expr* inBaseExpr, bool& outNeedDeref)
+ {
+ auto baseExpr = inBaseExpr;
+ baseExpr = CheckTerm(baseExpr);
+ return maybeInsertImplicitOpForMemberBase(baseExpr, outNeedDeref);
+ }
+
Expr* SemanticsVisitor::checkGeneralMemberLookupExpr(MemberExpr* expr, Type* baseType)
{
LookupResult lookupResult = lookUpMember(