Skip to content

Commit

Permalink
Add callback object to Context capable of recording progress
Browse files Browse the repository at this point in the history
  • Loading branch information
bishabosha committed Oct 24, 2023
1 parent 15033c7 commit 721888d
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 6 deletions.
19 changes: 17 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Contexts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import scala.annotation.internal.sharable

import DenotTransformers.DenotTransformer
import dotty.tools.dotc.profile.Profiler
import dotty.tools.dotc.sbt.interfaces.IncrementalCallback
import dotty.tools.dotc.sbt.interfaces.{IncrementalCallback, ProgressCallback}
import util.Property.Key
import util.Store
import plugins._
Expand All @@ -53,8 +53,9 @@ object Contexts {
private val (notNullInfosLoc, store8) = store7.newLocation[List[NotNullInfo]]()
private val (importInfoLoc, store9) = store8.newLocation[ImportInfo | Null]()
private val (typeAssignerLoc, store10) = store9.newLocation[TypeAssigner](TypeAssigner)
private val (progressCallbackLoc, store11) = store10.newLocation[ProgressCallback | Null]()

private val initialStore = store10
private val initialStore = store11

/** The current context */
inline def ctx(using ctx: Context): Context = ctx
Expand Down Expand Up @@ -177,6 +178,19 @@ object Contexts {
val local = incCallback
local != null && local.enabled || forceRun

/** The Zinc compile progress callback implementation if we are run from Zinc, null otherwise */
def progressCallback: ProgressCallback | Null = store(progressCallbackLoc)

/** Run `op` if there exists a Zinc progress callback */
inline def withProgressCallback(inline op: ProgressCallback => Unit): Unit =
val local = progressCallback
if local != null then op(local)

def cancelSignalRecorded: Boolean =
val local = progressCallback
val noSignalRecieved = local == null || !local.isCancelled
!noSignalRecieved // if true then cancel request was recorded

/** The current plain printer */
def printerFn: Context => Printer = store(printerFnLoc)

Expand Down Expand Up @@ -675,6 +689,7 @@ object Contexts {

def setCompilerCallback(callback: CompilerCallback): this.type = updateStore(compilerCallbackLoc, callback)
def setIncCallback(callback: IncrementalCallback): this.type = updateStore(incCallbackLoc, callback)
def setProgressCallback(callback: ProgressCallback): this.type = updateStore(progressCallbackLoc, callback)
def setPrinterFn(printer: Context => Printer): this.type = updateStore(printerFnLoc, printer)
def setSettings(settingsState: SettingsState): this.type = updateStore(settingsStateLoc, settingsState)
def setRun(run: Run | Null): this.type = updateStore(runLoc, run)
Expand Down
21 changes: 21 additions & 0 deletions compiler/src/dotty/tools/dotc/sbt/interfaces/ProgressCallback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dotty.tools.dotc.sbt.interfaces;

import dotty.tools.dotc.CompilationUnit;

public interface ProgressCallback {
/** Record that the cancellation signal has been recieved during the Zinc run. */
default void cancel() {}

/** Report on if there was a cancellation signal for the current Zinc run. */
default boolean isCancelled() { return false; }

/** Record that a unit has started compiling in the given phase. */
default void informUnitStarting(String phase, CompilationUnit unit) {}

/** Record the current compilation progress.
* @param current `completedPhaseCount * totalUnits + completedUnitsInCurrPhase + completedLate`
* @param total `totalPhases * totalUnits + totalLate`
* @return true if the compilation should continue (if false, then subsequent calls to `isCancelled()` will return true)
*/
default boolean progress(int current, int total, String currPhase, String nextPhase) { return true; }
}
2 changes: 1 addition & 1 deletion sbt-bridge/src/dotty/tools/xsbt/CompilerBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public final class CompilerBridge implements CompilerInterface2 {
public void run(VirtualFile[] sources, DependencyChanges changes, String[] options, Output output,
AnalysisCallback callback, Reporter delegate, CompileProgress progress, Logger log) {
CompilerBridgeDriver driver = new CompilerBridgeDriver(options, output);
driver.run(sources, callback, log, delegate);
driver.run(sources, callback, log, delegate, progress);
}
}
9 changes: 7 additions & 2 deletions sbt-bridge/src/dotty/tools/xsbt/CompilerBridgeDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import xsbti.Problem;
import xsbti.*;
import xsbti.compile.Output;
import xsbti.compile.CompileProgress;

import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -82,7 +83,8 @@ private static void reportMissingFile(DelegatingReporter reporter, SourceFile so
reporter.reportBasicWarning(message);
}

synchronized public void run(VirtualFile[] sources, AnalysisCallback callback, Logger log, Reporter delegate) {
synchronized public void run(
VirtualFile[] sources, AnalysisCallback callback, Logger log, Reporter delegate, CompileProgress progress) {
VirtualFile[] sortedSources = new VirtualFile[sources.length];
System.arraycopy(sources, 0, sortedSources, 0, sources.length);
Arrays.sort(sortedSources, (x0, x1) -> x0.id().compareTo(x1.id()));
Expand Down Expand Up @@ -111,6 +113,8 @@ synchronized public void run(VirtualFile[] sources, AnalysisCallback callback, L
return sourceFile.path();
});

ProgressCallbackImpl progressCallback = new ProgressCallbackImpl(progress);

IncrementalCallback incCallback = new IncrementalCallback(callback, sourceFile ->
asVirtualFile(sourceFile, reporter, lookup)
);
Expand All @@ -121,7 +125,8 @@ synchronized public void run(VirtualFile[] sources, AnalysisCallback callback, L
Contexts.Context initialCtx = initCtx()
.fresh()
.setReporter(reporter)
.setIncCallback(incCallback);
.setIncCallback(incCallback)
.setProgressCallback(progressCallback);

Contexts.Context context = setup(args, initialCtx).map(t -> t._2).getOrElse(() -> initialCtx);

Expand Down
37 changes: 37 additions & 0 deletions sbt-bridge/src/dotty/tools/xsbt/ProgressCallbackImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dotty.tools.xsbt;

import dotty.tools.dotc.sbt.interfaces.ProgressCallback;
import dotty.tools.dotc.CompilationUnit;

import xsbti.compile.CompileProgress;

public final class ProgressCallbackImpl implements ProgressCallback {
private boolean _cancelled = false;
private final CompileProgress _progress;

public ProgressCallbackImpl(CompileProgress progress) {
_progress = progress;
}

@Override
public void cancel() {
_cancelled = true;
}

@Override
public boolean isCancelled() {
return _cancelled;
}

@Override
public void informUnitStarting(String phase, CompilationUnit unit) {
_progress.startUnit(phase, unit.source().file().path());
}

@Override
public boolean progress(int current, int total, String currPhase, String nextPhase) {
boolean shouldAdvance = _progress.advance(current, total, currPhase, nextPhase);
if (!shouldAdvance) cancel();
return shouldAdvance;
}
}
3 changes: 2 additions & 1 deletion sbt-bridge/test/xsbt/ScalaCompilerForUnitTesting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class ScalaCompilerForUnitTesting {
def compileSrcs(groupedSrcs: List[List[String]]): (Seq[VirtualFile], TestCallback) = {
val temp = IO.createTemporaryDirectory
val analysisCallback = new TestCallback
val testProgress = new TestCompileProgress
val classesDir = new File(temp, "classes")
classesDir.mkdir()

Expand All @@ -148,7 +149,7 @@ class ScalaCompilerForUnitTesting {
output,
analysisCallback,
new TestReporter,
new CompileProgress {},
testProgress,
new TestLogger
)

Expand Down
5 changes: 5 additions & 0 deletions sbt-bridge/test/xsbt/TestCompileProgress.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package xsbt

import xsbti.compile.CompileProgress

class TestCompileProgress extends CompileProgress

0 comments on commit 721888d

Please sign in to comment.