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

Backticked vars misinterpreted by quote do: when creating pragmas #9745

Closed
genotrance opened this issue Nov 18, 2018 · 6 comments
Closed

Backticked vars misinterpreted by quote do: when creating pragmas #9745

genotrance opened this issue Nov 18, 2018 · 6 comments

Comments

@genotrance
Copy link
Contributor

I'm using macros to create types with quote do: but added pragmas are not working correctly since backticked variables are interpreted incorrectly.

Example

import macros

var
  typ {.compiletime.} = "struct ABC"
  name {.compiletime.} = "ABC".newIdentNode()

macro abc(): untyped =
  result = newNimNode(nnkStmtList)

  result.add(quote do:
    type `name` {.importc: `typ`.} = object
  )

  echo result.repr

abc()

Current Output

type
  ABC {.importc: 3.} = object

Expected Output

type
  ABC {.importc: "struct ABC".} = object

Note that name is interpreted correctly, only contents in side the pragma section are misinterpreted. If you add more elements - e.g. header or something else, you get numbers again.

@narimiran
Copy link
Member

I can reproduce.


Until this is fixed/resolved, here is a quick-n-dirty (-n-ugly ;)) version made by using dumpAstGen which works.

import macros

var
  typ {.compiletime.} = "struct ABC"
  name {.compiletime.} = "ABC".newIdentNode()

macro abc(): untyped =
  result = nnkStmtList.newTree(
    nnkTypeSection.newTree(
      nnkTypeDef.newTree(
        nnkPragmaExpr.newTree(
          name,
          nnkPragma.newTree(
            nnkExprColonExpr.newTree(
              newIdentNode("importc"),
              newLit(typ)
            )
          )
        ),
        newEmptyNode(),
        nnkObjectTy.newTree(
          newEmptyNode(),
          newEmptyNode(),
          newEmptyNode()
        )
      )
    )
  )
  echo result.repr

abc()

@genotrance
Copy link
Contributor Author

Thanks for sharing. If quote do: is unreliable, is parseStmt() better? Makes the code readable. That being said, since this is a library I'm working on, I want reliable interpretation so let me know if writing full trees is worth it.

@alehander92
Copy link
Contributor

https://github.com/alehander42/breeze this might make it a bit easier to build trees too.

@genotrance
Copy link
Contributor Author

Thanks I'll check it out. For now I'm using parseStmt because it is a bit more readable than building full trees.

@mratsim
Copy link
Collaborator

mratsim commented Nov 23, 2018

I've also noticed that the symbol pass for importc and exportc is done very early. For example it is done before template are expanded here: #9366.

@krux02
Copy link
Contributor

krux02 commented Jul 23, 2019

current output:

type
  ABC {.importc: "struct ABC".} = object

@krux02 krux02 closed this as completed Jul 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants