@@ -303,23 +303,26 @@ impl<'src> Compiler<'src> {
303
303
fn new ( opts : CompileOpts , source_code : SourceCode < ' src > , code_name : String ) -> Self {
304
304
let module_code = ir:: CodeInfo {
305
305
flags : bytecode:: CodeFlags :: NEW_LOCALS ,
306
- posonlyarg_count : 0 ,
307
- arg_count : 0 ,
308
- kwonlyarg_count : 0 ,
309
306
source_path : source_code. path . to_owned ( ) ,
310
- first_line_number : OneIndexed :: MIN ,
311
- obj_name : code_name. clone ( ) ,
312
- qualname : Some ( code_name) ,
313
307
private : None ,
314
308
blocks : vec ! [ ir:: Block :: default ( ) ] ,
315
309
current_block : ir:: BlockIdx ( 0 ) ,
316
- constants : IndexSet :: default ( ) ,
317
- name_cache : IndexSet :: default ( ) ,
318
- varname_cache : IndexSet :: default ( ) ,
319
- cellvar_cache : IndexSet :: default ( ) ,
320
- freevar_cache : IndexSet :: default ( ) ,
321
- fasthidden_cache : IndexMap :: default ( ) ,
310
+ metadata : ir:: CodeUnitMetadata {
311
+ name : code_name. clone ( ) ,
312
+ qualname : Some ( code_name) ,
313
+ consts : IndexSet :: default ( ) ,
314
+ names : IndexSet :: default ( ) ,
315
+ varnames : IndexSet :: default ( ) ,
316
+ cellvars : IndexSet :: default ( ) ,
317
+ freevars : IndexSet :: default ( ) ,
318
+ fasthidden : IndexMap :: default ( ) ,
319
+ argcount : 0 ,
320
+ posonlyargcount : 0 ,
321
+ kwonlyargcount : 0 ,
322
+ firstlineno : OneIndexed :: MIN ,
323
+ } ,
322
324
static_attributes : None ,
325
+ in_inlined_comp : false ,
323
326
} ;
324
327
Compiler {
325
328
code_stack : vec ! [ module_code] ,
@@ -415,28 +418,30 @@ impl Compiler<'_> {
415
418
416
419
let info = ir:: CodeInfo {
417
420
flags,
418
- posonlyarg_count,
419
- arg_count,
420
- kwonlyarg_count,
421
421
source_path,
422
- first_line_number,
423
- obj_name,
424
- qualname,
425
422
private,
426
-
427
423
blocks : vec ! [ ir:: Block :: default ( ) ] ,
428
424
current_block : ir:: BlockIdx ( 0 ) ,
429
- constants : IndexSet :: default ( ) ,
430
- name_cache : IndexSet :: default ( ) ,
431
- varname_cache,
432
- cellvar_cache,
433
- freevar_cache,
434
- fasthidden_cache : IndexMap :: default ( ) ,
425
+ metadata : ir:: CodeUnitMetadata {
426
+ name : obj_name,
427
+ qualname,
428
+ consts : IndexSet :: default ( ) ,
429
+ names : IndexSet :: default ( ) ,
430
+ varnames : varname_cache,
431
+ cellvars : cellvar_cache,
432
+ freevars : freevar_cache,
433
+ fasthidden : IndexMap :: default ( ) ,
434
+ argcount : arg_count,
435
+ posonlyargcount : posonlyarg_count,
436
+ kwonlyargcount : kwonlyarg_count,
437
+ firstlineno : first_line_number,
438
+ } ,
435
439
static_attributes : if is_class_scope {
436
440
Some ( IndexSet :: default ( ) )
437
441
} else {
438
442
None
439
443
} ,
444
+ in_inlined_comp : false ,
440
445
} ;
441
446
self . code_stack . push ( info) ;
442
447
}
@@ -452,15 +457,15 @@ impl Compiler<'_> {
452
457
// could take impl Into<Cow<str>>, but everything is borrowed from ast structs; we never
453
458
// actually have a `String` to pass
454
459
fn name ( & mut self , name : & str ) -> bytecode:: NameIdx {
455
- self . _name_inner ( name, |i| & mut i. name_cache )
460
+ self . _name_inner ( name, |i| & mut i. metadata . names )
456
461
}
457
462
fn varname ( & mut self , name : & str ) -> CompileResult < bytecode:: NameIdx > {
458
463
if Compiler :: is_forbidden_arg_name ( name) {
459
464
return Err ( self . error ( CodegenErrorType :: SyntaxError ( format ! (
460
465
"cannot assign to {name}" ,
461
466
) ) ) ) ;
462
467
}
463
- Ok ( self . _name_inner ( name, |i| & mut i. varname_cache ) )
468
+ Ok ( self . _name_inner ( name, |i| & mut i. metadata . varnames ) )
464
469
}
465
470
fn _name_inner (
466
471
& mut self ,
@@ -478,14 +483,14 @@ impl Compiler<'_> {
478
483
/// Set the qualified name for the current code object, based on CPython's compiler_set_qualname
479
484
fn set_qualname ( & mut self ) -> String {
480
485
let qualname = self . make_qualname ( ) ;
481
- self . current_code_info ( ) . qualname = Some ( qualname. clone ( ) ) ;
486
+ self . current_code_info ( ) . metadata . qualname = Some ( qualname. clone ( ) ) ;
482
487
qualname
483
488
}
484
489
fn make_qualname ( & mut self ) -> String {
485
490
let stack_size = self . code_stack . len ( ) ;
486
491
assert ! ( stack_size >= 1 ) ;
487
492
488
- let current_obj_name = self . current_code_info ( ) . obj_name . clone ( ) ;
493
+ let current_obj_name = self . current_code_info ( ) . metadata . name . clone ( ) ;
489
494
490
495
// If we're at the module level (stack_size == 1), qualname is just the name
491
496
if stack_size <= 1 {
@@ -497,7 +502,7 @@ impl Compiler<'_> {
497
502
let mut parent = & self . code_stack [ parent_idx] ;
498
503
499
504
// If parent is a type parameter scope, look at grandparent
500
- if parent. obj_name . starts_with ( "<generic parameters of " ) {
505
+ if parent. metadata . name . starts_with ( "<generic parameters of " ) {
501
506
if stack_size == 2 {
502
507
// If we're immediately within the module after type params,
503
508
// qualname is just the name
@@ -540,7 +545,7 @@ impl Compiler<'_> {
540
545
current_obj_name
541
546
} else {
542
547
// Check parent scope type
543
- let parent_obj_name = & parent. obj_name ;
548
+ let parent_obj_name = & parent. metadata . name ;
544
549
545
550
// Determine if parent is a function-like scope
546
551
let is_function_parent = parent. flags . contains ( bytecode:: CodeFlags :: IS_OPTIMIZED )
@@ -550,12 +555,12 @@ impl Compiler<'_> {
550
555
if is_function_parent {
551
556
// For functions, append .<locals> to parent qualname
552
557
// Use parent's qualname if available, otherwise use parent_obj_name
553
- let parent_qualname = parent. qualname . as_ref ( ) . unwrap_or ( parent_obj_name) ;
558
+ let parent_qualname = parent. metadata . qualname . as_ref ( ) . unwrap_or ( parent_obj_name) ;
554
559
format ! ( "{parent_qualname}.<locals>.{current_obj_name}" )
555
560
} else {
556
561
// For classes and other scopes, use parent's qualname directly
557
562
// Use parent's qualname if available, otherwise use parent_obj_name
558
- let parent_qualname = parent. qualname . as_ref ( ) . unwrap_or ( parent_obj_name) ;
563
+ let parent_qualname = parent. metadata . qualname . as_ref ( ) . unwrap_or ( parent_obj_name) ;
559
564
if parent_qualname == "<module>" {
560
565
// Module level, just use the name
561
566
current_obj_name
@@ -720,7 +725,7 @@ impl Compiler<'_> {
720
725
. ok_or_else ( || InternalError :: MissingSymbol ( name. to_string ( ) ) ) ,
721
726
) ;
722
727
let info = self . code_stack . last_mut ( ) . unwrap ( ) ;
723
- let mut cache = & mut info. name_cache ;
728
+ let mut cache = & mut info. metadata . names ;
724
729
enum NameOpType {
725
730
Fast ,
726
731
Global ,
@@ -729,7 +734,7 @@ impl Compiler<'_> {
729
734
}
730
735
let op_typ = match symbol. scope {
731
736
SymbolScope :: Local if self . ctx . in_func ( ) => {
732
- cache = & mut info. varname_cache ;
737
+ cache = & mut info. metadata . varnames ;
733
738
NameOpType :: Fast
734
739
}
735
740
SymbolScope :: GlobalExplicit => NameOpType :: Global ,
@@ -739,16 +744,16 @@ impl Compiler<'_> {
739
744
SymbolScope :: GlobalImplicit | SymbolScope :: Unknown => NameOpType :: Local ,
740
745
SymbolScope :: Local => NameOpType :: Local ,
741
746
SymbolScope :: Free => {
742
- cache = & mut info. freevar_cache ;
747
+ cache = & mut info. metadata . freevars ;
743
748
NameOpType :: Deref
744
749
}
745
750
SymbolScope :: Cell => {
746
- cache = & mut info. cellvar_cache ;
751
+ cache = & mut info. metadata . cellvars ;
747
752
NameOpType :: Deref
748
753
} // TODO: is this right?
749
754
SymbolScope :: TypeParams => {
750
755
// Type parameters are always cell variables
751
- cache = & mut info. cellvar_cache ;
756
+ cache = & mut info. metadata . cellvars ;
752
757
NameOpType :: Deref
753
758
} // SymbolScope::Unknown => NameOpType::Global,
754
759
} ;
@@ -764,7 +769,7 @@ impl Compiler<'_> {
764
769
. get_index_of ( name. as_ref ( ) )
765
770
. unwrap_or_else ( || cache. insert_full ( name. into_owned ( ) ) . 0 ) ;
766
771
if let SymbolScope :: Free = symbol. scope {
767
- idx += info. cellvar_cache . len ( ) ;
772
+ idx += info. metadata . cellvars . len ( ) ;
768
773
}
769
774
let op = match op_typ {
770
775
NameOpType :: Fast => match usage {
@@ -1653,7 +1658,8 @@ impl Compiler<'_> {
1653
1658
let ( doc_str, body) = split_doc ( body, & self . opts ) ;
1654
1659
1655
1660
self . current_code_info ( )
1656
- . constants
1661
+ . metadata
1662
+ . consts
1657
1663
. insert_full ( ConstantData :: None ) ;
1658
1664
1659
1665
// Emit RESUME instruction at function start
@@ -1774,18 +1780,20 @@ impl Compiler<'_> {
1774
1780
) ;
1775
1781
let parent_code = self . code_stack . last ( ) . unwrap ( ) ;
1776
1782
let vars = match symbol. scope {
1777
- SymbolScope :: Free => & parent_code. freevar_cache ,
1778
- SymbolScope :: Cell => & parent_code. cellvar_cache ,
1779
- SymbolScope :: TypeParams => & parent_code. cellvar_cache ,
1780
- _ if symbol. flags . contains ( SymbolFlags :: FREE_CLASS ) => & parent_code. freevar_cache ,
1783
+ SymbolScope :: Free => & parent_code. metadata . freevars ,
1784
+ SymbolScope :: Cell => & parent_code. metadata . cellvars ,
1785
+ SymbolScope :: TypeParams => & parent_code. metadata . cellvars ,
1786
+ _ if symbol. flags . contains ( SymbolFlags :: FREE_CLASS ) => {
1787
+ & parent_code. metadata . freevars
1788
+ }
1781
1789
x => unreachable ! (
1782
1790
"var {} in a {:?} should be free or cell but it's {:?}" ,
1783
1791
var, table. typ, x
1784
1792
) ,
1785
1793
} ;
1786
1794
let mut idx = vars. get_index_of ( var) . unwrap ( ) ;
1787
1795
if let SymbolScope :: Free = symbol. scope {
1788
- idx += parent_code. cellvar_cache . len ( ) ;
1796
+ idx += parent_code. metadata . cellvars . len ( ) ;
1789
1797
}
1790
1798
emit ! ( self , Instruction :: LoadClosure ( idx. to_u32( ) ) )
1791
1799
}
@@ -1910,7 +1918,8 @@ impl Compiler<'_> {
1910
1918
. code_stack
1911
1919
. last_mut ( )
1912
1920
. unwrap ( )
1913
- . cellvar_cache
1921
+ . metadata
1922
+ . cellvars
1914
1923
. iter ( )
1915
1924
. position ( |var| * var == "__class__" ) ;
1916
1925
@@ -3691,7 +3700,8 @@ impl Compiler<'_> {
3691
3700
} ;
3692
3701
3693
3702
self . current_code_info ( )
3694
- . constants
3703
+ . metadata
3704
+ . consts
3695
3705
. insert_full ( ConstantData :: None ) ;
3696
3706
3697
3707
self . compile_expression ( body) ?;
@@ -4344,7 +4354,7 @@ impl Compiler<'_> {
4344
4354
4345
4355
fn arg_constant ( & mut self , constant : ConstantData ) -> u32 {
4346
4356
let info = self . current_code_info ( ) ;
4347
- info. constants . insert_full ( constant) . 0 . to_u32 ( )
4357
+ info. metadata . consts . insert_full ( constant) . 0 . to_u32 ( )
4348
4358
}
4349
4359
4350
4360
fn emit_load_const ( & mut self , constant : ConstantData ) {
0 commit comments