Skip to content

factories

Build semantic sidecar entities and visible binding wrappers in one place so analyzer visitors can focus on traversal and rule orchestration.

Classes:

SemanticEntityFactory

SemanticEntityFactory(context: SemanticContext)

Create semantic symbols, functions, structs, modules, and visible bindings with consistent ids and qualified names. attributes: context: type: SemanticContext

Methods:

Source code in packages/irx/src/irx/analysis/factories.py
76
77
78
79
80
81
82
83
def __init__(self, context: SemanticContext) -> None:
    """
    title: Initialize SemanticEntityFactory.
    parameters:
      context:
        type: SemanticContext
    """
    self.context = context

make_callable_resolution

make_callable_resolution(
    function: SemanticFunction,
) -> CallableResolution
Source code in packages/irx/src/irx/analysis/factories.py
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
def make_callable_resolution(
    self,
    function: SemanticFunction,
) -> CallableResolution:
    """
    title: Create one resolved callable wrapper.
    parameters:
      function:
        type: SemanticFunction
    returns:
      type: CallableResolution
    """
    return CallableResolution(
        function=function,
        signature=function.signature,
    )

make_class

make_class(
    module_key: ModuleKey, node: ClassDefStmt
) -> SemanticClass
Source code in packages/irx/src/irx/analysis/factories.py
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
def make_class(
    self,
    module_key: ModuleKey,
    node: astx.ClassDefStmt,
) -> SemanticClass:
    """
    title: Create one semantic class entity.
    parameters:
      module_key:
        type: ModuleKey
      node:
        type: astx.ClassDefStmt
    returns:
      type: SemanticClass
    """
    return SemanticClass(
        symbol_id=self.context.next_symbol_id("class"),
        name=node.name,
        module_key=module_key,
        qualified_name=qualified_class_name(module_key, node.name),
        declaration=node,
        is_abstract=bool(getattr(node, "is_abstract", False)),
    )

make_class_binding

make_class_binding(
    class_: SemanticClass,
) -> SemanticBinding
Source code in packages/irx/src/irx/analysis/factories.py
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
def make_class_binding(
    self,
    class_: SemanticClass,
) -> SemanticBinding:
    """
    title: Create a visible binding for a class.
    parameters:
      class_:
        type: SemanticClass
    returns:
      type: SemanticBinding
    """
    return SemanticBinding(
        kind="class",
        module_key=class_.module_key,
        qualified_name=class_.qualified_name,
        class_=class_,
    )

make_class_member

make_class_member(
    class_: SemanticClass,
    *,
    name: str,
    kind: ClassMemberKind,
    declaration: AST,
    visibility: VisibilityKind,
    is_static: bool,
    is_constant: bool,
    is_mutable: bool,
    is_abstract: bool = False,
    type_: DataType | None = None,
    signature: FunctionSignature | None = None,
    signature_key: str | None = None,
    overrides: str | None = None,
    dispatch_slot: int | None = None,
    lowered_function: SemanticFunction | None = None,
) -> SemanticClassMember
Source code in packages/irx/src/irx/analysis/factories.py
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
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
def make_class_member(
    self,
    class_: SemanticClass,
    *,
    name: str,
    kind: ClassMemberKind,
    declaration: astx.AST,
    visibility: astx.VisibilityKind,
    is_static: bool,
    is_constant: bool,
    is_mutable: bool,
    is_abstract: bool = False,
    type_: astx.DataType | None = None,
    signature: FunctionSignature | None = None,
    signature_key: str | None = None,
    overrides: str | None = None,
    dispatch_slot: int | None = None,
    lowered_function: SemanticFunction | None = None,
) -> SemanticClassMember:
    """
    title: Create one semantic class-member record.
    parameters:
      class_:
        type: SemanticClass
      name:
        type: str
      kind:
        type: ClassMemberKind
      declaration:
        type: astx.AST
      visibility:
        type: astx.VisibilityKind
      is_static:
        type: bool
      is_constant:
        type: bool
      is_mutable:
        type: bool
      is_abstract:
        type: bool
      type_:
        type: astx.DataType | None
      signature:
        type: FunctionSignature | None
      signature_key:
        type: str | None
      overrides:
        type: str | None
      dispatch_slot:
        type: int | None
      lowered_function:
        type: SemanticFunction | None
    returns:
      type: SemanticClassMember
    """
    prefix = (
        "class_attr"
        if kind is ClassMemberKind.ATTRIBUTE
        else "class_method"
    )
    return SemanticClassMember(
        symbol_id=self.context.next_symbol_id(prefix),
        name=name,
        qualified_name=qualified_class_member_name(
            class_.module_key,
            class_.name,
            name,
            signature_key if kind is ClassMemberKind.METHOD else None,
        ),
        owner_name=class_.name,
        owner_qualified_name=class_.qualified_name,
        kind=kind,
        visibility=visibility,
        is_static=is_static,
        is_abstract=is_abstract,
        is_constant=is_constant,
        is_mutable=is_mutable,
        declaration=declaration,
        type_=clone_type(type_) if type_ is not None else None,
        signature=signature,
        signature_key=signature_key,
        overrides=overrides,
        dispatch_slot=dispatch_slot,
        lowered_function=lowered_function,
    )

