(
&mut self,
name: &str,
body: &[ast::Stmt],
decorator_list: &[ast::Decorator],
type_params: Option<&ast::TypeParams>,
arguments: Option<&ast::Arguments
| 5161 | } |
| 5162 | |
| 5163 | fn compile_class_def( |
| 5164 | &mut self, |
| 5165 | name: &str, |
| 5166 | body: &[ast::Stmt], |
| 5167 | decorator_list: &[ast::Decorator], |
| 5168 | type_params: Option<&ast::TypeParams>, |
| 5169 | arguments: Option<&ast::Arguments>, |
| 5170 | ) -> CompileResult<()> { |
| 5171 | self.prepare_decorators(decorator_list)?; |
| 5172 | |
| 5173 | let is_generic = type_params.is_some(); |
| 5174 | let firstlineno = self.get_source_line_number().get().to_u32(); |
| 5175 | |
| 5176 | // Save context before entering any scopes |
| 5177 | let saved_ctx = self.ctx; |
| 5178 | |
| 5179 | // Step 1: If generic, enter type params scope and compile type params |
| 5180 | if is_generic { |
| 5181 | let type_params_name = format!("<generic parameters of {name}>"); |
| 5182 | self.push_output( |
| 5183 | bytecode::CodeFlags::OPTIMIZED | bytecode::CodeFlags::NEWLOCALS, |
| 5184 | 0, |
| 5185 | 0, |
| 5186 | 0, |
| 5187 | type_params_name, |
| 5188 | )?; |
| 5189 | |
| 5190 | // Set private name for name mangling |
| 5191 | self.code_stack.last_mut().unwrap().private = Some(name.to_owned()); |
| 5192 | |
| 5193 | // TypeParams scope is function-like |
| 5194 | self.ctx = CompileContext { |
| 5195 | loop_data: None, |
| 5196 | in_class: saved_ctx.in_class, |
| 5197 | func: FunctionContext::Function, |
| 5198 | in_async_scope: false, |
| 5199 | }; |
| 5200 | |
| 5201 | // Compile type parameters and store as .type_params |
| 5202 | self.compile_type_params(type_params.unwrap())?; |
| 5203 | let dot_type_params = self.name(".type_params"); |
| 5204 | emit!( |
| 5205 | self, |
| 5206 | Instruction::StoreName { |
| 5207 | namei: dot_type_params |
| 5208 | } |
| 5209 | ); |
| 5210 | } |
| 5211 | |
| 5212 | // Step 2: Compile class body (always done, whether generic or not) |
| 5213 | let prev_ctx = self.ctx; |
| 5214 | self.ctx = CompileContext { |
| 5215 | func: FunctionContext::NoFunction, |
| 5216 | in_class: true, |
| 5217 | loop_data: None, |
| 5218 | in_async_scope: false, |
| 5219 | }; |
| 5220 | let class_code = self.compile_class_body(name, body, type_params, firstlineno)?; |
no test coverage detected