Skip to content

registry

Centralize semantic declaration and top-level registration policy so the analyzer delegates identity and duplicate handling to a smaller subsystem.

Classes:

SemanticRegistry

SemanticRegistry(
    context: SemanticContext, factory: SemanticEntityFactory
)

Register locals, functions, and structs while enforcing the duplicate declaration rules that semantic analysis currently exposes. attributes: context: type: SemanticContext factory: type: SemanticEntityFactory

Methods:

Source code in src/irx/analysis/registry.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def __init__(
    self,
    context: SemanticContext,
    factory: SemanticEntityFactory,
) -> None:
    """
    title: Initialize SemanticRegistry.
    parameters:
      context:
        type: SemanticContext
      factory:
        type: SemanticEntityFactory
    """
    self.context = context
    self.factory = factory

declare_local

declare_local(
    name: str,
    type_: DataType,
    *,
    is_mutable: bool,
    declaration: AST,
    kind: str = "variable",
) -> SemanticSymbol
Source code in src/irx/analysis/registry.py
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
def declare_local(
    self,
    name: str,
    type_: astx.DataType,
    *,
    is_mutable: bool,
    declaration: astx.AST,
    kind: str = "variable",
) -> SemanticSymbol:
    """
    title: Declare one lexical symbol.
    parameters:
      name:
        type: str
      type_:
        type: astx.DataType
      is_mutable:
        type: bool
      declaration:
        type: astx.AST
      kind:
        type: str
    returns:
      type: SemanticSymbol
    """
    symbol = self.factory.make_variable_symbol(
        self._current_module_key(),
        name,
        type_,
        is_mutable=is_mutable,
        declaration=declaration,
        kind=kind,
    )
    current_scope = self.context.scopes.current
    existing = (
        current_scope.symbols.get(name)
        if current_scope is not None
        else None
    )
    if not self.context.scopes.declare(symbol):
        self.context.diagnostics.add(
            f"Identifier already declared: {name}",
            node=declaration,
            code=DiagnosticCodes.SEMANTIC_DUPLICATE_DECLARATION,
            related=(
                ()
                if existing is None or existing.declaration is None
                else (
                    DiagnosticRelatedInformation(
                        "previous declaration is here",
                        node=existing.declaration,
                        module_key=existing.module_key,
                    ),
                )
            ),
        )
    return symbol

normalize_function_signature

normalize_function_signature(
    prototype: FunctionPrototype,
    *,
    definition: FunctionDef | None = None,
    validate_ffi: bool = True,
    validate_main: bool = True,
) -> FunctionSignature
Source code in src/irx/analysis/registry.py
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
def normalize_function_signature(
    self,
    prototype: astx.FunctionPrototype,
    *,
    definition: astx.FunctionDef | None = None,
    validate_ffi: bool = True,
    validate_main: bool = True,
) -> FunctionSignature:
    """
    title: Normalize and validate one semantic function signature.
    parameters:
      prototype:
        type: astx.FunctionPrototype
      definition:
        type: astx.FunctionDef | None
      validate_ffi:
        type: bool
      validate_main:
        type: bool
    returns:
      type: FunctionSignature
    """
    seen_parameter_names: set[str] = set()
    for argument in prototype.args.nodes:
        if argument.name in seen_parameter_names:
            self.context.diagnostics.add(
                f"Function '{prototype.name}' repeats parameter "
                f"'{argument.name}'",
                node=argument,
                code=DiagnosticCodes.SEMANTIC_DUPLICATE_DECLARATION,
            )
            continue
        seen_parameter_names.add(argument.name)

    is_extern = self._prototype_is_extern(prototype)
    is_variadic = self._prototype_is_variadic(prototype)
    symbol_name = self._prototype_symbol_name(prototype)
    required_runtime_features = normalize_runtime_features(
        prototype,
        diagnostics=self.context.diagnostics,
    )
    calling_convention = self._prototype_calling_convention(
        prototype,
        is_extern=is_extern,
    )

    if definition is not None and is_extern:
        self.context.diagnostics.add(
            f"Extern function '{prototype.name}' cannot define a body",
            node=definition,
            code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
        )
    if is_variadic and not is_extern:
        self.context.diagnostics.add(
            f"Function '{prototype.name}' may be variadic only when "
            "declared extern",
            node=prototype,
            code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
        )
    if is_extern and calling_convention is not CallingConvention.C:
        self.context.diagnostics.add(
            f"Extern function '{prototype.name}' must use calling "
            "convention 'c'",
            node=prototype,
            code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
        )
    if (
        not is_extern
        and calling_convention is not CallingConvention.IRX_DEFAULT
    ):
        self.context.diagnostics.add(
            f"IRx-defined function '{prototype.name}' must use calling "
            "convention 'irx_default'",
            node=prototype,
            code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
        )
    if not is_extern and symbol_name != prototype.name:
        self.context.diagnostics.add(
            f"Function '{prototype.name}' may override symbol_name only "
            "for extern declarations",
            node=prototype,
            code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
        )
    if not is_extern and required_runtime_features:
        self.context.diagnostics.add(
            f"Function '{prototype.name}' may declare runtime features "
            "only when declared extern",
            node=prototype,
            code=DiagnosticCodes.RUNTIME_FEATURE_UNKNOWN,
        )

    signature = self.factory.make_function_signature(
        prototype,
        calling_convention=calling_convention,
        is_variadic=is_variadic,
        is_extern=is_extern,
        symbol_name=symbol_name,
        required_runtime_features=required_runtime_features,
    )

    if validate_ffi and signature.is_extern:
        ffi = build_ffi_callable_info(
            self.context,
            signature=signature,
            prototype=prototype,
        )
        if ffi is not None:
            signature = replace(signature, ffi=ffi)

    if validate_main and signature.name == MAIN_FUNCTION_NAME:
        if signature.is_extern:
            self.context.diagnostics.add(
                "Function 'main' cannot be extern",
                node=prototype,
                code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
            )
        if signature.is_variadic:
            self.context.diagnostics.add(
                "Function 'main' must not be variadic",
                node=prototype,
                code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
            )
        if len(signature.parameters) != 0:
            self.context.diagnostics.add(
                "Function 'main' must not declare parameters",
                node=prototype,
                code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
            )
        if (
            signature.calling_convention
            is not CallingConvention.IRX_DEFAULT
        ):
            self.context.diagnostics.add(
                "Function 'main' must use calling convention "
                "'irx_default'",
                node=prototype,
                code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
            )
        if not same_type(signature.return_type, astx.Int32()):
            self.context.diagnostics.add(
                "Function 'main' must return Int32",
                node=prototype,
                code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
            )

    return signature

