11const std = @import ("std" );
2+ const Io = std .Io ;
23const assert = std .debug .assert ;
34const EpochSeconds = std .time .epoch .EpochSeconds ;
45const mem = std .mem ;
@@ -113,7 +114,7 @@ pub const Environment = struct {
113114 if (parsed > max_timestamp ) return error .InvalidEpoch ;
114115 return .{ .provided = parsed };
115116 } else {
116- const timestamp = std .math .cast (u64 , std . time . timestamp () ) orelse return error .InvalidEpoch ;
117+ const timestamp = std .math .cast (u64 , 0 ) orelse return error .InvalidEpoch ;
117118 return .{ .system = std .math .clamp (timestamp , 0 , max_timestamp ) };
118119 }
119120 }
@@ -124,6 +125,7 @@ const Compilation = @This();
124125gpa : Allocator ,
125126/// Allocations in this arena live all the way until `Compilation.deinit`.
126127arena : Allocator ,
128+ io : Io ,
127129diagnostics : * Diagnostics ,
128130
129131code_gen_options : CodeGenOptions = .default ,
@@ -157,21 +159,23 @@ type_store: TypeStore = .{},
157159ms_cwd_source_id : ? Source.Id = null ,
158160cwd : std.fs.Dir ,
159161
160- pub fn init (gpa : Allocator , arena : Allocator , diagnostics : * Diagnostics , cwd : std.fs.Dir ) Compilation {
162+ pub fn init (gpa : Allocator , arena : Allocator , io : Io , diagnostics : * Diagnostics , cwd : std.fs.Dir ) Compilation {
161163 return .{
162164 .gpa = gpa ,
163165 .arena = arena ,
166+ .io = io ,
164167 .diagnostics = diagnostics ,
165168 .cwd = cwd ,
166169 };
167170}
168171
169172/// Initialize Compilation with default environment,
170173/// pragma handlers and emulation mode set to target.
171- pub fn initDefault (gpa : Allocator , arena : Allocator , diagnostics : * Diagnostics , cwd : std.fs.Dir ) ! Compilation {
174+ pub fn initDefault (gpa : Allocator , arena : Allocator , io : Io , diagnostics : * Diagnostics , cwd : std.fs.Dir ) ! Compilation {
172175 var comp : Compilation = .{
173176 .gpa = gpa ,
174177 .arena = arena ,
178+ .io = io ,
175179 .diagnostics = diagnostics ,
176180 .environment = try Environment .loadAll (gpa ),
177181 .cwd = cwd ,
@@ -222,14 +226,14 @@ pub const SystemDefinesMode = enum {
222226 include_system_defines ,
223227};
224228
225- fn generateSystemDefines (comp : * Compilation , w : * std. Io.Writer ) ! void {
229+ fn generateSystemDefines (comp : * Compilation , w : * Io.Writer ) ! void {
226230 const define = struct {
227- fn define (_w : * std. Io.Writer , name : []const u8 ) ! void {
231+ fn define (_w : * Io.Writer , name : []const u8 ) ! void {
228232 try _w .print ("#define {s} 1\n " , .{name });
229233 }
230234 }.define ;
231235 const defineStd = struct {
232- fn defineStd (_w : * std. Io.Writer , name : []const u8 , is_gnu : bool ) ! void {
236+ fn defineStd (_w : * Io.Writer , name : []const u8 , is_gnu : bool ) ! void {
233237 if (is_gnu ) {
234238 try _w .print ("#define {s} 1\n " , .{name });
235239 }
@@ -956,7 +960,7 @@ fn generateSystemDefines(comp: *Compilation, w: *std.Io.Writer) !void {
956960pub fn generateBuiltinMacros (comp : * Compilation , system_defines_mode : SystemDefinesMode ) AddSourceError ! Source {
957961 try comp .type_store .initNamedTypes (comp );
958962
959- var allocating : std. Io.Writer.Allocating = try .initCapacity (comp .gpa , 2 << 13 );
963+ var allocating : Io.Writer.Allocating = try .initCapacity (comp .gpa , 2 << 13 );
960964 defer allocating .deinit ();
961965
962966 comp .writeBuiltinMacros (system_defines_mode , & allocating .writer ) catch | err | switch (err ) {
@@ -970,7 +974,7 @@ pub fn generateBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefi
970974 return comp .addSourceFromOwnedBuffer ("<builtin>" , contents , .user );
971975}
972976
973- fn writeBuiltinMacros (comp : * Compilation , system_defines_mode : SystemDefinesMode , w : * std. Io.Writer ) ! void {
977+ fn writeBuiltinMacros (comp : * Compilation , system_defines_mode : SystemDefinesMode , w : * Io.Writer ) ! void {
974978 if (system_defines_mode == .include_system_defines ) {
975979 try w .writeAll (
976980 \\#define __VERSION__ "Aro
@@ -1018,7 +1022,7 @@ fn writeBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefinesMode
10181022 }
10191023}
10201024
1021- fn generateFloatMacros (w : * std. Io.Writer , prefix : []const u8 , semantics : target_util.FPSemantics , ext : []const u8 ) ! void {
1025+ fn generateFloatMacros (w : * Io.Writer , prefix : []const u8 , semantics : target_util.FPSemantics , ext : []const u8 ) ! void {
10221026 const denormMin = semantics .chooseValue (
10231027 []const u8 ,
10241028 .{
@@ -1093,7 +1097,7 @@ fn generateFloatMacros(w: *std.Io.Writer, prefix: []const u8, semantics: target_
10931097 try w .print ("#define __{s}_MIN__ {s}{s}\n " , .{ prefix , min , ext });
10941098}
10951099
1096- fn generateTypeMacro (comp : * const Compilation , w : * std. Io.Writer , name : []const u8 , qt : QualType ) ! void {
1100+ fn generateTypeMacro (comp : * const Compilation , w : * Io.Writer , name : []const u8 , qt : QualType ) ! void {
10971101 try w .print ("#define {s} " , .{name });
10981102 try qt .print (comp , w );
10991103 try w .writeByte ('\n ' );
@@ -1128,7 +1132,7 @@ fn generateFastOrLeastType(
11281132 bits : usize ,
11291133 kind : enum { least , fast },
11301134 signedness : std.builtin.Signedness ,
1131- w : * std. Io.Writer ,
1135+ w : * Io.Writer ,
11321136) ! void {
11331137 const ty = comp .intLeastN (bits , signedness ); // defining the fast types as the least types is permitted
11341138
@@ -1158,7 +1162,7 @@ fn generateFastOrLeastType(
11581162 try comp .generateFmt (prefix , w , ty );
11591163}
11601164
1161- fn generateFastAndLeastWidthTypes (comp : * Compilation , w : * std. Io.Writer ) ! void {
1165+ fn generateFastAndLeastWidthTypes (comp : * Compilation , w : * Io.Writer ) ! void {
11621166 const sizes = [_ ]usize { 8 , 16 , 32 , 64 };
11631167 for (sizes ) | size | {
11641168 try comp .generateFastOrLeastType (size , .least , .signed , w );
@@ -1168,7 +1172,7 @@ fn generateFastAndLeastWidthTypes(comp: *Compilation, w: *std.Io.Writer) !void {
11681172 }
11691173}
11701174
1171- fn generateExactWidthTypes (comp : * Compilation , w : * std. Io.Writer ) ! void {
1175+ fn generateExactWidthTypes (comp : * Compilation , w : * Io.Writer ) ! void {
11721176 try comp .generateExactWidthType (w , .schar );
11731177
11741178 if (QualType .short .sizeof (comp ) > QualType .char .sizeof (comp )) {
@@ -1216,7 +1220,7 @@ fn generateExactWidthTypes(comp: *Compilation, w: *std.Io.Writer) !void {
12161220 }
12171221}
12181222
1219- fn generateFmt (comp : * const Compilation , prefix : []const u8 , w : * std. Io.Writer , qt : QualType ) ! void {
1223+ fn generateFmt (comp : * const Compilation , prefix : []const u8 , w : * Io.Writer , qt : QualType ) ! void {
12201224 const unsigned = qt .signedness (comp ) == .unsigned ;
12211225 const modifier = qt .formatModifier (comp );
12221226 const formats = if (unsigned ) "ouxX" else "di" ;
@@ -1225,15 +1229,15 @@ fn generateFmt(comp: *const Compilation, prefix: []const u8, w: *std.Io.Writer,
12251229 }
12261230}
12271231
1228- fn generateSuffixMacro (comp : * const Compilation , prefix : []const u8 , w : * std. Io.Writer , qt : QualType ) ! void {
1232+ fn generateSuffixMacro (comp : * const Compilation , prefix : []const u8 , w : * Io.Writer , qt : QualType ) ! void {
12291233 return w .print ("#define {s}_C_SUFFIX__ {s}\n " , .{ prefix , qt .intValueSuffix (comp ) });
12301234}
12311235
12321236/// Generate the following for a type:
12331237/// Name macro (e.g. #define __UINT32_TYPE__ unsigned int)
12341238/// Format strings (e.g. #define __UINT32_FMTu__ "u")
12351239/// Suffix macro (e.g. #define __UINT32_C_SUFFIX__ U)
1236- fn generateExactWidthType (comp : * Compilation , w : * std. Io.Writer , original_qt : QualType ) ! void {
1240+ fn generateExactWidthType (comp : * Compilation , w : * Io.Writer , original_qt : QualType ) ! void {
12371241 var qt = original_qt ;
12381242 const width = qt .sizeof (comp ) * 8 ;
12391243 const unsigned = qt .signedness (comp ) == .unsigned ;
@@ -1266,7 +1270,7 @@ pub fn hasHalfPrecisionFloatABI(comp: *const Compilation) bool {
12661270 return comp .langopts .allow_half_args_and_returns or target_util .hasHalfPrecisionFloatABI (comp .target );
12671271}
12681272
1269- fn generateIntMax (comp : * const Compilation , w : * std. Io.Writer , name : []const u8 , qt : QualType ) ! void {
1273+ fn generateIntMax (comp : * const Compilation , w : * Io.Writer , name : []const u8 , qt : QualType ) ! void {
12701274 const unsigned = qt .signedness (comp ) == .unsigned ;
12711275 const max : u128 = switch (qt .bitSizeof (comp )) {
12721276 8 = > if (unsigned ) std .math .maxInt (u8 ) else std .math .maxInt (i8 ),
@@ -1290,7 +1294,7 @@ pub fn wcharMax(comp: *const Compilation) u32 {
12901294 };
12911295}
12921296
1293- fn generateExactWidthIntMax (comp : * Compilation , w : * std. Io.Writer , original_qt : QualType ) ! void {
1297+ fn generateExactWidthIntMax (comp : * Compilation , w : * Io.Writer , original_qt : QualType ) ! void {
12941298 var qt = original_qt ;
12951299 const bit_count : u8 = @intCast (qt .sizeof (comp ) * 8 );
12961300 const unsigned = qt .signedness (comp ) == .unsigned ;
@@ -1307,16 +1311,16 @@ fn generateExactWidthIntMax(comp: *Compilation, w: *std.Io.Writer, original_qt:
13071311 return comp .generateIntMax (w , name , qt );
13081312}
13091313
1310- fn generateIntWidth (comp : * Compilation , w : * std. Io.Writer , name : []const u8 , qt : QualType ) ! void {
1314+ fn generateIntWidth (comp : * Compilation , w : * Io.Writer , name : []const u8 , qt : QualType ) ! void {
13111315 try w .print ("#define __{s}_WIDTH__ {d}\n " , .{ name , qt .sizeof (comp ) * 8 });
13121316}
13131317
1314- fn generateIntMaxAndWidth (comp : * Compilation , w : * std. Io.Writer , name : []const u8 , qt : QualType ) ! void {
1318+ fn generateIntMaxAndWidth (comp : * Compilation , w : * Io.Writer , name : []const u8 , qt : QualType ) ! void {
13151319 try comp .generateIntMax (w , name , qt );
13161320 try comp .generateIntWidth (w , name , qt );
13171321}
13181322
1319- fn generateSizeofType (comp : * Compilation , w : * std. Io.Writer , name : []const u8 , qt : QualType ) ! void {
1323+ fn generateSizeofType (comp : * Compilation , w : * Io.Writer , name : []const u8 , qt : QualType ) ! void {
13201324 try w .print ("#define {s} {d}\n " , .{ name , qt .sizeof (comp ) });
13211325}
13221326
@@ -1797,7 +1801,7 @@ pub const IncludeType = enum {
17971801 angle_brackets ,
17981802};
17991803
1800- fn getPathContents (comp : * Compilation , path : []const u8 , limit : std. Io.Limit ) ! []u8 {
1804+ fn getPathContents (comp : * Compilation , path : []const u8 , limit : Io.Limit ) ! []u8 {
18011805 if (mem .indexOfScalar (u8 , path , 0 ) != null ) {
18021806 return error .FileNotFound ;
18031807 }
@@ -1807,11 +1811,12 @@ fn getPathContents(comp: *Compilation, path: []const u8, limit: std.Io.Limit) ![
18071811 return comp .getFileContents (file , limit );
18081812}
18091813
1810- fn getFileContents (comp : * Compilation , file : std.fs.File , limit : std.Io.Limit ) ! []u8 {
1814+ fn getFileContents (comp : * Compilation , file : std.fs.File , limit : Io.Limit ) ! []u8 {
1815+ const io = comp .io ;
18111816 var file_buf : [4096 ]u8 = undefined ;
1812- var file_reader = file .reader (& file_buf );
1817+ var file_reader = file .reader (io , & file_buf );
18131818
1814- var allocating : std. Io.Writer.Allocating = .init (comp .gpa );
1819+ var allocating : Io.Writer.Allocating = .init (comp .gpa );
18151820 defer allocating .deinit ();
18161821 if (file_reader .getSize ()) | size | {
18171822 const limited_size = limit .minInt64 (size );
@@ -1838,7 +1843,7 @@ pub fn findEmbed(
18381843 includer_token_source : Source.Id ,
18391844 /// angle bracket vs quotes
18401845 include_type : IncludeType ,
1841- limit : std. Io.Limit ,
1846+ limit : Io.Limit ,
18421847 opt_dep_file : ? * DepFile ,
18431848) ! ? []u8 {
18441849 if (std .fs .path .isAbsolute (filename )) {
@@ -2002,8 +2007,7 @@ pub fn locSlice(comp: *const Compilation, loc: Source.Location) []const u8 {
20022007pub fn getSourceMTimeUncached (comp : * const Compilation , source_id : Source.Id ) ? u64 {
20032008 const source = comp .getSource (source_id );
20042009 if (comp .cwd .statFile (source .path )) | stat | {
2005- const mtime = @divTrunc (stat .mtime , std .time .ns_per_s );
2006- return std .math .cast (u64 , mtime );
2010+ return std .math .cast (u64 , stat .mtime .toSeconds ());
20072011 } else | _ | {
20082012 return null ;
20092013 }
0 commit comments