Skip to content

bindings

Own the per-module namespace that imports and top-level declarations expose, keeping visible-name lookup separate from lexical scope resolution.

Classes:

VisibleBindings

VisibleBindings(
    *,
    context: SemanticContext,
    factory: SemanticEntityFactory,
    bindings: dict[ModuleKey, dict[str, SemanticBinding]]
    | None = None,
)

Track top-level and imported names that are visible within each module namespace and enforce conflicts independently from lexical scopes. attributes: context: type: SemanticContext factory: type: SemanticEntityFactory _bindings: type: dict[ModuleKey, dict[str, SemanticBinding]]

Methods:

Attributes:

Source code in src/irx/analysis/bindings.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def __init__(
    self,
    *,
    context: SemanticContext,
    factory: SemanticEntityFactory,
    bindings: dict[ModuleKey, dict[str, SemanticBinding]] | None = None,
) -> None:
    """
    title: Initialize VisibleBindings.
    parameters:
      context:
        type: SemanticContext
      factory:
        type: SemanticEntityFactory
      bindings:
        type: dict[ModuleKey, dict[str, SemanticBinding]] | None
    """
    self.context = context
    self.factory = factory
    self._bindings = bindings or {}

tables property

tables: dict[ModuleKey, dict[str, SemanticBinding]]

bind

bind(
    local_name: str,
    binding: SemanticBinding,
    *,
    node: AST,
    module_key: ModuleKey | None = None,
) -> SemanticBinding
Source code in src/irx/analysis/bindings.py
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
def bind(
    self,
    local_name: str,
    binding: SemanticBinding,
    *,
    node: astx.AST,
    module_key: ModuleKey | None = None,
) -> SemanticBinding:
    """
    title: Bind one visible name in a module namespace.
    parameters:
      local_name:
        type: str
      binding:
        type: SemanticBinding
      node:
        type: astx.AST
      module_key:
        type: ModuleKey | None
    returns:
      type: SemanticBinding
    """
    bindings = self.bindings_for(module_key)
    existing = bindings.get(local_name)
    if existing is not None and self.binding_conflicts(existing, binding):
        self.context.diagnostics.add(
            f"Conflicting binding for '{local_name}'",
            node=node,
        )
        return binding
    bindings[local_name] = binding
    return binding

bind_class

bind_class(
    local_name: str,
    class_: SemanticClass,
    *,
    node: AST,
    module_key: ModuleKey | None = None,
) -> SemanticBinding
Source code in src/irx/analysis/bindings.py
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
242
243
244
def bind_class(
    self,
    local_name: str,
    class_: SemanticClass,
    *,
    node: astx.AST,
    module_key: ModuleKey | None = None,
) -> SemanticBinding:
    """
    title: Bind a class in a module namespace.
    parameters:
      local_name:
        type: str
      class_:
        type: SemanticClass
      node:
        type: astx.AST
      module_key:
        type: ModuleKey | None
    returns:
      type: SemanticBinding
    """
    return self.bind(
        local_name,
        self.factory.make_class_binding(class_),
        node=node,
        module_key=module_key,
    )

bind_function

bind_function(
    local_name: str,
    function: SemanticFunction,
    *,
    node: AST,
    module_key: ModuleKey | None = None,
) -> SemanticBinding
Source code in src/irx/analysis/bindings.py
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
def bind_function(
    self,
    local_name: str,
    function: SemanticFunction,
    *,
    node: astx.AST,
    module_key: ModuleKey | None = None,
) -> SemanticBinding:
    """
    title: Bind a function in a module namespace.
    parameters:
      local_name:
        type: str
      function:
        type: SemanticFunction
      node:
        type: astx.AST
      module_key:
        type: ModuleKey | None
    returns:
      type: SemanticBinding
    """
    return self.bind(
        local_name,
        self.factory.make_function_binding(function),
        node=node,
        module_key=module_key,
    )

bind_module

bind_module(
    local_name: str,
    module: SemanticModule,
    *,
    node: AST,
    module_key: ModuleKey | None = None,
) -> SemanticBinding
Source code in src/irx/analysis/bindings.py
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
def bind_module(
    self,
    local_name: str,
    module: SemanticModule,
    *,
    node: astx.AST,
    module_key: ModuleKey | None = None,
) -> SemanticBinding:
    """
    title: Bind an imported module in a module namespace.
    parameters:
      local_name:
        type: str
      module:
        type: SemanticModule
      node:
        type: astx.AST
      module_key:
        type: ModuleKey | None
    returns:
      type: SemanticBinding
    """
    return self.bind(
        local_name,
        self.factory.make_module_binding(module),
        node=node,
        module_key=module_key,
    )

bind_struct

bind_struct(
    local_name: str,
    struct: SemanticStruct,
    *,
    node: AST,
    module_key: ModuleKey | None = None,
) -> SemanticBinding
Source code in src/irx/analysis/bindings.py
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
def bind_struct(
    self,
    local_name: str,
    struct: SemanticStruct,
    *,
    node: astx.AST,
    module_key: ModuleKey | None = None,
) -> SemanticBinding:
    """
    title: Bind a struct in a module namespace.
    parameters:
      local_name:
        type: str
      struct:
        type: SemanticStruct
      node:
        type: astx.AST
      module_key:
        type: ModuleKey | None
    returns:
      type: SemanticBinding
    """
    return self.bind(
        local_name,
        self.factory.make_struct_binding(struct),
        node=node,
        module_key=module_key,
    )

binding_conflicts

binding_conflicts(
    existing: SemanticBinding, new_binding: SemanticBinding
) -> bool
Source code in src/irx/analysis/bindings.py
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def binding_conflicts(
    self,
    existing: SemanticBinding,
    new_binding: SemanticBinding,
) -> bool:
    """
    title: Return True when two bindings disagree.
    parameters:
      existing:
        type: SemanticBinding
      new_binding:
        type: SemanticBinding
    returns:
      type: bool
    """
    return (
        existing.kind != new_binding.kind
        or existing.qualified_name != new_binding.qualified_name
    )

bindings_for

bindings_for(
    module_key: ModuleKey | None = None,
) -> dict[str, SemanticBinding]
Source code in src/irx/analysis/bindings.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def bindings_for(
    self,
    module_key: ModuleKey | None = None,
) -> dict[str, SemanticBinding]:
    """
    title: Return one module's visible binding table.
    parameters:
      module_key:
        type: ModuleKey | None
    returns:
      type: dict[str, SemanticBinding]
    """
    current_module_key = self._current_module_key(module_key)
    return self._bindings.setdefault(current_module_key, {})

resolve

resolve(
    name: str, *, module_key: ModuleKey | None = None
) -> SemanticBinding | None
Source code in src/irx/analysis/bindings.py
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
def resolve(
    self,
    name: str,
    *,
    module_key: ModuleKey | None = None,
) -> SemanticBinding | None:
    """
    title: Resolve one visible binding by name.
    parameters:
      name:
        type: str
      module_key:
        type: ModuleKey | None
    returns:
      type: SemanticBinding | None
    """
    current_module_key = self._current_module_key(module_key)
    bindings = self._bindings.get(current_module_key, {})
    return bindings.get(name)