55# The MIT License (MIT)
66#
77# Copyright (c) 2017-2020 Kestrel Technology LLC
8+ # Copyright (c) 2020-2023 Henny B. Sipma
9+ # Copyright (c) 2024 Aarno Labs LLC
810#
911# Permission is hereby granted, free of charge, to any person obtaining a copy
1012# of this software and associated documentation files (the "Software"), to deal
2931
3032import xml .etree .ElementTree as ET
3133
34+ from typing import Dict , List , Tuple , TYPE_CHECKING
3235
33- from chc .linker .CompCompatibility import CompCompatibility
34- from chc .app .CCompInfo import CCompInfo
3536
36- from chc .util .UnionFind import UnionFind
37+
38+ from chc .app .CCompInfo import CCompInfo
39+ from chc .app .IndexManager import FileKeyReference , FileVarReference
3740from chc .app .CGlobalDictionary import CGlobalDictionary
3841
42+ from chc .linker .CompCompatibility import CompCompatibility
43+
3944import chc .util .fileutil as UF
45+ from chc .util .loggingutil import chklogger
46+ from chc .util .UnionFind import UnionFind
4047import chc .util .xmlutil as UX
4148
49+ if TYPE_CHECKING :
50+ from chc .app .CApplication import CApplication
51+ from chc .app .CFile import CFile
52+ from chc .app .CGlobalDeclarations import CGlobalDeclarations
53+ from chc .app .IndexManager import IndexManager
54+
4255"""
4356Starting point: a list of (fileindex,compinfo key) pairs that identify the
4457 locally declared structs
5467"""
5568
5669
57- class CLinker (object ):
58- def __init__ (self , capp ):
59- self .capp = capp # CApplication
60- self .declarations = self .capp .declarations
70+ class CLinker :
71+ def __init__ (self , capp : "CApplication" ) -> None :
72+ self ._capp = capp
73+ self ._compinfos : List ["CCompInfo" ] = []
74+ self ._compinfoxrefs : Dict [Tuple [int , int ], int ] = {}
75+ self ._varinfoxrefs : Dict [Tuple [int , int ], int ] = {}
76+
77+ @property
78+ def capp (self ) -> "CApplication" :
79+ return self ._capp
80+
81+ @property
82+ def indexmanager (self ) -> "IndexManager" :
83+ return self .capp .indexmanager
84+
85+ @property
86+ def declarations (self ) -> "CGlobalDeclarations" :
87+ return self .capp .declarations
88+
89+ @property
90+ def compinfos (self ) -> List ["CCompInfo" ]:
91+ return self ._compinfos
6192
62- def get_file_compinfo_xrefs (self , fileindex ):
63- result = {}
93+ @property
94+ def compinfoxrefs (self ) -> Dict [Tuple [int , int ], int ]:
95+ return self ._compinfoxrefs
96+
97+ @property
98+ def varinfoxrefs (self ) -> Dict [Tuple [int , int ], int ]:
99+ return self ._varinfoxrefs
100+
101+ def get_file_compinfo_xrefs (self , fileindex : int ) -> Dict [int , int ]:
102+ result : Dict [int , int ] = {}
64103 for (fidx , ckey ) in self .compinfoxrefs :
65104 if fidx == fileindex :
66105 result [ckey ] = self .compinfoxrefs [(fidx , ckey )]
67106 return result
68107
69- def get_file_varinfo_xrefs (self , fileindex ) :
70- result = {}
108+ def get_file_varinfo_xrefs (self , fileindex : int ) -> Dict [ int , int ] :
109+ result : Dict [ int , int ] = {}
71110 for (fidx , vid ) in self .varinfoxrefs :
72111 if fidx == fileindex :
73112 result [vid ] = self .varinfoxrefs [(fidx , vid )]
74113 return result
75114
115+ """
76116 def get_global_compinfos(self):
77117 return self.compinfoinstances
78118
79119 def get_shared_instances(self):
80120 return self.sharedinstances
121+ """
81122
82- def link_compinfos (self ):
83- def f (cfile ):
84- print ("Linking " + cfile .name )
85- compinfos = cfile .declarations .get_compinfos ()
123+ def link_compinfos (self ) -> None :
124+
125+ chklogger .logger .info ("Link compinfos" )
126+
127+ # index and connect the compinfos from the individual files
128+ for cfile in self .capp .cfiles :
129+ compinfos = cfile .get_compinfos ()
86130 self .declarations .index_file_compinfos (cfile .index , compinfos )
87131
88- self . capp . iter_files ( f )
132+ # register the relationships found with the index manager
89133 ckey2gckey = self .declarations .ckey2gckey
90134 for fid in ckey2gckey :
91135 for ckey in ckey2gckey [fid ]:
92136 gckey = ckey2gckey [fid ][ckey ]
93- self .capp .indexmanager .add_ckey2gckey (fid , ckey , gckey )
137+ filekey = FileKeyReference (fid , ckey )
138+ self .capp .indexmanager .add_ckey2gckey (filekey , gckey )
94139
95140 """
96- def linkcompinfos(self):
141+ def linkcompinfos(self) -> None :
97142 self._checkcompinfopairs()
98143
99144 print('Found ' + str(len(self.possiblycompatiblestructs)) +
@@ -144,10 +189,11 @@ def linkcompinfos(self):
144189 else:
145190 filename = self.capp.getfilebyindex(id[0]).getfilename()
146191 self.sharedinstances[gckey].append((filename,c))
192+
147193 """
148194
149- def link_varinfos (self ):
150- def f (cfile ) :
195+ def link_varinfos (self ) -> None :
196+ def f (cfile : "CFile" ) -> None :
151197 varinfos = cfile .declarations .get_global_varinfos ()
152198 self .declarations .index_file_varinfos (cfile .index , varinfos )
153199
@@ -157,39 +203,22 @@ def f(cfile):
157203 for fid in vid2gvid :
158204 for vid in vid2gvid [fid ]:
159205 gvid = vid2gvid [fid ][vid ]
160- self .capp .indexmanager .add_vid2gvid (fid , vid , gvid )
161-
162- """
163- def linkvarinfos(self):
164- globalvarinfos = {}
165- for vinfo in self.varinfos:
166- name = vinfo.getname()
167- if vinfo.getstorage() == 'static':
168- fileindex = vinfo.getfile().getindex()
169- name = name + '__file__' + str(fileindex) + '__'
170- if not name in globalvarinfos: globalvarinfos[name] = []
171- globalvarinfos[name].append(vinfo)
172-
173- gvid = 1
174- for name in sorted(globalvarinfos):
175- for vinfo in globalvarinfos[name]:
176- id = vinfo.getid()
177- self.varinfoxrefs[id] = gvid
178- self.capp.indexmanager.addvid2gvid(id[0],id[1],gvid)
179- gvid += 1
180- """
206+ filevar = FileVarReference (fid , vid )
207+ self .indexmanager .add_vid2gvid (filevar , gvid )
181208
182- def save_global_compinfos (self ):
183- path = self .capp .path
209+ def save_global_compinfos (self ) -> None :
210+ path = self .capp .targetpath
184211 xroot = UX .get_xml_header ("globals" , "globals" )
185212 xnode = ET .Element ("globals" )
186213 xroot .append (xnode )
187214 self .declarations .write_xml (xnode )
188- filename = UF .get_global_definitions_filename (path )
215+ filename = UF .get_global_definitions_filename (path , self .capp .projectname )
216+ chklogger .logger .info ("Saving global compinfos to %s" , filename )
189217 with open (filename , "w" ) as fp :
190218 fp .write (UX .doc_to_pretty (ET .ElementTree (xroot )))
191219
192- def _checkcompinfopairs (self ):
220+ """
221+ def _checkcompinfopairs(self) -> None:
193222 self.possiblycompatiblestructs = []
194223 compinfos = sorted(self.compinfos, key=lambda c: c.getid())
195224 print(
@@ -209,3 +238,4 @@ def _checkcompinfopairs(self):
209238 self.possiblycompatiblestructs.append(pair)
210239 else:
211240 self.notcompatiblestructs.add(pair)
241+ """
0 commit comments