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,
28712871 return g->builtin_types.entry_type;
28722872}
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
28842874static TypeTableEntry *resolve_expr_const_val_as_other_expr(CodeGen *g, AstNode *node, AstNode *other,
28852875 bool depends_on_compile_var)
28862876{
......@@ -2999,13 +2989,6 @@ static TypeTableEntry *resolve_expr_const_val_as_unsigned_num_lit(CodeGen *g, As
29992989 return g->builtin_types.entry_num_lit_int;
30002990}
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
30092992static TypeTableEntry *analyze_error_literal_expr(CodeGen *g, ImportTableEntry *import,
30102993 BlockContext *context, AstNode *node, Buf *err_name)
30112994{
......@@ -3905,26 +3888,6 @@ static TypeTableEntry *analyze_zeroes_literal_expr(CodeGen *g, ImportTableEntry
39053888 return expected_type ? expected_type : g->builtin_types.entry_undef;
39063889}
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
39283891static TypeTableEntry *analyze_number_literal_expr(CodeGen *g, ImportTableEntry *import,
39293892 BlockContext *block_context, TypeTableEntry *expected_type, AstNode *node)
39303893{
......@@ -5202,7 +5165,7 @@ static TypeTableEntry *analyze_expression_pointer_only(CodeGen *g, ImportTableEn
52025165 node->data.char_literal.value, false);
52035166 break;
52045167 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");
52065169 break;
52075170 case NodeTypeNullLiteral:
52085171 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
52145177 return_type = analyze_zeroes_literal_expr(g, import, context, expected_type, node);
52155178 break;
52165179 case NodeTypeThisLiteral:
5217 return_type = analyze_this_literal_expr(g, import, context, expected_type, node);
5180 zig_panic("moved to ir.cpp");
52185181 break;
52195182 case NodeTypeSymbol:
52205183 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_
335335 return &const_instruction->base;
336336}
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
338362static IrInstruction *ir_build_bin_op(IrBuilder *irb, AstNode *source_node, IrBinOp op_id,
339363 IrInstruction *op1, IrInstruction *op2)
340364{
......@@ -1493,6 +1517,37 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, AstNode *node) {
14931517
14941518}
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
14961551static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockContext *block_context,
14971552 LValPurpose lval)
14981553{
......@@ -1528,6 +1583,10 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont
15281583 return ir_gen_return(irb, node);
15291584 case NodeTypeFieldAccessExpr:
15301585 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);
15311590 case NodeTypeUnwrapErrorExpr:
15321591 case NodeTypeDefer:
15331592 case NodeTypeSliceExpr:
......@@ -1538,13 +1597,11 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont
15381597 case NodeTypeContinue:
15391598 case NodeTypeLabel:
15401599 case NodeTypeSwitchExpr:
1541 case NodeTypeBoolLiteral:
15421600 case NodeTypeStringLiteral:
15431601 case NodeTypeCharLiteral:
15441602 case NodeTypeNullLiteral:
15451603 case NodeTypeUndefinedLiteral:
15461604 case NodeTypeZeroesLiteral:
1547 case NodeTypeThisLiteral:
15481605 case NodeTypeErrorType:
15491606 case NodeTypeTypeLiteral:
15501607 case NodeTypeArrayType: