@@ -20,8 +20,6 @@ const AliasList = common.AliasList;
2020const ResultUsed = common .ResultUsed ;
2121const Scope = common .ScopeExtra (Context , Type );
2222
23- pub const Compilation = aro .Compilation ;
24-
2523const Context = struct {
2624 gpa : mem.Allocator ,
2725 arena : mem.Allocator ,
@@ -53,7 +51,7 @@ const Context = struct {
5351
5452 pattern_list : translate_c.PatternList ,
5553 tree : Tree ,
56- comp : * Compilation ,
54+ comp : * aro. Compilation ,
5755 mapper : aro.TypeMapper ,
5856
5957 fn getMangle (c : * Context ) u32 {
@@ -101,7 +99,7 @@ fn failDecl(c: *Context, loc: TokenIndex, name: []const u8, comptime format: []c
10199
102100pub fn translate (
103101 gpa : mem.Allocator ,
104- comp : * Compilation ,
102+ comp : * aro. Compilation ,
105103 args : []const []const u8 ,
106104) ! std.zig.Ast {
107105 try comp .addDefaultPragmaHandlers ();
@@ -117,23 +115,18 @@ pub fn translate(
117115 assert (driver .inputs .items .len == 1 );
118116 const source = driver .inputs .items [0 ];
119117
120- const builtin = try comp .generateBuiltinMacros ();
118+ const builtin_macros = try comp .generateBuiltinMacros (.include_system_defines );
121119 const user_macros = try comp .addSourceFromBuffer ("<command line>" , macro_buf .items );
122120
123- var pp = aro .Preprocessor .init (comp );
121+ var pp = try aro .Preprocessor .initDefault (comp );
124122 defer pp .deinit ();
125123
126- try pp .addBuiltinMacros ();
127-
128- _ = try pp .preprocess (builtin );
129- _ = try pp .preprocess (user_macros );
130- const eof = try pp .preprocess (source );
131- try pp .tokens .append (pp .comp .gpa , eof );
124+ try pp .preprocessSources (&.{ source , builtin_macros , user_macros });
132125
133- var tree = try aro . Parser . parse (& pp );
126+ var tree = try pp . parse ();
134127 defer tree .deinit ();
135128
136- if (driver .comp .diag .errors != 0 ) {
129+ if (driver .comp .diagnostics .errors != 0 ) {
137130 return error .SemanticAnalyzeFail ;
138131 }
139132
@@ -432,14 +425,11 @@ fn transEnumDecl(c: *Context, scope: *Scope, enum_decl: NodeIndex, field_nodes:
432425 };
433426
434427 const val = c .tree .value_map .get (field_node ).? ;
435- const str = try std .fmt .allocPrint (c .arena , "{d}" , .{val .data .int });
436- const int = try ZigTag .integer_literal .create (c .arena , str );
437-
438428 const enum_const_def = try ZigTag .enum_constant .create (c .arena , .{
439429 .name = enum_val_name ,
440430 .is_public = toplevel ,
441431 .type = enum_const_type_node ,
442- .value = int ,
432+ .value = try transCreateNodeAPInt ( c , val ) ,
443433 });
444434 if (toplevel )
445435 try addTopLevelDecl (c , enum_val_name , enum_const_def )
@@ -554,7 +544,7 @@ fn transFnType(
554544
555545 const linksection_string = blk : {
556546 if (raw_ty .getAttribute (.section )) | section | {
557- break :blk section . name . slice ( c . tree . strings ) ;
547+ break :blk c . comp . interner . get ( section . name . ref ()). bytes ;
558548 }
559549 break :blk null ;
560550 };
@@ -648,8 +638,7 @@ fn transExpr(c: *Context, node: NodeIndex, result_used: ResultUsed) TransError!Z
648638 const ty = c .tree .nodes .items (.ty )[@intFromEnum (node )];
649639 if (c .tree .value_map .get (node )) | val | {
650640 // TODO handle other values
651- const str = try std .fmt .allocPrint (c .arena , "{d}" , .{val .data .int });
652- const int = try ZigTag .integer_literal .create (c .arena , str );
641+ const int = try transCreateNodeAPInt (c , val );
653642 const as_node = try ZigTag .as .create (c .arena , .{
654643 .lhs = try transType (c , undefined , ty , undefined ),
655644 .rhs = int ,
@@ -662,3 +651,17 @@ fn transExpr(c: *Context, node: NodeIndex, result_used: ResultUsed) TransError!Z
662651 }
663652 return .none ;
664653}
654+
655+ fn transCreateNodeAPInt (c : * Context , int : aro.Value ) ! ZigNode {
656+ var space : aro.Interner.Tag.Int.BigIntSpace = undefined ;
657+ var big = int .toBigInt (& space , c .comp );
658+ const is_negative = ! big .positive ;
659+ big .positive = true ;
660+
661+ const str = big .toStringAlloc (c .arena , 10 , .lower ) catch | err | switch (err ) {
662+ error .OutOfMemory = > return error .OutOfMemory ,
663+ };
664+ const res = try ZigTag .integer_literal .create (c .arena , str );
665+ if (is_negative ) return ZigTag .negate .create (c .arena , res );
666+ return res ;
667+ }
0 commit comments