make_function

make_function(
    module_key: ModuleKey,
    prototype: FunctionPrototype,
    *,
    signature: FunctionSignature,
    definition: FunctionDef | None = None,
    args: tuple[SemanticSymbol, ...] | None = None,
) -> SemanticFunction
Source code in packages/irx/src/irx/analysis/factories.py
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
def make_function(
    self,
    module_key: ModuleKey,
    prototype: astx.FunctionPrototype,
    *,
    signature: FunctionSignature,
    definition: astx.FunctionDef | None = None,
    args: tuple[SemanticSymbol, ...] | None = None,
) -> SemanticFunction:
    """
    title: Create one semantic function entity.
    parameters:
      module_key:
        type: ModuleKey
      prototype:
        type: astx.FunctionPrototype
      signature:
        type: FunctionSignature
      definition:
        type: astx.FunctionDef | None
      args:
        type: tuple[SemanticSymbol, Ellipsis] | None
    returns:
      type: SemanticFunction
    """
    semantic_args = args
    if semantic_args is None:
        semantic_args = tuple(
            self.make_parameter_symbol(module_key, argument)
            for argument in prototype.args.nodes
        )
    return SemanticFunction(
        symbol_id=self.context.next_symbol_id("fn"),
        name=prototype.name,
        return_type=clone_type(signature.return_type),
        args=semantic_args,
        signature=signature,
        prototype=prototype,
        definition=definition,
        module_key=module_key,
        qualified_name=qualified_function_name(module_key, prototype.name),
        template_params=tuple(
            astx.TemplateParam(
                param.name,
                clone_type(param.bound),
                param.loc,
            )
            for param in astx.get_template_params(prototype)
        ),
    )

make_function_binding

make_function_binding(
    function: SemanticFunction,
) -> SemanticBinding
Source code in packages/irx/src/irx/analysis/factories.py
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
def make_function_binding(
    self,
    function: SemanticFunction,
) -> SemanticBinding:
    """
    title: Create a visible binding for a function.
    parameters:
      function:
        type: SemanticFunction
    returns:
      type: SemanticBinding
    """
    return SemanticBinding(
        kind="function",
        module_key=function.module_key,
        qualified_name=function.qualified_name,
        function=function,
    )

make_function_signature

make_function_signature(
    prototype: FunctionPrototype,
    *,
    calling_convention: CallingConvention,
    is_variadic: bool,
    is_extern: bool,
    symbol_name: str,
    required_runtime_features: tuple[str, ...] = (),
    ffi: FFICallableInfo | None = None,
) -> FunctionSignature
Source code in packages/irx/src/irx/analysis/factories.py
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
def make_function_signature(
    self,
    prototype: astx.FunctionPrototype,
    *,
    calling_convention: CallingConvention,
    is_variadic: bool,
    is_extern: bool,
    symbol_name: str,
    required_runtime_features: tuple[str, ...] = (),
    ffi: FFICallableInfo | None = None,
) -> FunctionSignature:
    """
    title: Create one canonical semantic function signature.
    parameters:
      prototype:
        type: astx.FunctionPrototype
      calling_convention:
        type: CallingConvention
      is_variadic:
        type: bool
      is_extern:
        type: bool
      symbol_name:
        type: str
      required_runtime_features:
        type: tuple[str, Ellipsis]
      ffi:
        type: FFICallableInfo | None
    returns:
      type: FunctionSignature
    """
    return FunctionSignature(
        name=prototype.name,
        parameters=tuple(
            self.make_parameter_spec(argument)
            for argument in prototype.args.nodes
        ),
        return_type=clone_type(prototype.return_type),
        calling_convention=calling_convention,
        is_variadic=is_variadic,
        is_extern=is_extern,
        symbol_name=symbol_name,
        required_runtime_features=required_runtime_features,
        ffi=ffi,
    )

make_import_binding

make_import_binding(
    *,
    local_name: str,
    requested_name: str,
    source_module_key: ModuleKey,
    binding: SemanticBinding,
) -> ResolvedImportBinding
Source code in packages/irx/src/irx/analysis/factories.py
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
def make_import_binding(
    self,
    *,
    local_name: str,
    requested_name: str,
    source_module_key: ModuleKey,
    binding: SemanticBinding,
) -> ResolvedImportBinding:
    """
    title: Create one resolved import binding record.
    parameters:
      local_name:
        type: str
      requested_name:
        type: str
      source_module_key:
        type: ModuleKey
      binding:
        type: SemanticBinding
    returns:
      type: ResolvedImportBinding
    """
    return ResolvedImportBinding(
        local_name=local_name,
        requested_name=requested_name,
        source_module_key=source_module_key,
        binding=binding,
    )

