Skip to content

Commit 1d0b174

Browse files
committed
Merge branch 'topic/lkql_jit/value_cleanup' into 'master'
Cleanup LKQL JIT values Closes #399 See merge request eng/libadalang/langkit-query-language!583
2 parents 7074382 + 4f8888d commit 1d0b174

File tree

65 files changed

+547
-1643
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+547
-1643
lines changed

lkql_jit/language/src/main/java/com/adacore/lkql_jit/LKQLContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ public void initSources() {
512512
// We should not get any scenario variable if we are being run
513513
// without a project file.
514514
if (vars.length != 0) {
515-
throw LKQLRuntimeException.fromMessage(
515+
throw LKQLRuntimeException.create(
516516
"Scenario variable specifications require a project file"
517517
);
518518
}
@@ -682,14 +682,14 @@ private void initCheckerCaches() {
682682

683683
// Verify that the instantiated rule exists
684684
if (checker == null) {
685-
throw LKQLRuntimeException.fromMessage(
685+
throw LKQLRuntimeException.create(
686686
"Could not find any rule named " + instance.ruleName()
687687
);
688688
}
689689

690690
// If the engine is in "fixer" mode, check that the rule has an auto-fixing function
691691
if (getEngineMode() == LKQLOptions.EngineMode.FIXER && checker.autoFix == null) {
692-
throw LKQLRuntimeException.fromMessage(
692+
throw LKQLRuntimeException.create(
693693
"Rule \"" +
694694
instance.ruleName() +
695695
"\" is not defining any auto-fixing function"

lkql_jit/language/src/main/java/com/adacore/lkql_jit/LKQLLanguage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ private LKQLNode translate(
318318
}
319319
return ast;
320320
} else {
321-
throw LKQLRuntimeException.fromMessage("Should not happen");
321+
throw LKQLRuntimeException.create("Should not happen");
322322
}
323323
}
324324

@@ -350,7 +350,7 @@ public LKQLNode translate(final Source source, String sourceName) {
350350
langkitCtx = lktAnalysisContext;
351351
src = source;
352352
} else {
353-
throw LKQLRuntimeException.fromMessage("Invalid lkql version");
353+
throw LKQLRuntimeException.create("Invalid lkql version");
354354
}
355355
} else {
356356
// By default, use lkql syntax
@@ -380,7 +380,7 @@ public LKQLNode translate(final Source source, String sourceName) {
380380
SourceSectionWrapper.create(diagnostic.getSourceLocationRange(), src)
381381
);
382382
}
383-
throw LKQLRuntimeException.fromMessage(
383+
throw LKQLRuntimeException.create(
384384
"Syntax errors in " + unit.getFileName(false) + ": stopping interpreter"
385385
);
386386
}

lkql_jit/language/src/main/java/com/adacore/lkql_jit/LKQLTypeSystem.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
LKQLObject.class,
5454
Nullish.class,
5555
LKQLRecValue.class,
56-
LKQLValue.class,
5756
}
5857
)
5958
public abstract class LKQLTypeSystem {

lkql_jit/language/src/main/java/com/adacore/lkql_jit/built_ins/BuiltInBody.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import com.adacore.lkql_jit.nodes.expressions.value_read.ReadParameter;
1111
import com.oracle.truffle.api.Truffle;
1212
import com.oracle.truffle.api.dsl.*;
13-
import com.oracle.truffle.api.frame.FrameInstance;
14-
import com.oracle.truffle.api.frame.FrameInstanceVisitor;
1513
import com.oracle.truffle.api.source.SourceSection;
1614

1715
/** This node represents a base for all built-in functions body. */
@@ -30,18 +28,13 @@ protected BuiltInBody() {
3028

3129
protected SourceSection getCallLocation() {
3230
return Truffle.getRuntime()
33-
.iterateFrames(
34-
new FrameInstanceVisitor<SourceSection>() {
35-
@Override
36-
public SourceSection visitFrame(FrameInstance frameInstance) {
37-
var callnode = frameInstance.getCallNode();
38-
if (callnode != null) {
39-
return callnode.getEncapsulatingSourceSection();
40-
}
41-
return null;
42-
}
31+
.iterateFrames(frameInstance -> {
32+
var callnode = frameInstance.getCallNode();
33+
if (callnode != null) {
34+
return callnode.getEncapsulatingSourceSection();
4335
}
44-
);
36+
return null;
37+
});
4538
}
4639

4740
// ----- Override methods -----

lkql_jit/language/src/main/java/com/adacore/lkql_jit/built_ins/BuiltInFunctionValue.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
package com.adacore.lkql_jit.built_ins;
77

88
import com.adacore.lkql_jit.LKQLLanguage;
9+
import com.adacore.lkql_jit.nodes.LKQLNode;
910
import com.adacore.lkql_jit.nodes.TopLevelList;
1011
import com.adacore.lkql_jit.nodes.expressions.Expr;
1112
import com.adacore.lkql_jit.nodes.root_nodes.FunctionRootNode;
1213
import com.adacore.lkql_jit.runtime.Closure;
1314
import com.adacore.lkql_jit.runtime.values.LKQLFunction;
1415
import com.oracle.truffle.api.CompilerDirectives;
16+
import com.oracle.truffle.api.nodes.Node;
1517

1618
/** This class represents the LKQL value of a built-in function. */
1719
public class BuiltInFunctionValue extends LKQLFunction {
@@ -36,7 +38,8 @@ public BuiltInFunctionValue(
3638
name,
3739
documentation,
3840
names,
39-
new Expr[names.length]
41+
new Expr[names.length],
42+
body
4043
);
4144
stringDefaultVals = defaultValues;
4245
}
@@ -53,13 +56,14 @@ public BuiltInFunctionValue(
5356
functionRootNode.getName(),
5457
documentation,
5558
names,
56-
new Expr[names.length]
59+
new Expr[names.length],
60+
functionRootNode.getBody()
5761
);
5862
stringDefaultVals = defaultValues;
5963
}
6064

6165
@Override
62-
public Expr[] getParameterDefaultValues() {
66+
public Node[] getParameterDefaultValues() {
6367
if (stringDefaultVals != null && !defaultValsEvald) {
6468
for (int i = 0; i < parameterDefaultValues.length; i++) {
6569
if (stringDefaultVals[i] != null) {
@@ -72,12 +76,12 @@ public Expr[] getParameterDefaultValues() {
7276
}
7377

7478
@CompilerDirectives.TruffleBoundary
75-
private Expr getDefaultValAt(int i) {
79+
private Node getDefaultValAt(int i) {
7680
var prg =
77-
((TopLevelList) LKQLLanguage.getLanguage(rootNode.getBody()).translate(
81+
((TopLevelList) LKQLLanguage.getLanguage((LKQLNode) body).translate(
7882
stringDefaultVals[i],
7983
"<defaultval>"
8084
)).program;
81-
return (Expr) prg[0];
85+
return prg[0];
8286
}
8387
}

lkql_jit/language/src/main/java/com/adacore/lkql_jit/built_ins/BuiltInFunctions.java

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,19 @@
66
package com.adacore.lkql_jit.built_ins;
77

88
import com.adacore.langkit_support.LangkitSupport;
9+
import com.adacore.lkql_jit.LKQLContext;
910
import com.adacore.lkql_jit.LKQLLanguage;
1011
import com.adacore.lkql_jit.LKQLTypeSystemGen;
1112
import com.adacore.lkql_jit.annotations.*;
1213
import com.adacore.lkql_jit.exception.LKQLRuntimeException;
1314
import com.adacore.lkql_jit.nodes.utils.ConcatenationNode;
1415
import com.adacore.lkql_jit.nodes.utils.ValueCombiner;
1516
import com.adacore.lkql_jit.runtime.values.*;
16-
import com.adacore.lkql_jit.runtime.values.bases.BasicLKQLValue;
1717
import com.adacore.lkql_jit.runtime.values.interfaces.Indexable;
1818
import com.adacore.lkql_jit.runtime.values.interfaces.Iterable;
19-
import com.adacore.lkql_jit.runtime.values.interfaces.LKQLValue;
19+
import com.adacore.lkql_jit.runtime.values.interfaces.Iterator;
2020
import com.adacore.lkql_jit.runtime.values.lists.LKQLList;
2121
import com.adacore.lkql_jit.utils.Constants;
22-
import com.adacore.lkql_jit.utils.Iterator;
2322
import com.adacore.lkql_jit.utils.LKQLTypesHelper;
2423
import com.adacore.lkql_jit.utils.TextWriter;
2524
import com.adacore.lkql_jit.utils.functions.ArrayUtils;
@@ -28,7 +27,9 @@
2827
import com.oracle.truffle.api.CompilerDirectives;
2928
import com.oracle.truffle.api.Truffle;
3029
import com.oracle.truffle.api.dsl.Cached;
30+
import com.oracle.truffle.api.dsl.Fallback;
3131
import com.oracle.truffle.api.dsl.Specialization;
32+
import com.oracle.truffle.api.exception.AbstractTruffleException;
3233
import com.oracle.truffle.api.frame.VirtualFrame;
3334
import com.oracle.truffle.api.interop.ArityException;
3435
import com.oracle.truffle.api.interop.InteropLibrary;
@@ -70,7 +71,16 @@ abstract static class PatternExpr extends BuiltInBody {
7071

7172
@Specialization
7273
protected LKQLPattern onValidArgs(String regex, @DefaultVal("true") boolean caseSensitive) {
73-
return new LKQLPattern(this, regex, caseSensitive);
74+
// Call the TRegex language to create the pattern value
75+
try {
76+
return LKQLPattern.create(
77+
regex,
78+
caseSensitive,
79+
LKQLLanguage.getContext(this).getEnv()
80+
);
81+
} catch (AbstractTruffleException e) {
82+
throw LKQLRuntimeException.regexSyntaxError(regex, this);
83+
}
7484
}
7585
}
7686

@@ -121,8 +131,18 @@ protected String onObject(Object obj, @CachedLibrary("obj") InteropLibrary objLi
121131
abstract static class DocExpr extends BuiltInBody {
122132

123133
@Specialization
124-
protected String onLKQLValue(LKQLValue value) {
125-
return value.lkqlDocumentation();
134+
protected String onFunction(LKQLFunction function) {
135+
return function.lkqlDocumentation();
136+
}
137+
138+
@Specialization
139+
protected String onSelector(LKQLSelector selector) {
140+
return selector.lkqlDocumentation();
141+
}
142+
143+
@Specialization
144+
protected String onNamespace(LKQLNamespace namespace) {
145+
return namespace.lkqlDocumentation();
126146
}
127147

128148
@Specialization
@@ -456,12 +476,17 @@ protected LKQLList onValidArgs(
456476
abstract static class ProfileExpr extends BuiltInBody {
457477

458478
@Specialization
459-
protected String onLKQLValue(LKQLValue val) {
460-
return val.lkqlProfile();
479+
protected String onFunction(LKQLFunction function) {
480+
return function.lkqlProfile();
481+
}
482+
483+
@Specialization
484+
protected String onSelector(LKQLSelector selector) {
485+
return selector.lkqlProfile();
461486
}
462487

463488
@Specialization
464-
protected String onOthers(Object obj) {
489+
protected String onAny(Object obj) {
465490
return "";
466491
}
467492
}
@@ -472,10 +497,10 @@ protected String onOthers(Object obj) {
472497
)
473498
abstract static class DocumentNamespaceExpr extends BuiltInBody {
474499

475-
private static void documentCallable(TextWriter writer, BasicLKQLValue callable) {
476-
writer.write(".. function:: " + callable.lkqlProfile() + "\n\n");
500+
private static void documentCallable(TextWriter writer, String profile, String doc) {
501+
writer.write(".. function:: " + profile + "\n\n");
477502
writer.withIndent(() -> {
478-
writer.write(callable.lkqlDocumentation());
503+
writer.write(doc);
479504
});
480505
writer.write("\n\n");
481506
}
@@ -502,7 +527,7 @@ protected String impl(LKQLNamespace namespace, String name) {
502527
.sorted(Comparator.comparing(LKQLFunction::getExecutableName));
503528

504529
for (var func : functions.toList()) {
505-
documentCallable(writer, func);
530+
documentCallable(writer, func.lkqlProfile(), func.lkqlDocumentation());
506531
}
507532

508533
writer.write("Selectors\n");
@@ -517,7 +542,7 @@ protected String impl(LKQLNamespace namespace, String name) {
517542
.sorted(Comparator.comparing(LKQLSelector::lkqlProfile));
518543

519544
for (var sel : selectors.toList()) {
520-
documentCallable(writer, sel);
545+
documentCallable(writer, sel.lkqlProfile(), sel.lkqlDocumentation());
521546
}
522547

523548
return sw.getBuffer().toString();
@@ -532,12 +557,35 @@ protected String impl(LKQLNamespace namespace, String name) {
532557
abstract static class HelpExpr extends BuiltInBody {
533558

534559
@Specialization
535-
protected Object onLKQLValue(LKQLValue value) {
536-
LKQLLanguage.getContext(this).println(
537-
StringUtils.concat(value.lkqlProfile(), "\n", value.lkqlDocumentation())
560+
protected Object onFunction(LKQLFunction function) {
561+
printHelp(
562+
LKQLLanguage.getContext(this),
563+
function.lkqlProfile(),
564+
function.lkqlDocumentation()
538565
);
539566
return LKQLUnit.INSTANCE;
540567
}
568+
569+
@Specialization
570+
protected Object onSelector(LKQLSelector selector) {
571+
printHelp(
572+
LKQLLanguage.getContext(this),
573+
selector.lkqlProfile(),
574+
selector.lkqlDocumentation()
575+
);
576+
return LKQLUnit.INSTANCE;
577+
}
578+
579+
@Fallback
580+
protected Object onAny(Object obj) {
581+
LKQLLanguage.getContext(this).println("No help available");
582+
return LKQLUnit.INSTANCE;
583+
}
584+
585+
@CompilerDirectives.TruffleBoundary
586+
private static void printHelp(LKQLContext context, String profile, String doc) {
587+
context.println(profile + "\n" + doc);
588+
}
541589
}
542590

543591
@BuiltInFunction(name = "units", doc = "Return a list of all units")

lkql_jit/language/src/main/java/com/adacore/lkql_jit/built_ins/methods/IterableMethods.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
import com.adacore.lkql_jit.built_ins.BuiltInBody;
1111
import com.adacore.lkql_jit.runtime.values.LKQLTuple;
1212
import com.adacore.lkql_jit.runtime.values.interfaces.Iterable;
13+
import com.adacore.lkql_jit.runtime.values.interfaces.Iterator;
1314
import com.adacore.lkql_jit.runtime.values.lists.LKQLList;
14-
import com.adacore.lkql_jit.utils.Iterator;
1515
import com.adacore.lkql_jit.utils.LKQLTypesHelper;
1616
import com.oracle.truffle.api.dsl.Specialization;
1717
import java.util.LinkedList;

lkql_jit/language/src/main/java/com/adacore/lkql_jit/checker/BaseChecker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public void setAlias(String alias) {
120120

121121
/** Get the checker path name. */
122122
public String getPath() {
123-
return function.rootNode.getBody().getSourceSection().getSource().getPath();
123+
return function.body.getSourceSection().getSource().getPath();
124124
}
125125

126126
// ----- Inner classes -----

lkql_jit/language/src/main/java/com/adacore/lkql_jit/checker/built_ins/NodeCheckerFunction.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.adacore.lkql_jit.checker.utils.CheckerUtils;
1616
import com.adacore.lkql_jit.exception.LKQLRuntimeException;
1717
import com.adacore.lkql_jit.exception.LangkitException;
18+
import com.adacore.lkql_jit.nodes.expressions.Expr;
1819
import com.adacore.lkql_jit.nodes.expressions.LKQLToBoolean;
1920
import com.adacore.lkql_jit.nodes.expressions.LKQLToBooleanNodeGen;
2021
import com.adacore.lkql_jit.options.LKQLOptions;
@@ -73,7 +74,7 @@ public static InstantiatedNodeChecker[] preprocessNodeCheckers(
7374
StringUtils.toLowerCase(paramName)
7475
);
7576
instance.arguments[j + 1] = userDefinedArg == null
76-
? function.getParameterDefaultValues()[j].executeGeneric(frame)
77+
? ((Expr) function.getParameterDefaultValues()[j]).executeGeneric(frame)
7778
: userDefinedArg;
7879
}
7980

@@ -235,7 +236,7 @@ public Object implem(MaterializedFrame frame, LangkitSupport.NodeInterface root)
235236
"Error(s) while applying a rewriting context: ",
236237
ArrayUtils.toString(applyRes.getDiagnostics())
237238
);
238-
throw LKQLRuntimeException.fromMessage(message);
239+
throw LKQLRuntimeException.create(message);
239240
}
240241

241242
// Then output the rewrote unit as required
@@ -430,7 +431,7 @@ private static void callAutoFix(
430431
);
431432
} catch (ArityException | UnsupportedTypeException | UnsupportedMessageException e) {
432433
// TODO: Move function runtime verification to the LKQLFunction class (#138)
433-
throw LKQLRuntimeException.fromJavaException(e, checker.autoFix.getBody());
434+
throw LKQLRuntimeException.fromJavaException(e, checker.autoFix.body);
434435
}
435436
}
436437

0 commit comments

Comments
 (0)