2018-07-12 12:08:40 -07:00
|
|
|
const std = @import("std");
|
2018-07-14 21:04:12 -07:00
|
|
|
const builtin = @import("builtin");
|
2018-07-12 12:08:40 -07:00
|
|
|
const Allocator = mem.Allocator;
|
|
|
|
const Decl = @import("decl.zig").Decl;
|
2018-07-14 13:12:41 -07:00
|
|
|
const Compilation = @import("compilation.zig").Compilation;
|
2018-07-12 12:08:40 -07:00
|
|
|
const mem = std.mem;
|
|
|
|
const ast = std.zig.ast;
|
|
|
|
const Value = @import("value.zig").Value;
|
|
|
|
const ir = @import("ir.zig");
|
2018-07-14 21:04:12 -07:00
|
|
|
const Span = @import("errmsg.zig").Span;
|
2018-07-12 12:08:40 -07:00
|
|
|
|
2017-12-26 16:44:08 -08:00
|
|
|
pub const Scope = struct {
|
|
|
|
id: Id,
|
2018-07-12 12:08:40 -07:00
|
|
|
parent: ?*Scope,
|
|
|
|
ref_count: usize,
|
|
|
|
|
|
|
|
pub fn ref(base: *Scope) void {
|
|
|
|
base.ref_count += 1;
|
|
|
|
}
|
|
|
|
|
2018-07-14 13:12:41 -07:00
|
|
|
pub fn deref(base: *Scope, comp: *Compilation) void {
|
2018-07-12 12:08:40 -07:00
|
|
|
base.ref_count -= 1;
|
|
|
|
if (base.ref_count == 0) {
|
2018-07-14 13:12:41 -07:00
|
|
|
if (base.parent) |parent| parent.deref(comp);
|
2018-07-12 12:08:40 -07:00
|
|
|
switch (base.id) {
|
|
|
|
Id.Decls => @fieldParentPtr(Decls, "base", base).destroy(),
|
2018-07-14 13:12:41 -07:00
|
|
|
Id.Block => @fieldParentPtr(Block, "base", base).destroy(comp),
|
|
|
|
Id.FnDef => @fieldParentPtr(FnDef, "base", base).destroy(comp),
|
|
|
|
Id.CompTime => @fieldParentPtr(CompTime, "base", base).destroy(comp),
|
|
|
|
Id.Defer => @fieldParentPtr(Defer, "base", base).destroy(comp),
|
|
|
|
Id.DeferExpr => @fieldParentPtr(DeferExpr, "base", base).destroy(comp),
|
2018-07-12 12:08:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn findFnDef(base: *Scope) ?*FnDef {
|
|
|
|
var scope = base;
|
|
|
|
while (true) {
|
|
|
|
switch (scope.id) {
|
|
|
|
Id.FnDef => return @fieldParentPtr(FnDef, "base", base),
|
|
|
|
Id.Decls => return null,
|
|
|
|
|
|
|
|
Id.Block,
|
|
|
|
Id.Defer,
|
|
|
|
Id.DeferExpr,
|
|
|
|
Id.CompTime,
|
|
|
|
=> scope = scope.parent orelse return null,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-26 16:44:08 -08:00
|
|
|
|
2018-07-18 14:40:59 -07:00
|
|
|
pub fn findDeferExpr(base: *Scope) ?*DeferExpr {
|
|
|
|
var scope = base;
|
|
|
|
while (true) {
|
|
|
|
switch (scope.id) {
|
|
|
|
Id.DeferExpr => return @fieldParentPtr(DeferExpr, "base", base),
|
|
|
|
|
|
|
|
Id.FnDef,
|
|
|
|
Id.Decls,
|
|
|
|
=> return null,
|
|
|
|
|
|
|
|
Id.Block,
|
|
|
|
Id.Defer,
|
|
|
|
Id.CompTime,
|
|
|
|
=> scope = scope.parent orelse return null,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-26 16:44:08 -08:00
|
|
|
pub const Id = enum {
|
|
|
|
Decls,
|
|
|
|
Block,
|
|
|
|
FnDef,
|
|
|
|
CompTime,
|
2018-07-12 12:08:40 -07:00
|
|
|
Defer,
|
|
|
|
DeferExpr,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const Decls = struct {
|
|
|
|
base: Scope,
|
|
|
|
table: Decl.Table,
|
|
|
|
|
|
|
|
/// Creates a Decls scope with 1 reference
|
2018-07-14 13:12:41 -07:00
|
|
|
pub fn create(comp: *Compilation, parent: ?*Scope) !*Decls {
|
2018-07-16 17:52:50 -07:00
|
|
|
const self = try comp.gpa().create(Decls{
|
2018-07-12 12:08:40 -07:00
|
|
|
.base = Scope{
|
|
|
|
.id = Id.Decls,
|
|
|
|
.parent = parent,
|
|
|
|
.ref_count = 1,
|
|
|
|
},
|
|
|
|
.table = undefined,
|
|
|
|
});
|
2018-07-16 17:52:50 -07:00
|
|
|
errdefer comp.gpa().destroy(self);
|
2018-07-12 12:08:40 -07:00
|
|
|
|
2018-07-16 17:52:50 -07:00
|
|
|
self.table = Decl.Table.init(comp.gpa());
|
2018-07-12 12:08:40 -07:00
|
|
|
errdefer self.table.deinit();
|
|
|
|
|
|
|
|
if (parent) |p| p.ref();
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn destroy(self: *Decls) void {
|
|
|
|
self.table.deinit();
|
|
|
|
self.table.allocator.destroy(self);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const Block = struct {
|
|
|
|
base: Scope,
|
2018-07-18 14:40:59 -07:00
|
|
|
incoming_values: std.ArrayList(*ir.Inst),
|
2018-07-12 12:08:40 -07:00
|
|
|
incoming_blocks: std.ArrayList(*ir.BasicBlock),
|
|
|
|
end_block: *ir.BasicBlock,
|
2018-07-18 14:40:59 -07:00
|
|
|
is_comptime: *ir.Inst,
|
2018-07-12 12:08:40 -07:00
|
|
|
|
2018-07-14 21:04:12 -07:00
|
|
|
safety: Safety,
|
|
|
|
|
|
|
|
const Safety = union(enum) {
|
|
|
|
Auto,
|
|
|
|
Manual: Manual,
|
|
|
|
|
|
|
|
const Manual = struct {
|
|
|
|
/// the source span that disabled the safety value
|
|
|
|
span: Span,
|
|
|
|
|
|
|
|
/// whether safety is enabled
|
|
|
|
enabled: bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn get(self: Safety, comp: *Compilation) bool {
|
|
|
|
return switch (self) {
|
|
|
|
Safety.Auto => switch (comp.build_mode) {
|
|
|
|
builtin.Mode.Debug,
|
|
|
|
builtin.Mode.ReleaseSafe,
|
|
|
|
=> true,
|
|
|
|
builtin.Mode.ReleaseFast,
|
|
|
|
builtin.Mode.ReleaseSmall,
|
|
|
|
=> false,
|
|
|
|
},
|
|
|
|
@TagType(Safety).Manual => |man| man.enabled,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-12 12:08:40 -07:00
|
|
|
/// Creates a Block scope with 1 reference
|
2018-07-14 13:12:41 -07:00
|
|
|
pub fn create(comp: *Compilation, parent: ?*Scope) !*Block {
|
2018-07-16 17:52:50 -07:00
|
|
|
const self = try comp.gpa().create(Block{
|
2018-07-12 12:08:40 -07:00
|
|
|
.base = Scope{
|
|
|
|
.id = Id.Block,
|
|
|
|
.parent = parent,
|
|
|
|
.ref_count = 1,
|
|
|
|
},
|
|
|
|
.incoming_values = undefined,
|
|
|
|
.incoming_blocks = undefined,
|
|
|
|
.end_block = undefined,
|
|
|
|
.is_comptime = undefined,
|
2018-07-14 21:04:12 -07:00
|
|
|
.safety = Safety.Auto,
|
2018-07-12 12:08:40 -07:00
|
|
|
});
|
2018-07-16 17:52:50 -07:00
|
|
|
errdefer comp.gpa().destroy(self);
|
2018-07-12 12:08:40 -07:00
|
|
|
|
|
|
|
if (parent) |p| p.ref();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-07-14 13:12:41 -07:00
|
|
|
pub fn destroy(self: *Block, comp: *Compilation) void {
|
2018-07-16 17:52:50 -07:00
|
|
|
comp.gpa().destroy(self);
|
2018-07-12 12:08:40 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const FnDef = struct {
|
|
|
|
base: Scope,
|
|
|
|
|
|
|
|
/// This reference is not counted so that the scope can get destroyed with the function
|
|
|
|
fn_val: *Value.Fn,
|
|
|
|
|
|
|
|
/// Creates a FnDef scope with 1 reference
|
|
|
|
/// Must set the fn_val later
|
2018-07-14 13:12:41 -07:00
|
|
|
pub fn create(comp: *Compilation, parent: ?*Scope) !*FnDef {
|
2018-07-16 17:52:50 -07:00
|
|
|
const self = try comp.gpa().create(FnDef{
|
2018-07-12 12:08:40 -07:00
|
|
|
.base = Scope{
|
|
|
|
.id = Id.FnDef,
|
|
|
|
.parent = parent,
|
|
|
|
.ref_count = 1,
|
|
|
|
},
|
|
|
|
.fn_val = undefined,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (parent) |p| p.ref();
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-07-14 13:12:41 -07:00
|
|
|
pub fn destroy(self: *FnDef, comp: *Compilation) void {
|
2018-07-16 17:52:50 -07:00
|
|
|
comp.gpa().destroy(self);
|
2018-07-12 12:08:40 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const CompTime = struct {
|
|
|
|
base: Scope,
|
|
|
|
|
|
|
|
/// Creates a CompTime scope with 1 reference
|
2018-07-14 13:12:41 -07:00
|
|
|
pub fn create(comp: *Compilation, parent: ?*Scope) !*CompTime {
|
2018-07-16 17:52:50 -07:00
|
|
|
const self = try comp.gpa().create(CompTime{
|
2018-07-12 12:08:40 -07:00
|
|
|
.base = Scope{
|
|
|
|
.id = Id.CompTime,
|
|
|
|
.parent = parent,
|
|
|
|
.ref_count = 1,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (parent) |p| p.ref();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-07-14 13:12:41 -07:00
|
|
|
pub fn destroy(self: *CompTime, comp: *Compilation) void {
|
2018-07-16 17:52:50 -07:00
|
|
|
comp.gpa().destroy(self);
|
2018-07-12 12:08:40 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const Defer = struct {
|
|
|
|
base: Scope,
|
|
|
|
defer_expr_scope: *DeferExpr,
|
|
|
|
kind: Kind,
|
|
|
|
|
|
|
|
pub const Kind = enum {
|
|
|
|
ScopeExit,
|
|
|
|
ErrorExit,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Creates a Defer scope with 1 reference
|
|
|
|
pub fn create(
|
2018-07-14 13:12:41 -07:00
|
|
|
comp: *Compilation,
|
2018-07-12 12:08:40 -07:00
|
|
|
parent: ?*Scope,
|
|
|
|
kind: Kind,
|
|
|
|
defer_expr_scope: *DeferExpr,
|
|
|
|
) !*Defer {
|
2018-07-16 17:52:50 -07:00
|
|
|
const self = try comp.gpa().create(Defer{
|
2018-07-12 12:08:40 -07:00
|
|
|
.base = Scope{
|
|
|
|
.id = Id.Defer,
|
|
|
|
.parent = parent,
|
|
|
|
.ref_count = 1,
|
|
|
|
},
|
|
|
|
.defer_expr_scope = defer_expr_scope,
|
|
|
|
.kind = kind,
|
|
|
|
});
|
2018-07-16 17:52:50 -07:00
|
|
|
errdefer comp.gpa().destroy(self);
|
2018-07-12 12:08:40 -07:00
|
|
|
|
|
|
|
defer_expr_scope.base.ref();
|
|
|
|
|
|
|
|
if (parent) |p| p.ref();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-07-14 13:12:41 -07:00
|
|
|
pub fn destroy(self: *Defer, comp: *Compilation) void {
|
|
|
|
self.defer_expr_scope.base.deref(comp);
|
2018-07-16 17:52:50 -07:00
|
|
|
comp.gpa().destroy(self);
|
2018-07-12 12:08:40 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const DeferExpr = struct {
|
|
|
|
base: Scope,
|
|
|
|
expr_node: *ast.Node,
|
2018-07-18 14:40:59 -07:00
|
|
|
reported_err: bool,
|
2018-07-12 12:08:40 -07:00
|
|
|
|
|
|
|
/// Creates a DeferExpr scope with 1 reference
|
2018-07-14 13:12:41 -07:00
|
|
|
pub fn create(comp: *Compilation, parent: ?*Scope, expr_node: *ast.Node) !*DeferExpr {
|
2018-07-16 17:52:50 -07:00
|
|
|
const self = try comp.gpa().create(DeferExpr{
|
2018-07-12 12:08:40 -07:00
|
|
|
.base = Scope{
|
|
|
|
.id = Id.DeferExpr,
|
|
|
|
.parent = parent,
|
|
|
|
.ref_count = 1,
|
|
|
|
},
|
|
|
|
.expr_node = expr_node,
|
2018-07-18 14:40:59 -07:00
|
|
|
.reported_err = false,
|
2018-07-12 12:08:40 -07:00
|
|
|
});
|
2018-07-16 17:52:50 -07:00
|
|
|
errdefer comp.gpa().destroy(self);
|
2018-07-12 12:08:40 -07:00
|
|
|
|
|
|
|
if (parent) |p| p.ref();
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-07-14 13:12:41 -07:00
|
|
|
pub fn destroy(self: *DeferExpr, comp: *Compilation) void {
|
2018-07-16 17:52:50 -07:00
|
|
|
comp.gpa().destroy(self);
|
2018-07-12 12:08:40 -07:00
|
|
|
}
|
2017-12-26 16:44:08 -08:00
|
|
|
};
|
|
|
|
};
|