Class Resolver
- java.lang.Object
-
- net.hydromatic.morel.compile.Resolver
-
public class Resolver extends Object
Converts AST expressions to Core expressions.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classResolver.FromResolverVisitor that convertsAst.FromtoCore.Fromby handling each subtype ofAst.FromStepcallingFromBuilderappropriately.private static classResolver.InitHelper for initialization.(package private) static classResolver.PatExpPattern and expression.(package private) static classResolver.ReferenceFinderVisitor that finds all references to unbound variables in an expression.(package private) static classResolver.ResolvedDatatypeDeclResolved datatype declaration.static classResolver.ResolvedDeclResolved declaration.(package private) classResolver.ResolvedValDeclResolved value declaration.
-
Field Summary
Fields Modifier and Type Field Description static com.google.common.collect.ImmutableMap<BuiltIn,Op>BUILT_IN_OP_MAPMap fromBuiltIn, toOp; the reverse ofOP_BUILT_IN_MAP, and needed when we convert an optimized expression back to human-readable Morel code.private Environmentenvprivate NameGeneratornameGeneratorstatic com.google.common.collect.ImmutableMap<Op,BuiltIn>OP_BUILT_IN_MAP(package private) TypeMaptypeMapprivate Map<Pair<Core.NamedPat,Type>,Core.NamedPat>variantIdMapContains variable declarations whose type at the point they are used is different (more specific) than in their declaration.
-
Constructor Summary
Constructors Modifier Constructor Description privateResolver(TypeMap typeMap, NameGenerator nameGenerator, Map<Pair<Core.NamedPat,Type>,Core.NamedPat> variantIdMap, Environment env)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidflatten(Map<Ast.Pat,Ast.Exp> matches, boolean flatten, Ast.Pat pat, Ast.Exp exp)private Core.ExpflattenLet(List<Ast.Decl> decls, Ast.Exp exp)private Core.NamedPatgetIdPat(Ast.Id id, Binding binding)Converts an Id that is a reference to a variable into an IdPat that represents its declaration.static Resolverof(TypeMap typeMap, Environment env)Creates a root Resolver.private booleanreferences(List<Resolver.PatExp> patExps)Returns whether any of the expressions inexpsreferences and of the variables defined inpats.private Resolver.ResolvedDeclresolve(Ast.Decl decl, List<Binding> bindings)private Resolver.ResolvedDatatypeDeclresolveDatatypeDecl(Ast.DatatypeDecl decl, List<Binding> bindings)private Resolver.ResolvedValDeclresolveValDecl(Ast.ValDecl valDecl, List<Binding> bindings)private BuiltIntoBuiltIn(Op op)private Core.AggregatetoCore(Ast.Aggregate aggregate, Collection<? extends Core.IdPat> groupKeys)private Core.ApplytoCore(Ast.Apply apply)private Core.CasetoCore(Ast.Case case_)private DataTypetoCore(Ast.DatatypeBind bind)Core.DatatypeDecltoCore(Ast.DatatypeDecl datatypeDecl)Core.DecltoCore(Ast.Decl node)private Core.ExptoCore(Ast.Exp exp)private Core.FntoCore(Ast.Fn fn)(package private) Core.ExptoCore(Ast.From from)private Core.IdtoCore(Ast.Id id)private Core.CasetoCore(Ast.If if_)private Core.ApplytoCore(Ast.InfixCall call)private Core.ExptoCore(Ast.Let let)private Core.ExptoCore(Ast.ListExp list)private Core.MatchtoCore(Ast.Match match)private Core.OrderItemtoCore(Ast.OrderItem orderItem)private Core.PattoCore(Ast.Pat pat)private Core.PattoCore(Ast.Pat pat, Type targetType)private Core.PattoCore(Ast.Pat pat, Type type, Type targetType)Converts a pattern to Core.private Core.TupletoCore(Ast.Record record)private Core.RecordSelectortoCore(Ast.RecordSelector recordSelector)private Core.TupletoCore(Ast.Tuple tuple)Core.ValDecltoCore(Ast.ValDecl valDecl)private Core.ExptoCoreFromEq(Ast.Exp exp)Translates "x" in "from e = x".private Core.IdPattoCorePat(Ast.Id id)Converts an id in a declaration to Core.private static <E,T>
com.google.common.collect.ImmutableList<T>transform(Iterable<? extends E> elements, Function<E,T> mapper)ResolverwithEnv(Iterable<Binding> bindings)Binds a Resolver to an environment that consists of the current environment plus some bindings.ResolverwithEnv(Environment env)Binds a Resolver to a new environment.
-
-
-
Field Detail
-
OP_BUILT_IN_MAP
public static final com.google.common.collect.ImmutableMap<Op,BuiltIn> OP_BUILT_IN_MAP
-
BUILT_IN_OP_MAP
public static final com.google.common.collect.ImmutableMap<BuiltIn,Op> BUILT_IN_OP_MAP
Map fromBuiltIn, toOp; the reverse ofOP_BUILT_IN_MAP, and needed when we convert an optimized expression back to human-readable Morel code.
-
typeMap
final TypeMap typeMap
-
nameGenerator
private final NameGenerator nameGenerator
-
env
private final Environment env
-
variantIdMap
private final Map<Pair<Core.NamedPat,Type>,Core.NamedPat> variantIdMap
Contains variable declarations whose type at the point they are used is different (more specific) than in their declaration.For example, the infix operator "op +" has type "α * α →" in the base environment, but at point of use might instead be "int * int → int". This map will contain a new
Core.IdPatfor all points that use it with that second type. Effectively, it is a phantom declaration, in aletthat doesn't exist. Without this shared declaration, all points have their own distinctCore.IdPat, which theAnalyzerwill think is used just once.
-
-
Constructor Detail
-
Resolver
private Resolver(TypeMap typeMap, NameGenerator nameGenerator, Map<Pair<Core.NamedPat,Type>,Core.NamedPat> variantIdMap, Environment env)
-
-
Method Detail
-
of
public static Resolver of(TypeMap typeMap, Environment env)
Creates a root Resolver.
-
withEnv
public Resolver withEnv(Environment env)
Binds a Resolver to a new environment.
-
withEnv
public final Resolver withEnv(Iterable<Binding> bindings)
Binds a Resolver to an environment that consists of the current environment plus some bindings.
-
transform
private static <E,T> com.google.common.collect.ImmutableList<T> transform(Iterable<? extends E> elements, Function<E,T> mapper)
-
toCore
public Core.ValDecl toCore(Ast.ValDecl valDecl)
Converts a simpleAst.ValDecl, of the formval v = e, to a CoreCore.ValDecl.Declarations such as
val (x, y) = (1, 2)andval emp :: rest = empsare considered complex, and are not handled by this method.Likewise recursive declarations.
-
toCore
public Core.DatatypeDecl toCore(Ast.DatatypeDecl datatypeDecl)
-
resolve
private Resolver.ResolvedDecl resolve(Ast.Decl decl, List<Binding> bindings)
-
resolveDatatypeDecl
private Resolver.ResolvedDatatypeDecl resolveDatatypeDecl(Ast.DatatypeDecl decl, List<Binding> bindings)
-
resolveValDecl
private Resolver.ResolvedValDecl resolveValDecl(Ast.ValDecl valDecl, List<Binding> bindings)
-
references
private boolean references(List<Resolver.PatExp> patExps)
Returns whether any of the expressions inexpsreferences and of the variables defined inpats.This method is used to decide whether it is safe to convert a recursive declaration into a non-recursive one.
-
toCore
private DataType toCore(Ast.DatatypeBind bind)
-
toCorePat
private Core.IdPat toCorePat(Ast.Id id)
Converts an id in a declaration to Core.
-
getIdPat
private Core.NamedPat getIdPat(Ast.Id id, Binding binding)
Converts an Id that is a reference to a variable into an IdPat that represents its declaration.
-
toCore
private Core.Tuple toCore(Ast.Tuple tuple)
-
toCore
private Core.Tuple toCore(Ast.Record record)
-
toCore
private Core.Exp toCore(Ast.ListExp list)
-
toCoreFromEq
private Core.Exp toCoreFromEq(Ast.Exp exp)
Translates "x" in "from e = x". Desugar to the same as if they had written "from e in [x]".
-
toCore
private Core.Apply toCore(Ast.Apply apply)
-
toCore
private Core.RecordSelector toCore(Ast.RecordSelector recordSelector)
-
toCore
private Core.Apply toCore(Ast.InfixCall call)
-
flatten
private void flatten(Map<Ast.Pat,Ast.Exp> matches, boolean flatten, Ast.Pat pat, Ast.Exp exp)
-
toCore
private Core.Pat toCore(Ast.Pat pat, Type type, Type targetType)
Converts a pattern to Core.Expands a pattern if it is a record pattern that has an ellipsis or if the arguments are not in the same order as the labels in the type.
-
toCore
private Core.Match toCore(Ast.Match match)
-
toCore
private Core.Aggregate toCore(Ast.Aggregate aggregate, Collection<? extends Core.IdPat> groupKeys)
-
toCore
private Core.OrderItem toCore(Ast.OrderItem orderItem)
-
-