Generate a class representing a function environment. Note that the variables in the function environment are not actually populated here. This is because when the environment class is generated, the function environment has not yet been visited. This behavior is allowed so that whe
(builder: IRBuilder)
| 34 | |
| 35 | |
| 36 | def setup_env_class(builder: IRBuilder) -> ClassIR: |
| 37 | """Generate a class representing a function environment. |
| 38 | |
| 39 | Note that the variables in the function environment are not |
| 40 | actually populated here. This is because when the environment |
| 41 | class is generated, the function environment has not yet been |
| 42 | visited. This behavior is allowed so that when the compiler visits |
| 43 | nested functions, it can use the returned ClassIR instance to |
| 44 | figure out free variables it needs to access. The remaining |
| 45 | attributes of the environment class are populated when the |
| 46 | environment registers are loaded. |
| 47 | |
| 48 | Return a ClassIR representing an environment for a function |
| 49 | containing a nested function. |
| 50 | """ |
| 51 | env_class = ClassIR( |
| 52 | f"{builder.fn_info.namespaced_name()}_env", |
| 53 | builder.module_name, |
| 54 | is_generated=True, |
| 55 | is_final_class=True, |
| 56 | ) |
| 57 | env_class.reuse_freed_instance = True |
| 58 | env_class.attributes[SELF_NAME] = RInstance(env_class) |
| 59 | if builder.fn_info.is_nested and builder.fn_infos[-2]._env_class is not None: |
| 60 | # If the function is nested, its environment class must contain an environment |
| 61 | # attribute pointing to its encapsulating functions' environment class. |
| 62 | env_class.attributes[ENV_ATTR_NAME] = RInstance(builder.fn_infos[-2].env_class) |
| 63 | env_class.mro = [env_class] |
| 64 | builder.fn_info.env_class = env_class |
| 65 | builder.classes.append(env_class) |
| 66 | return env_class |
| 67 | |
| 68 | |
| 69 | def finalize_env_class(builder: IRBuilder, prefix: str = "") -> None: |
no test coverage detected
searching dependent graphs…