-
Notifications
You must be signed in to change notification settings - Fork 55
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
Issue 375 add support for any type #380
Changes from 20 commits
7750771
e3fbdc1
8f03a6b
265abfd
ccdb789
560eb00
9098079
8037f58
43a53a6
276da9d
8b7bca3
fe7fdc4
9a54f4c
71e8a41
aafb8e8
fd7bb31
d2b77fe
807a3b9
a598e23
c51bfa3
4b8b977
96ee719
a7946cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
name = "rusty" | ||
version = "0.2.0" | ||
authors = ["Ghaith Hachem <[email protected]>", "Mathias Rieder <[email protected]>"] | ||
edition = "2018" | ||
edition = "2021" | ||
readme = "README.md" | ||
repository = "https://github.com/ghaith/rusty/" | ||
license = "LGPL-3.0-or-later" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,48 @@ | ||
// Copyright (c) 2020 Ghaith Hachem and Mathias Rieder | ||
|
||
use crate::ast::DataTypeDeclaration; | ||
|
||
use super::{ | ||
super::ast::{CompilationUnit, DataType, DataTypeDeclaration, UserTypeDeclaration, Variable}, | ||
super::ast::{CompilationUnit, DataType, UserTypeDeclaration, Variable}, | ||
Pou, SourceRange, | ||
}; | ||
use std::vec; | ||
use std::{collections::HashMap, vec}; | ||
|
||
pub fn pre_process(unit: &mut CompilationUnit) { | ||
//process all local variables from POUs | ||
for mut pou in unit.units.iter_mut() { | ||
//Find all generic types in that pou | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we put this into it's own function?. It kindo'f disturbs the level of detail of the rest of this function |
||
let mut generic_types = HashMap::new(); | ||
for binding in &pou.generics { | ||
let new_name = format!("__{}__{}", pou.name, binding.name); | ||
//Generate a type for the generic | ||
let data_type = UserTypeDeclaration { | ||
data_type: DataType::GenericType { | ||
name: new_name.clone(), | ||
generic_symbol: binding.name.clone(), | ||
nature: binding.nature, | ||
}, | ||
initializer: None, | ||
scope: Some(pou.name.clone()), | ||
location: pou.location.clone(), | ||
}; | ||
unit.types.push(data_type); | ||
generic_types.insert(binding.name.clone(), new_name); | ||
} | ||
//Find all variables that reference a generic type | ||
//Replace the reference with the generic type's name | ||
for var in pou | ||
.variable_blocks | ||
.iter_mut() | ||
.flat_map(|it| it.variables.iter_mut()) | ||
{ | ||
replace_generic_type_name(&mut var.data_type, &generic_types); | ||
} | ||
//Replace the return type's reference if needed | ||
if let Some(datatype) = pou.return_type.as_mut() { | ||
replace_generic_type_name(datatype, &generic_types); | ||
} | ||
|
||
let all_variables = pou | ||
.variable_blocks | ||
.iter_mut() | ||
|
@@ -39,7 +73,9 @@ pub fn pre_process(unit: &mut CompilationUnit) { | |
for dt in unit.types.iter_mut() { | ||
{ | ||
match &mut dt.data_type { | ||
DataType::StructType { name, variables } => { | ||
DataType::StructType { | ||
name, variables, .. | ||
} => { | ||
let name: &str = name.as_ref().map(|it| it.as_str()).unwrap_or("undefined"); | ||
variables | ||
.iter_mut() | ||
|
@@ -185,3 +221,24 @@ fn add_nested_datatypes( | |
}); | ||
} | ||
} | ||
|
||
fn replace_generic_type_name(dt: &mut DataTypeDeclaration, generics: &HashMap<String, String>) { | ||
match dt { | ||
DataTypeDeclaration::DataTypeDefinition { data_type, .. } => match data_type { | ||
DataType::ArrayType { | ||
referenced_type, .. | ||
} | ||
| DataType::PointerType { | ||
referenced_type, .. | ||
} => replace_generic_type_name(referenced_type.as_mut(), generics), | ||
_ => {} | ||
}, | ||
DataTypeDeclaration::DataTypeReference { | ||
referenced_type, .. | ||
} => { | ||
if let Some(type_name) = generics.get(referenced_type) { | ||
*referenced_type = type_name.clone(); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,7 +89,11 @@ impl<'ink> CodeGen<'ink> { | |
//Generate the POU stubs in the first go to make sure they can be referenced. | ||
for implementation in &unit.implementations { | ||
//Don't generate external functions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you update this comment to "don't generate external or generic functions |
||
if implementation.linkage != LinkageType::External { | ||
if implementation.linkage != LinkageType::External | ||
&& !global_index | ||
.get_type_information_or_void(&implementation.type_name) | ||
.is_generic() | ||
{ | ||
pou_generator.generate_implementation(implementation)?; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i find it a bit surprising, that typeNatures are note defined in typesystem.rs
I guess you want to avoid the dependency from AST -> typesystem?