pycforge 0.15.2__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.
Files changed (221) hide show
  1. pycforge/__init__.py +8 -0
  2. pycforge/__main__.py +2 -0
  3. pycforge/_version.py +1 -0
  4. pycforge/converter/__init__.py +0 -0
  5. pycforge/converter/analysis/__init__.py +5 -0
  6. pycforge/converter/analysis/functions.py +723 -0
  7. pycforge/converter/analysis/model.py +158 -0
  8. pycforge/converter/analysis/planning.py +464 -0
  9. pycforge/converter/analysis/stage.py +1976 -0
  10. pycforge/converter/analysis/symbols.py +237 -0
  11. pycforge/converter/analysis/validation.py +2166 -0
  12. pycforge/converter/c_output/__init__.py +2 -0
  13. pycforge/converter/c_output/conformance.py +297 -0
  14. pycforge/converter/c_output/renderer.py +220 -0
  15. pycforge/converter/conditional_regions/__init__.py +71 -0
  16. pycforge/converter/conditional_regions/analysis.py +481 -0
  17. pycforge/converter/conditional_regions/lowering.py +579 -0
  18. pycforge/converter/conditional_regions/model.py +243 -0
  19. pycforge/converter/conditional_regions/validation.py +690 -0
  20. pycforge/converter/containers/__init__.py +19 -0
  21. pycforge/converter/containers/analysis.py +542 -0
  22. pycforge/converter/containers/lowering.py +290 -0
  23. pycforge/converter/containers/model.py +146 -0
  24. pycforge/converter/contracts/__init__.py +0 -0
  25. pycforge/converter/contracts/configuration.py +178 -0
  26. pycforge/converter/contracts/identifiers.py +68 -0
  27. pycforge/converter/contracts/versions.py +71 -0
  28. pycforge/converter/core/__init__.py +0 -0
  29. pycforge/converter/core/artifact_io.py +87 -0
  30. pycforge/converter/core/cancellation.py +7 -0
  31. pycforge/converter/core/canonicalization.py +254 -0
  32. pycforge/converter/core/context.py +13 -0
  33. pycforge/converter/core/diagnostics.py +95 -0
  34. pycforge/converter/core/enums.py +23 -0
  35. pycforge/converter/core/fingerprint.py +22 -0
  36. pycforge/converter/core/pipeline.py +8 -0
  37. pycforge/converter/core/progress.py +25 -0
  38. pycforge/converter/core/request.py +50 -0
  39. pycforge/converter/core/resource_policy.py +31 -0
  40. pycforge/converter/core/result.py +23 -0
  41. pycforge/converter/core/serialization.py +36 -0
  42. pycforge/converter/core/stage.py +10 -0
  43. pycforge/converter/core/stage_artifact.py +80 -0
  44. pycforge/converter/core/stage_outcome.py +15 -0
  45. pycforge/converter/decision_trace/__init__.py +0 -0
  46. pycforge/converter/decision_trace/recorder.py +52 -0
  47. pycforge/converter/facade.py +459 -0
  48. pycforge/converter/frontend/__init__.py +4 -0
  49. pycforge/converter/frontend/normalizer.py +59 -0
  50. pycforge/converter/frontend/parser.py +20 -0
  51. pycforge/converter/frontend/source_document.py +93 -0
  52. pycforge/converter/frontend/stages.py +518 -0
  53. pycforge/converter/frontend/validation.py +76 -0
  54. pycforge/converter/io/__init__.py +0 -0
  55. pycforge/converter/io/atomic_writer.py +33 -0
  56. pycforge/converter/ir/__init__.py +1 -0
  57. pycforge/converter/ir/c_ir/__init__.py +4 -0
  58. pycforge/converter/ir/c_ir/builder.py +26 -0
  59. pycforge/converter/ir/c_ir/model.py +318 -0
  60. pycforge/converter/ir/c_ir/serialization.py +88 -0
  61. pycforge/converter/ir/c_ir/validation.py +834 -0
  62. pycforge/converter/ir/python_ir/__init__.py +17 -0
  63. pycforge/converter/ir/python_ir/nodes.py +86 -0
  64. pycforge/converter/keyword_calls/__init__.py +68 -0
  65. pycforge/converter/keyword_calls/analysis.py +458 -0
  66. pycforge/converter/keyword_calls/lowering.py +271 -0
  67. pycforge/converter/keyword_calls/model.py +304 -0
  68. pycforge/converter/keyword_calls/validation.py +802 -0
  69. pycforge/converter/keyword_only_calls/__init__.py +68 -0
  70. pycforge/converter/keyword_only_calls/analysis.py +640 -0
  71. pycforge/converter/keyword_only_calls/lowering.py +376 -0
  72. pycforge/converter/keyword_only_calls/model.py +328 -0
  73. pycforge/converter/keyword_only_calls/validation.py +1357 -0
  74. pycforge/converter/lowering.py +1000 -0
  75. pycforge/converter/modules/__init__.py +13 -0
  76. pycforge/converter/modules/analysis.py +1250 -0
  77. pycforge/converter/modules/lowering.py +200 -0
  78. pycforge/converter/modules/model.py +73 -0
  79. pycforge/converter/modules/stage.py +192 -0
  80. pycforge/converter/numeric_semantics/__init__.py +23 -0
  81. pycforge/converter/numeric_semantics/analysis.py +420 -0
  82. pycforge/converter/numeric_semantics/lowering.py +153 -0
  83. pycforge/converter/numeric_semantics/model.py +123 -0
  84. pycforge/converter/records/__init__.py +34 -0
  85. pycforge/converter/records/analysis.py +1394 -0
  86. pycforge/converter/records/lowering.py +393 -0
  87. pycforge/converter/records/model.py +319 -0
  88. pycforge/converter/rules/__init__.py +3 -0
  89. pycforge/converter/support_templates/__init__.py +49 -0
  90. pycforge/converter/support_templates/assembly.py +106 -0
  91. pycforge/converter/support_templates/factories.py +348 -0
  92. pycforge/converter/support_templates/model.py +159 -0
  93. pycforge/converter/support_templates/registry.py +388 -0
  94. pycforge/converter/telemetry/__init__.py +0 -0
  95. pycforge/converter/telemetry/sink.py +39 -0
  96. pycforge/ide/__init__.py +19 -0
  97. pycforge/ide/__main__.py +5 -0
  98. pycforge/ide/_worker_event_codec.py +276 -0
  99. pycforge/ide/_worker_protocol_json.py +197 -0
  100. pycforge/ide/_worker_protocol_types.py +90 -0
  101. pycforge/ide/_worker_request_codec.py +200 -0
  102. pycforge/ide/_worker_request_data.py +260 -0
  103. pycforge/ide/_worker_result_data.py +343 -0
  104. pycforge/ide/action_contract.py +469 -0
  105. pycforge/ide/action_validation.py +229 -0
  106. pycforge/ide/command_palette.py +223 -0
  107. pycforge/ide/controller.py +904 -0
  108. pycforge/ide/controller_conversion.py +546 -0
  109. pycforge/ide/controller_io.py +205 -0
  110. pycforge/ide/editor.py +951 -0
  111. pycforge/ide/editor_commands_qt.py +274 -0
  112. pycforge/ide/editor_lexical.py +43 -0
  113. pycforge/ide/editor_sidebars.py +236 -0
  114. pycforge/ide/editor_syntax.py +243 -0
  115. pycforge/ide/find_replace.py +535 -0
  116. pycforge/ide/icons.py +91 -0
  117. pycforge/ide/io_service.py +196 -0
  118. pycforge/ide/model.py +164 -0
  119. pycforge/ide/panels.py +903 -0
  120. pycforge/ide/positions.py +220 -0
  121. pycforge/ide/process_worker.py +130 -0
  122. pycforge/ide/qt.py +230 -0
  123. pycforge/ide/qt_actions.py +418 -0
  124. pycforge/ide/qt_close.py +169 -0
  125. pycforge/ide/qt_command_palette.py +297 -0
  126. pycforge/ide/qt_contract.py +220 -0
  127. pycforge/ide/qt_documents.py +590 -0
  128. pycforge/ide/qt_editor_buffers.py +113 -0
  129. pycforge/ide/qt_editor_surfaces.py +112 -0
  130. pycforge/ide/qt_menus.py +324 -0
  131. pycforge/ide/qt_projection.py +500 -0
  132. pycforge/ide/qt_shell.py +348 -0
  133. pycforge/ide/qt_shell_interactions.py +320 -0
  134. pycforge/ide/qt_state.py +393 -0
  135. pycforge/ide/qt_workspace_features.py +398 -0
  136. pycforge/ide/qt_workspace_navigation.py +208 -0
  137. pycforge/ide/qt_workspace_observers.py +173 -0
  138. pycforge/ide/qt_workspace_panels.py +545 -0
  139. pycforge/ide/qt_workspace_widgets.py +228 -0
  140. pycforge/ide/resources/icons/about.svg +1 -0
  141. pycforge/ide/resources/icons/add-document.svg +1 -0
  142. pycforge/ide/resources/icons/brand-mark.svg +1 -0
  143. pycforge/ide/resources/icons/bundle-search.svg +1 -0
  144. pycforge/ide/resources/icons/cancel.svg +1 -0
  145. pycforge/ide/resources/icons/check.svg +1 -0
  146. pycforge/ide/resources/icons/chevron-right.svg +1 -0
  147. pycforge/ide/resources/icons/close.svg +1 -0
  148. pycforge/ide/resources/icons/collapse-all.svg +1 -0
  149. pycforge/ide/resources/icons/command-palette.svg +1 -0
  150. pycforge/ide/resources/icons/convert.svg +1 -0
  151. pycforge/ide/resources/icons/copy.svg +1 -0
  152. pycforge/ide/resources/icons/cut.svg +1 -0
  153. pycforge/ide/resources/icons/decision-trace.svg +1 -0
  154. pycforge/ide/resources/icons/details.svg +1 -0
  155. pycforge/ide/resources/icons/diagnostics.svg +1 -0
  156. pycforge/ide/resources/icons/duplicate-line.svg +1 -0
  157. pycforge/ide/resources/icons/exit.svg +1 -0
  158. pycforge/ide/resources/icons/expand-all.svg +1 -0
  159. pycforge/ide/resources/icons/export.svg +1 -0
  160. pycforge/ide/resources/icons/find.svg +1 -0
  161. pycforge/ide/resources/icons/go-to-line.svg +1 -0
  162. pycforge/ide/resources/icons/go-to-output.svg +1 -0
  163. pycforge/ide/resources/icons/go-to-source.svg +1 -0
  164. pycforge/ide/resources/icons/history.svg +1 -0
  165. pycforge/ide/resources/icons/indent.svg +1 -0
  166. pycforge/ide/resources/icons/link-c.svg +1 -0
  167. pycforge/ide/resources/icons/mappings.svg +1 -0
  168. pycforge/ide/resources/icons/module.svg +1 -0
  169. pycforge/ide/resources/icons/move-down.svg +1 -0
  170. pycforge/ide/resources/icons/move-line-down.svg +1 -0
  171. pycforge/ide/resources/icons/move-line-up.svg +1 -0
  172. pycforge/ide/resources/icons/move-up.svg +1 -0
  173. pycforge/ide/resources/icons/next.svg +1 -0
  174. pycforge/ide/resources/icons/open.svg +1 -0
  175. pycforge/ide/resources/icons/outdent.svg +1 -0
  176. pycforge/ide/resources/icons/outline.svg +1 -0
  177. pycforge/ide/resources/icons/paste.svg +1 -0
  178. pycforge/ide/resources/icons/previous.svg +1 -0
  179. pycforge/ide/resources/icons/primary-module.svg +1 -0
  180. pycforge/ide/resources/icons/redo.svg +1 -0
  181. pycforge/ide/resources/icons/remove-document.svg +1 -0
  182. pycforge/ide/resources/icons/replace.svg +1 -0
  183. pycforge/ide/resources/icons/save-as.svg +1 -0
  184. pycforge/ide/resources/icons/save.svg +1 -0
  185. pycforge/ide/resources/icons/select-all.svg +1 -0
  186. pycforge/ide/resources/icons/settings.svg +1 -0
  187. pycforge/ide/resources/icons/show-c.svg +1 -0
  188. pycforge/ide/resources/icons/split-view.svg +1 -0
  189. pycforge/ide/resources/icons/summary.svg +1 -0
  190. pycforge/ide/resources/icons/telemetry.svg +1 -0
  191. pycforge/ide/resources/icons/toggle-comment.svg +1 -0
  192. pycforge/ide/resources/icons/toggle-fold.svg +1 -0
  193. pycforge/ide/resources/icons/undo.svg +1 -0
  194. pycforge/ide/resources/icons/whitespace.svg +1 -0
  195. pycforge/ide/revisions.py +400 -0
  196. pycforge/ide/search_service.py +280 -0
  197. pycforge/ide/session_history.py +150 -0
  198. pycforge/ide/source_editing.py +417 -0
  199. pycforge/ide/source_structure.py +558 -0
  200. pycforge/ide/source_structure_async.py +191 -0
  201. pycforge/ide/supervisor.py +503 -0
  202. pycforge/ide/theme.py +119 -0
  203. pycforge/ide/theme_stylesheet.py +598 -0
  204. pycforge/ide/theme_workspace_stylesheet.py +155 -0
  205. pycforge/ide/visual_tokens.py +142 -0
  206. pycforge/ide/worker_protocol.py +80 -0
  207. pycforge/ide/workspace_search.py +462 -0
  208. pycforge/ide/workspace_session.py +256 -0
  209. pycforge/laboratory/__init__.py +6 -0
  210. pycforge/laboratory/audits.py +2244 -0
  211. pycforge/laboratory/checkpoint_e.py +2685 -0
  212. pycforge/laboratory/cli.py +119 -0
  213. pycforge/laboratory/exit_codes.py +11 -0
  214. pycforge/laboratory/keyword_audit.py +661 -0
  215. pycforge/laboratory/keyword_only_audit.py +481 -0
  216. pycforge-0.15.2.dist-info/METADATA +209 -0
  217. pycforge-0.15.2.dist-info/RECORD +221 -0
  218. pycforge-0.15.2.dist-info/WHEEL +5 -0
  219. pycforge-0.15.2.dist-info/entry_points.txt +5 -0
  220. pycforge-0.15.2.dist-info/licenses/LICENSE +674 -0
  221. pycforge-0.15.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,19 @@
