@@ -13,7 +13,7 @@ use crate::{
13
13
typesystem:: DataType ,
14
14
} ;
15
15
16
- use inkwell:: types:: { AnyType , AnyTypeEnum , BasicMetadataTypeEnum , FunctionType } ;
16
+ use inkwell:: types:: { AnyType , AnyTypeEnum , FunctionType } ;
17
17
use inkwell:: {
18
18
types:: { BasicType , BasicTypeEnum } ,
19
19
values:: { BasicValue , BasicValueEnum } ,
@@ -104,7 +104,6 @@ pub fn generate_data_types<'ink>(
104
104
log:: debug!( "creating type `{name}`" ) ;
105
105
let gen_type = generator. create_type ( name, user_type) ?;
106
106
generator. types_index . associate_type ( name, gen_type) ?
107
- //Get and associate debug type
108
107
}
109
108
110
109
for ( name, user_type) in & pou_types {
@@ -181,35 +180,36 @@ pub fn generate_data_types<'ink>(
181
180
}
182
181
183
182
impl < ' ink > DataTypeGenerator < ' ink , ' _ > {
184
- fn create_function_type ( & self , name : & str ) -> Result < FunctionType < ' ink > , Diagnostic > {
185
- let return_type_dt = self . index . find_return_type ( name) . unwrap_or ( self . index . get_void_type ( ) ) ;
186
-
183
+ fn create_function_type ( & mut self , name : & str ) -> Result < FunctionType < ' ink > , Diagnostic > {
187
184
let return_type = self
188
185
. types_index
189
- . find_associated_type ( & return_type_dt. name )
186
+ . find_associated_type (
187
+ self . index . find_return_type ( name) . unwrap_or ( self . index . get_void_type ( ) ) . get_name ( ) ,
188
+ )
190
189
. map ( |opt| opt. as_any_type_enum ( ) )
191
190
. unwrap_or ( self . llvm . context . void_type ( ) . into ( ) ) ;
192
191
193
192
let mut parameter_types = vec ! [ ] ;
194
193
195
- // For methods, we need to add the 'this' parameter as the first parameter
194
+ // We need to add the POU type as the first parameter when dealing with methods
196
195
if let Some ( PouIndexEntry :: Method { parent_name, .. } ) = self . index . find_pou ( name) {
197
- // Get the owner class type and add it as the first parameter (this pointer)
198
- if let Ok ( owner_type) = self . types_index . get_associated_type ( parent_name) {
199
- parameter_types. push ( owner_type. ptr_type ( AddressSpace :: from ( ADDRESS_SPACE_GENERIC ) ) . into ( ) ) ;
200
- }
196
+ let ty = self . types_index . get_associated_type ( parent_name) . expect ( "must exist" ) ;
197
+ parameter_types. push ( ty. ptr_type ( AddressSpace :: from ( ADDRESS_SPACE_GENERIC ) ) . into ( ) ) ;
201
198
}
202
199
203
200
// Add the declared parameters
204
- let declared_params = self
205
- . index
206
- . get_declared_parameters ( name)
207
- . iter ( )
208
- . flat_map ( |param| self . types_index . get_associated_type ( & param. data_type_name ) )
209
- . map ( |opt| opt. into ( ) )
210
- . collect :: < Vec < BasicMetadataTypeEnum > > ( ) ;
211
-
212
- parameter_types. extend ( declared_params) ;
201
+ for parameter in self . index . get_declared_parameters ( name) {
202
+ // XXX: This is somewhat hacky, perhaps we can find a better solution? The main problem is that
203
+ // we query the data types of the parameters which may not yet have been registered. For example,
204
+ // at the time of writing this comment the `__auto_pointer_to_DINT` type was not registered for a
205
+ // VAR_IN_OUT parameter in a POU for which we create the function type here. I suppose the creation
206
+ // of function types must be done as the last step? Alternatively we could register opaque types and
207
+ // expand them later?
208
+ let ty = self
209
+ . create_type ( parameter. get_name ( ) , self . index . get_type ( & parameter. data_type_name ) . unwrap ( ) )
210
+ . unwrap ( ) ;
211
+ parameter_types. push ( ty. try_into ( ) . unwrap ( ) ) ;
212
+ }
213
213
214
214
let fn_type = match return_type {
215
215
AnyTypeEnum :: IntType ( value) => value. fn_type ( parameter_types. as_slice ( ) , false ) ,
0 commit comments