Skip to content

Commit

Permalink
Towards shallow nesting in IR (#7681)
Browse files Browse the repository at this point in the history
Refactoring deeply nested IR classes to shallow nesting.
Fixes #7017

# Important Notes
It's a big PR but fairly consistent. Split multiple hierarchy levels into subpackages.
  • Loading branch information
hubertp authored Aug 31, 2023
1 parent 73b8646 commit 24f263b
Show file tree
Hide file tree
Showing 161 changed files with 13,207 additions and 12,493 deletions.
3 changes: 1 addition & 2 deletions docs/runtime/caching.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,7 @@ typesetMember <- memberValue

#### Typing Operators

All typing operators in Enso (`IR.Type`) are dependent on their constituent
parts:
All typing operators in Enso (`Type`) are dependent on their constituent parts:

```
typingExpr <- expressionChildren
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import com.oracle.truffle.api.source.Source
import java.util.UUID
import org.enso.compiler.core.EnsoParser
import org.enso.compiler.core.IR
import org.enso.compiler.core.ir.Literal
import org.enso.compiler.core.ir.Name
import org.enso.compiler.core.ir.module.scope.definition
import org.enso.compiler.core.CompilerError
import org.enso.compiler.pass.analyse.DataflowAnalysis
import org.enso.interpreter.instrument.execution.model.PendingEdit
Expand Down Expand Up @@ -80,7 +83,7 @@ final class ChangesetBuilder[A: TextEditor: IndexedSource](
ir.preorder.filter(_.getExternalId == directlyAffectedId)
val oldIr = literals.head

def newIR(edit: PendingEdit): Option[IR.Literal] = {
def newIR(edit: PendingEdit): Option[Literal] = {
val value = edit match {
case pending: PendingEdit.SetExpressionValue => pending.value
case other: PendingEdit.ApplyEdit => other.edit.text
Expand All @@ -91,16 +94,16 @@ final class ChangesetBuilder[A: TextEditor: IndexedSource](
compiler
.generateIRInline(compiler.parse(source.getCharacters()))
.flatMap(_ match {
case ir: IR.Literal => Some(ir.setLocation(oldIr.location))
case _ => None
case ir: Literal => Some(ir.setLocation(oldIr.location))
case _ => None
})
}.get
}

oldIr match {
case node: IR.Literal.Number =>
case node: Literal.Number =>
newIR(pending).map(ir => new SimpleUpdate(node, pending.edit, ir))
case node: IR.Literal.Text =>
case node: Literal.Text =>
newIR(pending).map(ir => new SimpleUpdate(node, pending.edit, ir))
case _ => None
}
Expand Down Expand Up @@ -322,8 +325,8 @@ object ChangesetBuilder {

/** Get the IR name if available. */
private def getName(ir: IR): Option[String] = ir match {
case name: IR.Name => Some(name.name)
case _ => None
case name: Name => Some(name.name)
case _ => None
}

/** Build an internal representation of the [[IR]].
Expand Down Expand Up @@ -511,9 +514,9 @@ object ChangesetBuilder {
*/
private def getExpressionName(ir: IR, id: IR.Identifier): Option[String] =
ir.preorder.find(_.getId == id).collect {
case name: IR.Name =>
case name: Name =>
name.name
case method: IR.Module.Scope.Definition.Method =>
case method: definition.Method =>
method.methodName.name
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.enso.interpreter.instrument.execution

import com.oracle.truffle.api.source.SourceSection
import org.enso.compiler.core.IR
import org.enso.compiler.core.ir.IdentifiedLocation
import org.enso.interpreter.runtime.Module
import org.enso.syntax.text.Location
import org.enso.text.editing.{model, IndexedSource}
Expand Down Expand Up @@ -58,7 +59,7 @@ object LocationResolver {
*/
def getExpressionId(
ir: IR,
location: IR.IdentifiedLocation
location: IdentifiedLocation
): Option[ExpressionId] =
ir.preorder
.find(_.location.contains(location))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import com.oracle.truffle.api.TruffleLogger
import org.enso.compiler.CompilerResult
import org.enso.compiler.context._
import org.enso.compiler.core.CompilerError
import org.enso.compiler.core.IR
import org.enso.compiler.core.ir.{Diagnostic, Warning}
import org.enso.compiler.core.ir.expression.Error
import org.enso.compiler.pass.analyse.{
CachePreferenceAnalysis,
GatherDiagnostics
Expand Down Expand Up @@ -177,9 +178,9 @@ final class EnsureCompiledJob(
)
.diagnostics
val diagnostics = pass.collect {
case warn: IR.Warning =>
case warn: Warning =>
createDiagnostic(Api.DiagnosticType.Warning, module, warn)
case error: IR.Error =>
case error: Error =>
createDiagnostic(Api.DiagnosticType.Error, module, error)
}
sendDiagnosticUpdates(diagnostics)
Expand All @@ -196,7 +197,7 @@ final class EnsureCompiledJob(
private def createDiagnostic(
kind: Api.DiagnosticType,
module: Module,
diagnostic: IR.Diagnostic
diagnostic: Diagnostic
): Api.ExecutionResult.Diagnostic = {
Api.ExecutionResult.Diagnostic(
kind,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.enso.interpreter.instrument.job

import org.enso.compiler.core.IR
import org.enso.compiler.core.ir.Name
import org.enso.compiler.refactoring.IRUtils
import org.enso.interpreter.instrument.execution.RuntimeContext
import org.enso.interpreter.instrument.execution.model.PendingEdit
Expand Down Expand Up @@ -160,16 +161,15 @@ final class RefactoringRenameJob(
}
}

private def getLiteral(ir: IR): Option[IR.Name.Literal] =
private def getLiteral(ir: IR): Option[Name.Literal] =
ir match {
case literal: IR.Name.Literal => Some(literal)
case _ => None
case literal: Name.Literal => Some(literal)
case _ => None
}

private def getMethodDefinition(ir: IR): Option[IR.Name] =
private def getMethodDefinition(ir: IR): Option[Name] =
ir match {
case methodRef: IR.Name.MethodReference
if methodRef.typePointer.isEmpty =>
case methodRef: Name.MethodReference if methodRef.typePointer.isEmpty =>
Some(methodRef.methodName)
case _ =>
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package org.enso.interpreter.instrument.job

import cats.implicits._
import com.oracle.truffle.api.TruffleLogger
import org.enso.compiler.core.IR
import org.enso.compiler.core.ir.Function
import org.enso.compiler.core.ir.Name
import org.enso.compiler.core.ir.module.scope.definition
import org.enso.compiler.pass.analyse.{
CachePreferenceAnalysis,
DataflowAnalysis
Expand Down Expand Up @@ -390,14 +392,14 @@ object UpsertVisualizationJob {
visualizationExpression match {
case Api.VisualizationExpression.ModuleMethod(methodPointer, _) =>
module.getIr.bindings
.collect { case method: IR.Module.Scope.Definition.Method =>
.collect { case method: definition.Method =>
val methodReference = method.methodReference
val methodReferenceName = methodReference.methodName.name
val methodReferenceTypeOpt = methodReference.typePointer.map(_.name)

val externalIdOpt = method.body match {
case fun: IR.Function => fun.body.getExternalId
case _ => method.getExternalId
case fun: Function => fun.body.getExternalId
case _ => method.getExternalId
}
externalIdOpt.filter { _ =>
methodReferenceName == methodPointer.name &&
Expand Down Expand Up @@ -479,7 +481,7 @@ object UpsertVisualizationJob {
module.getIr.preorder
.find(_.getExternalId.contains(expressionId))
.collect {
case name: IR.Name =>
case name: Name.Literal =>
DataflowAnalysis.DependencyInfo.Type
.Dynamic(name.name, Some(expressionId))
case ir =>
Expand Down
Loading

0 comments on commit 24f263b

Please sign in to comment.