diff options
Diffstat (limited to 'src/ir.cpp')
| -rw-r--r-- | src/ir.cpp | 133 |
1 files changed, 55 insertions, 78 deletions
diff --git a/src/ir.cpp b/src/ir.cpp index 0d43398ba8..800b013d57 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -559,6 +559,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionAlignCast *) { return IrInstructionIdAlignCast; } +static constexpr IrInstructionId ir_instruction_id(IrInstructionOpaqueType *) { + return IrInstructionIdOpaqueType; +} + template<typename T> static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node) { T *special_instruction = allocate<T>(1); @@ -2238,6 +2242,12 @@ static IrInstruction *ir_build_align_cast(IrBuilder *irb, Scope *scope, AstNode return &instruction->base; } +static IrInstruction *ir_build_opaque_type(IrBuilder *irb, Scope *scope, AstNode *source_node) { + IrInstructionOpaqueType *instruction = ir_build_instruction<IrInstructionOpaqueType>(irb, scope, source_node); + + return &instruction->base; +} + static IrInstruction *ir_instruction_br_get_dep(IrInstructionBr *instruction, size_t index) { return nullptr; } @@ -2956,6 +2966,10 @@ static IrInstruction *ir_instruction_aligncast_get_dep(IrInstructionAlignCast *i } } +static IrInstruction *ir_instruction_opaquetype_get_dep(IrInstructionOpaqueType *instruction, size_t index) { + return nullptr; +} + static IrInstruction *ir_instruction_get_dep(IrInstruction *instruction, size_t index) { switch (instruction->id) { case IrInstructionIdInvalid: @@ -3154,6 +3168,8 @@ static IrInstruction *ir_instruction_get_dep(IrInstruction *instruction, size_t return ir_instruction_ptrtypeof_get_dep((IrInstructionPtrTypeOf *) instruction, index); case IrInstructionIdAlignCast: return ir_instruction_aligncast_get_dep((IrInstructionAlignCast *) instruction, index); + case IrInstructionIdOpaqueType: + return ir_instruction_opaquetype_get_dep((IrInstructionOpaqueType *) instruction, index); } zig_unreachable(); } @@ -4578,6 +4594,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo return ir_build_align_cast(irb, scope, node, arg0_value, arg1_value); } + case BuiltinFnIdOpaqueType: + return ir_build_opaque_type(irb, scope, node); } zig_unreachable(); } @@ -6044,27 +6062,30 @@ static bool render_instance_name_recursive(CodeGen *codegen, Buf *name, Scope *o return true; } -static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope, AstNode *node) { - assert(node->type == NodeTypeContainerDecl); - - ContainerKind kind = node->data.container_decl.kind; - Buf *name; - if (irb->exec->name) { - name = irb->exec->name; +static Buf *get_anon_type_name(CodeGen *codegen, IrExecutable *exec, const char *kind_name, AstNode *source_node) { + if (exec->name) { + return exec->name; } else { - FnTableEntry *fn_entry = exec_fn_entry(irb->exec); + FnTableEntry *fn_entry = exec_fn_entry(exec); if (fn_entry) { - name = buf_alloc(); + Buf *name = buf_alloc(); buf_append_buf(name, &fn_entry->symbol_name); buf_appendf(name, "("); - render_instance_name_recursive(irb->codegen, name, &fn_entry->fndef_scope->base, irb->exec->begin_scope); + render_instance_name_recursive(codegen, name, &fn_entry->fndef_scope->base, exec->begin_scope); buf_appendf(name, ")"); + return name; } else { - name = buf_sprintf("(anonymous %s at %s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize ")", container_string(kind), - buf_ptr(node->owner->path), node->line + 1, node->column + 1); + return buf_sprintf("(anonymous %s at %s:%" ZIG_PRI_usize ":%" ZIG_PRI_usize ")", kind_name, + buf_ptr(source_node->owner->path), source_node->line + 1, source_node->column + 1); } } +} +static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope, AstNode *node) { + assert(node->type == NodeTypeContainerDecl); + + ContainerKind kind = node->data.container_decl.kind; + Buf *name = get_anon_type_name(irb->codegen, irb->exec, container_string(kind), node); VisibMod visib_mod = VisibModPub; TldContainer *tld_container = allocate<TldContainer>(1); @@ -6114,9 +6135,14 @@ static IrInstruction *ir_gen_fn_proto(IrBuilder *irb, Scope *parent_scope, AstNo return irb->codegen->invalid_instruction; } - IrInstruction *return_type = ir_gen_node(irb, node->data.fn_proto.return_type, parent_scope); - if (return_type == irb->codegen->invalid_instruction) - return irb->codegen->invalid_instruction; + IrInstruction *return_type; + if (node->data.fn_proto.return_type == nullptr) { + return_type = ir_build_const_type(irb, parent_scope, node, irb->codegen->builtin_types.entry_void); + } else { + return_type = ir_gen_node(irb, node->data.fn_proto.return_type, parent_scope); + if (return_type == irb->codegen->invalid_instruction) + return irb->codegen->invalid_instruction; + } return ir_build_fn_proto(irb, parent_scope, node, param_types, align_value, return_type, is_var_args); } @@ -13358,9 +13384,11 @@ static TypeTableEntry *ir_analyze_instruction_c_import(IrAnalyze *ira, IrInstruc if (ira->codegen->verbose) { fprintf(stderr, "\nC imports:\n"); fprintf(stderr, "-----------\n"); - ast_render_decls(ira->codegen, stderr, 4, child_import); + ast_render(ira->codegen, stderr, child_import->root, 4); } + scan_decls(ira->codegen, child_import->decls_scope, child_import->root); + ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base); out_val->data.x_import = child_import; return ira->codegen->builtin_types.entry_namespace; @@ -15136,6 +15164,14 @@ static TypeTableEntry *ir_analyze_instruction_align_cast(IrAnalyze *ira, IrInstr return result->value.type; } +static TypeTableEntry *ir_analyze_instruction_opaque_type(IrAnalyze *ira, IrInstructionOpaqueType *instruction) { + Buf *name = get_anon_type_name(ira->codegen, ira->new_irb.exec, "opaque", instruction->base.source_node); + ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base); + out_val->data.x_type = get_opaque_type(ira->codegen, instruction->base.scope, instruction->base.source_node, + buf_ptr(name)); + return ira->codegen->builtin_types.entry_type; +} + static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) { switch (instruction->id) { case IrInstructionIdInvalid: @@ -15322,6 +15358,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi return ir_analyze_instruction_ptr_type_of(ira, (IrInstructionPtrTypeOf *)instruction); case IrInstructionIdAlignCast: return ir_analyze_instruction_align_cast(ira, (IrInstructionAlignCast *)instruction); + case IrInstructionIdOpaqueType: + return ir_analyze_instruction_opaque_type(ira, (IrInstructionOpaqueType *)instruction); } zig_unreachable(); } @@ -15500,6 +15538,7 @@ bool ir_has_side_effects(IrInstruction *instruction) { case IrInstructionIdOffsetOf: case IrInstructionIdTypeId: case IrInstructionIdAlignCast: + case IrInstructionIdOpaqueType: return false; case IrInstructionIdAsm: { @@ -15515,65 +15554,3 @@ bool ir_has_side_effects(IrInstruction *instruction) { } zig_unreachable(); } - -FnTableEntry *ir_create_inline_fn(CodeGen *codegen, Buf *fn_name, VariableTableEntry *var, Scope *parent_scope) { - FnTableEntry *fn_entry = create_fn_raw(FnInlineAuto, GlobalLinkageIdInternal); - buf_init_from_buf(&fn_entry->symbol_name, fn_name); - - fn_entry->fndef_scope = create_fndef_scope(nullptr, parent_scope, fn_entry); - fn_entry->child_scope = &fn_entry->fndef_scope->base; - - assert(var->value->type->id == TypeTableEntryIdMaybe); - TypeTableEntry *src_fn_type = var->value->type->data.maybe.child_type; - assert(src_fn_type->id == TypeTableEntryIdFn); - - FnTypeId new_fn_type = src_fn_type->data.fn.fn_type_id; - new_fn_type.cc = CallingConventionUnspecified; - - fn_entry->type_entry = get_fn_type(codegen, &new_fn_type); - - IrBuilder ir_builder = {0}; - IrBuilder *irb = &ir_builder; - - irb->codegen = codegen; - irb->exec = &fn_entry->ir_executable; - - AstNode *source_node = parent_scope->source_node; - - size_t arg_count = fn_entry->type_entry->data.fn.fn_type_id.param_count; - IrInstruction **args = allocate<IrInstruction *>(arg_count); - VariableTableEntry **arg_vars = allocate<VariableTableEntry *>(arg_count); - - define_local_param_variables(codegen, fn_entry, arg_vars); - Scope *scope = fn_entry->child_scope; - - irb->current_basic_block = ir_build_basic_block(irb, scope, "Entry"); - // Entry block gets a reference because we enter it to begin. - ir_ref_bb(irb->current_basic_block); - - IrInstruction *maybe_fn_ptr = ir_build_var_ptr(irb, scope, source_node, var, true, false); - IrInstruction *unwrapped_fn_ptr = ir_build_unwrap_maybe(irb, scope, source_node, maybe_fn_ptr, true); - IrInstruction *fn_ref_instruction = ir_build_load_ptr(irb, scope, source_node, unwrapped_fn_ptr); - - for (size_t i = 0; i < arg_count; i += 1) { - IrInstruction *var_ptr_instruction = ir_build_var_ptr(irb, scope, source_node, arg_vars[i], true, false); - args[i] = ir_build_load_ptr(irb, scope, source_node, var_ptr_instruction); - } - - IrInstruction *call_instruction = ir_build_call(irb, scope, source_node, nullptr, fn_ref_instruction, - arg_count, args, false, false); - ir_build_return(irb, scope, source_node, call_instruction); - - if (codegen->verbose) { - fprintf(stderr, "{\n"); - ir_print(codegen, stderr, &fn_entry->ir_executable, 4); - fprintf(stderr, "}\n"); - } - - analyze_fn_ir(codegen, fn_entry, nullptr); - - codegen->fn_defs.append(fn_entry); - - return fn_entry; -} - |
