Skip to content

analysis

Modules:

Classes:

Functions:

Diagnostic dataclass

Diagnostic(
    message: str,
    node: AST | None = None,
    code: str | None = None,
    severity: str = "error",
)

Methods:

format

format() -> str
Source code in src/irx/analysis/diagnostics.py
36
37
38
39
40
41
42
43
44
45
46
47
48
def format(self) -> str:
    """
    title: Format the diagnostic for human display.
    returns:
      type: str
    """
    location = ""
    if self.node is not None:
        loc = getattr(self.node, "loc", None)
        if loc is not None and getattr(loc, "line", -1) >= 0:
            location = f"{loc.line}:{loc.col}: "
    code = f"[{self.code}] " if self.code else ""
    return f"{location}{code}{self.message}"

DiagnosticBag

DiagnosticBag()

Methods:

Source code in src/irx/analysis/diagnostics.py
60
61
62
63
64
def __init__(self) -> None:
    """
    title: Initialize DiagnosticBag.
    """
    self.diagnostics: list[Diagnostic] = []

add

add(
    message: str,
    *,
    node: AST | None = None,
    code: str | None = None,
) -> None
Source code in src/irx/analysis/diagnostics.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def add(
    self,
    message: str,
    *,
    node: astx.AST | None = None,
    code: str | None = None,
) -> None:
    """
    title: Add one error diagnostic.
    parameters:
      message:
        type: str
      node:
        type: astx.AST | None
      code:
        type: str | None
    """
    self.diagnostics.append(
        Diagnostic(message=message, node=node, code=code)
    )

extend

extend(diagnostics: Iterable[Diagnostic]) -> None
Source code in src/irx/analysis/diagnostics.py
87
88
89
90
91
92
93
94
def extend(self, diagnostics: Iterable[Diagnostic]) -> None:
    """
    title: Extend the bag with diagnostics.
    parameters:
      diagnostics:
        type: Iterable[Diagnostic]
    """
    self.diagnostics.extend(diagnostics)

format

format() -> str
Source code in src/irx/analysis/diagnostics.py
104
105
106
107
108
109
110
def format(self) -> str:
    """
    title: Format the whole bag.
    returns:
      type: str
    """
    return "\n".join(diag.format() for diag in self.diagnostics)

has_errors

has_errors() -> bool
Source code in src/irx/analysis/diagnostics.py
 96
 97
 98
 99
100
101
102
def has_errors(self) -> bool:
    """
    title: Return True when any diagnostics exist.
    returns:
      type: bool
    """
    return bool(self.diagnostics)

raise_if_errors

raise_if_errors() -> None
Source code in src/irx/analysis/diagnostics.py
112
113
114
115
116
117
def raise_if_errors(self) -> None:
    """
    title: Raise SemanticError when errors exist.
    """
    if self.has_errors():
        raise SemanticError(self)

ResolvedAssignment dataclass

ResolvedAssignment(target: SemanticSymbol)

ResolvedOperator dataclass

ResolvedOperator(
    op_code: str,
    result_type: DataType | None = None,
    lhs_type: DataType | None = None,
    rhs_type: DataType | None = None,
    flags: SemanticFlags = SemanticFlags(),
)

SemanticError

SemanticError(diagnostics: DiagnosticBag)

Bases: Exception

Source code in src/irx/analysis/diagnostics.py
131
132
133
134
135
136
137
138
139
def __init__(self, diagnostics: DiagnosticBag) -> None:
    """
    title: Initialize SemanticError.
    parameters:
      diagnostics:
        type: DiagnosticBag
    """
    self.diagnostics = diagnostics
    super().__init__(diagnostics.format())

SemanticFlags dataclass

SemanticFlags(
    unsigned: bool = False,
    fast_math: bool = False,
    fma: bool = False,
    fma_rhs: AST | None = None,
)

SemanticFunction dataclass

SemanticFunction(
    symbol_id: str,
    name: str,
    return_type: DataType,
    args: tuple[SemanticSymbol, ...],
    prototype: FunctionPrototype,
    definition: FunctionDef | None = None,
)

SemanticInfo dataclass

SemanticInfo(
    resolved_type: DataType | None = None,
    resolved_symbol: SemanticSymbol | None = None,
    resolved_function: SemanticFunction | None = None,
    resolved_operator: ResolvedOperator | None = None,
    resolved_assignment: ResolvedAssignment | None = None,
    semantic_flags: SemanticFlags = SemanticFlags(),
    extras: dict[str, Any] = dict(),
)

SemanticSymbol dataclass

SemanticSymbol(
    symbol_id: str,
    name: str,
    type_: DataType,
    is_mutable: bool,
    kind: str,
    declaration: AST | None = None,
)

analyze

analyze(node: AST) -> AST
Source code in src/irx/analysis/facade.py
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
@public
def analyze(node: astx.AST) -> astx.AST:
    """
    title: Analyze one AST root and attach node.semantic sidecars.
    parameters:
      node:
        type: astx.AST
    returns:
      type: astx.AST
    """
    return SemanticAnalyzer().analyze(node)

analyze_module

analyze_module(module: Module) -> Module
Source code in src/irx/analysis/facade.py
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
@public
def analyze_module(module: astx.Module) -> astx.Module:
    """
    title: Analyze an AST module.
    parameters:
      module:
        type: astx.Module
    returns:
      type: astx.Module
    """
    return cast(astx.Module, analyze(module))