Skip to content
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

Merged
merged 23 commits into from
Dec 2, 2021
Merged

Conversation

ghaith
Copy link
Collaborator

@ghaith ghaith commented Nov 18, 2021

Generics/ANY type support:

Generics can be defined on functions
Functions with generics will be resolved to typed functions during the call
External declaration for typed functions are added
Generics implementation will be ignored in this commit.

@ghaith ghaith force-pushed the issue-375-Add_support_for_Any_type branch from 935691c to 8f03a6b Compare November 18, 2021 11:57
@codecov-commenter
Copy link

codecov-commenter commented Nov 18, 2021

Codecov Report

Merging #380 (a7946cc) into master (901304e) will increase coverage by 0.12%.
The diff coverage is 95.23%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #380      +/-   ##
==========================================
+ Coverage   92.89%   93.01%   +0.12%     
==========================================
  Files          31       31              
  Lines       10470    10924     +454     
==========================================
+ Hits         9726    10161     +435     
- Misses        744      763      +19     
Impacted Files Coverage Δ
src/ast.rs 92.17% <77.77%> (-1.59%) ⬇️
src/index.rs 97.18% <86.95%> (+1.06%) ⬆️
src/typesystem.rs 94.80% <94.93%> (-0.78%) ⬇️
src/codegen/generators/data_type_generator.rs 94.55% <95.23%> (-0.11%) ⬇️
src/resolver.rs 95.95% <95.95%> (+0.01%) ⬆️
src/parser.rs 98.01% <97.72%> (-0.02%) ⬇️
src/index/visitor.rs 96.07% <98.66%> (+0.50%) ⬆️
src/ast/pre_processor.rs 100.00% <100.00%> (ø)
src/codegen.rs 100.00% <100.00%> (ø)
src/codegen/generators/expression_generator.rs 86.97% <100.00%> (ø)
... and 11 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 901304e...a7946cc. Read the comment docs.

src/ast.rs Outdated
GenericType {
name: String,
generic_symbol: String,
nature: String,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this be usefull as an enum?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean the 'nature' field? So we could limit it to only possible ANY_XXX type natures? I think that might be good yes. I think once we're in OOP territory we are just working with interfaces / derived types anyway

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry yes, I'm talking about the nature. I thought it would be highlighted when i mark it when commenting

src/parser.rs Show resolved Hide resolved
@ghaith ghaith force-pushed the issue-375-Add_support_for_Any_type branch from 478a704 to c51bfa3 Compare December 1, 2021 06:59
@ghaith ghaith marked this pull request as ready for review December 1, 2021 07:26
@ghaith ghaith requested a review from riederm December 1, 2021 07:26

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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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

name: enum_name.to_string(),
initial_value: init,
information,
nature: TypeNature::Any,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is enum really ANY

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have type casting for ENUM -> DINT? If yes they could be Signed otherwise we have to assume any or live with conversion issues if you call a generic ANY_INT method with an enum

name: name.to_string(),
initial_value: init,
information,
nature: TypeNature::Any,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is subrange really any? It should be at least something numeric? no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subrange will / should never be queried directly, and at this stage I don't know the subtype of it, I could however in the least assume an Int nature so I'll change to that

@@ -362,6 +362,9 @@ pub enum Token {
#[regex("[a-zA-Z_][a-zA-Z_0-9]*#")]
TypeCastPrefix,

#[regex("ANY(?:_(DERIVED|ELEMENTARY|MAGNITUDE|NUM|REAL|INT|SIGNED|UNSIGNED|DURATION|BIT|CHARS|STRING|CHAR|DATE)+)?", super::parse_type_nature)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's ok for me, although I would also not mind to not make them keywords, but to look for an identifier and check in the parser (so syntactical validation) that this a valid nature

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can indeed delegate to the parser, in which case we expect something like an Identifie and we manually check the possible types and create the nature
I'll delay this to the end though, i'm prioritizing the other issues first

@@ -35,6 +42,95 @@ pub enum DirectAccessType {
DWord,
}

#[derive(Debug, PartialEq, Clone, Copy)]
Copy link
Collaborator

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?

src/resolver.rs Outdated
/// It collects all candidates for a generic function
/// Then chooses the best fitting function signature
/// And reannotates the function with the found information
fn update_generic_information(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we rename this to: update_generic_call_statement

src/resolver.rs Outdated
fn update_generic_information(
&mut self,
generics_candidates: HashMap<&str, Vec<&str>>,
operator_qualifier: &str,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

operator_qualifier gives me a hard time. as far as I can see, we don't really use this a qualifier somewhere here?
is this the implementation_name maybe?

src/resolver.rs Outdated
fn update_generic_function_parameters(
&mut self,
s: &AstStatement,
operator_qualifier: &str,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

operator_qualifier is this the function's name?

src/resolver.rs Outdated
);
self.annotation_map.add_generic_nature(p, *nature);
//If assginment, annotate the left side with the new type
if let AstStatement::Assignment { left, right, .. } = p {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usually the annotator does not annotate the assignment itself. semantically it is not correct that a := b reuslts in a's type - it results in void. there are languages that work like that, but so far rusty's ST-flavor does not.

3 lines above we annotated p, although it is an assignment.

i think we should only do this if it is not an Assignment. (so maybe just into an else-clause)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

}

#[test]
fn test_external_function_called() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very cool!

riederm and others added 3 commits December 1, 2021 12:09
added additional tests that test if generic function parameters
can be provided in an arbitrary order:

it should make no difference if you write
   foo(a := x, b := y);
or
   foo(b := y, a := x);

for a foo<T, V>
Copy link
Collaborator

@riederm riederm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 again, cool feature and nice implementation!

@ghaith ghaith merged commit ca2ebac into master Dec 2, 2021
@ghaith ghaith deleted the issue-375-Add_support_for_Any_type branch December 2, 2021 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants