Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "ST Debug oo test",
"program": "${workspaceFolder}/examples/oo_test",
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
Expand All @@ -20,7 +28,8 @@
}
},
"args": [
"target/demo.st"
"examples/oo_test.st",
"-g"
],
"cwd": "${workspaceFolder}"
},
Expand Down
1 change: 1 addition & 0 deletions compiler/plc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct Pou {
pub poly_mode: Option<PolymorphismMode>,
pub generics: Vec<GenericBinding>,
pub linkage: LinkageType,
pub super_class: Option<String>,
}

#[derive(Debug, PartialEq, Eq)]
Expand Down
Binary file added examples/oo_test
Binary file not shown.
34 changes: 34 additions & 0 deletions examples/oo_test.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
CLASS MyClass
VAR
x : DINT;
y : DINT;
END_VAR
END_CLASS

PROGRAM MyProg
VAR_IN_OUT
cls : MyClass;
END_VAR
VAR_OUTPUT
x : DINT;
y : DINT;
END_VAR
x := cls.x;
cls.y := y;
END_PROGRAM

FUNCTION main : DINT
VAR_TEMP
cls : MyClass;
END_VAR
VAR
x : DINT;
y : DINT;
END_VAR
cls.x := 2;
MyProg.y := 3;
MyProg(cls);
x := MyProg.x;
y := cls.y;
main := x + y;
END_FUNCTION
Binary file added oo_test.st
Binary file not shown.
45 changes: 34 additions & 11 deletions src/codegen/generators/data_type_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
typesystem::DataType,
};
use indexmap::IndexSet;
use inkwell::types::{AnyType, AnyTypeEnum};
use inkwell::{
types::{BasicType, BasicTypeEnum},
values::{BasicValue, BasicValueEnum},
Expand Down Expand Up @@ -177,7 +178,20 @@ impl<'ink, 'b> DataTypeGenerator<'ink, 'b> {
let members = members
.iter()
.filter(|it| !it.is_temp() && !it.is_return())
.map(|m| self.types_index.get_associated_type(m.get_type_name()))
.map(|m| {
//Generate fat pointer if needed
let type_name = self
.index
.find_effective_type_by_name(m.get_type_name())
.and_then(|it| it.get_type_information().get_inner_pointer_type())
.map(|it| format!("__fat_pointer_to_{}", it))
.filter(|it| {
m.get_declaration_type().is_by_ref() && self.index.find_type(it).is_some()
})
.unwrap_or_else(|| m.get_type_name().to_string());

self.types_index.get_associated_type(&type_name)
})
.collect::<Result<Vec<BasicTypeEnum>, Diagnostic>>()?;

let struct_type = match source {
Expand Down Expand Up @@ -215,9 +229,16 @@ impl<'ink, 'b> DataTypeGenerator<'ink, 'b> {
if dimensions.iter().any(|dimension| dimension.is_undetermined()) {
self.create_type(inner_type_name, self.index.get_type(inner_type_name)?)
} else {
self.index
.get_effective_type_by_name(inner_type_name)
.and_then(|inner_type| self.create_type(inner_type_name, inner_type))
let inner_type = if inner_type_name == "__FUNCTION_POINTER__" {
Ok(self.llvm.context.i32_type().fn_type(&[], false).as_any_type_enum())
} else {
self.index
.get_effective_type_by_name(inner_type_name)
.and_then(|inner_type| self.create_type(inner_type_name, inner_type))
.map(|inner_type| inner_type.as_any_type_enum())
};

inner_type
.and_then(|inner_type| self.create_nested_array_type(inner_type, dimensions))
.map(|it| it.as_basic_type_enum())
}
Expand Down Expand Up @@ -422,7 +443,7 @@ impl<'ink, 'b> DataTypeGenerator<'ink, 'b> {
/// `arr: ARRAY[0..3] OF INT`.
fn create_nested_array_type(
&self,
inner_type: BasicTypeEnum<'ink>,
inner_type: AnyTypeEnum<'ink>,
dimensions: &[Dimension],
) -> Result<BasicTypeEnum<'ink>, Diagnostic> {
let len = dimensions
Expand All @@ -438,12 +459,14 @@ impl<'ink, 'b> DataTypeGenerator<'ink, 'b> {
.ok_or_else(|| Diagnostic::codegen_error("Invalid array dimensions", SourceRange::undefined()))?;

let result = match inner_type {
BasicTypeEnum::IntType(ty) => ty.array_type(len),
BasicTypeEnum::FloatType(ty) => ty.array_type(len),
BasicTypeEnum::StructType(ty) => ty.array_type(len),
BasicTypeEnum::ArrayType(ty) => ty.array_type(len),
BasicTypeEnum::PointerType(ty) => ty.array_type(len),
BasicTypeEnum::VectorType(ty) => ty.array_type(len),
AnyTypeEnum::IntType(ty) => ty.array_type(len),
AnyTypeEnum::FloatType(ty) => ty.array_type(len),
AnyTypeEnum::StructType(ty) => ty.array_type(len),
AnyTypeEnum::ArrayType(ty) => ty.array_type(len),
AnyTypeEnum::PointerType(ty) => ty.array_type(len),
AnyTypeEnum::VectorType(ty) => ty.array_type(len),
AnyTypeEnum::FunctionType(ty) => ty.ptr_type(AddressSpace::default()).array_type(len),
AnyTypeEnum::VoidType(_) => unimplemented!()
}
.as_basic_type_enum();

Expand Down
Loading