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

Incremental Statix #108

Merged
merged 10 commits into from
Feb 6, 2023
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
Expand Up @@ -11,6 +11,7 @@
import mb.spoofax.compiler.adapter.data.MenuItemRepr;
import mb.spoofax.compiler.language.ClassLoaderResourcesCompiler;
import mb.spoofax.compiler.language.ConstraintAnalyzerLanguageCompiler;
import mb.spoofax.compiler.language.StatixSolverMode;
import mb.spoofax.compiler.util.ClassKind;
import mb.spoofax.compiler.util.GradleConfiguredDependency;
import mb.spoofax.compiler.util.MenuItemCollection;
Expand All @@ -19,6 +20,7 @@
import mb.spoofax.compiler.util.TemplateWriter;
import mb.spoofax.compiler.util.TypeInfo;
import mb.spoofax.compiler.util.TypeInfoCollection;
import mb.spoofax.core.Coordinate;
import mb.spoofax.core.language.command.CommandContextType;
import mb.spoofax.core.language.command.EditorFileType;
import mb.spoofax.core.language.command.EnclosingCommandContextType;
Expand Down Expand Up @@ -91,6 +93,10 @@ class Builder extends ConstraintAnalyzerAdapterCompilerData.Input.Builder {}

/// Configuration

default StatixSolverMode statixSolverMode() {
return languageProjectInput().statixSolverMode();
}

Optional<Integer> defaultStatixMessageStacktraceLength();

Optional<Integer> defaultStatixMessageTermDepth();
Expand All @@ -99,7 +105,6 @@ class Builder extends ConstraintAnalyzerAdapterCompilerData.Input.Builder {}

Optional<Boolean> defaultStatixSuppressCascadingErrors();


/// Kinds of classes (generated/extended/manual)

