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

Fill in line numbers for IR.bgv files #12218

Merged
merged 4 commits into from
Feb 8, 2025
Merged
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
@@ -1,51 +1,37 @@
package org.enso.compiler.dump.igv;

import java.io.File;
import java.net.URI;
import org.enso.compiler.core.IR;
import org.enso.compiler.core.ir.IdentifiedLocation;
import org.enso.compiler.dump.service.IRSource;

final class ASTLocation {
private final int lineNum;
// May be null
private final URI locationUri;
private final int offsetStart;
private final int offsetEnd;

private ASTLocation(int lineNum, URI locationUri, int offsetStart, int offsetEnd) {
this.lineNum = lineNum;
this.locationUri = locationUri;
this.offsetStart = offsetStart;
this.offsetEnd = offsetEnd;
private final IRSource<? extends IR> ctx;
private final IdentifiedLocation loc;

private ASTLocation(IRSource<? extends IR> ctx, IdentifiedLocation loc) {
this.ctx = ctx;
this.loc = loc;
}

public static ASTLocation fromIdentifiedLocation(IdentifiedLocation loc, File srcFile) {
int offStart = -1;
int offEnd = -1;
int lineNum = 1;
URI uri = null;
if (loc != null) {
offStart = loc.start();
offEnd = loc.end();
}
if (srcFile != null) {
uri = srcFile.toURI();
}
return new ASTLocation(lineNum, uri, offStart, offEnd);
public static ASTLocation fromIdentifiedLocation(
IdentifiedLocation loc, IRSource<? extends IR> ctx) {
return new ASTLocation(ctx, loc);
}

public int getLineNum() {
return lineNum;
return loc == null ? -1 : ctx.lineMap().apply(loc);
}

public int getOffsetStart() {
return offsetStart;
return loc == null ? -1 : loc.start();
}

public int getOffsetEnd() {
return offsetEnd;
return loc == null ? -1 : loc.end();
}

public URI getLocationUri() {
return locationUri;
return ctx.loc();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.enso.compiler.dump.igv;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
Expand All @@ -9,6 +8,7 @@
import java.util.stream.Collectors;
import org.enso.compiler.core.IR;
import org.enso.compiler.core.ir.MetadataStorage;
import org.enso.compiler.dump.service.IRSource;

final class ASTNode {

Expand Down Expand Up @@ -72,10 +72,10 @@ static final class Builder {
private final Map<String, Object> properties = new LinkedHashMap<>();
private ASTLocation location;

public static Builder fromIr(IR ir, File srcFile) {
public static Builder fromIr(IR ir, IRSource<? extends IR> ctx) {
var bldr = new Builder();
var label = Utils.label(ir);
var location = ASTLocation.fromIdentifiedLocation(ir.identifiedLocation(), srcFile);
var location = ASTLocation.fromIdentifiedLocation(ir.identifiedLocation(), ctx);
bldr.object = ir;
bldr.location = location;
bldr.property("label", label);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.enso.compiler.dump.igv;

import java.io.File;
import java.net.URI;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -28,6 +28,7 @@
import org.enso.compiler.core.ir.module.scope.Import;
import org.enso.compiler.core.ir.module.scope.definition.Method;
import org.enso.compiler.core.ir.module.scope.imports.Polyglot;
import org.enso.compiler.dump.service.IRSource;

/**
* Implemented only for dumping the AST to IGV. Heavily inspired by the internal {@code
Expand All @@ -38,10 +39,8 @@ final class EnsoModuleAST {

private final ASTNode root;

/** Underlying source file. May be null. */
private final File srcFile;

private final String moduleName;
/** Information about underlying source. */
private final IRSource<? extends IR> ctx;

private final Map<Integer, ASTNode> nodes = new HashMap<>();

Expand All @@ -53,42 +52,33 @@ final class EnsoModuleAST {

private final Map<UUID, Integer> nodeIds;

private EnsoModuleAST(
Module moduleIr, File srcFile, String moduleName, Map<UUID, Integer> nodeIds) {
this.nodeIds = nodeIds;
this.srcFile = srcFile;
this.moduleName = moduleName;
this.root = buildTree(moduleIr);
}

private EnsoModuleAST(Expression expr, String moduleName, Map<UUID, Integer> nodeIds) {
private EnsoModuleAST(IRSource<? extends IR> ctx, Map<UUID, Integer> nodeIds) {
this.nodeIds = nodeIds;
this.srcFile = null;
this.moduleName = moduleName;
this.root = buildTree(expr);
assert ctx != null;
this.ctx = ctx;
this.root = switch (ctx.ir()) {
case Module m -> buildTree(m);
case Expression e -> buildTree(e);
case null, default -> throw new IllegalArgumentException("ir: " + ctx.ir());
};
}

/**
* @param srcFile Source file for the module. May be null.
* @param moduleName FQN of the module.
* @param nodeIds Mapping of IR node UUIDs to sequential IDs expected by the IGV.
*/
static EnsoModuleAST fromModuleIR(
Module module, File srcFile, String moduleName, Map<UUID, Integer> nodeIds) {
return new EnsoModuleAST(module, srcFile, moduleName, nodeIds);
}

static EnsoModuleAST fromExpressionIR(
Expression expr, String moduleName, Map<UUID, Integer> nodeIds) {
return new EnsoModuleAST(expr, moduleName, nodeIds);
static EnsoModuleAST create(
IRSource<? extends IR> ctx, Map<UUID, Integer> nodeIds) {
return new EnsoModuleAST(ctx, nodeIds);
}

public File getSrcFile() {
return srcFile;
public URI getSrcFile() {
return ctx.loc();
}

public String getModuleName() {
return moduleName;
return ctx.name();
}

public List<ASTNode> getNodes() {
Expand Down Expand Up @@ -463,7 +453,7 @@ private ASTNode buildTree(DefinitionArgument argument) {
}

private ASTNode newNode(IR ir, Map<String, Object> props) {
ASTNode.Builder bldr = ASTNode.Builder.fromIr(ir, srcFile);
ASTNode.Builder bldr = ASTNode.Builder.fromIr(ir, ctx);
var nodeId = nodeIds.get(ir.getId());
if (nodeId == null) {
var lastSeqId = nodeIds.size();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.enso.compiler.dump.igv;

import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
Expand All @@ -16,6 +15,7 @@
import org.enso.compiler.core.ir.Expression;
import org.enso.compiler.core.ir.Module;
import org.enso.compiler.dump.service.IRDumper;
import org.enso.compiler.dump.service.IRSource;
import org.graalvm.graphio.GraphOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -50,7 +50,7 @@ private GraphOutput<EnsoModuleAST, ASTMethod> graphOutput() {
.blocks(EnsoModuleAST.AST_DUMP_STRUCTURE)
.elementsAndLocations(
EnsoModuleAST.AST_DUMP_STRUCTURE, EnsoModuleAST.AST_DUMP_STRUCTURE)
.attr("type", "Enso IR")
.attr("ensoIrVersion", "1.0")
.build(channel);
} catch (IOException e) {
throw new IllegalStateException(
Expand All @@ -74,26 +74,22 @@ private static WritableByteChannel createChannel(String moduleName) {
}

@Override
public void dumpModule(Module ir, String graphName, File srcFile, String afterPass) {
assert graphName.equals(this.moduleName);
dumpTask(ir, graphName, srcFile, afterPass);
public void dumpModule(IRSource<Module> ctx) {
assert ctx.name().equals(this.moduleName);
dumpTask(ctx);
}

@Override
public void dumpExpression(Expression expr, String graphName, String afterPass) {
dumpTask(expr, graphName, null, afterPass);
public void dumpExpression(IRSource<Expression> ctx) {
dumpTask(ctx);
}

private void dumpTask(IR ir, String moduleName, File srcFile, String afterPass) {
@SuppressWarnings("unchecked")
private void dumpTask(IRSource<? extends IR> ctx) {
var ir = ctx.ir();
var afterPass = ctx.afterPass();
LOGGER.trace("[{}] Creating EnsoModuleAST after pass {}", moduleName, afterPass);
EnsoModuleAST moduleAst;
if (ir instanceof Module moduleIr) {
moduleAst = EnsoModuleAST.fromModuleIR(moduleIr, srcFile, moduleName, nodeIds);
} else if (ir instanceof Expression expr) {
moduleAst = EnsoModuleAST.fromExpressionIR(expr, moduleName, nodeIds);
} else {
throw new IllegalArgumentException("Unsupported IR type: " + ir.getClass());
}
var moduleAst = EnsoModuleAST.create(ctx, nodeIds);
try {
if (!groupCreated) {
var groupProps = groupProps(moduleName, moduleAst);
Expand All @@ -115,7 +111,7 @@ private static Map<String, Object> groupProps(String moduleName, EnsoModuleAST g
var props = new HashMap<String, Object>();
props.put("moduleName", moduleName);
props.put("date", LocalDateTime.now());
props.put("srcFile", graph.getSrcFile() == null ? null : graph.getSrcFile().getAbsolutePath());
props.put("srcFile", graph.getSrcFile() == null ? null : graph.getSrcFile().toString());
return props;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.enso.compiler.dump.service;

import java.io.File;
import java.util.ServiceLoader;
import org.enso.compiler.core.ir.Module;

Expand Down Expand Up @@ -29,7 +28,7 @@ public IRDumper create(String moduleName) {
public void shutdown() {}

@Override
public void dumpModule(Module ir, String graphName, File srcFile, String afterPass) {}
public void dumpModule(IRSource<Module> ir) {}

@Override
public void close() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.enso.compiler.dump.service;

import java.io.File;
import org.enso.compiler.core.ir.Expression;
import org.enso.compiler.core.ir.Module;

Expand All @@ -9,22 +8,12 @@ public interface IRDumper {
/**
* Dumps module IR.
*
* @param ir IR of the module to dump
* @param graphName Name of the graph to be dumped. Usually a fully-qualified module name.
* @param srcFile Source file of the module. May be null.
* @param afterPass Name of the pass that this dumper runs after. Corresponds to name of the
* subgraph.
* @param dump what to dump
*/
void dumpModule(Module ir, String graphName, File srcFile, String afterPass);
void dumpModule(IRSource<Module> dump);

/**
* Dumps a single expression IR.
*
* @param expr the IR.
* @param graphName Name of the graph to be dumped. May contain spaces.
* @param afterPass Name of the subgraph.
*/
default void dumpExpression(Expression expr, String graphName, String afterPass) {
/** Dumps a single expression IR. */
default void dumpExpression(IRSource<Expression> dump) {
// nop
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.enso.compiler.dump.service;

import java.net.URI;
import java.util.function.Function;
import org.enso.compiler.core.IR;
import org.enso.compiler.core.ir.IdentifiedLocation;

/** Information about an IR dumping. */
public record IRSource<AnIR extends IR>(
AnIR ir,
String name,
String afterPass,
URI loc,
Function<IdentifiedLocation, Integer> lineMap) {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.PrintStream;
import java.net.URI;
import java.util.List;
import java.util.concurrent.Future;
import java.util.function.Consumer;
Expand All @@ -12,6 +13,7 @@
import org.enso.compiler.Passes;
import org.enso.compiler.core.CompilerStub;
import org.enso.compiler.core.ir.Diagnostic;
import org.enso.compiler.core.ir.IdentifiedLocation;
import org.enso.compiler.data.BindingsMap;
import org.enso.compiler.data.CompilerConfig;
import org.enso.compiler.data.IdMap;
Expand Down Expand Up @@ -138,8 +140,12 @@ public abstract static class Module {

public abstract CharSequence getCharacters() throws IOException;

public abstract int findLine(IdentifiedLocation loc);

public abstract String getPath();

public abstract URI getUri();

public abstract Package<? extends Object> getPackage();

public abstract QualifiedName getName();
Expand Down
Loading
Loading