make_module

make_module(
    module_key: ModuleKey,
    *,
    display_name: str | None = None,
) -> SemanticModule
Source code in packages/irx/src/irx/analysis/factories.py
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
def make_module(
    self,
    module_key: ModuleKey,
    *,
    display_name: str | None = None,
) -> SemanticModule:
    """
    title: Create one semantic module entity.
    parameters:
      module_key:
        type: ModuleKey
      display_name:
        type: str | None
    returns:
      type: SemanticModule
    """
    return SemanticModule(
        module_key=module_key,
        display_name=display_name,
    )

make_module_binding

make_module_binding(
    module: SemanticModule,
) -> SemanticBinding
Source code in packages/irx/src/irx/analysis/factories.py
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
def make_module_binding(
    self,
    module: SemanticModule,
) -> SemanticBinding:
    """
    title: Create a visible binding for a module.
    parameters:
      module:
        type: SemanticModule
    returns:
      type: SemanticBinding
    """
    return SemanticBinding(
        kind="module",
        module_key=module.module_key,
        qualified_name=str(module.module_key),
        module=module,
    )

make_parameter_spec

make_parameter_spec(argument: Argument) -> ParameterSpec
Source code in packages/irx/src/irx/analysis/factories.py
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
def make_parameter_spec(
    self,
    argument: astx.Argument,
) -> ParameterSpec:
    """
    title: Create one canonical semantic parameter specification.
    parameters:
      argument:
        type: astx.Argument
    returns:
      type: ParameterSpec
    """
    return ParameterSpec(
        name=argument.name,
        type_=clone_type(argument.type_),
        passing_kind=ParameterPassingKind.BY_VALUE,
        metadata=self._parameter_default_metadata(argument),
    )

make_parameter_symbol

make_parameter_symbol(
    module_key: ModuleKey, argument: Argument
) -> SemanticSymbol
Source code in packages/irx/src/irx/analysis/factories.py
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
def make_parameter_symbol(
    self,
    module_key: ModuleKey,
    argument: astx.Argument,
) -> SemanticSymbol:
    """
    title: Create one function-parameter semantic symbol.
    parameters:
      module_key:
        type: ModuleKey
      argument:
        type: astx.Argument
    returns:
      type: SemanticSymbol
    """
    return self.make_variable_symbol(
        module_key,
        argument.name,
        argument.type_,
        is_mutable=True,
        declaration=argument,
        kind="argument",
    )

make_struct

make_struct(
    module_key: ModuleKey, node: StructDefStmt
) -> SemanticStruct
Source code in packages/irx/src/irx/analysis/factories.py
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
def make_struct(
    self,
    module_key: ModuleKey,
    node: astx.StructDefStmt,
) -> SemanticStruct:
    """
    title: Create one semantic struct entity.
    parameters:
      module_key:
        type: ModuleKey
      node:
        type: astx.StructDefStmt
    returns:
      type: SemanticStruct
    """
    return SemanticStruct(
        symbol_id=self.context.next_symbol_id("struct"),
        name=node.name,
        module_key=module_key,
        qualified_name=qualified_struct_name(module_key, node.name),
        declaration=node,
    )

make_struct_binding

make_struct_binding(
    struct: SemanticStruct,
) -> SemanticBinding
Source code in packages/irx/src/irx/analysis/factories.py
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
def make_struct_binding(
    self,
    struct: SemanticStruct,
) -> SemanticBinding:
    """
    title: Create a visible binding for a struct.
    parameters:
      struct:
        type: SemanticStruct
    returns:
      type: SemanticBinding
    """
    return SemanticBinding(
        kind="struct",
        module_key=struct.module_key,
        qualified_name=struct.qualified_name,
        struct=struct,
    )

make_variable_symbol

make_variable_symbol(
    module_key: ModuleKey,
    name: str,
    type_: DataType,
    *,
    is_mutable: bool,
    declaration: AST | None,
    kind: str = "variable",
) -> SemanticSymbol
Source code in packages/irx/src/irx/analysis/factories.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
def make_variable_symbol(
    self,
    module_key: ModuleKey,
    name: str,
    type_: astx.DataType,
    *,
    is_mutable: bool,
    declaration: astx.AST | None,
    kind: str = "variable",
) -> SemanticSymbol:
    """
    title: Create a variable-like semantic symbol.
    parameters:
      module_key:
        type: ModuleKey
      name:
        type: str
      type_:
        type: astx.DataType
      is_mutable:
        type: bool
      declaration:
        type: astx.AST | None
      kind:
        type: str
    returns:
      type: SemanticSymbol
    """
    return variable_symbol(
        self.context.next_symbol_id(kind),
        module_key,
        name,
        clone_type(type_),
        is_mutable=is_mutable,
        declaration=declaration,
        kind=kind,
    )