fpco / inline-c

284 stars 51 forks source link

Templates with pointers breaks the compiler #113

Closed Ofenhed closed 3 years ago

Ofenhed commented 4 years ago

It seems that any pointer in a template definition breaks compilation.

For reference, here's a test that breaks compilation:

    Hspec.it "Template with pointers" $ do
      pt <- [C.block| std::vector<int*>* {
          return new std::vector<int*>();
        } |] :: IO (Ptr (StdVector.CStdVector (Ptr C.CInt)))
      [C.block| void {
          int *a = new int;
          *a = 100;
          $(std::vector<int*>* pt)->push_back(a);
          std::cout << *((*$(std::vector<int*>* pt))[0]) << std::endl;
        } |]
bitonic commented 4 years ago

@junjihashimoto , could you take a look at this?

Ofenhed commented 4 years ago

Digging around in the code shows that a template can only have TypeSpecifiers and numbers as argument. From what I can gather from the code, it seems that pointers (even more so if they are volatile), arrays, consts, long longs and chars with specified sign are examples of types which cannot be used. I was going to take a stab at it, but this looks as if it might need a bigger patch than I'd be comfortable submitting.

junjihashimoto commented 4 years ago

@bitonic @Ofenhed I'm sorry I didn't notice immediately. https://github.com/fpco/inline-c/blob/master/inline-c/src/Language/C/Types/Parse.hs#L641-L783 It seems that pointers cannot be used grammatically. This was not intended. C++ often uses smart pointers instead of raw pointers, so I'm not in trouble at all.

junjihashimoto commented 4 years ago

https://github.com/fpco/inline-c/pull/114 This PR does not support arrays, consts and so on, but std::vector<int*> is supported.

bitonic commented 4 years ago

@junjihashimoto thanks.

@Ofenhed can you try current master and see if things work for you?

Ofenhed commented 4 years ago

Thank you. Yes, it works for my use case. For now I guess the unsupported types can be reached using a typedef.