Giter VIP home page Giter VIP logo

c2nim's People

Contributors

andreaferretti avatar angluca avatar araq avatar bigepsilon avatar bumblingcoder avatar cdunn2001 avatar clyybber avatar data-man avatar dom96 avatar elcritch avatar flaviut avatar foldl avatar fowlmouth avatar garettbass avatar genotrance avatar gmpreussner avatar julienaubert avatar menduist avatar narimiran avatar nc-x avatar nouredineh avatar planetis-m avatar ratiotile avatar reactormonk avatar ringabout avatar royneary avatar rsduck avatar tmm1 avatar yglukhov avatar zielmicha avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

c2nim's Issues

nimble can't install c2nim for latest stable nim compiler (v0.10.2)

Commit 1625c5ab77 changed the nim compiler dependency in c2nim.nimble to >=0.10.3, which currently specifies the development/unstable branch of nim. The impact of this is that anyone with the latest stable nim release (version 0.10.2) cannot install c2nim using nimble because the package's compiler dependency is violated.

According to my understanding of the way nimble works, the way to address this problem is to tag the c2nim commit immediately preceding 1625c5ab77, which I believe is ffdb350b4f, where the compiler dependency is >=0.10.2. Since that would be the only tag for c2nim, nimble would pick up that version by default. Anyone wishing to pick up the latest c2nim commit would still be able to do so by telling nimble to install from HEAD.

Add translation-time calculated `#ifdef`'s

Just as c2nim has #def, which acts much like a preprocessor's #define, it would be convenient for c2nim to have a #ifdef which acts much like a preprocessor's.

In my particular case, I'm translating headers which have #ifdef sections that either contain c++ code, or non-standard C syntax extensions, which cause c2nim to error when translating. It's nontrivial to edit these sections away, as these header files can have 100+ instances.

Bad mapping for nested types

class Foo {
  public: struct Bar {
  };
};

produces the output

type 
  Foo* = object 


  type 
    Bar* = object 

Compiling this with nim cpp foo.nim yields:

foo.nim(5, 6) Error: identifier expected, but found 'keyword type'

long const int * parameter not parsed

C2Nim can not parse
void cdCanvasPattern(cdCanvas* canvas, int w, int h, long const int *pattern);

output error is: Error: ')' expected

but removing the const allows c2nim to parse the statement:
void cdCanvasPattern(cdCanvas* canvas, int w, int h, long int *pattern);

nim compiler version?

