-
Notifications
You must be signed in to change notification settings - Fork 48
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
Test Parser/sizeof_expr_ok fails #44
Comments
parsing There are basically 2 ways to use I think both uses should be valid, so the compiler will have to deal with this situation. |
added to wiki/Roadmap |
Another issue reading the code is that it's unclear (when I read the code) what information is encoded in the AST during different analyzer passes. Reading the code it looks like just having sizeof triggers type resolution at some places, which is a bit hairy. Sizeof is basically a very early compile-time macro resolution, and this problem will be greater with macros. As a first step, could you document what each analyzer does and how they are layered? |
Basically on the parsing pass (when creating the AST), all information is stored that is needed for analysis. This might seem obvious, so an example:
Something similar is done for all expressions. Since the type is not known at the moment of AST creation, it is set during analysis. The setting of the type and the resolving of IdentifierExpr's are the |
Can we some document outlining all passes on the AST and what's resolved / not resolved after each pass? Maybe introduce an explicit state so that we can assert early. For example, if we are in a state that should have resolved all literals, then we add plenty of more asserts throughout the code. Because currently some unfinished code actually leaves parts of the AST unresolved, which causes bugs further down the line. When untangling the issue it's not clear where the problem originated. Perhaps the wiki could just add a very simple explanation like x. XXXXXAnalysis "does this and that" |
I have some code where the state of the file analysis class is very clearly set with assertions to show what code can be called when. It's in a branch that depends on the "no_clang" changes though. |
For the sizeof problem, we should actually kick that over a standard "typeOf" expression, that will resolve the ambiguity:
Here again it might be better to use a prefix for compile time resolution so it's clear:
|
Another way would be to use |
Yes that would solve that issue, but is not really nice to look at. We could disallow the use of a variable inside sizeof, so you could only use sizeof(Type). I think I would prefer that to the type keyword... |
I opened #69 to decide whether this should be trivially solved by the naming rules. It depends on whether you want to backpedal on naming rules later on, but even then it should be fairly straightforward to put in an analyser. |
Also, I think that sizeof might want to return the size of any expression. So:
Should all be fine and inlined directly. I did some exploratory coding and it seemed to resolve itself in a fairly straightforward way. Something that has been nagging me is whether types should be first class entities. That is, should you be able to pass around a type and get information about it during runtime. If not, what about compile time? |
Apparently the above works fine in C, so I'll implement it that way. |
Ok, found another ambiguity, which is very bad:
Here it is not clear if we mean the size of the field a in foo, or if we refer to the function Foo.a. All of these "type or expression" are going to be rather hard to parse in this manner since they are deeply ambiguous. (And people will curse us badly when they try to parse it for an IDE or some other reason). There are a couple of solutions that spring to mind:
|
From what I understand Clang has a concept of "unevaluated expressions". Evaluating sizeof basically pushes the tokens there into a blob to be parsed later. Sort of (1). This seems to add quite a bit of complexity and added functionality:
Because C2 has
There is one thing that's not quite resolved though. Consider
|
Digging a bit further I now see that in C |
The test has this:
This fails (correctly) since a is not a type (
sizeof(i32*)
succeeds as expected). However, one might have expected this to be a typo, where we wanted&a
instead ofa*
. Sadlysizeof(&a)
does not seem to work.Also, there's a very valid case of this code:
This does not work either. In the &a case, '&' is probably incorrectly parsed by C2Parser::ParseSizeof, but the * case is more complex. Obviously we can't do a simple "replace x by its type" when resolving types, because that would allow the analyser to happily parse
x* a = 23
as equal toi32 a = 32
!Unfortunately I don't know the code sufficiently well to offer a fix that I'm comfortable with.
The text was updated successfully, but these errors were encountered: