From da638a018bb536116665dc28e857bd26f8ccec2c Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Sun, 18 Oct 2026 20:13:37 +0800 Subject: [PATCH] =?UTF-8?q?fix(match,sizeof):=20=E4=BF=AE=E5=A4=8D=20Match?= =?UTF-8?q?Class=20=E5=B1=9E=E6=80=A7=E5=90=8D=E4=B8=8E=20LLVM=20=E5=9F=BA?= =?UTF-8?q?=E6=9C=AC=E7=B1=BB=E5=9E=8B=20sizeof?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Match 分支改用 c.pattern;__sizeof__ 对 i1/i8/i16/i32/i64/f32/f64/ptr 直接返回字节数。 --- lib/core/Handles/HandlesExprBuiltin.py | 6 ++++++ lib/core/Handles/HandlesExprCall.py | 6 ++++++ lib/core/Handles/HandlesMatch.py | 4 ++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/core/Handles/HandlesExprBuiltin.py b/lib/core/Handles/HandlesExprBuiltin.py index bab40b9..63f0d0b 100644 --- a/lib/core/Handles/HandlesExprBuiltin.py +++ b/lib/core/Handles/HandlesExprBuiltin.py @@ -820,6 +820,12 @@ class ExprBuiltinHandle(BaseHandle): f"({ast.unparse(Node)})不是类型名(Name/Attribute),拒绝静默返回 0" ) + _LLVM_BASIC_SIZES: dict[str, int] = { + 'i1': 1, 'i8': 1, 'i16': 2, 'i32': 4, 'i64': 8, + 'f32': 4, 'f64': 8, 'ptr': 8, + } + if type_name in _LLVM_BASIC_SIZES: + return ir.Constant(ir.IntType(64), _LLVM_BASIC_SIZES[type_name]) if type_name not in Gen.structs: # str/bytes 是指针类型 (char*/void*),sizeof = 指针大小 if type_name in ('str', 'bytes'): diff --git a/lib/core/Handles/HandlesExprCall.py b/lib/core/Handles/HandlesExprCall.py index 00de038..8f4750a 100644 --- a/lib/core/Handles/HandlesExprCall.py +++ b/lib/core/Handles/HandlesExprCall.py @@ -4479,6 +4479,12 @@ class ExprCallHandle(BaseHandle): f"拒绝静默返回 0(会导致 alloc(0))" ) + _LLVM_BASIC_SIZES: dict[str, int] = { + 'i1': 1, 'i8': 1, 'i16': 2, 'i32': 4, 'i64': 8, + 'f32': 4, 'f64': 8, 'ptr': 8, + } + if type_name in _LLVM_BASIC_SIZES: + return ir.Constant(ir.IntType(64), _LLVM_BASIC_SIZES[type_name]) if type_name not in Gen.structs: # SHA1 前缀查找:如 ClassDef → 657e182b27c2a022.ClassDef struct_sha1_map = getattr(Gen, '_struct_sha1_map', {}) diff --git a/lib/core/Handles/HandlesMatch.py b/lib/core/Handles/HandlesMatch.py index 17458f6..86201c1 100644 --- a/lib/core/Handles/HandlesMatch.py +++ b/lib/core/Handles/HandlesMatch.py @@ -350,7 +350,7 @@ class MatchHandle(BaseHandle): if not HasDefault: Gen.builder.position_at_start(DefaultBB) Gen.builder.branch(AfterBB) - elif not any(isinstance(pattern, ast.MatchAs) and c.pattern.pattern is None for c in Node.cases) and not any(isinstance(pattern, ast.MatchSequence) for c in Node.cases): + elif not any(isinstance(c.pattern, ast.MatchAs) and c.pattern.pattern is None for c in Node.cases) and not any(isinstance(c.pattern, ast.MatchSequence) for c in Node.cases): Gen.builder.position_at_start(DefaultBB) Gen.builder.branch(AfterBB) Gen.builder.position_at_start(AfterBB) @@ -600,7 +600,7 @@ class MatchHandle(BaseHandle): if not HasDefault: Gen.builder.position_at_start(DefaultBB) Gen.builder.branch(AfterBB) - elif not any(isinstance(pattern, ast.MatchAs) and c.pattern.pattern is None for c in Node.cases) and not any(isinstance(pattern, ast.MatchSequence) for c in Node.cases): + elif not any(isinstance(c.pattern, ast.MatchAs) and c.pattern.pattern is None for c in Node.cases) and not any(isinstance(c.pattern, ast.MatchSequence) for c in Node.cases): Gen.builder.position_at_start(DefaultBB) Gen.builder.branch(AfterBB) Gen.builder.position_at_start(AfterBB) \ No newline at end of file