@@ -6,9 +6,8 @@ use crate::{
66 AstStatement , DirectAccessType , HardwareAccessType , Implementation , LinkageType , PouType ,
77 SourceRange , TypeNature ,
88 } ,
9- builtins,
9+ builtins:: { self , BuiltIn } ,
1010 diagnostics:: Diagnostic ,
11- lexer:: IdProvider ,
1211 typesystem:: { self , * } ,
1312} ;
1413
@@ -428,11 +427,6 @@ pub struct Index {
428427}
429428
430429impl 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-
436430 /// imports all entries from the given index into the current index
437431 ///
438432 /// imports all global_variables, member_variables, types and implementations
@@ -441,43 +435,40 @@ impl Index {
441435 /// into the current one
442436 pub fn import ( & mut self , mut other : Index ) {
443437 //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 ) ;
447440 self . global_variables . insert ( name, e) ;
448441 }
449442
450443 //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 ) ;
444+ for ( name, e) in other. enum_global_variables . drain ( ..) {
445+ let e = self . transfer_constants ( e, & mut other. constant_expressions ) ;
454446 self . enum_global_variables . insert ( name, e. clone ( ) ) ;
455447 self . enum_qualified_variables
456448 . insert ( e. qualified_name . to_lowercase ( ) , e) ;
457449 }
458450
459451 //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 ) ;
452+ for ( name, e) in other. global_initializers . drain ( ..) {
453+ let e = self . transfer_constants ( e, & mut other. constant_expressions ) ;
463454 self . global_initializers . insert ( name, e) ;
464455 }
465456
466457 //member variables
467458 for ( name, mut members) in other. member_variables . drain ( ..) {
468459 //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 ) ;
460+ let mut new_members = IndexMap :: default ( ) ;
461+ for ( name, e) in members. drain ( ..) {
462+ let e = self . transfer_constants ( e, & mut other. constant_expressions ) ;
463+ new_members. insert ( name, e) ;
472464 }
473- self . member_variables . insert ( name, members ) ;
465+ self . member_variables . insert ( name, new_members ) ;
474466 }
475467
476468 //types
477469 for ( name, mut e) in other. type_index . types . drain ( ..) {
478470 e. initial_value =
479471 self . maybe_import_const_expr ( & mut other. constant_expressions , & e. initial_value ) ;
480-
481472 match & mut e. information {
482473 //import constant expressions in array-type-definitions
483474 DataTypeInformation :: Array { dimensions, .. } => {
@@ -511,6 +502,38 @@ impl Index {
511502 // self.constant_expressions.import(other.constant_expressions)
512503 }
513504
505+ fn transfer_constants (
506+ & mut self ,
507+ mut variable : VariableIndexEntry ,
508+ import_from : & mut ConstExpressions ,
509+ ) -> VariableIndexEntry {
510+ variable. initial_value = self . maybe_import_const_expr ( import_from, & variable. initial_value ) ;
511+
512+ let binding = if let Some ( HardwareBinding {
513+ direction,
514+ access,
515+ entries,
516+ location,
517+ } ) = variable. get_hardware_binding ( )
518+ {
519+ let mut new_entries = vec ! [ ] ;
520+ for entry in entries {
521+ if let Some ( e) = self . maybe_import_const_expr ( import_from, & Some ( * entry) ) {
522+ new_entries. push ( e) ;
523+ }
524+ }
525+ Some ( HardwareBinding {
526+ direction : * direction,
527+ access : * access,
528+ entries : new_entries,
529+ location : location. clone ( ) ,
530+ } )
531+ } else {
532+ None
533+ } ;
534+ variable. set_hardware_binding ( binding)
535+ }
536+
514537 /// imports the corresponding const-expression (according to the given initializer-id) from the given ConstExpressions
515538 /// into self's const-expressions and returns the new Id
516539 fn maybe_import_const_expr (
@@ -520,7 +543,7 @@ impl Index {
520543 ) -> Option < ConstId > {
521544 initializer_id
522545 . as_ref ( )
523- . and_then ( |it| import_from. remove ( it) )
546+ . and_then ( |it| import_from. clone ( it) )
524547 . map ( |( init, target_type, scope) | {
525548 self . get_mut_const_expressions ( )
526549 . add_constant_expression ( init, target_type, scope)
@@ -540,7 +563,7 @@ impl Index {
540563 let ts = match type_size {
541564 TypeSize :: LiteralInteger ( _) => Some ( * type_size) ,
542565 TypeSize :: ConstExpression ( id) => import_from
543- . remove ( id)
566+ . clone ( id)
544567 . map ( |( expr, target_type, scope) | {
545568 self . get_mut_const_expressions ( ) . add_constant_expression (
546569 expr,
@@ -970,11 +993,17 @@ impl Index {
970993 InstanceIterator :: with_filter ( self , inner_filter)
971994 }
972995
973- pub fn is_builtin ( & self , function : & str ) -> bool {
996+ /// If the provided name is a builtin function, returns it from the builtin index
997+ pub fn get_builtin_function ( & self , name : & str ) -> Option < & ' _ BuiltIn > {
974998 //Find a type for that function, see if that type is builtin
975- self . find_effective_type_info ( function)
999+ if let Some ( true ) = self
1000+ . find_effective_type_info ( name)
9761001 . map ( DataTypeInformation :: is_builtin)
977- . unwrap_or_default ( )
1002+ {
1003+ builtins:: get_builtin ( name)
1004+ } else {
1005+ None
1006+ }
9781007 }
9791008}
9801009
0 commit comments