Skip to content

Commit 78cc2aa

Browse files
committed
Merge branch 'topic/generic' into 'master'
Genericize LKQL: switch Libadalang.AdaNode to LangkitSupport.NodeInterface See merge request eng/libadalang/langkit-query-language!447
2 parents 684d915 + 7ab15ab commit 78cc2aa

37 files changed

+502
-374
lines changed

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

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package com.adacore.lkql_jit;
77

8+
import com.adacore.langkit_support.LangkitSupport;
89
import com.adacore.libadalang.Libadalang;
910
import com.adacore.liblkqllang.Liblkqllang;
1011
import com.adacore.lkql_jit.checker.BaseChecker;
@@ -18,7 +19,7 @@
1819
import com.adacore.lkql_jit.runtime.GlobalScope;
1920
import com.adacore.lkql_jit.utils.Constants;
2021
import com.adacore.lkql_jit.utils.functions.StringUtils;
21-
import com.adacore.lkql_jit.utils.source_location.LalLocationWrapper;
22+
import com.adacore.lkql_jit.utils.source_location.LangkitLocationWrapper;
2223
import com.oracle.truffle.api.CompilerDirectives;
2324
import com.oracle.truffle.api.TruffleLanguage;
2425
import com.oracle.truffle.api.TruffleLogger;
@@ -58,10 +59,10 @@ public final class LKQLContext {
5859
// ----- Ada project attributes -----
5960

6061
/** The analysis context for the ada files. */
61-
private Libadalang.AnalysisContext adaContext;
62+
private LangkitSupport.AnalysisContextInterface analysisContext;
6263

6364
/** The rewriting context, opened from the Ada analysis context. */
64-
private Libadalang.RewritingContext rewritingContext;
65+
private LangkitSupport.RewritingContextInterface rewritingContext;
6566

6667
/** The project manager for the ada project. */
6768
private Libadalang.ProjectManager projectManager;
@@ -73,7 +74,7 @@ public final class LKQLContext {
7374
boolean isFatal = !this.keepGoingOnMissingFile();
7475
this.getDiagnosticEmitter()
7576
.emitFileNotFound(
76-
new LalLocationWrapper(from.getRoot(), this.linesCache),
77+
new LangkitLocationWrapper(from.getRoot(), this.linesCache),
7778
name,
7879
isFatal
7980
);
@@ -104,13 +105,13 @@ public final class LKQLContext {
104105
* The user-specified units to analyze. If not explicitly specified, those will be the units of
105106
* the root project.
106107
*/
107-
private Libadalang.AnalysisUnit[] specifiedUnits;
108+
private LangkitSupport.AnalysisUnit[] specifiedUnits;
108109

109110
/** All the units of the project, including those of its non-externally-built dependencies. */
110-
private Libadalang.AnalysisUnit[] allUnits;
111+
private LangkitSupport.AnalysisUnit[] allUnits;
111112

112113
/** The root nodes of all the analysis units of the project. */
113-
private Libadalang.AdaNode[] allUnitsRoots;
114+
private LangkitSupport.NodeInterface[] allUnitsRoots;
114115

115116
// ----- Checker attributes -----
116117

@@ -124,7 +125,7 @@ public final class LKQLContext {
124125
private NodeChecker[] filteredGeneralNodeCheckers = null;
125126

126127
/** Node checkers to run on non-SPARK nodes from the Ada sources. */
127-
private NodeChecker[] filteredAdaNodeCheckers = null;
128+
private NodeChecker[] filteredNodeCheckers = null;
128129

129130
/** Node checkers to run only on SPARK nodes from the Ada sources. */
130131
private NodeChecker[] filteredSparkNodeCheckers = null;
@@ -175,7 +176,7 @@ public void finalizeContext() {
175176
this.rewritingContext.close();
176177
}
177178
this.eventHandler.close();
178-
this.adaContext.close();
179+
this.analysisContext.close();
179180
if (this.projectManager != null) this.projectManager.close();
180181
}
181182

@@ -189,21 +190,21 @@ public GlobalScope getGlobal() {
189190
return this.global;
190191
}
191192

192-
public Libadalang.AnalysisUnit[] getSpecifiedUnits() {
193+
public LangkitSupport.AnalysisUnit[] getSpecifiedUnits() {
193194
if (!this.parsed) {
194195
this.parseSources();
195196
}
196197
return this.specifiedUnits;
197198
}
198199

199-
public Libadalang.AnalysisUnit[] getAllUnits() {
200+
public LangkitSupport.AnalysisUnit[] getAllUnits() {
200201
if (!this.parsed) {
201202
this.parseSources();
202203
}
203204
return this.allUnits;
204205
}
205206

206-
public Libadalang.AdaNode[] getAllUnitsRoots() {
207+
public LangkitSupport.NodeInterface[] getAllUnitsRoots() {
207208
if (!this.parsed) {
208209
this.parseSources();
209210
}
@@ -214,12 +215,12 @@ public boolean hasRewritingContext() {
214215
return this.rewritingContext != null;
215216
}
216217

217-
public Libadalang.RewritingContext getRewritingContext() {
218+
public LangkitSupport.RewritingContextInterface getRewritingContext() {
218219
if (this.rewritingContext == null) {
219220
if (!this.parsed) {
220221
this.parseSources();
221222
}
222-
this.rewritingContext = this.adaContext.startRewriting();
223+
this.rewritingContext = this.analysisContext.startRewriting();
223224
}
224225
return this.rewritingContext;
225226
}
@@ -229,7 +230,7 @@ public Libadalang.RewritingContext getRewritingContext() {
229230
* operation is a success then discard the current rewriting context. Otherwise, close it. This
230231
* method assumes that the current rewriting context is not null.
231232
*/
232-
public Libadalang.RewritingApplyResult applyOrCloseRewritingContext() {
233+
public LangkitSupport.RewritingApplyResult applyOrCloseRewritingContext() {
233234
final var res = this.rewritingContext.apply();
234235
if (!res.success) {
235236
this.rewritingContext.close();
@@ -441,20 +442,20 @@ public void parseSources() {
441442

442443
// For each specified source file, store its corresponding analysis unit in the list of
443444
// specified units
444-
this.specifiedUnits = new Libadalang.AnalysisUnit[usedSources.length];
445+
this.specifiedUnits = new LangkitSupport.AnalysisUnit[usedSources.length];
445446
for (int i = 0; i < usedSources.length; i++) {
446-
this.specifiedUnits[i] = this.adaContext.getUnitFromFile(usedSources[i]);
447+
this.specifiedUnits[i] = this.analysisContext.getUnitFromFile(usedSources[i]);
447448
}
448449

449450
// For each source file of the project, store its corresponding analysis unit in the list of
450451
// all
451452
// the units
452453
// of the project, as well as their root nodes.
453-
this.allUnits = new Libadalang.AnalysisUnit[this.allSourceFiles.size()];
454-
this.allUnitsRoots = new Libadalang.AdaNode[this.allSourceFiles.size()];
454+
this.allUnits = new LangkitSupport.AnalysisUnit[this.allSourceFiles.size()];
455+
this.allUnitsRoots = new LangkitSupport.NodeInterface[this.allSourceFiles.size()];
455456

456457
for (int i = 0; i < this.allUnits.length; i++) {
457-
this.allUnits[i] = this.adaContext.getUnitFromFile(this.allSourceFiles.get(i));
458+
this.allUnits[i] = this.analysisContext.getUnitFromFile(this.allSourceFiles.get(i));
458459
this.allUnitsRoots[i] = this.allUnits[i].getRoot();
459460
}
460461

@@ -531,7 +532,7 @@ public void initSources() {
531532
).toList()
532533
);
533534

534-
this.adaContext = this.projectManager.createContext(
535+
this.analysisContext = this.projectManager.createContext(
535536
this.getOptions().subprojectFile().orElse(null),
536537
this.eventHandler,
537538
true,
@@ -566,7 +567,11 @@ public void initSources() {
566567
final Libadalang.UnitProvider provider = this.projectManager.getProvider();
567568

568569
// Create the ada context and store it in the LKQL context
569-
this.adaContext = Libadalang.AnalysisContext.create(
570+
/*
571+
* TODO: Genericize LKQL or Java issue #502. Requires to make create static but not possible
572+
* via an interface nor an abstract class.
573+
*/
574+
this.analysisContext = Libadalang.AnalysisContext.create(
570575
charset,
571576
null,
572577
provider,
@@ -577,7 +582,7 @@ public void initSources() {
577582

578583
// In the absence of a project file, we consider for now that there are no configuration
579584
// pragmas.
580-
this.adaContext.setConfigPragmasMapping(null, null);
585+
this.analysisContext.setConfigPragmasMapping(null, null);
581586
}
582587
}
583588

@@ -657,11 +662,11 @@ public NodeChecker[] getAllNodeCheckers() {
657662
*
658663
* @return The node checkers array for Ada code only.
659664
*/
660-
public NodeChecker[] getAdaNodeCheckers() {
661-
if (this.filteredAdaNodeCheckers == null) {
665+
public NodeChecker[] getNodeCheckers() {
666+
if (this.filteredNodeCheckers == null) {
662667
this.initCheckerCaches();
663668
}
664-
return this.filteredAdaNodeCheckers;
669+
return this.filteredNodeCheckers;
665670
}
666671

667672
/**
@@ -694,18 +699,18 @@ public UnitChecker[] getUnitCheckersFiltered() {
694699
private void initCheckerCaches() {
695700
// Prepare the working variables
696701
final List<NodeChecker> generalNodeCheckers = new ArrayList<>();
697-
final List<NodeChecker> adaNodeCheckers = new ArrayList<>();
702+
final List<NodeChecker> nodeCheckers = new ArrayList<>();
698703
final List<NodeChecker> sparkNodeCheckers = new ArrayList<>();
699704
final List<UnitChecker> unitCheckers = new ArrayList<>();
700705
final Map<String, BaseChecker> allCheckers = this.global.getCheckers();
701706

702707
// Lambda to dispatch checkers in the correct lists
703708
final BiConsumer<BaseChecker, List<NodeChecker>> dispatchChecker = (
704709
checker,
705-
nodeCheckers
710+
nodeCheckerList
706711
) -> {
707712
if (checker instanceof NodeChecker nodeChecker) {
708-
nodeCheckers.add(nodeChecker);
713+
nodeCheckerList.add(nodeChecker);
709714
if (nodeChecker.isFollowGenericInstantiations()) {
710715
needsToFollowInstantiations = true;
711716
}
@@ -753,15 +758,15 @@ private void initCheckerCaches() {
753758

754759
switch (instance.sourceMode()) {
755760
case GENERAL -> dispatchChecker.accept(checker, generalNodeCheckers);
756-
case ADA -> dispatchChecker.accept(checker, adaNodeCheckers);
761+
case ADA -> dispatchChecker.accept(checker, nodeCheckers);
757762
case SPARK -> dispatchChecker.accept(checker, sparkNodeCheckers);
758763
}
759764
}
760765
}
761766

762767
// Set the checker caches
763768
this.filteredGeneralNodeCheckers = generalNodeCheckers.toArray(new NodeChecker[0]);
764-
this.filteredAdaNodeCheckers = adaNodeCheckers.toArray(new NodeChecker[0]);
769+
this.filteredNodeCheckers = nodeCheckers.toArray(new NodeChecker[0]);
765770
this.filteredSparkNodeCheckers = sparkNodeCheckers.toArray(new NodeChecker[0]);
766771
this.filteredUnitCheckers = unitCheckers.toArray(new UnitChecker[0]);
767772
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public final class LKQLLanguage extends TruffleLanguage<LKQLContext> {
4949
* This is the LKQL prelude. Those definitions are visible at the root of the LKQL context. This
5050
* is where we put all global definitions that must be accessible in every context
5151
*/
52+
/*
53+
* TODO: Genericize LKQL issue #499. Cannot genericize the prelude because NODE_DESCRIPTION_MAP
54+
* doesn't contain any node named NodeInterface but AdaNode here for Ada.
55+
*/
5256
private static final String PRELUDE_SOURCE =
5357
"""
5458
selector children
@@ -107,8 +111,8 @@ public LKQLLanguage() {
107111
// columns counting is based on characters:
108112
// https://www.graalvm.org/truffle/javadoc/com/oracle/truffle/api/source/Source.html#createSection(int,int,int,int)
109113
this.lkqlAnalysisContext = Liblkqllang.AnalysisContext.create(
110-
null,
111-
null,
114+
(String) null,
115+
(Liblkqllang.FileReader) null,
112116
null,
113117
null,
114118
true,

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package com.adacore.lkql_jit;
77

8+
import com.adacore.langkit_support.LangkitSupport;
89
import com.adacore.libadalang.Libadalang;
910
import com.adacore.lkql_jit.runtime.values.*;
1011
import com.adacore.lkql_jit.runtime.values.interfaces.Iterable;
@@ -41,12 +42,12 @@
4142
BaseLKQLList.class,
4243
Indexable.class,
4344
Iterable.class,
44-
Libadalang.AdaNode.class,
45-
Libadalang.Token.class,
46-
Libadalang.AnalysisUnit.class,
45+
LangkitSupport.NodeInterface.class,
46+
LangkitSupport.TokenInterface.class,
47+
LangkitSupport.AnalysisUnit.class,
4748
Libadalang.RewritingContext.class,
48-
Libadalang.RewritingNode.class,
49-
Libadalang.MemberReference.class,
49+
LangkitSupport.RewritingNodeInterface.class,
50+
LangkitSupport.MemberReferenceInterface.class,
5051
LKQLNamespace.class,
5152
LKQLObject.class,
5253
Truthy.class,
@@ -101,7 +102,7 @@ public static boolean isTruthy(Object value) {
101102
return (
102103
value instanceof Truthy ||
103104
value instanceof Boolean ||
104-
value instanceof Libadalang.AdaNode
105+
value instanceof LangkitSupport.NodeInterface
105106
);
106107
}
107108

@@ -112,7 +113,7 @@ public static Truthy asTruthy(Object value) {
112113
} else if (value instanceof Boolean b) {
113114
return Truthy.wrapBoolean(b);
114115
} else {
115-
return Truthy.wrapBoolean(value instanceof Libadalang.AdaNode);
116+
return Truthy.wrapBoolean(value instanceof LangkitSupport.NodeInterface);
116117
}
117118
}
118119

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
package com.adacore.lkql_jit.built_ins.methods;
77

8-
import com.adacore.libadalang.Libadalang;
9-
import com.adacore.libadalang.Libadalang.AnalysisUnit;
8+
import com.adacore.langkit_support.LangkitSupport;
9+
import com.adacore.langkit_support.LangkitSupport.AnalysisUnit;
1010
import com.adacore.lkql_jit.annotations.BuiltInMethod;
1111
import com.adacore.lkql_jit.annotations.BuiltinMethodContainer;
1212
import com.adacore.lkql_jit.built_ins.BuiltInBody;
@@ -25,7 +25,7 @@ abstract static class RootExpr extends BuiltInBody {
2525

2626
@Specialization
2727
public Object onUnit(AnalysisUnit self) {
28-
Libadalang.AdaNode res = self.getRoot();
28+
LangkitSupport.NodeInterface res = self.getRoot();
2929
return res.isNone() ? LKQLNull.INSTANCE : res;
3030
}
3131
}
@@ -44,14 +44,14 @@ abstract static class TokensExpr extends BuiltInBody {
4444

4545
@Specialization
4646
public LKQLList onUnit(AnalysisUnit self) {
47-
Libadalang.Token current = self.getFirstToken();
48-
Libadalang.Token last = self.getLastToken();
49-
ArrayList<Libadalang.Token> resList = new ArrayList<>();
47+
LangkitSupport.TokenInterface current = self.getFirstToken();
48+
LangkitSupport.TokenInterface last = self.getLastToken();
49+
ArrayList<LangkitSupport.TokenInterface> resList = new ArrayList<>();
5050
while (!current.isEquivalent(last) && !current.isNone()) {
5151
resList.add(current);
5252
current = current.next();
5353
}
54-
return new LKQLList(resList.toArray(new Libadalang.Token[0]));
54+
return new LKQLList(resList.toArray(new LangkitSupport.TokenInterface[0]));
5555
}
5656
}
5757

0 commit comments

Comments
 (0)