Skip to content

Commit 6c66147

Browse files
committed
Merge branch 'vosa/fnptr2' of github.com:PLC-lang/rusty into vosa/polymorphism
2 parents 2be008e + 350c39b commit 6c66147

17 files changed

+1005
-263
lines changed

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
demo:
2+
cargo r -- target/demo.st tests/lit/util/printf.pli --linker=clang && ./demo.st.out
3+
4+
ir:
5+
cargo r -- target/demo.st tests/lit/util/printf.pli --linker=clang --ir --output=/dev/stdout
6+
7+
test:
8+
cargo nextest run --no-fail-fast --workspace && ./scripts/build.sh --lit
9+
10+
.PHONY: demo ir test

src/codegen/generators/data_type_generator.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
typesystem::DataType,
1414
};
1515

16-
use inkwell::types::{AnyType, AnyTypeEnum, BasicMetadataTypeEnum, FunctionType};
16+
use inkwell::types::{AnyType, AnyTypeEnum, FunctionType};
1717
use inkwell::{
1818
types::{BasicType, BasicTypeEnum},
1919
values::{BasicValue, BasicValueEnum},
@@ -104,7 +104,6 @@ pub fn generate_data_types<'ink>(
104104
log::debug!("creating type `{name}`");
105105
let gen_type = generator.create_type(name, user_type)?;
106106
generator.types_index.associate_type(name, gen_type)?
107-
//Get and associate debug type
108107
}
109108

110109
for (name, user_type) in &pou_types {
@@ -181,35 +180,36 @@ pub fn generate_data_types<'ink>(
181180
}
182181

183182
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> {
187184
let return_type = self
188185
.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+
)
190189
.map(|opt| opt.as_any_type_enum())
191190
.unwrap_or(self.llvm.context.void_type().into());
192191

193192
let mut parameter_types = vec![];
194193

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
196195
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());
201198
}
202199

203200
// 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+
}
213213

214214
let fn_type = match return_type {
215215
AnyTypeEnum::IntType(value) => value.fn_type(parameter_types.as_slice(), false),

0 commit comments

Comments
 (0)