Debian Codegraph @

mypyc/irbuild/context.py

raw
1 """Helpers that store information about functions and the related classes."""
2
3 from __future__ import annotations
4
5 from mypy.nodes import FuncItem
6 from mypyc.ir.class_ir import ClassIR
7 from mypyc.ir.func_ir import INVALID_FUNC_DEF
8 from mypyc.ir.ops import BasicBlock, Value
9 from mypyc.irbuild.targets import AssignmentTarget
10
11
12 class FuncInfo:
13 """Contains information about functions as they are generated."""
14
15 def __init__(
16 self,
17 fitem: FuncItem = INVALID_FUNC_DEF,
18 name: str = "",
19 class_name: str | None = None,
20 namespace: str = "",
21 is_nested: bool = False,
22 contains_nested: bool = False,
23 is_decorated: bool = False,
24 in_non_ext: bool = False,
25 add_nested_funcs_to_env: bool = False,
26 is_comprehension_scope: bool = False,
27 ) -> None:
28 self.fitem = fitem
29 self.name = name
30 self.class_name = class_name
31 self.ns = namespace
32 # Callable classes implement the '__call__' method, and are used to represent functions
33 # that are nested inside of other functions.
34 self._callable_class: ImplicitClass | None = None
35 # Environment classes are ClassIR instances that contain attributes representing the
36 # variables in the environment of the function they correspond to. Environment classes are
37 # generated for functions that contain nested functions.
38 self._env_class: ClassIR | None = None
39 # Generator classes implement the '__next__' method, and are used to represent generators
40 # returned by generator functions.
41 self._generator_class: GeneratorClass | None = None
42 # Environment class registers are the local registers associated with instances of an
43 # environment class, used for getting and setting attributes. curr_env_reg is the register
44 # associated with the current environment.
45 self._curr_env_reg: Value | None = None
46 # These are flags denoting whether a given function is nested, contains a nested function,
47 # is decorated, or is within a non-extension class.
48 self.is_nested = is_nested
49 self.contains_nested = contains_nested
50 self.is_decorated = is_decorated
51 self.in_non_ext = in_non_ext
52 self.add_nested_funcs_to_env = add_nested_funcs_to_env
53 # Comprehension scopes are lightweight scope boundaries created when
54 # a comprehension body contains a lambda. The comprehension is still
55 # inlined (same basic blocks), but we push a new FuncInfo so the
56 # closure machinery can capture loop variables through env classes.
57 self.is_comprehension_scope = is_comprehension_scope
58
59 # TODO: add field for ret_type: RType = none_rprimitive
60
61 def namespaced_name(self) -> str:
62 return "_".join(x for x in [self.name, self.class_name, self.ns] if x)
63
64 @property
65 def is_generator(self) -> bool:
66 return self.fitem.is_generator or self.fitem.is_coroutine
67
68 @property
69 def is_coroutine(self) -> bool:
70 return self.fitem.is_coroutine
71
72 @property
73 def callable_class(self) -> ImplicitClass:
74 assert self._callable_class is not None
75 return self._callable_class
76
77 @callable_class.setter
78 def callable_class(self, cls: ImplicitClass) -> None:
79 self._callable_class = cls
80
81 @property
82 def env_class(self) -> ClassIR:
83 assert self._env_class is not None
84 return self._env_class
85
86 @env_class.setter
87 def env_class(self, ir: ClassIR) -> None:
88 self._env_class = ir
89
90 @property
91 def generator_class(self) -> GeneratorClass:
92 assert self._generator_class is not None
93 return self._generator_class
94
95 @generator_class.setter
96 def generator_class(self, cls: GeneratorClass) -> None:
97 self._generator_class = cls
98
99 @property
100 def curr_env_reg(self) -> Value:
101 assert self._curr_env_reg is not None
102 return self._curr_env_reg
103
104 def can_merge_generator_and_env_classes(self) -> bool:
105 # In simple cases we can place the environment into the generator class,
106 # instead of having two separate classes.
107 if self._generator_class and not self._generator_class.ir.is_final_class:
108 result = False
109 else:
110 result = self.is_generator and not self.is_nested and not self.contains_nested
111 return result
112
113
114 class ImplicitClass:
115 """Contains information regarding implicitly generated classes.
116
117 Implicit classes are generated for nested functions and generator
118 functions. They are not explicitly defined in the source code.
119
120 NOTE: This is both a concrete class and used as a base class.
121 """
122
123 def __init__(self, ir: ClassIR) -> None:
124 # The ClassIR instance associated with this class.
125 self.ir = ir
126 # The register associated with the 'self' instance for this generator class.
127 self._self_reg: Value | None = None
128 # Environment class registers are the local registers associated with instances of an
129 # environment class, used for getting and setting attributes. curr_env_reg is the register
130 # associated with the current environment. prev_env_reg is the self.__mypyc_env__ field
131 # associated with the previous environment.
132 self._curr_env_reg: Value | None = None
133 self._prev_env_reg: Value | None = None
134
135 @property
136 def self_reg(self) -> Value:
137 assert self._self_reg is not None
138 return self._self_reg
139
140 @self_reg.setter
141 def self_reg(self, reg: Value) -> None:
142 self._self_reg = reg
143
144 @property
145 def curr_env_reg(self) -> Value:
146 assert self._curr_env_reg is not None
147 return self._curr_env_reg
148
149 @curr_env_reg.setter
150 def curr_env_reg(self, reg: Value) -> None:
151 self._curr_env_reg = reg
152
153 @property
154 def prev_env_reg(self) -> Value:
155 assert self._prev_env_reg is not None
156 return self._prev_env_reg
157
158 @prev_env_reg.setter
159 def prev_env_reg(self, reg: Value) -> None:
160 self._prev_env_reg = reg
161
162
163 class GeneratorClass(ImplicitClass):
164 """Contains information about implicit generator function classes."""
165
166 def __init__(self, ir: ClassIR) -> None:
167 super().__init__(ir)
168 # This register holds the label number that the '__next__' function should go to the next
169 # time it is called.
170 self._next_label_reg: Value | None = None
171 self._next_label_target: AssignmentTarget | None = None
172
173 # These registers hold the error values for the generator object for the case that the
174 # 'throw' function is called.
175 self.exc_regs: tuple[Value, Value, Value] | None = None
176
177 # Holds the arg passed to send
178 self.send_arg_reg: Value | None = None
179
180 # Holds the PyObject ** pointer through which return value can be passed
181 # instead of raising StopIteration(ret_value) (only if not NULL). This
182 # is used for faster native-to-native calls.
183 self.stop_iter_value_reg: Value | None = None
184
185 # The switch block is used to decide which instruction to go using the value held in the
186 # next-label register.
187 self.switch_block = BasicBlock()
188 self.continuation_blocks: list[BasicBlock] = []
189
190 @property
191 def next_label_reg(self) -> Value:
192 assert self._next_label_reg is not None
193 return self._next_label_reg
194
195 @next_label_reg.setter
196 def next_label_reg(self, reg: Value) -> None:
197 self._next_label_reg = reg
198
199 @property
200 def next_label_target(self) -> AssignmentTarget:
201 assert self._next_label_target is not None
202 return self._next_label_target
203
204 @next_label_target.setter
205 def next_label_target(self, target: AssignmentTarget) -> None:
206 self._next_label_target = target