@@ -6,9 +6,8 @@ use crate::{
6
6
AstStatement , DirectAccessType , HardwareAccessType , Implementation , LinkageType , PouType ,
7
7
SourceRange , TypeNature ,
8
8
} ,
9
- builtins,
9
+ builtins:: { self , BuiltIn } ,
10
10
diagnostics:: Diagnostic ,
11
- lexer:: IdProvider ,
12
11
typesystem:: { self , * } ,
13
12
} ;
14
13
@@ -428,11 +427,6 @@ pub struct Index {
428
427
}
429
428
430
429
impl Index {
431
- pub fn create_with_builtins ( id_provider : IdProvider ) -> Index {
432
- let ( unit, _) = builtins:: parse_built_ins ( id_provider. clone ( ) ) ;
433
- visitor:: visit_index ( Index :: default ( ) , & unit, id_provider)
434
- }
435
-
436
430
/// imports all entries from the given index into the current index
437
431
///
438
432
/// imports all global_variables, member_variables, types and implementations
@@ -441,43 +435,41 @@ impl Index {
441
435
/// into the current one
442
436
pub fn import ( & mut self , mut other : Index ) {
443
437
//global variables
444
- for ( name, mut e) in other. global_variables . drain ( ..) {
445
- e. initial_value =
446
- self . maybe_import_const_expr ( & mut other. constant_expressions , & e. initial_value ) ;
438
+ for ( name, e) in other. global_variables . drain ( ..) {
439
+ let e = self . transfer_constants ( e, & mut other. constant_expressions ) ;
447
440
self . global_variables . insert ( name, e) ;
448
441
}
449
442
450
- //enum_global_variables
451
- for ( name , mut e) in other. enum_global_variables . drain ( ..) {
452
- e . initial_value =
453
- self . maybe_import_const_expr ( & mut other . constant_expressions , & e . initial_value ) ;
454
- self . enum_global_variables . insert ( name , e . clone ( ) ) ;
455
- self . enum_qualified_variables
456
- . insert ( e . qualified_name . to_lowercase ( ) , e) ;
443
+ //enmu_variables use the qualified variables since name conflicts will be overriden in the enum_global
444
+ for ( qualified_name , e) in other. enum_qualified_variables . drain ( ..) {
445
+ let e = self . transfer_constants ( e , & mut other . constant_expressions ) ;
446
+ dbg ! ( & e ) ;
447
+ self . enum_global_variables
448
+ . insert ( e . get_name ( ) . to_lowercase ( ) , e . clone ( ) ) ;
449
+ self . enum_qualified_variables . insert ( qualified_name, e) ;
457
450
}
458
451
459
452
//initializers
460
- for ( name, mut e) in other. global_initializers . drain ( ..) {
461
- e. initial_value =
462
- self . maybe_import_const_expr ( & mut other. constant_expressions , & e. initial_value ) ;
453
+ for ( name, e) in other. global_initializers . drain ( ..) {
454
+ let e = self . transfer_constants ( e, & mut other. constant_expressions ) ;
463
455
self . global_initializers . insert ( name, e) ;
464
456
}
465
457
466
458
//member variables
467
459
for ( name, mut members) in other. member_variables . drain ( ..) {
468
460
//enum qualified variables
469
- for ( _, mut e) in members. iter_mut ( ) {
470
- e. initial_value =
471
- self . maybe_import_const_expr ( & mut other. constant_expressions , & e. initial_value ) ;
461
+ let mut new_members = IndexMap :: default ( ) ;
462
+ for ( name, e) in members. drain ( ..) {
463
+ let e = self . transfer_constants ( e, & mut other. constant_expressions ) ;
464
+ new_members. insert ( name, e) ;
472
465
}
473
- self . member_variables . insert ( name, members ) ;
466
+ self . member_variables . insert ( name, new_members ) ;
474
467
}
475
468
476
469
//types
477
470
for ( name, mut e) in other. type_index . types . drain ( ..) {
478
471
e. initial_value =
479
472
self . maybe_import_const_expr ( & mut other. constant_expressions , & e. initial_value ) ;
480
-
481
473
match & mut e. information {
482
474
//import constant expressions in array-type-definitions
483
475
DataTypeInformation :: Array { dimensions, .. } => {
@@ -511,6 +503,38 @@ impl Index {
511
503
// self.constant_expressions.import(other.constant_expressions)
512
504
}
513
505
506
+ fn transfer_constants (
507
+ & mut self ,
508
+ mut variable : VariableIndexEntry ,
509
+ import_from : & mut ConstExpressions ,
510
+ ) -> VariableIndexEntry {
511
+ variable. initial_value = self . maybe_import_const_expr ( import_from, & variable. initial_value ) ;
512
+
513
+ let binding = if let Some ( HardwareBinding {
514
+ direction,
515
+ access,
516
+ entries,
517
+ location,
518
+ } ) = variable. get_hardware_binding ( )
519
+ {
520
+ let mut new_entries = vec ! [ ] ;
521
+ for entry in entries {
522
+ if let Some ( e) = self . maybe_import_const_expr ( import_from, & Some ( * entry) ) {
523
+ new_entries. push ( e) ;
524
+ }
525
+ }
526
+ Some ( HardwareBinding {
527
+ direction : * direction,
528
+ access : * access,
529
+ entries : new_entries,
530
+ location : location. clone ( ) ,
531
+ } )
532
+ } else {
533
+ None
534
+ } ;
535
+ variable. set_hardware_binding ( binding)
536
+ }
537
+
514
538
/// imports the corresponding const-expression (according to the given initializer-id) from the given ConstExpressions
515
539
/// into self's const-expressions and returns the new Id
516
540
fn maybe_import_const_expr (
@@ -520,7 +544,7 @@ impl Index {
520
544
) -> Option < ConstId > {
521
545
initializer_id
522
546
. as_ref ( )
523
- . and_then ( |it| import_from. remove ( it) )
547
+ . and_then ( |it| import_from. clone ( it) )
524
548
. map ( |( init, target_type, scope) | {
525
549
self . get_mut_const_expressions ( )
526
550
. add_constant_expression ( init, target_type, scope)
@@ -540,7 +564,7 @@ impl Index {
540
564
let ts = match type_size {
541
565
TypeSize :: LiteralInteger ( _) => Some ( * type_size) ,
542
566
TypeSize :: ConstExpression ( id) => import_from
543
- . remove ( id)
567
+ . clone ( id)
544
568
. map ( |( expr, target_type, scope) | {
545
569
self . get_mut_const_expressions ( ) . add_constant_expression (
546
570
expr,
@@ -970,11 +994,17 @@ impl Index {
970
994
InstanceIterator :: with_filter ( self , inner_filter)
971
995
}
972
996
973
- pub fn is_builtin ( & self , function : & str ) -> bool {
997
+ /// If the provided name is a builtin function, returns it from the builtin index
998
+ pub fn get_builtin_function ( & self , name : & str ) -> Option < & ' _ BuiltIn > {
974
999
//Find a type for that function, see if that type is builtin
975
- self . find_effective_type_info ( function)
1000
+ if let Some ( true ) = self
1001
+ . find_effective_type_info ( name)
976
1002
. map ( DataTypeInformation :: is_builtin)
977
- . unwrap_or_default ( )
1003
+ {
1004
+ builtins:: get_builtin ( name)
1005
+ } else {
1006
+ None
1007
+ }
978
1008
}
979
1009
}
980
1010
0 commit comments