4f698a1f5fc7 — Leonard Ritter 20 days ago
* compiler.bridge: support for float and int initializers for globals
* compiler.bridge: support for static globals
* compiler.bridge: static const globals with initializers are translated to constants
2 files changed, 76 insertions(+), 5 deletions(-)

M lib/scopes/compiler/bridge.sc
M testing/test_bridge.sc
M lib/scopes/compiler/bridge.sc +58 -4
@@ 608,15 608,69 @@ struct CVisitor
         ;
 
     fn translate-global (self cursor)
-        if ((clang_Cursor_getStorageClass cursor) == CX_SC_Static)
-            return;
         trace-expanding-anchor ('toanchor self cursor)
-        T := 'translate-type self (clang_getCursorType cursor)
+        TT := clang_getCursorType cursor
+        const? := clang_isConstQualifiedType TT
+        T := 'translate-type self TT
             fulldecl? = true
+        ST := 'storageof T
         name := Symbol (CXString->string-call clang_getCursorSpelling cursor)
         symname := Symbol (CXString->string-call clang_Cursor_getMangling cursor)
 
-        val := sc_global_new symname T 0 unnamed
+        static? := (clang_Cursor_getStorageClass cursor) == CX_SC_Static
+
+        storage_class := if static?
+            'Private
+        else unnamed
+
+        data := clang_Cursor_getVarDeclInitializer cursor
+
+        fail := inline "#hidden" (errsym)
+            self.externs = 'bind self.externs name `errsym
+            return;
+
+        init := switch data.kind
+        case CXCursor_InvalidFile # no initializer
+            `none
+        pass CXCursor_IntegerLiteral
+        pass CXCursor_FloatingLiteral
+        pass CXCursor_UnexposedExpr
+        pass CXCursor_InitListExpr
+        do
+            result := clang_Cursor_Evaluate cursor
+            switch (clang_EvalResult_getKind result)
+            case CXEval_Int
+                result := clang_EvalResult_getAsLongLong result
+                result := if (ST < integer)
+                    switch ('bitcount ST)
+                    case 8 `[(itrunc result i8)]
+                    case 16 `[(itrunc result i16)]
+                    case 32 `[(itrunc result i32)]
+                    default `result
+                else `result
+                sc_pure_cast_new T result
+            case CXEval_Float
+                result := clang_EvalResult_getAsDouble result
+                result := if (ST < real)
+                    switch ('bitcount ST)
+                    case 32 `[(fptrunc result f32)]
+                    default `result
+                else `result
+                sc_pure_cast_new T result
+            default
+                fail 'could-not-evaluate-initializer
+        default
+            fail 'could-not-translate-global-initializer
+
+        if (const? and static?)
+            if (('typeof init) != Nothing)
+                self.externs = 'bind self.externs name init
+                return;
+
+        val := sc_global_new symname T 0 storage_class
+        if (('typeof init) != Nothing)
+            sc_global_set_initializer val init
+
         #RT := 'qualifiersof val
         #conv := sc_pure_cast_new RT val
 

          
M testing/test_bridge.sc +18 -1
@@ 47,6 47,10 @@ include
             U y;
             K x;
         } M;
+        typedef struct {
+            int x;
+            int y;
+        } PT;
         enum _bleh {
             Oher = 0,
             Eher = -1

          
@@ 68,11 72,18 @@ include
             return a + b;
         }
 
-        //typedef float _Complex __cfloat128;
         typedef _Complex float __cfloat128 __attribute__ ((__mode__ (__TC__)));
 
         typedef __float128 f128_2;
 
+        static int hawk_tuah_i = 303;
+        static float hawk_tuah_f = 333.0;
+        //static PT m_init = { 23, 42 };
+
+        const static int c_hawk_tuah_i = 303;
+        const static float c_hawk_tuah_f = 333.0;
+        //const static PT c_m_init = { 23, 42 };
+
 print "define:"
 for k v in lib.define
     print k v

          
@@ 93,6 104,12 @@ print;
 test ((lib.extern.atestfunc 1 2) == 3)
 
 test (lib.typedef.kawonz.Uher == 1)
+
+test (lib.extern.hawk_tuah_i == 303)
+test (lib.extern.hawk_tuah_f == 333.0)
+
 print "ok."
 
+#print lib.extern.blah
+
 ;