Skip to content

Commit

Permalink
Merge branch 'topic/lkql_jit_values' into 'master'
Browse files Browse the repository at this point in the history
Rework the LKQL JIT value representations

Closes #71

See merge request eng/libadalang/langkit-query-language!121
  • Loading branch information
HugoGGuerrier committed Dec 4, 2023
2 parents def6b44 + 8601955 commit 23e012a
Show file tree
Hide file tree
Showing 175 changed files with 4,254 additions and 2,449 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
package com.adacore.lkql_jit;

import com.adacore.libadalang.Libadalang;
import com.adacore.lkql_jit.built_ins.BuiltInFunctionValue;
import com.adacore.lkql_jit.exception.LKQLRuntimeException;
import com.adacore.lkql_jit.nodes.declarations.FunctionDeclaration;
import com.adacore.lkql_jit.runtime.GlobalScope;
import com.adacore.lkql_jit.runtime.built_ins.BuiltInFunctionValue;
import com.adacore.lkql_jit.runtime.values.ObjectValue;
import com.adacore.lkql_jit.utils.Constants;
import com.adacore.lkql_jit.utils.LKQLConfigFileResult;
import com.adacore.lkql_jit.utils.checkers.BaseChecker;
import com.adacore.lkql_jit.utils.checkers.NodeChecker;
import com.adacore.lkql_jit.utils.checkers.UnitChecker;
import com.adacore.lkql_jit.utils.functions.ArrayUtils;
import com.adacore.lkql_jit.utils.functions.CheckerUtils;
import com.adacore.lkql_jit.utils.functions.ParsingUtils;
Expand Down Expand Up @@ -116,16 +117,16 @@ public final class LKQLContext {
private Map<String, Map<String, Object>> allRulesArgs = null;

/** The filtered node checkers cache. */
private ObjectValue[] filteredAllNodeCheckers = null;
private NodeChecker[] filteredAllNodeCheckers = null;

/** The filtered node checkers for Ada code only. */
private ObjectValue[] filteredAdaNodeCheckers = null;
private NodeChecker[] filteredAdaNodeCheckers = null;

/** The filtered node checkers for SPARK code only. */
private ObjectValue[] filteredSparkNodeCheckers = null;
private NodeChecker[] filteredSparkNodeCheckers = null;

/** The filtered unit checkers cache. */
private ObjectValue[] filteredUnitCheckers = null;
private UnitChecker[] filteredUnitCheckers = null;

/** Whether there is at least one rule that needs to follow generic instantiations. */
private boolean needsToFollowInstantiations = false;
Expand Down Expand Up @@ -789,7 +790,7 @@ public Object getRuleArg(String ruleName, String argName) {
* @return The node checkers array filtered according to options.
*/
@CompilerDirectives.TruffleBoundary
public ObjectValue[] getAllNodeCheckers() {
public NodeChecker[] getAllNodeCheckers() {
if (this.filteredAllNodeCheckers == null) {
this.initCheckerCaches();
}
Expand All @@ -801,7 +802,7 @@ public ObjectValue[] getAllNodeCheckers() {
*
* @return The node checkers array for Ada code only.
*/
public ObjectValue[] getAdaNodeCheckers() {
public NodeChecker[] getAdaNodeCheckers() {
if (this.filteredAdaNodeCheckers == null) {
this.initCheckerCaches();
}
Expand All @@ -813,7 +814,7 @@ public ObjectValue[] getAdaNodeCheckers() {
*
* @return The node checkers array for SPARK code only.
*/
public ObjectValue[] getSparkNodeCheckers() {
public NodeChecker[] getSparkNodeCheckers() {
if (this.filteredSparkNodeCheckers == null) {
this.initCheckerCaches();
}
Expand All @@ -826,7 +827,7 @@ public ObjectValue[] getSparkNodeCheckers() {
* @return The list for unit checkers filtered according to options.
*/
@CompilerDirectives.TruffleBoundary
public ObjectValue[] getUnitCheckersFiltered() {
public UnitChecker[] getUnitCheckersFiltered() {
if (this.filteredUnitCheckers == null) {
this.initCheckerCaches();
}
Expand All @@ -837,56 +838,57 @@ public ObjectValue[] getUnitCheckersFiltered() {
@CompilerDirectives.TruffleBoundary
private void initCheckerCaches() {
// Prepare the working variables
final List<ObjectValue> allNodeCheckers = new ArrayList<>();
final List<ObjectValue> adaNodeCheckers = new ArrayList<>();
final List<ObjectValue> sparkNodeCheckers = new ArrayList<>();
final List<ObjectValue> unitCheckers = new ArrayList<>();
final Map<String, ObjectValue> allCheckers = this.global.getCheckers();
final List<NodeChecker> allNodeCheckers = new ArrayList<>();
final List<NodeChecker> adaNodeCheckers = new ArrayList<>();
final List<NodeChecker> sparkNodeCheckers = new ArrayList<>();
final List<UnitChecker> unitCheckers = new ArrayList<>();
final Map<String, BaseChecker> allCheckers = this.global.getCheckers();

// Get the command line required rules
final List<String> allRules = this.getAllRules();
final List<String> adaRules = this.getAdaRules();
final List<String> sparkRules = this.getSparkRules();

// Lambda to dispatch checkers in the correct lists
final BiConsumer<ObjectValue, List<ObjectValue>> dispatchChecker =
final BiConsumer<BaseChecker, List<NodeChecker>> dispatchChecker =
(checker, nodeCheckers) -> {
if (checker.get("mode") == FunctionDeclaration.CheckerMode.NODE) {
nodeCheckers.add(checker);
if ((boolean) checker.get("follow_generic_instantiations")) {
if (checker instanceof NodeChecker nodeChecker) {
nodeCheckers.add(nodeChecker);
if (nodeChecker.isFollowGenericInstantiations()) {
needsToFollowInstantiations = true;
}
} else {
unitCheckers.add(checker);
UnitChecker unitChecker = (UnitChecker) checker;
unitCheckers.add(unitChecker);
}
};

// Lambda to get the checker object value from the rule name
final Function<String, ObjectValue> getAssociatedChecker =
final Function<String, BaseChecker> getAssociatedChecker =
(ruleName) -> {
// Get the checker from the given rule name
ObjectValue res = null;
BaseChecker checker;
final String aliasResolved = this.getRuleFromAlias(ruleName);
if (aliasResolved != null) {
res = new ObjectValue(allCheckers.get(aliasResolved));
res.set("alias", ruleName);
checker = allCheckers.get(aliasResolved).copy();
checker.setAlias(ruleName);
} else {
res = allCheckers.get(ruleName);
checker = allCheckers.get(ruleName);
}

// Verify that the checker is not null
if (res == null) {
if (checker == null) {
throw LKQLRuntimeException.fromMessage(
"Could not find any rule named " + ruleName);
}

// Return the result
return res;
return checker;
};

// If there is no wanted rule, run them all (if the appropriate option is set)
if (allRules.size() == 0 && this.env.getOptions().get(LKQLLanguage.fallbackToAllRules)) {
for (ObjectValue checker : allCheckers.values()) {
for (BaseChecker checker : allCheckers.values()) {
dispatchChecker.accept(checker, allNodeCheckers);
}
}
Expand All @@ -905,10 +907,10 @@ private void initCheckerCaches() {
}

// Set the checker caches
this.filteredAllNodeCheckers = allNodeCheckers.toArray(new ObjectValue[0]);
this.filteredAdaNodeCheckers = adaNodeCheckers.toArray(new ObjectValue[0]);
this.filteredSparkNodeCheckers = sparkNodeCheckers.toArray(new ObjectValue[0]);
this.filteredUnitCheckers = unitCheckers.toArray(new ObjectValue[0]);
this.filteredAllNodeCheckers = allNodeCheckers.toArray(new NodeChecker[0]);
this.filteredAdaNodeCheckers = adaNodeCheckers.toArray(new NodeChecker[0]);
this.filteredSparkNodeCheckers = sparkNodeCheckers.toArray(new NodeChecker[0]);
this.filteredUnitCheckers = unitCheckers.toArray(new UnitChecker[0]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
package com.adacore.lkql_jit;

import com.adacore.liblkqllang.Liblkqllang;
import com.adacore.lkql_jit.built_ins.BuiltInFactory;
import com.adacore.lkql_jit.exception.LKQLRuntimeException;
import com.adacore.lkql_jit.langkit_translator.LangkitTranslator;
import com.adacore.lkql_jit.nodes.LKQLNode;
import com.adacore.lkql_jit.nodes.TopLevelList;
import com.adacore.lkql_jit.nodes.root_nodes.TopLevelRootNode;
import com.adacore.lkql_jit.runtime.GlobalScope;
import com.adacore.lkql_jit.runtime.built_ins.BuiltInFactory;
import com.adacore.lkql_jit.utils.Constants;
import com.adacore.lkql_jit.utils.enums.DiagnosticOutputMode;
import com.oracle.truffle.api.CallTarget;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
package com.adacore.lkql_jit;

import com.adacore.libadalang.Libadalang;
import com.adacore.lkql_jit.runtime.values.*;
import com.adacore.lkql_jit.runtime.values.interfaces.Iterable;
import com.adacore.lkql_jit.runtime.values.interfaces.*;
import com.adacore.lkql_jit.built_ins.values.*;
import com.adacore.lkql_jit.built_ins.values.interfaces.Iterable;
import com.adacore.lkql_jit.built_ins.values.interfaces.*;
import com.adacore.lkql_jit.built_ins.values.lists.LKQLLazyList;
import com.adacore.lkql_jit.built_ins.values.lists.LKQLList;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.ImplicitCast;
import com.oracle.truffle.api.dsl.TypeCast;
Expand All @@ -39,27 +41,25 @@
* @author Hugo GUERRIER
*/
@TypeSystem({
UnitValue.class,
LKQLUnit.class,
long.class,
BigInteger.class,
String.class,
Pattern.class,
FunctionValue.class,
PropertyRefValue.class,
SelectorValue.class,
TupleValue.class,
ListValue.class,
LazyListValue.class,
SelectorListValue.class,
LazyCollection.class,
LKQLPattern.class,
LKQLFunction.class,
LKQLSelector.class,
LKQLProperty.class,
LKQLTuple.class,
LKQLList.class,
LKQLLazyList.class,
Indexable.class,
Iterable.class,
Libadalang.AdaNode.class,
Libadalang.Token.class,
Libadalang.AnalysisUnit.class,
boolean.class,
ObjectValue.class,
NamespaceValue.class,
LKQLNamespace.class,
LKQLObject.class,
Nullish.class,
LKQLValue.class,
})
Expand All @@ -73,9 +73,9 @@ public abstract class LKQLTypeSystem {
* @param value The value to test.
* @return True if the value is unit, false else.
*/
@TypeCheck(UnitValue.class)
public static boolean isUnit(Object value) {
return value == UnitValue.getInstance();
@TypeCheck(LKQLUnit.class)
public static boolean isUnit(final Object value) {
return value == LKQLUnit.INSTANCE;
}

/**
Expand All @@ -84,9 +84,9 @@ public static boolean isUnit(Object value) {
* @param value The value to cast.
* @return The unit value.
*/
@TypeCast(UnitValue.class)
public static UnitValue asUnit(Object value) {
return UnitValue.getInstance();
@TypeCast(LKQLUnit.class)
public static LKQLUnit asUnit(@SuppressWarnings("unused") final Object value) {
return LKQLUnit.INSTANCE;
}

// ----- Nullish values -----
Expand All @@ -98,8 +98,8 @@ public static UnitValue asUnit(Object value) {
* @return True if the value si nullish, false else.
*/
@TypeCheck(Nullish.class)
public static boolean isNullish(Object value) {
return value == UnitValue.getInstance() || value == NodeNull.getInstance();
public static boolean isNullish(final Object value) {
return value == LKQLUnit.INSTANCE || value == LKQLNull.INSTANCE;
}

// ----- Boolean value methods -----
Expand Down Expand Up @@ -156,7 +156,7 @@ public static BigInteger longToBigInteger(long value) {
*/
@TypeCheck(Libadalang.AdaNode.class)
public static boolean isAdaNode(Object nodeObject) {
return nodeObject instanceof Libadalang.AdaNode || nodeObject instanceof DepthNode;
return nodeObject instanceof Libadalang.AdaNode || nodeObject instanceof LKQLDepthNode;
}

/**
Expand All @@ -173,11 +173,11 @@ public static Libadalang.AdaNode asAdaNode(Object nodeObject) {
}

// If the value is a depth node
else if (nodeObject instanceof DepthNode depthNode) {
else if (nodeObject instanceof LKQLDepthNode depthNode) {
return depthNode.getNode();
}

// Return the default value
return NodeNull.getInstance();
return LKQLNull.INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
-- <http://www.gnu.org/licenses/.> --
----------------------------------------------------------------------------*/

package com.adacore.lkql_jit.runtime.built_ins;
package com.adacore.lkql_jit.built_ins;

import com.adacore.lkql_jit.built_ins.functions.*;
import com.adacore.lkql_jit.built_ins.methods.*;
import com.adacore.lkql_jit.built_ins.selectors.*;
import com.adacore.lkql_jit.langkit_translator.passes.framing_utils.ScriptFramesBuilder;
import com.adacore.lkql_jit.runtime.GlobalScope;
import com.adacore.lkql_jit.runtime.built_ins.functions.*;
import com.adacore.lkql_jit.runtime.built_ins.methods.*;
import com.adacore.lkql_jit.runtime.built_ins.selectors.*;
import java.util.ArrayList;
import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@
-- <http://www.gnu.org/licenses/.> --
----------------------------------------------------------------------------*/

package com.adacore.lkql_jit.runtime.built_ins;
package com.adacore.lkql_jit.built_ins;

import com.adacore.lkql_jit.built_ins.values.LKQLFunction;
import com.adacore.lkql_jit.nodes.expressions.Expr;
import com.adacore.lkql_jit.nodes.expressions.FunCall;
import com.adacore.lkql_jit.nodes.root_nodes.FunctionRootNode;
import com.adacore.lkql_jit.runtime.Closure;
import com.adacore.lkql_jit.runtime.values.FunctionValue;
import com.oracle.truffle.api.frame.VirtualFrame;

/**
* This class represents the base of a built-in function value.
*
* @author Hugo GUERRIER
*/
public final class BuiltInFunctionValue extends FunctionValue {
public final class BuiltInFunctionValue extends LKQLFunction {

// ----- Attributes -----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@
-- <http://www.gnu.org/licenses/.> --
----------------------------------------------------------------------------*/

package com.adacore.lkql_jit.runtime.built_ins;
package com.adacore.lkql_jit.built_ins;

import com.adacore.lkql_jit.built_ins.values.LKQLSelector;
import com.adacore.lkql_jit.nodes.declarations.selector.SelectorArm;
import com.adacore.lkql_jit.nodes.root_nodes.SelectorRootNode;
import com.adacore.lkql_jit.runtime.Closure;
import com.adacore.lkql_jit.runtime.values.SelectorValue;

/**
* This class represents the base of the built-in selector values.
*
* @author Hugo GUERRIER
*/
public class BuiltInSelectorValue extends SelectorValue {
public class BuiltInSelectorValue extends LKQLSelector {

// ----- Constructors -----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
-- <http://www.gnu.org/licenses/.> --
----------------------------------------------------------------------------*/

package com.adacore.lkql_jit.runtime.built_ins;
package com.adacore.lkql_jit.built_ins;

import com.adacore.lkql_jit.nodes.expressions.Expr;
import com.adacore.lkql_jit.nodes.expressions.FunCall;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
-- <http://www.gnu.org/licenses/.> --
----------------------------------------------------------------------------*/

package com.adacore.lkql_jit.runtime.built_ins.functions;
package com.adacore.lkql_jit.built_ins.functions;

import com.adacore.lkql_jit.LKQLTypeSystemGen;
import com.adacore.lkql_jit.built_ins.BuiltInFunctionValue;
import com.adacore.lkql_jit.exception.LKQLRuntimeException;
import com.adacore.lkql_jit.nodes.expressions.Expr;
import com.adacore.lkql_jit.nodes.expressions.FunCall;
import com.adacore.lkql_jit.runtime.built_ins.BuiltInFunctionValue;
import com.adacore.lkql_jit.utils.LKQLTypesHelper;
import com.adacore.lkql_jit.utils.functions.FileUtils;
import com.oracle.truffle.api.frame.VirtualFrame;
Expand Down

0 comments on commit 23e012a

Please sign in to comment.