register_class

register_class(node: ClassDefStmt) -> SemanticClass
Source code in src/irx/analysis/registry.py
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
def register_class(
    self,
    node: astx.ClassDefStmt,
) -> SemanticClass:
    """
    title: Register one top-level class.
    parameters:
      node:
        type: astx.ClassDefStmt
    returns:
      type: SemanticClass
    """
    module_key = self._current_module_key()
    existing = self.context.get_class(module_key, node.name)
    if existing is not None:
        if existing.declaration is not node:
            self.context.diagnostics.add(
                f"Class '{node.name}' already defined.",
                node=node,
                code=DiagnosticCodes.SEMANTIC_DUPLICATE_DECLARATION,
                related=(
                    DiagnosticRelatedInformation(
                        "previous class definition is here",
                        node=existing.declaration,
                        module_key=existing.module_key,
                    ),
                ),
            )
        return existing

    class_ = self.factory.make_class(module_key, node)
    self.context.register_class(class_)
    return class_

register_function

register_function(
    prototype: FunctionPrototype,
    *,
    definition: FunctionDef | None = None,
    validate_ffi: bool = True,
) -> SemanticFunction
Source code in src/irx/analysis/registry.py
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
def register_function(
    self,
    prototype: astx.FunctionPrototype,
    *,
    definition: astx.FunctionDef | None = None,
    validate_ffi: bool = True,
) -> SemanticFunction:
    """
    title: Register one top-level function.
    parameters:
      prototype:
        type: astx.FunctionPrototype
      definition:
        type: astx.FunctionDef | None
      validate_ffi:
        type: bool
    returns:
      type: SemanticFunction
    """
    signature = self.normalize_function_signature(
        prototype,
        definition=definition,
        validate_ffi=validate_ffi,
    )
    module_key = self._current_module_key()
    existing = self.context.get_function(module_key, prototype.name)
    if existing is not None:
        if not self.signatures_match(existing.signature, signature):
            mismatch = self._signature_mismatch_detail(
                existing.signature,
                signature,
                abi_only=False,
            )
            self.context.diagnostics.add(
                f"Conflicting declaration for function "
                f"'{prototype.name}': {mismatch}",
                node=definition or prototype,
                code=DiagnosticCodes.SEMANTIC_DUPLICATE_DECLARATION,
                related=(
                    DiagnosticRelatedInformation(
                        "previous declaration is here",
                        node=existing.definition or existing.prototype,
                        module_key=existing.module_key,
                    ),
                ),
            )
            return existing
        if definition is not None and existing.definition is not None:
            self.context.diagnostics.add(
                f"Function '{prototype.name}' already defined",
                node=definition,
                code=DiagnosticCodes.SEMANTIC_DUPLICATE_DECLARATION,
                related=(
                    DiagnosticRelatedInformation(
                        "previous definition is here",
                        node=existing.definition,
                        module_key=existing.module_key,
                    ),
                ),
            )
        if definition is not None and existing.definition is None:
            updated = replace(existing, definition=definition)
            self.context.register_function(updated)
            return updated
        return existing

    if signature.is_extern:
        for candidate in self.context.functions.values():
            if candidate.module_key != module_key:
                continue
            candidate_signature = candidate.signature
            if not candidate_signature.is_extern:
                continue
            if candidate_signature.symbol_name != signature.symbol_name:
                continue
            if candidate_signature.name == prototype.name:
                continue
            if self.signatures_match(candidate_signature, signature):
                continue
            mismatch = self._signature_mismatch_detail(
                candidate_signature,
                signature,
                abi_only=True,
            )
            self.context.diagnostics.add(
                f"Extern symbol '{signature.symbol_name}' is declared "
                f"incompatibly by '{candidate.name}' and "
                f"'{prototype.name}': {mismatch}",
                node=definition or prototype,
                code=DiagnosticCodes.FFI_INVALID_SIGNATURE,
                related=(
                    DiagnosticRelatedInformation(
                        "previous extern declaration is here",
                        node=candidate.definition or candidate.prototype,
                        module_key=candidate.module_key,
                    ),
                ),
            )
            break

    function = self.factory.make_function(
        module_key,
        prototype,
        signature=signature,
        definition=definition,
    )
    self.context.register_function(function)
    return function

