Skip to content

Commit

Permalink
documentation and minor code cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
hubertp committed Jul 28, 2022
1 parent 142f8d1 commit d6a9d42
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ public static Module empty(QualifiedName name, Package<TruffleFile> pkg) {
return new Module(name, pkg, false, null);
}

/**
* Creates a virtual module which only purpose is to export symbols of other modules.
*
* @param name the qualified name of the newly created module.
* @param pkg the package this module belongs to. May be {@code null}, if the module does not
* belong to a package.
* @param source source of the module declaring exports of the desired modules
* @return the virtual module
*/
public static Module virtual(QualifiedName name, Package<TruffleFile> pkg, Rope source) {
return new Module(name, pkg, true, source);
}
Expand Down Expand Up @@ -228,9 +237,9 @@ public void setDirectVirtualModulesRefs(List<QualifiedName> names) {
}

/**
* Return a list of directly referencing virtual modules of this one, if any.
* Return a list of directly referencing submodules of this one, if any.
*
* @return a non-null, possibly empty, array of fully qualified names of modules
* @return a non-null, possibly empty, list of fully qualified names of modules
*/
public List<QualifiedName> getDirectVirtualModulesRefs() {
if (directVirtualModulesRefs == null) {
Expand Down
15 changes: 10 additions & 5 deletions engine/runtime/src/main/scala/org/enso/compiler/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,10 @@ class Compiler(
val parsedAST = parse(module.getSource)
val expr = generateIR(parsedAST)
val exprWithModuleExports =
injectVirtualModuleExports(expr, module.getDirectVirtualModulesRefs)
if (module.isVirtual)
expr
else
injectVirtualModuleExports(expr, module.getDirectVirtualModulesRefs)
val discoveredModule =
recognizeBindings(exprWithModuleExports, moduleContext)
module.unsafeSetIr(discoveredModule)
Expand Down Expand Up @@ -586,7 +589,7 @@ class Compiler(
* ````
*
* @param ir IR to be enhanced
* @param modules fully qualified names pf modules
* @param modules fully qualified names of modules
* @return enhanced
*/
private def injectVirtualModuleExports(
Expand All @@ -596,9 +599,11 @@ class Compiler(
import scala.jdk.CollectionConverters._

val moduleNames = modules.asScala.map { q =>
val name = q.path.map(
IR.Name.Literal(_, isMethod = false, location = None)
) ++ List(IR.Name.Literal(q.item, isMethod = false, location = None))
val name = q.path.foldRight(
List(IR.Name.Literal(q.item, isMethod = false, location = None))
) { case (part, acc) =>
IR.Name.Literal(part, isMethod = false, location = None) :: acc
}
IR.Name.Qualified(name, location = None)
}.toList
ir.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,8 @@ object PackageRepository {
loadedModules.put(virtualModule.getName.toString, virtualModule)
} else {
val loaded = loadedModules(virtualModule.getName.toString)
if (!loaded.isVirtual) {
loaded.setDirectVirtualModulesRefs(refs.asJava)
}
assert(!loaded.isVirtual)
loaded.setDirectVirtualModulesRefs(refs.asJava)
}
}

Expand Down
8 changes: 8 additions & 0 deletions engine/runtime/src/main/scala/org/enso/compiler/core/IR.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6493,6 +6493,14 @@ object IR {
Array(shadowedName, shadower)
}

/** A warning that a submodule is being shadowed by the type of the same name
* therefore preventing the user from accessing the module via a qualified name.
*
* @param typename the type name shadowing the module
* @param moduleName the module being shadowed
* @param shadower the expression shadowing `moduleName`
* @param location the location at which the shadowing takes place
*/
sealed case class VirtualModule(
typeName: String,
moduleName: IR.Name.Qualified,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ case class BindingsMap(
candidates.distinct match {
case List() => Left(ResolutionNotFound)
case List(it) => Right(it)
case items =>
Left(ResolutionAmbiguous(items))
case items => Left(ResolutionAmbiguous(items))
}
}

Expand Down

0 comments on commit d6a9d42

Please sign in to comment.