Skip to content

Commit 913b141

Browse files
committed
Factor out logic for emitting struct type.
1 parent 1b6327c commit 913b141

File tree

2 files changed

+13
-28
lines changed

2 files changed

+13
-28
lines changed

langkit/java_api.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,17 @@ def __init__(self,
257257

258258
# ----- Util methods -----
259259

260+
def should_emit_struct(self, struct_type: ct.StructType) -> bool:
261+
"""
262+
Whether this struct type should be emitted in the Java bindings.
263+
"""
264+
if struct_type.is_entity_type:
265+
return struct_type == T.root_node.entity
266+
else:
267+
return (struct_type is T.entity_info
268+
or struct_type is T.env_md
269+
or struct_type.exposed)
270+
260271
def get_struct_fields(self, struct: ct.StructType) -> list[StructField]:
261272
"""
262273
Get the structure fields as a list.

langkit/templates/java_api/jni_impl_c.mako

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,8 @@ jobject AnalysisUnit_wrap(JNIEnv *, ${analysis_unit_type});
104104
${analysis_unit_type} AnalysisUnit_unwrap(JNIEnv *, jobject);
105105

106106
% for struct_type in ctx.struct_types:
107-
% if struct_type.is_entity_type:
108-
% if struct_type is root_entity:
107+
% if api.should_emit_struct(struct_type):
109108
${struct.jni_c_decl(struct_type)}
110-
% endif
111-
% else:
112-
<%
113-
emit_struct = (
114-
struct_type is T.entity_info
115-
or struct_type is T.env_md
116-
or struct_type.exposed
117-
)
118-
%>
119-
% if emit_struct:
120-
${struct.jni_c_decl(struct_type)}
121-
% endif
122109
% endif
123110
% endfor
124111

@@ -2134,21 +2121,8 @@ ${api.jni_func_sig("unit_diagnostic", "jobject")}(
21342121
// ==========
21352122

21362123
% for struct_type in ctx.struct_types:
2137-
% if struct_type.is_entity_type:
2138-
% if struct_type is root_entity:
2139-
${struct.jni_c_impl(struct_type)}
2140-
% endif
2141-
% else:
2142-
<%
2143-
emit_struct = (
2144-
struct_type is T.entity_info
2145-
or struct_type is T.env_md
2146-
or struct_type.exposed
2147-
)
2148-
%>
2149-
% if emit_struct:
2124+
% if api.should_emit_struct(struct_type):
21502125
${struct.jni_c_impl(struct_type)}
2151-
% endif
21522126
% endif
21532127
% endfor
21542128

0 commit comments

Comments
 (0)