2017-12-22 21:29:39 -08:00
|
|
|
const std = @import("std");
|
|
|
|
const os = std.os;
|
|
|
|
const io = std.io;
|
|
|
|
const mem = std.mem;
|
2018-07-05 12:09:02 -07:00
|
|
|
const Allocator = mem.Allocator;
|
2017-12-22 21:29:39 -08:00
|
|
|
const Buffer = std.Buffer;
|
|
|
|
const llvm = @import("llvm.zig");
|
|
|
|
const c = @import("c.zig");
|
|
|
|
const builtin = @import("builtin");
|
|
|
|
const Target = @import("target.zig").Target;
|
|
|
|
const warn = std.debug.warn;
|
2018-02-09 10:08:02 -08:00
|
|
|
const Token = std.zig.Token;
|
2017-12-22 21:29:39 -08:00
|
|
|
const ArrayList = std.ArrayList;
|
2018-05-30 15:26:09 -07:00
|
|
|
const errmsg = @import("errmsg.zig");
|
2018-06-29 12:39:55 -07:00
|
|
|
const ast = std.zig.ast;
|
|
|
|
const event = std.event;
|
2018-07-05 12:09:02 -07:00
|
|
|
const assert = std.debug.assert;
|
2018-07-12 12:08:40 -07:00
|
|
|
const AtomicRmwOp = builtin.AtomicRmwOp;
|
|
|
|
const AtomicOrder = builtin.AtomicOrder;
|
|
|
|
const Scope = @import("scope.zig").Scope;
|
|
|
|
const Decl = @import("decl.zig").Decl;
|
|
|
|
const ir = @import("ir.zig");
|
|
|
|
const Visib = @import("visib.zig").Visib;
|
|
|
|
const Value = @import("value.zig").Value;
|
|
|
|
const Type = Value.Type;
|
2018-07-13 18:56:38 -07:00
|
|
|
const Span = errmsg.Span;
|
2018-07-23 11:28:14 -07:00
|
|
|
const Msg = errmsg.Msg;
|
2018-07-14 12:45:15 -07:00
|
|
|
const codegen = @import("codegen.zig");
|
2018-07-16 17:52:50 -07:00
|
|
|
const Package = @import("package.zig").Package;
|
2018-07-17 10:18:13 -07:00
|
|
|
const link = @import("link.zig").link;
|
2018-07-17 15:36:47 -07:00
|
|
|
const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
|
2018-07-18 21:08:47 -07:00
|
|
|
const CInt = @import("c_int.zig").CInt;
|
2018-07-25 20:34:57 -07:00
|
|
|
const fs = event.fs;
|
|
|
|
|
|
|
|
const max_src_size = 2 * 1024 * 1024 * 1024; // 2 GiB
|
2018-07-14 12:45:15 -07:00
|
|
|
|
|
|
|
/// Data that is local to the event loop.
|
2018-08-10 09:28:20 -07:00
|
|
|
pub const ZigCompiler = struct {
|
2018-07-14 12:45:15 -07:00
|
|
|
loop: *event.Loop,
|
|
|
|
llvm_handle_pool: std.atomic.Stack(llvm.ContextRef),
|
2018-07-24 18:28:54 -07:00
|
|
|
lld_lock: event.Lock,
|
2018-07-14 12:45:15 -07:00
|
|
|
|
2018-07-16 17:52:50 -07:00
|
|
|
/// TODO pool these so that it doesn't have to lock
|
|
|
|
prng: event.Locked(std.rand.DefaultPrng),
|
|
|
|
|
2018-07-17 15:36:47 -07:00
|
|
|
native_libc: event.Future(LibCInstallation),
|
|
|
|
|
2018-07-16 17:52:50 -07:00
|
|
|
var lazy_init_targets = std.lazyInit(void);
|
|
|
|
|
2018-08-10 09:28:20 -07:00
|
|
|
fn init(loop: *event.Loop) !ZigCompiler {
|
2018-07-16 17:52:50 -07:00
|
|
|
lazy_init_targets.get() orelse {
|
|
|
|
Target.initializeAll();
|
|
|
|
lazy_init_targets.resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
var seed_bytes: [@sizeOf(u64)]u8 = undefined;
|
|
|
|
try std.os.getRandomBytes(seed_bytes[0..]);
|
|
|
|
const seed = std.mem.readInt(seed_bytes, u64, builtin.Endian.Big);
|
2018-07-19 20:52:44 -07:00
|
|
|
|
2018-08-10 09:28:20 -07:00
|
|
|
return ZigCompiler{
|
2018-07-14 12:45:15 -07:00
|
|
|
.loop = loop,
|
2018-07-24 18:28:54 -07:00
|
|
|
.lld_lock = event.Lock.init(loop),
|
2018-07-14 12:45:15 -07:00
|
|
|
.llvm_handle_pool = std.atomic.Stack(llvm.ContextRef).init(),
|
2018-07-16 17:52:50 -07:00
|
|
|
.prng = event.Locked(std.rand.DefaultPrng).init(loop, std.rand.DefaultPrng.init(seed)),
|
2018-07-17 15:36:47 -07:00
|
|
|
.native_libc = event.Future(LibCInstallation).init(loop),
|
2018-07-14 12:45:15 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-07-18 21:08:47 -07:00
|
|
|
/// Must be called only after EventLoop.run completes.
|
2018-08-10 09:28:20 -07:00
|
|
|
fn deinit(self: *ZigCompiler) void {
|
2018-07-24 18:28:54 -07:00
|
|
|
self.lld_lock.deinit();
|
2018-07-14 12:45:15 -07:00
|
|
|
while (self.llvm_handle_pool.pop()) |node| {
|
|
|
|
c.LLVMContextDispose(node.data);
|
|
|
|
self.loop.allocator.destroy(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets an exclusive handle on any LlvmContext.
|
|
|
|
/// Caller must release the handle when done.
|
2018-08-10 09:28:20 -07:00
|
|
|
pub fn getAnyLlvmContext(self: *ZigCompiler) !LlvmHandle {
|
2018-07-14 12:45:15 -07:00
|
|
|
if (self.llvm_handle_pool.pop()) |node| return LlvmHandle{ .node = node };
|
|
|
|
|
|
|
|
const context_ref = c.LLVMContextCreate() orelse return error.OutOfMemory;
|
|
|
|
errdefer c.LLVMContextDispose(context_ref);
|
|
|
|
|
|
|
|
const node = try self.loop.allocator.create(std.atomic.Stack(llvm.ContextRef).Node{
|
|
|
|
.next = undefined,
|
|
|
|
.data = context_ref,
|
|
|
|
});
|
|
|
|
errdefer self.loop.allocator.destroy(node);
|
|
|
|
|
|
|
|
return LlvmHandle{ .node = node };
|
|
|
|
}
|
2018-07-17 15:36:47 -07:00
|
|
|
|
2018-08-10 09:28:20 -07:00
|
|
|
pub async fn getNativeLibC(self: *ZigCompiler) !*LibCInstallation {
|
2018-07-17 15:36:47 -07:00
|
|
|
if (await (async self.native_libc.start() catch unreachable)) |ptr| return ptr;
|
|
|
|
try await (async self.native_libc.data.findNative(self.loop) catch unreachable);
|
|
|
|
self.native_libc.resolve();
|
|
|
|
return &self.native_libc.data;
|
|
|
|
}
|
2018-08-10 09:28:20 -07:00
|
|
|
|
|
|
|
/// Must be called only once, ever. Sets global state.
|
|
|
|
pub fn setLlvmArgv(allocator: *Allocator, llvm_argv: []const []const u8) !void {
|
|
|
|
if (llvm_argv.len != 0) {
|
|
|
|
var c_compatible_args = try std.cstr.NullTerminated2DArray.fromSlices(allocator, [][]const []const u8{
|
|
|
|
[][]const u8{"zig (LLVM option parsing)"},
|
|
|
|
llvm_argv,
|
|
|
|
});
|
|
|
|
defer c_compatible_args.deinit();
|
|
|
|
c.ZigLLVMParseCommandLineOptions(llvm_argv.len + 1, c_compatible_args.ptr);
|
|
|
|
}
|
|
|
|
}
|
2018-07-14 12:45:15 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
pub const LlvmHandle = struct {
|
|
|
|
node: *std.atomic.Stack(llvm.ContextRef).Node,
|
|
|
|
|
2018-08-10 09:28:20 -07:00
|
|
|
pub fn release(self: LlvmHandle, zig_compiler: *ZigCompiler) void {
|
|
|
|
zig_compiler.llvm_handle_pool.push(self.node);
|
2018-07-14 12:45:15 -07:00
|
|
|
}
|
|
|
|
};
|
2017-12-22 21:29:39 -08:00
|
|
|
|
2018-07-14 13:12:41 -07:00
|
|
|
pub const Compilation = struct {
|
2018-08-10 09:28:20 -07:00
|
|
|
zig_compiler: *ZigCompiler,
|
2018-06-29 12:39:55 -07:00
|
|
|
loop: *event.Loop,
|
2017-12-22 21:29:39 -08:00
|
|
|
name: Buffer,
|
2018-07-16 17:52:50 -07:00
|
|
|
llvm_triple: Buffer,
|
2017-12-22 21:29:39 -08:00
|
|
|
root_src_path: ?[]const u8,
|
|
|
|
target: Target,
|
2018-07-16 17:52:50 -07:00
|
|
|
llvm_target: llvm.TargetRef,
|
2017-12-22 21:29:39 -08:00
|
|
|
build_mode: builtin.Mode,
|
|
|
|
zig_lib_dir: []const u8,
|
2018-07-16 17:52:50 -07:00
|
|
|
zig_std_dir: []const u8,
|
|
|
|
|
|
|
|
/// lazily created when we need it
|
|
|
|
tmp_dir: event.Future(BuildError![]u8),
|
2017-12-22 21:29:39 -08:00
|
|
|
|
|
|
|
version_major: u32,
|
|
|
|
version_minor: u32,
|
|
|
|
version_patch: u32,
|
|
|
|
|
|
|
|
linker_script: ?[]const u8,
|
|
|
|
out_h_path: ?[]const u8,
|
|
|
|
|
|
|
|
is_test: bool,
|
|
|
|
each_lib_rpath: bool,
|
|
|
|
strip: bool,
|
|
|
|
is_static: bool,
|
|
|
|
linker_rdynamic: bool,
|
|
|
|
|
|
|
|
clang_argv: []const []const u8,
|
|
|
|
lib_dirs: []const []const u8,
|
|
|
|
rpath_list: []const []const u8,
|
|
|
|
assembly_files: []const []const u8,
|
2018-07-16 17:52:50 -07:00
|
|
|
|
|
|
|
/// paths that are explicitly provided by the user to link against
|
2017-12-22 21:29:39 -08:00
|
|
|
link_objects: []const []const u8,
|
|
|
|
|
2018-07-16 17:52:50 -07:00
|
|
|
/// functions that have their own objects that we need to link
|
|
|
|
/// it uses an optional pointer so that tombstone removals are possible
|
|
|
|
fn_link_set: event.Locked(FnLinkSet),
|
|
|
|
|
|
|
|
pub const FnLinkSet = std.LinkedList(?*Value.Fn);
|
|
|
|
|
2017-12-22 21:29:39 -08:00
|
|
|
windows_subsystem_windows: bool,
|
|
|
|
windows_subsystem_console: bool,
|
|
|
|
|
2018-05-31 07:56:59 -07:00
|
|
|
link_libs_list: ArrayList(*LinkLib),
|
|
|
|
libc_link_lib: ?*LinkLib,
|
2017-12-22 21:29:39 -08:00
|
|
|
|
2018-05-30 15:26:09 -07:00
|
|
|
err_color: errmsg.Color,
|
2017-12-22 21:29:39 -08:00
|
|
|
|
|
|
|
verbose_tokenize: bool,
|
|
|
|
verbose_ast_tree: bool,
|
|
|
|
verbose_ast_fmt: bool,
|
|
|
|
verbose_cimport: bool,
|
|
|
|
verbose_ir: bool,
|
|
|
|
verbose_llvm_ir: bool,
|
|
|
|
verbose_link: bool,
|
|
|
|
|
|
|
|
darwin_frameworks: []const []const u8,
|
|
|
|
darwin_version_min: DarwinVersionMin,
|
|
|
|
|
|
|
|
test_filters: []const []const u8,
|
|
|
|
test_name_prefix: ?[]const u8,
|
|
|
|
|
|
|
|
emit_file_type: Emit,
|
|
|
|
|
|
|
|
kind: Kind,
|
|
|
|
|
2018-06-29 12:39:55 -07:00
|
|
|
link_out_file: ?[]const u8,
|
|
|
|
events: *event.Channel(Event),
|
|
|
|
|
2018-07-05 12:09:02 -07:00
|
|
|
exported_symbol_names: event.Locked(Decl.Table),
|
|
|
|
|
2018-07-10 12:17:01 -07:00
|
|
|
/// Before code generation starts, must wait on this group to make sure
|
|
|
|
/// the build is complete.
|
2018-07-16 17:52:50 -07:00
|
|
|
prelink_group: event.Group(BuildError!void),
|
2018-07-10 12:17:01 -07:00
|
|
|
|
2018-07-10 17:18:43 -07:00
|
|
|
compile_errors: event.Locked(CompileErrList),
|
2018-07-10 12:17:01 -07:00
|
|
|
|
2018-07-12 12:08:40 -07:00
|
|
|
meta_type: *Type.MetaType,
|
|
|
|
void_type: *Type.Void,
|
|
|
|
bool_type: *Type.Bool,
|
|
|
|
noreturn_type: *Type.NoReturn,
|
2018-07-18 21:08:47 -07:00
|
|
|
comptime_int_type: *Type.ComptimeInt,
|
2018-07-22 20:27:58 -07:00
|
|
|
u8_type: *Type.Int,
|
2018-07-12 12:08:40 -07:00
|
|
|
|
|
|
|
void_value: *Value.Void,
|
|
|
|
true_value: *Value.Bool,
|
|
|
|
false_value: *Value.Bool,
|
|
|
|
noreturn_value: *Value.NoReturn,
|
|
|
|
|
2018-07-16 17:52:50 -07:00
|
|
|
target_machine: llvm.TargetMachineRef,
|
|
|
|
target_data_ref: llvm.TargetDataRef,
|
|
|
|
target_layout_str: [*]u8,
|
2018-07-22 20:27:58 -07:00
|
|
|
target_ptr_bits: u32,
|
2018-07-16 17:52:50 -07:00
|
|
|
|
|
|
|
/// for allocating things which have the same lifetime as this Compilation
|
|
|
|
arena_allocator: std.heap.ArenaAllocator,
|
|
|
|
|
|
|
|
root_package: *Package,
|
|
|
|
std_package: *Package,
|
|
|
|
|
2018-07-17 21:34:42 -07:00
|
|
|
override_libc: ?*LibCInstallation,
|
|
|
|
|
|
|
|
/// need to wait on this group before deinitializing
|
|
|
|
deinit_group: event.Group(void),
|
|
|
|
|
|
|
|
destroy_handle: promise,
|
2018-08-10 09:28:20 -07:00
|
|
|
main_loop_handle: promise,
|
|
|
|
main_loop_future: event.Future(void),
|
2018-07-17 21:34:42 -07:00
|
|
|
|
2018-07-18 14:40:59 -07:00
|
|
|
have_err_ret_tracing: bool,
|
|
|
|
|
2018-07-18 21:08:47 -07:00
|
|
|
/// not locked because it is read-only
|
|
|
|
primitive_type_table: TypeTable,
|
|
|
|
|
|
|
|
int_type_table: event.Locked(IntTypeTable),
|
2018-07-22 20:27:58 -07:00
|
|
|
array_type_table: event.Locked(ArrayTypeTable),
|
|
|
|
ptr_type_table: event.Locked(PtrTypeTable),
|
2018-07-24 11:20:49 -07:00
|
|
|
fn_type_table: event.Locked(FnTypeTable),
|
2018-07-18 21:08:47 -07:00
|
|
|
|
|
|
|
c_int_types: [CInt.list.len]*Type.Int,
|
|
|
|
|
2018-08-03 14:22:17 -07:00
|
|
|
fs_watch: *fs.Watch(*Scope.Root),
|
|
|
|
|
2018-07-18 21:08:47 -07:00
|
|
|
const IntTypeTable = std.HashMap(*const Type.Int.Key, *Type.Int, Type.Int.Key.hash, Type.Int.Key.eql);
|
2018-07-22 20:27:58 -07:00
|
|
|
const ArrayTypeTable = std.HashMap(*const Type.Array.Key, *Type.Array, Type.Array.Key.hash, Type.Array.Key.eql);
|
|
|
|
const PtrTypeTable = std.HashMap(*const Type.Pointer.Key, *Type.Pointer, Type.Pointer.Key.hash, Type.Pointer.Key.eql);
|
2018-07-24 11:20:49 -07:00
|
|
|
const FnTypeTable = std.HashMap(*const Type.Fn.Key, *Type.Fn, Type.Fn.Key.hash, Type.Fn.Key.eql);
|
2018-07-18 21:08:47 -07:00
|
|
|
const TypeTable = std.HashMap([]const u8, *Type, mem.hash_slice_u8, mem.eql_slice_u8);
|
|
|
|
|
2018-07-23 11:28:14 -07:00
|
|
|
const CompileErrList = std.ArrayList(*Msg);
|
2018-07-10 12:17:01 -07:00
|
|
|
|
2018-06-29 12:39:55 -07:00
|
|
|
// TODO handle some of these earlier and report them in a way other than error codes
|
|
|
|
pub const BuildError = error{
|
|
|
|
OutOfMemory,
|
|
|
|
EndOfStream,
|
|
|
|
BadFd,
|
|
|
|
Io,
|
|
|
|
IsDir,
|
|
|
|
Unexpected,
|
|
|
|
SystemResources,
|
|
|
|
SharingViolation,
|
|
|
|
PathAlreadyExists,
|
|
|
|
FileNotFound,
|
|
|
|
AccessDenied,
|
|
|
|
PipeBusy,
|
|
|
|
FileTooBig,
|
|
|
|
SymLinkLoop,
|
|
|
|
ProcessFdQuotaExceeded,
|
|
|
|
NameTooLong,
|
|
|
|
SystemFdQuotaExceeded,
|
|
|
|
NoDevice,
|
|
|
|
PathNotFound,
|
|
|
|
NoSpaceLeft,
|
|
|
|
NotDir,
|
|
|
|
FileSystem,
|
|
|
|
OperationAborted,
|
|
|
|
IoPending,
|
|
|
|
BrokenPipe,
|
|
|
|
WouldBlock,
|
|
|
|
FileClosed,
|
|
|
|
DestinationAddressRequired,
|
|
|
|
DiskQuota,
|
|
|
|
InputOutput,
|
|
|
|
NoStdHandles,
|
2018-07-02 12:25:23 -07:00
|
|
|
Overflow,
|
2018-07-02 14:55:32 -07:00
|
|
|
NotSupported,
|
2018-07-10 17:18:43 -07:00
|
|
|
BufferTooSmall,
|
2018-07-13 18:56:38 -07:00
|
|
|
Unimplemented, // TODO remove this one
|
|
|
|
SemanticAnalysisFailed, // TODO remove this one
|
2018-07-16 17:52:50 -07:00
|
|
|
ReadOnlyFileSystem,
|
|
|
|
LinkQuotaExceeded,
|
|
|
|
EnvironmentVariableNotFound,
|
2018-07-16 21:01:36 -07:00
|
|
|
AppDataDirUnavailable,
|
2018-07-17 10:18:13 -07:00
|
|
|
LinkFailed,
|
2018-07-17 21:34:42 -07:00
|
|
|
LibCRequiredButNotProvidedOrFound,
|
|
|
|
LibCMissingDynamicLinker,
|
2018-07-23 14:38:03 -07:00
|
|
|
InvalidDarwinVersionString,
|
2018-07-23 21:06:34 -07:00
|
|
|
UnsupportedLinkArchitecture,
|
2018-08-03 14:22:17 -07:00
|
|
|
UserResourceLimitReached,
|
2018-08-09 13:48:44 -07:00
|
|
|
InvalidUtf8,
|
2018-08-21 13:07:28 -07:00
|
|
|
BadPathName,
|
2018-06-29 12:39:55 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
pub const Event = union(enum) {
|
|
|
|
Ok,
|
|
|
|
Error: BuildError,
|
2018-07-23 11:28:14 -07:00
|
|
|
Fail: []*Msg,
|
2018-06-29 12:39:55 -07:00
|
|
|
};
|
|
|
|
|
2017-12-22 21:29:39 -08:00
|
|
|
pub const DarwinVersionMin = union(enum) {
|
|
|
|
None,
|
|
|
|
MacOS: []const u8,
|
|
|
|
Ios: []const u8,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const Kind = enum {
|
|
|
|
Exe,
|
|
|
|
Lib,
|
|
|
|
Obj,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const LinkLib = struct {
|
|
|
|
name: []const u8,
|
|
|
|
path: ?[]const u8,
|
2018-05-17 20:21:44 -07:00
|
|
|
|
2017-12-22 21:29:39 -08:00
|
|
|
/// the list of symbols we depend on from this lib
|
|
|
|
symbols: ArrayList([]u8),
|
|
|
|
provided_explicitly: bool,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const Emit = enum {
|
|
|
|
Binary,
|
|
|
|
Assembly,
|
|
|
|
LlvmIr,
|
|
|
|
};
|
|
|
|
|
2018-06-20 14:33:29 -07:00
|
|
|
pub fn create(
|
2018-08-10 09:28:20 -07:00
|
|
|
zig_compiler: *ZigCompiler,
|
2018-06-20 14:33:29 -07:00
|
|
|
name: []const u8,
|
|
|
|
root_src_path: ?[]const u8,
|
2018-07-16 17:52:50 -07:00
|
|
|
target: Target,
|
2018-06-20 14:33:29 -07:00
|
|
|
kind: Kind,
|
|
|
|
build_mode: builtin.Mode,
|
2018-07-16 17:52:50 -07:00
|
|
|
is_static: bool,
|
2018-06-20 14:33:29 -07:00
|
|
|
zig_lib_dir: []const u8,
|
2018-07-14 13:12:41 -07:00
|
|
|
) !*Compilation {
|
2018-08-10 09:28:20 -07:00
|
|
|
var optional_comp: ?*Compilation = null;
|
|
|
|
const handle = try async<zig_compiler.loop.allocator> createAsync(
|
|
|
|
&optional_comp,
|
|
|
|
zig_compiler,
|
|
|
|
name,
|
|
|
|
root_src_path,
|
|
|
|
target,
|
|
|
|
kind,
|
|
|
|
build_mode,
|
|
|
|
is_static,
|
|
|
|
zig_lib_dir,
|
|
|
|
);
|
|
|
|
return optional_comp orelse if (getAwaitResult(
|
|
|
|
zig_compiler.loop.allocator,
|
|
|
|
handle,
|
|
|
|
)) |_| unreachable else |err| err;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn createAsync(
|
|
|
|
out_comp: *?*Compilation,
|
|
|
|
zig_compiler: *ZigCompiler,
|
|
|
|
name: []const u8,
|
|
|
|
root_src_path: ?[]const u8,
|
|
|
|
target: Target,
|
|
|
|
kind: Kind,
|
|
|
|
build_mode: builtin.Mode,
|
|
|
|
is_static: bool,
|
|
|
|
zig_lib_dir: []const u8,
|
|
|
|
) !void {
|
|
|
|
// workaround for https://github.com/ziglang/zig/issues/1194
|
|
|
|
suspend {
|
|
|
|
resume @handle();
|
|
|
|
}
|
|
|
|
|
|
|
|
const loop = zig_compiler.loop;
|
|
|
|
var comp = Compilation{
|
2018-06-29 12:39:55 -07:00
|
|
|
.loop = loop,
|
2018-07-16 17:52:50 -07:00
|
|
|
.arena_allocator = std.heap.ArenaAllocator.init(loop.allocator),
|
2018-08-10 09:28:20 -07:00
|
|
|
.zig_compiler = zig_compiler,
|
2018-07-16 17:52:50 -07:00
|
|
|
.events = undefined,
|
2017-12-22 21:29:39 -08:00
|
|
|
.root_src_path = root_src_path,
|
2018-07-16 17:52:50 -07:00
|
|
|
.target = target,
|
|
|
|
.llvm_target = undefined,
|
2017-12-22 21:29:39 -08:00
|
|
|
.kind = kind,
|
|
|
|
.build_mode = build_mode,
|
|
|
|
.zig_lib_dir = zig_lib_dir,
|
2018-07-16 17:52:50 -07:00
|
|
|
.zig_std_dir = undefined,
|
|
|
|
.tmp_dir = event.Future(BuildError![]u8).init(loop),
|
2018-08-10 09:28:20 -07:00
|
|
|
.destroy_handle = @handle(),
|
|
|
|
.main_loop_handle = undefined,
|
|
|
|
.main_loop_future = event.Future(void).init(loop),
|
2018-07-16 17:52:50 -07:00
|
|
|
|
|
|
|
.name = undefined,
|
|
|
|
.llvm_triple = undefined,
|
2017-12-22 21:29:39 -08:00
|
|
|
|
|
|
|
.version_major = 0,
|
|
|
|
.version_minor = 0,
|
|
|
|
.version_patch = 0,
|
|
|
|
|
|
|
|
.verbose_tokenize = false,
|
|
|
|
.verbose_ast_tree = false,
|
|
|
|
.verbose_ast_fmt = false,
|
|
|
|
.verbose_cimport = false,
|
|
|
|
.verbose_ir = false,
|
|
|
|
.verbose_llvm_ir = false,
|
|
|
|
.verbose_link = false,
|
|
|
|
|
|
|
|
.linker_script = null,
|
|
|
|
.out_h_path = null,
|
|
|
|
.is_test = false,
|
|
|
|
.each_lib_rpath = false,
|
|
|
|
.strip = false,
|
2018-07-16 17:52:50 -07:00
|
|
|
.is_static = is_static,
|
2017-12-22 21:29:39 -08:00
|
|
|
.linker_rdynamic = false,
|
|
|
|
.clang_argv = [][]const u8{},
|
|
|
|
.lib_dirs = [][]const u8{},
|
|
|
|
.rpath_list = [][]const u8{},
|
|
|
|
.assembly_files = [][]const u8{},
|
|
|
|
.link_objects = [][]const u8{},
|
2018-07-16 17:52:50 -07:00
|
|
|
.fn_link_set = event.Locked(FnLinkSet).init(loop, FnLinkSet.init()),
|
2017-12-22 21:29:39 -08:00
|
|
|
.windows_subsystem_windows = false,
|
|
|
|
.windows_subsystem_console = false,
|
2018-08-10 09:28:20 -07:00
|
|
|
.link_libs_list = undefined,
|
2017-12-22 21:29:39 -08:00
|
|
|
.libc_link_lib = null,
|
2018-05-30 15:26:09 -07:00
|
|
|
.err_color = errmsg.Color.Auto,
|
2017-12-22 21:29:39 -08:00
|
|
|
.darwin_frameworks = [][]const u8{},
|
|
|
|
.darwin_version_min = DarwinVersionMin.None,
|
|
|
|
.test_filters = [][]const u8{},
|
|
|
|
.test_name_prefix = null,
|
|
|
|
.emit_file_type = Emit.Binary,
|
2018-06-29 12:39:55 -07:00
|
|
|
.link_out_file = null,
|
2018-07-05 12:09:02 -07:00
|
|
|
.exported_symbol_names = event.Locked(Decl.Table).init(loop, Decl.Table.init(loop.allocator)),
|
2018-07-16 17:52:50 -07:00
|
|
|
.prelink_group = event.Group(BuildError!void).init(loop),
|
2018-07-17 21:34:42 -07:00
|
|
|
.deinit_group = event.Group(void).init(loop),
|
2018-07-10 17:18:43 -07:00
|
|
|
.compile_errors = event.Locked(CompileErrList).init(loop, CompileErrList.init(loop.allocator)),
|
2018-07-18 21:08:47 -07:00
|
|
|
.int_type_table = event.Locked(IntTypeTable).init(loop, IntTypeTable.init(loop.allocator)),
|
2018-07-22 20:27:58 -07:00
|
|
|
.array_type_table = event.Locked(ArrayTypeTable).init(loop, ArrayTypeTable.init(loop.allocator)),
|
|
|
|
.ptr_type_table = event.Locked(PtrTypeTable).init(loop, PtrTypeTable.init(loop.allocator)),
|
2018-07-24 11:20:49 -07:00
|
|
|
.fn_type_table = event.Locked(FnTypeTable).init(loop, FnTypeTable.init(loop.allocator)),
|
2018-07-18 21:08:47 -07:00
|
|
|
.c_int_types = undefined,
|
2018-07-12 12:08:40 -07:00
|
|
|
|
|
|
|
.meta_type = undefined,
|
|
|
|
.void_type = undefined,
|
|
|
|
.void_value = undefined,
|
|
|
|
.bool_type = undefined,
|
|
|
|
.true_value = undefined,
|
|
|
|
.false_value = undefined,
|
|
|
|
.noreturn_type = undefined,
|
|
|
|
.noreturn_value = undefined,
|
2018-07-18 21:08:47 -07:00
|
|
|
.comptime_int_type = undefined,
|
2018-07-22 20:27:58 -07:00
|
|
|
.u8_type = undefined,
|
2018-07-16 17:52:50 -07:00
|
|
|
|
|
|
|
.target_machine = undefined,
|
|
|
|
.target_data_ref = undefined,
|
|
|
|
.target_layout_str = undefined,
|
2018-07-22 20:27:58 -07:00
|
|
|
.target_ptr_bits = target.getArchPtrBitWidth(),
|
2018-07-16 17:52:50 -07:00
|
|
|
|
|
|
|
.root_package = undefined,
|
|
|
|
.std_package = undefined,
|
2018-07-17 21:34:42 -07:00
|
|
|
|
|
|
|
.override_libc = null,
|
2018-07-18 14:40:59 -07:00
|
|
|
.have_err_ret_tracing = false,
|
2018-08-10 09:28:20 -07:00
|
|
|
.primitive_type_table = undefined,
|
2018-08-03 14:22:17 -07:00
|
|
|
|
|
|
|
.fs_watch = undefined,
|
|
|
|
};
|
2018-08-10 09:28:20 -07:00
|
|
|
comp.link_libs_list = ArrayList(*LinkLib).init(comp.arena());
|
|
|
|
comp.primitive_type_table = TypeTable.init(comp.arena());
|
|
|
|
|
|
|
|
defer {
|
2018-07-18 21:08:47 -07:00
|
|
|
comp.int_type_table.private_data.deinit();
|
2018-07-22 20:27:58 -07:00
|
|
|
comp.array_type_table.private_data.deinit();
|
|
|
|
comp.ptr_type_table.private_data.deinit();
|
2018-07-24 11:20:49 -07:00
|
|
|
comp.fn_type_table.private_data.deinit();
|
2018-07-16 17:52:50 -07:00
|
|
|
comp.arena_allocator.deinit();
|
|
|
|
}
|
|
|
|
|
|
|
|
comp.name = try Buffer.init(comp.arena(), name);
|
|
|
|
comp.llvm_triple = try target.getTriple(comp.arena());
|
|
|
|
comp.llvm_target = try Target.llvmTargetFromTriple(comp.llvm_triple);
|
|
|
|
comp.zig_std_dir = try std.os.path.join(comp.arena(), zig_lib_dir, "std");
|
|
|
|
|
|
|
|
const opt_level = switch (build_mode) {
|
|
|
|
builtin.Mode.Debug => llvm.CodeGenLevelNone,
|
|
|
|
else => llvm.CodeGenLevelAggressive,
|
|
|
|
};
|
|
|
|
|
|
|
|
const reloc_mode = if (is_static) llvm.RelocStatic else llvm.RelocPIC;
|
|
|
|
|
|
|
|
// LLVM creates invalid binaries on Windows sometimes.
|
|
|
|
// See https://github.com/ziglang/zig/issues/508
|
|
|
|
// As a workaround we do not use target native features on Windows.
|
|
|
|
var target_specific_cpu_args: ?[*]u8 = null;
|
|
|
|
var target_specific_cpu_features: ?[*]u8 = null;
|
2018-08-10 09:28:20 -07:00
|
|
|
defer llvm.DisposeMessage(target_specific_cpu_args);
|
|
|
|
defer llvm.DisposeMessage(target_specific_cpu_features);
|
2018-07-16 17:52:50 -07:00
|
|
|
if (target == Target.Native and !target.isWindows()) {
|
|
|
|
target_specific_cpu_args = llvm.GetHostCPUName() orelse return error.OutOfMemory;
|
|
|
|
target_specific_cpu_features = llvm.GetNativeFeatures() orelse return error.OutOfMemory;
|
|
|
|
}
|
|
|
|
|
|
|
|
comp.target_machine = llvm.CreateTargetMachine(
|
|
|
|
comp.llvm_target,
|
|
|
|
comp.llvm_triple.ptr(),
|
|
|
|
target_specific_cpu_args orelse c"",
|
|
|
|
target_specific_cpu_features orelse c"",
|
|
|
|
opt_level,
|
|
|
|
reloc_mode,
|
|
|
|
llvm.CodeModelDefault,
|
|
|
|
) orelse return error.OutOfMemory;
|
2018-08-10 09:28:20 -07:00
|
|
|
defer llvm.DisposeTargetMachine(comp.target_machine);
|
2018-07-16 17:52:50 -07:00
|
|
|
|
|
|
|
comp.target_data_ref = llvm.CreateTargetDataLayout(comp.target_machine) orelse return error.OutOfMemory;
|
2018-08-10 09:28:20 -07:00
|
|
|
defer llvm.DisposeTargetData(comp.target_data_ref);
|
2018-07-16 17:52:50 -07:00
|
|
|
|
|
|
|
comp.target_layout_str = llvm.CopyStringRepOfTargetData(comp.target_data_ref) orelse return error.OutOfMemory;
|
2018-08-10 09:28:20 -07:00
|
|
|
defer llvm.DisposeMessage(comp.target_layout_str);
|
2018-07-16 17:52:50 -07:00
|
|
|
|
|
|
|
comp.events = try event.Channel(Event).create(comp.loop, 0);
|
2018-08-10 09:28:20 -07:00
|
|
|
defer comp.events.destroy();
|
2018-07-16 17:52:50 -07:00
|
|
|
|
|
|
|
if (root_src_path) |root_src| {
|
|
|
|
const dirname = std.os.path.dirname(root_src) orelse ".";
|
|
|
|
const basename = std.os.path.basename(root_src);
|
|
|
|
|
|
|
|
comp.root_package = try Package.create(comp.arena(), dirname, basename);
|
|
|
|
comp.std_package = try Package.create(comp.arena(), comp.zig_std_dir, "index.zig");
|
|
|
|
try comp.root_package.add("std", comp.std_package);
|
|
|
|
} else {
|
|
|
|
comp.root_package = try Package.create(comp.arena(), ".", "");
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:22:17 -07:00
|
|
|
comp.fs_watch = try fs.Watch(*Scope.Root).create(loop, 16);
|
2018-08-10 09:28:20 -07:00
|
|
|
defer comp.fs_watch.destroy();
|
2018-08-03 14:22:17 -07:00
|
|
|
|
2018-07-14 13:12:41 -07:00
|
|
|
try comp.initTypes();
|
2018-08-10 09:28:20 -07:00
|
|
|
defer comp.primitive_type_table.deinit();
|
|
|
|
|
2018-08-10 10:19:07 -07:00
|
|
|
comp.main_loop_handle = async comp.mainLoop() catch unreachable;
|
2018-08-10 09:28:20 -07:00
|
|
|
// Set this to indicate that initialization completed successfully.
|
|
|
|
// from here on out we must not return an error.
|
|
|
|
// This must occur before the first suspend/await.
|
|
|
|
out_comp.* = ∁
|
2018-08-10 10:19:07 -07:00
|
|
|
// This suspend is resumed by destroy()
|
2018-08-10 09:28:20 -07:00
|
|
|
suspend;
|
|
|
|
// From here on is cleanup.
|
2018-08-10 10:19:07 -07:00
|
|
|
|
2018-08-10 09:28:20 -07:00
|
|
|
await (async comp.deinit_group.wait() catch unreachable);
|
2018-07-16 17:52:50 -07:00
|
|
|
|
2018-08-10 09:28:20 -07:00
|
|
|
if (comp.tmp_dir.getOrNull()) |tmp_dir_result| if (tmp_dir_result.*) |tmp_dir| {
|
|
|
|
// TODO evented I/O?
|
|
|
|
os.deleteTree(comp.arena(), tmp_dir) catch {};
|
|
|
|
} else |_| {};
|
2018-07-12 12:08:40 -07:00
|
|
|
}
|
|
|
|
|
2018-07-19 12:11:39 -07:00
|
|
|
/// it does ref the result because it could be an arbitrary integer size
|
2018-07-19 21:13:48 -07:00
|
|
|
pub async fn getPrimitiveType(comp: *Compilation, name: []const u8) !?*Type {
|
2018-07-19 12:11:39 -07:00
|
|
|
if (name.len >= 2) {
|
|
|
|
switch (name[0]) {
|
|
|
|
'i', 'u' => blk: {
|
|
|
|
for (name[1..]) |byte|
|
|
|
|
switch (byte) {
|
|
|
|
'0'...'9' => {},
|
|
|
|
else => break :blk,
|
|
|
|
};
|
|
|
|
const is_signed = name[0] == 'i';
|
|
|
|
const bit_count = std.fmt.parseUnsigned(u32, name[1..], 10) catch |err| switch (err) {
|
|
|
|
error.Overflow => return error.Overflow,
|
|
|
|
error.InvalidCharacter => unreachable, // we just checked the characters above
|
|
|
|
};
|
2018-07-19 21:13:48 -07:00
|
|
|
const int_type = try await (async Type.Int.get(comp, Type.Int.Key{
|
|
|
|
.bit_count = bit_count,
|
|
|
|
.is_signed = is_signed,
|
|
|
|
}) catch unreachable);
|
|
|
|
errdefer int_type.base.base.deref();
|
|
|
|
return &int_type.base;
|
2018-07-19 12:11:39 -07:00
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (comp.primitive_type_table.get(name)) |entry| {
|
|
|
|
entry.value.base.ref();
|
|
|
|
return entry.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-07-14 13:12:41 -07:00
|
|
|
fn initTypes(comp: *Compilation) !void {
|
2018-07-18 21:08:47 -07:00
|
|
|
comp.meta_type = try comp.arena().create(Type.MetaType{
|
2018-07-12 12:08:40 -07:00
|
|
|
.base = Type{
|
2018-07-18 21:08:47 -07:00
|
|
|
.name = "type",
|
2018-07-12 12:08:40 -07:00
|
|
|
.base = Value{
|
|
|
|
.id = Value.Id.Type,
|
2018-07-22 20:27:58 -07:00
|
|
|
.typ = undefined,
|
2018-07-14 12:45:15 -07:00
|
|
|
.ref_count = std.atomic.Int(usize).init(3), // 3 because it references itself twice
|
2018-07-12 12:08:40 -07:00
|
|
|
},
|
|
|
|
.id = builtin.TypeId.Type,
|
2018-07-22 20:27:58 -07:00
|
|
|
.abi_alignment = Type.AbiAlignment.init(comp.loop),
|
2018-07-12 12:08:40 -07:00
|
|
|
},
|
|
|
|
.value = undefined,
|
|
|
|
});
|
2018-07-14 13:12:41 -07:00
|
|
|
comp.meta_type.value = &comp.meta_type.base;
|
2018-07-22 20:27:58 -07:00
|
|
|
comp.meta_type.base.base.typ = &comp.meta_type.base;
|
2018-07-18 21:08:47 -07:00
|
|
|
assert((try comp.primitive_type_table.put(comp.meta_type.base.name, &comp.meta_type.base)) == null);
|
2018-07-12 12:08:40 -07:00
|
|
|
|
2018-07-18 21:08:47 -07:00
|
|
|
comp.void_type = try comp.arena().create(Type.Void{
|
2018-07-12 12:08:40 -07:00
|
|
|
.base = Type{
|
2018-07-18 21:08:47 -07:00
|
|
|
.name = "void",
|
2018-07-12 12:08:40 -07:00
|
|
|
.base = Value{
|
|
|
|
.id = Value.Id.Type,
|
2018-07-22 20:27:58 -07:00
|
|
|
.typ = &Type.MetaType.get(comp).base,
|
2018-07-14 12:45:15 -07:00
|
|
|
.ref_count = std.atomic.Int(usize).init(1),
|
2018-07-12 12:08:40 -07:00
|
|
|
},
|
|
|
|
.id = builtin.TypeId.Void,
|
2018-07-22 20:27:58 -07:00
|
|
|
.abi_alignment = Type.AbiAlignment.init(comp.loop),
|
2018-07-12 12:08:40 -07:00
|
|
|
},
|
|
|
|
});
|
2018-07-18 21:08:47 -07:00
|
|
|
assert((try comp.primitive_type_table.put(comp.void_type.base.name, &comp.void_type.base)) == null);
|
2018-07-12 12:08:40 -07:00
|
|
|
|
2018-07-18 21:08:47 -07:00
|
|
|
comp.noreturn_type = try comp.arena().create(Type.NoReturn{
|
2018-07-12 12:08:40 -07:00
|
|
|
.base = Type{
|
2018-07-18 21:08:47 -07:00
|
|
|
.name = "noreturn",
|
2018-07-12 12:08:40 -07:00
|
|
|
.base = Value{
|
|
|
|
.id = Value.Id.Type,
|
2018-07-22 20:27:58 -07:00
|
|
|
.typ = &Type.MetaType.get(comp).base,
|
2018-07-14 12:45:15 -07:00
|
|
|
.ref_count = std.atomic.Int(usize).init(1),
|
2018-07-12 12:08:40 -07:00
|
|
|
},
|
|
|
|
.id = builtin.TypeId.NoReturn,
|
2018-07-22 20:27:58 -07:00
|
|
|
.abi_alignment = Type.AbiAlignment.init(comp.loop),
|
2018-07-12 12:08:40 -07:00
|
|
|
},
|
|
|
|
});
|
2018-07-18 21:08:47 -07:00
|
|
|
assert((try comp.primitive_type_table.put(comp.noreturn_type.base.name, &comp.noreturn_type.base)) == null);
|
2018-07-12 12:08:40 -07:00
|
|
|
|
2018-07-18 21:08:47 -07:00
|
|
|
comp.comptime_int_type = try comp.arena().create(Type.ComptimeInt{
|
2018-07-12 12:08:40 -07:00
|
|
|
.base = Type{
|
2018-07-18 21:08:47 -07:00
|
|
|
.name = "comptime_int",
|
|
|
|
.base = Value{
|
|
|
|
.id = Value.Id.Type,
|
2018-07-22 20:27:58 -07:00
|
|
|
.typ = &Type.MetaType.get(comp).base,
|
2018-07-18 21:08:47 -07:00
|
|
|
.ref_count = std.atomic.Int(usize).init(1),
|
|
|
|
},
|
|
|
|
.id = builtin.TypeId.ComptimeInt,
|
2018-07-22 20:27:58 -07:00
|
|
|
.abi_alignment = Type.AbiAlignment.init(comp.loop),
|
2018-07-18 21:08:47 -07:00
|
|
|
},
|
|
|
|
});
|
|
|
|
assert((try comp.primitive_type_table.put(comp.comptime_int_type.base.name, &comp.comptime_int_type.base)) == null);
|
|
|
|
|
|
|
|
comp.bool_type = try comp.arena().create(Type.Bool{
|
|
|
|
.base = Type{
|
|
|
|
.name = "bool",
|
2018-07-12 12:08:40 -07:00
|
|
|
.base = Value{
|
|
|
|
.id = Value.Id.Type,
|
2018-07-22 20:27:58 -07:00
|
|
|
.typ = &Type.MetaType.get(comp).base,
|
2018-07-14 12:45:15 -07:00
|
|
|
.ref_count = std.atomic.Int(usize).init(1),
|
2018-07-12 12:08:40 -07:00
|
|
|
},
|
|
|
|
.id = builtin.TypeId.Bool,
|
2018-07-22 20:27:58 -07:00
|
|
|
.abi_alignment = Type.AbiAlignment.init(comp.loop),
|
2018-07-12 12:08:40 -07:00
|
|
|
},
|
|
|
|
});
|
2018-07-18 21:08:47 -07:00
|
|
|
assert((try comp.primitive_type_table.put(comp.bool_type.base.name, &comp.bool_type.base)) == null);
|
2018-07-12 12:08:40 -07:00
|
|
|
|
2018-07-18 21:08:47 -07:00
|
|
|
comp.void_value = try comp.arena().create(Value.Void{
|
2018-07-12 12:08:40 -07:00
|
|
|
.base = Value{
|
|
|
|
.id = Value.Id.Void,
|
2018-07-22 20:27:58 -07:00
|
|
|
.typ = &Type.Void.get(comp).base,
|
2018-07-14 12:45:15 -07:00
|
|
|
.ref_count = std.atomic.Int(usize).init(1),
|
2018-07-12 12:08:40 -07:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-07-18 21:08:47 -07:00
|
|
|
comp.true_value = try comp.arena().create(Value.Bool{
|
2018-07-12 12:08:40 -07:00
|
|
|
.base = Value{
|
|
|
|
.id = Value.Id.Bool,
|
2018-07-22 20:27:58 -07:00
|
|
|
.typ = &Type.Bool.get(comp).base,
|
2018-07-14 12:45:15 -07:00
|
|
|
.ref_count = std.atomic.Int(usize).init(1),
|
2018-07-12 12:08:40 -07:00
|
|
|
},
|
|
|
|
.x = true,
|
|
|
|
});
|
|
|
|
|
2018-07-18 21:08:47 -07:00
|
|
|
comp.false_value = try comp.arena().create(Value.Bool{
|
2018-07-12 12:08:40 -07:00
|
|
|
.base = Value{
|
|
|
|
.id = Value.Id.Bool,
|
2018-07-22 20:27:58 -07:00
|
|
|
.typ = &Type.Bool.get(comp).base,
|
2018-07-14 12:45:15 -07:00
|
|
|
.ref_count = std.atomic.Int(usize).init(1),
|
2018-07-12 12:08:40 -07:00
|
|
|
},
|
|
|
|
.x = false,
|
2018-06-20 08:40:21 -07:00
|
|
|
});
|
2018-07-12 12:08:40 -07:00
|
|
|
|
2018-07-18 21:08:47 -07:00
|
|
|
comp.noreturn_value = try comp.arena().create(Value.NoReturn{
|
2018-07-12 12:08:40 -07:00
|
|
|
.base = Value{
|
|
|
|
.id = Value.Id.NoReturn,
|
2018-07-22 20:27:58 -07:00
|
|
|
.typ = &Type.NoReturn.get(comp).base,
|
2018-07-14 12:45:15 -07:00
|
|
|
.ref_count = std.atomic.Int(usize).init(1),
|
2018-07-12 12:08:40 -07:00
|
|
|
},
|
|
|
|
});
|
2017-12-22 21:29:39 -08:00
|
|
|
|
2018-07-18 21:08:47 -07:00
|
|
|
for (CInt.list) |cint, i| {
|
|
|
|
const c_int_type = try comp.arena().create(Type.Int{
|
|
|
|
.base = Type{
|
|
|
|
.name = cint.zig_name,
|
|
|
|
.base = Value{
|
|
|
|
.id = Value.Id.Type,
|
2018-07-22 20:27:58 -07:00
|
|
|
.typ = &Type.MetaType.get(comp).base,
|
2018-07-18 21:08:47 -07:00
|
|
|
.ref_count = std.atomic.Int(usize).init(1),
|
|
|
|
},
|
|
|
|
.id = builtin.TypeId.Int,
|
2018-07-22 20:27:58 -07:00
|
|
|
.abi_alignment = Type.AbiAlignment.init(comp.loop),
|
2018-07-18 21:08:47 -07:00
|
|
|
},
|
|
|
|
.key = Type.Int.Key{
|
|
|
|
.is_signed = cint.is_signed,
|
|
|
|
.bit_count = comp.target.cIntTypeSizeInBits(cint.id),
|
|
|
|
},
|
|
|
|
.garbage_node = undefined,
|
|
|
|
});
|
|
|
|
comp.c_int_types[i] = c_int_type;
|
|
|
|
assert((try comp.primitive_type_table.put(cint.zig_name, &c_int_type.base)) == null);
|
|
|
|
}
|
2018-07-22 20:27:58 -07:00
|
|
|
comp.u8_type = try comp.arena().create(Type.Int{
|
|
|
|
.base = Type{
|
|
|
|
.name = "u8",
|
|
|
|
.base = Value{
|
|
|
|
.id = Value.Id.Type,
|
|
|
|
.typ = &Type.MetaType.get(comp).base,
|
|
|
|
.ref_count = std.atomic.Int(usize).init(1),
|
|
|
|
},
|
|
|
|
.id = builtin.TypeId.Int,
|
|
|
|
.abi_alignment = Type.AbiAlignment.init(comp.loop),
|
|
|
|
},
|
|
|
|
.key = Type.Int.Key{
|
|
|
|
.is_signed = false,
|
|
|
|
.bit_count = 8,
|
|
|
|
},
|
|
|
|
.garbage_node = undefined,
|
|
|
|
});
|
|
|
|
assert((try comp.primitive_type_table.put(comp.u8_type.base.name, &comp.u8_type.base)) == null);
|
2018-07-17 21:34:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn destroy(self: *Compilation) void {
|
2018-08-10 09:28:20 -07:00
|
|
|
cancel self.main_loop_handle;
|
2018-07-17 21:34:42 -07:00
|
|
|
resume self.destroy_handle;
|
|
|
|
}
|
|
|
|
|
2018-08-10 09:28:20 -07:00
|
|
|
fn start(self: *Compilation) void {
|
|
|
|
self.main_loop_future.resolve();
|
2018-06-29 12:39:55 -07:00
|
|
|
}
|
|
|
|
|
2018-08-10 09:28:20 -07:00
|
|
|
async fn mainLoop(self: *Compilation) void {
|
|
|
|
// wait until start() is called
|
|
|
|
_ = await (async self.main_loop_future.get() catch unreachable);
|
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
var build_result = await (async self.initialCompile() catch unreachable);
|
2018-07-13 18:56:38 -07:00
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
while (true) {
|
2018-08-03 14:22:17 -07:00
|
|
|
const link_result = if (build_result) blk: {
|
|
|
|
break :blk await (async self.maybeLink() catch unreachable);
|
|
|
|
} else |err| err;
|
2018-07-13 18:56:38 -07:00
|
|
|
// this makes a handy error return trace and stack trace in debug mode
|
|
|
|
if (std.debug.runtime_safety) {
|
2018-08-02 14:04:17 -07:00
|
|
|
link_result catch unreachable;
|
2018-07-13 18:56:38 -07:00
|
|
|
}
|
|
|
|
|
2018-07-10 17:18:43 -07:00
|
|
|
const compile_errors = blk: {
|
|
|
|
const held = await (async self.compile_errors.acquire() catch unreachable);
|
|
|
|
defer held.release();
|
|
|
|
break :blk held.value.toOwnedSlice();
|
|
|
|
};
|
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
if (link_result) |_| {
|
2018-07-13 18:56:38 -07:00
|
|
|
if (compile_errors.len == 0) {
|
|
|
|
await (async self.events.put(Event.Ok) catch unreachable);
|
|
|
|
} else {
|
|
|
|
await (async self.events.put(Event{ .Fail = compile_errors }) catch unreachable);
|
|
|
|
}
|
|
|
|
} else |err| {
|
|
|
|
// if there's an error then the compile errors have dangling references
|
2018-07-16 17:52:50 -07:00
|
|
|
self.gpa().free(compile_errors);
|
2018-07-13 18:56:38 -07:00
|
|
|
|
|
|
|
await (async self.events.put(Event{ .Error = err }) catch unreachable);
|
2018-07-10 17:18:43 -07:00
|
|
|
}
|
2018-07-13 18:56:38 -07:00
|
|
|
|
2018-08-03 14:22:17 -07:00
|
|
|
// First, get an item from the watch channel, waiting on the channel.
|
2018-08-02 14:04:17 -07:00
|
|
|
var group = event.Group(BuildError!void).init(self.loop);
|
2018-08-03 14:22:17 -07:00
|
|
|
{
|
2018-08-07 19:12:47 -07:00
|
|
|
const ev = (await (async self.fs_watch.channel.get() catch unreachable)) catch |err| {
|
|
|
|
build_result = err;
|
|
|
|
continue;
|
2018-08-03 14:22:17 -07:00
|
|
|
};
|
2018-08-07 19:12:47 -07:00
|
|
|
const root_scope = ev.data;
|
2018-08-03 14:22:17 -07:00
|
|
|
group.call(rebuildFile, self, root_scope) catch |err| {
|
|
|
|
build_result = err;
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// Next, get all the items from the channel that are buffered up.
|
2018-08-07 19:12:47 -07:00
|
|
|
while (await (async self.fs_watch.channel.getOrNull() catch unreachable)) |ev_or_err| {
|
|
|
|
if (ev_or_err) |ev| {
|
|
|
|
const root_scope = ev.data;
|
|
|
|
group.call(rebuildFile, self, root_scope) catch |err| {
|
2018-08-03 14:22:17 -07:00
|
|
|
build_result = err;
|
|
|
|
continue;
|
2018-08-07 19:12:47 -07:00
|
|
|
};
|
|
|
|
} else |err| {
|
2018-08-03 14:22:17 -07:00
|
|
|
build_result = err;
|
|
|
|
continue;
|
2018-08-07 19:12:47 -07:00
|
|
|
}
|
2018-08-02 14:04:17 -07:00
|
|
|
}
|
|
|
|
build_result = await (async group.wait() catch unreachable);
|
2018-06-29 12:39:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
async fn rebuildFile(self: *Compilation, root_scope: *Scope.Root) !void {
|
|
|
|
const tree_scope = blk: {
|
|
|
|
const source_code = (await (async fs.readFile(
|
|
|
|
self.loop,
|
2018-08-03 14:22:17 -07:00
|
|
|
root_scope.realpath,
|
2018-08-02 14:04:17 -07:00
|
|
|
max_src_size,
|
|
|
|
) catch unreachable)) catch |err| {
|
2018-08-03 14:22:17 -07:00
|
|
|
try self.addCompileErrorCli(root_scope.realpath, "unable to open: {}", @errorName(err));
|
|
|
|
return;
|
2018-07-19 12:11:39 -07:00
|
|
|
};
|
2018-08-02 14:04:17 -07:00
|
|
|
errdefer self.gpa().free(source_code);
|
2017-12-22 21:29:39 -08:00
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
const tree = try self.gpa().createOne(ast.Tree);
|
|
|
|
tree.* = try std.zig.parse(self.gpa(), source_code);
|
|
|
|
errdefer {
|
|
|
|
tree.deinit();
|
|
|
|
self.gpa().destroy(tree);
|
|
|
|
}
|
2018-07-10 17:18:43 -07:00
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
break :blk try Scope.AstTree.create(self, tree, root_scope);
|
|
|
|
};
|
|
|
|
defer tree_scope.base.deref(self);
|
2018-07-05 12:09:02 -07:00
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
var error_it = tree_scope.tree.errors.iterator(0);
|
|
|
|
while (error_it.next()) |parse_error| {
|
|
|
|
const msg = try Msg.createFromParseErrorAndScope(self, tree_scope, parse_error);
|
|
|
|
errdefer msg.destroy();
|
2018-07-23 11:28:14 -07:00
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
try await (async self.addCompileErrorAsync(msg) catch unreachable);
|
|
|
|
}
|
|
|
|
if (tree_scope.tree.errors.len != 0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-05 12:09:02 -07:00
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
const locked_table = await (async root_scope.decls.table.acquireWrite() catch unreachable);
|
|
|
|
defer locked_table.release();
|
2018-07-05 12:09:02 -07:00
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
var decl_group = event.Group(BuildError!void).init(self.loop);
|
|
|
|
defer decl_group.deinit();
|
2018-07-10 12:17:01 -07:00
|
|
|
|
2018-08-03 14:22:17 -07:00
|
|
|
try await try async self.rebuildChangedDecls(
|
2018-08-02 14:04:17 -07:00
|
|
|
&decl_group,
|
2018-08-03 14:22:17 -07:00
|
|
|
locked_table.value,
|
2018-08-02 14:04:17 -07:00
|
|
|
root_scope.decls,
|
|
|
|
&tree_scope.tree.root_node.decls,
|
|
|
|
tree_scope,
|
|
|
|
);
|
2018-07-18 14:40:59 -07:00
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
try await (async decl_group.wait() catch unreachable);
|
|
|
|
}
|
2018-07-05 12:09:02 -07:00
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
async fn rebuildChangedDecls(
|
|
|
|
self: *Compilation,
|
|
|
|
group: *event.Group(BuildError!void),
|
|
|
|
locked_table: *Decl.Table,
|
|
|
|
decl_scope: *Scope.Decls,
|
2018-08-03 14:22:17 -07:00
|
|
|
ast_decls: *ast.Node.Root.DeclList,
|
2018-08-02 14:04:17 -07:00
|
|
|
tree_scope: *Scope.AstTree,
|
|
|
|
) !void {
|
|
|
|
var existing_decls = try locked_table.clone();
|
|
|
|
defer existing_decls.deinit();
|
2018-07-19 12:11:39 -07:00
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
var ast_it = ast_decls.iterator(0);
|
|
|
|
while (ast_it.next()) |decl_ptr| {
|
|
|
|
const decl = decl_ptr.*;
|
|
|
|
switch (decl.id) {
|
|
|
|
ast.Node.Id.Comptime => {
|
|
|
|
const comptime_node = @fieldParentPtr(ast.Node.Comptime, "base", decl);
|
|
|
|
|
|
|
|
// TODO connect existing comptime decls to updated source files
|
|
|
|
|
2018-08-03 14:22:17 -07:00
|
|
|
try self.prelink_group.call(addCompTimeBlock, self, tree_scope, &decl_scope.base, comptime_node);
|
2018-08-02 14:04:17 -07:00
|
|
|
},
|
|
|
|
ast.Node.Id.VarDecl => @panic("TODO"),
|
|
|
|
ast.Node.Id.FnProto => {
|
|
|
|
const fn_proto = @fieldParentPtr(ast.Node.FnProto, "base", decl);
|
|
|
|
|
|
|
|
const name = if (fn_proto.name_token) |name_token| tree_scope.tree.tokenSlice(name_token) else {
|
2018-08-03 14:22:17 -07:00
|
|
|
try self.addCompileError(tree_scope, Span{
|
2018-08-02 14:04:17 -07:00
|
|
|
.first = fn_proto.fn_token,
|
|
|
|
.last = fn_proto.fn_token + 1,
|
|
|
|
}, "missing function name");
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (existing_decls.remove(name)) |entry| {
|
|
|
|
// compare new code to existing
|
2018-08-03 14:59:11 -07:00
|
|
|
if (entry.value.cast(Decl.Fn)) |existing_fn_decl| {
|
|
|
|
// Just compare the old bytes to the new bytes of the top level decl.
|
|
|
|
// Even if the AST is technically the same, we want error messages to display
|
|
|
|
// from the most recent source.
|
|
|
|
const old_decl_src = existing_fn_decl.base.tree_scope.tree.getNodeSource(
|
|
|
|
&existing_fn_decl.fn_proto.base,
|
|
|
|
);
|
|
|
|
const new_decl_src = tree_scope.tree.getNodeSource(&fn_proto.base);
|
|
|
|
if (mem.eql(u8, old_decl_src, new_decl_src)) {
|
|
|
|
// it's the same, we can skip this decl
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
@panic("TODO decl changed implementation");
|
|
|
|
// Add the new thing before dereferencing the old thing. This way we don't end
|
|
|
|
// up pointlessly re-creating things we end up using in the new thing.
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
@panic("TODO decl changed kind");
|
|
|
|
}
|
2018-08-02 14:04:17 -07:00
|
|
|
} else {
|
|
|
|
// add new decl
|
2018-07-19 12:11:39 -07:00
|
|
|
const fn_decl = try self.gpa().create(Decl.Fn{
|
|
|
|
.base = Decl{
|
|
|
|
.id = Decl.Id.Fn,
|
|
|
|
.name = name,
|
2018-08-02 14:04:17 -07:00
|
|
|
.visib = parseVisibToken(tree_scope.tree, fn_proto.visib_token),
|
2018-07-19 12:11:39 -07:00
|
|
|
.resolution = event.Future(BuildError!void).init(self.loop),
|
2018-08-02 14:04:17 -07:00
|
|
|
.parent_scope = &decl_scope.base,
|
2018-08-03 14:22:17 -07:00
|
|
|
.tree_scope = tree_scope,
|
2018-07-19 12:11:39 -07:00
|
|
|
},
|
|
|
|
.value = Decl.Fn.Val{ .Unresolved = {} },
|
|
|
|
.fn_proto = fn_proto,
|
|
|
|
});
|
2018-08-03 14:22:17 -07:00
|
|
|
tree_scope.base.ref();
|
2018-07-19 12:11:39 -07:00
|
|
|
errdefer self.gpa().destroy(fn_decl);
|
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
try group.call(addTopLevelDecl, self, &fn_decl.base, locked_table);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ast.Node.Id.TestDecl => @panic("TODO"),
|
|
|
|
else => unreachable,
|
2018-07-05 12:09:02 -07:00
|
|
|
}
|
2018-08-02 14:04:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var existing_decl_it = existing_decls.iterator();
|
|
|
|
while (existing_decl_it.next()) |entry| {
|
|
|
|
// this decl was deleted
|
|
|
|
const existing_decl = entry.value;
|
|
|
|
@panic("TODO handle decl deletion");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn initialCompile(self: *Compilation) !void {
|
|
|
|
if (self.root_src_path) |root_src_path| {
|
|
|
|
const root_scope = blk: {
|
|
|
|
// TODO async/await os.path.real
|
|
|
|
const root_src_real_path = os.path.real(self.gpa(), root_src_path) catch |err| {
|
2018-08-03 14:22:17 -07:00
|
|
|
try self.addCompileErrorCli(root_src_path, "unable to open: {}", @errorName(err));
|
|
|
|
return;
|
2018-08-02 14:04:17 -07:00
|
|
|
};
|
|
|
|
errdefer self.gpa().free(root_src_real_path);
|
|
|
|
|
|
|
|
break :blk try Scope.Root.create(self, root_src_real_path);
|
|
|
|
};
|
|
|
|
defer root_scope.base.deref(self);
|
2018-07-22 20:27:58 -07:00
|
|
|
|
2018-08-03 14:22:17 -07:00
|
|
|
assert((try await try async self.fs_watch.addFile(root_scope.realpath, root_scope)) == null);
|
|
|
|
try await try async self.rebuildFile(root_scope);
|
2018-07-05 12:09:02 -07:00
|
|
|
}
|
2018-08-02 14:04:17 -07:00
|
|
|
}
|
2018-07-19 12:11:39 -07:00
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
async fn maybeLink(self: *Compilation) !void {
|
2018-07-22 20:27:58 -07:00
|
|
|
(await (async self.prelink_group.wait() catch unreachable)) catch |err| switch (err) {
|
|
|
|
error.SemanticAnalysisFailed => {},
|
|
|
|
else => return err,
|
|
|
|
};
|
2018-07-17 10:18:13 -07:00
|
|
|
|
|
|
|
const any_prelink_errors = blk: {
|
|
|
|
const compile_errors = await (async self.compile_errors.acquire() catch unreachable);
|
|
|
|
defer compile_errors.release();
|
|
|
|
|
|
|
|
break :blk compile_errors.value.len != 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!any_prelink_errors) {
|
2018-07-17 21:34:42 -07:00
|
|
|
try await (async link(self) catch unreachable);
|
2018-07-17 10:18:13 -07:00
|
|
|
}
|
2018-07-05 12:09:02 -07:00
|
|
|
}
|
|
|
|
|
2018-07-18 14:40:59 -07:00
|
|
|
/// caller takes ownership of resulting Code
|
|
|
|
async fn genAndAnalyzeCode(
|
|
|
|
comp: *Compilation,
|
2018-08-03 14:22:17 -07:00
|
|
|
tree_scope: *Scope.AstTree,
|
2018-07-18 14:40:59 -07:00
|
|
|
scope: *Scope,
|
|
|
|
node: *ast.Node,
|
|
|
|
expected_type: ?*Type,
|
2018-07-19 12:11:39 -07:00
|
|
|
) !*ir.Code {
|
|
|
|
const unanalyzed_code = try await (async ir.gen(
|
2018-07-18 14:40:59 -07:00
|
|
|
comp,
|
|
|
|
node,
|
2018-08-03 14:22:17 -07:00
|
|
|
tree_scope,
|
2018-07-18 14:40:59 -07:00
|
|
|
scope,
|
2018-07-19 12:11:39 -07:00
|
|
|
) catch unreachable);
|
2018-07-18 14:40:59 -07:00
|
|
|
defer unanalyzed_code.destroy(comp.gpa());
|
|
|
|
|
|
|
|
if (comp.verbose_ir) {
|
|
|
|
std.debug.warn("unanalyzed:\n");
|
|
|
|
unanalyzed_code.dump();
|
|
|
|
}
|
|
|
|
|
2018-07-19 12:11:39 -07:00
|
|
|
const analyzed_code = try await (async ir.analyze(
|
2018-07-18 14:40:59 -07:00
|
|
|
comp,
|
|
|
|
unanalyzed_code,
|
|
|
|
expected_type,
|
2018-07-19 12:11:39 -07:00
|
|
|
) catch unreachable);
|
2018-07-18 14:40:59 -07:00
|
|
|
errdefer analyzed_code.destroy(comp.gpa());
|
|
|
|
|
2018-07-19 12:11:39 -07:00
|
|
|
if (comp.verbose_ir) {
|
|
|
|
std.debug.warn("analyzed:\n");
|
|
|
|
analyzed_code.dump();
|
|
|
|
}
|
|
|
|
|
2018-07-18 14:40:59 -07:00
|
|
|
return analyzed_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn addCompTimeBlock(
|
|
|
|
comp: *Compilation,
|
2018-08-03 14:22:17 -07:00
|
|
|
tree_scope: *Scope.AstTree,
|
2018-07-18 14:40:59 -07:00
|
|
|
scope: *Scope,
|
|
|
|
comptime_node: *ast.Node.Comptime,
|
|
|
|
) !void {
|
|
|
|
const void_type = Type.Void.get(comp);
|
|
|
|
defer void_type.base.base.deref(comp);
|
|
|
|
|
2018-07-19 12:11:39 -07:00
|
|
|
const analyzed_code = (await (async genAndAnalyzeCode(
|
2018-07-18 14:40:59 -07:00
|
|
|
comp,
|
2018-08-03 14:22:17 -07:00
|
|
|
tree_scope,
|
2018-07-18 14:40:59 -07:00
|
|
|
scope,
|
|
|
|
comptime_node.expr,
|
|
|
|
&void_type.base,
|
2018-07-19 12:11:39 -07:00
|
|
|
) catch unreachable)) catch |err| switch (err) {
|
|
|
|
// This poison value should not cause the errdefers to run. It simply means
|
|
|
|
// that comp.compile_errors is populated.
|
|
|
|
error.SemanticAnalysisFailed => return {},
|
|
|
|
else => return err,
|
|
|
|
};
|
2018-07-18 14:40:59 -07:00
|
|
|
analyzed_code.destroy(comp.gpa());
|
|
|
|
}
|
|
|
|
|
2018-08-02 14:04:17 -07:00
|
|
|
async fn addTopLevelDecl(
|
|
|
|
self: *Compilation,
|
|
|
|
decl: *Decl,
|
|
|
|
locked_table: *Decl.Table,
|
|
|
|
) !void {
|
2018-08-03 14:22:17 -07:00
|
|
|
const is_export = decl.isExported(decl.tree_scope.tree);
|
2018-07-05 12:09:02 -07:00
|
|
|
|
2018-07-10 12:17:01 -07:00
|
|
|
if (is_export) {
|
2018-07-16 17:52:50 -07:00
|
|
|
try self.prelink_group.call(verifyUniqueSymbol, self, decl);
|
|
|
|
try self.prelink_group.call(resolveDecl, self, decl);
|
2018-07-10 12:17:01 -07:00
|
|
|
}
|
2018-07-22 20:27:58 -07:00
|
|
|
|
2018-08-03 14:22:17 -07:00
|
|
|
const gop = try locked_table.getOrPut(decl.name);
|
|
|
|
if (gop.found_existing) {
|
|
|
|
try self.addCompileError(decl.tree_scope, decl.getSpan(), "redefinition of '{}'", decl.name);
|
2018-07-22 20:27:58 -07:00
|
|
|
// TODO note: other definition here
|
2018-08-03 14:22:17 -07:00
|
|
|
} else {
|
|
|
|
gop.kv.value = decl;
|
2018-07-22 20:27:58 -07:00
|
|
|
}
|
2018-07-10 12:17:01 -07:00
|
|
|
}
|
2018-07-05 12:09:02 -07:00
|
|
|
|
2018-08-03 14:22:17 -07:00
|
|
|
fn addCompileError(self: *Compilation, tree_scope: *Scope.AstTree, span: Span, comptime fmt: []const u8, args: ...) !void {
|
|
|
|
const text = try std.fmt.allocPrint(self.gpa(), fmt, args);
|
|
|
|
errdefer self.gpa().free(text);
|
|
|
|
|
|
|
|
const msg = try Msg.createFromScope(self, tree_scope, span, text);
|
|
|
|
errdefer msg.destroy();
|
|
|
|
|
|
|
|
try self.prelink_group.call(addCompileErrorAsync, self, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn addCompileErrorCli(self: *Compilation, realpath: []const u8, comptime fmt: []const u8, args: ...) !void {
|
2018-07-19 12:11:39 -07:00
|
|
|
const text = try std.fmt.allocPrint(self.gpa(), fmt, args);
|
|
|
|
errdefer self.gpa().free(text);
|
2018-07-10 17:18:43 -07:00
|
|
|
|
2018-08-03 14:22:17 -07:00
|
|
|
const msg = try Msg.createFromCli(self, realpath, text);
|
2018-07-23 11:28:14 -07:00
|
|
|
errdefer msg.destroy();
|
|
|
|
|
|
|
|
try self.prelink_group.call(addCompileErrorAsync, self, msg);
|
2018-07-10 17:18:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn addCompileErrorAsync(
|
2018-07-14 13:12:41 -07:00
|
|
|
self: *Compilation,
|
2018-07-23 11:28:14 -07:00
|
|
|
msg: *Msg,
|
2018-07-10 17:18:43 -07:00
|
|
|
) !void {
|
2018-07-23 11:28:14 -07:00
|
|
|
errdefer msg.destroy();
|
2018-07-10 17:18:43 -07:00
|
|
|
|
|
|
|
const compile_errors = await (async self.compile_errors.acquire() catch unreachable);
|
|
|
|
defer compile_errors.release();
|
|
|
|
|
|
|
|
try compile_errors.value.append(msg);
|
|
|
|
}
|
|
|
|
|
2018-07-14 13:12:41 -07:00
|
|
|
async fn verifyUniqueSymbol(self: *Compilation, decl: *Decl) !void {
|
2018-07-10 12:17:01 -07:00
|
|
|
const exported_symbol_names = await (async self.exported_symbol_names.acquire() catch unreachable);
|
|
|
|
defer exported_symbol_names.release();
|
|
|
|
|
|
|
|
if (try exported_symbol_names.value.put(decl.name, decl)) |other_decl| {
|
2018-07-10 17:18:43 -07:00
|
|
|
try self.addCompileError(
|
2018-08-03 14:22:17 -07:00
|
|
|
decl.tree_scope,
|
2018-07-10 17:18:43 -07:00
|
|
|
decl.getSpan(),
|
|
|
|
"exported symbol collision: '{}'",
|
|
|
|
decl.name,
|
|
|
|
);
|
2018-07-12 12:08:40 -07:00
|
|
|
// TODO add error note showing location of other symbol
|
2018-07-05 12:09:02 -07:00
|
|
|
}
|
2017-12-22 21:29:39 -08:00
|
|
|
}
|
|
|
|
|
2018-07-14 21:04:12 -07:00
|
|
|
pub fn haveLibC(self: *Compilation) bool {
|
|
|
|
return self.libc_link_lib != null;
|
|
|
|
}
|
|
|
|
|
2018-07-14 13:12:41 -07:00
|
|
|
pub fn addLinkLib(self: *Compilation, name: []const u8, provided_explicitly: bool) !*LinkLib {
|
2017-12-22 21:29:39 -08:00
|
|
|
const is_libc = mem.eql(u8, name, "c");
|
|
|
|
|
|
|
|
if (is_libc) {
|
|
|
|
if (self.libc_link_lib) |libc_link_lib| {
|
|
|
|
return libc_link_lib;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (self.link_libs_list.toSliceConst()) |existing_lib| {
|
|
|
|
if (mem.eql(u8, name, existing_lib.name)) {
|
|
|
|
return existing_lib;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-16 17:52:50 -07:00
|
|
|
const link_lib = try self.gpa().create(LinkLib{
|
2017-12-22 21:29:39 -08:00
|
|
|
.name = name,
|
|
|
|
.path = null,
|
|
|
|
.provided_explicitly = provided_explicitly,
|
2018-07-16 17:52:50 -07:00
|
|
|
.symbols = ArrayList([]u8).init(self.gpa()),
|
2018-06-20 08:40:21 -07:00
|
|
|
});
|
2018-01-07 13:51:46 -08:00
|
|
|
try self.link_libs_list.append(link_lib);
|
2017-12-22 21:29:39 -08:00
|
|
|
if (is_libc) {
|
|
|
|
self.libc_link_lib = link_lib;
|
2018-07-17 15:36:47 -07:00
|
|
|
|
|
|
|
// get a head start on looking for the native libc
|
2018-07-17 21:34:42 -07:00
|
|
|
if (self.target == Target.Native and self.override_libc == null) {
|
|
|
|
try self.deinit_group.call(startFindingNativeLibC, self);
|
2018-07-17 15:36:47 -07:00
|
|
|
}
|
2017-12-22 21:29:39 -08:00
|
|
|
}
|
|
|
|
return link_lib;
|
|
|
|
}
|
2018-06-29 12:39:55 -07:00
|
|
|
|
2018-07-17 15:36:47 -07:00
|
|
|
/// cancels itself so no need to await or cancel the promise.
|
|
|
|
async fn startFindingNativeLibC(self: *Compilation) void {
|
2018-07-17 21:34:42 -07:00
|
|
|
await (async self.loop.yield() catch unreachable);
|
2018-07-17 15:36:47 -07:00
|
|
|
// we don't care if it fails, we're just trying to kick off the future resolution
|
2018-08-10 09:28:20 -07:00
|
|
|
_ = (await (async self.zig_compiler.getNativeLibC() catch unreachable)) catch return;
|
2018-07-17 15:36:47 -07:00
|
|
|
}
|
|
|
|
|
2018-07-16 17:52:50 -07:00
|
|
|
/// General Purpose Allocator. Must free when done.
|
|
|
|
fn gpa(self: Compilation) *mem.Allocator {
|
2018-06-29 12:39:55 -07:00
|
|
|
return self.loop.allocator;
|
|
|
|
}
|
2018-07-16 17:52:50 -07:00
|
|
|
|
|
|
|
/// Arena Allocator. Automatically freed when the Compilation is destroyed.
|
|
|
|
fn arena(self: *Compilation) *mem.Allocator {
|
|
|
|
return &self.arena_allocator.allocator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If the temporary directory for this compilation has not been created, it creates it.
|
|
|
|
/// Then it creates a random file name in that dir and returns it.
|
|
|
|
pub async fn createRandomOutputPath(self: *Compilation, suffix: []const u8) !Buffer {
|
|
|
|
const tmp_dir = try await (async self.getTmpDir() catch unreachable);
|
|
|
|
const file_prefix = await (async self.getRandomFileName() catch unreachable);
|
|
|
|
|
|
|
|
const file_name = try std.fmt.allocPrint(self.gpa(), "{}{}", file_prefix[0..], suffix);
|
|
|
|
defer self.gpa().free(file_name);
|
|
|
|
|
|
|
|
const full_path = try os.path.join(self.gpa(), tmp_dir, file_name[0..]);
|
|
|
|
errdefer self.gpa().free(full_path);
|
|
|
|
|
|
|
|
return Buffer.fromOwnedSlice(self.gpa(), full_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If the temporary directory for this Compilation has not been created, creates it.
|
|
|
|
/// Then returns it. The directory is unique to this Compilation and cleaned up when
|
|
|
|
/// the Compilation deinitializes.
|
|
|
|
async fn getTmpDir(self: *Compilation) ![]const u8 {
|
|
|
|
if (await (async self.tmp_dir.start() catch unreachable)) |ptr| return ptr.*;
|
|
|
|
self.tmp_dir.data = await (async self.getTmpDirImpl() catch unreachable);
|
|
|
|
self.tmp_dir.resolve();
|
|
|
|
return self.tmp_dir.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn getTmpDirImpl(self: *Compilation) ![]u8 {
|
|
|
|
const comp_dir_name = await (async self.getRandomFileName() catch unreachable);
|
|
|
|
const zig_dir_path = try getZigDir(self.gpa());
|
|
|
|
defer self.gpa().free(zig_dir_path);
|
|
|
|
|
|
|
|
const tmp_dir = try os.path.join(self.arena(), zig_dir_path, comp_dir_name[0..]);
|
|
|
|
try os.makePath(self.gpa(), tmp_dir);
|
|
|
|
return tmp_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn getRandomFileName(self: *Compilation) [12]u8 {
|
|
|
|
// here we replace the standard +/ with -_ so that it can be used in a file name
|
|
|
|
const b64_fs_encoder = std.base64.Base64Encoder.init(
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",
|
|
|
|
std.base64.standard_pad_char,
|
|
|
|
);
|
|
|
|
|
|
|
|
var rand_bytes: [9]u8 = undefined;
|
|
|
|
|
|
|
|
{
|
2018-08-10 09:28:20 -07:00
|
|
|
const held = await (async self.zig_compiler.prng.acquire() catch unreachable);
|
2018-07-16 17:52:50 -07:00
|
|
|
defer held.release();
|
|
|
|
|
|
|
|
held.value.random.bytes(rand_bytes[0..]);
|
|
|
|
}
|
|
|
|
|
|
|
|
var result: [12]u8 = undefined;
|
|
|
|
b64_fs_encoder.encode(result[0..], rand_bytes);
|
|
|
|
return result;
|
|
|
|
}
|
2018-07-18 21:08:47 -07:00
|
|
|
|
|
|
|
fn registerGarbage(comp: *Compilation, comptime T: type, node: *std.atomic.Stack(*T).Node) void {
|
|
|
|
// TODO put the garbage somewhere
|
|
|
|
}
|
2018-07-19 12:11:39 -07:00
|
|
|
|
|
|
|
/// Returns a value which has been ref()'d once
|
2018-08-03 14:22:17 -07:00
|
|
|
async fn analyzeConstValue(
|
|
|
|
comp: *Compilation,
|
|
|
|
tree_scope: *Scope.AstTree,
|
|
|
|
scope: *Scope,
|
|
|
|
node: *ast.Node,
|
|
|
|
expected_type: *Type,
|
|
|
|
) !*Value {
|
|
|
|
const analyzed_code = try await (async comp.genAndAnalyzeCode(tree_scope, scope, node, expected_type) catch unreachable);
|
2018-07-19 12:11:39 -07:00
|
|
|
defer analyzed_code.destroy(comp.gpa());
|
|
|
|
|
|
|
|
return analyzed_code.getCompTimeResult(comp);
|
|
|
|
}
|
|
|
|
|
2018-08-03 14:22:17 -07:00
|
|
|
async fn analyzeTypeExpr(comp: *Compilation, tree_scope: *Scope.AstTree, scope: *Scope, node: *ast.Node) !*Type {
|
2018-07-19 12:11:39 -07:00
|
|
|
const meta_type = &Type.MetaType.get(comp).base;
|
|
|
|
defer meta_type.base.deref(comp);
|
|
|
|
|
2018-08-03 14:22:17 -07:00
|
|
|
const result_val = try await (async comp.analyzeConstValue(tree_scope, scope, node, meta_type) catch unreachable);
|
2018-07-19 12:11:39 -07:00
|
|
|
errdefer result_val.base.deref(comp);
|
|
|
|
|
|
|
|
return result_val.cast(Type).?;
|
|
|
|
}
|
2018-07-22 20:27:58 -07:00
|
|
|
|
|
|
|
/// This declaration has been blessed as going into the final code generation.
|
|
|
|
pub async fn resolveDecl(comp: *Compilation, decl: *Decl) !void {
|
|
|
|
if (await (async decl.resolution.start() catch unreachable)) |ptr| return ptr.*;
|
|
|
|
|
|
|
|
decl.resolution.data = try await (async generateDecl(comp, decl) catch unreachable);
|
|
|
|
decl.resolution.resolve();
|
|
|
|
return decl.resolution.data;
|
|
|
|
}
|
2017-12-22 21:29:39 -08:00
|
|
|
};
|
|
|
|
|
2018-07-05 12:09:02 -07:00
|
|
|
fn parseVisibToken(tree: *ast.Tree, optional_token_index: ?ast.TokenIndex) Visib {
|
|
|
|
if (optional_token_index) |token_index| {
|
|
|
|
const token = tree.tokens.at(token_index);
|
|
|
|
assert(token.id == Token.Id.Keyword_pub);
|
|
|
|
return Visib.Pub;
|
|
|
|
} else {
|
|
|
|
return Visib.Private;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-12 12:08:40 -07:00
|
|
|
/// The function that actually does the generation.
|
2018-07-14 13:12:41 -07:00
|
|
|
async fn generateDecl(comp: *Compilation, decl: *Decl) !void {
|
2018-07-12 12:08:40 -07:00
|
|
|
switch (decl.id) {
|
|
|
|
Decl.Id.Var => @panic("TODO"),
|
|
|
|
Decl.Id.Fn => {
|
|
|
|
const fn_decl = @fieldParentPtr(Decl.Fn, "base", decl);
|
2018-07-14 13:12:41 -07:00
|
|
|
return await (async generateDeclFn(comp, fn_decl) catch unreachable);
|
2018-07-12 12:08:40 -07:00
|
|
|
},
|
|
|
|
Decl.Id.CompTime => @panic("TODO"),
|
2018-07-10 17:18:43 -07:00
|
|
|
}
|
2018-07-12 12:08:40 -07:00
|
|
|
}
|
2018-07-10 17:18:43 -07:00
|
|
|
|
2018-07-14 13:12:41 -07:00
|
|
|
async fn generateDeclFn(comp: *Compilation, fn_decl: *Decl.Fn) !void {
|
2018-08-03 14:22:17 -07:00
|
|
|
const tree_scope = fn_decl.base.tree_scope;
|
|
|
|
|
2018-07-22 20:27:58 -07:00
|
|
|
const body_node = fn_decl.fn_proto.body_node orelse return await (async generateDeclFnProto(comp, fn_decl) catch unreachable);
|
2018-07-05 12:09:02 -07:00
|
|
|
|
2018-07-14 13:12:41 -07:00
|
|
|
const fndef_scope = try Scope.FnDef.create(comp, fn_decl.base.parent_scope);
|
|
|
|
defer fndef_scope.base.deref(comp);
|
2018-07-05 12:09:02 -07:00
|
|
|
|
2018-08-03 14:22:17 -07:00
|
|
|
const fn_type = try await (async analyzeFnType(comp, tree_scope, fn_decl.base.parent_scope, fn_decl.fn_proto) catch unreachable);
|
2018-07-14 13:12:41 -07:00
|
|
|
defer fn_type.base.base.deref(comp);
|
2018-07-05 12:09:02 -07:00
|
|
|
|
2018-07-16 17:52:50 -07:00
|
|
|
var symbol_name = try std.Buffer.init(comp.gpa(), fn_decl.base.name);
|
2018-07-22 20:27:58 -07:00
|
|
|
var symbol_name_consumed = false;
|
|
|
|
errdefer if (!symbol_name_consumed) symbol_name.deinit();
|
2018-07-14 12:45:15 -07:00
|
|
|
|
2018-07-17 10:18:13 -07:00
|
|
|
// The Decl.Fn owns the initial 1 reference count
|
2018-07-14 13:12:41 -07:00
|
|
|
const fn_val = try Value.Fn.create(comp, fn_type, fndef_scope, symbol_name);
|
2018-07-22 20:27:58 -07:00
|
|
|
fn_decl.value = Decl.Fn.Val{ .Fn = fn_val };
|
|
|
|
symbol_name_consumed = true;
|
2018-07-05 12:09:02 -07:00
|
|
|
|
2018-07-24 11:20:49 -07:00
|
|
|
// Define local parameter variables
|
2018-07-24 17:24:05 -07:00
|
|
|
for (fn_type.key.data.Normal.params) |param, i| {
|
|
|
|
//AstNode *param_decl_node = get_param_decl_node(fn_table_entry, i);
|
|
|
|
const param_decl = @fieldParentPtr(ast.Node.ParamDecl, "base", fn_decl.fn_proto.params.at(i).*);
|
|
|
|
const name_token = param_decl.name_token orelse {
|
2018-08-03 14:22:17 -07:00
|
|
|
try comp.addCompileError(tree_scope, Span{
|
2018-07-24 17:24:05 -07:00
|
|
|
.first = param_decl.firstToken(),
|
|
|
|
.last = param_decl.type_node.firstToken(),
|
|
|
|
}, "missing parameter name");
|
|
|
|
return error.SemanticAnalysisFailed;
|
|
|
|
};
|
2018-08-03 14:22:17 -07:00
|
|
|
const param_name = tree_scope.tree.tokenSlice(name_token);
|
2018-07-24 17:24:05 -07:00
|
|
|
|
|
|
|
// if (is_noalias && get_codegen_ptr_type(param_type) == nullptr) {
|
|
|
|
// add_node_error(g, param_decl_node, buf_sprintf("noalias on non-pointer parameter"));
|
|
|
|
// }
|
|
|
|
|
|
|
|
// TODO check for shadowing
|
|
|
|
|
|
|
|
const var_scope = try Scope.Var.createParam(
|
|
|
|
comp,
|
|
|
|
fn_val.child_scope,
|
|
|
|
param_name,
|
|
|
|
¶m_decl.base,
|
|
|
|
i,
|
|
|
|
param.typ,
|
|
|
|
);
|
|
|
|
fn_val.child_scope = &var_scope.base;
|
|
|
|
|
|
|
|
try fn_type.non_key.Normal.variable_list.append(var_scope);
|
|
|
|
}
|
2018-07-24 11:20:49 -07:00
|
|
|
|
2018-07-19 12:11:39 -07:00
|
|
|
const analyzed_code = try await (async comp.genAndAnalyzeCode(
|
2018-08-03 14:22:17 -07:00
|
|
|
tree_scope,
|
2018-07-24 17:24:05 -07:00
|
|
|
fn_val.child_scope,
|
2018-07-18 14:40:59 -07:00
|
|
|
body_node,
|
2018-07-24 11:20:49 -07:00
|
|
|
fn_type.key.data.Normal.return_type,
|
2018-07-19 12:11:39 -07:00
|
|
|
) catch unreachable);
|
2018-07-16 17:52:50 -07:00
|
|
|
errdefer analyzed_code.destroy(comp.gpa());
|
2018-07-13 18:56:38 -07:00
|
|
|
|
2018-07-24 17:24:05 -07:00
|
|
|
assert(fn_val.block_scope != null);
|
|
|
|
|
2018-07-14 21:04:12 -07:00
|
|
|
// Kick off rendering to LLVM module, but it doesn't block the fn decl
|
2018-07-14 12:45:15 -07:00
|
|
|
// analysis from being complete.
|
2018-07-16 17:52:50 -07:00
|
|
|
try comp.prelink_group.call(codegen.renderToLlvm, comp, fn_val, analyzed_code);
|
|
|
|
try comp.prelink_group.call(addFnToLinkSet, comp, fn_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn addFnToLinkSet(comp: *Compilation, fn_val: *Value.Fn) void {
|
|
|
|
fn_val.base.ref();
|
|
|
|
defer fn_val.base.deref(comp);
|
|
|
|
|
|
|
|
fn_val.link_set_node.data = fn_val;
|
|
|
|
|
|
|
|
const held = await (async comp.fn_link_set.acquire() catch unreachable);
|
|
|
|
defer held.release();
|
|
|
|
|
|
|
|
held.value.append(fn_val.link_set_node);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn getZigDir(allocator: *mem.Allocator) ![]u8 {
|
2018-07-18 07:07:22 -07:00
|
|
|
return os.getAppDataDir(allocator, "zig");
|
2018-07-12 12:08:40 -07:00
|
|
|
}
|
2018-07-22 20:27:58 -07:00
|
|
|
|
2018-08-03 14:22:17 -07:00
|
|
|
async fn analyzeFnType(
|
|
|
|
comp: *Compilation,
|
|
|
|
tree_scope: *Scope.AstTree,
|
|
|
|
scope: *Scope,
|
|
|
|
fn_proto: *ast.Node.FnProto,
|
|
|
|
) !*Type.Fn {
|
2018-07-22 20:27:58 -07:00
|
|
|
const return_type_node = switch (fn_proto.return_type) {
|
|
|
|
ast.Node.FnProto.ReturnType.Explicit => |n| n,
|
|
|
|
ast.Node.FnProto.ReturnType.InferErrorSet => |n| n,
|
|
|
|
};
|
2018-08-03 14:22:17 -07:00
|
|
|
const return_type = try await (async comp.analyzeTypeExpr(tree_scope, scope, return_type_node) catch unreachable);
|
2018-07-22 20:27:58 -07:00
|
|
|
return_type.base.deref(comp);
|
|
|
|
|
|
|
|
var params = ArrayList(Type.Fn.Param).init(comp.gpa());
|
|
|
|
var params_consumed = false;
|
2018-07-24 11:20:49 -07:00
|
|
|
defer if (!params_consumed) {
|
2018-07-22 20:27:58 -07:00
|
|
|
for (params.toSliceConst()) |param| {
|
|
|
|
param.typ.base.deref(comp);
|
|
|
|
}
|
|
|
|
params.deinit();
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
var it = fn_proto.params.iterator(0);
|
|
|
|
while (it.next()) |param_node_ptr| {
|
|
|
|
const param_node = param_node_ptr.*.cast(ast.Node.ParamDecl).?;
|
2018-08-03 14:22:17 -07:00
|
|
|
const param_type = try await (async comp.analyzeTypeExpr(tree_scope, scope, param_node.type_node) catch unreachable);
|
2018-07-22 20:27:58 -07:00
|
|
|
errdefer param_type.base.deref(comp);
|
|
|
|
try params.append(Type.Fn.Param{
|
|
|
|
.typ = param_type,
|
|
|
|
.is_noalias = param_node.noalias_token != null,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-07-24 11:20:49 -07:00
|
|
|
|
|
|
|
const key = Type.Fn.Key{
|
|
|
|
.alignment = null,
|
|
|
|
.data = Type.Fn.Key.Data{
|
2018-07-24 17:24:05 -07:00
|
|
|
.Normal = Type.Fn.Key.Normal{
|
2018-07-24 11:20:49 -07:00
|
|
|
.return_type = return_type,
|
|
|
|
.params = params.toOwnedSlice(),
|
|
|
|
.is_var_args = false, // TODO
|
|
|
|
.cc = Type.Fn.CallingConvention.Auto, // TODO
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2018-07-22 20:27:58 -07:00
|
|
|
params_consumed = true;
|
2018-07-24 11:20:49 -07:00
|
|
|
var key_consumed = false;
|
|
|
|
defer if (!key_consumed) {
|
|
|
|
for (key.data.Normal.params) |param| {
|
|
|
|
param.typ.base.deref(comp);
|
|
|
|
}
|
|
|
|
comp.gpa().free(key.data.Normal.params);
|
|
|
|
};
|
|
|
|
|
|
|
|
const fn_type = try await (async Type.Fn.get(comp, key) catch unreachable);
|
|
|
|
key_consumed = true;
|
2018-07-22 20:27:58 -07:00
|
|
|
errdefer fn_type.base.base.deref(comp);
|
|
|
|
|
|
|
|
return fn_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn generateDeclFnProto(comp: *Compilation, fn_decl: *Decl.Fn) !void {
|
2018-08-03 14:22:17 -07:00
|
|
|
const fn_type = try await (async analyzeFnType(
|
|
|
|
comp,
|
|
|
|
fn_decl.base.tree_scope,
|
|
|
|
fn_decl.base.parent_scope,
|
|
|
|
fn_decl.fn_proto,
|
|
|
|
) catch unreachable);
|
2018-07-22 20:27:58 -07:00
|
|
|
defer fn_type.base.base.deref(comp);
|
|
|
|
|
|
|
|
var symbol_name = try std.Buffer.init(comp.gpa(), fn_decl.base.name);
|
|
|
|
var symbol_name_consumed = false;
|
|
|
|
defer if (!symbol_name_consumed) symbol_name.deinit();
|
|
|
|
|
|
|
|
// The Decl.Fn owns the initial 1 reference count
|
|
|
|
const fn_proto_val = try Value.FnProto.create(comp, fn_type, symbol_name);
|
|
|
|
fn_decl.value = Decl.Fn.Val{ .FnProto = fn_proto_val };
|
|
|
|
symbol_name_consumed = true;
|
|
|
|
}
|
2018-08-10 09:28:20 -07:00
|
|
|
|
|
|
|
// TODO these are hacks which should probably be solved by the language
|
|
|
|
fn getAwaitResult(allocator: *Allocator, handle: var) @typeInfo(@typeOf(handle)).Promise.child.? {
|
|
|
|
var result: ?@typeInfo(@typeOf(handle)).Promise.child.? = null;
|
|
|
|
cancel (async<allocator> getAwaitResultAsync(handle, &result) catch unreachable);
|
|
|
|
return result.?;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn getAwaitResultAsync(handle: var, out: *?@typeInfo(@typeOf(handle)).Promise.child.?) void {
|
|
|
|
out.* = await handle;
|
|
|
|
}
|