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

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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 @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,13 @@ case object AliasAnalysis extends IRPass {
}
}

private def isSyntheticSelf(name: Name): Boolean = {
name match {
case Name.Self(_, true, _) => true
case _ => false
}
}

/** Performs alias analysis on the argument definitions for a function.
*
* Care is taken during this analysis to ensure that spurious resolutions do
Expand All @@ -519,18 +526,11 @@ case object AliasAnalysis extends IRPass {
builder: GraphBuilder
): List[DefinitionArgument] = {
args.map {
case arg @ DefinitionArgument.Specified(
selfName @ Name.Self(_, true, _),
_,
_,
_,
_,
_
) =>
case arg: DefinitionArgument.Specified if isSyntheticSelf(arg.name) =>
// Synthetic `self` must not be added to the scope, but it has to be added as a
// definition for frame index metadata
val definition = builder.newDef(
selfName.name,
arg.name.name,
arg.getId(),
arg.getExternalId
)
Expand All @@ -542,18 +542,14 @@ case object AliasAnalysis extends IRPass {
alias.AliasMetadata.Occurrence(builder.toGraph(), definition.id)
)
)
.copy(
.copyWithAscribedType(
ascribedType = arg.ascribedType.map(analyseExpression(_, builder))
)

case arg @ DefinitionArgument.Specified(
name,
_,
value,
suspended,
_,
_
) =>
case arg: DefinitionArgument.Specified =>
val name = arg.name
val value = arg.defaultValue
val suspended = arg.suspended
val nameOccursInScope =
builder.findDef(
name.name
Expand Down Expand Up @@ -585,7 +581,7 @@ case object AliasAnalysis extends IRPass {
)
} else {
arg
.copy(
.copyWithAscribedType(
ascribedType = Some(Redefined.Arg(name, arg.identifiedLocation))
)
.updateMetadata(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,12 @@ case object CachePreferenceAnalysis extends IRPass {
weights: WeightInfo
): DefinitionArgument = {
argument match {
case spec @ DefinitionArgument.Specified(_, _, defValue, _, _, _) =>
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))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -715,16 +715,17 @@ case object DataflowAnalysis extends IRPass {
info: DependencyInfo
): DefinitionArgument = {
argument match {
case spec @ DefinitionArgument.Specified(_, _, defValue, _, _, _) =>
val specDep = asStatic(spec)
case spec: DefinitionArgument.Specified =>
val defValue = spec.defaultValue
val specDep = asStatic(spec)
defValue.foreach(expr => {
val exprDep = asStatic(expr)
info.dependents.updateAt(exprDep, Set(specDep))
info.dependencies.updateAt(specDep, Set(exprDep))
})

spec
.copy(
.copyWithDefaultValue(
defaultValue = defValue.map(analyseExpression(_, info))
)
.updateMetadata(new MetadataPair(this, info))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,9 @@ case object DemandAnalysis extends IRPass {
arg: DefinitionArgument
): DefinitionArgument = {
arg match {
case spec @ DefinitionArgument.Specified(_, _, default, _, _, _) =>
spec.copy(
case spec: DefinitionArgument.Specified =>
val default = spec.defaultValue
spec.copyWithDefaultValue(
defaultValue = default.map(x =>
analyseExpression(
x,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ case object GenerateMethodBodies extends IRPass {
* @return the `self` argument
*/
private def genSyntheticSelf(): DefinitionArgument.Specified = {
DefinitionArgument.Specified(
new DefinitionArgument.Specified(
Name.Self(identifiedLocation = null, synthetic = true),
None,
defaultValue = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ class LambdaShorthandToLambdaMini(

new Function.Lambda(
List(
DefinitionArgument.Specified(
name = Name.Literal(
new DefinitionArgument.Specified(
Name.Literal(
newName.name,
isMethod = false,
null
),
ascribedType = None,
defaultValue = None,
suspended = false,
identifiedLocation = null
None,
None,
false,
null
)
),
newName,
Expand Down Expand Up @@ -154,17 +154,13 @@ class LambdaShorthandToLambdaMini(
val resultExpr = if (functionIsShorthand) {
new Function.Lambda(
List(
DefinitionArgument.Specified(
new DefinitionArgument.Specified(
Name
.Literal(
updatedName.get,
isMethod = false,
fn.location.orNull
),
None,
None,
suspended = false,
null
)
)
),
appResult,
Expand Down Expand Up @@ -196,13 +192,7 @@ class LambdaShorthandToLambdaMini(
val locWithoutId =
newVec.location.map(l => new IdentifiedLocation(l.location()))
bindings.foldLeft(newVec: Expression) { (body, bindingName) =>
val defArg = 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)
}

Expand Down Expand Up @@ -329,7 +319,7 @@ class LambdaShorthandToLambdaMini(
diagnostics = nameBlank.diagnostics
)

val lambdaArg = DefinitionArgument.Specified(
val lambdaArg = new DefinitionArgument.Specified(
scrutineeName.copy(id = null),
None,
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ object NoSelfInStatic extends IRPass {
)
}

private def isSelfName(name: Name): Boolean = {
name match {
case self: Name.Self => !self.synthetic
case _ => false
}
}

/** A method is static if it is either not defined within a type, or if it does not
* contain a non-synthetic `self` argument.
*/
Expand All @@ -67,14 +74,7 @@ object NoSelfInStatic extends IRPass {
arguments: List[DefinitionArgument]
): Option[DefinitionArgument] = {
arguments.collectFirst {
case arg @ DefinitionArgument.Specified(
Name.Self(_, false, _),
_,
_,
_,
_,
_
) =>
case arg: DefinitionArgument.Specified if isSelfName(arg.name) =>
arg
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,23 +194,18 @@ case object UnusedBindings extends IRPass {
val isUsed = aliasInfo.graph.linksFor(aliasInfo.id).nonEmpty

argument match {
case s @ DefinitionArgument.Specified(
_: Name.Self,
_,
_,
_,
_,
_
) =>
case s: DefinitionArgument.Specified if s.name.isInstanceOf[Name.Self] =>
s
case s @ DefinitionArgument.Specified(name, _, default, _, _, _) =>
case s: DefinitionArgument.Specified =>
val name = s.name
val default = s.defaultValue
if (!isIgnored && !isUsed) {
val nameToReport = name match {
case literal: Name.Literal =>
literal.originalName.getOrElse(literal)
case _ => name
}
s.copy(
s.copyWithDefaultValue(
defaultValue = default.map(runExpression(_, context))
).addDiagnostic(warnings.Unused.FunctionArgument(nameToReport))
} else s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,11 @@ case object LambdaConsolidate extends IRPass {
if (isShadowed) {
val restArgs = args.drop(ix + 1)
arg match {
case spec @ DefinitionArgument.Specified(argName, _, _, _, _, _) =>
case spec: DefinitionArgument.Specified =>
val argName = spec.name
val mShadower = restArgs.collectFirst {
case s @ DefinitionArgument.Specified(sName, _, _, _, _, _)
if sName.name == argName.name =>
case s: DefinitionArgument.Specified
if s.name.name == argName.name =>
s
}

Expand Down Expand Up @@ -404,6 +405,7 @@ case object LambdaConsolidate extends IRPass {
} else Set[UUID @Identifier]()

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

Expand Down Expand Up @@ -434,7 +436,8 @@ case object LambdaConsolidate extends IRPass {
)
} else oldName

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

Expand Down Expand Up @@ -467,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,11 @@ case object IgnoredBindings extends IRPass {
freshNameSupply: FreshNameSupply
): DefinitionArgument = {
arg match {
case spec @ DefinitionArgument.Specified(
Name.Self(_, _, _),
_,
_,
_,
_,
_
) =>
case spec: DefinitionArgument.Specified
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))
Expand All @@ -244,9 +238,8 @@ case object IgnoredBindings extends IRPass {
} else {
setNotIgnored(
spec
.copy(
defaultValue =
spec.defaultValue.map(resolveExpression(_, freshNameSupply))
.copyWithDefaultValue(
spec.defaultValue.map(resolveExpression(_, freshNameSupply))
)
)
}
Expand All @@ -260,8 +253,8 @@ case object IgnoredBindings extends IRPass {
*/
private def isIgnoreArg(ir: DefinitionArgument): Boolean = {
ir match {
case DefinitionArgument.Specified(name, _, _, _, _, _) =>
isIgnore(name)
case spec: DefinitionArgument.Specified =>
isIgnore(spec.name)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,20 +224,15 @@ case object TypeSignatures extends IRPass {
argument: DefinitionArgument
): DefinitionArgument =
argument match {
case specified @ DefinitionArgument.Specified(
_,
Some(ascribedType),
_,
_,
_,
_
) =>
val sig = resolveExpression(ascribedType.duplicate())
specified.copy(
name = specified.name.updateMetadata(
case specified: DefinitionArgument.Specified
if specified.ascribedType.isDefined =>
val ascribedType = specified.ascribedType.get
val sig = resolveExpression(ascribedType.duplicate())
specified.copyWithNameAndAscribedType(
specified.name.updateMetadata(
new MetadataPair(this, Signature(sig))
),
ascribedType = Some(
Some(
ascribedType.updateMetadata(new MetadataPair(this, Signature(sig)))
)
)
Expand Down
Loading
Loading