-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
emit("here"): "c code"
+ other emit fixes; new module experimental/backendutils.nim
#13953
Conversation
emit(here): "c code"
+ other emit fixes; new module experimental/backendutils.nimemit("here"): "c code"
+ other emit fixes; new module experimental/backendutils.nim
emit("here"): "c code"
+ other emit fixes; new module experimental/backendutils.nimemit("here"): "c code"
+ other emit fixes; new module experimental/backendutils.nim
emit("here"): "c code"
+ other emit fixes; new module experimental/backendutils.nimemit("here"): "c code"
+ other emit fixes; new module experimental/backendutils.nim
I have an idea and would like to hear your opinion about it: I personally don't like the pragma notation for emit. I also don't like multi line string literals. So this is my idea to avoid it: template c_sizeof*(T: typedesc): int =
var s: int
# the symbols s and T are captured
asm(efHere, [s, T]):
s = sizeof(T); // remember, this is C code without quotes in Nim code
s This is how it can work: I use the The reason for |
I've opened nim-lang/RFCs#210 to followup on your proposal, 1 part of your proposal is good and should be implemented, see my comments there; that being said it's orthogonal to this PR (in the sense that either/both can be implemented). |
Why do we need even more |
On the contrary, I view the existing semantics as a hack: as @krux02 said here #13943 (comment)
Furthermore, this PR enables implementing things like a correct |
While I understand it's necessity, using |
That would be a breaking change. It would not only break all wrappers that wrap C macros it would also make it impossible to fix them. Of course I am not telling you something new. I just want to know why you want so desperately to break this. |
Well no, rejected. Since |
that makes no sense for code that needs to use it, eg code that is interop heavy. |
It's not "crippled", it's simply not as typo-safe as you would like. |
But I have real use cases for |
What's the real use case? This needs to be justfiied way better and also should be backed up by an accepted RFC. |
thank you for re-opening. I will will follow-up with an RFC, but that will take some time. |
This pull request has been automatically marked as stale because it has not had recent activity. If you think it is still a valid PR, please rebase it on the latest devel; otherwise it will be closed. Thank you for your contributions. |
No RFC has been written. We can always re-open it later. |
fixes A3,A4,A5 from #13943, adds a sane
emit
syntax, and new experimental moduleexperimental/backendutils.nim
P1
new emit syntax:
emit("typeSection"): "some C code"
this replaces the very hacky
emit: "/TYPESECTION*/some C code"
which continues to work but is now discouraged and will eventually be deprecated (with a warning). As mentioned by @krux02 #13943 (comment)The new API is safe and will give CT error if section is invalid.
(also avoids silent errors like #15244 (comment) where a missing space caused obscure error)
P2
new emit section:
emit("here"): "some C code emitted here"
Since nim code can appear anywhere including at module scope, it makes sense to allow emitting "right here" instead of at some file section.
As an example use case, this enables writing a correct
c_sizeof
, ie, fixing Example 3 from #13943, so that it works regardless of where it's called from (top-level, block-level or proc-level)See tests for more examples.
P3
emit at block scope now uses section=here instead of being treated as module scope.
This fixes example 2 from #13943
P4
new experimental module
experimental/backendutils.nim
This experimental module enables introspecting the generated file (eg C,C++), for tests, debugging, and user code.
P5
see tests
tests/stdlib/tbackendutils.nim
which has tests for everythingP6
new syntax for pragmas:
{.foo(args): baz.}
is now allowed, and rewritten as:{.foo(args, baz).}
(where args represents 1 or more arguments).This enables the
but is generally useful and matches the way the rest of nim syntax works
after PR