1
1
package cscompiler ;
2
2
3
- #if (macro || cs_runtime)
4
-
3
+ #if(macro || cs_runtime)
5
4
import sys .io .File ;
6
5
7
6
import haxe .io .Path ;
8
7
import haxe .macro .Expr ;
9
8
import haxe .macro .Type ;
10
9
11
10
// ---
12
-
13
11
import reflaxe .BaseCompiler ;
14
12
import reflaxe .GenericCompiler ;
15
13
import reflaxe .data .ClassVarData ;
@@ -27,7 +25,6 @@ using reflaxe.helpers.OperatorHelper;
27
25
using reflaxe .helpers .TypeHelper ;
28
26
29
27
// ---
30
-
31
28
import cscompiler .ast .CSTypePath ;
32
29
import cscompiler .ast .CSExpr ;
33
30
import cscompiler .ast .CSArg ;
@@ -49,40 +46,40 @@ class CSCompiler extends reflaxe.GenericCompiler<CSTopLevel, CSTopLevel, CSState
49
46
The namespace used for top-level module types.
50
47
**/
51
48
public static final DEFAULT_ROOT_NAMESPACE = " haxe.root" ;
52
-
49
+
53
50
/**
54
51
Handles implementation of `compileClassImpl`.
55
52
**/
56
53
public var classComp (default , null ): CSCompiler_Class ;
57
-
54
+
58
55
/**
59
56
Handles implementation of `compileEnumImpl`.
60
57
**/
61
58
public var enumComp (default , null ): CSCompiler_Enum ;
62
-
59
+
63
60
/**
64
61
Handles implementation of `compileExprImpl`.
65
62
**/
66
63
public var exprComp (default , null ): CSCompiler_Expr ;
67
-
64
+
68
65
/**
69
66
Handles implementation of `compileType`, `compileModuleType`, and `compileClassName`.
70
67
**/
71
68
public var typeComp (default , null ): CSCompiler_Type ;
72
-
69
+
73
70
/**
74
71
Keep the inverted mapping of `nameToHashTable` to handle potential collisions
75
72
**/
76
- public var hashToNameTable (default , null ): Map <Int ,String > = new Map ();
77
-
73
+ public var hashToNameTable (default , null ): Map <Int , String > = new Map ();
74
+
78
75
/**
79
76
Constructor.
80
77
**/
81
78
public function new () {
82
79
super ();
83
80
createComponents ();
84
81
}
85
-
82
+
86
83
/**
87
84
Constructs all the components of the compiler.
88
85
@@ -91,45 +88,45 @@ class CSCompiler extends reflaxe.GenericCompiler<CSTopLevel, CSTopLevel, CSState
91
88
inline function createComponents () {
92
89
// Bypass Haxe null-safety not allowing `this` usage.
93
90
@:nullSafety (Off ) var self = this ;
94
-
91
+
95
92
classComp = new CSCompiler_Class (self );
96
93
enumComp = new CSCompiler_Enum (self );
97
94
exprComp = new CSCompiler_Expr (self );
98
95
typeComp = new CSCompiler_Type (self );
99
96
}
100
-
97
+
101
98
// ---
102
-
99
+
103
100
/**
104
101
The file name used for main function code.
105
102
106
103
TODO: Was this the name used in the original Haxe/C# target?
107
104
**/
108
105
static final BootFilename = " HaxeBoot.cs" ;
109
-
106
+
110
107
/**
111
108
Called at the start of compilation.
112
109
**/
113
110
public override function onCompileStart () {
114
111
setupMainFunction ();
115
112
setupCsProj ();
116
113
}
117
-
114
+
118
115
/**
119
116
If -main exists, generate a Main function in C#.
120
117
**/
121
118
function setupMainFunction () {
122
119
final mainExpr = getMainExpr ();
123
120
if (mainExpr != null ) {
124
121
final csExpr = compileExpressionOrError (mainExpr );
125
-
122
+
126
123
// TODO: Convert `csExpr` to `String` using printer
127
124
final csCode = " " ;
128
-
125
+
129
126
appendToExtraFile (BootFilename , haxeBootContent (csCode ));
130
127
}
131
128
}
132
-
129
+
133
130
/**
134
131
Returns the content generated for the `HaxeBoot.cs`.
135
132
@@ -147,7 +144,7 @@ namespace Haxe {
147
144
}
148
145
' );
149
146
}
150
-
147
+
151
148
/**
152
149
Adds a .csproj file to the output directory.
153
150
@@ -159,17 +156,17 @@ namespace Haxe {
159
156
Otherwise, a default .csproj is generated.
160
157
**/
161
158
function setupCsProj () {
162
- if (D_NoCsproj .isDefined ()) {
159
+ if (D_NoCsproj .isDefined ()) {
163
160
return ;
164
161
}
165
- if (! D_Csproj .isDefined ()) {
162
+ if (! D_Csproj .isDefined ()) {
166
163
appendToExtraFile (" build.csproj" , csProjDefaultContent ());
167
164
return ;
168
165
}
169
166
final path = new Path (Context .resolvePath (D_Csproj .getValue ()));
170
167
appendToExtraFile (' ${path .file }. ${path .ext }' , File .getContent (path .toString ()));
171
168
}
172
-
169
+
173
170
/**
174
171
Returns the default content of the .csproj file.
175
172
**/
@@ -188,13 +185,13 @@ namespace Haxe {
188
185
</Project>
189
186
' );
190
187
}
191
-
188
+
192
189
/**
193
190
Called at the end of compilation.
194
191
**/
195
192
public override function onCompileEnd () {
196
193
}
197
-
194
+
198
195
/**
199
196
Generate output.
200
197
@@ -204,33 +201,33 @@ namespace Haxe {
204
201
// TODO: Print all classes and enums using these vars from `GenericCompiler`:
205
202
// var classes: Array<CSClass>
206
203
// var enums: Array<CSEnum>
207
-
204
+
208
205
return {
209
206
hasNext : () -> false ,
210
207
next : () -> {
211
208
return new DataAndFileInfo (StringOrBytes .fromString (" " ), @:nullSafety (Off ) null , null , null );
212
209
}
213
210
};
214
211
}
215
-
212
+
216
213
// ---
217
-
214
+
218
215
/**
219
216
Generate the C# output given the Haxe class information.
220
217
**/
221
218
public function compileClassImpl (classType : ClassType , varFields : Array <ClassVarData >, funcFields : Array <ClassFuncData >): Null <CSTopLevel > {
222
219
return classComp .compile (classType , varFields , funcFields );
223
220
}
224
-
221
+
225
222
/**
226
223
Generate the C# output given the Haxe enum information.
227
224
**/
228
225
public function compileEnumImpl (enumType : EnumType , options : Array <EnumOptionData >): Null <CSTopLevel > {
229
226
return enumComp .compile (enumType , options );
230
227
}
231
-
228
+
232
229
// ---
233
-
230
+
234
231
/**
235
232
Generates the C# type from `haxe.macro.Type`.
236
233
@@ -243,112 +240,107 @@ namespace Haxe {
243
240
}
244
241
return result ;
245
242
}
246
-
243
+
247
244
/**
248
245
Generate C# output for `ModuleType` used in an expression
249
246
(i.e. for cast or static access).
250
247
**/
251
248
public function compileModuleType (m : ModuleType ): CSTypePath {
252
249
return typeComp .compileModuleType (m );
253
250
}
254
-
251
+
255
252
/**
256
253
Get the name of the `ClassType` as it should appear in
257
254
the C# output.
258
255
**/
259
256
public function compileClassName (classType : ClassType ): String {
260
257
return typeComp .compileClassName (classType );
261
258
}
262
-
259
+
263
260
// ---
264
-
261
+
265
262
/**
266
263
Generate the C# output for a function argument.
267
264
268
265
Note: it's possible for an argument to be optional but not have an `expr`.
269
266
**/
270
- public function compileFunctionArgument (t : Type , name : String , pos : Position , optional : Bool , expr : Null <TypedExpr > = null ): CSArg {
267
+ public function compileFunctionArgument (t : Type , name : String , pos : Position , optional : Bool , expr : Null <TypedExpr > = null ): CSArg {
271
268
return {
272
269
name : compileVarName (name ),
273
270
type : compileType (t , pos ),
274
271
opt : optional ,
275
272
value : expr != null ? compileToCSExpr (expr ) : null
276
273
};
277
274
}
278
-
275
+
279
276
public function compileClassVarExpr (expr : TypedExpr ): Null <CSExpr > {
280
277
return compileToCSExpr (expr );
281
278
}
282
-
279
+
283
280
public function compileClassFuncExpr (expr : TypedExpr ): Null <CSStatement > {
284
281
return compileExpression (expr );
285
282
}
286
-
283
+
287
284
/**
288
285
Compile an expression and ensure it is an actual C# expression (not a statement)
289
286
**/
290
287
public function compileToCSExpr (expr : TypedExpr ): Null <CSExpr > {
291
288
return exprComp .compileToCSExpr (expr );
292
289
}
293
-
290
+
294
291
/**
295
292
Generate the C# output given the Haxe typed expression (`TypedExpr`).
296
293
**/
297
294
public function compileExpressionImpl (expr : TypedExpr , topLevel : Bool ): Null <CSStatement > {
298
295
return exprComp .compile (expr , topLevel );
299
296
}
300
-
297
+
301
298
/**
302
299
Wrap a block of code with the given name space
303
300
**/
304
- public function wrapNameSpace (nameSpace : String , s : String ): String {
301
+ public function wrapNameSpace (nameSpace : String , s : String ): String {
305
302
return " namespace " + nameSpace + " {\n " + StringTools .rtrim (s .tab ()) + " \n }\n " ;
306
303
}
307
-
304
+
308
305
/**
309
306
Remove blank white space at the end of each line,
310
307
and trim empty lines.
311
308
**/
312
- public function cleanWhiteSpaces (s : String ): String {
313
-
309
+ public function cleanWhiteSpaces (s : String ): String {
314
310
// Temporary workaround.
315
-
311
+
316
312
// TODO: edit reflaxe SyntaxHelper.tab() so that it
317
313
// doesn't add spaces/tabs to empty lines when indenting
318
314
// a block, and make this method not needed anymore
319
-
315
+
320
316
final lines = s .split (" \n " );
321
- for (i in 0 ... lines .length ) {
317
+ for (i in 0 ... lines .length ) {
322
318
lines [i ] = StringTools .rtrim (lines [i ]);
323
319
}
324
320
return lines .join (" \n " );
325
321
}
326
-
322
+
327
323
/**
328
324
Get a hash code for the given name. If the name is new,
329
325
add an entry to `hashToNameTable` so that it can be used
330
326
for field lookup in generated code.
331
327
**/
332
328
public function nameToHash (name : String ): Int {
333
-
334
- var h : Int = 0 ;
335
- for (i in 0 ... name .length ) {
336
- h = 223 * h + name .charCodeAt (i );
337
- }
338
- h % = 0x1FFFFF7B ;
339
-
340
- while (hashToNameTable .exists (h ) && hashToNameTable .get (h ) != name ) {
329
+ var h : Int = 0 ;
330
+ for (i in 0 ... name .length ) {
331
+ h = 223 * h + name .charCodeAt (i );
332
+ }
333
+ h % = 0x1FFFFF7B ;
334
+
335
+ while (hashToNameTable .exists (h ) && hashToNameTable .get (h ) != name ) {
341
336
h ++ ;
342
337
}
343
-
344
- if (! hashToNameTable .exists (h )) {
338
+
339
+ if (! hashToNameTable .exists (h )) {
345
340
hashToNameTable .set (h , name );
346
341
}
347
-
342
+
348
343
return h ;
349
-
350
344
}
351
-
352
345
}
353
-
354
- #end
346
+ #end
0 commit comments