From e0cf069ba2359cf2d013e2d523cb56a42aed1f2a Mon Sep 17 00:00:00 2001 From: Hugo Guerrier Date: Mon, 10 Jul 2023 15:52:51 +0200 Subject: [PATCH] Change 'Pointer' to 'WordPointer' when possible Use the 'WordPointer' as much as possible when pointing to a word-like value. --- langkit/java_api.py | 41 ++++----- langkit/templates/java_api/array.mako | 12 +-- langkit/templates/java_api/iterator.mako | 4 +- langkit/templates/java_api/main_class.mako | 97 +++++++++++----------- langkit/templates/java_api/struct.mako | 6 +- 5 files changed, 80 insertions(+), 80 deletions(-) diff --git a/langkit/java_api.py b/langkit/java_api.py index f25d66851..bbe74000f 100644 --- a/langkit/java_api.py +++ b/langkit/java_api.py @@ -233,6 +233,7 @@ class JavaAPISettings(AbstractAPISettings): "PointerBase", "Pointer", "VoidPointer", + "WordPointer", "CCharPointer", "CCharPointerPointer", "CDoublePointer", @@ -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)) ]) @@ -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)" @@ -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();"), @@ -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, @@ -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 _: @@ -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: @@ -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());" ), ]) diff --git a/langkit/templates/java_api/array.mako b/langkit/templates/java_api/array.mako index e98967874..56c76a3f2 100644 --- a/langkit/templates/java_api/array.mako +++ b/langkit/templates/java_api/array.mako @@ -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()); } /** @@ -122,7 +122,7 @@ * in. */ void unwrap( - final Pointer pointer + final WordPointer pointer ${( ", final AnalysisContext currentContext" if cls.element_type.is_symbol_type else @@ -139,7 +139,7 @@ ); // Place the result in the pointer - pointer.writeWord(0, resNative); + pointer.write(resNative); } /** @@ -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()); } /** diff --git a/langkit/templates/java_api/iterator.mako b/langkit/templates/java_api/iterator.mako index 83eb0d1d6..af6fb44a0 100644 --- a/langkit/templates/java_api/iterator.mako +++ b/langkit/templates/java_api/iterator.mako @@ -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()); } /** diff --git a/langkit/templates/java_api/main_class.mako b/langkit/templates/java_api/main_class.mako index c2cbd136c..4dcb4ec80 100644 --- a/langkit/templates/java_api/main_class.mako +++ b/langkit/templates/java_api/main_class.mako @@ -1007,9 +1007,9 @@ public final class ${ctx.lib_name.camel} { * @return The Java big integer. */ static BigInteger wrap( - final Pointer pointer + final WordPointer pointer ) { - return wrap((BigIntegerNative) pointer.readWord(0)); + return wrap((BigIntegerNative) pointer.read()); } /** @@ -1026,17 +1026,16 @@ public final class ${ctx.lib_name.camel} { } /** - * Unwrap the given big integer in the given pointer as a native - * big integer. + * Unwrap the given Java big integer in the given word pointer. * * @param bigInteger The big integer to unwrap. - * @param pointer The pointer to place the big integer in. + * @param pointer The word pointer to place the big integer in. */ static void unwrap( final BigInteger bigInteger, - final Pointer pointer + final WordPointer pointer ) { - pointer.writeWord(0, unwrap(bigInteger)); + pointer.write(unwrap(bigInteger)); } /** @@ -1069,9 +1068,9 @@ public final class ${ctx.lib_name.camel} { * @param pointer The pointer to the big integer to release. */ static void release( - final Pointer pointer + final WordPointer pointer ) { - release((BigIntegerNative) pointer.readWord(0)); + release((BigIntegerNative) pointer.read()); } /** @@ -1162,9 +1161,9 @@ public final class ${ctx.lib_name.camel} { * @return The wrapped symbol. */ static Symbol wrap( - final Pointer pointer + final WordPointer pointer ) { - return wrap((SymbolNative) pointer.readWord(0)); + return wrap((SymbolNative) pointer.read()); } /** @@ -1246,9 +1245,9 @@ public final class ${ctx.lib_name.camel} { * @return The Java string wrapped from the native string. */ static String wrap( - final Pointer pointer + final WordPointer pointer ) { - return wrap((StringNative) pointer.readWord(0)); + return wrap((StringNative) pointer.read()); } /** @@ -1264,16 +1263,16 @@ public final class ${ctx.lib_name.camel} { } /** - * Unwrap the string in the given pointer. + * Unwrap the string in the given word pointer. * * @param string The string to unwrap. - * @param pointer The pointer to place the native string in. + * @param pointer The word pointer to place the native string in. */ static void unwrap( final String string, - final Pointer pointer + final WordPointer pointer ) { - pointer.writeWord(0, unwrap(string)); + pointer.write(unwrap(string)); } /** @@ -1294,9 +1293,9 @@ public final class ${ctx.lib_name.camel} { * @param pointer The pointer to the native string. */ static void release( - final Pointer pointer + final WordPointer pointer ) { - release((StringNative) pointer.readWord(0)); + release((StringNative) pointer.read()); } /** @@ -1511,9 +1510,9 @@ public final class ${ctx.lib_name.camel} { * @return The wrapped text. */ static Text wrap( - final Pointer pointer + final WordPointer pointer ) { - return wrap((TextNative) pointer.readWord(0)); + return wrap((TextNative) pointer.read()); } /** @@ -1658,9 +1657,9 @@ public final class ${ctx.lib_name.camel} { * @return The newly wrapper source location. */ static SourceLocation wrap( - final Pointer pointer + final WordPointer pointer ) { - return wrap((SourceLocationNative) pointer.readWord(0)); + return wrap((SourceLocationNative) pointer.read()); } /** @@ -1762,9 +1761,9 @@ public final class ${ctx.lib_name.camel} { * @return The newly wrapped source location range. */ static SourceLocationRange wrap( - final Pointer pointer + final WordPointer pointer ) { - return wrap((SourceLocationRangeNative) pointer.readWord(0)); + return wrap((SourceLocationRangeNative) pointer.read()); } /** @@ -1875,9 +1874,9 @@ public final class ${ctx.lib_name.camel} { * @return The wrapped diagnostic. */ static Diagnostic wrap( - final Pointer pointer + final WordPointer pointer ) { - return wrap((DiagnosticNative) pointer.readWord(0)); + return wrap((DiagnosticNative) pointer.read()); } /** @@ -1989,9 +1988,9 @@ public final class ${ctx.lib_name.camel} { * @return The newly wrapped file reader. */ static FileReader wrap( - final Pointer pointer + final WordPointer pointer ) { - return wrap((FileReaderNative) pointer.readWord(0)); + return wrap((FileReaderNative) pointer.read()); } /** @@ -2014,9 +2013,9 @@ public final class ${ctx.lib_name.camel} { * @param pointer The pointer to unwrap the file reader in. */ void unwrap( - final Pointer pointer + final WordPointer pointer ) { - pointer.writeWord(0, this.unwrap()); + pointer.write(0, this.unwrap()); } /** @@ -2084,9 +2083,9 @@ public final class ${ctx.lib_name.camel} { * @return The wrapped unit provider. */ static UnitProvider wrap( - final Pointer pointer + final WordPointer pointer ) { - return wrap((UnitProviderNative) pointer.readWord(0)); + return wrap((UnitProviderNative) pointer.read()); } /** @@ -2109,9 +2108,9 @@ public final class ${ctx.lib_name.camel} { * @param pointer The pointer to place the native unit provider in. */ void unwrap( - final Pointer pointer + final WordPointer pointer ) { - pointer.writeWord(0, this.unwrap()); + pointer.write(this.unwrap()); } /** @@ -2262,9 +2261,9 @@ public final class ${ctx.lib_name.camel} { * @return The wrapped event handler. */ EventHandler wrap( - final Pointer pointer + final WordPointer pointer ) { - return wrap((EventHandlerNative) pointer.readWord(0)); + return wrap((EventHandlerNative) pointer.read()); } /** @@ -2285,9 +2284,9 @@ public final class ${ctx.lib_name.camel} { * @param pointer The pointer to place the native event handler. */ void unwrap( - final Pointer pointer + final WordPointer pointer ) { - pointer.writeWord(0, this.unwrap()); + pointer.write(this.unwrap()); } /** @@ -2427,11 +2426,11 @@ public final class ${ctx.lib_name.camel} { * one. */ static Token wrap( - final Pointer pointer, + final WordPointer pointer, final AnalysisUnit unit ) { return wrap( - (TokenNative) pointer.readWord(0), + (TokenNative) pointer.read(), unit ); } @@ -3026,9 +3025,9 @@ public final class ${ctx.lib_name.camel} { * @return The newly wrapped analysis context. */ static AnalysisContext wrap( - final Pointer pointer + final WordPointer pointer ) { - return wrap((AnalysisContextNative) pointer.readWord(0)); + return wrap((AnalysisContextNative) pointer.read()); } /** @@ -3049,9 +3048,9 @@ public final class ${ctx.lib_name.camel} { * @param pointer The pointer to place the native analysis unit. */ void unwrap( - final Pointer pointer + final WordPointer pointer ) { - pointer.writeWord(0, this.unwrap()); + pointer.write(this.unwrap()); } /** @@ -3338,9 +3337,9 @@ public final class ${ctx.lib_name.camel} { * @return The newly wrapped analysis unit. */ static AnalysisUnit wrap( - final Pointer pointer + final WordPointer pointer ) { - return wrap((AnalysisUnitNative) pointer.readWord(0)); + return wrap((AnalysisUnitNative) pointer.read()); } /** @@ -3361,9 +3360,9 @@ public final class ${ctx.lib_name.camel} { * @param pointer The pointer to place the native analysis unit in. */ void unwrap( - final Pointer pointer + final WordPointer pointer ) { - pointer.writeWord(0, this.unwrap()); + pointer.write(this.unwrap()); } /** diff --git a/langkit/templates/java_api/struct.mako b/langkit/templates/java_api/struct.mako index ced617ce9..9a05ff1a5 100644 --- a/langkit/templates/java_api/struct.mako +++ b/langkit/templates/java_api/struct.mako @@ -77,9 +77,9 @@ * @return The newly wrapped structure. */ static ${java_type} wrap( - final Pointer niPointer + final WordPointer pointer ) { - return wrap((${ni_type}) niPointer.readWord(0)); + return wrap((${ni_type}) pointer.read()); } /** @@ -153,7 +153,7 @@ * @return The None instance because the structure is empty. */ static ${java_type} wrap( - final Pointer niPointer + final WordPointer pointer ) { return ${java_type}.NONE; }