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

Codegen error: tuple of emitted C++ class appears before class declaration #15244

Closed
deech opened this issue Aug 29, 2020 · 2 comments
Closed

Comments

@deech
Copy link
Contributor

deech commented Aug 29, 2020

Tuple struct is generated before the emitted class resulting in a compile error from the C++ compiler.

Example

{.emit: """ /*INCLUDESECTION*/
#include <stdio.h>
""".}

{.emit: """ /*TYPESECTION*/
class C {
public:
  C();
  ~C();
  void whatever() { puts("whatever"); };
};
""".}

type
  C* {.importcpp:"C".} = object

proc makeC():ptr C {.importcpp: "new C()".}
proc whatever(c:ptr C){.importcpp: "#.whatever()"}

proc cTuple():(ptr C, ptr C) =
  result = (makeC(),makeC())

let (c1,c2) = cTuple()
c1.whatever()
c2.whatever()

Current Output

....CC: classtuple.nim
/home/deech/.cache/nim/classtuple_d/@mclasstuple.nim.cpp:44:1: error: ‘C’ does not name a type
   44 | C* Field0;
      | ^
/home/deech/.cache/nim/classtuple_d/@mclasstuple.nim.cpp:45:1: error: ‘C’ does not name a type
   45 | C* Field1;
      | ^
/home/deech/Nim/interop/classtuple.nim: In function ‘tyTuple__4K8uZbmB6yl2t1XJqL11Qg cTuple__oTOQqabzGbHm6A9agZwqIQw()’:
/home/deech/Nim/interop/classtuple.nim:20:9: error: ‘struct tyTuple__4K8uZbmB6yl2t1XJqL11Qg’ has no member named ‘Field0’
   20 |   result = (makeC(),makeC())
      |         ^~~~~~
compilation terminated due to -fmax-errors=3.

Expected Output

Successful compilation

Possible Solution

The generated output shows the tuple struct is declared ahead of the class declaration causing the problem.

...
/* section: NIM_merge_TYPES */
struct tyTuple__4K8uZbmB6yl2t1XJqL11Qg {
C* Field0;
C* Field1;
};

...

 /*TYPESECTION*/
class C {
public:
  C();
  ~C();
  void whatever() { puts("whatever"); };
};
$ nim -v
Nim Compiler Version 1.3.5 [Linux: amd64]
Compiled at 2020-08-28
Copyright (c) 2006-2020 by Andreas Rumpf

git hash: 13e659cfec83eb3c2c3c2bbbf10d01ba59bc0d5b
active boot switches: -d:release
@Araq
Copy link
Member

Araq commented Aug 31, 2020

Try this please (note the lack of spaces):

{.emit: """/*INCLUDESECTION*/
#include <stdio.h>
""".}

{.emit: """/*TYPESECTION*/
class C {
public:
  C();
  ~C();
  void whatever() { puts("whatever"); };
};
""".}

type
  C* {.importcpp:"C".} = object

proc makeC():ptr C {.importcpp: "new C()".}
proc whatever(c:ptr C){.importcpp: "#.whatever()"}

proc cTuple():(ptr C, ptr C) =
  result = (makeC(),makeC())

let (c1,c2) = cTuple()
c1.whatever()
c2.whatever()

@deech
Copy link
Contributor Author

deech commented Aug 31, 2020

This worked. Thanks!

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