Skip to content

registry

Classes:

Functions:

RuntimeFeatureRegistry

RuntimeFeatureRegistry()

Methods:

Source code in src/irx/builder/runtime/registry.py
37
38
39
40
41
def __init__(self) -> None:
    """
    title: Initialize RuntimeFeatureRegistry.
    """
    self._features: dict[str, RuntimeFeature] = {}

get

get(name: str) -> RuntimeFeature
Source code in src/irx/builder/runtime/registry.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def get(self, name: str) -> RuntimeFeature:
    """
    title: Return a runtime feature by name.
    parameters:
      name:
        type: str
    returns:
      type: RuntimeFeature
    """
    try:
        return self._features[name]
    except KeyError as exc:
        available = self.names()
        raise RuntimeFeatureError(
            Diagnostic(
                message=f"runtime feature '{name}' is not registered",
                code=DiagnosticCodes.RUNTIME_FEATURE_UNKNOWN,
                phase="runtime",
                notes=(
                    f"available runtime features: {', '.join(available)}",
                )
                if available
                else (),
                cause=KeyError(name),
            )
        ) from exc

names

names() -> tuple[str, ...]
Source code in src/irx/builder/runtime/registry.py
83
84
85
86
87
88
89
def names(self) -> tuple[str, ...]:
    """
    title: Return the registered runtime feature names.
    returns:
      type: tuple[str, Ellipsis]
    """
    return tuple(sorted(self._features))

register

register(feature: RuntimeFeature) -> None
Source code in src/irx/builder/runtime/registry.py
43
44
45
46
47
48
49
50
51
52
53
54
def register(self, feature: RuntimeFeature) -> None:
    """
    title: Register a runtime feature by name.
    parameters:
      feature:
        type: RuntimeFeature
    """
    if feature.name in self._features:
        raise ValueError(
            f"Runtime feature '{feature.name}' already exists"
        )
    self._features[feature.name] = feature

RuntimeFeatureState

RuntimeFeatureState(
    owner: "VisitorProtocol",
    registry: RuntimeFeatureRegistry,
    active_features: Iterable[str] | None = None,
)

Methods:

Source code in src/irx/builder/runtime/registry.py
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
def __init__(
    self,
    owner: "VisitorProtocol",
    registry: RuntimeFeatureRegistry,
    active_features: Iterable[str] | None = None,
) -> None:
    """
    title: Initialize RuntimeFeatureState.
    parameters:
      owner:
        type: VisitorProtocol
      registry:
        type: RuntimeFeatureRegistry
      active_features:
        type: Iterable[str] | None
    """
    self._owner = owner
    self._registry = registry
    self._active_features: set[str] = set()
    self._declared_symbols: dict[tuple[str, str], ir.Function] = {}

    if active_features is None:
        return

    for feature_name in active_features:
        self.activate(feature_name)

activate

activate(feature_name: str) -> None
Source code in src/irx/builder/runtime/registry.py
139
140
141
142
143
144
145
146
147
def activate(self, feature_name: str) -> None:
    """
    title: Activate a runtime feature for the current module.
    parameters:
      feature_name:
        type: str
    """
    self._registry.get(feature_name)
    self._active_features.add(feature_name)

active_feature_names

active_feature_names() -> tuple[str, ...]
Source code in src/irx/builder/runtime/registry.py
160
161
162
163
164
165
166
def active_feature_names(self) -> tuple[str, ...]:
    """
    title: Return the active feature names.
    returns:
      type: tuple[str, Ellipsis]
    """
    return tuple(sorted(self._active_features))

feature

feature(feature_name: str) -> RuntimeFeature
Source code in src/irx/builder/runtime/registry.py
168
169
170
171
172
173
174
175
176
177
def feature(self, feature_name: str) -> RuntimeFeature:
    """
    title: Return a registered feature by name.
    parameters:
      feature_name:
        type: str
    returns:
      type: RuntimeFeature
    """
    return self._registry.get(feature_name)

feature_declares_symbol