Trying to install using nimble:
nimble install c2nim
I got the error:
Downloading git://github.com/nim-lang/c2nim into /tmp/nimble_24998/githubcom_nimlangc2nim using git...
Initialized empty Git repository in /tmp/nimble_24998/githubcom_nimlangc2nim/.git/
remote: Counting objects: 38, done.
remote: Compressing objects: 100% (34/34), done.
remote: Total 38 (delta 0), reused 18 (delta 0), pack-reused 0
Unpacking objects: 100% (38/38), done.
From git://github.com/nim-lang/c2nim

  • branch master -> FETCH_HEAD
  • [new branch] master -> origin/master
    HEAD is now at 4ed003b fixes #37; implemented --assumeIfIsTrue
    Switched to a new branch 'origin/master'
    Looking for compiler (#devel)...
    None found, installing...
    Downloading https://github.com/Araq/Nim.git into /tmp/nimble_24998/githubcom_AraqNimgit_devel using git...
    Initialized empty Git repository in /tmp/nimble_24998/githubcom_AraqNimgit_devel/.git/
    remote: Counting objects: 1854, done.
    remote: Compressing objects: 100% (1757/1757), done.
    remote: Total 1854 (delta 41), reused 904 (delta 31), pack-reused 0
    Receiving objects: 100% (1854/1854), 3.15 MiB | 1.02 MiB/s, done.
    Resolving deltas: 100% (41/41), done.
    From https://github.com/Araq/Nim
  • branch devel -> FETCH_HEAD
  • [new branch] devel -> origin/devel
    HEAD is now at 3767763 Merge pull request #2968 from def-/neutral-language
    Switched to a new branch 'devel'
    Unsatisfied dependency: nim (>= 0.11.3)

What should I do to install c2nim?

My nim version is 0.11.2, the last released:
nim -ver
Nim Compiler Version 0.11.2 (2015-06-22) [FreeBSD: amd64]
Copyright (c) 2006-2015 by Andreas Rumpf

git hash: 45b6082c12dd6fc90a3dd3ca97e1ba157c3d6464
active boot switches: -d:release

#define for array initializer not handled

The following failed with Error: did not expect { in first line.

#define FOO { \
    0x1, 0x2, 0x3, 0x4,     \
    0x5, 0x6, 0x7, 0x8      \
}
static const uint8_t foobar[] = FOO;

ifndefs do not translate properly at all. Possibly a failed attempt to optimize out Include Once constructs

#define HOG

#ifndef HOG
#define HOG
//this HEG should not be defined, but it is
#define HEG
#endif

//if the first statement is not defining the input to the ifndef, the
//effect is interrupted and the construct is translated correctly
#ifndef HOG
#define HEG
#define HOG
#endif

//this will completely disappear, so if anything expects A to be defined later on,
//it will be disappointed.
#ifndef A
#define A
#endif

goes to

const 
  HOG* = true

#this HEG should not be defined, but it is

const 
  HEG* = true

#if the first statement is not defining the input to the ifndef, the
#effect is interrupted and the construct is translated correctly

when not defined(HOG): 
  const 
    HEG* = true
    HOG* = true

#this will completely disappear, so if anything expects A to be defined later on,
#it will be disappointed.

c2nim doesn't like using macro to declare imported symbol

I have a C/C++ header file that I would like to convert to nim. It uses __declspec (dllimport) to reference functions from a provided dll. The C code looks like this:

#ifndef LINUX
  #if !defined (AW_SDK) && !defined (AW_BROWSER) && !defined (AW_STATIC)
    #define AWAPI     __declspec (dllimport)
  #else
    #define AWAPI
  #endif
...

and later on in the code uses the AWAPI macro to declare each imported symbol:

AWAPI int   aw_sdk_build (void);
AWAPI int   aw_init (int build);
AWAPI int   aw_init_bind (int build, unsigned long Addr);

c2nim doesn't like doing this however, and complains:

aw.h(1330, 25) Error: ';' expected

The msdn documentation also recommends doing this: https://msdn.microsoft.com/en-us/library/8fskxacy.aspx
This shouldn't be too hard to support, right? We could, in theory, just use the {.importc: "____".}?

CPP Operator pointers

The following fails with
Error: identifier expected, but found '*'

for c2nim --cpp conversion of

class failClass1 {
public:
  int tmpI;
  operator MyOp1() { return tmpI; }
  operator MyOp2*() { return *tmpI; }   // <- this fails, for MyOp2*()
};

Unable to importc objc class hierarchy

Originally opened by @gradha on February 9th

The importc pragma works ok for single objc classes, but can't be used to model hierarchies of external classes. The following code illustrates in valid Nimrod code what I want to do:

block:
  {.passL: "-lobjc".}
  {.passL: "-framework AppKit".}

type
  TNSObject* = object of TObject
  PNSObject* = ref TNSObject

  TNSString* = object of TNSObject
  PNSString* = ref TNSString

proc check_isa*(x: PNSObject) =
  echo "The isa value is ", repr(x)

when isMainModule:
  var a: PNSString
  check_isa(a)

Essentially, this models the objc NSObject with NSString being a subclass. Now, trying to do the same with importc:

type
  TNSObject* {.importc: "NSObject", inheritable,
    header: """<Foundation/Foundation.h>""".} = object
  PNSObject* = ptr TNSObject

  TNSString* {.importc: "NSString".} = object of TNSObject
  PNSString* = ptr TNSString

Nimrod compiles fine but fails during backend compilation:

/private/tmp/cycle/nimcache/cycle_h.m:109:27: error: 'NSString' does not have a
      member named 'Sup'
        checkisa_80015(&a_80026->Sup);
                        ~~~~~~~  ^
/private/tmp/cycle/nimcache/cycle_h.m:115:17: error: application of 'sizeof' to
      interface 'NSObject' is not supported on this architecture and platform
NTI80007.size = sizeof(NSObject);
                ^     ~~~~~~~~~~
2 errors generated.
Error: execution of an external program failed

As far as I can see in the generated code, the compiler is generating code for the imported classes as if they were normal nimrod structs, hence the access of the Sup field or trying to initialize the structure with sizeof.

How could this be avoided? It seems that the only way to make something like this work is teach the codegen part about objc classes. If on the Nimrod side they can't be expressed as object of XXX then the only other solution I can see is adding converters for all types to their base types or manual casting between them.

Maybe this has been already solved for C++ interfacing?

unary defined operator

should be supported. only relevant in #if/#elif

#if defined ARRAY_SIZE
#define TABLE_SIZE ARRAY_SIZE
#elif !defined BUFFER_SIZE
#define TABLE_SIZE 128
#else
#define TABLE_SIZE BUFFER_SIZE
#endif 

Parameter names using reserved keywords generate bad nim code

The following header:

void *hoedown_realloc(void *ptr, int size) ;

will generate the following nim file:

proc hoedown_realloc*(ptr: pointer; size: cint): pointer

This won't compile because ptr is a reserved keyword. It can be solved by quoting the name or mangling it (say, add the letter c).

c2nim - Cast Syntax recognition error

c2nim doesn't like casts in #define statements, such as this one:

#define dhFreeString(string) SysFreeString((BSTR) string)

The above code will work if #define is changed to #def , but then no macro is generated.

This line was taken from the disphelper library header, which I am currently trying to wrap. The exact error is this:

c:\common\nimwrappers\disphelper_custom.h(73, 56) Error: ')' expected

bitfields not implemented

struct bits {
  int flag:1;
  int opts:4;
};
c2nim/testsuite/tests/bitfield.h(2, 13) Error: ';' expected

c2nim not marking structs as {.pure.}

C code:

struct Point {
    int x, y;
};

Converted Nim code:

type
  Point* = object
    x*: cint
    y*: cint

According to Nim Manual, an object type can be marked with the {.pure.} so that its type field which is used for RTTI is omitted. This used to be necessary for binary compatibility with other compiled languages.

I interpret the above sentence as: If you don't specify {.pure.}, the struct will not be ABI compatible with underlying C library, due to some RTTI features Nim might have.

Could you give me a reason, why {.pure.} is not added into converted Nim code? Or if it is necessary, could you add it?

Large numbers

I have the following defined in a header:

#define MPD_RADIX 10000000000000000000ULL /* 10**19 */

c2nim throws the following message:

Error: number 10000000000000000000 out of valid range

Update:
Seems this can be fixed by manually appending 'u64 when dealing with long long in C header files

c2nim output file contains trailing whitespace

There is an extra space after "type ", "const " and many other lines emitted into a c2nim generated .nim file.

It would be nice to omit these as they make some editors and source control systems fussy.

Fails on `++`

Thanks @BlaXpirit for reporting!

Expected AST (will have to be modified for i++ vs ++i):

StmtListExpr
  Call
    Ident !"inc"
    Ident !ident
  Empty
  Ident !"i"

Actual AST:

Call
  Ident !"inc"
  Ident !ident

c2nim fails on some typedefs

For example:

typedef LBER_INT_T ber_int_t;
is correctly parsed

typedef signed LBER_INT_T ber_sint_t;
Error: ';' expected

It seems that 'signed' is the cause.

Idem for:
typedef int* (*BER_ERRNO_FN) LDAP_P(( void ));

Issues with structure alignment macros (libovr)

I'm currently trying generate Nim bindings for the latest Oculus Rift SDK. Unfortunately the main header file makes heavy use of preprocessor macros. I'm currently stuck at handling this set of structure alignment macros. I was trying to define the following in my ifdef C2NIM block (and putting the above macro definitions in an ifndef C2NIM block):

#  def OVR_ALIGNAS(n) __attribute__((aligned(n)))

However I'm getting the following error in this code line:

.../lib/OVR_CAPI_0_5_0.h(276, 28) Error: '*' expected

Is there any workaround to this issue?

CPP function ptrs

function pointers in a Class fail to convert with error
Error: identifier expected, but found '*'
when converting (with --cpp)

class failClass {
public:
  void (*warning)(const char*, ...);    // <- this fails!!
  void *warning(const char*, ...);
};

define inside struct fails

struct test {
  int field;
#define SIZE 123
  int ary[SIZE];
}
c2nim/testsuite/tests/structdefine.h(3, 8) Error: identifier expected, but found '#'

Is this a valid pre-processor declaration?

Hi. Suppose I'm trying to wrap a test.h header file with this declaration (in the real header I'm trying to wrap, this is one of FMOD's reverb presets):

#define FMOD_PRESET_LIVINGROOM       {   500,    3,   4, 5000,  10, 100, 100, 250, 0,   160,  58, -19.0f }

If I try to translate using:

c2nim --debug test.h

I get this error:

opt/fmodstudioapi10804linux/api/lowlevel/inc/test.h(1, 45) Error: did not expect {

Now, if I turn the #define into a #def, c2nim will translate, but test.nim will be empty.

I'm trying to understand c2nim's documentation, but I'm not sure if I'm doing something wrong.

Any thoughts or suggestions are appreciated. Thanks!

add option to include common preamble

I'm using c2nim with ffmpeg, which contains 98 header files. I would like to avoid patching every file for c2nim consumption (and then have to maintain that patch), since most of the headers fail to parse with the same errors. For instance, all the headers include libavutil/attributes.h which defines macros like attribute_deprecated and av_const that are used throughout the rest of the code base:

attribute_deprecated void func();
int64_t av_const av_gcd(int64_t a, int64_t b);
void func2(int av_unused flags);

I'd like to be able to pass c2nim a file that it parses before parsing the supplied header:

$ cat > common.h <<EOS
#def av_unused
#def av_const
#def attribute_deprecated
EOS
$ c2nim --preamble common.h avcodec.h

Array error

//======== c code =========
const GLfloat diamond[4][2] = {
{ 0.0, 1.0 }, // Top point
{ 1.0, 0.0 }, // Right point
{ 0.0, -1.0 }, // Bottom point
{ -1.0, 0.0 } }; // Left point

//======== will be nim code =======
var cc: array[2, array[4, float]] = [[0.0, 1.0], [1.0, 0.0],
[0.0, - 1.0], [- 1.0, 0.0]]

hello.nim(7, 36) Error: type mismatch: got (Array constructor[0..3, Array constructor[0..1, float]]) but expected 'array[0..1, array[0..3, float]]'

c2nim - variable wrapped in parenthesis cannot be used with the arrow (->) operator

The following #define statement (taken from the disphelper library's main header) throws an error when run through c2nim:

#define SAFE_RELEASE(pObj) { if (pObj) { (pObj)->lpVtbl->Release(pObj); (pObj) = NULL; } }

Changing the #define to #def negates the error, but produces no macro/template. Removing the parenthesis from "(pObj)" in "(pObj)->", to "pObj->" also negates the error, and produces the correct macro.

The exact error produced:

c:\common\nimwrappers\disphelper_custom.h(75, 71) Error: ';' expected

GCC compiles the header file, c2nim gives: Error: ';' expected.

I'm trying to wrap fftw3. On line 2, c2nim incorrectly expects a semicolon. Here is a minimal example (which compiles with gcc). Exact output from c2nim is: m.h(2, 56) Error: ';' expected

#if !defined(FFTW_NO_Complex) && defined(_Complex_I) && defined(complex) && defined(I)
#  define FFTW_DEFINE_COMPLEX(R, C) typedef R _Complex C
#else
#  define FFTW_DEFINE_COMPLEX(R, C) typedef R C[2]
#endif    

#define FFTW_DEFINE_API(X, R, C)             \
    FFTW_DEFINE_COMPLEX(R, C);               \

FFTW_DEFINE_API(FFTW_MANGLE_DOUBLE, double, fftw_complex)

c2nim could generate enums in ascending order

Originally posted by @gradha on June 10th

The following snippet of code is successfully translated with c2nim:

typedef enum {
    HOEDOWN_BUF_OK = 0,
    HOEDOWN_BUF_ENOMEM = -1
} hoedown_buferror_t;

However the result nimrod source won't compile because the enums aren't sorted in ascending order. It would be nice if c2nim could detect this and generate the proper sequence without manual tweaking.

CPP #ifdef #endif within class definition

This files to convert, with
Error: identifier expected, but found '#'
when c2nim --cpp <filename.h>
code containing

class failClass1 {
public:
  int tmpI;
#ifdef windows
  void someProc(int x);
#endif
};

Comment out the #ifdef and #endif and it converts without error.

cpp.nim(88, 41) Error: undeclared identifier: 'TMacro'

I can't install c2nim :

$ nim --v
Nim Compiler Version 0.11.3 (2015-06-20) [Linux: amd64]
Copyright (c) 2006-2015 by Andreas Rumpf

git hash: 49745248545e0a751c37293fd68e34d95596d455
active boot switches: -d:release
$ nimble --v
nimble v0.6.2 compiled at 2015-06-20 02:49:10

$ nimble install c2nim > ~/nimble.log

nimble.log

Thank you

c2nim marks all C++ classes final, doesn't understand namespaces

Originally opened by @gradhaon February 10th

The following simple hierarchy example gives troubles to c2nim using the --cpp switch (didn't notice any other useful switches):

#include <stdio.h>

class Base {
public:
    int a;

    void sayHello();
};

void Base::sayHello() { printf("Base hello\n"); }

class Door : public Base {
public:
    int height;

    void sayHello();
};

void Door::sayHello() { printf("Door hello\n"); }

void test()
{
    Base a;
    Door d;
    a.sayHello();
    d.sayHello();
}

int main(void) {
    test();
    return 0;
}

The generated code is quite similar:

type
  Base* {.pure, final.} = object
    a*: cint


proc sayHello*(this: var Base)
proc Base::sayHello*() =
  printf("Base hello\x0A")

type
  Door* {.pure, final.} = object of Base
    height*: cint


proc sayHello*(this: var Door)
proc Door::sayHello*() =
  printf("Door hello\x0A")

proc test*() =
  var a: Base
  var d: Door
  a.sayHello()
  d.sayHello()

proc main*(): cint =
  test()
  return 0

One has to remove the final pragmas and fix the :: method lines (or remove them in favour of the forward declarations).

typedef with struct is not properly generated.

Code like

typedef struct st_tree_node{
    char *name;
    int n_children;
    struct st_tree_node **children;
} TreeNode;

is converted to

type
  TreeNode* = object
    name*: cstring
    n_children*: cint
    children*: ptr ptr st_tree_node

but st_tree_node is not defined.

The proper nim code should be :

type
  st_tree_node* = object
    name*: cstring
    n_children*: cint
    children*: ptr ptr st_tree_node

  TreeNode* = st_tree_node

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.