Commit bc6c33b1b6

Andrew Kelley <superjoe30@gmail.com>
2016-11-03 19:26:21
IR: support this literal and bool literal
1 parent c8333d0
Changed files (2)
src/analyze.cpp
@@ -2871,16 +2871,6 @@ static TypeTableEntry *resolve_expr_const_val_as_type(CodeGen *g, AstNode *node,
     return g->builtin_types.entry_type;
 }
 
-static TypeTableEntry *resolve_expr_const_val_as_block(CodeGen *g, AstNode *node, BlockContext *block_context,
-        bool depends_on_compile_var)
-{
-    Expr *expr = get_resolved_expr(node);
-    expr->const_val.ok = true;
-    expr->const_val.data.x_block = block_context;
-    expr->const_val.depends_on_compile_var = depends_on_compile_var;
-    return g->builtin_types.entry_block;
-}
-
 static TypeTableEntry *resolve_expr_const_val_as_other_expr(CodeGen *g, AstNode *node, AstNode *other,
         bool depends_on_compile_var)
 {
@@ -2999,13 +2989,6 @@ static TypeTableEntry *resolve_expr_const_val_as_unsigned_num_lit(CodeGen *g, As
     return g->builtin_types.entry_num_lit_int;
 }
 
-static TypeTableEntry *resolve_expr_const_val_as_import(CodeGen *g, AstNode *node, ImportTableEntry *import) {
-    Expr *expr = get_resolved_expr(node);
-    expr->const_val.ok = true;
-    expr->const_val.data.x_import = import;
-    return g->builtin_types.entry_namespace;
-}
-
 static TypeTableEntry *analyze_error_literal_expr(CodeGen *g, ImportTableEntry *import,
         BlockContext *context, AstNode *node, Buf *err_name)
 {
@@ -3905,26 +3888,6 @@ static TypeTableEntry *analyze_zeroes_literal_expr(CodeGen *g, ImportTableEntry
     return expected_type ? expected_type : g->builtin_types.entry_undef;
 }
 
-static TypeTableEntry *analyze_this_literal_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
-        TypeTableEntry *expected_type, AstNode *node)
-{
-    if (!context->parent) {
-        return resolve_expr_const_val_as_import(g, node, import);
-    }
-    if (context->fn_entry && (!context->parent->fn_entry ||
-        (context->parent->parent && !context->parent->parent->fn_entry)))
-    {
-        return resolve_expr_const_val_as_fn(g, node, context->fn_entry, false);
-    }
-    if (context->node->type == NodeTypeContainerDecl) {
-        return resolve_expr_const_val_as_type(g, node, context->node->data.struct_decl.type_entry, false);
-    }
-    if (context->node->type == NodeTypeBlock) {
-        return resolve_expr_const_val_as_block(g, node, context, false);
-    }
-    zig_unreachable();
-}
-
 static TypeTableEntry *analyze_number_literal_expr(CodeGen *g, ImportTableEntry *import,
         BlockContext *block_context, TypeTableEntry *expected_type, AstNode *node)
 {
@@ -5202,7 +5165,7 @@ static TypeTableEntry *analyze_expression_pointer_only(CodeGen *g, ImportTableEn
                     node->data.char_literal.value, false);
             break;
         case NodeTypeBoolLiteral:
-            return_type = resolve_expr_const_val_as_bool(g, node, node->data.bool_literal.value, false);
+            zig_panic("moved to ir.cpp");
             break;
         case NodeTypeNullLiteral:
             return_type = analyze_null_literal_expr(g, import, context, expected_type, node);
@@ -5214,7 +5177,7 @@ static TypeTableEntry *analyze_expression_pointer_only(CodeGen *g, ImportTableEn
             return_type = analyze_zeroes_literal_expr(g, import, context, expected_type, node);
             break;
         case NodeTypeThisLiteral:
-            return_type = analyze_this_literal_expr(g, import, context, expected_type, node);
+            zig_panic("moved to ir.cpp");
             break;
         case NodeTypeSymbol:
             return_type = analyze_symbol_expr(g, import, context, expected_type, node, pointer_only);
src/ir.cpp
@@ -335,6 +335,30 @@ static IrInstruction *ir_build_const_generic_fn(IrBuilder *irb, AstNode *source_
     return &const_instruction->base;
 }
 
+static IrInstruction *ir_build_const_import(IrBuilder *irb, AstNode *source_node, ImportTableEntry *import) {
+    IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
+    const_instruction->base.type_entry = irb->codegen->builtin_types.entry_namespace;
+    const_instruction->base.static_value.ok = true;
+    const_instruction->base.static_value.data.x_import = import;
+    return &const_instruction->base;
+}
+
+static IrInstruction *ir_build_const_scope(IrBuilder *irb, AstNode *source_node, BlockContext *scope) {
+    IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
+    const_instruction->base.type_entry = irb->codegen->builtin_types.entry_block;
+    const_instruction->base.static_value.ok = true;
+    const_instruction->base.static_value.data.x_block = scope;
+    return &const_instruction->base;
+}
+
+static IrInstruction *ir_build_const_bool(IrBuilder *irb, AstNode *source_node, bool value) {
+    IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
+    const_instruction->base.type_entry = irb->codegen->builtin_types.entry_block;
+    const_instruction->base.static_value.ok = true;
+    const_instruction->base.static_value.data.x_bool = value;
+    return &const_instruction->base;
+}
+
 static IrInstruction *ir_build_bin_op(IrBuilder *irb, AstNode *source_node, IrBinOp op_id,
         IrInstruction *op1, IrInstruction *op2)
 {
@@ -1493,6 +1517,37 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, AstNode *node) {
 
 }
 
+static IrInstruction *ir_gen_this_literal(IrBuilder *irb, AstNode *node) {
+    assert(node->type == NodeTypeThisLiteral);
+
+    BlockContext *scope = node->block_context;
+
+    if (!scope->parent)
+        return ir_build_const_import(irb, node, node->owner);
+
+    if (scope->fn_entry && (!scope->parent->fn_entry ||
+        (scope->parent->parent && !scope->parent->parent->fn_entry)))
+    {
+        return ir_build_const_fn(irb, node, scope->fn_entry);
+    }
+
+    if (scope->node->type == NodeTypeContainerDecl) {
+        TypeTableEntry *container_type = scope->node->data.struct_decl.type_entry;
+        assert(container_type);
+        return ir_build_const_type(irb, node, container_type);
+    }
+
+    if (scope->node->type == NodeTypeBlock)
+        return ir_build_const_scope(irb, node, scope);
+
+    zig_unreachable();
+}
+
+static IrInstruction *ir_gen_bool_literal(IrBuilder *irb, AstNode *node) {
+    assert(node->type == NodeTypeBoolLiteral);
+    return ir_build_const_bool(irb, node, node->data.bool_literal.value);
+}
+
 static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockContext *block_context,
         LValPurpose lval)
 {
@@ -1528,6 +1583,10 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont
             return ir_gen_return(irb, node);
         case NodeTypeFieldAccessExpr:
             return ir_gen_field_access(irb, node, lval);
+        case NodeTypeThisLiteral:
+            return ir_gen_this_literal(irb, node);
+        case NodeTypeBoolLiteral:
+            return ir_gen_bool_literal(irb, node);
         case NodeTypeUnwrapErrorExpr:
         case NodeTypeDefer:
         case NodeTypeSliceExpr:
@@ -1538,13 +1597,11 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont
         case NodeTypeContinue:
         case NodeTypeLabel:
         case NodeTypeSwitchExpr:
-        case NodeTypeBoolLiteral:
         case NodeTypeStringLiteral:
         case NodeTypeCharLiteral:
         case NodeTypeNullLiteral:
         case NodeTypeUndefinedLiteral:
         case NodeTypeZeroesLiteral:
-        case NodeTypeThisLiteral:
         case NodeTypeErrorType:
         case NodeTypeTypeLiteral:
         case NodeTypeArrayType: