struct literals can be compile time known
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 23 Dec 2020 02:09:32 +0000 (20:09 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 23 Dec 2020 02:09:32 +0000 (20:09 -0600)
onyx
src/onyxchecker.c
src/onyxwasm.c

diff --git a/onyx b/onyx
index a876c611d7701c1e25f0f4a37d0db373f6a97a52..20ed83eb90b5eadcc350d55edb596ea86f263501 100755 (executable)
Binary files a/onyx and b/onyx differ
index e27227418df984bcfb195a107bb6a6279023f946..c82b6cedd65dc9174c92b5fb436f95229c028ce4 100644 (file)
@@ -895,6 +895,8 @@ b32 check_struct_literal(AstStructLiteral* sl) {
     AstTyped** actual = sl->values;
     StructMember smem;
 
+    b32 all_comptime = 1;
+
     fori (i, 0, mem_count) {
         if (check_expression(actual)) return 1;
 
@@ -914,9 +916,15 @@ b32 check_struct_literal(AstStructLiteral* sl) {
             return 1;
         }
 
+        if (((*actual)->flags & Ast_Flag_Comptime) == 0)
+            all_comptime = 0;
+
         actual++;
     }
 
+    if (all_comptime)
+        sl->flags |= Ast_Flag_Comptime;
+
     return 0;
 }
 
index 000e8f27e0e9ddf6b19ed6a13f3d3ae4cec7cc4f..3ae39b74cc53153376d3f251a50885735470f008 100644 (file)
@@ -2993,6 +2993,21 @@ static void emit_raw_data(OnyxWasmModule* mod, ptr data, AstTyped* node) {
         break;    
     }
 
+    case Ast_Kind_Struct_Literal: {
+        AstStructLiteral* sl = (AstStructLiteral *) node;
+
+        Type* sl_type = sl->type;
+        assert(sl_type->kind == Type_Kind_Struct);
+
+        i32 i = 0;
+        bh_arr_each(AstTyped *, expr, sl->values) {
+            emit_raw_data(mod, bh_pointer_add(data, sl_type->Struct.memarr[i]->offset), *expr);
+            i++;
+        }
+
+        break;
+    }
+
     case Ast_Kind_StrLit: {
         AstStrLit* sl = (AstStrLit *) node;