Skip to content

Commit 1b42dae

Browse files
committed
Refactor IR JSON schema, serializer, and model
Change the bindgen IR to a compact, typed JSON schema and update tooling accordingly. Top-level JSON now uses a "files" map with grouped categories (types/enums/aliases/functions/classes/constants) and per-declaration metadata (source_path, decl_index). Type references were normalized (named/alias_ref/pointer/array/etc.), qualifiers/scoped only emitted when present, and function/method signatures simplified. Serializer and parser were rewritten with dedicated helpers for types/fields/params/methods/files and stronger validation. IR model refactored to introduce IRDecl base, typed buckets on IRFile, stable items() ordering by decl_index, and an append() helper. Normalizer now emits unqualified type names and computes decl_index from cursor location. README and sample_ir.json updated to the new format.
1 parent 93730d8 commit 1b42dae

File tree

5 files changed

+842
-605
lines changed

5 files changed

+842
-605
lines changed

tools/bindgen/README.md

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,16 @@ C++ Headers
8181

8282
### 类型系统(IRType)
8383

84+
- IR JSON 的类型引用采用“最小必要字段”编码,不再输出大量空字段
8485
- 基础类型:`void`, `bool`, `int8/16/32/64`, `uint8/16/32/64`, `float32/64`
8586
- 扩展基础类型:`size_t`, `ssize_t`, `intptr`, `uintptr`
86-
- 类型别名:`typedef/using` 归一化为 `alias`
87-
- 指针类型:`pointer<T>`
88-
- 数组类型:`array<T, N>`
89-
- 结构体:`struct { fields }`
90-
- 枚举:`enum { values }`
9187
- 字符串:`char*` 规范化为 `cstring`
92-
- 函数指针:`fnptr(ret, params)`(MVP 可忽略,后续支持)
93-
- 限定符:`const/volatile` 记录在 `qualifiers`
94-
- C++ `enum class` 归一化为 `enum`(保留 `scoped` 标记)
88+
- 命名类型引用:`{ "kind": "named", "name": "...", "type_kind": "struct|enum|class|alias|unknown" }`
89+
- 指针/引用:`pointer<T>`, `reference<T>`, `rvalue_reference<T>`
90+
- 数组:`array<T, N>`
91+
- 函数指针:`function_pointer`(MVP 先保留占位)
92+
- 限定符:仅在存在时输出 `qualifiers`
93+
- C++ `enum class` 归一化为 `enum`,定义节点保留 `scoped`
9594

9695
### 函数(IRFunction)
9796

@@ -102,67 +101,53 @@ C++ Headers
102101

103102
### 模块(IRModule)
104103

105-
- IR 输出为 `path -> IRFile` 的 map(不再包含 `headers` 节点)
104+
- IR JSON 顶层包含 `files`
105+
- `files``path -> IRFile` 的 map(不再包含 `headers` 节点)
106106
- `IRFile` 结构包含:
107107
- types
108108
- enums
109+
- aliases
109110
- functions
110111
- classes
111112
- constants
112-
- aliases
113-
- `source_path`:结构体/枚举/函数/别名/常量/类/方法/字段所在头文件路径(如 `src/foundation/color.h`
113+
- 顶层声明节点统一包含:
114+
- `source_path`
115+
- `decl_index`
116+
- `IRFile` 在 JSON 中按类别分组;空分组不会输出
117+
- Python 内部仍提供按声明顺序展开的 `items` 视图供模板消费
114118

115119
### IR JSON 示例(最小)
116120

117121
```json
118122
{
119-
"src/foundation/geometry.h": {
120-
"types": [
121-
{
122-
"kind": "struct",
123-
"name": "Point",
124-
"source_path": "src/foundation/geometry.h",
125-
"fields": [
126-
{"name": "x", "type": {"kind": "float32"}, "source_path": "src/foundation/geometry.h"},
127-
{"name": "y", "type": {"kind": "float32"}, "source_path": "src/foundation/geometry.h"}
128-
]
129-
}
130-
],
131-
"functions": [
132-
{
133-
"name": "na_distance",
134-
"source_path": "src/foundation/geometry.h",
135-
"return_type": {"kind": "float32"},
136-
"params": [
137-
{"name": "a", "type": {"kind": "pointer", "to": {"kind": "struct", "name": "Point"}}, "nullable": false},
138-
{"name": "b", "type": {"kind": "pointer", "to": {"kind": "struct", "name": "Point"}}, "nullable": false}
139-
]
140-
}
141-
],
142-
"enums": [],
143-
"classes": [],
144-
"constants": [],
145-
"aliases": []
146-
},
147-
"src/foundation/error.h": {
148-
"types": [],
149-
"enums": [],
150-
"functions": [],
151-
"classes": [],
152-
"constants": [
153-
{"name": "NA_OK", "type": {"kind": "int32"}, "value": 0, "source_path": "src/foundation/error.h"}
154-
],
155-
"aliases": []
156-
},
157-
"src/foundation/types.h": {
158-
"types": [],
159-
"enums": [],
160-
"functions": [],
161-
"classes": [],
162-
"constants": [],
163-
"aliases": [
164-
{"name": "NAHandle", "target": {"kind": "uintptr"}, "source_path": "src/foundation/types.h"}
165-
]
123+
"files": {
124+
"src/foundation/geometry.h": {
125+
"types": [
126+
{
127+
"kind": "struct",
128+
"name": "Point",
129+
"source_path": "src/foundation/geometry.h",
130+
"decl_index": 1008,
131+
"fields": [
132+
{"name": "x", "type": {"kind": "float32"}},
133+
{"name": "y", "type": {"kind": "float32"}}
134+
]
135+
}
136+
],
137+
"functions": [
138+
{
139+
"kind": "function",
140+
"name": "na_distance",
141+
"source_path": "src/foundation/geometry.h",
142+
"decl_index": 2008,
143+
"return_type": {"kind": "float32"},
144+
"params": [
145+
{"name": "a", "type": {"kind": "pointer", "to": {"kind": "named", "name": "Point", "type_kind": "struct"}}},
146+
{"name": "b", "type": {"kind": "pointer", "to": {"kind": "named", "name": "Point", "type_kind": "struct"}}}
147+
]
148+
}
149+
]
150+
}
166151
}
167152
}
168153
```

0 commit comments

Comments
 (0)