-
Notifications
You must be signed in to change notification settings - Fork 326
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
Use GraphBuilder to construct an alias Graph #11491
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
...ntime-compiler/src/main/java/org/enso/compiler/pass/analyse/alias/graph/GraphBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package org.enso.compiler.pass.analyse.alias.graph; | ||
|
||
/** | ||
* Builder of {@link Graph}. Separates the concerns of building a graph of local symbol definitions | ||
* and their usages from the actual querying of those symbols. | ||
*/ | ||
public final class GraphBuilder { | ||
private final Graph graph; | ||
private final Graph.Scope scope; | ||
|
||
private GraphBuilder(Graph graph, Graph.Scope scope) { | ||
this.graph = graph; | ||
this.scope = scope; | ||
} | ||
|
||
/** | ||
* Creates new empty builder. | ||
* | ||
* @return empty builder | ||
*/ | ||
public static GraphBuilder create() { | ||
var topLevel = Graph$.MODULE$.create(); | ||
return create(topLevel, topLevel.rootScope()); | ||
} | ||
|
||
/** | ||
* Creates a builder for given graph and scope. | ||
* | ||
* @param g the graph | ||
* @param s its scope | ||
* @return builder operating on the graph {@code g} starting at scope {@code s} | ||
*/ | ||
public static GraphBuilder create(Graph g, Graph.Scope s) { | ||
return new GraphBuilder(g, s); | ||
} | ||
|
||
/** | ||
* Creates a child scope and returns a builder for it. | ||
* | ||
* @return new builder for newly created scope, but the same graph | ||
*/ | ||
public GraphBuilder addChild() { | ||
return new GraphBuilder(graph, scope.addChild()); | ||
} | ||
|
||
/** | ||
* Adds occurrence to current scope. | ||
* | ||
* @return this builder with modified scope | ||
*/ | ||
public GraphBuilder add(GraphOccurrence occ) { | ||
this.scope.add(occ); | ||
return this; | ||
} | ||
|
||
/** | ||
* Adds definition to current scope. | ||
* | ||
* @return this builder with modified scope | ||
*/ | ||
public GraphBuilder addDefinition(GraphOccurrence.Def def) { | ||
this.scope.addDefinition(def); | ||
return this; | ||
} | ||
|
||
/** | ||
* Finds definition ID of provided symbol. | ||
* | ||
* @param name the name of the symbol | ||
* @return -1 if not such symbol found, otherwise ID of the symbol | ||
*/ | ||
public int findDef(String name) { | ||
var first = this.scope.occurrences().values().find(occ -> occ.symbol().equals(name)); | ||
if (first.nonEmpty() && first.get() instanceof GraphOccurrence.Def def) { | ||
return def.id(); | ||
} else { | ||
return -1; | ||
} | ||
} | ||
|
||
/** Creates new definition for */ | ||
public GraphOccurrence.Def newDef( | ||
String symbol, java.util.UUID identifier, scala.Option<java.util.UUID> externalId) { | ||
return newDef(symbol, identifier, externalId, false); | ||
} | ||
|
||
public GraphOccurrence.Def newDef( | ||
String symbol, | ||
java.util.UUID identifier, | ||
scala.Option<java.util.UUID> externalId, | ||
boolean suspended) { | ||
return new GraphOccurrence.Def(graph.nextId(), symbol, identifier, externalId, suspended); | ||
} | ||
|
||
/** Factory method to create new [GraphOccurrence.Use]. */ | ||
public GraphOccurrence.Use newUse( | ||
String symbol, java.util.UUID identifier, scala.Option<java.util.UUID> externalId) { | ||
return new GraphOccurrence.Use(graph.nextId(), symbol, identifier, externalId); | ||
} | ||
|
||
public void resolveLocalUsage(GraphOccurrence.Use use) { | ||
graph.resolveLocalUsage(use); | ||
} | ||
|
||
public Graph toGraph() { | ||
return graph; | ||
} | ||
|
||
public Graph.Scope toScope() { | ||
return scope; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we ensure that
toGraph
was called just once? Looking atAliasAnalysis
, it seems to me thattoGraph
creates snapshots of a graph, i.e., the builder is reusable?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried that, but it is probably not going to work. For example processing arguments is trying to modify a builder that has already been queried for a graph/scope:
Maybe it doesn't matter. The goal of the
GraphBuilder
was to identify the code that can modify theGraph.Scope
- which has been achieved. All such code needs theGraphBuilder
.