nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.58k stars 1.47k forks source link

importcpp generates @ literally #4055

Open vegansk opened 8 years ago

vegansk commented 8 years ago

This was a working example of FFI with Qt. And now it is broken on devel. config.nims:

mode = ScriptMode.Verbose

task build, "Build the app":
  let cflags = staticExec"pkg-config --cflags Qt5Core" & "-fPIC"
  let libs = staticExec"pkg-config --libs Qt5Core"
  switch "passC", cflags
  switch "passL", libs
  setCommand "cpp", "qtapp"

qtapp.nim

import unittest

type
  QByteArray {.importcpp: "QByteArray", header: "QByteArray".} = object
  QString {.importcpp: "QString", header: "QString".} = object

####################################################################################################
# QByteArray
proc initQByteArray(): QByteArray {.importcpp: "QByteArray(@)", constructor.}
proc initQByteArray(data: cstring, size: cint = -1): QByteArray {.importcpp: "QByteArray(@)", constructor.}
proc constData(this: QByteArray): cstring {.importcpp: "constData".}

####################################################################################################
# QString
proc initQString(): QString {.importcpp: "QString(@)", constructor.}
proc initQString(s: cstring = ""): QString {.importcpp: "QString(@)", constructor.}
proc initQString(ba: QByteArray): QString {.importcpp: "QString(@)", constructor.}
proc toUtf8(this: QString): QByteArray {.importcpp: "toUtf8".}
proc fromUtf8(s: cstring): QString {.importcpp: "QString::fromUtf8(@)".}
proc `$`(s: QString): string = $s.toUtf8.constData

suite "Qt tests":

  test "QString":
    let s = "Hello, world!".initQString
    echo s
    check: $s == "Hello, world!"

The error is:

Error: execution of an external compiler program 'g++ -c  -I/usr/include/qt5/QtCore -I/usr/include/qt5  -fPIC -w -w -fpermissive  -I/home/vega/work/nim/Nim/lib -o /home/vega/work/nim/nimtests/ffi/qt/nimcache/nimtests_qtapp.o /home/vega/work/nim/nimtests/ffi/qt/nimcache/nimtests_qtapp.cpp' failed with exit code: 256

In file included from /home/vega/work/nim/nimtests/ffi/qt/nimcache/nimtests_qtapp.cpp:9:0:
/home/vega/work/nim/nimtests/ffi/qt/nimcache/nimtests_qtapp.cpp:200:28: error: stray ‘@’ in program
 N_NIMCALL(QString, QString(@))(NCSTRING s);
                            ^
/home/vega/work/nim/Nim/lib/nimbase.h:215:44: note: in definition of macro ‘N_NIMCALL’
 #  define N_NIMCALL(rettype, name) rettype name /* no modifier */
                                            ^
/home/vega/work/nim/nimtests/ffi/qt/nimcache/nimtests_qtapp.cpp:200:42: error: ‘QString’ declared as function returning a function
 N_NIMCALL(QString, QString(@))(NCSTRING s);
                                          ^
vegansk commented 8 years ago

The problem disappears if I add nodecl or header pragma