comp_ctx: use more pointers to variable metadata
This commit is contained in:
parent
ea4dd5fde4
commit
182d831408
2 changed files with 30 additions and 15 deletions
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue