ooc-lang / rock

:ocean: self-hosted ooc compiler that generates c99
http://ooc-lang.org/
MIT License
404 stars 40 forks source link

rock tries to resolve enum values inside versioned-out blocks #935

Closed ghost closed 8 years ago

ghost commented 9 years ago
version(windows) {
include windows
TestEnum: enum {
    Symbol1 = WAIT_OBJECT_0
}
}

this won't compile on Unix as rock tries to resolve the value of Symbol1, even though everything is inside version block specific to Windows:

bin/rock --version rock 0.9.11-head codename sapporo, built on Mon Oct 19 14:24:24 2015

rock_tmp/ooc/enums/enums.c: In function ‘enums_load’: rock_tmp/ooc/enums/enums.c:98:13: error: ‘WAIT_OBJECT_0’ undeclared (first use in this function) WAIT_OBJECT_0

horasal commented 9 years ago

Yes, it's a bug.

Enums are always generated as global variables regardless of versionblock.

At rock/middle/EnumDecl.ooc : createCovers:

        slit := StructLiteral new(valuesCoverDecl getInstanceType(), elements, token)
        valuesGlobal = VariableDecl new(null, name + "__values", slit, token)
        valuesGlobal isGlobal = true
        token module body add(valuesGlobal)

the generated cover is always global. Then global variable will be unwrapped to a global definition and a assignment, so you will see the following c codes :

        #if defined(__WIN32__) || defined(__WIN64__)
        lang_types__Class___load__();
        #endif
        issue935__TestEnum__values_t___load__();
        issue935__TestEnum__values = (issue935__TestEnum__values_t) { 
            WAIT_OBJECT_0
        }
ghost commented 9 years ago

Thanks for the explanation. We're using a simple workaround for now:

version(windows) {
    include windows
    TestEnum: class {
        Symbol1: static extern (WAIT_OBJECT_0) Long
    }
}