Skip to content

Commit d6b4ea9

Browse files
committed
Merge branch 'topic/java_cleanups' into 'master'
Various cleanups in the Java bindings Closes #767 See merge request eng/libadalang/langkit!1020
2 parents ead41a4 + 2ca1204 commit d6b4ea9

File tree

8 files changed

+136
-63
lines changed

8 files changed

+136
-63
lines changed

langkit/templates/java_api/array.mako

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,9 @@
102102
${elem_ni_ref_type} toRead;
103103
104104
// Iterate over all array elements
105+
final int elemSize = SizeOf.get(${elem_ni_type}.class);
105106
for(int i = 0 ; i < size ; i++) {
106-
nativeItem = nativeItems.add(
107-
i * SizeOf.get(${elem_ni_type}.class)
108-
);
107+
nativeItem = nativeItems.add(i * elemSize);
109108
toRead = WordFactory.unsigned(nativeItem.rawValue());
110109
content[i] = ${
111110
api.ni_wrap(cls.element_type, "toRead", [])
@@ -166,10 +165,9 @@
166165
${elem_ni_ref_type} toWrite;
167166
168167
// Place all elements in the native array
168+
final int elemSize = SizeOf.get(${elem_ni_type}.class);
169169
for(int i = 0 ; i < this.content.length ; i++) {
170-
nativeItem = nativeItems.add(
171-
i * SizeOf.get(${elem_ni_type}.class)
172-
);
170+
nativeItem = nativeItems.add(i * elemSize);
173171
toWrite = WordFactory.unsigned(
174172
nativeItem.rawValue()
175173
);

langkit/templates/java_api/jni_impl_c.mako

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ const char * to_c_string(
802802
return (*env)->GetStringUTFChars(env, j_string, NULL);
803803
}
804804

805-
// Release the given C string assocaited with the given Java string
805+
// Release the given C string associated with the given Java string
806806
void release_c_string(
807807
JNIEnv *env,
808808
jstring j_string,
@@ -2530,7 +2530,7 @@ ${iterator.jni_c_impl(iterator_type)}
25302530
% endfor
25312531

25322532
// ==========
2533-
// AST node functions
2533+
// Node functions
25342534
// ==========
25352535

25362536
// Return whether the two given entities are equal
@@ -2720,7 +2720,7 @@ ${api.jni_func_sig("node_image", "jobject")}(
27202720
}
27212721

27222722
// ==========
2723-
// AST node field accessors
2723+
// Node field accessors
27242724
// ==========
27252725

27262726
% for astnode in ctx.astnode_types:

langkit/templates/java_api/main_class.mako

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,6 @@ public final class ${ctx.lib_name.camel} {
322322

323323
/**
324324
* Convert a C Langkit exception to the LangkitException class.
325-
326-
* @param
327325
*/
328326
private static LangkitException wrapException(
329327
final LangkitExceptionNative exc
@@ -369,7 +367,7 @@ public final class ${ctx.lib_name.camel} {
369367
// ==========
370368

371369
/**
372-
* Interface to visit the AST.
370+
* Interface to visit the parse tree.
373371
*/
374372
public static interface BasicVisitor<T> {
375373
T visit(${root_node_type} node);
@@ -381,7 +379,7 @@ public final class ${ctx.lib_name.camel} {
381379
}
382380

383381
/**
384-
* Interface to visit the AST with a parameter.
382+
* Interface to visit the parse tree with a parameter.
385383
*/
386384
public static interface ParamVisitor<T, P> {
387385
T visit(${root_node_type} node, P param);
@@ -807,7 +805,7 @@ public final class ${ctx.lib_name.camel} {
807805
% endfor
808806
;
809807

810-
// ----- Class attributes
808+
// ----- Class attributes -----
811809

812810
/** Singleton that represents the none expcetion kind. */
813811
public static final ExceptionKind NONE =
@@ -1820,7 +1818,7 @@ public final class ${ctx.lib_name.camel} {
18201818

18211819
// ----- Class attributes -----
18221820

1823-
/** Singleton that represents the none diagnositc. */
1821+
/** Singleton that represents the none diagnostic. */
18241822
public static final Diagnostic NONE = new Diagnostic(
18251823
SourceLocationRange.NONE,
18261824
Text.NONE
@@ -1873,8 +1871,8 @@ public final class ${ctx.lib_name.camel} {
18731871
/**
18741872
* Wrap a pointer to a native diagnostic.
18751873
*
1876-
* @param pointer The pointer to the native diagnositc.
1877-
* @return The wrapped diagnositc.
1874+
* @param pointer The pointer to the native diagnostic.
1875+
* @return The wrapped diagnostic.
18781876
*/
18791877
static Diagnostic wrap(
18801878
final Pointer pointer
@@ -1886,7 +1884,7 @@ public final class ${ctx.lib_name.camel} {
18861884
* Wrap a diagnostic native value in the Java class.
18871885
*
18881886
* @param diagnosticNative The diagnostic NI native value.
1889-
* @return The newly wrapped diagnositc.
1887+
* @return The newly wrapped diagnostic.
18901888
*/
18911889
static Diagnostic wrap(
18921890
final DiagnosticNative diagnosticNative
@@ -2049,7 +2047,7 @@ public final class ${ctx.lib_name.camel} {
20492047

20502048
}
20512049

2052-
${c_doc('langkit.unit_provider_type')}
2050+
${java_doc('langkit.unit_provider_type', 4)}
20532051
public static final class UnitProvider implements AutoCloseable {
20542052

20552053
// ----- Class attributes -----
@@ -2141,7 +2139,7 @@ public final class ${ctx.lib_name.camel} {
21412139

21422140
}
21432141

2144-
${java_doc('langkit.event_handler_type')}
2142+
${java_doc('langkit.event_handler_type', 4)}
21452143
public static final class EventHandler implements AutoCloseable {
21462144

21472145
// ----- Class attributes -----
@@ -2687,9 +2685,9 @@ public final class ${ctx.lib_name.camel} {
26872685
if(o == this) return true;
26882686
if(!(o instanceof Token)) return false;
26892687
final Token other = (Token) o;
2690-
return other.tokenDataHandler.equals(this.tokenDataHandler) &&
2691-
other.tokenIndex == this.tokenIndex &&
2692-
other.triviaIndex == this.triviaIndex;
2688+
return this.tokenDataHandler.equals(other.tokenDataHandler) &&
2689+
this.tokenIndex == other.tokenIndex &&
2690+
this.triviaIndex == other.triviaIndex;
26932691
}
26942692

26952693
// ----- Inner classes -----
@@ -3071,6 +3069,26 @@ public final class ${ctx.lib_name.camel} {
30713069
return this.eventHandler;
30723070
}
30733071

3072+
// ----- Class methods -----
3073+
3074+
/**
3075+
* Increase the reference counter of the given context.
3076+
*
3077+
* @param context The context to increase the reference counter of.
3078+
*/
3079+
private static void increaseRefCounter(
3080+
final AnalysisContext context
3081+
) {
3082+
// Increase the context reference counter of the context if not null
3083+
if(!context.reference.isNull()) {
3084+
if(ImageInfo.inImageCode()) {
3085+
NI_LIB.${nat("context_incref")}(context.reference.ni());
3086+
} else {
3087+
JNI_LIB.${nat("context_incref")}(context.reference.jni());
3088+
}
3089+
}
3090+
}
3091+
30743092
// ----- Instance methods -----
30753093

30763094
/**
@@ -3260,24 +3278,6 @@ public final class ${ctx.lib_name.camel} {
32603278
}
32613279
% endif
32623280

3263-
/**
3264-
* Increase the reference counter of the given context.
3265-
*
3266-
* @param context The context to increase the reference counter of.
3267-
*/
3268-
private static void increaseRefCounter(
3269-
final AnalysisContext context
3270-
) {
3271-
// Increase the context reference counter of the context if not null
3272-
if(!context.reference.isNull()) {
3273-
if(ImageInfo.inImageCode()) {
3274-
NI_LIB.${nat("context_incref")}(context.reference.ni());
3275-
} else {
3276-
JNI_LIB.${nat("context_incref")}(context.reference.jni());
3277-
}
3278-
}
3279-
}
3280-
32813281
/** @see java.lang.AutoCloseable#close() */
32823282
@Override
32833283
public void close() {
@@ -3551,9 +3551,9 @@ public final class ${ctx.lib_name.camel} {
35513551
}
35523552

35533553
/**
3554-
* Get the list of assiated diagnositcs. Those are parsing errors.
3554+
* Get the list of associated diagnostics. Those are parsing errors.
35553555
*
3556-
* @return The diagnositcs of the unit.
3556+
* @return The diagnostics of the unit.
35573557
*/
35583558
public List<Diagnostic> getDiagnostics() {
35593559
final int diagnosticCount;
@@ -3605,7 +3605,7 @@ public final class ${ctx.lib_name.camel} {
36053605
if(this == o) return true;
36063606
if(!(o instanceof AnalysisUnit)) return false;
36073607
final AnalysisUnit other = (AnalysisUnit) o;
3608-
return other.reference.equals(other.reference);
3608+
return this.reference.equals(other.reference);
36093609
}
36103610

36113611
}
@@ -4096,28 +4096,28 @@ public final class ${ctx.lib_name.camel} {
40964096
// ----- Dumping methods -----
40974097

40984098
/**
4099-
* Return the AST in a string.
4099+
* Return the parsing tree in a string.
41004100
*
4101-
* @return The string containing the representation of the AST
4101+
* @return The string containing the representation of the parsing tree
41024102
* from the node.
41034103
*/
41044104
@CompilerDirectives.TruffleBoundary
4105-
public String dumpAST() {
4105+
public String dumpTree() {
41064106
final StringBuilder builder = new StringBuilder();
4107-
this.dumpAST(builder);
4107+
this.dumpTree(builder);
41084108
return builder.toString();
41094109
}
41104110

41114111
/**
4112-
* Dump the AST in the given string builder.
4112+
* Dump the parse tree in the given string builder.
41134113
*
4114-
* @param builder The builder to dump the AST in.
4114+
* @param builder The builder to dump the parse tree in.
41154115
*/
41164116
@CompilerDirectives.TruffleBoundary
4117-
public void dumpAST(
4117+
public void dumpTree(
41184118
final StringBuilder builder
41194119
) {
4120-
this.dumpAST(builder, "");
4120+
this.dumpTree(builder, "");
41214121
}
41224122

41234123
/**
@@ -4137,17 +4137,18 @@ public final class ${ctx.lib_name.camel} {
41374137
builder.append(indent)
41384138
.append(name)
41394139
.append(":\n");
4140-
value.dumpAST(builder, indent + " ");
4140+
value.dumpTree(builder, indent + " ");
41414141
}
41424142

41434143
/**
4144-
* Dump the AST in the given string builder with the indent level.
4144+
* Dump the parse tree in the given string builder with the indent
4145+
* level.
41454146
*
4146-
* @param builder The builder to dump the AST in.
4147+
* @param builder The builder to dump the tree in.
41474148
* @param indent The starting indent level.
41484149
*/
41494150
@CompilerDirectives.TruffleBoundary
4150-
protected void dumpAST(
4151+
protected void dumpTree(
41514152
final StringBuilder builder,
41524153
String indent
41534154
) {
@@ -4271,7 +4272,7 @@ public final class ${ctx.lib_name.camel} {
42714272

42724273
}
42734274

4274-
// ===== Generated AST node wrapping classes =====
4275+
// ===== Generated node wrapping classes =====
42754276

42764277
% for astnode in ctx.astnode_types:
42774278
% if astnode != T.root_node:

langkit/templates/java_api/readme_md.mako

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class Main {
4141
${ctx.lib_name.camel}.AnalysisContext.create();
4242
${ctx.lib_name.camel}.AnalysisUnit unit = ctx.getUnitFromFile(myFile);
4343

44-
// Dump the AST
44+
// Dump the parsed tree
4545
System.out.println(unit.root().dump());
4646

4747
}

testsuite/tests/java_api/general/common/BindingsTests.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@ private static void testUnitFileName() {
114114
) {
115115
// Get the unit for the foo.txt file
116116
AnalysisUnit unit = context.getUnitFromFile("foo.txt");
117+
AnalysisUnit other = context.getUnitFromFile("foo.txt");
118+
AnalysisUnit notSame =
119+
context.getUnitFromBuffer("var identifier", "bar.txt");
120+
System.out.println(
121+
unit.toString() + " != " + other + " = " + (unit != other)
122+
);
123+
System.out.println(
124+
unit.toString() + ".equals(" + other + ") = " +
125+
unit.equals(other)
126+
);
127+
System.out.println(
128+
unit.toString() + ".equals(" + notSame +
129+
") = " + unit.equals(notSame)
130+
);
117131
System.out.println(
118132
"Unit for the file " +
119133
new File(unit.getFileName()).getName()
@@ -168,7 +182,7 @@ private static void testTokens() {
168182
System.out.println("Text range with a NO_TOKEN = " +
169183
Token.textRange(first, none));
170184

171-
// Test the token equivalence
185+
// Test the token equivalence and equality
172186
AnalysisUnit eqUnit = context.getUnitFromBuffer(
173187
"null identifier example identifier example",
174188
"foo.txt"
@@ -189,6 +203,20 @@ private static void testTokens() {
189203
}
190204
current = current.next();
191205
}
206+
207+
Token left = eqUnit.getFirstToken();
208+
Token right = eqUnit.getFirstToken();
209+
while(!(left.isNone() || right.isNone())) {
210+
System.out.println(
211+
left.toString() + " != " + right + " = " + (left != right)
212+
);
213+
System.out.println(
214+
left.toString() + ".equals(" + right + ") = " +
215+
left.equals(right)
216+
);
217+
left = left.next();
218+
right = right.next();
219+
}
192220
}
193221

194222
// Display the footer
@@ -219,7 +247,7 @@ private static void testNodes() {
219247
"Unit root children = " + root.children().toString()
220248
);
221249
System.out.println(
222-
"Unit root AST dump = " + root.dumpAST()
250+
"Unit root tree dump = " + root.dumpTree()
223251
);
224252
System.out.println(
225253
"Unit root is a list node = " + root.isListType()

0 commit comments

Comments
 (0)