Skip to content

Commit

Permalink
refactor: Remove -prefix from enum-variants
Browse files Browse the repository at this point in the history
  • Loading branch information
mhasel committed Jan 28, 2025
1 parent 6c4a761 commit 8fa2fd9
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 78 deletions.
34 changes: 16 additions & 18 deletions compiler/plc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ impl Debug for Variable {

impl Variable {
pub fn replace_data_type_with_reference_to(&mut self, type_name: String) -> DataTypeDeclaration {
let new_data_type = DataTypeDeclaration::DataTypeReference {
let new_data_type = DataTypeDeclaration::Reference {
referenced_type: type_name,
location: self.data_type_declaration.get_location(),
};
Expand All @@ -508,18 +508,18 @@ impl Variable {

#[derive(Clone, PartialEq)]
pub enum DataTypeDeclaration {
DataTypeReference { referenced_type: String, location: SourceLocation },
DataTypeDefinition { data_type: DataType, location: SourceLocation, scope: Option<String> },
Reference { referenced_type: String, location: SourceLocation },
Definition { data_type: DataType, location: SourceLocation, scope: Option<String> },
Aggregate { referenced_type: String, location: SourceLocation },
}

impl Debug for DataTypeDeclaration {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
DataTypeDeclaration::DataTypeReference { referenced_type, .. } => {
DataTypeDeclaration::Reference { referenced_type, .. } => {
f.debug_struct("DataTypeReference").field("referenced_type", referenced_type).finish()
}
DataTypeDeclaration::DataTypeDefinition { data_type, .. } => {
DataTypeDeclaration::Definition { data_type, .. } => {
f.debug_struct("DataTypeDefinition").field("data_type", data_type).finish()
}
DataTypeDeclaration::Aggregate { referenced_type, .. } => {
Expand All @@ -538,32 +538,30 @@ impl From<&DataTypeDeclaration> for SourceLocation {
impl DataTypeDeclaration {
pub fn get_name(&self) -> Option<&str> {
match self {
DataTypeDeclaration::Aggregate { referenced_type, .. }
| DataTypeDeclaration::DataTypeReference { referenced_type, .. } => {
Some(referenced_type.as_str())
}
DataTypeDeclaration::DataTypeDefinition { data_type, .. } => data_type.get_name(),
Self::Aggregate { referenced_type, .. }
| DataTypeDeclaration::Reference { referenced_type, .. } => Some(referenced_type.as_str()),
DataTypeDeclaration::Definition { data_type, .. } => data_type.get_name(),
}
}

pub fn get_location(&self) -> SourceLocation {
match self {
DataTypeDeclaration::DataTypeReference { location, .. } => location.clone(),
DataTypeDeclaration::DataTypeDefinition { location, .. } => location.clone(),
DataTypeDeclaration::Aggregate { location, .. } => location.clone(),
DataTypeDeclaration::Reference { location, .. } => location.clone(),
DataTypeDeclaration::Definition { location, .. } => location.clone(),
Self::Aggregate { location, .. } => location.clone(),
}
}

pub fn get_referenced_type(&self) -> Option<String> {
let DataTypeDeclaration::DataTypeReference { referenced_type, .. } = self else { return None };
let DataTypeDeclaration::Reference { referenced_type, .. } = self else { return None };
Some(referenced_type.to_owned())
}

pub fn get_inner_pointer_ty(&self) -> Option<DataTypeDeclaration> {
match self {
DataTypeDeclaration::DataTypeReference { .. } => Some(self.clone()),
DataTypeDeclaration::Reference { .. } => Some(self.clone()),

DataTypeDeclaration::DataTypeDefinition { data_type, .. } => {
DataTypeDeclaration::Definition { data_type, .. } => {
if let DataType::PointerType { referenced_type, .. } = data_type {
return referenced_type.get_inner_pointer_ty();
}
Expand Down Expand Up @@ -702,11 +700,11 @@ fn replace_reference(
type_name: String,
location: &SourceLocation,
) -> Option<DataTypeDeclaration> {
if let DataTypeDeclaration::DataTypeReference { .. } = **referenced_type {
if let DataTypeDeclaration::Reference { .. } = **referenced_type {
return None;
}
let new_data_type =
DataTypeDeclaration::DataTypeReference { referenced_type: type_name, location: location.clone() };
DataTypeDeclaration::Reference { referenced_type: type_name, location: location.clone() };
let old_data_type = std::mem::replace(referenced_type, Box::new(new_data_type));
Some(*old_data_type)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/plc_ast/src/mut_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ impl WalkerMut for DataTypeDeclaration {
where
V: AstVisitorMut,
{
if let DataTypeDeclaration::DataTypeDefinition { data_type, .. } = self {
if let DataTypeDeclaration::Definition { data_type, .. } = self {
visitor.visit_data_type(data_type);
}
}
Expand Down
25 changes: 11 additions & 14 deletions compiler/plc_ast/src/pre_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,12 @@ pub fn pre_process(unit: &mut CompilationUnit, mut id_provider: IdProvider) {
let name: &str = name.as_ref().map(|it| it.as_str()).unwrap_or("undefined");

let type_name = internal_type_name("", name);
let type_ref = DataTypeDeclaration::DataTypeReference {
let type_ref = DataTypeDeclaration::Reference {
referenced_type: type_name.clone(),
location: SourceLocation::internal(), //return_type.get_location(),
};
let datatype = std::mem::replace(referenced_type, Box::new(type_ref));
if let DataTypeDeclaration::DataTypeDefinition { mut data_type, location, scope } =
*datatype
{
if let DataTypeDeclaration::Definition { mut data_type, location, scope } = *datatype {
data_type.set_name(type_name);
add_nested_datatypes(name, &mut data_type, &mut new_types, &location);
let data_type = UserTypeDeclaration { data_type, initializer: None, location, scope };
Expand Down Expand Up @@ -301,13 +299,12 @@ fn preprocess_return_type(pou: &mut Pou, types: &mut Vec<UserTypeDeclaration>) {
if let Some(return_type) = &pou.return_type {
if should_generate_implicit(return_type) {
let type_name = format!("__{}_return", &pou.name); // TODO: Naming convention (see plc_util/src/convention.rs)
let type_ref = DataTypeDeclaration::DataTypeReference {
let type_ref = DataTypeDeclaration::Reference {
referenced_type: type_name.clone(),
location: return_type.get_location(),
};
let datatype = std::mem::replace(&mut pou.return_type, Some(type_ref));
if let Some(DataTypeDeclaration::DataTypeDefinition { mut data_type, location, scope }) = datatype
{
if let Some(DataTypeDeclaration::Definition { mut data_type, location, scope }) = datatype {
data_type.set_name(type_name);
add_nested_datatypes(pou.name.as_str(), &mut data_type, types, &location);
let data_type = UserTypeDeclaration { data_type, initializer: None, location, scope };
Expand All @@ -319,9 +316,9 @@ fn preprocess_return_type(pou: &mut Pou, types: &mut Vec<UserTypeDeclaration>) {

fn should_generate_implicit(datatype: &DataTypeDeclaration) -> bool {
match datatype {
DataTypeDeclaration::DataTypeReference { .. } | DataTypeDeclaration::Aggregate { .. } => false,
DataTypeDeclaration::DataTypeDefinition { data_type: DataType::VarArgs { .. }, .. } => false,
DataTypeDeclaration::DataTypeDefinition { .. } => true,
DataTypeDeclaration::Reference { .. } | DataTypeDeclaration::Aggregate { .. } => false,
DataTypeDeclaration::Definition { data_type: DataType::VarArgs { .. }, .. } => false,
DataTypeDeclaration::Definition { .. } => true,
}
}

Expand All @@ -335,7 +332,7 @@ fn pre_process_variable_data_type(
types: &mut Vec<UserTypeDeclaration>,
) {
let new_type_name = internal_type_name(&format!("{container_name}_"), &variable.name);
if let DataTypeDeclaration::DataTypeDefinition { mut data_type, location, scope } =
if let DataTypeDeclaration::Definition { mut data_type, location, scope } =
variable.replace_data_type_with_reference_to(new_type_name.clone())
{
// create index entry
Expand All @@ -353,7 +350,7 @@ fn add_nested_datatypes(
location: &SourceLocation,
) {
let new_type_name = format!("{container_name}_"); // TODO: Naming convention (see plc_util/src/convention.rs)
if let Some(DataTypeDeclaration::DataTypeDefinition { mut data_type, location: inner_location, scope }) =
if let Some(DataTypeDeclaration::Definition { mut data_type, location: inner_location, scope }) =
datatype.replace_data_type_with_reference_to(new_type_name.clone(), location)
{
data_type.set_name(new_type_name.clone());
Expand All @@ -364,15 +361,15 @@ fn add_nested_datatypes(

fn replace_generic_type_name(dt: &mut DataTypeDeclaration, generics: &FxHashMap<String, String>) {
match dt {
DataTypeDeclaration::DataTypeDefinition { data_type, .. } => match data_type {
DataTypeDeclaration::Definition { data_type, .. } => match data_type {
DataType::ArrayType { referenced_type, .. }
| DataType::PointerType { referenced_type, .. }
| DataType::VarArgs { referenced_type: Some(referenced_type), .. } => {
replace_generic_type_name(referenced_type.as_mut(), generics)
}
_ => {}
},
DataTypeDeclaration::DataTypeReference { referenced_type, .. } => {
DataTypeDeclaration::Reference { referenced_type, .. } => {
if let Some(type_name) = generics.get(referenced_type) {
referenced_type.clone_from(type_name);
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/plc_ast/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ impl Walker for DataTypeDeclaration {
where
V: AstVisitor,
{
if let DataTypeDeclaration::DataTypeDefinition { data_type, .. } = self {
if let DataTypeDeclaration::Definition { data_type, .. } = self {
visitor.visit_data_type(data_type);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/index/indexer/pou_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl<'i> PouIndexer<'i> {

for block in &pou.variable_blocks {
for var in &block.variables {
let varargs = if let DataTypeDeclaration::DataTypeDefinition {
let varargs = if let DataTypeDeclaration::Definition {
data_type: plc_ast::ast::DataType::VarArgs { referenced_type, sized },
..
} = &var.data_type_declaration
Expand Down
12 changes: 6 additions & 6 deletions src/index/indexer/user_type_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ impl UserTypeIndexer<'_, '_> {
let (vla_arr_type_declaration, dim_arr_type_declaration) =
if self.index.get_effective_type_by_name(&dummy_array_name).is_ok() {
(
DataTypeDeclaration::DataTypeReference {
DataTypeDeclaration::Reference {
referenced_type: member_array_name,
location: SourceLocation::internal(),
},
DataTypeDeclaration::DataTypeReference {
DataTypeDeclaration::Reference {
referenced_type: member_dimensions_name,
location: SourceLocation::internal(),
},
Expand All @@ -146,10 +146,10 @@ impl UserTypeIndexer<'_, '_> {

// define internal vla members
(
DataTypeDeclaration::DataTypeDefinition {
DataTypeDeclaration::Definition {
data_type: DataType::PointerType {
name: Some(member_array_name),
referenced_type: Box::new(DataTypeDeclaration::DataTypeReference {
referenced_type: Box::new(DataTypeDeclaration::Reference {
referenced_type: dummy_array_name,
location: SourceLocation::internal(),
}),
Expand All @@ -158,7 +158,7 @@ impl UserTypeIndexer<'_, '_> {
location: SourceLocation::internal(),
scope: None,
},
DataTypeDeclaration::DataTypeDefinition {
DataTypeDeclaration::Definition {
data_type: DataType::ArrayType {
name: Some(member_dimensions_name),
bounds: AstNode::new(
Expand All @@ -184,7 +184,7 @@ impl UserTypeIndexer<'_, '_> {
0,
SourceLocation::internal(),
),
referenced_type: Box::new(DataTypeDeclaration::DataTypeReference {
referenced_type: Box::new(DataTypeDeclaration::Reference {
referenced_type: DINT_TYPE.to_string(),
location: SourceLocation::internal(),
}),
Expand Down
2 changes: 1 addition & 1 deletion src/lowering/initializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fn create_init_unit(
vec![VariableBlock::default().with_block_type(VariableBlockType::InOut).with_variables(vec![
Variable {
name: "self".into(),
data_type_declaration: DataTypeDeclaration::DataTypeReference {
data_type_declaration: DataTypeDeclaration::Reference {
referenced_type: container_name.to_string(),
location: location.clone(),
},
Expand Down
Loading

0 comments on commit 8fa2fd9

Please sign in to comment.