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,834 @@
1
+ from __future__ import annotations
2
+
3
+ import math
4
+ import re
5
+ from dataclasses import dataclass
6
+
7
+ from pycforge.converter.contracts.identifiers import (
8
+ C11_EXTERNAL_IDENTIFIERS as _C11_EXTERNAL_IDENTIFIERS,
9
+ C_KEYWORDS as _C_KEYWORDS,
10
+ TARGET_RESERVED_NAMES as _TARGET_RESERVED_NAMES,
11
+ )
12
+ from pycforge.converter.contracts.configuration import MAX_CONTAINER_ELEMENTS
13
+
14
+ from .model import *
15
+
16
+ _ID = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")
17
+ _ALLOWED_BASES = {"void","char","int","long","int8_t","uint8_t","int16_t","uint16_t","int32_t","uint32_t","int64_t","uint64_t","bool","double"}
18
+ _LOGICAL = {CBinaryOp.LOGICAL_AND, CBinaryOp.LOGICAL_OR}
19
+ _COMPARISONS = {CBinaryOp.EQUAL, CBinaryOp.NOT_EQUAL, CBinaryOp.LESS, CBinaryOp.LESS_EQUAL, CBinaryOp.GREATER, CBinaryOp.GREATER_EQUAL}
20
+ _NUMERIC = {"int8_t","uint8_t","int16_t","uint16_t","int32_t","uint32_t","int64_t","uint64_t","int","long","double"}
21
+ _INTEGER = _NUMERIC - {"double"}
22
+ _INTEGER_ONLY = {CBinaryOp.REMAINDER, CBinaryOp.SHIFT_LEFT, CBinaryOp.SHIFT_RIGHT, CBinaryOp.BIT_AND, CBinaryOp.BIT_XOR, CBinaryOp.BIT_OR}
23
+ _HEADER = re.compile(r"^[A-Za-z0-9_./-]+$")
24
+ _REGISTERED_HEADERS = {("stdint.h", True), ("stdbool.h", True)}
25
+ _STDINT_BASES = {"int8_t","uint8_t","int16_t","uint16_t","int32_t","uint32_t","int64_t","uint64_t"}
26
+ _SHA256_HEX = re.compile(r"^[0-9a-f]{64}$")
27
+ _MODULE_AWARE_SCHEMAS = {
28
+ MODULE_SCHEMA_VERSION,
29
+ RECORD_SCHEMA_VERSION,
30
+ NUMERIC_SCHEMA_VERSION,
31
+ CONDITIONAL_SCHEMA_VERSION,
32
+ KEYWORD_CALL_SCHEMA_VERSION,
33
+ KEYWORD_ONLY_CALL_SCHEMA_VERSION,
34
+ }
35
+ _RECORD_AWARE_SCHEMAS = {
36
+ RECORD_SCHEMA_VERSION,
37
+ NUMERIC_SCHEMA_VERSION,
38
+ CONDITIONAL_SCHEMA_VERSION,
39
+ KEYWORD_CALL_SCHEMA_VERSION,
40
+ KEYWORD_ONLY_CALL_SCHEMA_VERSION,
41
+ }
42
+ _CONTAINER_AWARE_SCHEMAS = {
43
+ CONTAINER_SCHEMA_VERSION,
44
+ MODULE_SCHEMA_VERSION,
45
+ RECORD_SCHEMA_VERSION,
46
+ NUMERIC_SCHEMA_VERSION,
47
+ CONDITIONAL_SCHEMA_VERSION,
48
+ KEYWORD_CALL_SCHEMA_VERSION,
49
+ KEYWORD_ONLY_CALL_SCHEMA_VERSION,
50
+ }
51
+ _HELPER_AWARE_SCHEMAS = {
52
+ HELPER_SCHEMA_VERSION,
53
+ CONTAINER_SCHEMA_VERSION,
54
+ MODULE_SCHEMA_VERSION,
55
+ RECORD_SCHEMA_VERSION,
56
+ NUMERIC_SCHEMA_VERSION,
57
+ CONDITIONAL_SCHEMA_VERSION,
58
+ KEYWORD_CALL_SCHEMA_VERSION,
59
+ KEYWORD_ONLY_CALL_SCHEMA_VERSION,
60
+ }
61
+
62
+
63
+ @dataclass(frozen=True, slots=True)
64
+ class CIRValidationResult:
65
+ accepted: bool
66
+ errors: tuple[str, ...]
67
+
68
+
69
+ def _identifier_error(name: str, *, file_scope: bool = False) -> str | None:
70
+ if not _ID.fullmatch(name): return f"invalid C identifier: {name}"
71
+ if name in _C_KEYWORDS or name in _TARGET_RESERVED_NAMES: return f"reserved target name cannot be an identifier: {name}"
72
+ if name.startswith("_"): return f"implementation-reserved identifier: {name}"
73
+ if file_scope and name in _C11_EXTERNAL_IDENTIFIERS: return f"reserved C11 external identifier: {name}"
74
+ return None
75
+
76
+
77
+ def validate_translation_unit(unit: CTranslationUnit) -> CIRValidationResult:
78
+ errors: list[str] = []
79
+ node_ids: set[str] = set()
80
+ globals_by_binding: dict[str, tuple[CIdentifier, CType | None]] = {}
81
+ global_spelling: dict[str, str] = {}
82
+ signatures: dict[str, tuple[CType, tuple[CType, ...]]] = {}
83
+ parameter_identities: dict[str, tuple[tuple[str, str], ...]] = {}
84
+ function_storage: dict[str, CStorage] = {}
85
+ function_origins: dict[str, str] = {}
86
+ external_significant_prefixes: dict[str, str] = {}
87
+ function_ownership: dict[str, tuple[str | None, str | None, int | None]] = {}
88
+ represented_cross_module_calls: set[tuple[str, str]] = set()
89
+ active_source_owner: str | None = None
90
+ active_source_document: str | None = None
91
+ prototype_bindings: set[str] = set()
92
+ definition_bindings: set[str] = set()
93
+ used_bases: set[str] = set()
94
+ record_definitions: dict[str, CRecordDefinition] = {}
95
+ record_bindings: dict[str, CRecordDefinition] = {}
96
+ record_fields: dict[str, tuple[CRecordDefinition, CRecordField]] = {}
97
+
98
+ if unit.schema_version not in SUPPORTED_SCHEMA_VERSIONS:
99
+ errors.append("unsupported C IR schema")
100
+ if not unit.target_contract:
101
+ errors.append("missing Target C Source Contract")
102
+
103
+ module_by_id: dict[str, CModuleManifestEntry] = {}
104
+ module_by_document: dict[str, CModuleManifestEntry] = {}
105
+ module_order_index: dict[str, int] = {}
106
+ if unit.schema_version in _MODULE_AWARE_SCHEMAS:
107
+ module_ids = [item.module_id for item in unit.module_manifest]
108
+ document_ids = [item.document_id for item in unit.module_manifest]
109
+ bundle_ordinals = sorted(item.bundle_ordinal for item in unit.module_manifest)
110
+ if not unit.module_manifest:
111
+ errors.append("C IR 0.12+ requires a nonempty module manifest")
112
+ if len(module_ids) != len(set(module_ids)) or any(not item for item in module_ids):
113
+ errors.append("module manifest IDs must be nonempty and unique")
114
+ if len(document_ids) != len(set(document_ids)) or any(not item for item in document_ids):
115
+ errors.append("module manifest document IDs must be nonempty and unique")
116
+ if bundle_ordinals != list(range(len(unit.module_manifest))):
117
+ errors.append("module manifest bundle ordinals must be contiguous")
118
+ if sum(item.is_primary is True for item in unit.module_manifest) != 1:
119
+ errors.append("module manifest requires exactly one primary document")
120
+ if any(item.is_primary is not (item.bundle_ordinal == 0) for item in unit.module_manifest):
121
+ errors.append("module manifest primary marker must identify bundle ordinal zero")
122
+ if any(not item.logical_name for item in unit.module_manifest):
123
+ errors.append("module manifest logical source names must be nonempty")
124
+ if tuple(module_ids) != unit.module_order or len(unit.module_order) != len(set(unit.module_order)):
125
+ errors.append("module manifest and module order disagree")
126
+ module_by_id = {item.module_id: item for item in unit.module_manifest}
127
+ module_by_document = {item.document_id: item for item in unit.module_manifest}
128
+ module_order_index = {module_id: ordinal for ordinal, module_id in enumerate(unit.module_order)}
129
+ if unit.module_dependencies != tuple(sorted(set(unit.module_dependencies))):
130
+ errors.append("module dependency edges must be unique and sorted")
131
+ for importer, target in unit.module_dependencies:
132
+ if importer not in module_by_id or target not in module_by_id or importer == target:
133
+ errors.append("module dependency edge references an invalid module")
134
+ elif module_order_index[target] >= module_order_index[importer]:
135
+ errors.append("C IR module order is not dependency-first")
136
+ elif unit.module_manifest or unit.module_order or unit.module_dependencies:
137
+ errors.append("module metadata requires C IR schema 0.12 or later")
138
+
139
+ def node(node_id: str) -> None:
140
+ if not node_id: errors.append("empty C IR node id")
141
+ elif node_id in node_ids: errors.append(f"duplicate C IR node id: {node_id}")
142
+ else: node_ids.add(node_id)
143
+
144
+ def record_source_document(
145
+ provenance: CProvenance,
146
+ subject: str,
147
+ *,
148
+ expected: str | None = None,
149
+ ) -> str | None:
150
+ """Validate the document anchor for a Phase 13 record C IR node."""
151
+
152
+ if unit.schema_version not in _RECORD_AWARE_SCHEMAS:
153
+ return provenance.source_document_id
154
+ document_id = provenance.source_document_id
155
+ if not isinstance(document_id, str) or document_id not in module_by_document:
156
+ errors.append(
157
+ f"{subject} provenance must reference a document in the module manifest"
158
+ )
159
+ return None
160
+ if expected is not None and document_id != expected:
161
+ errors.append(f"{subject} provenance disagrees with its owning source document")
162
+ return document_id
163
+
164
+ def typ(value: CType, *, allow_void: bool = False) -> None:
165
+ used_bases.add(value.base)
166
+ if value.base not in _ALLOWED_BASES and value.base not in record_definitions:
167
+ errors.append(f"unsupported C base type: {value.base}")
168
+ if value.base in record_definitions and unit.schema_version not in _RECORD_AWARE_SCHEMAS:
169
+ errors.append(f"record types require C IR schema 0.13: {value.base}")
170
+ if value.base == "void" and value.pointer_depth == 0 and not allow_void: errors.append("void object type is invalid")
171
+ if value.base == "void" and value.array_extents: errors.append("array element type cannot be void")
172
+ if not isinstance(value.pointer_depth, int) or isinstance(value.pointer_depth, bool) or value.pointer_depth < 0: errors.append("invalid pointer depth")
173
+ if any(not isinstance(item, CQualifier) for item in value.qualifiers): errors.append("invalid type qualifier")
174
+ if len(set(value.qualifiers)) != len(value.qualifiers): errors.append("duplicate type qualifier")
175
+ if not isinstance(value.object_const, bool): errors.append("invalid object-const marker")
176
+ if value.object_const and not value.array_extents: errors.append("object-const is reserved for fixed-array elements")
177
+ if value.array_extents:
178
+ if unit.schema_version not in _CONTAINER_AWARE_SCHEMAS:
179
+ errors.append("array types require C IR schema 0.11")
180
+ if len(value.array_extents) != 1:
181
+ errors.append("only one-dimensional fixed arrays are supported")
182
+ if any(
183
+ not isinstance(item, int)
184
+ or isinstance(item, bool)
185
+ or not 1 <= item <= MAX_CONTAINER_ELEMENTS
186
+ for item in value.array_extents
187
+ ):
188
+ errors.append("array extent must be an integer from 1 through 64")
189
+
190
+ def identifier(value: CIdentifier, *, file_scope: bool = False) -> None:
191
+ if not value.binding_id: errors.append("empty C binding id")
192
+ problem = _identifier_error(value.spelling, file_scope=file_scope)
193
+ if problem: errors.append(problem)
194
+
195
+ def declare_global(value: CIdentifier, type_ref: CType | None) -> None:
196
+ identifier(value, file_scope=True)
197
+ if value.binding_id in record_bindings:
198
+ errors.append(f"binding ID is shared by a record type and an object: {value.binding_id}")
199
+ old = globals_by_binding.get(value.binding_id)
200
+ if old and old[0].spelling != value.spelling: errors.append(f"binding spelling mismatch: {value.binding_id}")
201
+ elif old and old[1] != type_ref: errors.append(f"binding type mismatch: {value.binding_id}")
202
+ else: globals_by_binding.setdefault(value.binding_id, (value, type_ref))
203
+ other = global_spelling.get(value.spelling)
204
+ if other and other != value.binding_id: errors.append(f"duplicate global identifier spelling: {value.spelling}")
205
+ global_spelling.setdefault(value.spelling, value.binding_id)
206
+
207
+ def register_function(value: CFunctionPrototype | CFunctionDefinition) -> None:
208
+ helper = value.provenance.origin_kind == "support-template"
209
+ if not isinstance(value.storage, CStorage):
210
+ errors.append(f"invalid function storage for {value.identifier.spelling}")
211
+ elif helper:
212
+ if unit.schema_version not in _HELPER_AWARE_SCHEMAS:
213
+ errors.append(f"support-template function requires C IR schema 0.10 or later: {value.identifier.spelling}")
214
+ if value.storage is not CStorage.STATIC:
215
+ errors.append(f"support-template function requires static linkage: {value.identifier.spelling}")
216
+ if not value.identifier.spelling.startswith("pycf_"):
217
+ errors.append(f"support-template function requires reserved pycf_ spelling: {value.identifier.spelling}")
218
+ elif value.storage is not CStorage.NONE:
219
+ errors.append(f"source function requires default external linkage: {value.identifier.spelling}")
220
+ elif not helper:
221
+ significant_prefix=value.identifier.spelling[:31]
222
+ previous=external_significant_prefixes.get(significant_prefix)
223
+ if previous is not None and previous != value.identifier.binding_id:
224
+ errors.append(f"external identifiers collide within the C11 31-character significant prefix: {value.identifier.spelling}")
225
+ external_significant_prefixes.setdefault(significant_prefix,value.identifier.binding_id)
226
+ if unit.schema_version in _MODULE_AWARE_SCHEMAS and not helper:
227
+ owner = module_by_id.get(value.owner_module_id or "")
228
+ if (
229
+ owner is None
230
+ or value.owner_document_id != owner.document_id
231
+ or not isinstance(value.bundle_function_ordinal, int)
232
+ or isinstance(value.bundle_function_ordinal, bool)
233
+ or value.bundle_function_ordinal < 0
234
+ ):
235
+ errors.append(f"source function has invalid module ownership: {value.identifier.spelling}")
236
+ if len(unit.module_manifest) > 1 and not value.identifier.spelling.startswith("pycm_"):
237
+ errors.append(f"multi-document source function requires reserved pycm_ spelling: {value.identifier.spelling}")
238
+ elif len(unit.module_manifest) > 1:
239
+ digest_start = len("pycm_")
240
+ digest_end = digest_start + 64
241
+ digest = value.identifier.spelling[digest_start:digest_end]
242
+ escaped_owner = (value.owner_module_id or "").replace("_", "_u").replace(".", "_d")
243
+ readable_suffix = value.identifier.spelling[digest_end:]
244
+ expected_prefix = f"__{escaped_owner}__"
245
+ if (
246
+ not _SHA256_HEX.fullmatch(digest)
247
+ or not readable_suffix.startswith(expected_prefix)
248
+ or not readable_suffix.removeprefix(expected_prefix)
249
+ ):
250
+ errors.append(
251
+ f"multi-document source function requires digest-first module-qualified spelling: {value.identifier.spelling}"
252
+ )
253
+ if len(unit.module_manifest) == 1 and value.identifier.spelling.startswith("pycm_"):
254
+ errors.append(f"singleton source function must retain its legacy spelling: {value.identifier.spelling}")
255
+ if value.provenance.source_document_id != value.owner_document_id:
256
+ errors.append(f"source function provenance disagrees with module ownership: {value.identifier.spelling}")
257
+ elif unit.schema_version not in _MODULE_AWARE_SCHEMAS and any(
258
+ item is not None
259
+ for item in (value.owner_module_id, value.owner_document_id, value.bundle_function_ordinal)
260
+ ):
261
+ errors.append(f"module ownership metadata requires C IR schema 0.12 or later: {value.identifier.spelling}")
262
+ typ(value.return_type, allow_void=True)
263
+ if value.return_type.array_extents:
264
+ errors.append(f"function return types cannot be arrays: {value.identifier.spelling}")
265
+ returned_record = record_definitions.get(value.return_type.base)
266
+ if (
267
+ returned_record is not None
268
+ and not helper
269
+ and returned_record.provenance.source_document_id != value.owner_document_id
270
+ ):
271
+ errors.append(
272
+ f"record return type belongs to a foreign source document: {value.identifier.spelling}"
273
+ )
274
+ for parameter in value.parameters:
275
+ node(parameter.node_id); typ(parameter.type_ref); identifier(parameter.identifier)
276
+ if parameter.type_ref.array_extents:
277
+ errors.append(f"array parameters are outside the fixed local-container profile: {value.identifier.spelling}")
278
+ parameter_record = record_definitions.get(parameter.type_ref.base)
279
+ if parameter_record is not None and not helper:
280
+ record_source_document(
281
+ parameter.provenance,
282
+ f"record parameter {parameter.identifier.spelling}",
283
+ expected=value.owner_document_id,
284
+ )
285
+ record_source_document(
286
+ parameter.identifier.provenance,
287
+ f"record parameter identifier {parameter.identifier.spelling}",
288
+ expected=value.owner_document_id,
289
+ )
290
+ if (
291
+ parameter_record.provenance.source_document_id
292
+ != value.owner_document_id
293
+ ):
294
+ errors.append(
295
+ "record parameter type belongs to a foreign source document: "
296
+ f"{parameter.identifier.spelling}"
297
+ )
298
+ parameter_spellings = [item.identifier.spelling for item in value.parameters]
299
+ if len(parameter_spellings) != len(set(parameter_spellings)): errors.append(f"duplicate parameter spelling in {value.identifier.spelling}")
300
+ signature = (value.return_type, tuple(item.type_ref for item in value.parameters))
301
+ declare_global(value.identifier, None)
302
+ old = signatures.get(value.identifier.binding_id)
303
+ if old and old != signature: errors.append(f"inconsistent function signature: {value.identifier.spelling}")
304
+ signatures.setdefault(value.identifier.binding_id, signature)
305
+ old_storage = function_storage.get(value.identifier.binding_id)
306
+ if old_storage is not None and old_storage is not value.storage:
307
+ errors.append(f"prototype/definition storage mismatch: {value.identifier.spelling}")
308
+ function_storage.setdefault(value.identifier.binding_id, value.storage)
309
+ origin = value.provenance.origin_kind
310
+ old_origin = function_origins.get(value.identifier.binding_id)
311
+ if old_origin is not None and old_origin != origin and "support-template" in {old_origin, origin}:
312
+ errors.append(f"prototype/definition provenance mismatch: {value.identifier.spelling}")
313
+ function_origins.setdefault(value.identifier.binding_id, origin)
314
+ ownership = (value.owner_module_id, value.owner_document_id, value.bundle_function_ordinal)
315
+ old_ownership = function_ownership.get(value.identifier.binding_id)
316
+ if old_ownership is not None and old_ownership != ownership:
317
+ errors.append(f"prototype/definition module ownership mismatch: {value.identifier.spelling}")
318
+ function_ownership.setdefault(value.identifier.binding_id, ownership)
319
+ identities = tuple((item.identifier.binding_id, item.identifier.spelling) for item in value.parameters)
320
+ old_identities = parameter_identities.get(value.identifier.binding_id)
321
+ if old_identities and old_identities != identities: errors.append(f"prototype/definition parameter identity mismatch: {value.identifier.spelling}")
322
+ parameter_identities.setdefault(value.identifier.binding_id, identities)
323
+ declaration_set = prototype_bindings if isinstance(value, CFunctionPrototype) else definition_bindings
324
+ if value.identifier.binding_id in declaration_set: errors.append(f"duplicate function {'prototype' if isinstance(value,CFunctionPrototype) else 'definition'}: {value.identifier.spelling}")
325
+ declaration_set.add(value.identifier.binding_id)
326
+
327
+ node(unit.node_id)
328
+ headers: set[tuple[str, bool]] = set()
329
+ for include in unit.includes:
330
+ node(include.node_id)
331
+ if not include.header or not _HEADER.fullmatch(include.header) or include.header.startswith("/") or ".." in include.header.split("/"): errors.append(f"invalid include header: {include.header}")
332
+ key = (include.header, include.system)
333
+ if key in headers: errors.append(f"duplicate include: {include.header}")
334
+ if key not in _REGISTERED_HEADERS: errors.append(f"unregistered include: {include.header}")
335
+ headers.add(key)
336
+
337
+ # Register typedef names before validating any use so record types are
338
+ # structural and independent of declaration traversal during validation.
339
+ for declaration in unit.declarations:
340
+ if not isinstance(declaration, CRecordDefinition):
341
+ continue
342
+ if unit.schema_version not in _RECORD_AWARE_SCHEMAS:
343
+ errors.append("record definitions require C IR schema 0.13")
344
+ identifier(declaration.identifier, file_scope=True)
345
+ record_document = record_source_document(
346
+ declaration.provenance,
347
+ f"record definition {declaration.identifier.spelling}",
348
+ )
349
+ record_source_document(
350
+ declaration.identifier.provenance,
351
+ f"record type identifier {declaration.identifier.spelling}",
352
+ expected=record_document,
353
+ )
354
+ previous_binding = record_bindings.get(declaration.identifier.binding_id)
355
+ if previous_binding is not None:
356
+ errors.append(f"duplicate record type binding: {declaration.identifier.binding_id}")
357
+ else:
358
+ record_bindings[declaration.identifier.binding_id] = declaration
359
+ previous_spelling = record_definitions.get(declaration.identifier.spelling)
360
+ if previous_spelling is not None:
361
+ errors.append(f"duplicate record type spelling: {declaration.identifier.spelling}")
362
+ else:
363
+ record_definitions[declaration.identifier.spelling] = declaration
364
+ other = global_spelling.get(declaration.identifier.spelling)
365
+ if other is not None and other != declaration.identifier.binding_id:
366
+ errors.append(f"duplicate global identifier spelling: {declaration.identifier.spelling}")
367
+ global_spelling.setdefault(declaration.identifier.spelling, declaration.identifier.binding_id)
368
+
369
+ # Register all external names and signatures before validating bodies so
370
+ # forward calls are structural and never depend on declaration order.
371
+ for declaration in unit.declarations:
372
+ node(declaration.node_id)
373
+ if isinstance(declaration, CRecordDefinition):
374
+ if not declaration.fields:
375
+ errors.append(f"record definition requires at least one field: {declaration.identifier.spelling}")
376
+ if len(declaration.fields) > MAX_CONTAINER_ELEMENTS:
377
+ errors.append(f"record definition exceeds the 64-field limit: {declaration.identifier.spelling}")
378
+ record_document = declaration.provenance.source_document_id
379
+ field_spellings: set[str] = set()
380
+ for field in declaration.fields:
381
+ node(field.node_id)
382
+ identifier(field.identifier)
383
+ field_document = record_source_document(
384
+ field.provenance,
385
+ f"record field {field.identifier.spelling}",
386
+ expected=record_document,
387
+ )
388
+ record_source_document(
389
+ field.identifier.provenance,
390
+ f"record field identifier {field.identifier.spelling}",
391
+ expected=field_document or record_document,
392
+ )
393
+ typ(field.type_ref)
394
+ if field.type_ref not in (
395
+ CType("int64_t"),
396
+ CType("double"),
397
+ CType("bool"),
398
+ ):
399
+ errors.append(
400
+ "record fields require an exact unqualified int64_t, "
401
+ f"double, or bool type: {field.identifier.spelling}"
402
+ )
403
+ if field.type_ref.base in record_definitions:
404
+ errors.append(f"nested record fields are outside the C IR 0.13 profile: {field.identifier.spelling}")
405
+ if field.type_ref.array_extents:
406
+ errors.append(f"record fields must be scalar: {field.identifier.spelling}")
407
+ if field.identifier.spelling in field_spellings:
408
+ errors.append(f"duplicate record field spelling: {field.identifier.spelling}")
409
+ field_spellings.add(field.identifier.spelling)
410
+ if field.identifier.binding_id in record_bindings:
411
+ errors.append(f"binding ID is shared by a record type and field: {field.identifier.binding_id}")
412
+ previous_field = record_fields.get(field.identifier.binding_id)
413
+ if previous_field is not None:
414
+ errors.append(f"duplicate record field binding: {field.identifier.binding_id}")
415
+ else:
416
+ record_fields[field.identifier.binding_id] = (declaration, field)
417
+ elif isinstance(declaration, (CFunctionPrototype, CFunctionDefinition)):
418
+ if isinstance(declaration, CFunctionPrototype) and unit.schema_version == LEGACY_SCHEMA_VERSION:
419
+ errors.append("function prototypes require C IR schema 0.9")
420
+ register_function(declaration)
421
+ elif isinstance(declaration, CVariableDeclaration):
422
+ typ(declaration.type_ref); declare_global(declaration.identifier, declaration.type_ref)
423
+ if declaration.type_ref.array_extents:
424
+ errors.append("fixed arrays must have automatic function-local storage")
425
+ else:
426
+ errors.append(f"unknown external declaration: {type(declaration).__name__}")
427
+
428
+ if unit.schema_version in {
429
+ SCHEMA_VERSION,
430
+ HELPER_SCHEMA_VERSION,
431
+ CONTAINER_SCHEMA_VERSION,
432
+ MODULE_SCHEMA_VERSION,
433
+ RECORD_SCHEMA_VERSION,
434
+ NUMERIC_SCHEMA_VERSION,
435
+ CONDITIONAL_SCHEMA_VERSION,
436
+ KEYWORD_CALL_SCHEMA_VERSION,
437
+ KEYWORD_ONLY_CALL_SCHEMA_VERSION,
438
+ }:
439
+ if prototype_bindings != definition_bindings:
440
+ errors.append("C IR 0.9+ requires exactly one prototype for every function definition")
441
+ definition_seen = False
442
+ for declaration in unit.declarations:
443
+ if isinstance(declaration, CFunctionDefinition):
444
+ definition_seen = True
445
+ elif isinstance(declaration, CFunctionPrototype) and definition_seen:
446
+ errors.append("function prototypes must precede function definitions")
447
+ break
448
+ if unit.schema_version in _MODULE_AWARE_SCHEMAS:
449
+ def declaration_category(value: CExternalDeclaration) -> int:
450
+ if isinstance(value, CRecordDefinition):
451
+ return 0
452
+ helper = value.provenance.origin_kind == "support-template"
453
+ if isinstance(value, CFunctionPrototype):
454
+ return 1 if helper else 2
455
+ if isinstance(value, CVariableDeclaration):
456
+ return 3
457
+ return 4 if helper else 5
458
+
459
+ categories = [declaration_category(item) for item in unit.declarations]
460
+ if categories != sorted(categories):
461
+ errors.append("C IR 0.12+ declaration categories are not in assembly order")
462
+ source_prototypes = [
463
+ item for item in unit.declarations
464
+ if isinstance(item, CFunctionPrototype) and item.provenance.origin_kind != "support-template"
465
+ ]
466
+ source_definitions = [
467
+ item for item in unit.declarations
468
+ if isinstance(item, CFunctionDefinition) and item.provenance.origin_kind != "support-template"
469
+ ]
470
+ expected_ordinals = list(range(len(source_prototypes)))
471
+ if [item.bundle_function_ordinal for item in source_prototypes] != expected_ordinals:
472
+ errors.append("source prototypes are not in module/function order")
473
+ if [item.bundle_function_ordinal for item in source_definitions] != expected_ordinals:
474
+ errors.append("source definitions are not in module/function order")
475
+
476
+ scopes: list[dict[str, tuple[CIdentifier, CType]]] = []
477
+ spellings: list[dict[str, str]] = []
478
+
479
+ def push_scope() -> None:
480
+ scopes.append({}); spellings.append({})
481
+
482
+ def pop_scope() -> None:
483
+ scopes.pop(); spellings.pop()
484
+
485
+ def declare_local(value: CIdentifier, type_ref: CType) -> None:
486
+ identifier(value); typ(type_ref)
487
+ if value.binding_id in record_bindings or value.binding_id in record_fields:
488
+ errors.append(f"local binding ID collides with a record binding: {value.binding_id}")
489
+ if value.binding_id in scopes[-1]: errors.append(f"duplicate local binding declaration: {value.binding_id}")
490
+ other = spellings[-1].get(value.spelling)
491
+ if other and other != value.binding_id: errors.append(f"duplicate local identifier spelling: {value.spelling}")
492
+ scopes[-1][value.binding_id] = (value, type_ref)
493
+ spellings[-1][value.spelling] = value.binding_id
494
+
495
+ def lookup(binding_id: str) -> tuple[CIdentifier, CType | None] | None:
496
+ for scope in reversed(scopes):
497
+ if binding_id in scope: return scope[binding_id]
498
+ return globals_by_binding.get(binding_id)
499
+
500
+ def initializer_type_matches(declared: CType, actual: CType) -> bool:
501
+ if declared == actual:
502
+ return True
503
+ # An aggregate initializer is represented by the unqualified record
504
+ # value it constructs. C permits that initializer for a const record
505
+ # object; const applies to the destination after initialization.
506
+ return (
507
+ actual.base in record_definitions
508
+ and actual == CType(actual.base)
509
+ and declared == CType(actual.base, (CQualifier.CONST,))
510
+ )
511
+
512
+ def expression(
513
+ value: CExpression,
514
+ *,
515
+ initializer: bool = False,
516
+ assignment_target: bool = False,
517
+ ) -> CType | None:
518
+ node(value.node_id)
519
+ if isinstance(value, CRecordInitializer):
520
+ if unit.schema_version not in _RECORD_AWARE_SCHEMAS:
521
+ errors.append("record initializers require C IR schema 0.13")
522
+ if not initializer:
523
+ errors.append("record initializers are valid only as variable initializers")
524
+ typ(value.record_type)
525
+ expected_type = CType(value.record_type.base)
526
+ definition = record_definitions.get(value.record_type.base)
527
+ if definition is None:
528
+ errors.append(f"record initializer names an unknown record type: {value.record_type.base}")
529
+ for item in value.elements:
530
+ expression(item)
531
+ return None
532
+ definition_document = definition.provenance.source_document_id
533
+ record_source_document(
534
+ value.provenance,
535
+ f"record initializer for {definition.identifier.spelling}",
536
+ expected=active_source_document or definition_document,
537
+ )
538
+ if (
539
+ active_source_document is not None
540
+ and definition_document != active_source_document
541
+ ):
542
+ errors.append(
543
+ "record initializer type belongs to a foreign source document: "
544
+ f"{definition.identifier.spelling}"
545
+ )
546
+ if value.record_type != expected_type:
547
+ errors.append("record initializer type must be an unqualified non-pointer record type")
548
+ actual_types = tuple(expression(item) for item in value.elements)
549
+ expected_types = tuple(field.type_ref for field in definition.fields)
550
+ if len(actual_types) != len(expected_types):
551
+ errors.append(
552
+ f"record initializer arity mismatch for {definition.identifier.spelling}: "
553
+ f"expected {len(expected_types)}, found {len(actual_types)}"
554
+ )
555
+ elif any(actual != expected for actual, expected in zip(actual_types, expected_types)):
556
+ errors.append(f"record initializer field type mismatch for {definition.identifier.spelling}")
557
+ return expected_type
558
+ if isinstance(value, CInitializerList):
559
+ if not initializer:
560
+ errors.append("initializer lists are valid only as variable initializers")
561
+ if not value.elements:
562
+ errors.append("empty initializer lists are unsupported")
563
+ return None
564
+ if len(value.elements) > MAX_CONTAINER_ELEMENTS:
565
+ errors.append("initializer list exceeds the fixed capacity limit")
566
+ element_types = tuple(expression(item) for item in value.elements)
567
+ first = element_types[0]
568
+ if first is None or first.array_extents or any(item != first for item in element_types):
569
+ errors.append("initializer-list elements must have one homogeneous scalar type")
570
+ return None
571
+ return CType(first.base, first.qualifiers, first.pointer_depth, (len(value.elements),))
572
+ if isinstance(value, CIdentifierRef):
573
+ found = lookup(value.binding_id)
574
+ if not found:
575
+ errors.append(f"unresolved binding reference: {value.binding_id}")
576
+ return None
577
+ if found[1] is None:
578
+ errors.append(f"function binding used outside a call target: {value.binding_id}")
579
+ return None
580
+ return found[1]
581
+ if isinstance(value, CSubscriptExpr):
582
+ container_type = expression(value.container)
583
+ index_type = expression(value.index)
584
+ if container_type is None or not container_type.array_extents:
585
+ errors.append("subscript base must have a fixed array type")
586
+ return None
587
+ if index_type != CType("int64_t"):
588
+ errors.append("subscript index must have int64_t type")
589
+ return CType(
590
+ container_type.base,
591
+ container_type.qualifiers,
592
+ container_type.pointer_depth,
593
+ container_type.array_extents[1:],
594
+ )
595
+ if isinstance(value, CMemberAccessExpr):
596
+ if unit.schema_version not in _RECORD_AWARE_SCHEMAS:
597
+ errors.append("record member access requires C IR schema 0.13")
598
+ receiver_type = expression(value.receiver)
599
+ if not isinstance(value.mode, CMemberAccessMode):
600
+ errors.append("invalid record member access mode")
601
+ return None
602
+ if receiver_type is None or receiver_type.array_extents:
603
+ errors.append("record member receiver has an invalid type")
604
+ return None
605
+ if value.mode is CMemberAccessMode.DIRECT:
606
+ if receiver_type.pointer_depth != 0:
607
+ errors.append("direct record member access requires a record object")
608
+ return None
609
+ elif receiver_type.pointer_depth != 1:
610
+ errors.append("pointer record member access requires a single-level record pointer")
611
+ return None
612
+ definition = record_definitions.get(receiver_type.base)
613
+ if definition is None:
614
+ errors.append("record member receiver must have a declared record type")
615
+ return None
616
+ definition_document = definition.provenance.source_document_id
617
+ expected_document = active_source_document or definition_document
618
+ record_source_document(
619
+ value.provenance,
620
+ "record member access",
621
+ expected=expected_document,
622
+ )
623
+ record_source_document(
624
+ value.receiver.provenance,
625
+ "record member receiver",
626
+ expected=expected_document,
627
+ )
628
+ if (
629
+ active_source_document is not None
630
+ and definition_document != active_source_document
631
+ ):
632
+ errors.append("record member receiver type belongs to a foreign source document")
633
+ field_entry = record_fields.get(value.field_binding_id)
634
+ if field_entry is None:
635
+ errors.append(f"unresolved record field binding: {value.field_binding_id}")
636
+ return None
637
+ owner, field = field_entry
638
+ if owner.identifier.binding_id != definition.identifier.binding_id:
639
+ errors.append(
640
+ f"record field does not belong to receiver type: {value.field_binding_id}"
641
+ )
642
+ return None
643
+ if assignment_target and CQualifier.CONST in receiver_type.qualifiers:
644
+ errors.append("assignment target is a member of a const record object")
645
+ return field.type_ref
646
+ if isinstance(value, CIntegerLiteral):
647
+ if value.suffix not in {"","L","LL","U","UL","ULL"}: errors.append(f"invalid integer suffix: {value.suffix}")
648
+ if value.value < 0: errors.append("C integer literal nodes must be non-negative; use structured unary negation")
649
+ if value.suffix in {"", "L", "LL"} and value.value > 2**63-1: errors.append("signed integer literal exceeds int64_t representation")
650
+ return CType("int64_t")
651
+ if isinstance(value, CFloatLiteral):
652
+ if not math.isfinite(value.value): errors.append("non-finite floating literal is unsupported")
653
+ return CType("double")
654
+ if isinstance(value, CBooleanLiteral):
655
+ used_bases.add("bool")
656
+ return CType("bool")
657
+ if isinstance(value, CStringLiteral):
658
+ if value.encoding != "utf-8": errors.append(f"unsupported string encoding: {value.encoding}")
659
+ if "\x00" in value.value: errors.append("embedded NUL in string literal is unsupported")
660
+ return CType("char", (CQualifier.CONST,), 1)
661
+ if isinstance(value, CUnaryExpr):
662
+ operand = expression(value.operand)
663
+ if value.op is CUnaryOp.LOGICAL_NOT:
664
+ if operand != CType("bool"): errors.append("logical-not operand must be bool")
665
+ return CType("bool")
666
+ if value.op is CUnaryOp.NEGATE:
667
+ if operand is None or operand.base not in _NUMERIC or operand.pointer_depth or operand.array_extents: errors.append("negation operand must be numeric")
668
+ return operand
669
+ if value.op is CUnaryOp.BITWISE_NOT:
670
+ if operand is None or operand.base not in _INTEGER or operand.pointer_depth or operand.array_extents: errors.append("bitwise-not operand must be an integer")
671
+ return operand
672
+ if value.op is CUnaryOp.ADDRESS_OF:
673
+ if operand is None: return None
674
+ if operand.array_extents:
675
+ errors.append("address-of on fixed arrays is outside the C IR profile")
676
+ return None
677
+ return CType(operand.base, operand.qualifiers, operand.pointer_depth + 1)
678
+ if value.op is CUnaryOp.DEREFERENCE:
679
+ if operand is None or operand.pointer_depth <= 0 or operand.array_extents:
680
+ errors.append("dereference operand must be a pointer")
681
+ return None
682
+ return CType(operand.base, operand.qualifiers, operand.pointer_depth - 1)
683
+ errors.append("unknown unary operator")
684
+ return None
685
+ if isinstance(value, CBinaryExpr):
686
+ left, right = expression(value.left), expression(value.right)
687
+ if value.op in _LOGICAL:
688
+ if left != CType("bool") or right != CType("bool"): errors.append("logical operator operands must be bool")
689
+ return CType("bool")
690
+ if value.op in _COMPARISONS:
691
+ if left is None or right is None or left != right or left.array_extents or right.array_extents: errors.append("comparison operand types are incompatible")
692
+ return CType("bool")
693
+ if left is None or right is None or left != right or left.base not in _NUMERIC or left.pointer_depth or left.array_extents:
694
+ errors.append("binary arithmetic operand types are incompatible")
695
+ return None
696
+ if value.op in _INTEGER_ONLY and left.base not in _INTEGER:
697
+ errors.append("integer-only binary operator has a non-integer operand")
698
+ return None
699
+ return left
700
+ if isinstance(value, CCallExpr):
701
+ node(value.callee.node_id)
702
+ if lookup(value.callee.binding_id) is None:
703
+ errors.append(f"unresolved call target binding: {value.callee.binding_id}")
704
+ signature = signatures.get(value.callee.binding_id)
705
+ if not signature:
706
+ errors.append(f"call target is not a declared function: {value.callee.binding_id}")
707
+ for argument in value.arguments: expression(argument)
708
+ return None
709
+ if unit.schema_version in _MODULE_AWARE_SCHEMAS and active_source_owner is not None:
710
+ callee_owner = function_ownership.get(value.callee.binding_id, (None, None, None))[0]
711
+ if callee_owner is not None and callee_owner != active_source_owner:
712
+ represented_cross_module_calls.add((active_source_owner, callee_owner))
713
+ expected_return, expected_parameters = signature
714
+ actual = tuple(expression(argument) for argument in value.arguments)
715
+ if len(actual) != len(expected_parameters): errors.append("call argument count does not match prototype")
716
+ elif any(left != right for left, right in zip(actual, expected_parameters)): errors.append("call argument type does not match prototype")
717
+ return expected_return
718
+ errors.append(f"unknown expression node: {type(value).__name__}")
719
+ return None
720
+
721
+ def block(value: CBlock, return_type: CType, loop_depth: int, *, own_scope: bool = True) -> None:
722
+ node(value.node_id)
723
+ if own_scope: push_scope()
724
+ for item in value.statements:
725
+ statement(item, return_type, loop_depth)
726
+ if own_scope: pop_scope()
727
+
728
+ def statement(value: CStatement, return_type: CType, loop_depth: int) -> None:
729
+ node(value.node_id)
730
+ if isinstance(value, CVariableDeclaration):
731
+ initializer_type = expression(value.initializer, initializer=True) if value.initializer else None
732
+ declare_local(value.identifier, value.type_ref)
733
+ local_record = record_definitions.get(value.type_ref.base)
734
+ if local_record is not None:
735
+ record_document = local_record.provenance.source_document_id
736
+ expected_document = active_source_document or record_document
737
+ record_source_document(
738
+ value.provenance,
739
+ f"local record {value.identifier.spelling}",
740
+ expected=expected_document,
741
+ )
742
+ record_source_document(
743
+ value.identifier.provenance,
744
+ f"local record identifier {value.identifier.spelling}",
745
+ expected=expected_document,
746
+ )
747
+ if (
748
+ active_source_document is not None
749
+ and record_document != active_source_document
750
+ ):
751
+ errors.append(
752
+ "local record type belongs to a foreign source document: "
753
+ f"{value.identifier.spelling}"
754
+ )
755
+ expected_initializer_type = CType(
756
+ value.type_ref.base,
757
+ value.type_ref.qualifiers,
758
+ value.type_ref.pointer_depth,
759
+ value.type_ref.array_extents,
760
+ )
761
+ if initializer_type is not None and not initializer_type_matches(expected_initializer_type, initializer_type):
762
+ errors.append(f"initializer type mismatch for {value.identifier.spelling}")
763
+ if value.type_ref.array_extents and not isinstance(value.initializer, CInitializerList):
764
+ errors.append(f"fixed array requires an initializer list: {value.identifier.spelling}")
765
+ elif isinstance(value, CExpressionStatement):
766
+ expression(value.expression)
767
+ elif isinstance(value, CReturnStatement):
768
+ actual = expression(value.expression) if value.expression else None
769
+ if return_type.base == "void" and return_type.pointer_depth == 0:
770
+ if actual is not None: errors.append("void function returns a value")
771
+ elif actual != return_type: errors.append("return expression type does not match function signature")
772
+ elif isinstance(value, CAssignmentStatement):
773
+ target = expression(value.target, assignment_target=True)
774
+ assigned = expression(value.value)
775
+ if target is None or assigned is None or target != assigned: errors.append("assignment types are incompatible")
776
+ if isinstance(value.target, CSubscriptExpr): errors.append("fixed-array element assignment is outside the C IR 0.11 profile")
777
+ elif isinstance(value, CIfStatement):
778
+ if expression(value.condition) != CType("bool"): errors.append("if condition must be bool C IR")
779
+ block(value.then_block, return_type, loop_depth)
780
+ if value.else_block: block(value.else_block, return_type, loop_depth)
781
+ elif isinstance(value, CWhileStatement):
782
+ if expression(value.condition) != CType("bool"): errors.append("while condition must be bool C IR")
783
+ block(value.body, return_type, loop_depth + 1)
784
+ elif isinstance(value, CForStatement):
785
+ push_scope()
786
+ if value.initializer.type_ref.array_extents:
787
+ errors.append("for-loop initializer cannot declare a fixed array")
788
+ statement(value.initializer, return_type, loop_depth)
789
+ if expression(value.condition) != CType("bool"): errors.append("for condition must be bool C IR")
790
+ statement(value.update, return_type, loop_depth)
791
+ block(value.body, return_type, loop_depth + 1)
792
+ pop_scope()
793
+ elif isinstance(value, (CBreakStatement, CContinueStatement)):
794
+ if loop_depth <= 0: errors.append(f"{type(value).__name__} requires an enclosing loop")
795
+ else:
796
+ errors.append(f"unknown statement node: {type(value).__name__}")
797
+
798
+ for declaration in unit.declarations:
799
+ if isinstance(declaration, CFunctionDefinition):
800
+ source_function = declaration.provenance.origin_kind != "support-template"
801
+ active_source_owner = declaration.owner_module_id if source_function else None
802
+ active_source_document = (
803
+ declaration.owner_document_id if source_function else None
804
+ )
805
+ push_scope()
806
+ for parameter in declaration.parameters:
807
+ declare_local(parameter.identifier, parameter.type_ref)
808
+ block(declaration.body, declaration.return_type, 0, own_scope=False)
809
+ pop_scope()
810
+ active_source_owner = None
811
+ active_source_document = None
812
+ elif isinstance(declaration, CVariableDeclaration) and declaration.initializer is not None:
813
+ actual = expression(declaration.initializer, initializer=True)
814
+ expected = CType(
815
+ declaration.type_ref.base,
816
+ declaration.type_ref.qualifiers,
817
+ declaration.type_ref.pointer_depth,
818
+ declaration.type_ref.array_extents,
819
+ )
820
+ if actual is not None and not initializer_type_matches(expected, actual):
821
+ errors.append(f"global initializer type mismatch for {declaration.identifier.spelling}")
822
+
823
+ if unit.schema_version in _MODULE_AWARE_SCHEMAS:
824
+ missing_call_edges = represented_cross_module_calls - set(unit.module_dependencies)
825
+ if missing_call_edges:
826
+ rendered = ", ".join(f"{importer}->{target}" for importer, target in sorted(missing_call_edges))
827
+ errors.append(f"module dependencies omit represented cross-module calls: {rendered}")
828
+
829
+ if used_bases & _STDINT_BASES and ("stdint.h", True) not in headers:
830
+ errors.append("fixed-width integer types require registered system include stdint.h")
831
+ if "bool" in used_bases and ("stdbool.h", True) not in headers:
832
+ errors.append("Boolean types or literals require registered system include stdbool.h")
833
+
834
+ return CIRValidationResult(not errors, tuple(errors))