feature_declares_symbol(
    feature_name: str, symbol_name: str
) -> bool
Source code in src/irx/builder/runtime/registry.py
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
def feature_declares_symbol(
    self,
    feature_name: str,
    symbol_name: str,
) -> bool:
    """
    title: Return whether one feature declares one symbol.
    parameters:
      feature_name:
        type: str
      symbol_name:
        type: str
    returns:
      type: bool
    """
    return symbol_name in self.feature(feature_name).symbols

is_active

is_active(feature_name: str) -> bool
Source code in src/irx/builder/runtime/registry.py
149
150
151
152
153
154
155
156
157
158
def is_active(self, feature_name: str) -> bool:
    """
    title: Return whether a feature is active for the current module.
    parameters:
      feature_name:
        type: str
    returns:
      type: bool
    """
    return feature_name in self._active_features

linker_flags

linker_flags() -> tuple[str, ...]
Source code in src/irx/builder/runtime/registry.py
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
def linker_flags(self) -> tuple[str, ...]:
    """
    title: Return the linker flags required by active features.
    returns:
      type: tuple[str, Ellipsis]
    """
    flags: list[str] = []
    seen: set[str] = set()

    for feature_name in self.active_feature_names():
        feature = self.feature(feature_name)
        for flag in feature.linker_flags:
            if flag in seen:
                continue
            seen.add(flag)
            flags.append(flag)

    return tuple(flags)

native_artifacts

native_artifacts() -> tuple[NativeArtifact, ...]
Source code in src/irx/builder/runtime/registry.py
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
def native_artifacts(self) -> tuple[NativeArtifact, ...]:
    """
    title: Return the native artifacts required by active features.
    returns:
      type: tuple[NativeArtifact, Ellipsis]
    """
    artifacts: list[NativeArtifact] = []
    seen: set[tuple[str, str]] = set()

    for feature_name in self.active_feature_names():
        feature = self.feature(feature_name)
        for artifact in feature.artifacts:
            dedupe_key = (artifact.kind, str(artifact.path))
            if dedupe_key in seen:
                continue
            seen.add(dedupe_key)
            artifacts.append(artifact)

    return tuple(artifacts)

require_symbol

require_symbol(
    feature_name: str, symbol_name: str
) -> Function
Source code in src/irx/builder/runtime/registry.py
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
def require_symbol(
    self,
    feature_name: str,
    symbol_name: str,
) -> ir.Function:
    """
    title: Activate a feature and declare one of its external symbols.
    parameters:
      feature_name:
        type: str
      symbol_name:
        type: str
    returns:
      type: ir.Function
    """
    self.activate(feature_name)
    cache_key = (feature_name, symbol_name)
    cached = self._declared_symbols.get(cache_key)
    if cached is not None:
        return cached

    feature = self.feature(feature_name)
    try:
        symbol_spec = feature.symbols[symbol_name]
    except KeyError as exc:
        raise RuntimeFeatureError(
            Diagnostic(
                message=(
                    f"runtime feature '{feature_name}' does not declare "
                    f"symbol '{symbol_name}'"
                ),
                code=DiagnosticCodes.RUNTIME_FEATURE_SYMBOL_MISSING,
                phase="runtime",
                notes=(
                    "declared symbols: "
                    f"{', '.join(sorted(feature.symbols))}",
                )
                if feature.symbols
                else (),
                cause=KeyError(symbol_name),
            )
        ) from exc

    declared = symbol_spec.declare(self._owner)
    self._declared_symbols[cache_key] = declared
    return declared

get_default_runtime_feature_registry cached

get_default_runtime_feature_registry() -> (
    RuntimeFeatureRegistry
)
Source code in src/irx/builder/runtime/registry.py
283
284
285
286
287
288
289
290
291
292
293
294
295
296
@lru_cache(maxsize=1)
@typechecked
def get_default_runtime_feature_registry() -> RuntimeFeatureRegistry:
    """
    title: Build the default runtime feature registry.
    returns:
      type: RuntimeFeatureRegistry
    """
    registry = RuntimeFeatureRegistry()
    registry.register(build_libc_runtime_feature())
    registry.register(build_libm_runtime_feature())
    registry.register(build_buffer_runtime_feature())
    registry.register(build_arrow_runtime_feature())
    return registry