11use crate :: dif:: DIFConvertible ;
2- use crate :: dif:: representation:: DIFTypeRepresentation ;
2+ use crate :: dif:: representation:: { DIFTypeRepresentation , DIFValueRepresentation } ;
33use crate :: references:: reference:: Reference ;
44use crate :: references:: reference:: ReferenceMutability ;
55use crate :: references:: reference:: mutability_option_as_int;
@@ -14,7 +14,13 @@ use crate::values::core_values::r#type::Type;
1414use crate :: values:: pointer:: PointerAddress ;
1515use core:: cell:: RefCell ;
1616use core:: prelude:: rust_2024:: * ;
17+ use std:: hash:: RandomState ;
18+ use indexmap:: IndexMap ;
1719use serde:: { Deserialize , Serialize } ;
20+ use crate :: libs:: core:: { get_core_lib_type, CoreLibPointerId } ;
21+ use crate :: values:: core_value:: CoreValue ;
22+ use crate :: values:: value:: Value ;
23+ use crate :: values:: value_container:: ValueContainer ;
1824
1925#[ derive( Clone , Debug , PartialEq , Serialize , Deserialize ) ]
2026#[ serde( tag = "kind" , content = "def" , rename_all = "lowercase" ) ]
@@ -136,6 +142,52 @@ impl DIFTypeDefinition {
136142 } ,
137143 }
138144 }
145+
146+ fn to_type_definition (
147+ & self ,
148+ memory : & RefCell < Memory > ,
149+ ) -> TypeDefinition {
150+ match self {
151+ DIFTypeDefinition :: Intersection ( types) => {
152+ TypeDefinition :: Intersection (
153+ types
154+ . iter ( )
155+ . map ( |t| {
156+ t. to_type_container ( memory)
157+ } )
158+ . collect ( ) ,
159+ )
160+ }
161+ DIFTypeDefinition :: Union ( types) => TypeDefinition :: Union (
162+ types
163+ . iter ( )
164+ . map ( |t| t. to_type_container ( memory) )
165+ . collect ( ) ,
166+ ) ,
167+ DIFTypeDefinition :: Reference ( type_ref_addr) => {
168+ let type_ref = memory
169+ . borrow_mut ( )
170+ . get_type_reference ( type_ref_addr)
171+ . expect ( "Reference not found in memory" )
172+ . clone ( ) ;
173+ TypeDefinition :: Reference ( type_ref)
174+ }
175+ DIFTypeDefinition :: Type ( dif_type) => {
176+ TypeDefinition :: Type ( Box :: new ( dif_type. to_type ( memory) ) )
177+ } ,
178+ DIFTypeDefinition :: Marker ( ptr_address) => {
179+ TypeDefinition :: Marker ( ptr_address. clone ( ) )
180+ }
181+ DIFTypeDefinition :: Unit => TypeDefinition :: Unit ,
182+ DIFTypeDefinition :: Never => TypeDefinition :: Never ,
183+ DIFTypeDefinition :: Unknown => TypeDefinition :: Unknown ,
184+ _ => {
185+ core:: todo!(
186+ "DIFTypeDefinition::to_type_definition for this variant is not implemented yet"
187+ )
188+ }
189+ }
190+ }
139191}
140192
141193#[ derive( Clone , Debug , PartialEq , Serialize , Deserialize ) ]
@@ -177,6 +229,18 @@ impl DIFType {
177229 ) ,
178230 }
179231 }
232+
233+ pub ( crate ) fn to_type ( & self , memory : & RefCell < Memory > ) -> Type {
234+ Type {
235+ reference_mutability : self . mutability . clone ( ) ,
236+ type_definition : DIFTypeDefinition :: to_type_definition (
237+ & self . type_definition ,
238+ memory,
239+ ) ,
240+ base_type : None ,
241+ }
242+ }
243+
180244}
181245
182246impl From < DIFTypeRepresentation > for DIFType {
@@ -220,6 +284,26 @@ impl DIFTypeContainer {
220284 }
221285 }
222286 }
287+
288+ pub fn to_type_container (
289+ & self ,
290+ memory : & RefCell < Memory > ,
291+ ) -> TypeContainer {
292+ match self {
293+ DIFTypeContainer :: Type ( dif_type) => {
294+ TypeContainer :: Type ( dif_type. to_type ( memory) )
295+ }
296+ DIFTypeContainer :: Reference ( addr) => {
297+ TypeContainer :: TypeReference (
298+ memory
299+ . borrow_mut ( )
300+ . get_type_reference ( addr)
301+ . expect ( "Reference not found in memory" )
302+ . clone ( )
303+ )
304+ }
305+ }
306+ }
223307}
224308
225309#[ cfg( test) ]
0 commit comments