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

Invalid C++ code being generated #5994

Closed
Misiur opened this issue Feb 2, 2017 · 6 comments
Closed

Invalid C++ code being generated #5994

Misiur opened this issue Feb 2, 2017 · 6 comments
Assignees
Labels

Comments

@Misiur
Copy link

@Misiur Misiur commented Feb 2, 2017

Again, really sorry for not being able to reproduce minimal viable case - I can't isolate just the failing code without library I'm using. The code compiles fine on haxe 3.2, and returns weird errors for 3.4. (not haxe ones, but C++ - should this be reported in hxcpp instead?)

https://github.com/Misiur/haxe34crasher - requires "ecx" library.

Compiling group: haxe

cl.exe -Iinclude -nologo /WX- /fp:precise -DHX_WINDOWS -D_USING_V140_SDK71_ -GR -O2(optim-std) -Zi(debug) -FdD:\Dev\private\haxe\crasher\export\bin\obj/msvc19xp/vc.pdb(debug) -Od(debug) -O2(release) -Os(optim-size) -FS -Oy- -c -EHs -GS- -arch:SSE -ID:/Programs/Haxe/haxe/lib/hxcpp/3,4,49/include -DHXCPP_VISIT_ALLOCS(haxe) -DHXCPP_API_LEVEL=331(haxe) -D_CRT_SECURE_NO_DEPRECATE -D_ALLOW_MSC_VER_MISMATCH -D_ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH -wd4996 ... tags=[haxe,static]

 - src/Foobar.cpp 

Error: Foobar.cpp
./src/Foobar.cpp(82): error C2672: 'cpp::Pointer_obj::fromRaw': no matching overloaded function found
./src/Foobar.cpp(82): error C2784: 'cpp::Pointer<T> cpp::Pointer_obj::fromRaw(const T *)': could not deduce template argument for 'const T *' from 'cpp::RawAutoCast'
D:/Programs/Haxe/haxe/lib/hxcpp/3,4,49/include\cpp/Pointer.h(527): note: see declaration of 'cpp::Pointer_obj::fromRaw'
./src/Foobar.cpp(82): error C2784: 'cpp::Pointer<T> cpp::Pointer_obj::fromRaw(T *)': could not deduce template argument for 'T *' from 'cpp::RawAutoCast'
D:/Programs/Haxe/haxe/lib/hxcpp/3,4,49/include\cpp/Pointer.h(525): note: see declaration of 'cpp::Pointer_obj::fromRaw'
./src/Foobar.cpp(82): error C2227: left of '->get_value' must point to class/struct/union/generic type
Error: Build failed
@nadako
Copy link
Member

@nadako nadako commented Feb 4, 2017

@hughsando that was pretty easy to reduce:

class Main {
    static function main() {
        cpp.Pointer.fromRaw(cpp.Pointer.addressOf(null).rawCast());
    }
}

generates

HXLINE(   3)		::cpp::Pointer_obj::fromRaw(::cpp::Pointer_obj::addressOf(null())->rawCast());

which fails with

./src/Main.cpp(29): error C2672: 'cpp::Pointer_obj::fromRaw': no matching overloaded function found
./src/Main.cpp(29): error C2784: 'cpp::Pointer<T> cpp::Pointer_obj::fromRaw(const T *)': could not deduce template argument for 'const T *' from 'cpp::RawAutoCast'
d:/code/haxe/lib/hxcpp/git/include\cpp/Pointer.h(527): note: see declaration of 'cpp::Pointer_obj::fromRaw'
./src/Main.cpp(29): error C2784: 'cpp::Pointer<T> cpp::Pointer_obj::fromRaw(T *)': could not deduce template argument for 'T *' from 'cpp::RawAutoCast'
d:/code/haxe/lib/hxcpp/git/include\cpp/Pointer.h(525): note: see declaration of 'cpp::Pointer_obj::fromRaw'
@hughsando
Copy link
Member

@hughsando hughsando commented Feb 6, 2017

@nadako
Copy link
Member

@nadako nadako commented Feb 6, 2017

Hmm, it wasn't null there in the original code, I just added it for a minimal example.

@Misiur
Copy link
Author

@Misiur Misiur commented Mar 2, 2017

Sorry for not getting back sooner. I updated to git version of hxcpp, and now I get more weird results. First with the exact same repo I linked in first post:

Error: Foobar.cpp
./src/Foobar.cpp(82): error C2440: 'type cast': cannot convert from 'void' to 'Foo'
./src/Foobar.cpp(82): note: Expressions of type void cannot be converted to other types

Generated code in Foobar.cpp

HXLINE(  91)		 ::ecx::Service tmp = _hx___world->_services->__get((int)1).StaticCast<  ::ecx::Service >();
HXLINE( 108)		this->_foo = ( ( ::Foo)(::cpp::Pointer_obj::fromRaw(::cpp::Pointer_obj::addressOf(tmp)->rawCast())->get_value()) );

I tried to isolate the problem as @nadako did, but I'm not certain if it's equivalent as the error is a little different this time:

class Foo 
{
    public function new() {}

    public var bar = 15;
}

class Main
{
    static function main()
    {
        var tmp = new Foo();
        var foo = cpp.Pointer.fromRaw(cpp.Pointer.addressOf(tmp).rawCast()).get_value();
        trace(foo);
    }
}

Error while compiling

Error: Main.cpp
./src/Main.cpp(33): error C2440: 'initializing': cannot convert from 'void' to 'Dynamic &'
./src/Main.cpp(33): note: Expressions of type void cannot be converted to other types

CPP code generated:

HXLINE(  12)		 ::Foo tmp =  ::Foo_obj::__alloc( HX_CTX );
HXLINE(  13)		 ::Dynamic & foo = ::cpp::Pointer_obj::fromRaw(::cpp::Pointer_obj::addressOf(tmp)->rawCast())->get_value();
HXLINE(  14)		::haxe::Log_obj::trace(foo,hx::SourceInfo(HX_("Main.hx",05,5c,7e,08),14,HX_("Main",59,64,2f,33),HX_("main",39,38,56,48)));

Thank you

@hughsando
Copy link
Member

@hughsando hughsando commented Mar 3, 2017

The problem here is that the "rawCast" converts the pointer to a "void *", and there is nothing then to tell hxcpp what the type should be, ie that it should be a Foo *. The when you go back fromRaw and get a value, you end up with a void. To work around this, you can add explicit casts, or assign to a typed variable

class Test
{
    static function main()
    {
        var tmp = new Foo();
        var foo:cpp.Pointer<Foo> = cpp.Pointer.fromRaw(cpp.Pointer.addressOf(tmp).rawCast());
        trace(foo.value);
    }
}
@eliasku
Copy link

@eliasku eliasku commented May 15, 2018

@hughsando Thanks for snippet!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.