@@ -28,26 +28,36 @@ pub fn emit_program(sa: Sema) -> Program {
28
28
globals : Vec :: new ( ) ,
29
29
packages : Vec :: new ( ) ,
30
30
modules : Vec :: new ( ) ,
31
+ classes : Vec :: new ( ) ,
32
+ structs : Vec :: new ( ) ,
33
+ enums : Default :: default ( ) ,
34
+ traits : Default :: default ( ) ,
35
+ source_files : Default :: default ( ) ,
31
36
} ;
32
37
33
38
emitter. create_packages ( & sa) ;
34
39
emitter. create_modules ( & sa) ;
40
+ emitter. create_classes ( & sa) ;
41
+ emitter. create_structs ( & sa) ;
35
42
emitter. create_functions ( & sa) ;
36
43
emitter. create_globals ( & sa) ;
44
+ emitter. create_enums ( & sa) ;
45
+ emitter. create_traits ( & sa) ;
46
+ emitter. create_source_files ( & sa) ;
37
47
38
48
Program {
39
49
packages : emitter. packages ,
40
50
modules : emitter. modules ,
41
51
functions : emitter. functions ,
42
52
globals : emitter. globals ,
43
- classes : create_classes ( & sa ) ,
44
- structs : create_structs ( & sa ) ,
45
- enums : create_enums ( & sa ) ,
46
- traits : create_traits ( & sa ) ,
53
+ classes : emitter . classes ,
54
+ structs : emitter . structs ,
55
+ enums : emitter . enums ,
56
+ traits : emitter . traits ,
47
57
extensions : create_extensions ( & sa) ,
48
58
impls : create_impls ( & sa) ,
49
59
aliases : create_aliases ( & sa) ,
50
- source_files : create_source_files ( & sa ) ,
60
+ source_files : emitter . source_files ,
51
61
stdlib_package_id : convert_package_id ( sa. stdlib_package_id ( ) ) ,
52
62
program_package_id : convert_package_id ( sa. program_package_id ( ) ) ,
53
63
boots_package_id : sa. boots_package_id . map ( |p| convert_package_id ( p) ) ,
@@ -62,6 +72,11 @@ struct Emitter {
62
72
globals : Vec < GlobalData > ,
63
73
packages : Vec < PackageData > ,
64
74
modules : Vec < ModuleData > ,
75
+ classes : Vec < ClassData > ,
76
+ structs : Vec < StructData > ,
77
+ enums : Vec < EnumData > ,
78
+ traits : Vec < TraitData > ,
79
+ source_files : Vec < SourceFileData > ,
65
80
}
66
81
67
82
impl Emitter {
@@ -96,6 +111,88 @@ impl Emitter {
96
111
}
97
112
}
98
113
114
+ fn create_classes ( & mut self , sa : & Sema ) {
115
+ for ( _class_id, class) in sa. classes . iter ( ) {
116
+ let name = sa. interner . str ( class. name ) . to_string ( ) ;
117
+ let fields = self . create_class_fields ( sa, & * class) ;
118
+
119
+ self . classes . push ( ClassData {
120
+ module_id : convert_module_id ( class. module_id ) ,
121
+ name,
122
+ type_params : create_type_params ( sa, class. type_param_definition ( ) ) ,
123
+ fields,
124
+ } )
125
+ }
126
+ }
127
+
128
+ fn create_class_fields ( & mut self , sa : & Sema , class : & ClassDefinition ) -> Vec < ClassField > {
129
+ class
130
+ . fields
131
+ . iter ( )
132
+ . map ( |f| ClassField {
133
+ ty : bty_from_ty ( f. ty ( ) ) ,
134
+ name : f. name . map ( |n| sa. interner . str ( n) . to_string ( ) ) ,
135
+ } )
136
+ . collect ( )
137
+ }
138
+
139
+ fn create_structs ( & mut self , sa : & Sema ) {
140
+ for ( _struct_id, struct_) in sa. structs . iter ( ) {
141
+ let name = sa. interner . str ( struct_. name ) . to_string ( ) ;
142
+ let fields = self . create_struct_fields ( sa, struct_) ;
143
+
144
+ self . structs . push ( StructData {
145
+ module_id : convert_module_id ( struct_. module_id ) ,
146
+ name,
147
+ type_params : create_type_params ( sa, struct_. type_param_definition ( ) ) ,
148
+ fields,
149
+ } )
150
+ }
151
+ }
152
+
153
+ fn create_struct_fields ( & mut self , sa : & Sema , struct_ : & StructDefinition ) -> Vec < StructField > {
154
+ struct_
155
+ . fields
156
+ . iter ( )
157
+ . map ( |f| StructField {
158
+ ty : bty_from_ty ( f. ty ( ) ) ,
159
+ name : f. name . map ( |n| sa. interner . str ( n) . to_string ( ) ) ,
160
+ } )
161
+ . collect ( )
162
+ }
163
+
164
+ fn create_enums ( & mut self , sa : & Sema ) {
165
+ for ( _id, enum_) in sa. enums . iter ( ) {
166
+ let name = sa. interner . str ( enum_. name ) . to_string ( ) ;
167
+ let variants = self . create_enum_variants ( sa, & * enum_) ;
168
+
169
+ self . enums . push ( EnumData {
170
+ module_id : convert_module_id ( enum_. module_id ) ,
171
+ name,
172
+ type_params : create_type_params ( sa, enum_. type_param_definition ( ) ) ,
173
+ variants,
174
+ } )
175
+ }
176
+ }
177
+
178
+ fn create_enum_variants ( & mut self , sa : & Sema , enum_ : & EnumDefinition ) -> Vec < EnumVariant > {
179
+ let mut result = Vec :: new ( ) ;
180
+
181
+ for variant in enum_. variants ( ) {
182
+ let arguments = variant
183
+ . fields
184
+ . iter ( )
185
+ . map ( |f| bty_from_ty ( f. parsed_type . ty ( ) ) )
186
+ . collect ( ) ;
187
+ result. push ( EnumVariant {
188
+ name : sa. interner . str ( variant. name ) . to_string ( ) ,
189
+ arguments,
190
+ } )
191
+ }
192
+
193
+ result
194
+ }
195
+
99
196
fn create_functions ( & mut self , sa : & Sema ) {
100
197
for ( id, fct) in sa. fcts . iter ( ) {
101
198
let name = sa. interner . str ( fct. name ) . to_string ( ) ;
@@ -191,6 +288,44 @@ impl Emitter {
191
288
} )
192
289
}
193
290
}
291
+
292
+ fn create_traits ( & mut self , sa : & Sema ) {
293
+ for ( _id, trait_) in sa. traits . iter ( ) {
294
+ let name = sa. interner . str ( trait_. name ) . to_string ( ) ;
295
+
296
+ let methods = trait_
297
+ . methods ( )
298
+ . iter ( )
299
+ . map ( |f| convert_function_id ( * f) )
300
+ . collect ( ) ;
301
+
302
+ let virtual_methods = trait_
303
+ . methods ( )
304
+ . iter ( )
305
+ . filter ( |f| {
306
+ let method = sa. fct ( * * f) ;
307
+ !method. is_trait_object_ignore
308
+ } )
309
+ . map ( |f| convert_function_id ( * f) )
310
+ . collect ( ) ;
311
+
312
+ self . traits . push ( TraitData {
313
+ module_id : convert_module_id ( trait_. module_id ) ,
314
+ name,
315
+ type_params : create_type_params ( sa, & trait_. type_param_definition ( ) ) ,
316
+ methods,
317
+ virtual_methods,
318
+ } )
319
+ }
320
+ }
321
+
322
+ fn create_source_files ( & mut self , sa : & Sema ) {
323
+ for ( _id, file) in sa. source_files . iter ( ) {
324
+ self . source_files . push ( SourceFileData {
325
+ path : file. path . to_string_lossy ( ) . to_string ( ) ,
326
+ } )
327
+ }
328
+ }
194
329
}
195
330
196
331
fn create_extensions ( sa : & Sema ) -> Vec < ExtensionData > {
@@ -293,51 +428,6 @@ fn global_initializer_function_id(
293
428
)
294
429
}
295
430
296
- fn create_classes ( sa : & Sema ) -> Vec < ClassData > {
297
- let mut result = Vec :: new ( ) ;
298
-
299
- for ( _class_id, class) in sa. classes . iter ( ) {
300
- let name = sa. interner . str ( class. name ) . to_string ( ) ;
301
-
302
- result. push ( ClassData {
303
- module_id : convert_module_id ( class. module_id ) ,
304
- name,
305
- type_params : create_type_params ( sa, class. type_param_definition ( ) ) ,
306
- fields : create_class_fields ( sa, & * class) ,
307
- } )
308
- }
309
-
310
- result
311
- }
312
-
313
- fn create_class_fields ( sa : & Sema , class : & ClassDefinition ) -> Vec < ClassField > {
314
- class
315
- . fields
316
- . iter ( )
317
- . map ( |f| ClassField {
318
- ty : bty_from_ty ( f. ty ( ) ) ,
319
- name : f. name . map ( |n| sa. interner . str ( n) . to_string ( ) ) ,
320
- } )
321
- . collect ( )
322
- }
323
-
324
- fn create_structs ( sa : & Sema ) -> Vec < StructData > {
325
- let mut result = Vec :: new ( ) ;
326
-
327
- for ( _struct_id, struct_) in sa. structs . iter ( ) {
328
- let name = sa. interner . str ( struct_. name ) . to_string ( ) ;
329
-
330
- result. push ( StructData {
331
- module_id : convert_module_id ( struct_. module_id ) ,
332
- name,
333
- type_params : create_type_params ( sa, struct_. type_param_definition ( ) ) ,
334
- fields : create_struct_fields ( sa, & * struct_) ,
335
- } )
336
- }
337
-
338
- result
339
- }
340
-
341
431
fn create_type_params ( sa : & Sema , type_params : & TypeParamDefinition ) -> TypeParamData {
342
432
let names = type_params
343
433
. names ( )
@@ -361,98 +451,6 @@ fn create_type_params(sa: &Sema, type_params: &TypeParamDefinition) -> TypeParam
361
451
}
362
452
}
363
453
364
- fn create_struct_fields ( sa : & Sema , struct_ : & StructDefinition ) -> Vec < StructField > {
365
- struct_
366
- . fields
367
- . iter ( )
368
- . map ( |f| StructField {
369
- ty : bty_from_ty ( f. ty ( ) ) ,
370
- name : f. name . map ( |n| sa. interner . str ( n) . to_string ( ) ) ,
371
- } )
372
- . collect ( )
373
- }
374
-
375
- fn create_enums ( sa : & Sema ) -> Vec < EnumData > {
376
- let mut result = Vec :: new ( ) ;
377
-
378
- for ( _id, enum_) in sa. enums . iter ( ) {
379
- let name = sa. interner . str ( enum_. name ) . to_string ( ) ;
380
-
381
- result. push ( EnumData {
382
- module_id : convert_module_id ( enum_. module_id ) ,
383
- name,
384
- type_params : create_type_params ( sa, enum_. type_param_definition ( ) ) ,
385
- variants : create_enum_variants ( sa, & * enum_) ,
386
- } )
387
- }
388
-
389
- result
390
- }
391
-
392
- fn create_enum_variants ( sa : & Sema , enum_ : & EnumDefinition ) -> Vec < EnumVariant > {
393
- let mut result = Vec :: new ( ) ;
394
-
395
- for variant in enum_. variants ( ) {
396
- let arguments = variant
397
- . fields
398
- . iter ( )
399
- . map ( |f| bty_from_ty ( f. parsed_type . ty ( ) ) )
400
- . collect ( ) ;
401
- result. push ( EnumVariant {
402
- name : sa. interner . str ( variant. name ) . to_string ( ) ,
403
- arguments,
404
- } )
405
- }
406
-
407
- result
408
- }
409
-
410
- fn create_traits ( sa : & Sema ) -> Vec < TraitData > {
411
- let mut result = Vec :: new ( ) ;
412
-
413
- for ( _id, trait_) in sa. traits . iter ( ) {
414
- let name = sa. interner . str ( trait_. name ) . to_string ( ) ;
415
-
416
- let methods = trait_
417
- . methods ( )
418
- . iter ( )
419
- . map ( |f| convert_function_id ( * f) )
420
- . collect ( ) ;
421
-
422
- let virtual_methods = trait_
423
- . methods ( )
424
- . iter ( )
425
- . filter ( |f| {
426
- let method = sa. fct ( * * f) ;
427
- !method. is_trait_object_ignore
428
- } )
429
- . map ( |f| convert_function_id ( * f) )
430
- . collect ( ) ;
431
-
432
- result. push ( TraitData {
433
- module_id : convert_module_id ( trait_. module_id ) ,
434
- name,
435
- type_params : create_type_params ( sa, & trait_. type_param_definition ( ) ) ,
436
- methods,
437
- virtual_methods,
438
- } )
439
- }
440
-
441
- result
442
- }
443
-
444
- fn create_source_files ( sa : & Sema ) -> Vec < SourceFileData > {
445
- let mut result = Vec :: new ( ) ;
446
-
447
- for ( _id, file) in sa. source_files . iter ( ) {
448
- result. push ( SourceFileData {
449
- path : file. path . to_string_lossy ( ) . to_string ( ) ,
450
- } )
451
- }
452
-
453
- result
454
- }
455
-
456
454
fn find_main_fct_id ( sa : & Sema ) -> Option < FunctionId > {
457
455
let name = sa. interner . intern ( "main" ) ;
458
456
let table = sa. module_table ( sa. program_module_id ( ) ) ;
0 commit comments