-
Notifications
You must be signed in to change notification settings - Fork 261
Add pointers deduced types #196
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
Add pointers deduced types #196
Conversation
9323c7b
to
5c7dff1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, I think if we merge this the work maybe should happen later in sema.h
(see comment in the review). But at the moment I'm leaning toward saying that maybe I'd rather leave this until the Lifetime static analysis is implemented, which will do a lot of this Pointer type deduction and state tracking... and that won't be in the near term (sorry!).
I appreciate all the work on this, but I think the best case is that this PR probably should be updated to move some of the analysis to sema, and at that it will be a placeholder that will be replaced... that may limit the amount of additional time you want to put into it at this stage.
Again, sorry, I know this was a lot of work and there are good ideas here, and thanks for understanding!
errors.emplace_back( | ||
op->position(), | ||
op->to_string(true) + " - pointer bitwise manipulation is illegal - use std::bit_cast to convert to raw bytes first" | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't had the cycles to really grok this, and the main reason I'm reading through the PR is because (a) the test case at top is clear and (b) the code change is mostly localized here, both of which are strengths (thanks!). That also limits the downside because it should be very local.
I'm not yet sure whether I should merge this... In the long term I aim to implement my C++ Core Guidelines Lifetime analysis, and that will likely replace at least parts of this work. But if we do merge this now, I still view it as an interim '[expanded] initial partial implementation' and don't think we would want to go further in this direction in the near term. First we would want to implement the Lifetime analysis and then see where we are, and the Lifetime implementation itself needs to be farther in the future after classes and metaclass functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main change here is about calling a new function is_pointer_declaration(unqual->identifier)
and to check not only the operator that is called on the node but also the results of the function call (e.g. n()++
)
@hsutter Do you think I can try to start implementing a Lifetime static analysis? Or maybe I should try to move pointer reduction to sema? Or do you see anything else I can work on that will support you? |
Pointers are now deduced after multiple dereferences mixed with address of operators. If the type is defined in cpp2 it should be deduced. Pointer types are also deduced when returned from functions. Deducing of doesn't work for parameter_declaration_list_node. This change change `sema::get_local_declaration_of` to `sema::get_declaration_of` that takes additional parameter that makes it work locally or globally.
Current limitiation is that when deducing pointers from functions the order of the functions matters - the deduced functions needs to be before deduced part of code.
5c7dff1
to
d62b93e
Compare
Thanks. On reviewing this, I think maybe it's okay not to move this to sema -- this would be a (useful) placeholder, but something that would be replaced in the future and is pretty localized now. So I would lean toward taking this pretty much as is, and not trying to go further on this path for now. I appreciate the offer for implementing Lifetime, but that will go best after classes are done because parts of Lifetime depend on classes. Probably similar with most other feature extension PRs -- I think the most useful thing will be for me to focus on classes in the coming few months, and in the meantime if you can focus on letting me know about any bug reports and repros that you find that would be useful (thanks again for those and please send those anytime!), and then after classes are in and stable it would be time to look at new/extended features again. |
@@ -0,0 +1,12 @@ | |||
pure2-deducing-pointers.cpp2... | |||
pure2-deducing-pointers.cpp2(14,8): error: = - pointer assignment from null or integer is illegal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for including a new regression test.
Minor naming thing that I can clean up post-merge: Tests that are intended to fail cppfront compilation (not generate a .cpp file) should have a name that ends in ...error.cpp2
. That way my super-simple run-tests.bat
test runner will count it as not intending to to create a .cpp
and not flag a mismatch error. I'll take care of this post-merge though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right... will correct that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see that you have merged that. Should I prepare another PR with the name change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way. Thank you for merging that. Appreciate it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, I did in another commit. All's well, thanks!
* Add deducing pointer types Pointers are now deduced after multiple dereferences mixed with address of operators. If the type is defined in cpp2 it should be deduced. Pointer types are also deduced when returned from functions. Deducing of doesn't work for parameter_declaration_list_node. This change change `sema::get_local_declaration_of` to `sema::get_declaration_of` that takes additional parameter that makes it work locally or globally. * Add tests for deducing pointers Current limitiation is that when deducing pointers from functions the order of the functions matters - the deduced functions needs to be before deduced part of code. * Add comments to the code
The current implementation has safety guarantees only for explicit pointers declaration. If the pointer type is deduced, cppfront does not provide a safety check.
This change introduces pointer type deduction for types defined in cpp2. After this change cppfront will process below code
And will generate errors:
Unfortunately, this implementation does not deduce pointer types from functions declared after the analyzed code. That means that defining
fun
andfun2
functions after themain
will block deducing pointer type for linesThis implementation also does not deduce pointer types returned as
parameter_declaration_list_node
This replaces: #93 & #94