1
+ """Bounded Phase 11 container facts and analysis."""
2
+
3
+ from .analysis import BoundedContainerAnalyzer
4
+ from .model import (
5
+ MAX_CONTAINER_ELEMENTS,
6
+ ContainerAccessFact,
7
+ ContainerBindingFact,
8
+ ContainerIterationFact,
9
+ ContainerShapeFact,
10
+ )
11
+
12
+ __all__ = [
13
+ "MAX_CONTAINER_ELEMENTS",
14
+ "BoundedContainerAnalyzer",
15
+ "ContainerAccessFact",
16
+ "ContainerBindingFact",
17
+ "ContainerIterationFact",
18
+ "ContainerShapeFact",
19
+ ]
@@ -0,0 +1,542 @@
1
+ """Deterministic analysis for the approved fixed local-container profile."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections import defaultdict
6
+ from dataclasses import replace
7
+ from typing import Any
8
+
9
+ from pycforge.converter.analysis.model import ValueCategory
10
+ from pycforge.converter.analysis.symbols import PythonIRIndex
11
+
12
+ from .model import (
13
+ MAX_CONTAINER_ELEMENTS,
14
+ ContainerAccessFact,
15
+ ContainerBindingFact,
16
+ ContainerIterationFact,
17
+ ContainerShapeFact,
18
+ )
19
+
20
+
21
+ _SCALAR_CATEGORIES = {
22
+ ValueCategory.INTEGER,
23
+ ValueCategory.FLOAT,
24
+ ValueCategory.BOOLEAN,
25
+ ValueCategory.STRING,
26
+ }
27
+ _LITERAL_KINDS = {"List": "list", "Tuple": "tuple", "Dict": "dict"}
28
+ _COMPREHENSION_KINDS = {
29
+ "ListComp": "list-comprehension",
30
+ "DictComp": "dict-comprehension",
31
+ "GeneratorExp": "generator-expression",
32
+ }
33
+
34
+
35
+ class BoundedContainerAnalyzer:
36
+ """Publish shapes, bindings, proven accesses, and bounded iterations."""
37
+
38
+ def __init__(
39
+ self,
40
+ module: dict[str, Any],
41
+ bindings: tuple[dict[str, Any], ...],
42
+ categories: dict[str, ValueCategory],
43
+ ) -> None:
44
+ self.index = PythonIRIndex(module)
45
+ self.categories = categories
46
+ self.bindings = bindings
47
+ self.binding_by_occurrence = {
48
+ occurrence: binding
49
+ for binding in bindings
50
+ for occurrence in binding["occurrence_node_ids"]
51
+ }
52
+ self.parents: dict[str, list[tuple[str, str]]] = defaultdict(list)
53
+ for parent in self.index.nodes.values():
54
+ for field_name, value in parent["fields"].items():
55
+ for child_id in self._references(value):
56
+ if child_id in self.index.nodes:
57
+ self.parents[child_id].append((parent["node_id"], field_name))
58
+
59
+ def analyze(
60
+ self,
61
+ ) -> tuple[
62
+ tuple[ContainerShapeFact, ...],
63
+ tuple[ContainerBindingFact, ...],
64
+ tuple[ContainerAccessFact, ...],
65
+ tuple[ContainerIterationFact, ...],
66
+ ]:
67
+ shapes = tuple(
68
+ sorted(
69
+ (
70
+ self._shape(node)
71
+ for node in self.index.nodes.values()
72
+ if node["kind"] in _LITERAL_KINDS | _COMPREHENSION_KINDS
73
+ ),
74
+ key=lambda item: item.literal_node_id,
75
+ )
76
+ )
77
+ shape_by_node = {item.literal_node_id: item for item in shapes}
78
+ raw_bindings, assignments_by_binding = self._raw_bindings(shape_by_node)
79
+ raw_by_id = {item.binding_id: item for item in raw_bindings}
80
+ accesses = self._accesses(raw_by_id, shape_by_node)
81
+ iterations = self._iterations(raw_by_id, shape_by_node)
82
+ bindings = self._finalize_bindings(
83
+ raw_bindings,
84
+ assignments_by_binding,
85
+ )
86
+ finalized = {item.binding_id: item for item in bindings}
87
+ invalid_binding_ids = {
88
+ binding_id for binding_id, item in finalized.items() if not item.valid
89
+ }
90
+ accesses = tuple(
91
+ replace(
92
+ item,
93
+ result_category=ValueCategory.UNKNOWN,
94
+ resolved_offset=None,
95
+ supported=False,
96
+ diagnostic_code="PYC3403",
97
+ reason="Container access depends on an invalid fixed binding",
98
+ )
99
+ if item.supported and item.binding_id in invalid_binding_ids
100
+ else item
101
+ for item in accesses
102
+ )
103
+ iterations = tuple(
104
+ replace(
105
+ item,
106
+ target_category=ValueCategory.UNKNOWN,
107
+ supported=False,
108
+ diagnostic_code="PYC3403",
109
+ reason="Container iteration depends on an invalid fixed binding",
110
+ )
111
+ if item.supported and item.binding_id in invalid_binding_ids
112
+ else item
113
+ for item in iterations
114
+ )
115
+ return shapes, bindings, accesses, iterations
116
+
117
+ def _shape(self, node: dict[str, Any]) -> ContainerShapeFact:
118
+ kind = node["kind"]
119
+ if kind in _COMPREHENSION_KINDS:
120
+ return ContainerShapeFact(
121
+ node["node_id"],
122
+ _COMPREHENSION_KINDS[kind],
123
+ 0,
124
+ (),
125
+ (),
126
+ (),
127
+ ValueCategory.UNKNOWN,
128
+ ValueCategory.UNKNOWN,
129
+ ValueCategory.UNKNOWN,
130
+ (),
131
+ "unsupported-comprehension",
132
+ False,
133
+ False,
134
+ "PYC3406",
135
+ "Container comprehensions and generator expressions are unsupported",
136
+ )
137
+ if kind in {"List", "Tuple"}:
138
+ elements = tuple(node["fields"].get("elts", ()))
139
+ container_kind = _LITERAL_KINDS[kind]
140
+ category = self._homogeneous_category(elements)
141
+ problem = self._capacity_problem(len(elements))
142
+ if problem is None and any(
143
+ self.index.nodes.get(item, {}).get("kind") in _LITERAL_KINDS | _COMPREHENSION_KINDS
144
+ for item in elements
145
+ ):
146
+ problem = ("PYC3401", "Nested containers are unsupported in the fixed profile")
147
+ if problem is None and category not in _SCALAR_CATEGORIES:
148
+ problem = (
149
+ "PYC3402",
150
+ "Container elements must have one homogeneous supported scalar representation",
151
+ )
152
+ valid = problem is None
153
+ return ContainerShapeFact(
154
+ node["node_id"],
155
+ container_kind,
156
+ len(elements),
157
+ elements,
158
+ (),
159
+ (),
160
+ category,
161
+ ValueCategory.UNKNOWN,
162
+ ValueCategory.UNKNOWN,
163
+ (),
164
+ "fixed-automatic-array",
165
+ kind == "List",
166
+ valid,
167
+ None if valid else problem[0],
168
+ None if valid else problem[1],
169
+ )
170
+
171
+ keys = tuple(node["fields"].get("keys", ()))
172
+ values = tuple(node["fields"].get("values", ()))
173
+ problem = self._capacity_problem(len(values))
174
+ key_values: list[int | str] = []
175
+ key_categories: list[ValueCategory] = []
176
+ if problem is None and (len(keys) != len(values) or any(not isinstance(item, str) for item in keys)):
177
+ problem = ("PYC3401", "Dictionary unpacking or malformed key/value shape is unsupported")
178
+ if problem is None:
179
+ for key_id in keys:
180
+ literal = self._literal_key(key_id)
181
+ if literal is None:
182
+ problem = ("PYC3402", "Dictionary keys must be literal int or str values")
183
+ break
184
+ key_value, key_category = literal
185
+ key_values.append(key_value)
186
+ key_categories.append(key_category)
187
+ if problem is None and (
188
+ not key_categories
189
+ or any(item is not key_categories[0] for item in key_categories)
190
+ ):
191
+ problem = ("PYC3402", "Dictionary keys must use one homogeneous int or str representation")
192
+ if problem is None and len({(type(item).__name__, item) for item in key_values}) != len(key_values):
193
+ problem = ("PYC3402", "Duplicate dictionary keys are unsupported in the fixed profile")
194
+ value_category = self._homogeneous_category(values)
195
+ if problem is None and any(
196
+ self.index.nodes.get(item, {}).get("kind") in _LITERAL_KINDS | _COMPREHENSION_KINDS
197
+ for item in values
198
+ ):
199
+ problem = ("PYC3401", "Nested containers are unsupported in the fixed profile")
200
+ if problem is None and value_category not in _SCALAR_CATEGORIES:
201
+ problem = (
202
+ "PYC3402",
203
+ "Dictionary values must have one homogeneous supported scalar representation",
204
+ )
205
+ valid = problem is None
206
+ return ContainerShapeFact(
207
+ node["node_id"],
208
+ "dict",
209
+ len(values),
210
+ (),
211
+ tuple(item for item in keys if isinstance(item, str)),
212
+ values,
213
+ ValueCategory.UNKNOWN,
214
+ key_categories[0] if key_categories else ValueCategory.UNKNOWN,
215
+ value_category,
216
+ tuple(key_values),
217
+ "parallel-fixed-automatic-arrays",
218
+ False,
219
+ valid,
220
+ None if valid else problem[0],
221
+ None if valid else problem[1],
222
+ )
223
+
224
+ @staticmethod
225
+ def _capacity_problem(capacity: int) -> tuple[str, str] | None:
226
+ if capacity <= 0:
227
+ return "PYC3401", "Empty containers have no approved element representation"
228
+ if capacity > MAX_CONTAINER_ELEMENTS:
229
+ return (
230
+ "PYC3401",
231
+ f"Container capacity exceeds the fixed limit of {MAX_CONTAINER_ELEMENTS}",
232
+ )
233
+ return None
234
+
235
+ def _homogeneous_category(self, node_ids: tuple[str, ...]) -> ValueCategory:
236
+ values = tuple(self.categories.get(item, ValueCategory.UNKNOWN) for item in node_ids)
237
+ if values and all(item is values[0] for item in values):
238
+ return values[0]
239
+ if any(item is ValueCategory.CONTRADICTORY for item in values) or values:
240
+ return ValueCategory.CONTRADICTORY
241
+ return ValueCategory.UNKNOWN
242
+
243
+ def _raw_bindings(
244
+ self,
245
+ shapes: dict[str, ContainerShapeFact],
246
+ ) -> tuple[tuple[ContainerBindingFact, ...], dict[str, tuple[str, ...]]]:
247
+ assignments_by_binding: dict[str, list[str]] = defaultdict(list)
248
+ candidates: dict[str, list[ContainerBindingFact]] = defaultdict(list)
249
+ for node in self.index.nodes.values():
250
+ if node["kind"] != "Assign":
251
+ continue
252
+ targets = tuple(node["fields"].get("targets", ()))
253
+ if len(targets) != 1 or targets[0] not in self.index.nodes:
254
+ continue
255
+ target = self.index.node(targets[0])
256
+ if target["kind"] != "Name":
257
+ continue
258
+ binding = self.binding_by_occurrence.get(target["node_id"])
259
+ if not binding:
260
+ continue
261
+ binding_id = binding["binding_id"]
262
+ assignments_by_binding[binding_id].append(node["node_id"])
263
+ value_id = node["fields"].get("value")
264
+ shape = shapes.get(value_id)
265
+ if not shape or shape.container_kind not in {"list", "tuple", "dict"}:
266
+ continue
267
+ candidates[binding_id].append(
268
+ ContainerBindingFact(
269
+ binding_id,
270
+ binding["source_name"],
271
+ node["node_id"],
272
+ target["node_id"],
273
+ shape.literal_node_id,
274
+ shape.container_kind,
275
+ shape.capacity,
276
+ shape.element_category,
277
+ shape.key_category,
278
+ shape.value_category,
279
+ (),
280
+ (),
281
+ shape.valid,
282
+ shape.diagnostic_code,
283
+ shape.literal_node_id if not shape.valid else None,
284
+ shape.reason,
285
+ )
286
+ )
287
+ selected = tuple(
288
+ sorted(
289
+ (
290
+ sorted(items, key=lambda item: self._ordinal(item.assignment_node_id))[0]
291
+ for items in candidates.values()
292
+ ),
293
+ key=lambda item: item.binding_id,
294
+ )
295
+ )
296
+ return selected, {
297
+ key: tuple(sorted(values, key=self._ordinal))
298
+ for key, values in assignments_by_binding.items()
299
+ }
300
+
301
+ def _finalize_bindings(
302
+ self,
303
+ bindings: tuple[ContainerBindingFact, ...],
304
+ assignments_by_binding: dict[str, tuple[str, ...]],
305
+ ) -> tuple[ContainerBindingFact, ...]:
306
+ result: list[ContainerBindingFact] = []
307
+ for fact in bindings:
308
+ binding = next(item for item in self.bindings if item["binding_id"] == fact.binding_id)
309
+ allowed: list[str] = []
310
+ invalid: list[str] = []
311
+ invalid_details: list[tuple[str, str, str]] = []
312
+ if not self._is_direct_function_body(fact.assignment_node_id):
313
+ invalid_details.append(
314
+ (
315
+ fact.assignment_node_id,
316
+ "PYC3403",
317
+ "A fixed container must be bound directly in a function body",
318
+ )
319
+ )
320
+ for occurrence in binding["occurrence_node_ids"]:
321
+ if occurrence == fact.target_node_id:
322
+ allowed.append(occurrence)
323
+ continue
324
+ parents = self.parents.get(occurrence, ())
325
+ accepted = False
326
+ rejected = False
327
+ for parent_id, field_name in parents:
328
+ parent = self.index.node(parent_id)
329
+ if parent["kind"] == "Subscript" and field_name == "value":
330
+ accepted = True
331
+ elif parent["kind"] == "For" and field_name == "iter":
332
+ accepted = True
333
+ elif parent["kind"] == "Attribute" and field_name == "value":
334
+ invalid_details.append(
335
+ (
336
+ parent_id,
337
+ "PYC3406",
338
+ "Container methods and mutation are unsupported in the fixed profile",
339
+ )
340
+ )
341
+ rejected = True
342
+ if accepted and not rejected:
343
+ allowed.append(occurrence)
344
+ elif not accepted:
345
+ invalid.append(occurrence)
346
+ invalid_details.append(
347
+ (
348
+ occurrence,
349
+ "PYC3403",
350
+ "Container aliasing, escape, rebinding, or scalar use is unsupported",
351
+ )
352
+ )
353
+ else:
354
+ invalid.append(occurrence)
355
+ assignments = assignments_by_binding.get(fact.binding_id, ())
356
+ if len(assignments) != 1:
357
+ rejection = assignments[1] if len(assignments) > 1 else fact.assignment_node_id
358
+ invalid_details.append(
359
+ (
360
+ rejection,
361
+ "PYC3403",
362
+ "A fixed container binding must be assigned exactly once",
363
+ )
364
+ )
365
+ if invalid_details:
366
+ invalid_details.sort(key=lambda item: self._ordinal(item[0]))
367
+ rejection_node, code, reason = invalid_details[0]
368
+ else:
369
+ rejection_node, code, reason = fact.rejection_node_id, fact.diagnostic_code, fact.reason
370
+ valid = fact.valid and not invalid_details and len(assignments) == 1
371
+ result.append(
372
+ replace(
373
+ fact,
374
+ allowed_use_node_ids=tuple(sorted(set(allowed), key=self._ordinal)),
375
+ invalid_use_node_ids=tuple(sorted(set(invalid), key=self._ordinal)),
376
+ valid=valid,
377
+ diagnostic_code=None if valid else code,
378
+ rejection_node_id=None if valid else rejection_node,
379
+ reason=None if valid else reason,
380
+ )
381
+ )
382
+ return tuple(sorted(result, key=lambda item: item.binding_id))
383
+
384
+ def _accesses(
385
+ self,
386
+ bindings: dict[str, ContainerBindingFact],
387
+ shapes: dict[str, ContainerShapeFact],
388
+ ) -> tuple[ContainerAccessFact, ...]:
389
+ result: list[ContainerAccessFact] = []
390
+ for node in self.index.nodes.values():
391
+ if node["kind"] != "Subscript":
392
+ continue
393
+ fields = node["fields"]
394
+ base = self.index.nodes.get(fields.get("value"))
395
+ binding = self.binding_by_occurrence.get(base["node_id"]) if base and base["kind"] == "Name" else None
396
+ fact = bindings.get(binding["binding_id"] if binding else "")
397
+ if not fact:
398
+ continue
399
+ shape = shapes[fact.literal_node_id]
400
+ slice_id = fields.get("slice")
401
+ if self._is_mutation_target(node["node_id"]):
402
+ result.append(self._rejected_access(node, fact, slice_id, "PYC3406", "Container element mutation is unsupported"))
403
+ continue
404
+ if not shape.valid:
405
+ result.append(self._rejected_access(node, fact, slice_id, shape.diagnostic_code or "PYC3402", shape.reason or "Invalid container shape"))
406
+ continue
407
+ if fact.container_kind in {"list", "tuple"}:
408
+ index = self._signed_integer_literal(slice_id)
409
+ if index is None:
410
+ result.append(self._rejected_access(node, fact, slice_id, "PYC3404", "List and tuple indices must be compile-time integer literals"))
411
+ continue
412
+ offset = index if index >= 0 else fact.capacity + index
413
+ if not 0 <= offset < fact.capacity:
414
+ result.append(self._rejected_access(node, fact, slice_id, "PYC3405", "Container index is outside the fixed capacity", source_index=index))
415
+ continue
416
+ result.append(ContainerAccessFact(node["node_id"], fact.binding_id, fact.container_kind, slice_id, fact.element_category, offset, index, None, True, None, None))
417
+ continue
418
+ key = self._literal_key(slice_id)
419
+ if key is None or key[1] is not fact.key_category:
420
+ result.append(self._rejected_access(node, fact, slice_id, "PYC3404", "Dictionary lookup requires a literal key matching the fixed key representation"))
421
+ continue
422
+ key_value = key[0]
423
+ try:
424
+ offset = shape.key_values.index(key_value)
425
+ except ValueError:
426
+ result.append(self._rejected_access(node, fact, slice_id, "PYC3405", "Dictionary key is absent from the fixed literal", key_value=key_value))
427
+ continue
428
+ result.append(ContainerAccessFact(node["node_id"], fact.binding_id, "dict", slice_id, fact.value_category, offset, None, key_value, True, None, None))
429
+ return tuple(sorted(result, key=lambda item: item.subscript_node_id))
430
+
431
+ def _rejected_access(
432
+ self,
433
+ node: dict[str, Any],
434
+ fact: ContainerBindingFact,
435
+ slice_id: str | None,
436
+ code: str,
437
+ reason: str,
438
+ *,
439
+ source_index: int | None = None,
440
+ key_value: int | str | None = None,
441
+ ) -> ContainerAccessFact:
442
+ return ContainerAccessFact(node["node_id"], fact.binding_id, fact.container_kind, slice_id, ValueCategory.UNKNOWN, None, source_index, key_value, False, code, reason)
443
+
444
+ def _iterations(
445
+ self,
446
+ bindings: dict[str, ContainerBindingFact],
447
+ shapes: dict[str, ContainerShapeFact],
448
+ ) -> tuple[ContainerIterationFact, ...]:
449
+ result: list[ContainerIterationFact] = []
450
+ for node in self.index.nodes.values():
451
+ if node["kind"] != "For":
452
+ continue
453
+ fields = node["fields"]
454
+ iterator = self.index.nodes.get(fields.get("iter"))
455
+ if not iterator:
456
+ continue
457
+ if iterator["kind"] in _LITERAL_KINDS:
458
+ result.append(ContainerIterationFact(node["node_id"], None, _LITERAL_KINDS[iterator["kind"]], fields.get("target"), ValueCategory.UNKNOWN, 0, "unresolved", False, "PYC3407", "A container iterable must be a directly bound fixed local name"))
459
+ continue
460
+ if iterator["kind"] != "Name":
461
+ continue
462
+ binding = self.binding_by_occurrence.get(iterator["node_id"])
463
+ fact = bindings.get(binding["binding_id"] if binding else "")
464
+ if not fact:
465
+ continue
466
+ shape = shapes[fact.literal_node_id]
467
+ target_id = fields.get("target")
468
+ target = self.index.nodes.get(target_id)
469
+ category = fact.key_category if fact.container_kind == "dict" else fact.element_category
470
+ if not shape.valid:
471
+ result.append(ContainerIterationFact(node["node_id"], fact.binding_id, fact.container_kind, target_id, ValueCategory.UNKNOWN, fact.capacity, "unresolved", False, shape.diagnostic_code, shape.reason))
472
+ elif not target or target["kind"] != "Name" or fields.get("orelse"):
473
+ result.append(ContainerIterationFact(node["node_id"], fact.binding_id, fact.container_kind, target_id, category, fact.capacity, "source-insertion-order", False, "PYC3407", "Container iteration requires a single-name target and no loop else"))
474
+ else:
475
+ result.append(ContainerIterationFact(node["node_id"], fact.binding_id, fact.container_kind, target_id, category, fact.capacity, "source-insertion-order", True, None, None))
476
+ return tuple(sorted(result, key=lambda item: item.for_node_id))
477
+
478
+ def _literal_key(self, node_id: str | None) -> tuple[int | str, ValueCategory] | None:
479
+ if not node_id or node_id not in self.index.nodes:
480
+ return None
481
+ node = self.index.node(node_id)
482
+ if node["kind"] == "Constant":
483
+ value = node["fields"].get("value")
484
+ if isinstance(value, bool):
485
+ return None
486
+ if isinstance(value, int) and -(2**63 - 1) <= value <= 2**63 - 1:
487
+ return value, ValueCategory.INTEGER
488
+ if isinstance(value, str) and "\x00" not in value:
489
+ return value, ValueCategory.STRING
490
+ return None
491
+ value = self._signed_integer_literal(node_id)
492
+ return (value, ValueCategory.INTEGER) if value is not None else None
493
+
494
+ def _signed_integer_literal(self, node_id: str | None) -> int | None:
495
+ if not node_id or node_id not in self.index.nodes:
496
+ return None
497
+ node = self.index.node(node_id)
498
+ if node["kind"] == "Constant":
499
+ value = node["fields"].get("value")
500
+ if isinstance(value, int) and not isinstance(value, bool) and -(2**63 - 1) <= value <= 2**63 - 1:
501
+ return value
502
+ return None
503
+ if node["kind"] != "UnaryOp":
504
+ return None
505
+ operator = self.index.nodes.get(node["fields"].get("op"), {}).get("kind")
506
+ operand = self.index.nodes.get(node["fields"].get("operand"))
507
+ if not operand or operand["kind"] != "Constant":
508
+ return None
509
+ value = operand["fields"].get("value")
510
+ if not isinstance(value, int) or isinstance(value, bool) or value > 2**63 - 1:
511
+ return None
512
+ signed = -value if operator == "USub" else value if operator == "UAdd" else None
513
+ return signed if signed is not None and -(2**63 - 1) <= signed <= 2**63 - 1 else None
514
+
515
+ def _is_mutation_target(self, node_id: str) -> bool:
516
+ for parent_id, field_name in self.parents.get(node_id, ()):
517
+ parent = self.index.node(parent_id)
518
+ if parent["kind"] in {"Assign", "AnnAssign", "AugAssign", "Delete"} and field_name in {"targets", "target"}:
519
+ return True
520
+ return False
521
+
522
+ def _is_direct_function_body(self, node_id: str) -> bool:
523
+ return any(
524
+ field_name == "body" and self.index.node(parent_id)["kind"] == "FunctionDef"
525
+ for parent_id, field_name in self.parents.get(node_id, ())
526
+ )
527
+
528
+ def _ordinal(self, node_id: str) -> tuple[int, str]:
529
+ node = self.index.nodes.get(node_id, {})
530
+ span = node.get("provenance", {}).get("source_span") or {}
531
+ offset = (span.get("start") or {}).get("offset")
532
+ return (offset if isinstance(offset, int) else 2**63 - 1, node_id)
533
+
534
+ @classmethod
535
+ def _references(cls, value: Any) -> tuple[str, ...]:
536
+ if isinstance(value, str) and value.startswith(("py-", "syn-")):
537
+ return (value,)
538
+ if isinstance(value, (tuple, list)):
539
+ return tuple(item for value_item in value for item in cls._references(value_item))
540
+ if isinstance(value, dict):
541
+ return tuple(item for value_item in value.values() for item in cls._references(value_item))
542
+ return ()