From 182d8314086961a2e02f9f1dd673e95f83687367 Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 28 Sep 2019 20:52:45 -0300 Subject: [PATCH] comp_ctx: use more pointers to variable metadata --- src/codegen.zig | 1 + src/comp_ctx.zig | 44 +++++++++++++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/codegen.zig b/src/codegen.zig index 592a79b..ce6e787 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -381,6 +381,7 @@ pub const Codegen = struct { ); stmt.*.VarDecl.llvm_alloca = variable; + var_metadata.*.llvm_alloca = variable; var llvm_expr = try self.emitExpr(builder, vardecl.value); _ = llvm.LLVMBuildStore(builder, llvm_expr, variable); diff --git a/src/comp_ctx.zig b/src/comp_ctx.zig index 56d829c..f691c7d 100644 --- a/src/comp_ctx.zig +++ b/src/comp_ctx.zig @@ -146,12 +146,24 @@ pub const VariableMetadata = struct { from_scope: ?*Scope = null, from_function: ?*FunctionSymbol = null, - pub fn withScope(scope: *Scope, typ: SymbolUnderlyingType) VariableMetadata { - return VariableMetadata{ .typ = typ, .from_scope = scope, .using = .Scope }; + pub fn withScope( + allocator: *std.mem.Allocator, + scope: *Scope, + typ: SymbolUnderlyingType, + ) !*VariableMetadata { + var meta = try allocator.create(VariableMetadata); + meta.* = VariableMetadata{ .typ = typ, .from_scope = scope, .using = .Scope }; + return meta; } - pub fn withParam(func: *FunctionSymbol, typ: SymbolUnderlyingType) VariableMetadata { - return VariableMetadata{ .typ = typ, .from_function = func, .using = .Function }; + pub fn withParam( + allocator: *std.mem.Allocator, + func: *FunctionSymbol, + typ: SymbolUnderlyingType, + ) !*VariableMetadata { + var meta = try allocator.create(VariableMetadata); + meta.* = VariableMetadata{ .typ = typ, .from_function = func, .using = .Function }; + return meta; } }; @@ -350,41 +362,43 @@ pub const CompilationContext = struct { self: *@This(), scope_opt: ?*Scope, name: []const u8, - ) ?VariableMetadata { + ) error{OutOfMemory}!?*VariableMetadata { if (scope_opt == null) return null; var scope = scope_opt.?; var kv_opt = scope.env.get(name); if (kv_opt) |kv| { - return VariableMetadata.withScope(scope, kv.value); + return try VariableMetadata.withScope(self.allocator, scope, kv.value); } else { return self.resolveVarTypeInScope(scope.parent, name); } } /// Resolve a given name's type, assuming it is a variable. - pub fn resolveVarType(self: *@This(), name: []const u8) !VariableMetadata { - var var_type: ?VariableMetadata = null; + pub fn resolveVarType(self: *@This(), name: []const u8) !*VariableMetadata { + var var_type: ?*VariableMetadata = null; if (self.current_scope) |scope| { - var_type = self.resolveVarTypeInScope(scope, name); + var_type = try self.resolveVarTypeInScope(scope, name); if (var_type) |typ| return typ; } if (self.cur_function) |cur_function| { var kv_opt = cur_function.parameters.get(name); - if (kv_opt) |kv| return VariableMetadata.withParam(cur_function, kv.value.typ); + if (kv_opt) |kv| + return try VariableMetadata.withParam( + self.allocator, + cur_function, + kv.value.typ, + ); } std.debug.warn("Unknown name {}\n", name); return CompilationError.UnknownName; } - pub fn insertMetadata(self: *@This(), ptr: *const ast.Expr, metadata: VariableMetadata) !void { - var meta = try self.allocator.create(VariableMetadata); - meta.* = metadata; - + pub fn insertMetadata(self: *@This(), ptr: *const ast.Expr, metadata: *VariableMetadata) !void { std.debug.assert(ast.ExprType(ptr.*) == .Variable); - _ = try self.metadata_map.put(ptr, meta); + _ = try self.metadata_map.put(ptr, metadata); } };