Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
mhasel committed Dec 20, 2024
2 parents 25b8273 + 9572f31 commit 83c5bbf
Show file tree
Hide file tree
Showing 26 changed files with 253 additions and 216 deletions.
12 changes: 12 additions & 0 deletions compiler/plc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,12 @@ impl Debug for DataTypeDeclaration {
}
}

impl From<&DataTypeDeclaration> for SourceLocation {
fn from(value: &DataTypeDeclaration) -> Self {
value.get_location()
}
}

impl DataTypeDeclaration {
pub fn get_name(&self) -> Option<&str> {
match self {
Expand Down Expand Up @@ -940,6 +946,12 @@ impl Debug for AstNode {
}
}

impl From<&AstNode> for SourceLocation {
fn from(value: &AstNode) -> Self {
value.get_location()
}
}

impl AstNode {
///Returns the statement in a singleton list, or the contained statements if the statement is already a list
pub fn get_as_list(&self) -> Vec<&AstNode> {
Expand Down
64 changes: 46 additions & 18 deletions compiler/plc_diagnostics/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,29 +176,42 @@ impl Diagnostic {

//Helper methods for diagnostics
impl Diagnostic {
pub fn unexpected_token_found(expected: &str, found: &str, range: SourceLocation) -> Diagnostic {
pub fn unexpected_token_found<T>(expected: &str, found: &str, location: T) -> Diagnostic
where
T: Into<SourceLocation>,
{
Diagnostic::new(format!("Unexpected token: expected {expected} but found {found}"))
.with_error_code("E007")
.with_location(range)
.with_location(location)
}

pub fn missing_function(location: SourceLocation) -> Diagnostic {
pub fn missing_function<T>(location: T) -> Diagnostic
where
T: Into<SourceLocation>,
{
Diagnostic::new("Cannot generate code outside of function context.")
.with_error_code("E072")
.with_location(location)
.with_location(location.into())
}

pub fn codegen_error(message: impl Into<String>, location: SourceLocation) -> Diagnostic {
Diagnostic::new(message).with_location(location).with_error_code("E071")
pub fn codegen_error<T, U>(message: T, location: U) -> Diagnostic
where
T: Into<String>,
U: Into<SourceLocation>,
{
Diagnostic::new(message).with_location(location.into()).with_error_code("E071")
}

pub fn llvm_error(file: &str, llvm_error: &str) -> Diagnostic {
Diagnostic::new(format!("{file}: Internal llvm error: {:}", llvm_error)).with_error_code("E005")
}

pub fn missing_token(expected_token: &str, range: SourceLocation) -> Diagnostic {
pub fn missing_token<T>(expected_token: &str, location: T) -> Diagnostic
where
T: Into<SourceLocation>,
{
Diagnostic::new(format!("Missing expected Token {expected_token}"))
.with_location(range)
.with_location(location)
.with_error_code("E006")
}

Expand All @@ -224,11 +237,17 @@ impl Diagnostic {
.with_location(location.into())
}

pub fn unknown_type(type_name: &str, location: SourceLocation) -> Diagnostic {
pub fn unknown_type<T>(type_name: &str, location: T) -> Diagnostic
where
T: Into<SourceLocation>,
{
Diagnostic::new(format!("Unknown type: {type_name:}")).with_error_code("E052").with_location(location)
}

pub fn unresolved_reference(reference: &str, location: SourceLocation) -> Diagnostic {
pub fn unresolved_reference<T>(reference: &str, location: T) -> Diagnostic
where
T: Into<SourceLocation>,
{
Diagnostic::new(format!("Could not resolve reference to {reference:}"))
.with_error_code("E048")
.with_location(location)
Expand All @@ -243,7 +262,10 @@ impl Diagnostic {
.with_location(location)
}

pub fn cannot_generate_initializer(variable_name: &str, location: SourceLocation) -> Diagnostic {
pub fn cannot_generate_initializer<T>(variable_name: &str, location: T) -> Diagnostic
where
T: Into<SourceLocation>,
{
Self::new(format!(
"Cannot generate literal initializer for '{variable_name}': Value cannot be derived"
))
Expand All @@ -253,20 +275,23 @@ impl Diagnostic {

pub fn cannot_generate_call_statement(operator: &AstNode) -> Diagnostic {
//TODO: We could probably get a better slice here
Diagnostic::codegen_error(
format!("cannot generate call statement for {:?}", operator),
operator.get_location(),
)
Diagnostic::codegen_error(format!("cannot generate call statement for {:?}", operator), operator)
}

pub fn cannot_generate_from_empty_literal(type_name: &str, location: SourceLocation) -> Diagnostic {
pub fn cannot_generate_from_empty_literal<T>(type_name: &str, location: T) -> Diagnostic
where
T: Into<SourceLocation>,
{
Diagnostic::codegen_error(
format!("Cannot generate {type_name} from empty literal").as_str(),
location,
)
}

pub fn const_pragma_is_not_allowed(location: SourceLocation) -> Diagnostic {
pub fn const_pragma_is_not_allowed<T>(location: T) -> Diagnostic
where
T: Into<SourceLocation>,
{
Diagnostic::new("Pragma {constant} is not allowed in POU declarations")
.with_location(location)
.with_error_code("E105")
Expand All @@ -275,7 +300,10 @@ impl Diagnostic {

// CFC related diagnostics
impl Diagnostic {
pub fn unnamed_control(location: SourceLocation) -> Diagnostic {
pub fn unnamed_control<T>(location: T) -> Diagnostic
where
T: Into<SourceLocation>,
{
Diagnostic::new("Unnamed control").with_error_code("E087").with_location(location)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ attributes #0 = { nofree nosync nounwind readnone speculatable willreturn }
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "mainProg", scope: !2, file: !2, line: 2, type: !3, isLocal: false, isDefinition: true)
!2 = !DIFile(filename: "file2.st", directory: "lib")
!3 = !DICompositeType(tag: DW_TAG_structure_type, name: "mainProg", scope: !2, file: !2, line: 2, align: 64, flags: DIFlagPublic, elements: !4, identifier: "mainProg")
!3 = !DICompositeType(tag: DW_TAG_structure_type, name: "mainProg", scope: !2, file: !2, line: 2, flags: DIFlagPublic, elements: !4, identifier: "mainProg")
!4 = !{}
!5 = !{i32 2, !"Dwarf Version", i32 5}
!6 = !{i32 2, !"Debug Info Version", i32 3}
Expand Down Expand Up @@ -73,7 +73,7 @@ attributes #0 = { nofree nosync nounwind readnone speculatable willreturn }
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "mainProg", scope: !2, file: !2, line: 2, type: !3, isLocal: false, isDefinition: true)
!2 = !DIFile(filename: "file2.st", directory: "lib")
!3 = !DICompositeType(tag: DW_TAG_structure_type, name: "mainProg", scope: !2, file: !2, line: 2, align: 64, flags: DIFlagPublic, elements: !4, identifier: "mainProg")
!3 = !DICompositeType(tag: DW_TAG_structure_type, name: "mainProg", scope: !2, file: !2, line: 2, flags: DIFlagPublic, elements: !4, identifier: "mainProg")
!4 = !{}
!5 = !{i32 2, !"Dwarf Version", i32 5}
!6 = !{i32 2, !"Debug Info Version", i32 3}
Expand Down Expand Up @@ -114,7 +114,7 @@ attributes #0 = { nofree nosync nounwind readnone speculatable willreturn }
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "mainProg", scope: !2, file: !2, line: 2, type: !3, isLocal: false, isDefinition: true)
!2 = !DIFile(filename: "file2.st", directory: "lib")
!3 = !DICompositeType(tag: DW_TAG_structure_type, name: "mainprog", scope: !2, file: !2, line: 2, align: 64, flags: DIFlagPublic, elements: !4, identifier: "mainprog")
!3 = !DICompositeType(tag: DW_TAG_structure_type, name: "mainprog", scope: !2, file: !2, line: 2, flags: DIFlagPublic, elements: !4, identifier: "mainprog")
!4 = !{}
!5 = !{i32 2, !"Dwarf Version", i32 5}
!6 = !{i32 2, !"Debug Info Version", i32 3}
Expand Down Expand Up @@ -155,7 +155,7 @@ declare !dbg !19 void @mainProg(%mainProg*)
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "mainProg", scope: !2, file: !2, line: 2, type: !3, isLocal: false, isDefinition: true)
!2 = !DIFile(filename: "file2.st", directory: "lib")
!3 = !DICompositeType(tag: DW_TAG_structure_type, name: "mainprog", scope: !2, file: !2, line: 2, align: 64, flags: DIFlagPublic, elements: !4, identifier: "mainprog")
!3 = !DICompositeType(tag: DW_TAG_structure_type, name: "mainprog", scope: !2, file: !2, line: 2, flags: DIFlagPublic, elements: !4, identifier: "mainprog")
!4 = !{}
!5 = !{i32 2, !"Dwarf Version", i32 5}
!6 = !{i32 2, !"Debug Info Version", i32 3}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ attributes #0 = { nofree nosync nounwind readnone speculatable willreturn }
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "mainProg", scope: !2, file: !2, line: 2, type: !3, isLocal: false, isDefinition: true)
!2 = !DIFile(filename: "file2.st", directory: "")
!3 = !DICompositeType(tag: DW_TAG_structure_type, name: "mainProg", scope: !2, file: !2, line: 2, align: 64, flags: DIFlagPublic, elements: !4, identifier: "mainProg")
!3 = !DICompositeType(tag: DW_TAG_structure_type, name: "mainProg", scope: !2, file: !2, line: 2, flags: DIFlagPublic, elements: !4, identifier: "mainProg")
!4 = !{}
!5 = !{i32 2, !"Dwarf Version", i32 5}
!6 = !{i32 2, !"Debug Info Version", i32 3}
Expand Down Expand Up @@ -73,7 +73,7 @@ attributes #0 = { nofree nosync nounwind readnone speculatable willreturn }
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "mainProg", scope: !2, file: !2, line: 2, type: !3, isLocal: false, isDefinition: true)
!2 = !DIFile(filename: "file2.st", directory: "")
!3 = !DICompositeType(tag: DW_TAG_structure_type, name: "mainProg", scope: !2, file: !2, line: 2, align: 64, flags: DIFlagPublic, elements: !4, identifier: "mainProg")
!3 = !DICompositeType(tag: DW_TAG_structure_type, name: "mainProg", scope: !2, file: !2, line: 2, flags: DIFlagPublic, elements: !4, identifier: "mainProg")
!4 = !{}
!5 = !{i32 2, !"Dwarf Version", i32 5}
!6 = !{i32 2, !"Debug Info Version", i32 3}
Expand Down Expand Up @@ -114,7 +114,7 @@ attributes #0 = { nofree nosync nounwind readnone speculatable willreturn }
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "mainProg", scope: !2, file: !2, line: 2, type: !3, isLocal: false, isDefinition: true)
!2 = !DIFile(filename: "file2.st", directory: "")
!3 = !DICompositeType(tag: DW_TAG_structure_type, name: "mainprog", scope: !2, file: !2, line: 2, align: 64, flags: DIFlagPublic, elements: !4, identifier: "mainprog")
!3 = !DICompositeType(tag: DW_TAG_structure_type, name: "mainprog", scope: !2, file: !2, line: 2, flags: DIFlagPublic, elements: !4, identifier: "mainprog")
!4 = !{}
!5 = !{i32 2, !"Dwarf Version", i32 5}
!6 = !{i32 2, !"Debug Info Version", i32 3}
Expand Down Expand Up @@ -155,7 +155,7 @@ declare !dbg !19 void @mainProg(%mainProg*)
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "mainProg", scope: !2, file: !2, line: 2, type: !3, isLocal: false, isDefinition: true)
!2 = !DIFile(filename: "file2.st", directory: "")
!3 = !DICompositeType(tag: DW_TAG_structure_type, name: "mainprog", scope: !2, file: !2, line: 2, align: 64, flags: DIFlagPublic, elements: !4, identifier: "mainprog")
!3 = !DICompositeType(tag: DW_TAG_structure_type, name: "mainprog", scope: !2, file: !2, line: 2, flags: DIFlagPublic, elements: !4, identifier: "mainprog")
!4 = !{}
!5 = !{i32 2, !"Dwarf Version", i32 5}
!6 = !{i32 2, !"Debug Info Version", i32 3}
Expand Down
2 changes: 1 addition & 1 deletion compiler/plc_xml/src/xml_parser/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn transform_jump(
.as_ref()
.filter(|it| !it.is_empty())
.map(|it| session.parse_expression(it.as_ref(), control.local_id, control.execution_order_id))
.ok_or_else(|| Diagnostic::unnamed_control(location.clone()))?;
.ok_or_else(|| Diagnostic::unnamed_control(&location))?;

Ok(AstFactory::create_jump_statement(Box::new(condition), Box::new(target), location, session.next_id()))
}
Expand Down
47 changes: 14 additions & 33 deletions src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ lazy_static! {
//Generate an access from the first param
if let (&[k], params) = params.split_at(1) {
let type_hint = params.first()
.ok_or_else(|| Diagnostic::codegen_error("Invalid signature for MUX", location.clone()))
.ok_or_else(|| Diagnostic::codegen_error("Invalid signature for MUX", &location))
.and_then(|it| generator.get_type_hint_info_for(it))?;
//Create a temp var
let result_type = generator.llvm_index.get_associated_type(type_hint.get_name())?;
Expand Down Expand Up @@ -259,9 +259,9 @@ lazy_static! {
// return size of llvm type
let size = generator.llvm_index
.get_associated_type(type_name)
.map_err(|_| Diagnostic::codegen_error(format!("Could not find associated data type: {type_name}"), location.clone())
.map_err(|_| Diagnostic::codegen_error(format!("Could not find associated data type: {type_name}"), &location)
)?.size_of()
.ok_or_else(|| Diagnostic::codegen_error("Parameter type is not sized.", location.clone()))?
.ok_or_else(|| Diagnostic::codegen_error("Parameter type is not sized.", &location))?
.as_basic_value_enum();
Ok(ExpressionValue::RValue(size))
} else {
Expand Down Expand Up @@ -598,7 +598,7 @@ fn validate_builtin_symbol_parameter_count(
operation: Operator,
) {
let Some(params) = parameters else {
validator.push_diagnostic(Diagnostic::invalid_argument_count(2, 0, operator.get_location()));
validator.push_diagnostic(Diagnostic::invalid_argument_count(2, 0, operator));
return;
};

Expand All @@ -607,20 +607,12 @@ fn validate_builtin_symbol_parameter_count(
// non-extensible operators
Operator::Minus | Operator::Division | Operator::NotEqual => {
if count != 2 {
validator.push_diagnostic(Diagnostic::invalid_argument_count(
2,
count,
operator.get_location(),
));
validator.push_diagnostic(Diagnostic::invalid_argument_count(2, count, operator));
}
}
_ => {
if count < 2 {
validator.push_diagnostic(Diagnostic::invalid_argument_count(
2,
count,
operator.get_location(),
));
validator.push_diagnostic(Diagnostic::invalid_argument_count(2, count, operator));
}
}
}
Expand Down Expand Up @@ -765,19 +757,16 @@ fn validate_variable_length_array_bound_function(
index: &Index,
) {
let Some(parameters) = parameters else {
validator.push_diagnostic(Diagnostic::invalid_argument_count(2, 0, operator.get_location()));
validator.push_diagnostic(Diagnostic::invalid_argument_count(2, 0, operator));

// no params, nothing to validate
return;
};

let params = ast::flatten_expression_list(parameters);

if params.len() > 2 {
validator.push_diagnostic(Diagnostic::invalid_argument_count(
2,
params.len(),
operator.get_location(),
));
validator.push_diagnostic(Diagnostic::invalid_argument_count(2, params.len(), operator));
}

match (params.first(), params.get(1)) {
Expand All @@ -792,7 +781,7 @@ fn validate_variable_length_array_bound_function(
TypeNature::Int
))
.with_error_code("E062")
.with_location(idx.get_location()),
.with_location(*idx),
)
}

Expand All @@ -809,16 +798,12 @@ fn validate_variable_length_array_bound_function(

if dimension_idx < 1 || dimension_idx > n_dimensions {
validator.push_diagnostic(
Diagnostic::new("Index out of bound")
.with_error_code("E046")
.with_location(operator.get_location()),
Diagnostic::new("Index out of bound").with_error_code("E046").with_location(operator),
)
}
};
}
(Some(_), None) => {
validator.push_diagnostic(Diagnostic::invalid_argument_count(2, 1, operator.get_location()))
}
(Some(_), None) => validator.push_diagnostic(Diagnostic::invalid_argument_count(2, 1, operator)),
_ => unreachable!(),
}
}
Expand All @@ -830,18 +815,14 @@ fn validate_argument_count(
expected: usize,
) {
let Some(params) = parameters else {
validator.push_diagnostic(Diagnostic::invalid_argument_count(expected, 0, operator.get_location()));
validator.push_diagnostic(Diagnostic::invalid_argument_count(expected, 0, operator));
return;
};

let params = flatten_expression_list(params);

if params.len() != expected {
validator.push_diagnostic(Diagnostic::invalid_argument_count(
expected,
params.len(),
operator.get_location(),
));
validator.push_diagnostic(Diagnostic::invalid_argument_count(expected, params.len(), operator));
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/codegen/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl<'ink> DebugBuilder<'ink> {
let res = self
.debug_info
.create_basic_type(name, size, encoding as u32, DIFlagsConstants::PUBLIC)
.map_err(|err| Diagnostic::codegen_error(err, location.clone()))?;
.map_err(|err| Diagnostic::codegen_error(err, location))?;
self.register_concrete_type(name, DebugType::Basic(res));
Ok(())
}
Expand Down Expand Up @@ -279,11 +279,13 @@ impl<'ink> DebugBuilder<'ink> {
let mut running_offset = MemoryLocation::new(0);
for (member_name, dt, location) in index_types.into_iter() {
let di_type = self.get_or_create_debug_type(dt, index)?;

//Adjust the offset based on the field alignment
let type_info = dt.get_type_information();
let alignment = type_info.get_alignment(index);
let size = type_info.get_size(index);
running_offset = running_offset.align_to(alignment);

types.push(
self.debug_info
.create_member_type(
Expand Down
Loading

0 comments on commit 83c5bbf

Please sign in to comment.