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

genSym'ed proc name + exportc = invalid code #20911

Open
Menduist opened this issue Nov 24, 2022 · 6 comments
Open

genSym'ed proc name + exportc = invalid code #20911

Menduist opened this issue Nov 24, 2022 · 6 comments

Comments

@Menduist
Copy link
Contributor

Menduist commented Nov 24, 2022

What happened?

import macros
macro xx: untyped =
  result = newProc(genSym(nskProc))
  result.addPragma(newIdentNode("exportc"))
xx()

Nim Version

devel
d73fff1

Current Standard Output Logs

In file included from /home/tavon/.cache/nim/test_d/@mtest.nim.c:7:
/home/tavon/.cache/nim/test_d/@mtest.nim.c:31:33: error: expected identifier or ‘(’ before ‘:’ token
   31 |   N_LIB_PRIVATE N_NIMCALL(void, :tmp)(void);
      |                                 ^
/home/tavon/.choosenim/toolchains/nim-#devel/lib/nimbase.h:257:44: note: in definition of macro ‘N_NIMCALL’
  257 | #  define N_NIMCALL(rettype, name) rettype name /* no modifier */
      |                                            ^~~~
/home/tavon/.cache/nim/test_d/@mtest.nim.c:67:31: error: expected identifier or ‘(’ before ‘:’ token
   67 | N_LIB_PRIVATE N_NIMCALL(void, :tmp)(void) {
      |                               ^
/home/tavon/.choosenim/toolchains/nim-#devel/lib/nimbase.h:257:44: note: in definition of macro ‘N_NIMCALL’
  257 | #  define N_NIMCALL(rettype, name) rettype name /* no modifier */
      |                                            ^~~~
Error: execution of an external compiler program 'gcc -c  -w -fmax-errors=3 -pthread   -I'/home/tavon/.choosenim/toolchains/nim-#devel/lib' -I/tmp -o /home/tavon/.cache/nim/test_d/@mtest.nim.c.o /home/tavon/.cache/nim/test_d/@mtest.nim.c' failed with exit code: 1

Expected Standard Output Logs

works

Possible Solution

No response

Additional Information

No response

@Araq
Copy link
Member

Araq commented Nov 25, 2022

What should be done?

@Menduist
Copy link
Contributor Author

Menduist commented Nov 25, 2022

Not sure, my use case is that I don't care about the proc name, I just want to save it somewhere to be able to call it
So maybe genSym should provide the mangled name, instead of :tmp, that gets mangled & unique-ified later?
Or exportc with no parameter should export the post mangle name, not pre-mangled

@Araq
Copy link
Member

Araq commented Nov 25, 2022

Well since you export the name, you cannot leave the name to the compiler.

@Menduist
Copy link
Contributor Author

The compiler can generate whatever it wants, as long as I can retrieve its final form in the macro & it's valid

@Araq
Copy link
Member

Araq commented Nov 25, 2022

Why do you use exportc when you don't care about the name?

@Menduist
Copy link
Contributor Author

I use a system which can call a C procedure if I give it the procedure name.
So I use it like this (pseudocode):

macro spawn(body: untyped) =
  let
    name = genSym(nskProc)
    prc = newProc(name, body = body)
  prc.addPragma(newIdentNode("exportc"))
  newStmtList(
    prc,
    newCall("magicCaller", newStrLitNode(name.strVal))
  )

spawn: echo "heya"

So I don't care about the name, I just need to be able to give it to the magicCaller in its final form.
As a workaround of this issue, I just used a random string generator instead of the genSym, and that works fine. But less elegant :)

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

No branches or pull requests

2 participants