Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: generation of memoized class member calls #982

Merged
merged 8 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
SdsBlock,
SdsBlockLambda,
SdsCall,
SdsClassMember,
SdsDeclaration,
SdsExpression,
SdsModule,
Expand Down Expand Up @@ -1018,6 +1019,10 @@ export class SafeDsPythonGenerator {
Parameter.isOptional(this.nodeMapper.argumentToParameter(arg)),
);
const fullyQualifiedTargetName = this.generateFullyQualifiedFunctionName(expression);
if (!containsOptionalArgs && isSdsMemberAccess(expression.receiver)) {
const referenceImport = this.createImportDataForReference(expression.receiver.member!);
frame.addImport(referenceImport);
}
return expandTracedToNode(expression)`${RUNNER_PACKAGE}.memoized_call("${fullyQualifiedTargetName}", ${
containsOptionalArgs ? 'lambda *_ : ' : ''
}${
Expand All @@ -1028,7 +1033,9 @@ export class SafeDsPythonGenerator {
expression.receiver.receiver.receiver,
frame,
)}.${this.generateExpression(expression.receiver.member!, frame)}`
: this.generateExpression(expression.receiver, frame)
: isSdsMemberAccess(expression.receiver)
? this.getClassQualifiedNameForMember(<SdsClassMember>callable)
: this.generateExpression(expression.receiver, frame)
}, [${generateThisParam ? thisParam : ''}${
generateThisParam && memoizedArgs.length > 0 ? ', ' : ''
}${joinTracedToNode(expression.argumentList, 'arguments')(
Expand Down Expand Up @@ -1083,6 +1090,16 @@ export class SafeDsPythonGenerator {
throw new Error('Callable of provided call does not exist or is not a declaration.');
}

private getClassQualifiedNameForMember(expression: SdsClassMember): string {
WinPlay02 marked this conversation as resolved.
Show resolved Hide resolved
const classMemberPath = [this.getPythonNameOrDefault(expression)];
let enclosingClass = AstUtils.getContainerOfType(expression.$container, isSdsClass);
while (enclosingClass) {
WinPlay02 marked this conversation as resolved.
Show resolved Hide resolved
classMemberPath.push(this.getPythonNameOrDefault(enclosingClass));
WinPlay02 marked this conversation as resolved.
Show resolved Hide resolved
enclosingClass = AstUtils.getContainerOfType(enclosingClass.$container, isSdsClass);
}
return classMemberPath.reverse().join('.');
}

private getArgumentsMap(
argumentList: SdsArgument[],
frame: GenerationInfoFrame,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Imports ----------------------------------------------------------------------

import safeds_runner
from tests.generator.memberAccessWithRunnerIntegration import C, f, factory, g, h
from safeds.data.tabular.containers import Table
from tests.generator.memberAccessWithRunnerIntegration import C, f, factory, factoryNested, g, h, Outer
from typing import Any, TypeVar

# Type variables ---------------------------------------------------------------
Expand All @@ -27,3 +28,14 @@ def test():
f(safeds_runner.memoized_call("tests.generator.memberAccessWithRunnerIntegration.C.j", C.j, [safeds_runner.memoized_call("tests.generator.memberAccessWithRunnerIntegration.C", C, [], []), 123], []))
f(safeds_runner.memoized_call("tests.generator.memberAccessWithRunnerIntegration.C.k2", C.k2, [safeds_runner.memoized_call("tests.generator.memberAccessWithRunnerIntegration.C", C, [], []), 'abc'], []))
f(safeds_runner.memoized_call("tests.generator.memberAccessWithRunnerIntegration.C.from_csv_file", C.from_csv_file, ['abc.csv'], [safeds_runner.file_mtime('abc.csv')]))
a = safeds_runner.memoized_call("safeds.data.tabular.containers.Table.from_csv_file", Table.from_csv_file, ['abc.csv'], [safeds_runner.file_mtime('abc.csv')])
safeds_runner.save_placeholder('a', a)
v = safeds_runner.memoized_call("safeds.data.tabular.containers.Table.get_column", Table.get_column, [a, 'b'], [])
safeds_runner.save_placeholder('v', v)
f(v)
f(safeds_runner.memoized_call("tests.generator.memberAccessWithRunnerIntegration.Outer.Nested.f", Outer.Nested.f, [], []))
nestedInstance = safeds_runner.memoized_call("tests.generator.memberAccessWithRunnerIntegration.factoryNested", factoryNested, [], [])
safeds_runner.save_placeholder('nestedInstance', nestedInstance)
nestedResult = safeds_runner.memoized_call("tests.generator.memberAccessWithRunnerIntegration.Outer.Nested.g", Outer.Nested.g, [nestedInstance], [])
safeds_runner.save_placeholder('nestedResult', nestedResult)
f(nestedResult)

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,20 @@ class C() {
static fun from_csv_file(name: String) -> c: C
}

class Outer() {
class Nested() {
@Pure
static fun f() -> result: Boolean

@Pure
fun g() -> result: Boolean
}
}

@Pure fun factory() -> instance: C?

@Pure fun factoryNested() -> instance: Outer.Nested

pipeline test {
f(g().result);
f(h().result1);
Expand All @@ -38,4 +50,11 @@ pipeline test {
f(C().j(123));
f(C().k("abc"));
f(C.from_csv_file("abc.csv"));
val a = Table.fromCsvFile("abc.csv");
val v = a.getColumn("b");
f(v);
f(Outer.Nested.f());
val nestedInstance = factoryNested();
val nestedResult = nestedInstance.g();
f(nestedResult);
}