Skip to content

Commit

Permalink
Merge branch 'topic/java_api/ni_pointers' into 'master'
Browse files Browse the repository at this point in the history
Change 'Pointer' to 'WordPointer' when possible

Closes #770

See merge request eng/libadalang/langkit!1023
  • Loading branch information
HugoGGuerrier committed Mar 13, 2024
2 parents 2957667 + e0cf069 commit 7bbd9f3
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 80 deletions.
41 changes: 21 additions & 20 deletions langkit/java_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ class JavaAPISettings(AbstractAPISettings):
"PointerBase",
"Pointer",
"VoidPointer",
"WordPointer",
"CCharPointer",
"CCharPointerPointer",
"CDoublePointer",
Expand Down Expand Up @@ -628,12 +629,12 @@ def ni_reference_type(self, the_type: CompiledType) -> str:
(T.Bool, lambda _: "CCharPointer"),
(T.Int, lambda _: "CIntPointer"),
(T.Character, lambda _: "CIntPointer"),
(T.BigInt, lambda _: "Pointer"),
(T.String, lambda _: "Pointer"),
(T.AnalysisUnit, lambda _: "Pointer"),
(T.AnalysisContext, lambda _: "Pointer"),
(ct.ArrayType, lambda _: "Pointer"),
(ct.IteratorType, lambda _: "Pointer"),
(T.BigInt, lambda _: "WordPointer"),
(T.String, lambda _: "WordPointer"),
(T.AnalysisUnit, lambda _: "WordPointer"),
(T.AnalysisContext, lambda _: "WordPointer"),
(ct.ArrayType, lambda _: "WordPointer"),
(ct.IteratorType, lambda _: "WordPointer"),
(ct.EnumType, lambda _: "CIntPointer"),
(object, lambda t: self.ni_type(t))
])
Expand All @@ -646,7 +647,7 @@ def ni_stack_value(self, the_type: CompiledType) -> str:
"""
ref_type = self.ni_reference_type(the_type)
if ref_type in self.ni_pointer_types:
return "StackValue.get(SizeOf.get(VoidPointer.class))"
return "StackValue.get(SizeOf.get(WordPointer.class))"
else:
return f"StackValue.get({ref_type}.class)"

Expand Down Expand Up @@ -718,8 +719,9 @@ def ni_unwrap(self,
(T.Character, lambda _: f"{source}.value;"),
(
T.BigInt, lambda t: (
f"{self.ni_stack_value(t)};"
f"BigIntegerWrapper.unwrap({source}, {export});"
f"StackValue.get(SizeOf.get(WordPointer.class));"
f"BigIntegerWrapper.unwrap({source},"
f"(WordPointer) {export});"
)
),
(ct.EnumType, lambda _: f"{source}.toC();"),
Expand Down Expand Up @@ -771,21 +773,19 @@ def object_unwrap(self,
ref_type = self.ni_reference_type(the_type)

if ref_type not in self.ni_pointer_types:
res = (
f"{self.ni_stack_value(the_type)};"
return (
f"StackValue.get({ref_type}.class);"
f"{source}.unwrap({export});"
)

else:
if the_type.is_ada_record:
res = (
f"{self.ni_stack_value(the_type)};"
return (
f"StackValue.get({ref_type}.class);"
f"{source}.unwrap({export});"
)
else:
res = f"{source}.unwrap();"

return res
return f"{source}.unwrap();"

def ni_write(self,
the_type: CompiledType,
Expand All @@ -799,6 +799,7 @@ def ni_write(self,
:param source: The source Java expression to write.
:param pointer: The pointer to write in.
"""

return dispatch_on_type(the_type, [
(
T.Bool, lambda _:
Expand All @@ -821,15 +822,15 @@ def ni_write(self,
),
(
T.AnalysisContext, lambda _:
f"{pointer}.writeWord(0, {source}.unwrap());"
f"{pointer}.write({source}.unwrap());"
),
(
T.AnalysisUnit, lambda _:
f"{pointer}.writeWord(0, {source}.unwrap());"
f"{pointer}.write({source}.unwrap());"
),
(
ct.IteratorType, lambda _:
f"{pointer}.writeWord(0, {source}.unwrap());"
f"{pointer}.write({source}.unwrap());"
),
(
ct.ArrayType, lambda t:
Expand Down Expand Up @@ -925,7 +926,7 @@ def ni_field_unwrap(self, flat: FlatStructField) -> str:
(ct.StructType, lambda _: f"{getter}.unwrap({to_write});"),
(
object, lambda t:
f"{to_write}.writeWord(0, {getter}.unwrap());"
f"{to_write}.write({getter}.unwrap());"
),
])

Expand Down
12 changes: 6 additions & 6 deletions langkit/templates/java_api/array.mako
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@
* @return The newly wrapped array.
*/
static ${java_type} wrap(
final Pointer pointer
final WordPointer pointer
) {
return wrap((${ni_type}) pointer.readWord(0));
return wrap((${ni_type}) pointer.read());
}
/**
Expand Down Expand Up @@ -122,7 +122,7 @@
* in.
*/
void unwrap(
final Pointer pointer
final WordPointer pointer
${(
", final AnalysisContext currentContext"
if cls.element_type.is_symbol_type else
Expand All @@ -139,7 +139,7 @@
);
// Place the result in the pointer
pointer.writeWord(0, resNative);
pointer.write(resNative);
}
/**
Expand Down Expand Up @@ -188,9 +188,9 @@
* @param The pointer to the array to release.
*/
static void release(
final Pointer pointer
final WordPointer pointer
) {
release((${ni_type}) pointer.readWord(0));
release((${ni_type}) pointer.read());
}
/**
Expand Down
4 changes: 2 additions & 2 deletions langkit/templates/java_api/iterator.mako
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
* is null.
*/
static ${class_name} wrap(
Pointer niPointer
final WordPointer niPointer
) {
if(niPointer.isNull()) return null;
else return wrap((${ni_name}) niPointer.readWord(0));
else return wrap((${ni_name}) niPointer.read());
}
/**
Expand Down
Loading

0 comments on commit 7bbd9f3

Please sign in to comment.