authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-03 14:26:21-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-03 14:26:21-04:00
logbc6c33b1b64822b0667ab88b73f4b5c4b302154f
treeea1a904d61a65d57ae3e8df8f861d4eea71ae77c
parentc8333d0cc9a2941ea35f1202073dabd9058beebc

IR: support this literal and bool literal


2 files changed, 61 insertions(+), 41 deletions(-)

src/analyze.cpp+2-39
...@@ -2871,16 +2871,6 @@ static TypeTableEntry *resolve_expr_const_val_as_type(CodeGen *g, AstNode *node,...@@ -2871,16 +2871,6 @@ static TypeTableEntry *resolve_expr_const_val_as_type(CodeGen *g, AstNode *node,
2871 return g->builtin_types.entry_type;2871 return g->builtin_types.entry_type;
2872}2872}
28732873
2874static TypeTableEntry *resolve_expr_const_val_as_block(CodeGen *g, AstNode *node, BlockContext *block_context,
2875 bool depends_on_compile_var)
2876{
2877 Expr *expr = get_resolved_expr(node);
2878 expr->const_val.ok = true;
2879 expr->const_val.data.x_block = block_context;
2880 expr->const_val.depends_on_compile_var = depends_on_compile_var;
2881 return g->builtin_types.entry_block;
2882}
2883
2884static TypeTableEntry *resolve_expr_const_val_as_other_expr(CodeGen *g, AstNode *node, AstNode *other,2874static TypeTableEntry *resolve_expr_const_val_as_other_expr(CodeGen *g, AstNode *node, AstNode *other,
2885 bool depends_on_compile_var)2875 bool depends_on_compile_var)
2886{2876{
...@@ -2999,13 +2989,6 @@ static TypeTableEntry *resolve_expr_const_val_as_unsigned_num_lit(CodeGen *g, As...@@ -2999,13 +2989,6 @@ static TypeTableEntry *resolve_expr_const_val_as_unsigned_num_lit(CodeGen *g, As
2999 return g->builtin_types.entry_num_lit_int;2989 return g->builtin_types.entry_num_lit_int;
3000}2990}
30012991
3002static TypeTableEntry *resolve_expr_const_val_as_import(CodeGen *g, AstNode *node, ImportTableEntry *import) {
3003 Expr *expr = get_resolved_expr(node);
3004 expr->const_val.ok = true;
3005 expr->const_val.data.x_import = import;
3006 return g->builtin_types.entry_namespace;
3007}
3008
3009static TypeTableEntry *analyze_error_literal_expr(CodeGen *g, ImportTableEntry *import,2992static TypeTableEntry *analyze_error_literal_expr(CodeGen *g, ImportTableEntry *import,
3010 BlockContext *context, AstNode *node, Buf *err_name)2993 BlockContext *context, AstNode *node, Buf *err_name)
3011{2994{
...@@ -3905,26 +3888,6 @@ static TypeTableEntry *analyze_zeroes_literal_expr(CodeGen *g, ImportTableEntry...@@ -3905,26 +3888,6 @@ static TypeTableEntry *analyze_zeroes_literal_expr(CodeGen *g, ImportTableEntry
3905 return expected_type ? expected_type : g->builtin_types.entry_undef;3888 return expected_type ? expected_type : g->builtin_types.entry_undef;
3906}3889}
39073890
3908static TypeTableEntry *analyze_this_literal_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
3909 TypeTableEntry *expected_type, AstNode *node)
3910{
3911 if (!context->parent) {
3912 return resolve_expr_const_val_as_import(g, node, import);
3913 }
3914 if (context->fn_entry && (!context->parent->fn_entry ||
3915 (context->parent->parent && !context->parent->parent->fn_entry)))
3916 {
3917 return resolve_expr_const_val_as_fn(g, node, context->fn_entry, false);
3918 }
3919 if (context->node->type == NodeTypeContainerDecl) {
3920 return resolve_expr_const_val_as_type(g, node, context->node->data.struct_decl.type_entry, false);
3921 }
3922 if (context->node->type == NodeTypeBlock) {
3923 return resolve_expr_const_val_as_block(g, node, context, false);
3924 }
3925 zig_unreachable();
3926}
3927
3928static TypeTableEntry *analyze_number_literal_expr(CodeGen *g, ImportTableEntry *import,3891static TypeTableEntry *analyze_number_literal_expr(CodeGen *g, ImportTableEntry *import,
3929 BlockContext *block_context, TypeTableEntry *expected_type, AstNode *node)3892 BlockContext *block_context, TypeTableEntry *expected_type, AstNode *node)
3930{3893{
...@@ -5202,7 +5165,7 @@ static TypeTableEntry *analyze_expression_pointer_only(CodeGen *g, ImportTableEn...@@ -5202,7 +5165,7 @@ static TypeTableEntry *analyze_expression_pointer_only(CodeGen *g, ImportTableEn
5202 node->data.char_literal.value, false);5165 node->data.char_literal.value, false);
5203 break;5166 break;
5204 case NodeTypeBoolLiteral:5167 case NodeTypeBoolLiteral:
5205 return_type = resolve_expr_const_val_as_bool(g, node, node->data.bool_literal.value, false);5168 zig_panic("moved to ir.cpp");
5206 break;5169 break;
5207 case NodeTypeNullLiteral:5170 case NodeTypeNullLiteral:
5208 return_type = analyze_null_literal_expr(g, import, context, expected_type, node);5171 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...@@ -5214,7 +5177,7 @@ static TypeTableEntry *analyze_expression_pointer_only(CodeGen *g, ImportTableEn
5214 return_type = analyze_zeroes_literal_expr(g, import, context, expected_type, node);5177 return_type = analyze_zeroes_literal_expr(g, import, context, expected_type, node);
5215 break;5178 break;
5216 case NodeTypeThisLiteral:5179 case NodeTypeThisLiteral:
5217 return_type = analyze_this_literal_expr(g, import, context, expected_type, node);5180 zig_panic("moved to ir.cpp");
5218 break;5181 break;
5219 case NodeTypeSymbol:5182 case NodeTypeSymbol:
5220 return_type = analyze_symbol_expr(g, import, context, expected_type, node, pointer_only);5183 return_type = analyze_symbol_expr(g, import, context, expected_type, node, pointer_only);
src/ir.cpp+59-2
...@@ -335,6 +335,30 @@ static IrInstruction *ir_build_const_generic_fn(IrBuilder *irb, AstNode *source_...@@ -335,6 +335,30 @@ static IrInstruction *ir_build_const_generic_fn(IrBuilder *irb, AstNode *source_
335 return &const_instruction->base;335 return &const_instruction->base;
336}336}
337337
338static IrInstruction *ir_build_const_import(IrBuilder *irb, AstNode *source_node, ImportTableEntry *import) {
339 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
340 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_namespace;
341 const_instruction->base.static_value.ok = true;
342 const_instruction->base.static_value.data.x_import = import;
343 return &const_instruction->base;
344}
345
346static IrInstruction *ir_build_const_scope(IrBuilder *irb, AstNode *source_node, BlockContext *scope) {
347 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
348 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_block;
349 const_instruction->base.static_value.ok = true;
350 const_instruction->base.static_value.data.x_block = scope;
351 return &const_instruction->base;
352}
353
354static IrInstruction *ir_build_const_bool(IrBuilder *irb, AstNode *source_node, bool value) {
355 IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, source_node);
356 const_instruction->base.type_entry = irb->codegen->builtin_types.entry_block;
357 const_instruction->base.static_value.ok = true;
358 const_instruction->base.static_value.data.x_bool = value;
359 return &const_instruction->base;
360}
361
338static IrInstruction *ir_build_bin_op(IrBuilder *irb, AstNode *source_node, IrBinOp op_id,362static IrInstruction *ir_build_bin_op(IrBuilder *irb, AstNode *source_node, IrBinOp op_id,
339 IrInstruction *op1, IrInstruction *op2)363 IrInstruction *op1, IrInstruction *op2)
340{364{
...@@ -1493,6 +1517,37 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, AstNode *node) {...@@ -1493,6 +1517,37 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, AstNode *node) {
14931517
1494}1518}
14951519
1520static IrInstruction *ir_gen_this_literal(IrBuilder *irb, AstNode *node) {
1521 assert(node->type == NodeTypeThisLiteral);
1522
1523 BlockContext *scope = node->block_context;
1524
1525 if (!scope->parent)
1526 return ir_build_const_import(irb, node, node->owner);
1527
1528 if (scope->fn_entry && (!scope->parent->fn_entry ||
1529 (scope->parent->parent && !scope->parent->parent->fn_entry)))
1530 {
1531 return ir_build_const_fn(irb, node, scope->fn_entry);
1532 }
1533
1534 if (scope->node->type == NodeTypeContainerDecl) {
1535 TypeTableEntry *container_type = scope->node->data.struct_decl.type_entry;
1536 assert(container_type);
1537 return ir_build_const_type(irb, node, container_type);
1538 }
1539
1540 if (scope->node->type == NodeTypeBlock)
1541 return ir_build_const_scope(irb, node, scope);
1542
1543 zig_unreachable();
1544}
1545
1546static IrInstruction *ir_gen_bool_literal(IrBuilder *irb, AstNode *node) {
1547 assert(node->type == NodeTypeBoolLiteral);
1548 return ir_build_const_bool(irb, node, node->data.bool_literal.value);
1549}
1550
1496static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockContext *block_context,1551static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockContext *block_context,
1497 LValPurpose lval)1552 LValPurpose lval)
1498{1553{
...@@ -1528,6 +1583,10 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont...@@ -1528,6 +1583,10 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont
1528 return ir_gen_return(irb, node);1583 return ir_gen_return(irb, node);
1529 case NodeTypeFieldAccessExpr:1584 case NodeTypeFieldAccessExpr:
1530 return ir_gen_field_access(irb, node, lval);1585 return ir_gen_field_access(irb, node, lval);
1586 case NodeTypeThisLiteral:
1587 return ir_gen_this_literal(irb, node);
1588 case NodeTypeBoolLiteral:
1589 return ir_gen_bool_literal(irb, node);
1531 case NodeTypeUnwrapErrorExpr:1590 case NodeTypeUnwrapErrorExpr:
1532 case NodeTypeDefer:1591 case NodeTypeDefer:
1533 case NodeTypeSliceExpr:1592 case NodeTypeSliceExpr:
...@@ -1538,13 +1597,11 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont...@@ -1538,13 +1597,11 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont
1538 case NodeTypeContinue:1597 case NodeTypeContinue:
1539 case NodeTypeLabel:1598 case NodeTypeLabel:
1540 case NodeTypeSwitchExpr:1599 case NodeTypeSwitchExpr:
1541 case NodeTypeBoolLiteral:
1542 case NodeTypeStringLiteral:1600 case NodeTypeStringLiteral:
1543 case NodeTypeCharLiteral:1601 case NodeTypeCharLiteral:
1544 case NodeTypeNullLiteral:1602 case NodeTypeNullLiteral:
1545 case NodeTypeUndefinedLiteral:1603 case NodeTypeUndefinedLiteral:
1546 case NodeTypeZeroesLiteral:1604 case NodeTypeZeroesLiteral:
1547 case NodeTypeThisLiteral:
1548 case NodeTypeErrorType:1605 case NodeTypeErrorType:
1549 case NodeTypeTypeLiteral:1606 case NodeTypeTypeLiteral:
1550 case NodeTypeArrayType:1607 case NodeTypeArrayType: