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

[cpp] Gencpp Rework Mk II #11819

Merged
merged 105 commits into from
Feb 5, 2025
Merged

Conversation

Aidan63
Copy link
Contributor

@Aidan63 Aidan63 commented Nov 11, 2024

This is a follow up to my previous cpp generator rework and is what I was aiming for in the first place.

Class and interface generation (as well as native and managed variants) was contained in one big function with a sprawling set of if statements to generate code based on those 4 types. Incorrect branches being taken has been the root of several bugs and the code in general is very unweildy and hard to extend.
When module level functions came out I had a go at extending the generator to output these as namespace level functions / variables but gave up as adding an extra variant was too much to deal with.

So now the cpp generate splits types into one of 5 categories, native class, managed class, native interface, managed interface. or enums. There are now dedicated generator functions for the header and implementation code of these 5 types which is mostly stitching together calls to shared functions with very little branching involved.

The fields for the class and interface types are also split up to make the generators work easier. tclass_fields are divided up into variables (physical), properties (non physical), functions, and dynamic functions. This means that areas such as generating mark and visit functions only need to iterate over the variables which makes the code much easier to follow. Previously it was iterating over all fields and needing to filter out dynamic functions, non physical fields, etc.

This division works really well and there is no case where parts of the generator needs to then further filter the lists which is quite satisfying. When these fields get split up we also handle various hxcpp implementation details. E.g. dynamic functions will put a variable into othe variable list, abstract functions will place a no-op function into the function list, and variables with default functions will place a dynamic function into the dynamic function list. These means a lot of the edge cases which were handled multiple times in various places in the generator are now handled once at retyping which, again, simplifies the generators code.

Some stuff such as class Ids and interface slots are now assigned during retyping which means the context is now more immutable.

There was more I was thinking of doing such as the retyped classes containg tcpp expressions and types but this has been going on for quite a while now and I think its good enough. I've manually ran the hxcpp test suite and it all passes and I've ran some of the tests with the generational GC and that still works.

@Aidan63
Copy link
Contributor Author

Aidan63 commented Nov 19, 2024

Figured it out, it was a GC related issue in the end.

Sub classes which contained no GC fields but did inherit from a parent which does contain GC fields was allocating itself with the container flag set to false.
The retyped classes now contain more information on the container status of a class so it will correctly allocate classes with the container flag and only override mark and visit if the current class has GC fields.

@Aidan63 Aidan63 marked this pull request as ready for review November 19, 2024 22:09
@Aidan63
Copy link
Contributor Author

Aidan63 commented Nov 29, 2024

Didn't expect to do any more but I've updated the main retyper to pass around a context object which stores the previous ref based state.
Had an idea improving my existing callable PR which would require some info from the retyping stage and having this retyping context object opens that up.

@Aidan63
Copy link
Contributor Author

Aidan63 commented Jan 2, 2025

This should also fix HaxeFoundation/hxcpp#1172

I noticed the inconsistent :: prefixes and fixed a few when first doing this refactor but I missed the one in that issue. I've just done another sweep and seem to have corrected all Dynamic, Array, and String references to prefix :: since they're the main top level hxcpp types.

@Aidan63
Copy link
Contributor Author

Aidan63 commented Feb 3, 2025

@Simn, @hughsando thoughts on getting this merge in? I've just about finished another branch which builds on top of this (features this time, not more refactoring!) so would be good to get this one in so the diff on the PR isn't a nightmare.

@Simn
Copy link
Member

Simn commented Feb 4, 2025

This looks much tamer than the previous refactoring, so I'm fine with it.

@Aidan63
Copy link
Contributor Author

Aidan63 commented Feb 5, 2025

@Simn Hugh has given the thumbs up, and since it seems to be The Day of Merges...

@Simn Simn merged commit d2af500 into HaxeFoundation:development Feb 5, 2025
58 checks passed
@Aidan63 Aidan63 deleted the gencpp_rework_mkii branch February 5, 2025 19:06
@skial skial mentioned this pull request Feb 6, 2025
1 task
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.

2 participants