Skip to content

Commit 32a0538

Browse files
committed
refactor
1 parent c32ede9 commit 32a0538

File tree

1 file changed

+31
-49
lines changed

1 file changed

+31
-49
lines changed

compiler/codegen/src/compile.rs

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,24 +2173,18 @@ impl Compiler<'_> {
21732173
) -> CompileResult<()> {
21742174
self.prepare_decorators(decorator_list)?;
21752175

2176-
let has_type_params = type_params.is_some();
2176+
let is_generic = type_params.is_some();
21772177
let firstlineno = self.get_source_line_number().get().to_u32();
21782178

2179-
if has_type_params {
2180-
// For PEP 695 classes, we need to:
2181-
// 1. Enter type params scope
2182-
// 2. Compile type params
2183-
// 3. Call compile_class_body (creates nested scope)
2184-
// 4. Generate class creation code
2185-
// 5. Exit type params scope and wrap everything in a function
2186-
2179+
// Step 1: If generic, enter type params scope and compile type params
2180+
if is_generic {
21872181
let type_params_name = format!("<generic parameters of {}>", name);
21882182
self.push_output(
21892183
bytecode::CodeFlags::IS_OPTIMIZED | bytecode::CodeFlags::NEW_LOCALS,
21902184
0,
21912185
0,
21922186
0,
2193-
type_params_name.clone(),
2187+
type_params_name,
21942188
);
21952189

21962190
// Set private name for name mangling
@@ -2200,35 +2194,39 @@ impl Compiler<'_> {
22002194
self.compile_type_params(type_params.unwrap())?;
22012195
let dot_type_params = self.name(".type_params");
22022196
emit!(self, Instruction::StoreLocal(dot_type_params));
2197+
}
22032198

2204-
// Compile the class body (creates its own scope)
2205-
let prev_ctx = self.ctx;
2206-
self.ctx = CompileContext {
2207-
func: FunctionContext::NoFunction,
2208-
in_class: true,
2209-
loop_data: None,
2210-
};
2211-
let class_code = self.compile_class_body(name, body, type_params, firstlineno)?;
2212-
self.ctx = prev_ctx;
2199+
// Step 2: Compile class body (always done, whether generic or not)
2200+
let prev_ctx = self.ctx;
2201+
self.ctx = CompileContext {
2202+
func: FunctionContext::NoFunction,
2203+
in_class: true,
2204+
loop_data: None,
2205+
};
2206+
let class_code = self.compile_class_body(name, body, type_params, firstlineno)?;
2207+
self.ctx = prev_ctx;
22132208

2214-
// Back in type params scope, create .generic_base
2209+
// Step 3: Generate the rest of the code for the call
2210+
if is_generic {
2211+
// Still in type params scope
2212+
let dot_type_params = self.name(".type_params");
2213+
let dot_generic_base = self.name(".generic_base");
2214+
2215+
// Create .generic_base
22152216
emit!(self, Instruction::LoadNameAny(dot_type_params));
22162217
emit!(
22172218
self,
22182219
Instruction::CallIntrinsic1 {
22192220
func: bytecode::IntrinsicFunction1::SubscriptGeneric
22202221
}
22212222
);
2222-
let dot_generic_base = self.name(".generic_base");
22232223
emit!(self, Instruction::StoreLocal(dot_generic_base));
22242224

2225-
// Generate the class creation code (still in type params scope)
2225+
// Generate class creation code
22262226
emit!(self, Instruction::LoadBuildClass);
22272227

2228-
// Set up the class function
2228+
// Set up the class function with type params
22292229
let mut func_flags = bytecode::MakeFunctionFlags::empty();
2230-
2231-
// Load .type_params for the class function
22322230
emit!(self, Instruction::LoadNameAny(dot_type_params));
22332231
func_flags |= bytecode::MakeFunctionFlags::TYPE_PARAMS;
22342232

@@ -2240,12 +2238,10 @@ impl Compiler<'_> {
22402238
code: Box::new(class_code),
22412239
});
22422240
self.emit_load_const(ConstantData::Str { value: name.into() });
2243-
22442241
emit!(self, Instruction::MakeFunction(func_flags));
2245-
22462242
self.emit_load_const(ConstantData::Str { value: name.into() });
22472243

2248-
// Compile bases with .generic_base appended
2244+
// Compile original bases
22492245
let base_count = if let Some(arguments) = arguments {
22502246
for arg in &arguments.args {
22512247
self.compile_expression(arg)?;
@@ -2272,7 +2268,6 @@ impl Compiler<'_> {
22722268
}
22732269
self.compile_expression(&keyword.value)?;
22742270
}
2275-
22762271
emit!(
22772272
self,
22782273
Instruction::CallFunctionKeyword {
@@ -2286,52 +2281,38 @@ impl Compiler<'_> {
22862281
// Return the created class
22872282
self.emit_return_value();
22882283

2289-
// Exit type params scope and get the code object
2284+
// Exit type params scope and wrap in function
22902285
let type_params_code = self.exit_scope();
22912286

2292-
// Now execute the type params function
2287+
// Execute the type params function
22932288
if self.build_closure(&type_params_code) {
22942289
// Should not need closure
22952290
}
22962291
self.emit_load_const(ConstantData::Code {
22972292
code: Box::new(type_params_code),
22982293
});
22992294
self.emit_load_const(ConstantData::Str {
2300-
value: type_params_name.into(),
2295+
value: format!("<generic parameters of {}>", name).into(),
23012296
});
23022297
emit!(
23032298
self,
23042299
Instruction::MakeFunction(bytecode::MakeFunctionFlags::empty())
23052300
);
2306-
23072301
emit!(self, Instruction::CallFunctionPositional { nargs: 0 });
23082302
} else {
2309-
// Traditional class without type params
2310-
let prev_ctx = self.ctx;
2311-
self.ctx = CompileContext {
2312-
func: FunctionContext::NoFunction,
2313-
in_class: true,
2314-
loop_data: None,
2315-
};
2316-
2317-
let code = self.compile_class_body(name, body, type_params, firstlineno)?;
2318-
self.ctx = prev_ctx;
2319-
2303+
// Non-generic class: standard path
23202304
emit!(self, Instruction::LoadBuildClass);
23212305

23222306
let mut func_flags = bytecode::MakeFunctionFlags::empty();
2323-
2324-
if self.build_closure(&code) {
2307+
if self.build_closure(&class_code) {
23252308
func_flags |= bytecode::MakeFunctionFlags::CLOSURE;
23262309
}
23272310

23282311
self.emit_load_const(ConstantData::Code {
2329-
code: Box::new(code),
2312+
code: Box::new(class_code),
23302313
});
23312314
self.emit_load_const(ConstantData::Str { value: name.into() });
2332-
23332315
emit!(self, Instruction::MakeFunction(func_flags));
2334-
23352316
self.emit_load_const(ConstantData::Str { value: name.into() });
23362317

23372318
let call = if let Some(arguments) = arguments {
@@ -2342,6 +2323,7 @@ impl Compiler<'_> {
23422323
self.compile_normal_call(call);
23432324
}
23442325

2326+
// Step 4: Apply decorators and store (common to both paths)
23452327
self.apply_decorators(decorator_list);
23462328
self.store_name(name)
23472329
}

0 commit comments

Comments
 (0)