MCPcopy Create free account
hub / github.com/BasisResearch/lean.py / _classify

Function _classify

lean_py/_parse.py:275–328  ·  view source on GitHub ↗
(ast: c_ast.FileAST, defines: dict[str, int], header_path: Path)

Source from the content-addressed store, hash-verified

273
274def _extract_inline_names(header_path: Path) -> set[str]:
275 text = header_path.read_text()
276 pattern = re.compile(r"static\s+inline\s+(?:LEAN_ALWAYS_INLINE\s+)?[\w\s*]+\s+(\w+)\s*\(")
277 return {m.group(1) for m in pattern.finditer(text)}
278
279
280# ============================================================================
281# Classification
282# ============================================================================
283
284
285def _classify(ast: c_ast.FileAST, defines: dict[str, int], header_path: Path) -> HeaderModel:
286 model = HeaderModel()
287 model.constants = defines
288
289 export_names = _extract_export_names(header_path)
290 inline_names = _extract_inline_names(header_path)
291
292 for node in ast.ext:
293 if isinstance(node, c_ast.Typedef):
294 if isinstance(node.type, c_ast.TypeDecl) and isinstance(node.type.type, c_ast.Struct):
295 struct = _extract_struct(node.type.type)
296 if struct:
297 struct.name = node.name
298 model.structs.append(struct)
299 continue
300 typedef_type = _decl_type_to_str(node.type)
301 model.typedefs.append(TypedefDef(name=node.name, underlying_type=typedef_type))
302
303 elif isinstance(node, c_ast.Decl):
304 if isinstance(node.type, c_ast.FuncDecl):
305 func_decl = node.type
306 ret_type = _decl_type_to_str(func_decl.type)
307 params, is_variadic = _extract_func_params(func_decl)
308 fname = node.name or ""
309 func = FuncDecl(
310 name=fname,
311 return_type=ret_type,
312 params=params,
313 is_variadic=is_variadic,
314 )
315 if fname in export_names:
316 model.exported_functions.append(func)
317 elif fname in inline_names:
318 model.inline_functions.append(func)
319
320 elif isinstance(node, c_ast.FuncDef):
321 decl = node.decl
322 if isinstance(decl.type, c_ast.FuncDecl):
323 func_decl = decl.type
324 ret_type = _decl_type_to_str(func_decl.type)
325 params, is_variadic = _extract_func_params(func_decl)
326 fname = decl.name or ""
327 func = FuncDecl(
328 name=fname,
329 return_type=ret_type,
330 params=params,
331 is_variadic=is_variadic,

Callers 1

get_header_modelFunction · 0.85

Calls 8

HeaderModelClass · 0.85
_extract_export_namesFunction · 0.85
_extract_inline_namesFunction · 0.85
_extract_structFunction · 0.85
_decl_type_to_strFunction · 0.85
TypedefDefClass · 0.85
_extract_func_paramsFunction · 0.85
FuncDeclClass · 0.85

Tested by

no test coverage detected