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

importcpp types don't work well at module scope #14389

Open
timotheecour opened this issue May 18, 2020 · 0 comments
Open

importcpp types don't work well at module scope #14389

timotheecour opened this issue May 18, 2020 · 0 comments

Comments

@timotheecour
Copy link
Member

  • requiresInit should forbid (during sempass) var a: A or var a = default(A) if A is a C++ struct/class with no default constructor; but currently it only gives a cgen error; that's the lower priority bug since the code is invalid anyway
  • the more annoying bug is that var a = A(args) doesn't work at module scope due to current cgen implementation

Example

{.emit:"""
#include <stdio.h>
struct A{
  A(int a){ printf("ctor\n"); }
  ~A(){ printf("dtor\n"); }
};
""".}
type A {.importcpp: "A", requiresInit.} = object
proc initA(a: cint): A {.importcpp: "A(@)", constructor.}

proc fun() =
  var a1 = initA(12) # ok: this works
  # var a2: A # BUG: this should give semphase error but give cgen error
  # var a3 = default(A) # BUG: ditto
fun()

var a4 = initA(12) # BUG: this doesn't work but should

Current Output

@mt10772.nim.cpp:63:17: error: no matching constructor for initialization of 'A'

Expected Output

works

Possible Solution

cgen should instead use pointers for all global importcpp variables (regardless of whether they have default ctor or not):

instead of generating:

/* section: NIM_merge_VARS */
N_LIB_PRIVATE A a__kEcttXN5qSDQ71dflcCVww;
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void) {{
  a__kEcttXN5qSDQ71dflcCVww = A(((int) 12));
}}

the compiler should generate:

/* section: NIM_merge_VARS */
N_LIB_PRIVATE A* a__kEcttXN5qSDQ71dflcCVww;
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void) {{
  *a__kEcttXN5qSDQ71dflcCVww = A(((int) 12));
}}

Additional Information

markspanbroek added a commit to markspanbroek/nim-ili9341 that referenced this issue Feb 26, 2021
These statements no longer cause an error at the module scope:
let tft = initIli9321(9, 10)

Introduces a C++ subclass with a default constructor.
Workaround for nim-lang/Nim#14389
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

1 participant