register_struct

register_struct(node: StructDefStmt) -> SemanticStruct
Source code in src/irx/analysis/registry.py
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
def register_struct(
    self,
    node: astx.StructDefStmt,
) -> SemanticStruct:
    """
    title: Register one top-level struct.
    parameters:
      node:
        type: astx.StructDefStmt
    returns:
      type: SemanticStruct
    """
    module_key = self._current_module_key()
    existing = self.context.get_struct(module_key, node.name)
    if existing is not None:
        if existing.declaration is not node:
            self.context.diagnostics.add(
                f"Struct '{node.name}' already defined.",
                node=node,
                code=DiagnosticCodes.SEMANTIC_DUPLICATE_DECLARATION,
                related=(
                    DiagnosticRelatedInformation(
                        "previous struct definition is here",
                        node=existing.declaration,
                        module_key=existing.module_key,
                    ),
                ),
            )
        return existing

    struct = self.factory.make_struct(module_key, node)
    self.context.register_struct(struct)
    return struct

resolve_class

resolve_class(
    name: str, *, module_key: ModuleKey | None = None
) -> SemanticClass | None
Source code in src/irx/analysis/registry.py
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
def resolve_class(
    self,
    name: str,
    *,
    module_key: ModuleKey | None = None,
) -> SemanticClass | None:
    """
    title: Resolve one registered class.
    parameters:
      name:
        type: str
      module_key:
        type: ModuleKey | None
    returns:
      type: SemanticClass | None
    """
    lookup_module_key = module_key or self._current_module_key()
    return self.context.get_class(lookup_module_key, name)

resolve_function

resolve_function(
    name: str, *, module_key: ModuleKey | None = None
) -> SemanticFunction | None
Source code in src/irx/analysis/registry.py
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
def resolve_function(
    self,
    name: str,
    *,
    module_key: ModuleKey | None = None,
) -> SemanticFunction | None:
    """
    title: Resolve one registered function.
    parameters:
      name:
        type: str
      module_key:
        type: ModuleKey | None
    returns:
      type: SemanticFunction | None
    """
    lookup_module_key = module_key or self._current_module_key()
    return self.context.get_function(lookup_module_key, name)

resolve_struct

resolve_struct(
    name: str, *, module_key: ModuleKey | None = None
) -> SemanticStruct | None
Source code in src/irx/analysis/registry.py
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
def resolve_struct(
    self,
    name: str,
    *,
    module_key: ModuleKey | None = None,
) -> SemanticStruct | None:
    """
    title: Resolve one registered struct.
    parameters:
      name:
        type: str
      module_key:
        type: ModuleKey | None
    returns:
      type: SemanticStruct | None
    """
    lookup_module_key = module_key or self._current_module_key()
    return self.context.get_struct(lookup_module_key, name)

signatures_match

signatures_match(
    lhs: FunctionSignature, rhs: FunctionSignature
) -> bool
Source code in src/irx/analysis/registry.py
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
def signatures_match(
    self,
    lhs: FunctionSignature,
    rhs: FunctionSignature,
) -> bool:
    """
    title: Return whether two canonical function signatures match.
    parameters:
      lhs:
        type: FunctionSignature
      rhs:
        type: FunctionSignature
    returns:
      type: bool
    """
    if lhs.name != rhs.name:
        return False
    if lhs.calling_convention is not rhs.calling_convention:
        return False
    if lhs.is_variadic != rhs.is_variadic:
        return False
    if lhs.is_extern != rhs.is_extern:
        return False
    if lhs.symbol_name != rhs.symbol_name:
        return False
    if lhs.required_runtime_features != rhs.required_runtime_features:
        return False
    if not same_type(lhs.return_type, rhs.return_type):
        return False
    if len(lhs.parameters) != len(rhs.parameters):
        return False
    return all(
        self._same_parameter_spec(lhs_param, rhs_param)
        for lhs_param, rhs_param in zip(lhs.parameters, rhs.parameters)
    )