Skip to content

Commit 0c71834

Browse files
committed
refactor: Simplify pre_linter types
Merged `FunctionMap` and `SubMap` into one type `SignatureMap`. Merged `FunctionSignature` and `SubSignature` into one type `Signature`. Merged `FunctionContext` and `SubContext` into one type `SubprogramContext`. Improved documentation.
1 parent 2d00350 commit 0c71834

10 files changed

Lines changed: 311 additions & 274 deletions

File tree

rusty_linter/src/converter/common/context.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use crate::core::TypeResolver;
2-
use crate::core::TypeResolverImpl;
3-
use crate::core::{FunctionMap, HasFunctions, HasSubs, HasUserDefinedTypes, SubMap};
1+
use crate::core::*;
42
use crate::names::Names;
53
use crate::pre_linter::PreLinterResult;
64
use rusty_parser::*;
@@ -19,13 +17,13 @@ impl TypeResolver for Context {
1917
}
2018

2119
impl HasFunctions for Context {
22-
fn functions(&self) -> &FunctionMap {
20+
fn functions(&self) -> &SignatureMap {
2321
self.pre_linter_result.functions()
2422
}
2523
}
2624

2725
impl HasSubs for Context {
28-
fn subs(&self) -> &SubMap {
26+
fn subs(&self) -> &SignatureMap {
2927
self.pre_linter_result.subs()
3028
}
3129
}
@@ -58,6 +56,6 @@ impl Context {
5856
pub fn function_qualifier(&self, bare_name: &BareName) -> Option<TypeQualifier> {
5957
self.functions()
6058
.get(bare_name)
61-
.map(|function_signature_pos| function_signature_pos.element.qualifier())
59+
.and_then(|function_signature_pos| function_signature_pos.element.qualifier())
6260
}
6361
}

rusty_linter/src/core/traits.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
use crate::core::{FunctionMap, ResolvedParamType, SubMap};
1+
use crate::core::{ResolvedParamType, SignatureMap};
22
use rusty_parser::{
33
Expression, ExpressionPos, ExpressionType, HasExpressionType, TypeQualifier, UserDefinedTypes,
44
};
55

66
pub trait HasFunctions {
7-
fn functions(&self) -> &FunctionMap;
7+
fn functions(&self) -> &SignatureMap;
88
}
99

1010
pub trait HasSubs {
11-
fn subs(&self) -> &SubMap;
11+
fn subs(&self) -> &SignatureMap;
1212
}
1313

1414
pub trait HasUserDefinedTypes {

rusty_linter/src/core/types.rs

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
use crate::pre_linter::{FunctionSignature, SubSignature};
2-
use rusty_common::Positioned;
3-
use rusty_parser::{BareName, BuiltInStyle, QualifiedName, TypeQualifier};
41
use std::collections::HashMap;
52

6-
pub type SubMap = HashMap<BareName, Positioned<SubSignature>>;
7-
pub type FunctionMap = HashMap<BareName, Positioned<FunctionSignature>>;
3+
use rusty_common::Positioned;
4+
use rusty_parser::{BareName, BuiltInStyle, QualifiedName, TypeQualifier};
85

96
/// Holds the resolved name of a subprogram.
107
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
@@ -23,9 +20,73 @@ pub enum NameContext {
2320
Function,
2421
}
2522

26-
#[derive(Eq, PartialEq)]
23+
/// A resolved parameter type.
24+
#[derive(PartialEq)]
2725
pub enum ResolvedParamType {
26+
/// A built-in type.
27+
/// The type qualifier indicates the type.
28+
/// The style indicates how the parameter was declared:
29+
/// Compact: e.g. `A$` or Extended e.g. `A AS STRING`
2830
BuiltIn(TypeQualifier, BuiltInStyle),
31+
32+
/// A user defined type.
2933
UserDefined(BareName),
34+
35+
/// An array type.
36+
/// Dimensions are not allowed for parameter types.
3037
Array(Box<Self>),
3138
}
39+
40+
/// A collection of resolved parameter types.
41+
pub type ResolvedParamTypes = Vec<ResolvedParamType>;
42+
43+
/// The signature of a FUNCTION or SUB.
44+
/// Consists of the resolved parameter types and, in case of a FUNCTION, the return type.
45+
#[derive(PartialEq)]
46+
pub struct Signature {
47+
/// The return type of a FUNCTION, or None if this is the signature of a SUB.
48+
q: Option<TypeQualifier>,
49+
50+
/// The resolved parameter types.
51+
param_types: ResolvedParamTypes,
52+
}
53+
54+
impl Signature {
55+
pub fn new_sub(param_types: ResolvedParamTypes) -> Self {
56+
Self {
57+
q: None,
58+
param_types,
59+
}
60+
}
61+
62+
pub fn new_function(q: TypeQualifier, param_types: ResolvedParamTypes) -> Self {
63+
Self {
64+
q: Some(q),
65+
param_types,
66+
}
67+
}
68+
69+
pub fn qualifier(&self) -> Option<TypeQualifier> {
70+
self.q.as_ref().copied()
71+
}
72+
73+
pub fn param_types(&self) -> &ResolvedParamTypes {
74+
&self.param_types
75+
}
76+
}
77+
78+
// Equality between a Signature and a TypeQualifier.
79+
// Equality is done against the return type of the Signature,
80+
// which always fails for a SUB.
81+
82+
impl PartialEq<TypeQualifier> for Signature {
83+
fn eq(&self, other: &TypeQualifier) -> bool {
84+
match &self.q {
85+
Some(q) => q == other,
86+
_ => false,
87+
}
88+
}
89+
}
90+
91+
/// A map of (bare) subprogram names to their respective signatures.
92+
pub type SignatureMap = HashMap<BareName, Positioned<Signature>>;

rusty_linter/src/post_linter/user_defined_function_linter.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use crate::core::{CanCastTo, HasFunctions, ResolvedParamType};
2-
use crate::core::{LintError, LintErrorPos};
3-
use crate::pre_linter::ResolvedParamTypes;
1+
use crate::core::*;
42
use rusty_common::*;
53
use rusty_parser::*;
64

@@ -165,7 +163,7 @@ where
165163
if let Name::Qualified(bare_name, qualifier) = name {
166164
match self.linter_context.functions().get(bare_name) {
167165
Some(function_signature_pos) => {
168-
if function_signature_pos.element.qualifier() != *qualifier {
166+
if function_signature_pos.element != *qualifier {
169167
Err(LintError::TypeMismatch.at(function_signature_pos))
170168
} else {
171169
lint_call_args(args, function_signature_pos.element.param_types(), pos)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use crate::core::ConstLookup;
2+
use rusty_common::CaseInsensitiveString;
3+
use rusty_parser::BareName;
4+
use rusty_variant::Variant;
5+
use std::collections::HashMap;
6+
7+
pub type ConstantMap = HashMap<BareName, Variant>;
8+
9+
impl ConstLookup for ConstantMap {
10+
fn get_resolved_constant(&self, name: &CaseInsensitiveString) -> Option<&Variant> {
11+
self.get(name)
12+
}
13+
}

0 commit comments

Comments
 (0)