@Value.Default default ClassKind classKind() { return ClassKind.Generated; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Builder extends ConstraintAnalyzerLanguageCompilerData.Input.Builder {}

@Value.Default default boolean multiFile() { return false; }

@Value.Default default StatixSolverMode statixSolverMode() { return StatixSolverMode.TRADITIONAL; }

/// Kinds of classes (generated/extended/manual)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package mb.spoofax.compiler.language;

public enum StatixSolverMode {
TRADITIONAL, CONCURRENT, INCREMENTAL
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,21 @@ public class {{baseAnalyzeMultiTaskDef.id}} extends ConstraintAnalyzeMultiTaskDe
context.require(classLoaderResources.tryGetAsNativeResource({{languageProjectInput.constraintAnalyzer.qualifiedId}}.class), ResourceStampers.hashFile());
context.require(classLoaderResources.tryGetAsNativeResource({{languageProjectInput.constraintAnalyzerFactory.qualifiedId}}.class), ResourceStampers.hashFile());
final StrategoRuntime strategoRuntime = context.require(getStrategoRuntimeProvider, None.instance).getValue().get();
return constraintAnalyzer.analyze(root, asts, constraintAnalyzerContext, strategoRuntime{{#languageProjectInput.enableStatix}}.addContextObject(mb.statix.spoofax.IStatixProjectConfig.class, createStatixProjectConfig()){{/languageProjectInput.enableStatix}}, resourceService);
return constraintAnalyzer.analyze(root, asts, constraintAnalyzerContext, decorateStrategoRuntime(strategoRuntime, root), resourceService);
}

{{^languageProjectInput.enableStatix}}
protected StrategoRuntime decorateStrategoRuntime(StrategoRuntime strategoRuntime, ResourcePath root) {
return strategoRuntime;
}
{{/languageProjectInput.enableStatix}}
{{#languageProjectInput.enableStatix}}
protected StrategoRuntime decorateStrategoRuntime(StrategoRuntime strategoRuntime, ResourcePath root) {
return strategoRuntime.addContextObject(mb.statix.spoofax.IStatixProjectConfig. class, createStatixProjectConfig())
.addContextObject(mb.spoofax2.common.primitive.generic.Spoofax2ProjectContext.class, createSpoofax2ProjectContext(root))
.addContextObject(mb.statix.common.Spoofax3StatixProjectConfig.class, createSpoofax3ProjectConfig());
}

protected mb.statix.spoofax.IStatixProjectConfig createStatixProjectConfig() {
return new mb.statix.spoofax.StatixProjectConfig(
null,
Expand All @@ -84,5 +95,18 @@ public class {{baseAnalyzeMultiTaskDef.id}} extends ConstraintAnalyzeMultiTaskDe
{{#defaultStatixSuppressCascadingErrors}}{{this}}{{/defaultStatixSuppressCascadingErrors}}{{^defaultStatixSuppressCascadingErrors}}true{{/defaultStatixSuppressCascadingErrors}}
);
}

protected mb.spoofax2.common.primitive.generic.Spoofax2ProjectContext createSpoofax2ProjectContext(ResourcePath root) {
return new mb.spoofax2.common.primitive.generic.Spoofax2ProjectContext(
root,
mb.common.util.MultiMapView.of(),
mb.common.util.MultiMapView.of()
);
}

protected mb.statix.common.Spoofax3StatixProjectConfig createSpoofax3ProjectConfig() {
return mb.statix.common.Spoofax3StatixProjectConfig.of(mb.statix.spoofax.SolverMode.{{statixSolverMode}});
}

{{/languageProjectInput.enableStatix}}
}
4 changes: 4 additions & 0 deletions core/statix.common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {

dependencies {
api(platform(project(":spoofax.depconstraints")))
annotationProcessor(platform(project(":spoofax.depconstraints")))

api("org.metaborg:common")

Expand All @@ -13,4 +14,7 @@ dependencies {
implementation(project(":jsglr.common"))

compileOnly("org.checkerframework:checker-qual-android")
compileOnly("org.immutables:value-annotations")

annotationProcessor("org.immutables:value")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mb.statix.common;

import mb.statix.spoofax.SolverMode;
import org.immutables.value.Value;

@Value.Immutable
public abstract class ASpoofax3StatixProjectConfig {

@Value.Parameter public abstract SolverMode solverMode();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package mb.statix.common;

import mb.statix.spoofax.SolverMode;
import mb.stratego.common.AdaptException;
import mb.stratego.common.AdaptableContext;
import org.spoofax.interpreter.core.IContext;
import org.spoofax.interpreter.core.InterpreterException;
import org.spoofax.interpreter.library.AbstractPrimitive;

import java.util.Optional;

public abstract class StatixCommonPrimitive extends AbstractPrimitive {
public StatixCommonPrimitive(String name, int svars, int tvars) {
super(name, svars, tvars);
}

protected Optional<SolverMode> getSolverMode(IContext env) throws InterpreterException, AdaptException {
Spoofax3StatixProjectConfig config = AdaptableContext.adaptContextObject(env.contextObject(), Spoofax3StatixProjectConfig.class);
return Optional.of(config.solverMode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mb.statix.common;

import mb.stratego.common.AdaptException;
import org.spoofax.interpreter.core.IContext;
import org.spoofax.interpreter.core.InterpreterException;
import org.spoofax.interpreter.stratego.Strategy;
import org.spoofax.interpreter.terms.IStrategoTerm;


public class StatixConcurrentEnabledPrimitive extends StatixCommonPrimitive {

public StatixConcurrentEnabledPrimitive() {
super("STX_is_concurrent_enabled", 0, 0);
}

@Override
public boolean call(IContext env, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {
try {
return getSolverMode(env).map(m -> m.concurrent).orElse(false);
} catch(AdaptException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public StatixPrimitiveLibrary() {
add(new STX_labelord_lt());

add(new StatixProjectConfigPrimitive());

add(new FailingPrimitive("STX_is_concurrent_enabled"));
add(new StatixConcurrentEnabledPrimitive());
add(new StatixSolverModePrimitive());
}

@Override public String getOperatorRegistryName() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package mb.statix.common;

import mb.nabl2.terms.stratego.StrategoBlob;
import mb.statix.spoofax.SolverMode;
import mb.stratego.common.AdaptException;
import org.spoofax.interpreter.core.IContext;
import org.spoofax.interpreter.core.InterpreterException;
import org.spoofax.interpreter.stratego.Strategy;
import org.spoofax.interpreter.terms.IStrategoTerm;

import java.util.Optional;

public class StatixSolverModePrimitive extends StatixCommonPrimitive {

public StatixSolverModePrimitive() {
super("STX_solver_mode", 0, 0);
}

@Override
public boolean call(IContext env, Strategy[] svars, IStrategoTerm[] tvars) throws InterpreterException {
try {
Optional<SolverMode> mode = getSolverMode(env);
if(!mode.isPresent()) {
return false;
}
env.setCurrent(new StrategoBlob(mode.get()));
return true;
} catch(AdaptException e) {
return false;
}
}
}
6 changes: 6 additions & 0 deletions example/mod/mod.spoofax/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ plugins {
id("org.metaborg.spoofax.compiler.gradle.adapter")
}

fun compositeBuild(name: String) = "$group:$name:$version"

dependencies {
api(compositeBuild("spoofax2.common"))
}

languageAdapterProject {
languageProject.set(project(":mod"))
compilerInput {
Expand Down
11 changes: 7 additions & 4 deletions lwb/metalang/cfg/cfg.spoofax2/editor/Syntax.esv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Syntax

imports
imports

completion/colorer/cfg-cc-esv

Expand All @@ -14,9 +14,9 @@ language
fences : [ ] ( ) { }

menus

menu: "Syntax" (openeditor)

action: "Format" = editor-format (source)
action: "Show parsed AST" = debug-show-aterm (source)

Expand All @@ -34,6 +34,7 @@ colorer
funcid = 153 51 0
operator = 0 0 0
comment = 63 127 95 italic
option = 0 0 128

colorer

Expand All @@ -48,4 +49,6 @@ colorer
StringLit : string
PathLit : string
JavaIdLit : varid
JavaQIdLit : varid
JavaQIdLit : varid

StatixSolverMode : option
6 changes: 6 additions & 0 deletions lwb/metalang/cfg/cfg.spoofax2/syntax/part/language_base.sdf3
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ context-free sorts
BracketSymbolOption
StylerOption
ConstraintAnalyzerOption
StatixSolverMode
MultilangAnalyzerOption
StrategoRuntimeOption
TegoRuntimeOption
Expand Down Expand Up @@ -80,12 +81,17 @@ context-free syntax
ConstraintAnalyzerOption.ConstraintAnalyzerEnableStatix = <enable-statix = <Expr>>
ConstraintAnalyzerOption.ConstraintAnalyzerMultiFile = <multi-file = <Expr>>
ConstraintAnalyzerOption.ConstraintAnalyzerStrategoStrategy = <stratego-strategy = <Expr>>
ConstraintAnalyzerOption.ConstraintAnalyzerStatixSolverMode = <statix-solver-mode = <StatixSolverMode>>

ConstraintAnalyzerOption.ConstraintAnalyzerDefaultStatixMessageStacktraceLength = <default-statix-message-stacktrace-length = <Expr>>
ConstraintAnalyzerOption.ConstraintAnalyzerDefaultStatixMessageTermDepth = <default-statix-message-term-depth = <Expr>>
ConstraintAnalyzerOption.ConstraintAnalyzerDefaultStatixTestLogLevel = <default-statix-test-log-level = <Expr>>
ConstraintAnalyzerOption.ConstraintAnalyzerDefaultStatixSuppressCascadingErrors = <default-statix-supress-cascading-errors = <Expr>>

StatixSolverMode.Traditional = <traditional>
StatixSolverMode.Concurrent = <concurrent>
StatixSolverMode.Incremental = <incremental>

context-free syntax

Part.MultilangAnalyzerSection = <multilang-analyzer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ rules // Constraint analyzer section and options
typeOfExpr(s, e) == BOOL() | error $[Expected boolean]@e.
constraintAnalyzerOptionOk(s, ConstraintAnalyzerStrategoStrategy(e)) :-
typeOfExpr(s, e) == STRATEGY() | error $[Expected Stratego strategy identifier]@e.
constraintAnalyzerOptionOk(s, ConstraintAnalyzerStatixSolverMode(_)).

constraintAnalyzerOptionOk(s, ConstraintAnalyzerDefaultStatixMessageStacktraceLength(e)) :-
typeOfExpr(s, e) == UINT() | error $[Expected unsigned integer]@e.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import mb.spoofax.compiler.language.MultilangAnalyzerLanguageCompiler;
import mb.spoofax.compiler.language.ParserLanguageCompiler;
import mb.spoofax.compiler.language.ParserVariant;
import mb.spoofax.compiler.language.StatixSolverMode;
import mb.spoofax.compiler.language.StrategoRuntimeLanguageCompiler;
import mb.spoofax.compiler.language.StylerLanguageCompiler;
import mb.spoofax.compiler.platform.EclipseProjectCompiler;
Expand Down Expand Up @@ -424,6 +425,18 @@ public static Output convert(
subParts.forOneSubtermAsBool("ConstraintAnalyzerEnableStatix", base::enableStatix);
subParts.forOneSubtermAsBool("ConstraintAnalyzerMultiFile", base::multiFile);
subParts.forOneSubtermAsString("ConstraintAnalyzerStrategoStrategy", base::strategoStrategy);
subParts.forOneSubterm("ConstraintAnalyzerStatixSolverMode", mode -> {
if(TermUtils.isAppl(mode, "Traditional", 0)) {
base.statixSolverMode(StatixSolverMode.TRADITIONAL);
} else if(TermUtils.isAppl(mode, "Concurrent", 0)) {
base.statixSolverMode(StatixSolverMode.CONCURRENT);
} else if(TermUtils.isAppl(mode, "Incremental", 0)) {
base.statixSolverMode(StatixSolverMode.INCREMENTAL);
} else {
throw new InvalidAstShapeException("Statix solver mode", mode);
}
});

// TODO: more constraintAnalyzer language properties
final ConstraintAnalyzerAdapterCompiler.Input.Builder adapter = adapterBuilder.withConstraintAnalyzer();
subParts.forOneSubtermAsInt("ConstraintAnalyzerDefaultStatixMessageStacktraceLength", adapter::defaultStatixMessageStacktraceLength);
Expand Down