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

Replace more IR Scala case classes with Java interfaces #12211

Merged
merged 23 commits into from
Feb 11, 2025
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
255ac9a
Do not use extractors for DefinitionArgument
Akirathan Jan 31, 2025
cd6d277
Do not use DefinitionArgument.apply
Akirathan Jan 31, 2025
cd178d1
Replace DefinitionArgument case class with Java interface
Akirathan Jan 31, 2025
6309510
Implement DefinitionArgument.showCode.
Akirathan Feb 4, 2025
0bf4a78
Fix compilation error in runtime-integration-tests
Akirathan Feb 5, 2025
2b14325
showCode method is not implemented in the generated superclass.
Akirathan Feb 6, 2025
9a7c2e3
Implement showCode method in tests.
Akirathan Feb 7, 2025
3e91fde
Fix match guard in SuggestionBuilder.
Akirathan Feb 7, 2025
b67720e
Merge branch 'develop' into wip/akirathan/more-java-ir
Akirathan Feb 10, 2025
596a6a0
Refactor match extractors of Application.Prefix to normal match
Akirathan Feb 10, 2025
c9935a0
Refactor apply to new operator
Akirathan Feb 10, 2025
365cb7c
Refactor match extractors of Application.Force to normal match
Akirathan Feb 10, 2025
61d225e
Refactor apply to new operator
Akirathan Feb 10, 2025
70fe733
IRNodeClassGenerator includes some necessary imports
Akirathan Feb 10, 2025
346bb9e
Literal trait is not sealed
Akirathan Feb 10, 2025
0223af5
Replace Application with Java
Akirathan Feb 10, 2025
d291479
Fix some copy methods and constructors
Akirathan Feb 10, 2025
91e1391
Explicitly call apply method in tests
Akirathan Feb 10, 2025
7ff0437
Fix some copy methods and constructors
Akirathan Feb 10, 2025
dd5c4c0
Generate copy builder
Akirathan Feb 10, 2025
a2002e4
setLocation uses copy builder
Akirathan Feb 10, 2025
d8f3bd2
Only add imports for types that are not in an unnamed package
Akirathan Feb 10, 2025
8b3d9a6
Fix null IdentifiedLocation in OperatorToFunctionTest
Akirathan Feb 10, 2025
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
Prev Previous commit
Next Next commit
Replace DefinitionArgument case class with Java interface
Akirathan committed Jan 31, 2025
commit cd178d1a2889767148ffdcb3ad150d1897bddf5a
Original file line number Diff line number Diff line change
@@ -253,7 +253,7 @@ private static Expression addTypeAscriptionToSelfArgument(Expression methodBody)
if (firstArg instanceof DefinitionArgument.Specified selfArg
&& selfArg.name() instanceof Name.Self) {
var selfType = new Name.SelfType(selfArg.identifiedLocation(), new MetadataStorage());
var newSelfArg = selfArg.copyWithAscribedType(selfType);
var newSelfArg = selfArg.copyWithAscribedType(Option.apply(selfType));
return lambdaWithNewSelfArg(lambda, newSelfArg);
} else {
throw new CompilerError(
Original file line number Diff line number Diff line change
@@ -542,7 +542,7 @@ case object AliasAnalysis extends IRPass {
alias.AliasMetadata.Occurrence(builder.toGraph(), definition.id)
)
)
.copy(
.copyWithAscribedType(
ascribedType = arg.ascribedType.map(analyseExpression(_, builder))
)

@@ -581,7 +581,7 @@ case object AliasAnalysis extends IRPass {
)
} else {
arg
.copy(
.copyWithAscribedType(
ascribedType = Some(Redefined.Arg(name, arg.identifiedLocation))
)
.updateMetadata(
Original file line number Diff line number Diff line change
@@ -191,7 +191,9 @@ case object CachePreferenceAnalysis extends IRPass {
case spec: DefinitionArgument.Specified =>
val defValue = spec.defaultValue
spec
.copy(defaultValue = defValue.map(analyseExpression(_, weights)))
.copyWithDefaultValue(defaultValue =
defValue.map(analyseExpression(_, weights))
)
.updateMetadata(new MetadataPair(this, weights))
}
}
Original file line number Diff line number Diff line change
@@ -725,7 +725,7 @@ case object DataflowAnalysis extends IRPass {
})

spec
.copy(
.copyWithDefaultValue(
defaultValue = defValue.map(analyseExpression(_, info))
)
.updateMetadata(new MetadataPair(this, info))
Original file line number Diff line number Diff line change
@@ -286,7 +286,7 @@ case object DemandAnalysis extends IRPass {
arg match {
case spec: DefinitionArgument.Specified =>
val default = spec.defaultValue
spec.copy(
spec.copyWithDefaultValue(
defaultValue = default.map(x =>
analyseExpression(
x,
Original file line number Diff line number Diff line change
@@ -333,7 +333,7 @@ case object ComplexType extends IRPass {

val binding = new definition.Method.Binding(
methodRef.duplicate(),
args.map(_.duplicate()),
args.map(_.duplicate(true, true, true, false)),
isPrivate,
body.duplicate(),
identifiedLocation,
Original file line number Diff line number Diff line change
@@ -77,15 +77,15 @@ class LambdaShorthandToLambdaMini(
new Function.Lambda(
List(
new DefinitionArgument.Specified(
name = Name.Literal(
Name.Literal(
newName.name,
isMethod = false,
null
),
ascribedType = None,
defaultValue = None,
suspended = false,
identifiedLocation = null
None,
None,
false,
null
)
),
newName,
@@ -160,11 +160,7 @@ class LambdaShorthandToLambdaMini(
updatedName.get,
isMethod = false,
fn.location.orNull
),
None,
None,
suspended = false,
null
)
)
),
appResult,
@@ -196,13 +192,7 @@ class LambdaShorthandToLambdaMini(
val locWithoutId =
newVec.location.map(l => new IdentifiedLocation(l.location()))
bindings.foldLeft(newVec: Expression) { (body, bindingName) =>
val defArg = new DefinitionArgument.Specified(
bindingName,
ascribedType = None,
defaultValue = None,
suspended = false,
identifiedLocation = null
)
val defArg = new DefinitionArgument.Specified(bindingName)
new Function.Lambda(List(defArg), body, locWithoutId.orNull)
}

Original file line number Diff line number Diff line change
@@ -205,7 +205,7 @@ case object UnusedBindings extends IRPass {
literal.originalName.getOrElse(literal)
case _ => name
}
s.copy(
s.copyWithDefaultValue(
defaultValue = default.map(runExpression(_, context))
).addDiagnostic(warnings.Unused.FunctionArgument(nameToReport))
} else s
Original file line number Diff line number Diff line change
@@ -405,6 +405,7 @@ case object LambdaConsolidate extends IRPass {
} else Set[UUID @Identifier]()

usageIds
case _ => Set[UUID @Identifier]()
}
}

@@ -435,7 +436,8 @@ case object LambdaConsolidate extends IRPass {
)
} else oldName

spec.copy(name = newName)
spec.withName(newName)
case (arg, _) => arg
}
}

@@ -468,7 +470,8 @@ case object LambdaConsolidate extends IRPass {

val processedArgList = args.zip(newDefaults).map {
case (spec: DefinitionArgument.Specified, default) =>
spec.copy(defaultValue = default)
spec.copyWithDefaultValue(default)
case (arg, _) => arg
}

(processedArgList, newBody)
Original file line number Diff line number Diff line change
@@ -214,7 +214,7 @@ case object IgnoredBindings extends IRPass {
if spec.name.isInstanceOf[Name.Self] =>
// Note [Ignored `this` Argument]
spec
.copy(defaultValue =
.copyWithDefaultValue(
spec.defaultValue.map(resolveExpression(_, freshNameSupply))
)
.updateMetadata(new MetadataPair(this, State.Ignored))
@@ -238,9 +238,8 @@ case object IgnoredBindings extends IRPass {
} else {
setNotIgnored(
spec
.copy(
defaultValue =
spec.defaultValue.map(resolveExpression(_, freshNameSupply))
.copyWithDefaultValue(
spec.defaultValue.map(resolveExpression(_, freshNameSupply))
)
)
}
Original file line number Diff line number Diff line change
@@ -283,8 +283,8 @@ case object SuspendedArguments extends IRPass {
arg match {
case spec: DefinitionArgument.Specified =>
if (representsSuspended(typ) || spec.suspended) {
spec.copy(suspended = true)
} else spec.copy(suspended = false)
spec.copyWithSuspended(true)
} else spec.copyWithSuspended(false)
}
}

Original file line number Diff line number Diff line change
@@ -228,11 +228,11 @@ case object TypeSignatures extends IRPass {
if specified.ascribedType.isDefined =>
val ascribedType = specified.ascribedType.get
val sig = resolveExpression(ascribedType.duplicate())
specified.copy(
name = specified.name.updateMetadata(
specified.copyWithNameAndAscribedType(
specified.name.updateMetadata(
new MetadataPair(this, Signature(sig))
),
ascribedType = Some(
Some(
ascribedType.updateMetadata(new MetadataPair(this, Signature(sig)))
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.enso.compiler.core.ir;

import java.util.function.Function;
import org.enso.compiler.core.IR;
import org.enso.runtime.parser.dsl.GenerateFields;
import org.enso.runtime.parser.dsl.GenerateIR;
import org.enso.runtime.parser.dsl.IRChild;
import org.enso.runtime.parser.dsl.IRField;
import scala.Option;

public interface DefinitionArgument extends IR {
Name name();

Option<Expression> ascribedType();

Option<Expression> defaultValue();

boolean suspended();

@Override
DefinitionArgument mapExpressions(Function<Expression, Expression> fn);

@Override
DefinitionArgument duplicate(
boolean keepLocations,
boolean keepMetadata,
boolean keepDiagnostics,
boolean keepIdentifiers);

DefinitionArgument withName(Name ir);

@GenerateIR(interfaces = {DefinitionArgument.class})
final class Specified extends DefinitionArgumentSpecifiedGen {
@GenerateFields
public Specified(
@IRChild Name name,
@IRChild Option<Expression> ascribedType,
@IRChild Option<Expression> defaultValue,
@IRField boolean suspended,
IdentifiedLocation identifiedLocation,
MetadataStorage passData,
DiagnosticStorage diagnostics) {
super(name, ascribedType, defaultValue, suspended, identifiedLocation, passData, diagnostics);
}

public Specified(
Name name,
Option<Expression> ascribedType,
Option<Expression> defaultValue,
boolean suspended,
IdentifiedLocation identifiedLocation) {
this(
name,
ascribedType,
defaultValue,
suspended,
identifiedLocation,
new MetadataStorage(),
null);
}

public Specified(
Name name,
Option<Expression> ascribedType,
Option<Expression> defaultValue,
boolean suspended,
IdentifiedLocation identifiedLocation,
MetadataStorage passData) {
this(name, ascribedType, defaultValue, suspended, identifiedLocation, passData, null);
}

public Specified(Name name) {
this(name, Option.empty(), Option.empty(), false, null, new MetadataStorage());
}

@Override
public DefinitionArgument withName(Name ir) {
return copy(
diagnostics, passData, location, id, ir, ascribedType(), defaultValue(), suspended());
}

public Specified copyWithAscribedType(Option<Expression> ascribedType) {
return copy(
diagnostics, passData, location, id, name(), ascribedType, defaultValue(), suspended());
}

public Specified copyWithDefaultValue(Option<Expression> defaultValue) {
return copy(
diagnostics, passData, location, id, name(), ascribedType(), defaultValue, suspended());
}

public Specified copyWithSuspended(boolean suspended) {
return copy(
diagnostics, passData, location, id, name(), ascribedType(), defaultValue(), suspended);
}

public Specified copy(Option<Expression> defaultValue, Option<Expression> ascribedType) {
return copy(
diagnostics, passData, location, id, name(), ascribedType, defaultValue, suspended());
}

public Specified copy(Name name, Option<Expression> defaultValue) {
return copy(
diagnostics, passData, location, id, name, ascribedType(), defaultValue, suspended());
}

public Specified copyWithNameAndAscribedType(Name name, Option<Expression> ascribedType) {
return copy(
diagnostics, passData, location, id, name, ascribedType, defaultValue(), suspended());
}
}
}
Loading
Loading