Skip to content

Commit

Permalink
Simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
Schottkyc137 committed Dec 9, 2024
1 parent 940efc7 commit fc66a81
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 46 deletions.
50 changes: 27 additions & 23 deletions vhdl_lang/src/analysis/expression.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2024, Lukas Scheller lukasscheller@icloud.com
*/

//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this file,
//! You can obtain one at http://mozilla.org/MPL/2.0/.
Expand Down Expand Up @@ -213,6 +221,22 @@ impl<'a, 't> AnalyzeContext<'a, 't> {
self.any_matcher().can_be_target_type(typ, ttyp)
}

pub fn check_type_mismatch(
&self,
typ: TypeEnt<'a>,
ttyp: TypeEnt<'a>,
pos: TokenSpan,
diagnostics: &mut dyn DiagnosticHandler,
) {
if !self.can_be_target_type(typ, ttyp.base()) {
diagnostics.push(Diagnostic::type_mismatch(
&pos.pos(self.ctx),
&typ.describe(),
ttyp,
));
}
}

pub fn expr_unknown_ttyp(
&self,
scope: &Scope<'a>,
Expand Down Expand Up @@ -741,13 +765,7 @@ impl<'a, 't> AnalyzeContext<'a, 't> {
if let Some(type_mark) =
as_fatal(self.analyze_qualified_expression(scope, qexpr, diagnostics))?
{
if !self.can_be_target_type(type_mark, target_base.base()) {
diagnostics.push(Diagnostic::type_mismatch(
&span.pos(self.ctx),
&type_mark.describe(),
target_type,
));
}
self.check_type_mismatch(type_mark, target_type, span, diagnostics);
}
}
Expression::Binary(ref mut op, ref mut left, ref mut right) => {
Expand All @@ -772,14 +790,7 @@ impl<'a, 't> AnalyzeContext<'a, 't> {
))? {
Some(Disambiguated::Unambiguous(overloaded)) => {
let op_type = overloaded.return_type().unwrap();

if !self.can_be_target_type(op_type, target_type.base()) {
diagnostics.push(Diagnostic::type_mismatch(
&span.pos(self.ctx),
&op_type.describe(),
target_type,
));
}
self.check_type_mismatch(op_type, target_type, span, diagnostics);
}
Some(Disambiguated::Ambiguous(candidates)) => {
diagnostics.push(Diagnostic::ambiguous_op(
Expand Down Expand Up @@ -815,14 +826,7 @@ impl<'a, 't> AnalyzeContext<'a, 't> {
))? {
Some(Disambiguated::Unambiguous(overloaded)) => {
let op_type = overloaded.return_type().unwrap();

if !self.can_be_target_type(op_type, target_type.base()) {
diagnostics.push(Diagnostic::type_mismatch(
&span.pos(self.ctx),
&op_type.describe(),
target_type,
));
}
self.check_type_mismatch(op_type, target_type, span, diagnostics);
}
Some(Disambiguated::Ambiguous(candidates)) => {
diagnostics.push(Diagnostic::ambiguous_op(
Expand Down
38 changes: 15 additions & 23 deletions vhdl_lang/src/analysis/semantic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2024, Lukas Scheller lukasscheller@icloud.com
*/

//! This Source Code Form is subject to the terms of the Mozilla Public
//! License, v. 2.0. If a copy of the MPL was not distributed with this file,
//! You can obtain one at http://mozilla.org/MPL/2.0/.
Expand All @@ -22,28 +30,9 @@ impl<'a> AnalyzeContext<'a, '_> {
choices: &mut [WithTokenSpan<Choice>],
diagnostics: &mut dyn DiagnosticHandler,
) -> FatalResult {
for choice in choices.iter_mut() {
self.choice_with_ttyp(scope, ttyp, choice, diagnostics)?;
}
Ok(())
}

fn check_resolved_name<T>(
&self,
ttyp: Option<TypeEnt<'a>>,
typ: TypeEnt<'a>,
diagnostics: &mut dyn DiagnosticHandler,
pos: &WithTokenSpan<T>,
) {
if let Some(ttyp) = ttyp {
if !self.can_be_target_type(typ, ttyp.base()) {
diagnostics.push(Diagnostic::type_mismatch(
&pos.pos(self.ctx),
&typ.describe(),
ttyp,
));
}
}
choices
.iter_mut()
.try_for_each(|choice| self.choice_with_ttyp(scope, ttyp, choice, diagnostics))
}

pub fn choice_with_ttyp(
Expand All @@ -55,13 +44,16 @@ impl<'a> AnalyzeContext<'a, '_> {
) -> FatalResult {
match choice.item {
Choice::Expression(ref mut expr) => {
// Check any names like `typ'range` before checking for any expressions
if let Expression::Name(name) = expr {
if let Some(resolved_name) =
as_fatal(self.name_resolve(scope, choice.span, name, diagnostics))?
{
match resolved_name {
ResolvedName::Type(typ) => {
self.check_resolved_name(ttyp, typ, diagnostics, choice)
if let Some(ttyp) = ttyp {
self.check_type_mismatch(typ, ttyp, choice.span, diagnostics);
}
}
_ => {
if let Some(ttyp) = ttyp {
Expand Down

0 comments on commit fc66a81

Please sign in to comment.