Skip to content

Commit df52c60

Browse files
committed
Add TypeMapper and provide pre-mapped template context
Introduce a TypeMapper and a set of Mapped* dataclasses to map IR types/items to target-language representations before templating. build_context now produces mapped files/items (preserving declaration order) and exposes both mapped data and the raw IR; generator.py was updated to consume MappedFile instances and build per-file contexts from the pre-mapped data. Many runtime type-mapping filters were removed in favor of using the mapped objects directly; Jinja helper filters were simplified and the loader logic cleaned up. Templates were updated to use the mapped item shapes (file.items, item.type, etc.). A sample IR (example/sample_ir.json) was added and related IR/serializer/model changes were made to support the refactor.
1 parent 45a3e7b commit df52c60

File tree

12 files changed

+1270
-577
lines changed

12 files changed

+1270
-577
lines changed

tools/bindgen/codegen/context.py

Lines changed: 567 additions & 16 deletions
Large diffs are not rendered by default.

tools/bindgen/codegen/generator.py

Lines changed: 39 additions & 382 deletions
Large diffs are not rendered by default.
Lines changed: 12 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{#- Per-file template that generates bindings for each IR source file -#}
2-
{#- This template is rendered once per IR file -#}
2+
{#- Items are rendered in declaration order -#}
33

44
{#- Import partial macros -#}
55
{% from 'partials/type.j2' import render_type %}
@@ -11,87 +11,32 @@
1111

1212
// =============================================================================
1313
// Auto-generated bindings for: {{ file_path }}
14-
// Language: {{ mapping.language | default("example") }}
14+
// Language: {{ mapping.language }}
1515
// =============================================================================
1616

1717
{% if is_empty %}
1818
// This file has no exported symbols.
1919
{% else %}
20-
// Summary:
21-
// Types: {{ types | length }}
22-
// Enums: {{ enums | length }}
23-
// Functions: {{ functions | length }}
24-
// Classes: {{ classes | length }}
25-
// Constants: {{ constants | length }}
26-
// Aliases: {{ aliases | length }}
20+
// Items: {{ file.items | length }}
2721

28-
{#- ========== Types (Structs) ========== -#}
29-
{% if has_types %}
30-
// -----------------------------------------------------------------------------
31-
// Types (Structs)
32-
// -----------------------------------------------------------------------------
33-
34-
{% for item in types %}
22+
{% for item in file.items %}
23+
{#- Render each item based on its kind -#}
24+
{% if item.kind == 'struct' %}
3525
{{ render_type(item) }}
36-
37-
{% endfor %}
38-
{% endif %}
39-
{#- ========== Enums ========== -#}
40-
{% if has_enums %}
41-
// -----------------------------------------------------------------------------
42-
// Enums
43-
// -----------------------------------------------------------------------------
44-
45-
{% for item in enums %}
26+
{% elif item.kind == 'enum' %}
4627
{{ render_enum(item) }}
47-
48-
{% endfor %}
49-
{% endif %}
50-
{#- ========== Functions ========== -#}
51-
{% if has_functions %}
52-
// -----------------------------------------------------------------------------
53-
// Functions
54-
// -----------------------------------------------------------------------------
55-
56-
{% for item in functions %}
28+
{% elif item.kind == 'function' %}
5729
{{ render_function(item) }}
58-
59-
{% endfor %}
60-
{% endif %}
61-
{#- ========== Classes ========== -#}
62-
{% if has_classes %}
63-
// -----------------------------------------------------------------------------
64-
// Classes
65-
// -----------------------------------------------------------------------------
66-
67-
{% for item in classes %}
30+
{% elif item.kind == 'class' %}
6831
{{ render_class(item) }}
69-
70-
{% endfor %}
71-
{% endif %}
72-
{#- ========== Constants ========== -#}
73-
{% if has_constants %}
74-
// -----------------------------------------------------------------------------
75-
// Constants
76-
// -----------------------------------------------------------------------------
77-
78-
{% for item in constants %}
32+
{% elif item.kind == 'constant' %}
7933
{{ render_constant(item) }}
80-
81-
{% endfor %}
82-
{% endif %}
83-
{#- ========== Aliases ========== -#}
84-
{% if has_aliases %}
85-
// -----------------------------------------------------------------------------
86-
// Type Aliases
87-
// -----------------------------------------------------------------------------
88-
89-
{% for item in aliases %}
34+
{% elif item.kind == 'alias' %}
9035
{{ render_alias(item) }}
36+
{% endif %}
9137

9238
{% endfor %}
9339
{% endif %}
94-
{% endif %}
9540
// =============================================================================
9641
// End of {{ file_name }}
9742
// =============================================================================

tools/bindgen/example/bindgen/template/partials/alias.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{#- Partial template for rendering a single type alias -#}
2-
{#- Expected context: item (IRAlias), type_mapping (TypeMappingConfig) -#}
2+
{#- Expected context: item (MappedAlias) -#}
33
{%- macro render_alias(item) -%}
44
// Alias: {{ item.name }}
55
// Source: {{ item.source_path }}
6-
type {{ item.name }} = {{ item.target | map_type }};
6+
type {{ item.name }} = {{ item.target }};
77
{%- endmacro -%}
88

99
{#- Render alias if item is provided directly in context -#}

tools/bindgen/example/bindgen/template/partials/class.j2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{#- Partial template for rendering a single class -#}
2-
{#- Expected context: item (IRClass), type_mapping (TypeMappingConfig) -#}
2+
{#- Expected context: item (MappedClass) -#}
33
{%- macro render_class(item) -%}
44
// Class: {{ item.name }}
55
// Source: {{ item.source_path }}
@@ -10,15 +10,15 @@ class {{ item.name }}{% if item.bases %} extends {{ item.bases | join(', ') }}{%
1010
{%- if item.fields %}
1111
// Fields
1212
{%- for field in item.fields %}
13-
{{ field.name }}: {{ field.type | map_type }};
13+
{{ field.name }}: {{ field.type }};
1414
{%- endfor %}
1515
{%- endif %}
1616
{%- if item.methods %}
1717

1818
// Methods
1919
{%- for method in item.methods %}
2020
{% if method.static %}static {% endif %}{% if method.const %}const {% endif %}{{ method.name }}(
21-
{%- for param in method.params %}{{ param.name }}: {{ param.type | map_type }}{% if not loop.last %}, {% endif %}{%- endfor %}) -> {{ method.return_type | map_type }};
21+
{%- for param in method.params %}{{ param.name }}: {{ param.type }}{% if not loop.last %}, {% endif %}{%- endfor %}) -> {{ method.return_type }};
2222
{%- endfor %}
2323
{%- endif %}
2424
}

tools/bindgen/example/bindgen/template/partials/constant.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{#- Partial template for rendering a single constant -#}
2-
{#- Expected context: item (IRConstant), type_mapping (TypeMappingConfig) -#}
2+
{#- Expected context: item (MappedConstant) -#}
33
{%- macro render_constant(item) -%}
44
// Constant: {{ item.name }}
55
// Source: {{ item.source_path }}
6-
const {{ item.name }}: {{ item.type | map_type }} = {{ item.value }};
6+
const {{ item.name }}: {{ item.type }} = {{ item.value }};
77
{%- endmacro -%}
88

99
{#- Render constant if item is provided directly in context -#}

tools/bindgen/example/bindgen/template/partials/enum.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{#- Partial template for rendering a single enum -#}
2-
{#- Expected context: item (IREnum) -#}
2+
{#- Expected context: item (MappedEnum) -#}
33
{%- macro render_enum(item) -%}
44
// Enum: {{ item.name }}
55
// Source: {{ item.source_path }}

tools/bindgen/example/bindgen/template/partials/function.j2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{#- Partial template for rendering a single function -#}
2-
{#- Expected context: item (IRFunction), type_mapping (TypeMappingConfig) -#}
2+
{#- Expected context: item (MappedFunction) -#}
33
{%- macro render_function(item) -%}
44
// Function: {{ item.name }}
55
// Source: {{ item.source_path }}
66
fn {{ item.name }}(
77
{%- for param in item.params %}
8-
{{ param.name }}: {{ param.type | map_type }}{% if param.nullable %} | null{% endif %}{% if not loop.last %},{% endif %}
8+
{{ param.name }}: {{ param.type }}{% if param.nullable %} | null{% endif %}{% if not loop.last %},{% endif %}
99

1010
{%- endfor %}
11-
) -> {{ item.return_type | map_type }};
11+
) -> {{ item.return_type }};
1212
{%- endmacro -%}
1313

1414
{#- Render function if item is provided directly in context -#}

tools/bindgen/example/bindgen/template/partials/type.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{#- Partial template for rendering a single struct type -#}
2-
{#- Expected context: item (IRStruct), type_mapping (TypeMappingConfig) -#}
2+
{#- Expected context: item (MappedStruct) -#}
33
{%- macro render_type(item) -%}
44
// Struct: {{ item.name }}
55
// Source: {{ item.source_path }}
66
struct {{ item.name }} {
77
{%- for field in item.fields %}
8-
{{ field.name }}: {{ field.type | map_type }};
8+
{{ field.name }}: {{ field.type }};
99
{%- endfor %}
1010
}
1111
{%- endmacro -%}

0 commit comments

Comments
 (0)