embeddedpy 0.1.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.
Potentially problematic release.
This version of embeddedpy might be problematic. Click here for more details.
- embeddedpy/__init__.py +55 -0
- embeddedpy/arrays.py +142 -0
- embeddedpy/bytes.py +65 -0
- embeddedpy/compiler/__init__.py +55 -0
- embeddedpy/compiler/__main__.py +8 -0
- embeddedpy/compiler/api.py +337 -0
- embeddedpy/compiler/cgen_helpers.py +298 -0
- embeddedpy/compiler/checker.py +244 -0
- embeddedpy/compiler/checker_pass1.py +3229 -0
- embeddedpy/compiler/checker_pass2.py +1879 -0
- embeddedpy/compiler/cli.py +310 -0
- embeddedpy/compiler/codegen.py +2407 -0
- embeddedpy/compiler/config.py +186 -0
- embeddedpy/compiler/errors.py +26 -0
- embeddedpy/compiler/frontend.py +381 -0
- embeddedpy/compiler/irtypes.py +663 -0
- embeddedpy/compiler/lang_api.py +163 -0
- embeddedpy/compiler/modules.py +583 -0
- embeddedpy/compiler/preprocessor.py +364 -0
- embeddedpy/compiler/runtime_lib.py +1395 -0
- embeddedpy/compiler/type_symbols.py +278 -0
- embeddedpy/enums.py +20 -0
- embeddedpy/interop.py +444 -0
- embeddedpy/io.py +119 -0
- embeddedpy/memory.py +196 -0
- embeddedpy/pointers.py +552 -0
- embeddedpy/strings.py +68 -0
- embeddedpy/types.py +510 -0
- embeddedpy-0.1.0.dist-info/METADATA +258 -0
- embeddedpy-0.1.0.dist-info/RECORD +34 -0
- embeddedpy-0.1.0.dist-info/WHEEL +5 -0
- embeddedpy-0.1.0.dist-info/entry_points.txt +2 -0
- embeddedpy-0.1.0.dist-info/licenses/LICENSE +21 -0
- embeddedpy-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
"""Pure-function helpers for C code generation.
|
|
2
|
+
|
|
3
|
+
Pure functions that do not depend on a CodeGen instance — such as
|
|
4
|
+
"type -> C type string", "literal escaping", and "list dimension computation"
|
|
5
|
+
— are split out here for reuse and unit testing, and to let codegen.py focus
|
|
6
|
+
on AST traversal and statement/expression emission.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import ast
|
|
10
|
+
import re
|
|
11
|
+
from typing import List, Optional
|
|
12
|
+
|
|
13
|
+
from . import irtypes as T
|
|
14
|
+
|
|
15
|
+
_COMPARE_OPS = {
|
|
16
|
+
ast.Eq: "==",
|
|
17
|
+
ast.NotEq: "!=",
|
|
18
|
+
ast.Lt: "<",
|
|
19
|
+
ast.LtE: "<=",
|
|
20
|
+
ast.Gt: ">",
|
|
21
|
+
ast.GtE: ">=",
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
# Matches a C function definition line: starts with a return type, contains (,
|
|
25
|
+
# ends with { or }, and does not yet carry a static / typedef / struct prefix.
|
|
26
|
+
_FUNC_DEF_RE = re.compile(
|
|
27
|
+
r"^(?!static\b|typedef\b|struct\b|\})"
|
|
28
|
+
r"(?:void|int|double|bool|char|size_t|"
|
|
29
|
+
r"int\d+_t|uint\d+_t|py_str|py_bytes|py_file|py_block)\b"
|
|
30
|
+
r".*\(.*\).*[{}]\s*$"
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
def _make_static(code: str) -> str:
|
|
34
|
+
"""Prefix function definitions in the snippet with static; keep existing ones unchanged."""
|
|
35
|
+
out = []
|
|
36
|
+
for line in code.split("\n"):
|
|
37
|
+
if _FUNC_DEF_RE.match(line):
|
|
38
|
+
out.append("static " + line)
|
|
39
|
+
else:
|
|
40
|
+
out.append(line)
|
|
41
|
+
return "\n".join(out)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
# Placeholder markers for the two pools, replaced/removed once we know whether
|
|
45
|
+
# an arena is needed:
|
|
46
|
+
# MANUAL / MANUAL_END -- wrap a module-level statement so its value lands in
|
|
47
|
+
# the manual pool
|
|
48
|
+
# FRAME_* -- a user function entry records the auto-pool watermark
|
|
49
|
+
# and forces a switch back to the auto pool; every exit
|
|
50
|
+
# returns to the frame's lower bound
|
|
51
|
+
# The criterion is whether the user source is "inside a function body", and is
|
|
52
|
+
# independent of whether the output has a main: main is only the C host that
|
|
53
|
+
# carries module-level statements, and exists only after compilation; the rule
|
|
54
|
+
# still holds when library / header have no main. Allocations inside a function
|
|
55
|
+
# body always land in the auto pool; mem.arena_reset() only clears the manual
|
|
56
|
+
# pool.
|
|
57
|
+
_ARENA_MANUAL_MARKER = "__PY_ARENA_MANUAL__"
|
|
58
|
+
_ARENA_MANUAL_END_MARKER = "__PY_ARENA_MANUAL_END__"
|
|
59
|
+
_ARENA_FRAME_MARK = "__PY_ARENA_FRAME_MARK__"
|
|
60
|
+
_ARENA_FRAME_RELEASE = "__PY_ARENA_FRAME_RELEASE__"
|
|
61
|
+
_ARENA_FRAME_FLOOR = "__PY_ARENA_FRAME_FLOOR__"
|
|
62
|
+
# Each user statement emits a "current source line" placeholder, used as a
|
|
63
|
+
# fallback by runtime helper functions that cannot get AST nodes (e.g. f-string
|
|
64
|
+
# formatting buffers) when reporting pool exhaustion; followed by a decimal line
|
|
65
|
+
# number.
|
|
66
|
+
_ARENA_LINE_PREFIX = "__PY_ARENA_LINE__:"
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _scalar(t: T.Type) -> T.Scalar:
|
|
70
|
+
return T.base(T.resolve(t))
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def c_type(t: T.Type) -> str:
|
|
74
|
+
"""Return the C type text for type t (pointer carries ``*``, array uses its element type)."""
|
|
75
|
+
r = T.resolve(t)
|
|
76
|
+
if isinstance(r, T.Struct):
|
|
77
|
+
return r.c_name
|
|
78
|
+
if isinstance(r, T.Enum):
|
|
79
|
+
return r.c_name
|
|
80
|
+
if isinstance(r, T.Ptr):
|
|
81
|
+
inner = T.resolve(r.target)
|
|
82
|
+
if isinstance(inner, T.ExternFuncPtr):
|
|
83
|
+
# External function pointer type: write the C type name by value
|
|
84
|
+
# (e.g. cb), not expanded into void (*)(...)
|
|
85
|
+
return inner.c_name
|
|
86
|
+
if isinstance(inner, T.Func):
|
|
87
|
+
ft = T.resolve(r.target)
|
|
88
|
+
args = ", ".join(c_type(a) for a in ft.args) or "void"
|
|
89
|
+
return f"{c_type(ft.ret)} (*)({args})"
|
|
90
|
+
return f"{c_type(r.target)} *"
|
|
91
|
+
if isinstance(r, T.Array):
|
|
92
|
+
# The array's element type; dimensions are appended separately at the
|
|
93
|
+
# declaration site (e.g. int32_t buf[3])
|
|
94
|
+
return c_type(r.element)
|
|
95
|
+
return _scalar(t).ctype
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def func_ptr_cdecl(ft: T.Func, name: str, prefix: str = "") -> str:
|
|
99
|
+
"""Generate a function pointer declaration: ret (*name)(args)."""
|
|
100
|
+
ft = T.resolve(ft)
|
|
101
|
+
args = ", ".join(c_type(a) for a in ft.args) or "void"
|
|
102
|
+
return f"{prefix}{c_type(ft.ret)} (*{name})({args})"
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _cdecl_parts(t: T.Type):
|
|
106
|
+
"""Split a type into ``(base type text, declarator template)`` where ``%s``
|
|
107
|
+
in the template is the identifier position.
|
|
108
|
+
|
|
109
|
+
A C declarator "expands outward from the name": array/function suffixes
|
|
110
|
+
attach to the name, pointer asterisks attach before the name, and inner
|
|
111
|
+
suffixes require parentheses. Recursive template assembly is the only way to
|
|
112
|
+
cover both ``int32_t *a[3]`` and ``void (*a[3])(void)`` without turning a
|
|
113
|
+
function pointer into the illegal base type ``void (*)(void) *``.
|
|
114
|
+
"""
|
|
115
|
+
r = T.resolve(t)
|
|
116
|
+
if isinstance(r, T.Func):
|
|
117
|
+
args = ", ".join(c_type(a) for a in r.args) or "void"
|
|
118
|
+
return c_type(r.ret), f"%s({args})"
|
|
119
|
+
if isinstance(r, T.ExternFuncPtr):
|
|
120
|
+
# An external function pointer is a typedef name in a header; write the
|
|
121
|
+
# type name by value
|
|
122
|
+
return r.c_name, "%s"
|
|
123
|
+
if isinstance(r, T.Array):
|
|
124
|
+
base, pat = _cdecl_parts(r.element)
|
|
125
|
+
dim = "" if r.length < 0 else str(r.length)
|
|
126
|
+
return base, pat.replace("%s", f"%s[{dim}]", 1)
|
|
127
|
+
if isinstance(r, T.Ptr):
|
|
128
|
+
inner = T.resolve(r.target)
|
|
129
|
+
if isinstance(inner, T.ExternFuncPtr):
|
|
130
|
+
return inner.c_name, "%s"
|
|
131
|
+
base, pat = _cdecl_parts(inner)
|
|
132
|
+
if pat == "%s":
|
|
133
|
+
return base, f"*{pat}"
|
|
134
|
+
if pat.startswith("(*%s)"):
|
|
135
|
+
# Adding another layer of pointer adds one more star, keeping the
|
|
136
|
+
# (**name)(...) shape
|
|
137
|
+
return base, pat.replace("(*%s)", "(**%s)", 1)
|
|
138
|
+
# Inner suffix (array/function): the star must be parenthesized,
|
|
139
|
+
# otherwise it would be read as an array/function
|
|
140
|
+
return base, pat.replace("%s", "(*%s)", 1)
|
|
141
|
+
return c_type(r), "%s"
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def c_abstract_decl(t: T.Type, quals: Optional[List[str]] = None) -> str:
|
|
145
|
+
"""Generate a C type name (abstract declarator), usable for casts and function return types."""
|
|
146
|
+
base, pat = _cdecl_parts(t)
|
|
147
|
+
return f"{_quals_prefix(quals)}{base} {pat.replace('%s', '', 1)}".rstrip()
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def array_dims(t: T.Type) -> List[int]:
|
|
151
|
+
"""Extract the dimensions of each array level (outermost first), e.g. array[array[int32]] -> [3, 2]."""
|
|
152
|
+
dims: List[int] = []
|
|
153
|
+
cur = T.resolve(t)
|
|
154
|
+
while isinstance(cur, T.Array):
|
|
155
|
+
dims.append(cur.length)
|
|
156
|
+
cur = T.resolve(cur.element)
|
|
157
|
+
return dims
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def _quals_prefix(quals: Optional[List[str]]) -> str:
|
|
161
|
+
"""Type qualifier prefix: const / volatile (order matches C)."""
|
|
162
|
+
if not quals:
|
|
163
|
+
return ""
|
|
164
|
+
rendered: List[str] = []
|
|
165
|
+
for q in quals:
|
|
166
|
+
text = q
|
|
167
|
+
if text not in rendered:
|
|
168
|
+
rendered.append(text)
|
|
169
|
+
return " ".join(rendered) + " "
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def decl_str(
|
|
173
|
+
t: T.Type,
|
|
174
|
+
name: str,
|
|
175
|
+
quals: Optional[List[str]] = None,
|
|
176
|
+
bitfield: bool = False,
|
|
177
|
+
) -> str:
|
|
178
|
+
"""Generate the full C declaration text (arrays place dimensions after the name).
|
|
179
|
+
|
|
180
|
+
quals are type qualifiers (const / volatile), written before the type,
|
|
181
|
+
matching C. bitfield=True is only used by aggregate type field emission:
|
|
182
|
+
Bits[N] compiles to ``T name : N;``. If this type ever appears in a local
|
|
183
|
+
variable/parameter it is declared as a plain integer, to avoid emitting
|
|
184
|
+
illegal C.
|
|
185
|
+
"""
|
|
186
|
+
r = T.resolve(t)
|
|
187
|
+
if bitfield and isinstance(r, T.Bits):
|
|
188
|
+
return f"{_quals_prefix(quals)}{c_type(r)} {name} : {r.width}"
|
|
189
|
+
base, pat = _cdecl_parts(r)
|
|
190
|
+
return f"{_quals_prefix(quals)}{base} {pat.replace('%s', name, 1)}"
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def zero_value(t: T.Type) -> str:
|
|
194
|
+
"""Return the C zero-value initializer expression for type t, used for uninitialized declarations and array zero-fill."""
|
|
195
|
+
r = T.resolve(t)
|
|
196
|
+
if isinstance(r, T.ExternFuncPtr):
|
|
197
|
+
return "NULL"
|
|
198
|
+
if isinstance(r, T.Struct):
|
|
199
|
+
return f"({r.c_name}){{0}}"
|
|
200
|
+
if isinstance(r, T.Enum):
|
|
201
|
+
# Enumeration zero value: take the first member
|
|
202
|
+
if r.members:
|
|
203
|
+
first = next(iter(r.members.values()))
|
|
204
|
+
return f"({r.c_name}){first}"
|
|
205
|
+
return f"({r.c_name})0"
|
|
206
|
+
if isinstance(r, T.Ptr):
|
|
207
|
+
return "NULL"
|
|
208
|
+
if isinstance(r, T.Array):
|
|
209
|
+
return "{0}"
|
|
210
|
+
s = _scalar(t)
|
|
211
|
+
if s is T.STR:
|
|
212
|
+
return "(py_str){NULL, 0, 0}"
|
|
213
|
+
if s is T.BYTES:
|
|
214
|
+
return "(py_bytes){NULL, 0}"
|
|
215
|
+
if s is T.FILE:
|
|
216
|
+
return "(py_file){NULL}"
|
|
217
|
+
if s is T.BOOL:
|
|
218
|
+
return "0"
|
|
219
|
+
return "0"
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def c_string(text: str) -> str:
|
|
223
|
+
"""Convert a Python string into a C string literal.
|
|
224
|
+
|
|
225
|
+
Escape by UTF-8 **byte** (not by code point): on the C side py_str.data
|
|
226
|
+
stores exactly UTF-8 bytes, whose length is recorded separately in
|
|
227
|
+
py_str.blen, while the code-point length is recorded in py_str.len.
|
|
228
|
+
|
|
229
|
+
Each byte uses three-digit octal (\\NNN). Octal escapes consume at most three
|
|
230
|
+
digits in C, so padding to a fixed three digits prevents a following Arabic
|
|
231
|
+
numeral from being swallowed in: ("\\3031" would be 0xC3 followed by '1').
|
|
232
|
+
"""
|
|
233
|
+
parts: List[str] = []
|
|
234
|
+
for byte in text.encode("utf-8"):
|
|
235
|
+
ch = chr(byte)
|
|
236
|
+
if byte == 0x5C:
|
|
237
|
+
parts.append("\\\\")
|
|
238
|
+
elif byte == 0x22:
|
|
239
|
+
parts.append('\\"')
|
|
240
|
+
elif byte == 0x0A:
|
|
241
|
+
parts.append("\\n")
|
|
242
|
+
elif byte == 0x0D:
|
|
243
|
+
parts.append("\\r")
|
|
244
|
+
elif byte == 0x09:
|
|
245
|
+
parts.append("\\t")
|
|
246
|
+
elif 32 <= byte <= 126:
|
|
247
|
+
parts.append(ch)
|
|
248
|
+
else:
|
|
249
|
+
parts.append(f"\\{byte:03o}")
|
|
250
|
+
return '"' + "".join(parts) + '"'
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def byte_len(text: str) -> int:
|
|
254
|
+
"""Compute the byte length of a string under UTF-8 encoding, for the C-side byte array length."""
|
|
255
|
+
return len(text.encode("utf-8"))
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def c_byte_array(data: bytes) -> str:
|
|
259
|
+
"""Convert a Python bytes literal into a C uint8_t initializer list."""
|
|
260
|
+
if not data:
|
|
261
|
+
return "{0}"
|
|
262
|
+
return "{" + ", ".join(str(b) for b in data) + "}"
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def i64_literal(value: int) -> str:
|
|
266
|
+
"""Write a compile-time integer as a C int64_t literal.
|
|
267
|
+
|
|
268
|
+
A bare decimal literal may first be inferred as int/long and then promoted in
|
|
269
|
+
C, so INT64_C fixes the width; negative numbers are split into a unary minus
|
|
270
|
+
to avoid the macro argument carrying its own sign.
|
|
271
|
+
"""
|
|
272
|
+
if value == -(1 << 63):
|
|
273
|
+
return "(-INT64_C(9223372036854775807) - 1)"
|
|
274
|
+
if value < 0:
|
|
275
|
+
return f"(-INT64_C({-value}))"
|
|
276
|
+
return f"INT64_C({value})"
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def _char_ctype(node: ast.AST) -> str:
|
|
280
|
+
"""Return the C spelling of the node's target type (char / signed char / unsigned char)."""
|
|
281
|
+
t = getattr(node, "_etype", None)
|
|
282
|
+
resolved = T.resolve(t) if t is not None else None
|
|
283
|
+
if isinstance(resolved, T.Scalar) and T.is_byte_char(resolved):
|
|
284
|
+
return resolved.ctype
|
|
285
|
+
return "char"
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
def _c_char_literal(cp: int, ctype: str = "char") -> str:
|
|
289
|
+
"""Convert a byte value into a C character literal.
|
|
290
|
+
|
|
291
|
+
Printable, non-special characters are written directly as ``'x'`` (a char
|
|
292
|
+
constant has type int; C converts it to the target type); backslash and
|
|
293
|
+
single quote must be escaped; the rest (control characters, non-ASCII) use
|
|
294
|
+
the ``(ctype)<n>`` integer form to avoid octal/hex escape ambiguity.
|
|
295
|
+
"""
|
|
296
|
+
if 32 <= cp < 127 and cp not in (ord("\\"), ord("'")):
|
|
297
|
+
return f"'{chr(cp)}'"
|
|
298
|
+
return f"({ctype}){cp}"
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
"""Type inference and subset semantics checking (Checker aggregator module).
|
|
2
|
+
|
|
3
|
+
The Checker itself only keeps the orchestration logic (construction, run, error
|
|
4
|
+
reporting); the rest of the type-checking logic is spread across checker_pass1
|
|
5
|
+
(first pass: type unification and inference) and checker_pass2 (second pass:
|
|
6
|
+
precise validation). Symbols / constants / dataclasses are in type_symbols.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import ast
|
|
10
|
+
from typing import Dict, List, Optional, Tuple
|
|
11
|
+
|
|
12
|
+
from . import irtypes as T
|
|
13
|
+
from . import preprocessor as pp
|
|
14
|
+
from . import runtime_lib
|
|
15
|
+
from .config import CompileConfig
|
|
16
|
+
from .errors import CompileError, Diagnostic
|
|
17
|
+
from .frontend import parse_source
|
|
18
|
+
from .type_symbols import (
|
|
19
|
+
Symbol, FuncInfo, ModuleInfo, BUILTIN_FUNCS, fold_int_expr,
|
|
20
|
+
)
|
|
21
|
+
from .checker_pass1 import Pass1
|
|
22
|
+
from .checker_pass2 import Pass2
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class Checker(Pass1, Pass2):
|
|
26
|
+
"""Facade for the two-pass type check: merges Pass1 and Pass2, holding the scope, function table, and builtin symbols."""
|
|
27
|
+
def __init__(self, tree: ast.Module, filename: str,
|
|
28
|
+
config: Optional[CompileConfig] = None) -> None:
|
|
29
|
+
self.tree = tree
|
|
30
|
+
self.filename = filename
|
|
31
|
+
self.config = config or CompileConfig()
|
|
32
|
+
# Header output has declarations only: ordinary function bodies emit
|
|
33
|
+
# nothing, and semantic checking is skipped entirely, but signature,
|
|
34
|
+
# decorator, type-annotation, and registration-statement checks are still
|
|
35
|
+
# kept.
|
|
36
|
+
self._header_mode = self.config.output_type == "header"
|
|
37
|
+
self.scope: Dict[str, Symbol] = {}
|
|
38
|
+
self.globals: Dict[str, Symbol] = {}
|
|
39
|
+
self.functions: Dict[str, FuncInfo] = {}
|
|
40
|
+
self.builtins = {
|
|
41
|
+
name: Symbol(kind="builtin", name=name, type=T.VOID)
|
|
42
|
+
for name in BUILTIN_FUNCS
|
|
43
|
+
}
|
|
44
|
+
self.func: Optional[FuncInfo] = None
|
|
45
|
+
self.loop_depth = 0
|
|
46
|
+
self._second_pass = False
|
|
47
|
+
# Headers to #include: (header filename, is-system-header)
|
|
48
|
+
self.includes: List[Tuple[str, bool]] = []
|
|
49
|
+
# Compile-time registrations of bare calls (no LHS)
|
|
50
|
+
self.decls: List[Symbol] = []
|
|
51
|
+
# Section descriptor bindings: name -> C section name. The binding
|
|
52
|
+
# statement itself produces no C code; the descriptor is only allowed in
|
|
53
|
+
# the ``name[T]`` position of a type annotation.
|
|
54
|
+
self._section_bindings: Dict[str, str] = {}
|
|
55
|
+
# Registered C macro names: a macro is a C-level global text name, so
|
|
56
|
+
# collision checks use the C name rather than the binding name with a
|
|
57
|
+
# module prefix, otherwise same-named macros from different submodules
|
|
58
|
+
# would each emit their own #define.
|
|
59
|
+
self._macro_c_names = set()
|
|
60
|
+
|
|
61
|
+
def run(self) -> ModuleInfo:
|
|
62
|
+
# First mark the named declarations inside compile-time condition blocks,
|
|
63
|
+
# so the later include collection and bare-call registration can treat
|
|
64
|
+
# them separately from same-named calls at module top level.
|
|
65
|
+
pp.mark_pp_block_decls(self.tree)
|
|
66
|
+
self._register_imports()
|
|
67
|
+
self._register_structs()
|
|
68
|
+
self._collect_globals()
|
|
69
|
+
self._scan_bare_decls()
|
|
70
|
+
self._register_type_aliases()
|
|
71
|
+
self._register_functions()
|
|
72
|
+
self._collect_includes()
|
|
73
|
+
self._verify_c_names()
|
|
74
|
+
|
|
75
|
+
# First pass: only type unification, binding all inferred variables
|
|
76
|
+
self._second_pass = False
|
|
77
|
+
for stmt in self.tree.body:
|
|
78
|
+
if isinstance(stmt, (ast.Import, ast.ImportFrom, ast.FunctionDef, ast.ClassDef)):
|
|
79
|
+
continue
|
|
80
|
+
if self._is_docstring(stmt) or getattr(stmt, "_extern_type", False) \
|
|
81
|
+
or getattr(stmt, "_extern_const", False) \
|
|
82
|
+
or getattr(stmt, "_extern_var", False) \
|
|
83
|
+
or getattr(stmt, "_macro", False) \
|
|
84
|
+
or getattr(stmt, "_section_binding", False) \
|
|
85
|
+
or getattr(stmt, "_bare_decl", False):
|
|
86
|
+
continue
|
|
87
|
+
self._type_stmt(stmt)
|
|
88
|
+
for fi in self.functions.values():
|
|
89
|
+
if not self._header_mode:
|
|
90
|
+
self._type_function(fi)
|
|
91
|
+
for fi in self.functions.values():
|
|
92
|
+
self._finish_function(fi)
|
|
93
|
+
self._verify()
|
|
94
|
+
|
|
95
|
+
# Second pass: types are now known, run semantic validation and attach
|
|
96
|
+
# the annotations needed for code generation
|
|
97
|
+
self._second_pass = True
|
|
98
|
+
for stmt in self.tree.body:
|
|
99
|
+
if isinstance(stmt, (ast.Import, ast.ImportFrom, ast.FunctionDef, ast.ClassDef)):
|
|
100
|
+
continue
|
|
101
|
+
if self._is_docstring(stmt) or getattr(stmt, "_extern_type", False) \
|
|
102
|
+
or getattr(stmt, "_extern_const", False) \
|
|
103
|
+
or getattr(stmt, "_extern_var", False) \
|
|
104
|
+
or getattr(stmt, "_macro", False) \
|
|
105
|
+
or getattr(stmt, "_section_binding", False) \
|
|
106
|
+
or getattr(stmt, "_bare_decl", False):
|
|
107
|
+
continue
|
|
108
|
+
self._check_stmt(stmt)
|
|
109
|
+
for fi in self.functions.values():
|
|
110
|
+
if not self._header_mode:
|
|
111
|
+
self._check_function(fi)
|
|
112
|
+
return ModuleInfo(
|
|
113
|
+
self.filename, self.globals, self.functions, self.scope,
|
|
114
|
+
self.includes, self.decls,
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
def _verify_c_names(self) -> None:
|
|
118
|
+
"""Each C identifier in the output has exactly one owner.
|
|
119
|
+
|
|
120
|
+
The compiler-generated ``f_`` / ``g_`` internal names are claimed first;
|
|
121
|
+
names the user writes via export / extern_* / typedef / define go into C
|
|
122
|
+
as-is, and must collide neither with internal names, nor with runtime
|
|
123
|
+
identifiers, the actual system-header symbols included, nor the output's
|
|
124
|
+
guard macros. Missing any category would let compilation pass but blow up
|
|
125
|
+
only when the real C is compiled — a silent divergence.
|
|
126
|
+
"""
|
|
127
|
+
reserved = set(runtime_lib.RESERVED_IDENTIFIERS)
|
|
128
|
+
headers = list(self.config.include)
|
|
129
|
+
for h, _system in self.includes:
|
|
130
|
+
headers.append(h)
|
|
131
|
+
for h in headers:
|
|
132
|
+
if h in self.config.exclude:
|
|
133
|
+
continue
|
|
134
|
+
reserved.update(runtime_lib.HEADER_SYMBOLS.get(h, ()))
|
|
135
|
+
reserved.update(runtime_lib.RUNTIME_TYPE_GUARDS.values())
|
|
136
|
+
reserved.add(self.config.header_guard(self.filename))
|
|
137
|
+
|
|
138
|
+
owners: Dict[str, str] = {}
|
|
139
|
+
|
|
140
|
+
def claim(name: str, desc: str, node: ast.AST, user_written: bool) -> None:
|
|
141
|
+
if not name:
|
|
142
|
+
return
|
|
143
|
+
if user_written and name in reserved:
|
|
144
|
+
self._error(
|
|
145
|
+
node,
|
|
146
|
+
f"C name '{name}' collides with an identifier used by the "
|
|
147
|
+
"generated C runtime or its headers; choose a different name",
|
|
148
|
+
code="name",
|
|
149
|
+
)
|
|
150
|
+
prev = owners.get(name)
|
|
151
|
+
if prev is not None and prev != desc:
|
|
152
|
+
self._error(node, f"duplicate C name '{name}' ({prev} and {desc})",
|
|
153
|
+
code="name")
|
|
154
|
+
owners[name] = desc
|
|
155
|
+
|
|
156
|
+
for fi in self.functions.values():
|
|
157
|
+
if fi.export_c_name:
|
|
158
|
+
continue
|
|
159
|
+
if fi.is_extern and fi.extern_c_name:
|
|
160
|
+
continue
|
|
161
|
+
claim(f"f_{fi.name}", f"function {fi.name}", fi.node, False)
|
|
162
|
+
for sym in self.globals.values():
|
|
163
|
+
if sym.kind == "global" and sym.c_name:
|
|
164
|
+
claim(sym.c_name, f"global {sym.name}", self.tree, False)
|
|
165
|
+
for fi in self.functions.values():
|
|
166
|
+
if fi.export_c_name:
|
|
167
|
+
claim(fi.export_c_name, f"exported function {fi.name}", fi.node, True)
|
|
168
|
+
elif fi.is_extern and fi.extern_c_name:
|
|
169
|
+
claim(fi.extern_c_name, f"external function {fi.name}", fi.node, True)
|
|
170
|
+
|
|
171
|
+
def api_name(call: ast.Call) -> str:
|
|
172
|
+
f = call.func
|
|
173
|
+
if (isinstance(f, ast.Attribute) and isinstance(f.value, ast.Name)
|
|
174
|
+
and f.value.id == "c"):
|
|
175
|
+
return f.attr
|
|
176
|
+
return ""
|
|
177
|
+
|
|
178
|
+
for stmt in self.tree.body:
|
|
179
|
+
target: Optional[ast.Name] = None
|
|
180
|
+
call: Optional[ast.Call] = None
|
|
181
|
+
if isinstance(stmt, ast.Assign) and len(stmt.targets) == 1:
|
|
182
|
+
if isinstance(stmt.targets[0], ast.Name):
|
|
183
|
+
target = stmt.targets[0]
|
|
184
|
+
if isinstance(stmt.value, ast.Call):
|
|
185
|
+
call = stmt.value
|
|
186
|
+
elif isinstance(stmt, ast.Expr) and isinstance(stmt.value, ast.Call):
|
|
187
|
+
call = stmt.value
|
|
188
|
+
if call is None:
|
|
189
|
+
continue
|
|
190
|
+
kind = api_name(call)
|
|
191
|
+
if kind == "typedef" and target is not None:
|
|
192
|
+
orig = getattr(stmt, "_embeddedpy_orig_name", target.id)
|
|
193
|
+
claim(orig, f"type alias {orig}", target, True)
|
|
194
|
+
# c.extern_type only references an already-existing external C type
|
|
195
|
+
# name (size_t / char / ...); it emits nothing and declares no new
|
|
196
|
+
# symbol, so it does not participate in the ownership table's
|
|
197
|
+
# collision check.
|
|
198
|
+
elif kind in ("extern_var", "extern_const", "define") and call.args:
|
|
199
|
+
arg = call.args[0]
|
|
200
|
+
if isinstance(arg, ast.Constant) and isinstance(arg.value, str) and arg.value:
|
|
201
|
+
claim(arg.value, f"c.{kind} {arg.value}", arg, True)
|
|
202
|
+
|
|
203
|
+
def _fold_range_arg(self, arg: ast.AST, index: int) -> bool:
|
|
204
|
+
"""Fold a compile-time constant argument of for-range, attaching ``_range_fold`` on success and returning True.
|
|
205
|
+
|
|
206
|
+
Evaluate using Python integer semantics then emit a 64-bit literal, to
|
|
207
|
+
avoid the constant being inferred as 32-bit first and wrapping around.
|
|
208
|
+
Expressions containing variables return False and take the regular type
|
|
209
|
+
inference and cast path.
|
|
210
|
+
"""
|
|
211
|
+
value = fold_int_expr(arg)
|
|
212
|
+
if value is None:
|
|
213
|
+
return False
|
|
214
|
+
if not (-(1 << 63) <= value <= (1 << 63) - 1):
|
|
215
|
+
self._error(arg, "range argument is out of int64 range")
|
|
216
|
+
if index == 2 and value == 0:
|
|
217
|
+
self._error(arg, "range step cannot be zero")
|
|
218
|
+
arg._range_fold = value
|
|
219
|
+
return True
|
|
220
|
+
|
|
221
|
+
def _error(self, node: ast.AST, message: str, code: str = "type") -> "CompileError":
|
|
222
|
+
raise CompileError(
|
|
223
|
+
[
|
|
224
|
+
Diagnostic(
|
|
225
|
+
message=message,
|
|
226
|
+
line=getattr(node, "lineno", 0),
|
|
227
|
+
col=getattr(node, "col_offset", 0),
|
|
228
|
+
filename=getattr(node, "_embeddedpy_filename", self.filename),
|
|
229
|
+
code=code,
|
|
230
|
+
)
|
|
231
|
+
]
|
|
232
|
+
)
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def check_tree(tree: ast.Module, filename: str,
|
|
236
|
+
config: Optional[CompileConfig] = None) -> ModuleInfo:
|
|
237
|
+
"""Compilation entry point: run the two-pass check on the whole syntax tree and return the module info."""
|
|
238
|
+
return Checker(tree, filename, config).run()
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def check_source(source: str, filename: str = "<string>") -> ModuleInfo:
|
|
242
|
+
"""Compilation entry point (string source): parse first, then check."""
|
|
243
|
+
tree = parse_source(source, filename)
|
|
244
|
+
return check_tree(tree, filename)
|