From 6a23949f07f4eba38086b656e7073ce3bf8cd2fe Mon Sep 17 00:00:00 2001 From: Yong He Date: Fri, 13 Jun 2025 22:13:00 -0700 Subject: Allow interface methods to have default implementations. (#7439) --- source/slang/slang-check-expr.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'source/slang/slang-check-expr.cpp') diff --git a/source/slang/slang-check-expr.cpp b/source/slang/slang-check-expr.cpp index 9f5c1dc2e..4c6bf98d2 100644 --- a/source/slang/slang-check-expr.cpp +++ b/source/slang/slang-check-expr.cpp @@ -5346,6 +5346,12 @@ Expr* SemanticsExprVisitor::visitThisExpr(ThisExpr* expr) } return expr; } + else if (auto defaultImplDecl = as(containerDecl)) + { + expr->type.type = + DeclRefType::create(m_astBuilder, DeclRef(defaultImplDecl->thisTypeDecl)); + return expr; + } #if 0 else if (auto aggTypeDecl = as(containerDecl)) { @@ -5401,7 +5407,12 @@ Expr* SemanticsExprVisitor::visitThisTypeExpr(ThisTypeExpr* expr) expr->type.type = thisTypeType; return expr; } - + else if (auto defaultImplDecl = as(containerDecl)) + { + expr->type.type = + DeclRefType::create(m_astBuilder, DeclRef(defaultImplDecl->thisTypeDecl)); + return expr; + } scope = scope->parent; } @@ -5409,6 +5420,22 @@ Expr* SemanticsExprVisitor::visitThisTypeExpr(ThisTypeExpr* expr) return CreateErrorExpr(expr); } +Expr* SemanticsExprVisitor::visitThisInterfaceExpr(ThisInterfaceExpr* expr) +{ + auto scope = expr->scope; + + auto containerDecl = findParentInterfaceDecl(scope->containerDecl); + + // ThisInterfaceExpr can only be synthesized by the compiler during parsing + // an interface decl with default implementation, so container must always + // be an interface decl. + SLANG_ASSERT(containerDecl); + expr->declRef = + createDefaultSubstitutionsIfNeeded(m_astBuilder, this, getDefaultDeclRef(containerDecl)); + expr->type = m_astBuilder->getTypeType(DeclRefType::create(m_astBuilder, expr->declRef)); + return expr; +} + Expr* SemanticsExprVisitor::visitCastToSuperTypeExpr(CastToSuperTypeExpr* expr) { // CastToSuperType is effectively a struct field. -- cgit v1.2.3