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

fix #12150 C++ free functions are now supported using new importcpp:"!$1" syntax #15653

Conversation

timotheecour
Copy link
Member

fix #12150

example

proc freeFn(a: cint) {.importcpp: "!$1".}
# maps to `void freeFn(int)`

@timotheecour timotheecour changed the title fix #12150 importcpp:"!$1" works with free functions fix #12150 C++ free functions are now supported using new importcpp:"!$1" syntax Oct 20, 2020
@timotheecour timotheecour force-pushed the pr_fix_importcpp_D20201019T210713 branch from 3ac73a6 to 2f63f57 Compare October 20, 2020 07:30
Copy link
Contributor

@Clyybber Clyybber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be handled in ccgcalls.genPatternCall instead.

compiler/ast.nim Show resolved Hide resolved
@timotheecour
Copy link
Member Author

timotheecour commented Oct 20, 2020

This should be handled in ccgcalls.genPatternCall instead.

this wouldn't help with #12150, try it; one problem was that sfInfixCall inhibited emitting a declaration in client code that calls importcpp on a free function

@Varriount
Copy link
Contributor

Why doesn't importc work for this?

@timotheecour
Copy link
Member Author

Why doesn't importc work for this?

because it would use c mangling. If you have a C++ free function, (eg: void fun(int a);), this would mangle differently.

@Araq
Copy link
Member

Araq commented Oct 21, 2020

this wouldn't help with #12150, try it; one problem was that sfInfixCall inhibited emitting a declaration in client code that calls importcpp on a free function

Please explain this in more detail.

@timotheecour
Copy link
Member Author

@Araq this line should explain everything:

if sfInfixCall notin prc.flags: genProcPrototype(m, prc)

in the example of #12150, fun1 is an importcpp proc therefore before PR has sfInfixCall in prc.flags, so genProcPrototype isn't called, and fun1 isn't declared in t0725.nim, hence giving error: use of undeclared identifier 'fun1'

after PR, I distinguish C++ free functions (which need a declaration) vs methods (sfInfixCall), which don't.

@Araq
Copy link
Member

Araq commented Oct 23, 2020

Ok, but I meant to ask:

This should be handled in ccgcalls.genPatternCall instead.

Why can't it be done in genPatternCall? Seems the most obvious place to put it.

@timotheecour
Copy link
Member Author

timotheecour commented Oct 23, 2020

I don't see how that would work, at very least I don't see how it would be simpler than the way I wrote it, if you have a concrete idea in mind please provide more details / edit this PR.

I need to tell whether an importcpp symbol is sfInfixCall or not early on (inside processImportCpp) since it affects semantics/codegen in a few places, so moving the following lines to genPatternCall adds complexity

let isFreeFunction = extname.startsWith "!"
  if isFreeFunction:
    extname = extname[1..^1]
  else:
    incl(s.flags, sfInfixCall)

and interface of genPatternCall doesn't fit well here

@timotheecour timotheecour force-pushed the pr_fix_importcpp_D20201019T210713 branch from e60c014 to df242a0 Compare November 1, 2020 07:44
@timotheecour
Copy link
Member Author

timotheecour commented Nov 1, 2020

Hi @Araq anything else needed here? I rebased to fix conflicts

@timotheecour
Copy link
Member Author

rebased again to fix bitrot conflicts... I'd really like to have this merged

@timotheecour
Copy link
Member Author

ping @xflywind

Copy link
Member

@ringabout ringabout left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

doc/manual.rst Outdated Show resolved Hide resolved
@timotheecour timotheecour force-pushed the pr_fix_importcpp_D20201019T210713 branch from 7759b03 to 18dc326 Compare March 6, 2021 08:33
Copy link
Member Author

@timotheecour timotheecour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PTAL

@timotheecour
Copy link
Member Author

@Araq any other comments or can we merge this?

@Araq
Copy link
Member

Araq commented Mar 11, 2021

Sorry for being late but if we really need this, we should have a dedicated importCppProc pragma instead of more magic going on in the import pattern.

@timotheecour
Copy link
Member Author

timotheecour commented Jul 2, 2021

maybe we need {.pattern: .} instead of this PR, which would be more general (work in c,cpp,js backends), refs nim-lang/RFCs#315 (comment)

eg:

proc freeFn(a: cint) {.pattern: "$call".} # whole call; honors backend mangling (c/c++/js)
proc freeFn2(a, b: cint) {.pattern: "std::bar(@)".} # calls std::bar(a, b)
proc `+`*(a, b: int128): int128 {.pattern: "$1 + $2".} # calls a + b
proc `+!`*(a, b: int128): int128 {.pattern: "$2 + $1".}  # calls b + a
proc freeFn3(a, b: cint) {.pattern: "$0(1, @)".} # calls freeFn3(1, a, b)

@Araq
Copy link
Member

Araq commented Mar 26, 2022

Either importCppProc or pattern would be better. Closing for now.

@Araq Araq closed this Mar 26, 2022
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

Successfully merging this pull request may close these issues.

importcpp does not work with C++ free functions (works with C++ methods)
5 participants