Skip to content

Commit 618a3d1

Browse files
committed
split vtable into module, add first tests
1 parent 8208773 commit 618a3d1

File tree

4 files changed

+577
-102
lines changed

4 files changed

+577
-102
lines changed

compiler/plc_driver/src/pipelines.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,20 @@ use ast::{
1919

2020
use itertools::Itertools;
2121
use log::debug;
22-
use participant::{PipelineParticipant, PipelineParticipantMut, VTableIndexer};
22+
use participant::{PipelineParticipant, PipelineParticipantMut};
2323
use plc::{
2424
codegen::{CodegenContext, GeneratedModule},
2525
index::{indexer, FxIndexSet, Index},
2626
linker::LinkerType,
27-
lowering::{
28-
property::PropertyLowerer,
29-
{calls::AggregateTypeLowerer, InitVisitor},
30-
},
27+
lowering::{calls::AggregateTypeLowerer, property::PropertyLowerer, InitVisitor},
3128
output::FormatOption,
3229
parser::parse_file,
3330
resolver::{
3431
const_evaluator::UnresolvableConstant, AnnotationMapImpl, AstAnnotations, Dependency, StringLiterals,
3532
TypeAnnotator,
3633
},
3734
validation::Validator,
35+
vtable::VTableIndexer,
3836
ConfigFormat, ErrorFormat, OnlineChange, Target, Threads,
3937
};
4038
use plc_diagnostics::{

compiler/plc_driver/src/pipelines/participant.rs

Lines changed: 4 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ use std::{
1111
sync::{Arc, Mutex, RwLock},
1212
};
1313

14-
use ast::ast::{DataType, DataTypeDeclaration, UserTypeDeclaration};
15-
use ast::{ast::Variable, provider::IdProvider};
14+
use ast::provider::IdProvider;
1615
use plc::{
17-
codegen::GeneratedModule, index::Index, lowering::calls::AggregateTypeLowerer, output::FormatOption,
18-
typesystem::VOID_POINTER_TYPE, ConfigFormat, OnlineChange, Target,
16+
codegen::GeneratedModule, lowering::calls::AggregateTypeLowerer, output::FormatOption,
17+
vtable::VTableIndexer, ConfigFormat, OnlineChange, Target,
1918
};
2019
use plc_diagnostics::diagnostics::Diagnostic;
2120
use plc_lowering::inheritance::InheritanceLowerer;
2221
use project::{object::Object, project::LibraryInformation};
23-
use source_code::{source_location::SourceLocation, SourceContainer};
22+
use source_code::SourceContainer;
2423

2524
use super::{AnnotatedProject, AnnotatedUnit, GeneratedProject, IndexedProject, ParsedProject};
2625

@@ -283,98 +282,6 @@ impl PipelineParticipantMut for AggregateTypeLowerer {
283282
}
284283
}
285284

286-
pub struct VTableIndexer {
287-
id_provider: IdProvider,
288-
}
289-
290-
impl VTableIndexer {
291-
pub fn new(id_provider: IdProvider) -> Self {
292-
Self { id_provider }
293-
}
294-
295-
fn generate_vtable_name(name: &str) -> String {
296-
format!("__vtable_{name}")
297-
}
298-
299-
fn create_vtables_for_pous(index: &Index) -> Vec<UserTypeDeclaration> {
300-
let mut vtables = Vec::new();
301-
for pou in index.get_pous().values().filter(|pou| pou.is_function_block() || pou.is_class()) {
302-
let mut variables = Vec::new();
303-
304-
if let Some(parent) = pou.get_super_class() {
305-
variables.push(VTableIndexer::create_vtable_reference(parent));
306-
}
307-
308-
for interface in pou.get_interfaces() {
309-
variables.push(VTableIndexer::create_vtable_reference(interface));
310-
}
311-
312-
for method in index.get_methods_local(pou.get_name()) {
313-
variables.push(VTableIndexer::create_void_pointer(method.get_name()));
314-
}
315-
316-
vtables.push(VTableIndexer::create_vtable(pou.get_name(), variables));
317-
}
318-
319-
vtables
320-
}
321-
322-
fn create_vtables_for_interfaces(index: &Index) -> Vec<UserTypeDeclaration> {
323-
let mut vtables = Vec::new();
324-
for interface in index.get_interfaces().values() {
325-
let mut variables = Vec::new();
326-
for extension in &interface.extensions {
327-
variables.push(VTableIndexer::create_vtable_reference(&extension.name));
328-
}
329-
330-
for method in interface.get_declared_methods(index) {
331-
variables.push(VTableIndexer::create_void_pointer(method.get_name()));
332-
}
333-
334-
vtables.push(VTableIndexer::create_vtable(interface.get_name(), variables));
335-
}
336-
337-
vtables
338-
}
339-
340-
/// Creates a void pointer variable with the given name and location
341-
fn create_void_pointer(name: &str) -> Variable {
342-
Variable {
343-
name: name.to_string(),
344-
data_type_declaration: DataTypeDeclaration::Reference {
345-
referenced_type: VOID_POINTER_TYPE.into(),
346-
location: SourceLocation::internal(),
347-
},
348-
initializer: None,
349-
address: None,
350-
location: SourceLocation::internal(),
351-
}
352-
}
353-
354-
fn create_vtable_reference(name: &str) -> Variable {
355-
Variable {
356-
name: VTableIndexer::generate_vtable_name(name),
357-
data_type_declaration: DataTypeDeclaration::Reference {
358-
referenced_type: VTableIndexer::generate_vtable_name(name),
359-
location: SourceLocation::internal(),
360-
},
361-
initializer: None,
362-
address: None,
363-
location: SourceLocation::internal(),
364-
}
365-
}
366-
367-
/// Creates a vtable with the given member variables and a mangled name of the form `__vtable_<name>`
368-
fn create_vtable(name: &str, variables: Vec<Variable>) -> UserTypeDeclaration {
369-
UserTypeDeclaration {
370-
data_type: DataType::StructType { name: Some(Self::generate_vtable_name(name)), variables },
371-
initializer: None,
372-
location: SourceLocation::internal(),
373-
scope: Some(name.to_string()),
374-
}
375-
}
376-
}
377-
378285
impl PipelineParticipantMut for VTableIndexer {
379286
// TODO: Don't track overridden methods in vtable, as they're part of the parent instance (same for interfaces)
380287
fn post_index(&mut self, indexed_project: IndexedProject) -> IndexedProject {

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub mod output;
4343
pub mod parser;
4444
pub mod resolver;
4545
mod test_utils;
46+
pub mod vtable;
4647

4748
pub mod typesystem;
4849
pub mod validation;

0 commit comments

Comments
 (0)