Skip to content

Support for Polymorphism #1474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ lazy_static! {
E034, Error, include_str!("./error_codes/E034.md"),
E035, Error, include_str!("./error_codes/E035.md"),
E036, Error, include_str!("./error_codes/E036.md"),
E037, Error, include_str!("./error_codes/E037.md"),
E037, Warning, include_str!("./error_codes/E037.md"),
E038, Error, include_str!("./error_codes/E038.md"), // Missing type
E039, Warning, include_str!("./error_codes/E039.md"),
E040, Error, include_str!("./error_codes/E040.md"),
Expand Down
13 changes: 9 additions & 4 deletions compiler/plc_driver/src/pipelines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,15 @@ use plc::{
codegen::{CodegenContext, GeneratedModule},
index::{indexer, FxIndexSet, Index},
linker::LinkerType,
lowering::{
property::PropertyLowerer,
{calls::AggregateTypeLowerer, InitVisitor},
},
lowering::{calls::AggregateTypeLowerer, property::PropertyLowerer, InitVisitor},
output::FormatOption,
parser::parse_file,
resolver::{
const_evaluator::UnresolvableConstant, AnnotationMapImpl, AstAnnotations, Dependency, StringLiterals,
TypeAnnotator,
},
validation::Validator,
vtable::VTableIndexer,
ConfigFormat, ErrorFormat, OnlineChange, Target, Threads,
};
use plc_diagnostics::{
Expand Down Expand Up @@ -263,6 +261,7 @@ impl<T: SourceContainer> BuildPipeline<T> {
Box::new(InitParticipant::new(self.project.get_init_symbol_name(), self.context.provider())),
Box::new(AggregateTypeLowerer::new(self.context.provider())),
Box::new(InheritanceLowerer::new(self.context.provider())),
Box::new(VTableIndexer::new(self.context.provider())),
];

for participant in mut_participants {
Expand Down Expand Up @@ -637,6 +636,12 @@ impl AnnotatedUnit {
}
}

impl From<AnnotatedUnit> for CompilationUnit {
fn from(value: AnnotatedUnit) -> Self {
value.unit
}
}

/// A project that has been annotated with information about different types and used units
pub struct AnnotatedProject {
pub units: Vec<AnnotatedUnit>,
Expand Down
25 changes: 23 additions & 2 deletions compiler/plc_driver/src/pipelines/participant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use std::{

use ast::provider::IdProvider;
use plc::{
codegen::GeneratedModule, lowering::calls::AggregateTypeLowerer, output::FormatOption, ConfigFormat,
OnlineChange, Target,
codegen::GeneratedModule, lowering::calls::AggregateTypeLowerer, output::FormatOption,
vtable::VTableIndexer, ConfigFormat, OnlineChange, Target,
};
use plc_diagnostics::diagnostics::Diagnostic;
use plc_lowering::inheritance::InheritanceLowerer;
Expand Down Expand Up @@ -281,3 +281,24 @@ impl PipelineParticipantMut for AggregateTypeLowerer {
indexed_project.annotate(self.id_provider.clone())
}
}

impl PipelineParticipantMut for VTableIndexer {
// TODO: Don't track overridden methods in vtable, as they're part of the parent instance (same for interfaces)
fn post_index(&mut self, indexed_project: IndexedProject) -> IndexedProject {
let IndexedProject { mut project, index, .. } = indexed_project;

let vtables_pou = VTableIndexer::create_vtables_for_pous(&index);
let vtables_intf = VTableIndexer::create_vtables_for_interfaces(&index);
let (internal, external) = VTableIndexer::create_global_variables_for_vtable(&index);

//FIXME: should we create the vtable in the unit of its pou?
if let Some(unit) = project.units.first_mut() {
unit.user_types.extend(vtables_pou);
unit.user_types.extend(vtables_intf);
unit.global_vars.push(internal);
unit.global_vars.push(external);
}

project.index(self.id_provider.clone())
}
}
9 changes: 9 additions & 0 deletions compiler/plc_source/src/source_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,15 @@ impl SourceLocation {
let SourceLocation { span, file } = self;
SourceLocation { span, file: FileMarker::Internal(file.get_name().unwrap_or_default()) }
}

pub fn into_internal_with_file(self) -> Self {
let SourceLocation { file, .. } = self;
SourceLocation {
span: CodeSpan::None,
file: FileMarker::Internal(file.get_name().unwrap_or_default()),
}
}

/// Constructs an undefined SourceRange with a 0..0 range and no filename
pub fn undefined() -> SourceLocation {
SourceLocation { span: CodeSpan::None, file: FileMarker::default() }
Expand Down
10 changes: 10 additions & 0 deletions compiler/plc_util/src/convention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ pub fn internal_type_name<T: AsRef<str> + Display>(prefix: T, original_type_name
format!("__{prefix}{original_type_name}")
}

/// Returns the default vtable global variable name for a function block or class
pub fn generate_vtable_name(name: &str) -> String {
format!("__vtable_{name}")
}

/// Returns the default vtable type name for a function block, class, or interface
pub fn generate_vtable_type_name(name: &str) -> String {
format!("__vtable_{name}_type")
}

#[cfg(test)]
mod tests {
#[test]
Expand Down
27 changes: 27 additions & 0 deletions libs/stdlib/src/bistable_functionblocks.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[repr(C)]
#[derive(Debug, Default)]
pub struct SetResetParams {
__vtable: usize,
set: bool,
reset: bool,
output: bool,
Expand All @@ -12,6 +13,21 @@ impl SetResetParams {
}
}

#[repr(C)]
pub struct VTable {
pub body: extern "C" fn(&mut SetResetParams),
}

#[allow(non_upper_case_globals)]
#[no_mangle]
#[used]
pub static __vtable_SR: VTable = VTable { body: SR };

#[allow(non_upper_case_globals)]
#[no_mangle]
#[used]
pub static __SR__init: SetResetParams =
SetResetParams { __vtable: 0, set: false, reset: false, output: false };
///.
/// Bistable function, set dominant
///
Expand All @@ -21,6 +37,17 @@ pub extern "C" fn SR(params: &mut SetResetParams) {
params.set_output(params.set | (!params.reset & params.output));
}

#[allow(non_upper_case_globals)]
#[no_mangle]
#[used]
pub static __vtable_RS: VTable = VTable { body: RS };

#[allow(non_upper_case_globals)]
#[no_mangle]
#[used]
pub static __RS__init: SetResetParams =
SetResetParams { __vtable: 0, set: false, reset: false, output: false };

///.
/// Bistable function, reset dominant
///
Expand Down
Loading
Loading