l0n0lc 0.8.4__py3-none-any.whl → 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- l0n0lc/Py/350/275/254Cpp/350/275/254/350/257/221/345/231/250.py +579 -0
- l0n0lc/__init__.py +75 -6
- l0n0lc/aot/347/274/226/350/257/221.py +679 -0
- l0n0lc/ast/350/256/277/351/227/256/350/200/205.py +599 -0
- l0n0lc/cpp/347/261/273/345/236/213.py +330 -0
- l0n0lc/cpp/347/274/226/350/257/221/345/231/250.py +317 -0
- l0n0lc/simd/344/274/230/345/214/226.py +260 -0
- l0n0lc/std_map.py +153 -0
- l0n0lc/std_set.py +185 -0
- l0n0lc/std_vector.py +96 -0
- l0n0lc//344/273/243/347/240/201/344/274/230/345/214/226.py +302 -0
- l0n0lc//344/273/243/347/240/201/347/224/237/346/210/220.py +546 -0
- l0n0lc//344/276/235/350/265/226/346/263/250/345/205/245.py +155 -0
- l0n0lc//345/215/263/346/227/266/347/274/226/350/257/221.py +192 -0
- l0n0lc//345/217/230/351/207/217/347/256/241/347/220/206/345/231/250.py +123 -0
- l0n0lc//345/237/272/347/241/200/346/230/240/345/260/204.py +103 -0
- l0n0lc//345/237/272/347/241/200/346/267/267/345/205/245.py +147 -0
- l0n0lc//345/256/271/345/231/250/346/236/204/345/273/272/345/231/250.py +214 -0
- l0n0lc//345/267/245/345/205/267.py +285 -0
- l0n0lc//345/271/266/350/241/214/347/274/226/350/257/221/345/231/250.py +412 -0
- l0n0lc//345/274/202/345/270/270.py +474 -0
- l0n0lc//346/225/260/347/273/204/345/257/271/350/261/241/346/261/240.py +248 -0
- l0n0lc//346/226/207/344/273/266/347/256/241/347/220/206/345/231/250.py +286 -0
- l0n0lc//346/227/245/345/277/227/345/267/245/345/205/267.py +152 -0
- l0n0lc//347/261/273/345/236/213/346/216/250/346/226/255/345/267/245/345/205/267.py +352 -0
- l0n0lc//347/261/273/345/236/213/350/275/254/346/215/242.py +210 -0
- l0n0lc//347/261/273/346/224/257/346/214/201.py +372 -0
- l0n0lc//347/274/226/350/257/221/344/270/212/344/270/213/346/226/207.py +132 -0
- l0n0lc//347/274/226/350/257/221/347/256/241/347/220/206/345/231/250.py +171 -0
- l0n0lc//350/241/250/350/276/276/345/274/217/345/244/204/347/220/206.py +462 -0
- l0n0lc//350/275/254/350/257/221/345/231/250/345/267/245/345/205/267.py +49 -0
- l0n0lc//350/277/220/350/241/214/346/227/266/345/212/240/350/275/275.py +217 -0
- l0n0lc//351/200/232/347/224/250/345/267/245/345/205/267.py +149 -0
- l0n0lc-1.0.0.dist-info/METADATA +363 -0
- l0n0lc-1.0.0.dist-info/RECORD +39 -0
- {l0n0lc-0.8.4.dist-info → l0n0lc-1.0.0.dist-info}/WHEEL +1 -1
- l0n0lc-1.0.0.dist-info/entry_points.txt +2 -0
- {l0n0lc-0.8.4.dist-info → l0n0lc-1.0.0.dist-info}/licenses/LICENSE +0 -0
- l0n0lc/StdList.py +0 -24
- l0n0lc/StdMap.py +0 -21
- l0n0lc/c/345/237/272/347/241/200/345/244/204/347/220/206.py +0 -207
- l0n0lc/jit.py +0 -604
- l0n0lc//347/274/226/350/257/221.py +0 -58
- l0n0lc//351/200/232/347/224/250.py +0 -134
- l0n0lc-0.8.4.dist-info/METADATA +0 -241
- l0n0lc-0.8.4.dist-info/RECORD +0 -12
- {l0n0lc-0.8.4.dist-info → l0n0lc-1.0.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
"""
|
|
2
|
+
类型推断工具模块 - 统一处理类型推断逻辑
|
|
3
|
+
消除代码中重复的类型推断和返回类型推断逻辑
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import ast
|
|
7
|
+
import inspect
|
|
8
|
+
import ctypes
|
|
9
|
+
from typing import Any, Optional, Set, Union, TYPE_CHECKING
|
|
10
|
+
from .工具 import 全局上下文
|
|
11
|
+
from .类型转换 import 类型转换器
|
|
12
|
+
from .通用工具 import 解析自定义类型
|
|
13
|
+
from .日志工具 import 日志
|
|
14
|
+
|
|
15
|
+
if TYPE_CHECKING:
|
|
16
|
+
from _ctypes import _CData
|
|
17
|
+
else:
|
|
18
|
+
# 运行时使用ctypes._SimpleCData作为类型替代
|
|
19
|
+
_CData = ctypes._SimpleCData
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class 类型推断工具:
|
|
23
|
+
"""统一的类型推断工具类"""
|
|
24
|
+
|
|
25
|
+
@staticmethod
|
|
26
|
+
def 从注解推断返回类型(node: ast.FunctionDef, transpiler) -> Optional[str]:
|
|
27
|
+
"""
|
|
28
|
+
从函数注解推断返回类型
|
|
29
|
+
|
|
30
|
+
Args:
|
|
31
|
+
node: 函数定义节点
|
|
32
|
+
transpiler: 转译器实例
|
|
33
|
+
|
|
34
|
+
Returns:
|
|
35
|
+
Optional[str]: C++返回类型,如果无法推断则返回None
|
|
36
|
+
"""
|
|
37
|
+
if node.returns:
|
|
38
|
+
return 解析自定义类型(node.returns, transpiler)
|
|
39
|
+
return None
|
|
40
|
+
|
|
41
|
+
@staticmethod
|
|
42
|
+
def 从返回语句推断类型(return_nodes: list, transpiler) -> Optional[str]:
|
|
43
|
+
"""
|
|
44
|
+
从返回语句推断类型
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
return_nodes: 返回语句节点列表
|
|
48
|
+
transpiler: 转译器实例
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
Optional[str]: 推断的C++类型
|
|
52
|
+
"""
|
|
53
|
+
if not return_nodes:
|
|
54
|
+
return None
|
|
55
|
+
|
|
56
|
+
# 分析所有返回语句的值类型
|
|
57
|
+
return_types = set()
|
|
58
|
+
for ret_node in return_nodes:
|
|
59
|
+
if ret_node.value:
|
|
60
|
+
# 获取返回值的类型
|
|
61
|
+
value_type = 类型推断工具._从表达式推断类型(ret_node.value, transpiler)
|
|
62
|
+
if value_type:
|
|
63
|
+
return_types.add(value_type)
|
|
64
|
+
|
|
65
|
+
# 如果所有返回值类型一致,返回该类型
|
|
66
|
+
if len(return_types) == 1:
|
|
67
|
+
return return_types.pop()
|
|
68
|
+
|
|
69
|
+
# 如果有多种类型,返回默认类型
|
|
70
|
+
return None
|
|
71
|
+
|
|
72
|
+
@staticmethod
|
|
73
|
+
def _从表达式推断类型(expr: ast.expr, transpiler) -> Optional[str]:
|
|
74
|
+
"""
|
|
75
|
+
从表达式推断类型
|
|
76
|
+
|
|
77
|
+
Args:
|
|
78
|
+
expr: 表达式节点
|
|
79
|
+
transpiler: 转译器实例
|
|
80
|
+
|
|
81
|
+
Returns:
|
|
82
|
+
Optional[str]: 推断的C++类型
|
|
83
|
+
"""
|
|
84
|
+
if isinstance(expr, ast.Constant):
|
|
85
|
+
# 常量类型推断
|
|
86
|
+
if isinstance(expr.value, int):
|
|
87
|
+
return "int"
|
|
88
|
+
elif isinstance(expr.value, float):
|
|
89
|
+
return "double"
|
|
90
|
+
elif isinstance(expr.value, str):
|
|
91
|
+
return "std::string"
|
|
92
|
+
elif isinstance(expr.value, bool):
|
|
93
|
+
return "bool"
|
|
94
|
+
elif isinstance(expr, ast.Name):
|
|
95
|
+
# 变量名类型推断
|
|
96
|
+
var = transpiler.获取C变量(expr.id)
|
|
97
|
+
if var:
|
|
98
|
+
return var.类型
|
|
99
|
+
elif isinstance(expr, ast.Call):
|
|
100
|
+
# 函数调用类型推断
|
|
101
|
+
return 类型推断工具._从函数调用推断类型(expr, transpiler)
|
|
102
|
+
elif isinstance(expr, ast.BinOp):
|
|
103
|
+
# 二元运算类型推断
|
|
104
|
+
return 类型推断工具._从二元运算推断类型(expr, transpiler)
|
|
105
|
+
|
|
106
|
+
return None
|
|
107
|
+
|
|
108
|
+
@staticmethod
|
|
109
|
+
def _从函数调用推断类型(call: ast.Call, transpiler) -> Optional[str]:
|
|
110
|
+
"""
|
|
111
|
+
从函数调用推断返回类型
|
|
112
|
+
|
|
113
|
+
Args:
|
|
114
|
+
call: 函数调用节点
|
|
115
|
+
transpiler: 转译器实例
|
|
116
|
+
|
|
117
|
+
Returns:
|
|
118
|
+
Optional[str]: 推断的C++类型
|
|
119
|
+
"""
|
|
120
|
+
if isinstance(call.func, ast.Name):
|
|
121
|
+
func_name = call.func.id
|
|
122
|
+
|
|
123
|
+
# 检查是否是已知的内置函数
|
|
124
|
+
if func_name in 全局上下文.Python内置映射:
|
|
125
|
+
py_func = 全局上下文.Python内置映射[func_name]
|
|
126
|
+
if hasattr(py_func, '__annotations__') and 'return' in py_func.__annotations__:
|
|
127
|
+
return 解析自定义类型(py_func.__annotations__['return'], transpiler)
|
|
128
|
+
|
|
129
|
+
# 检查函数映射表
|
|
130
|
+
for mapped_func, mapping in 全局上下文.函数映射表.items():
|
|
131
|
+
if mapped_func.__name__ == func_name:
|
|
132
|
+
# 可以从映射中推断返回类型
|
|
133
|
+
if hasattr(mapped_func, '__annotations__') and 'return' in mapped_func.__annotations__:
|
|
134
|
+
return 解析自定义类型(mapped_func.__annotations__['return'], transpiler)
|
|
135
|
+
|
|
136
|
+
return None
|
|
137
|
+
|
|
138
|
+
@staticmethod
|
|
139
|
+
def _从二元运算推断类型(binop: ast.BinOp, transpiler) -> Optional[str]:
|
|
140
|
+
"""
|
|
141
|
+
从二元运算推断类型
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
binop: 二元运算节点
|
|
145
|
+
transpiler: 转译器实例
|
|
146
|
+
|
|
147
|
+
Returns:
|
|
148
|
+
Optional[str]: 推断的C++类型
|
|
149
|
+
"""
|
|
150
|
+
left_type = 类型推断工具._从表达式推断类型(binop.left, transpiler)
|
|
151
|
+
right_type = 类型推断工具._从表达式推断类型(binop.right, transpiler)
|
|
152
|
+
|
|
153
|
+
# 简单的类型提升规则
|
|
154
|
+
if left_type and right_type:
|
|
155
|
+
if left_type == right_type:
|
|
156
|
+
return left_type
|
|
157
|
+
elif left_type == "double" or right_type == "double":
|
|
158
|
+
return "double"
|
|
159
|
+
elif left_type == "int" and right_type == "int":
|
|
160
|
+
return "int"
|
|
161
|
+
|
|
162
|
+
return None
|
|
163
|
+
|
|
164
|
+
@staticmethod
|
|
165
|
+
def 推断函数返回类型(func: Any, transpiler) -> str:
|
|
166
|
+
"""
|
|
167
|
+
推断函数的返回类型
|
|
168
|
+
|
|
169
|
+
Args:
|
|
170
|
+
func: 函数对象
|
|
171
|
+
transpiler: 转译器实例
|
|
172
|
+
|
|
173
|
+
Returns:
|
|
174
|
+
str: C++返回类型
|
|
175
|
+
"""
|
|
176
|
+
# 1. 检查类型注解
|
|
177
|
+
if hasattr(func, '__annotations__') and 'return' in func.__annotations__:
|
|
178
|
+
return 解析自定义类型(func.__annotations__['return'], transpiler)
|
|
179
|
+
|
|
180
|
+
# 2. 检查函数映射
|
|
181
|
+
mapped_type = 全局上下文.类型映射表.get(func)
|
|
182
|
+
if mapped_type:
|
|
183
|
+
return mapped_type.目标类型
|
|
184
|
+
|
|
185
|
+
# 3. 默认返回类型
|
|
186
|
+
return "void"
|
|
187
|
+
|
|
188
|
+
@staticmethod
|
|
189
|
+
def 解析类型注解(annotation: Any, transpiler) -> str:
|
|
190
|
+
"""
|
|
191
|
+
解析类型注解
|
|
192
|
+
|
|
193
|
+
Args:
|
|
194
|
+
annotation: 类型注解
|
|
195
|
+
transpiler: 转译器实例
|
|
196
|
+
|
|
197
|
+
Returns:
|
|
198
|
+
str: C++类型
|
|
199
|
+
"""
|
|
200
|
+
if isinstance(annotation, str):
|
|
201
|
+
# 字符串类型的注解
|
|
202
|
+
return annotation
|
|
203
|
+
else:
|
|
204
|
+
# AST节点或类型对象
|
|
205
|
+
return 解析自定义类型(annotation, transpiler)
|
|
206
|
+
|
|
207
|
+
@staticmethod
|
|
208
|
+
def 推断参数类型(node: ast.arg, transpiler) -> tuple[str, Any, Optional[_CData]]:
|
|
209
|
+
"""
|
|
210
|
+
统一的参数类型推断逻辑
|
|
211
|
+
|
|
212
|
+
Args:
|
|
213
|
+
node: 参数节点
|
|
214
|
+
transpiler: 转译器实例
|
|
215
|
+
|
|
216
|
+
Returns:
|
|
217
|
+
tuple: (C++类型, Python类型, ctypes类型)
|
|
218
|
+
"""
|
|
219
|
+
name = node.arg
|
|
220
|
+
|
|
221
|
+
if node.annotation is None:
|
|
222
|
+
# 对于类方法的self/cls参数,不需要类型注解
|
|
223
|
+
if hasattr(transpiler, '是否为类') and transpiler.是否为类 and name in ["self", "cls"]:
|
|
224
|
+
# 类型转换:ctypes.c_voidp 继承自 _SimpleCData,而 _SimpleCData 继承自 _CData
|
|
225
|
+
return "void*", type(None), ctypes.c_voidp # type: ignore
|
|
226
|
+
else:
|
|
227
|
+
from .通用工具 import 统一抛出错误
|
|
228
|
+
统一抛出错误(transpiler, f"Argument '{name}' must have type annotation", node)
|
|
229
|
+
|
|
230
|
+
# 使用表达式访问者获取类型
|
|
231
|
+
from .表达式处理 import 表达式访问者
|
|
232
|
+
expr_visitor = 表达式访问者(transpiler)
|
|
233
|
+
py_type = expr_visitor.获取值(node.annotation, is_type_annotation=True)
|
|
234
|
+
|
|
235
|
+
# 处理字符串类型注解
|
|
236
|
+
if isinstance(py_type, str) and py_type not in ["int", "float", "str", "bool", "void", "bytes"]:
|
|
237
|
+
if py_type in transpiler.全局变量:
|
|
238
|
+
potential_class = transpiler.全局变量[py_type]
|
|
239
|
+
if inspect.isclass(potential_class):
|
|
240
|
+
py_type = potential_class
|
|
241
|
+
日志.类型推断信息(name, str(potential_class), potential_class.__name__)
|
|
242
|
+
|
|
243
|
+
# 获取C++类型
|
|
244
|
+
c_type = 解析自定义类型(py_type, transpiler)
|
|
245
|
+
|
|
246
|
+
# 获取ctypes类型
|
|
247
|
+
ctypes_type = 类型转换器.Python类型转ctypes(py_type) if not hasattr(transpiler, '可执行文件名') or not transpiler.可执行文件名 else None
|
|
248
|
+
|
|
249
|
+
return c_type, py_type, ctypes_type
|
|
250
|
+
|
|
251
|
+
@staticmethod
|
|
252
|
+
def 处理列表参数类型(name: str, c_type: str, py_type: Any, transpiler) -> tuple:
|
|
253
|
+
"""
|
|
254
|
+
处理列表参数类型的统一逻辑
|
|
255
|
+
|
|
256
|
+
Args:
|
|
257
|
+
name: 参数名
|
|
258
|
+
c_type: C++类型
|
|
259
|
+
py_type: Python类型
|
|
260
|
+
transpiler: 转译器实例
|
|
261
|
+
|
|
262
|
+
Returns:
|
|
263
|
+
tuple: (指针变量, 长度变量, ctypes参数类型列表)
|
|
264
|
+
"""
|
|
265
|
+
if not str(c_type).startswith("std::vector"):
|
|
266
|
+
return None, None, []
|
|
267
|
+
|
|
268
|
+
# 添加vector头文件
|
|
269
|
+
transpiler.包含头文件.add("<vector>")
|
|
270
|
+
|
|
271
|
+
# 提取基础类型
|
|
272
|
+
base_type = str(c_type)[12:-1] # std::vector<int> -> int
|
|
273
|
+
|
|
274
|
+
# 创建指针和长度变量
|
|
275
|
+
ptr_name = f"{name}_ptr"
|
|
276
|
+
len_name = f"{name}_len"
|
|
277
|
+
|
|
278
|
+
from .cpp类型 import C变量
|
|
279
|
+
ptr_var = C变量(f"{base_type}*", ptr_name, True)
|
|
280
|
+
len_var = C变量("int64_t", len_name, True)
|
|
281
|
+
|
|
282
|
+
# 处理ctypes类型
|
|
283
|
+
ctypes_types = []
|
|
284
|
+
if not hasattr(transpiler, '可执行文件名') or not transpiler.可执行文件名:
|
|
285
|
+
origin = getattr(py_type, "__origin__", None)
|
|
286
|
+
args = getattr(py_type, "__args__", [])
|
|
287
|
+
if origin is list and args:
|
|
288
|
+
elem_type = args[0]
|
|
289
|
+
ctypes_elem = 类型转换器.Python类型转ctypes(elem_type)
|
|
290
|
+
ctypes_types = [ctypes.POINTER(ctypes_elem), ctypes.c_int64]
|
|
291
|
+
else:
|
|
292
|
+
from .通用工具 import 统一抛出错误
|
|
293
|
+
统一抛出错误(transpiler, f"Complex list type {py_type} not supported for JIT args", None) # type: ignore
|
|
294
|
+
|
|
295
|
+
return ptr_var, len_var, ctypes_types
|
|
296
|
+
|
|
297
|
+
@staticmethod
|
|
298
|
+
def 检查并添加容器头文件(c_type: str, transpiler):
|
|
299
|
+
"""
|
|
300
|
+
检查并添加容器类型的头文件
|
|
301
|
+
|
|
302
|
+
Args:
|
|
303
|
+
c_type: C++类型
|
|
304
|
+
transpiler: 转译器实例
|
|
305
|
+
"""
|
|
306
|
+
if str(c_type).startswith("std::unordered_set"):
|
|
307
|
+
transpiler.包含头文件.add("<unordered_set>")
|
|
308
|
+
elif str(c_type).startswith("std::unordered_map"):
|
|
309
|
+
transpiler.包含头文件.add("<unordered_map>")
|
|
310
|
+
# 如果键或值类型包含string,添加string头文件
|
|
311
|
+
if "std::string" in str(c_type):
|
|
312
|
+
transpiler.包含头文件.add("<string>")
|
|
313
|
+
elif str(c_type).startswith("std::vector"):
|
|
314
|
+
transpiler.包含头文件.add("<vector>")
|
|
315
|
+
elif str(c_type).startswith("std::string"):
|
|
316
|
+
transpiler.包含头文件.add("<string>")
|
|
317
|
+
|
|
318
|
+
@staticmethod
|
|
319
|
+
def 推断变量类型(node: ast.AST, transpiler) -> Optional[str]:
|
|
320
|
+
"""
|
|
321
|
+
统一的变量类型推断
|
|
322
|
+
|
|
323
|
+
Args:
|
|
324
|
+
node: AST节点
|
|
325
|
+
transpiler: 转译器实例
|
|
326
|
+
|
|
327
|
+
Returns:
|
|
328
|
+
Optional[str]: 推断的C++类型
|
|
329
|
+
"""
|
|
330
|
+
# 类型检查:确保node是ast.expr类型
|
|
331
|
+
if not isinstance(node, ast.expr):
|
|
332
|
+
return None
|
|
333
|
+
return 类型推断工具._从表达式推断类型(node, transpiler)
|
|
334
|
+
|
|
335
|
+
@staticmethod
|
|
336
|
+
def 验证类型一致性(types: Set[str], context: str, transpiler) -> bool:
|
|
337
|
+
"""
|
|
338
|
+
验证类型一致性
|
|
339
|
+
|
|
340
|
+
Args:
|
|
341
|
+
types: 类型集合
|
|
342
|
+
context: 上下文信息
|
|
343
|
+
transpiler: 转译器实例
|
|
344
|
+
|
|
345
|
+
Returns:
|
|
346
|
+
bool: 类型是否一致
|
|
347
|
+
"""
|
|
348
|
+
if len(types) <= 1:
|
|
349
|
+
return True
|
|
350
|
+
|
|
351
|
+
日志.警告(f"类型不一致 {context}: 发现 {len(types)} 种不同类型")
|
|
352
|
+
return False
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import ctypes
|
|
2
|
+
from typing import Any, Dict, Set, Tuple, Union, List, get_origin, get_args
|
|
3
|
+
from .工具 import 全局上下文
|
|
4
|
+
from .基础映射 import *
|
|
5
|
+
|
|
6
|
+
class 类型转换器:
|
|
7
|
+
"""处理Python类型到C++类型和ctypes类型的转换"""
|
|
8
|
+
|
|
9
|
+
@staticmethod
|
|
10
|
+
def Python类型转C类型(py_type: Any, 类型实例=None) -> Any:
|
|
11
|
+
"""
|
|
12
|
+
将Python类型转换为C++类型字符串
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
py_type: Python类型对象或类型字符串
|
|
16
|
+
类型实例: 类型实例(可选),用于推断返回类型明确的函数调用等
|
|
17
|
+
|
|
18
|
+
Returns:
|
|
19
|
+
C++类型字符串
|
|
20
|
+
"""
|
|
21
|
+
# 2. 检查全局类型映射表
|
|
22
|
+
ret = 全局上下文.类型映射表.get(py_type)
|
|
23
|
+
if ret is not None:
|
|
24
|
+
return ret.目标类型
|
|
25
|
+
|
|
26
|
+
# 4. 处理泛型类型
|
|
27
|
+
origin = get_origin(py_type)
|
|
28
|
+
args = get_args(py_type)
|
|
29
|
+
|
|
30
|
+
# 4.1 Union types
|
|
31
|
+
if origin is Union:
|
|
32
|
+
from .cpp类型 import Cpp类型
|
|
33
|
+
return Cpp类型.任意
|
|
34
|
+
|
|
35
|
+
# 4.2 List[...] -> std::vector<...>
|
|
36
|
+
if origin is list:
|
|
37
|
+
if args:
|
|
38
|
+
elem_type = 类型转换器.Python类型转C类型(args[0])
|
|
39
|
+
return f"std::vector<{elem_type}>"
|
|
40
|
+
|
|
41
|
+
# 4.3 Dict[K, V] -> std::unordered_map<K, V>
|
|
42
|
+
if origin is dict:
|
|
43
|
+
if len(args) == 2:
|
|
44
|
+
key_type = 类型转换器.Python类型转C类型(args[0])
|
|
45
|
+
val_type = 类型转换器.Python类型转C类型(args[1])
|
|
46
|
+
return f"std::unordered_map<{key_type}, {val_type}>"
|
|
47
|
+
|
|
48
|
+
# 4.4 Set[T] -> std::unordered_set<T>
|
|
49
|
+
if origin is set:
|
|
50
|
+
if args:
|
|
51
|
+
elem_type = 类型转换器.Python类型转C类型(args[0])
|
|
52
|
+
return f"std::unordered_set<{elem_type}>"
|
|
53
|
+
|
|
54
|
+
# 4.5 Tuple[T1, T2...] -> std::tuple<T1, T2...> or std::pair<T1, T2>
|
|
55
|
+
if origin is tuple:
|
|
56
|
+
if args:
|
|
57
|
+
# Check for Tuple[int, ...] (variable length, homogenous)
|
|
58
|
+
if len(args) == 2 and args[1] is Ellipsis:
|
|
59
|
+
elem_type = 类型转换器.Python类型转C类型(args[0])
|
|
60
|
+
# Map to vector for variable length tuple
|
|
61
|
+
return f"std::vector<{elem_type}>"
|
|
62
|
+
|
|
63
|
+
arg_types = [类型转换器.Python类型转C类型(arg) for arg in args]
|
|
64
|
+
if len(arg_types) == 2:
|
|
65
|
+
return f"std::pair<{arg_types[0]}, {arg_types[1]}>"
|
|
66
|
+
return f'std::tuple<{", ".join(arg_types)}>'
|
|
67
|
+
|
|
68
|
+
# 5. None类型处理 - 映射到void或特殊标记
|
|
69
|
+
if py_type is type(None):
|
|
70
|
+
return "void" # 或者使用特殊标记如 "NoneType"
|
|
71
|
+
|
|
72
|
+
# 6. 直接传入的字符串类型名称 (例如 'int')
|
|
73
|
+
if isinstance(py_type, str):
|
|
74
|
+
return py_type
|
|
75
|
+
|
|
76
|
+
# 7. 特殊处理:如果类型实例是函数调用且已知返回类型
|
|
77
|
+
if (
|
|
78
|
+
类型实例
|
|
79
|
+
and hasattr(类型实例, "返回C类型")
|
|
80
|
+
and 类型实例.返回C类型 is not None
|
|
81
|
+
):
|
|
82
|
+
return 类型实例.返回C类型
|
|
83
|
+
|
|
84
|
+
raise TypeError(f"Unsupported type: {py_type}")
|
|
85
|
+
|
|
86
|
+
@staticmethod
|
|
87
|
+
def Python类型转ctypes(py_type: Any) -> Any:
|
|
88
|
+
"""
|
|
89
|
+
将Python类型转换为ctypes类型
|
|
90
|
+
|
|
91
|
+
Args:
|
|
92
|
+
py_type: Python类型对象
|
|
93
|
+
|
|
94
|
+
Returns:
|
|
95
|
+
ctypes类型对象
|
|
96
|
+
"""
|
|
97
|
+
# 2. 检查全局类型映射表
|
|
98
|
+
ret = 全局上下文.类型映射表.get(py_type)
|
|
99
|
+
if ret is not None and ret.ctypes类型 is not None:
|
|
100
|
+
return ret.ctypes类型
|
|
101
|
+
|
|
102
|
+
# 4. 处理特殊类型
|
|
103
|
+
if py_type in (None, "None", type(None)):
|
|
104
|
+
return None
|
|
105
|
+
|
|
106
|
+
# 5. 处理泛型类型
|
|
107
|
+
origin = get_origin(py_type)
|
|
108
|
+
args = get_args(py_type)
|
|
109
|
+
|
|
110
|
+
# 5.1 List[T] -> POINTER(T)
|
|
111
|
+
if origin is list and args:
|
|
112
|
+
elem_type = 类型转换器.Python类型转ctypes(args[0])
|
|
113
|
+
return ctypes.POINTER(elem_type)
|
|
114
|
+
|
|
115
|
+
# 5.2 处理类型字符串
|
|
116
|
+
if isinstance(py_type, str):
|
|
117
|
+
if py_type == "void":
|
|
118
|
+
return None
|
|
119
|
+
# 其他字符串类型可能是自定义类名,无法确定具体的ctypes类型
|
|
120
|
+
return ctypes.c_void_p
|
|
121
|
+
# 5.3 默认返回void指针
|
|
122
|
+
return ctypes.c_void_p
|
|
123
|
+
|
|
124
|
+
@staticmethod
|
|
125
|
+
def C类型转Python(c_type: str) -> Any:
|
|
126
|
+
"""
|
|
127
|
+
将C++类型字符串转换为Python类型对象
|
|
128
|
+
|
|
129
|
+
Args:
|
|
130
|
+
c_type: C++类型字符串
|
|
131
|
+
|
|
132
|
+
Returns:
|
|
133
|
+
Python类型对象
|
|
134
|
+
"""
|
|
135
|
+
# 直接匹配
|
|
136
|
+
ret = 全局上下文.反向类型映射表.get(c_type)
|
|
137
|
+
if ret is not None:
|
|
138
|
+
return ret
|
|
139
|
+
|
|
140
|
+
# 处理容器类型
|
|
141
|
+
if c_type.startswith("std::vector<"):
|
|
142
|
+
# 提取元素类型
|
|
143
|
+
elem_type_str = c_type[11:-1] # 去掉 "std::vector<" 和 ">"
|
|
144
|
+
elem_type = 类型转换器.C类型转Python(elem_type_str)
|
|
145
|
+
if elem_type:
|
|
146
|
+
return List[elem_type]
|
|
147
|
+
else:
|
|
148
|
+
return List[int] # 默认元素类型
|
|
149
|
+
elif c_type.startswith("std::unordered_map<"):
|
|
150
|
+
# 提取键值类型
|
|
151
|
+
content = c_type[18:-1] # 去掉 "std::unordered_map<" 和 ">"
|
|
152
|
+
if ", " in content:
|
|
153
|
+
key_type_str, val_type_str = content.split(", ", 1)
|
|
154
|
+
key_type = 类型转换器.C类型转Python(key_type_str)
|
|
155
|
+
val_type = 类型转换器.C类型转Python(val_type_str)
|
|
156
|
+
if key_type and val_type:
|
|
157
|
+
return Dict[key_type, val_type]
|
|
158
|
+
return Dict[int, int]
|
|
159
|
+
|
|
160
|
+
elif c_type.startswith("std::unordered_set<"):
|
|
161
|
+
# 提取元素类型
|
|
162
|
+
elem_type_str = c_type[18:-1] # 去掉 "std::unordered_set<" 和 ">"
|
|
163
|
+
elem_type = 类型转换器.C类型转Python(elem_type_str)
|
|
164
|
+
if elem_type:
|
|
165
|
+
return Set[elem_type]
|
|
166
|
+
else:
|
|
167
|
+
return Set[int] # 默认元素类型
|
|
168
|
+
|
|
169
|
+
elif c_type.startswith("std::pair<"):
|
|
170
|
+
# 提取两个元素类型
|
|
171
|
+
content = c_type[10:-1] # 去掉 "std::pair<" 和 ">"
|
|
172
|
+
if ", " in content:
|
|
173
|
+
first_type_str, second_type_str = content.split(", ", 1)
|
|
174
|
+
first_type = 类型转换器.C类型转Python(first_type_str)
|
|
175
|
+
second_type = 类型转换器.C类型转Python(second_type_str)
|
|
176
|
+
if first_type and second_type:
|
|
177
|
+
return Tuple[first_type, second_type]
|
|
178
|
+
# 默认类型
|
|
179
|
+
return Tuple[int, int]
|
|
180
|
+
|
|
181
|
+
elif c_type.startswith("std::tuple<"):
|
|
182
|
+
# 提取所有元素类型
|
|
183
|
+
content = c_type[10:-1] # 去掉 "std::tuple<" 和 ">"
|
|
184
|
+
if content:
|
|
185
|
+
elem_type_strs = [s.strip() for s in content.split(",")]
|
|
186
|
+
elem_types = []
|
|
187
|
+
for elem_type_str in elem_type_strs:
|
|
188
|
+
elem_type = 类型转换器.C类型转Python(elem_type_str)
|
|
189
|
+
elem_types.append(elem_type if elem_type else int)
|
|
190
|
+
return Tuple[tuple(elem_types)]
|
|
191
|
+
else:
|
|
192
|
+
return Tuple[()] # 空元组
|
|
193
|
+
|
|
194
|
+
# 处理指针类型
|
|
195
|
+
if c_type.endswith("*"):
|
|
196
|
+
base_type = 类型转换器.C类型转Python(c_type[:-1].strip())
|
|
197
|
+
# 指针类型在Python中通常用原类型表示
|
|
198
|
+
return base_type
|
|
199
|
+
|
|
200
|
+
# 处理引用类型
|
|
201
|
+
if c_type.endswith("&"):
|
|
202
|
+
base_type = 类型转换器.C类型转Python(c_type[:-1].strip())
|
|
203
|
+
# 引用类型在Python中用原类型表示
|
|
204
|
+
return base_type
|
|
205
|
+
|
|
206
|
+
# 处理const修饰符
|
|
207
|
+
if c_type.startswith("const "):
|
|
208
|
+
base_type = 类型转换器.C类型转Python(c_type[6:].strip())
|
|
209
|
+
return base_type
|
|
210
|
+
return None
|