type_io.icl 12.7 KB
Newer Older
1
2
3
/*
	module owner: Martijn Vervoort
*/
4
5
implementation module type_io

Martijn Vervoort's avatar
Martijn Vervoort committed
6
import StdEnv, compare_constructor
7
import scanner, general, Heap, typeproperties, utilities, checksupport
8
import trans
9

Martijn Vervoort's avatar
Martijn Vervoort committed
10
import type_io_common
Martijn Vervoort's avatar
Martijn Vervoort committed
11
12
13
14
15
16
// normal form:
// -	type variables in type definitions are normalized by checkTypeDef in the
//		module checktypes.icl. The position of a type variable in the left-hand
//		side of a type constructor is used.
// -	algebraic datatypes; constructors are alphabetically ordered in this 
//		module
17
//
Martijn Vervoort's avatar
Martijn Vervoort committed
18
19
// unsupported:
// - 	ADTs
20

21
22
:: WriteTypeInfoState
	= { 
23
24
25
26
27
28
29
		wtis_n_type_vars						:: !Int
	,	wtis_common_defs						:: !{#CommonDefs}	
	,	wtis_type_defs							:: !.{#{#CheckedTypeDef}}
	,	wtis_collected_conses					:: !ImportedConstructors
	,	wtis_type_heaps							:: !.TypeHeaps
	,	wtis_var_heap							:: !.VarHeap
	,	wtis_main_dcl_module_n 					:: !Int
30
	};
Martijn Vervoort's avatar
Martijn Vervoort committed
31
	
32
33
class WriteTypeInfo a 
where
34
	write_type_info :: a !*File !*WriteTypeInfoState -> (!*File,!*WriteTypeInfoState)
35
36
37
	
instance WriteTypeInfo CommonDefs
where 
38
	write_type_info {com_type_defs,com_cons_defs} tcl_file wtis
39
40
		# (tcl_file,wtis)
			= write_type_info com_type_defs tcl_file wtis
41
42
 		= write_type_info com_cons_defs tcl_file wtis

43
44
instance WriteTypeInfo ConsDef
where 
45
	write_type_info {cons_ident,cons_type,cons_type_index,cons_exi_vars} tcl_file wtis=:{wtis_n_type_vars}
46
47
48
49
50
 		// normalize ...
 		# (th_vars,wtis)
 			= sel_type_var_heap wtis
 		# (_,(_,th_vars))
 			= mapSt normalize_type_var cons_exi_vars (wtis_n_type_vars,th_vars)
51
  		# wtis = { wtis & wtis_type_heaps.th_vars = th_vars }
52
53
 		// ... normalize
		# (tcl_file,wtis)
54
			= write_type_info cons_ident tcl_file wtis
55
		# (tcl_file,wtis)
56
			= write_type_info cons_type tcl_file wtis						
57
58
59
60
61
		# (tcl_file,wtis)
			= write_type_info cons_type_index tcl_file wtis
		# (tcl_file,wtis)
			= write_type_info cons_exi_vars tcl_file wtis
		= (tcl_file,wtis)
62
			
63
instance WriteTypeInfo (TypeDef TypeRhs)
64
where 
65
	write_type_info {td_ident,td_arity,td_args,td_rhs} tcl_file wtis
66
67
68
69
70
		// normalize ...
 		# (th_vars,wtis)
 			= sel_type_var_heap wtis
 		# (_,(n_type_vars,th_vars))
 			= mapSt normalize_type_var td_args (0,th_vars)
71
  		# wtis = { wtis & wtis_type_heaps.th_vars = th_vars, wtis_n_type_vars = n_type_vars }
72
73
 		// ... normalize
 		# (tcl_file,wtis)
74
 			= write_type_info td_ident tcl_file wtis
75
		# (tcl_file,wtis)
76
 			= write_type_info td_arity tcl_file wtis
77
 		# (tcl_file,wtis)
78
 			= write_type_info td_args tcl_file wtis
79
80
81
82
83
84
85
86
87
		# (tcl_file,wtis)
 			= write_type_info td_rhs tcl_file wtis
 		= (tcl_file,wtis)
 	
normalize_type_var :: !ATypeVar (!Int,!*TypeVarHeap) -> (!Int,(!Int,!*TypeVarHeap))
normalize_type_var td_arg=:{atv_variable={tv_info_ptr}} (id,th_vars)
	# th_vars
		= writePtr tv_info_ptr (TVI_Normalized id) th_vars
	= (id,(inc id,th_vars));
88

89
90
91
92
93
sel_type_var_heap :: !*WriteTypeInfoState -> (!*TypeVarHeap,!*WriteTypeInfoState)
sel_type_var_heap wtis=:{wtis_type_heaps}
	# (th_vars,wtis_type_heaps)
		= sel wtis_type_heaps
	= (th_vars,{ wtis & wtis_type_heaps = wtis_type_heaps} )
94
95
96
where
	sel wtis_type_heaps=:{th_vars}
		= (th_vars,{ wtis_type_heaps & th_vars = newHeap } )
97
 
98
99
instance WriteTypeInfo ATypeVar
where 
100
	write_type_info {atv_variable} tcl_file wtis
101
102
103
 		# (tcl_file,wtis)
 			= write_type_info atv_variable tcl_file wtis
 		= (tcl_file,wtis)
104

105
106
instance WriteTypeInfo TypeVar
where
107
108
109
110
111
112
113
114
	write_type_info {tv_info_ptr} tcl_file wtis
		# (th_vars,wtis)
			= sel_type_var_heap wtis
		# ( v,th_vars)
			= readPtr tv_info_ptr th_vars
		# tcl_file
			= fwritei (get_type_var_nf_number v) tcl_file

115
  		# wtis = { wtis & wtis_type_heaps.th_vars = th_vars }
116
117
118
119
 		= (tcl_file,wtis)	
 	where 
 		get_type_var_nf_number (TVI_Normalized i)	= i

120
121
instance WriteTypeInfo TypeRhs
where 
122
123
124
	write_type_info (AlgType defined_symbols) tcl_file wtis
 		# tcl_file
 			= fwritec AlgTypeCode tcl_file
Martijn Vervoort's avatar
Martijn Vervoort committed
125
126
		# defined_symbols
			= (sortBy (\{ds_ident={id_name=id_name1}} {ds_ident={id_name=id_name2}} -> id_name1 < id_name2) defined_symbols)
127
128
129
		# (tcl_file,wtis)
			= write_type_info defined_symbols tcl_file wtis
		= (tcl_file,wtis)
130
		
131
132
	write_type_info (SynType _) tcl_file wtis
		# tcl_file
133
 			= fwritec SynTypeCode tcl_file;
134
  		= (tcl_file,wtis) 
135
		
136
	write_type_info (RecordType {rt_constructor,rt_fields}) tcl_file wtis
137
138
 		#! tcl_file
 			= fwritec RecordTypeCode tcl_file;
139
140
141
142
143
		#! (tcl_file,wtis)
			= write_type_info rt_constructor tcl_file wtis
		#! (tcl_file,wtis)
			= write_type_info rt_fields tcl_file wtis
		= (tcl_file,wtis)
144

145
	write_type_info (AbstractType _) tcl_file wtis
146
147
 		#! tcl_file
 			= fwritec AbstractTypeCode tcl_file;
148
149
150
151
152
 		// unimplemented
		= (tcl_file,wtis)

	write_type_info (AbstractSynType _ _) tcl_file wtis
 		#! tcl_file	= fwritec AbstractTypeCode tcl_file;
153
 		// unimplemented
154
		= (tcl_file,wtis)
155
156
157
		
instance WriteTypeInfo DefinedSymbol 
where
158
159
160
161
162
163
164
165
	write_type_info {ds_ident,ds_arity,ds_index} tcl_file wtis
		# (tcl_file,wtis)
			= write_type_info ds_ident tcl_file wtis
		# (tcl_file,wtis)
			= write_type_info ds_arity tcl_file wtis
		# (tcl_file,wtis)
			= write_type_info ds_index tcl_file wtis
		= (tcl_file,wtis)
166
167
168

instance WriteTypeInfo Ident 
where
169
	write_type_info {id_name} tcl_file wtis
170
171
		# tcl_file
			= fwritei (size id_name) tcl_file
172
		= (fwrites id_name tcl_file,wtis)
173

174
175
instance WriteTypeInfo FieldSymbol
where
176
177
	write_type_info {fs_ident} tcl_file wtis
		= write_type_info fs_ident tcl_file wtis
178

Martijn Vervoort's avatar
Martijn Vervoort committed
179
180
instance WriteTypeInfo SymbolType
where
181
182
183
	write_type_info symbol_type tcl_file wtis
		#! ({st_vars,st_args,st_args_strictness,st_arity,st_result},wtis)
			= expand_symbol_type symbol_type wtis
184
185
186
		# (tcl_file,wtis)
			= write_type_info st_vars tcl_file wtis
		# (tcl_file,wtis)
187
188
189
			= write_type_info st_args tcl_file wtis
		# (tcl_file,wtis)
			= write_type_info st_args_strictness tcl_file wtis
190
191
192
193
194
		# (tcl_file,wtis)
			= write_type_info st_arity tcl_file wtis
		# (tcl_file,wtis)
			= write_type_info st_result tcl_file wtis
		= (tcl_file,wtis)
195
196
	where
		expand_symbol_type symbol_type wtis=:{wtis_common_defs,wtis_type_defs,wtis_main_dcl_module_n,wtis_collected_conses,wtis_type_heaps,wtis_var_heap}
197
198
			# (expanded_symbol_type,wtis_type_defs,wtis_type_heaps,wtis_var_heap)
				= convertSymbolTypeWithoutCollectingImportedConstructors False wtis_common_defs symbol_type wtis_main_dcl_module_n wtis_type_defs wtis_type_heaps wtis_var_heap;
199
200
201
202
203
204
205
206
			# wtis
				= { wtis &
					wtis_type_defs							= wtis_type_defs
				,	wtis_type_heaps							= wtis_type_heaps
				,	wtis_var_heap							= wtis_var_heap
				};
			= (expanded_symbol_type,wtis)
				
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
instance WriteTypeInfo StrictnessList
where
	write_type_info NotStrict tcl_file wtis
		# tcl_file
			= fwritec NotStrictCode tcl_file
		= (tcl_file,wtis)
	write_type_info (Strict i) tcl_file wtis
		# tcl_file
			= fwritec StrictCode tcl_file
		# tcl_file
			= fwritei i tcl_file
		= (tcl_file,wtis)
	write_type_info (StrictList i tail) tcl_file wtis
		# tcl_file
			= fwritec StrictListCode tcl_file
		# tcl_file
			= fwritei i tcl_file
		= write_type_info tail tcl_file wtis
				
Martijn Vervoort's avatar
Martijn Vervoort committed
226
227
instance WriteTypeInfo AType
where
228
	write_type_info {at_type} tcl_file wtis
229
230
231
		# (tcl_file,wtis)
			= write_type_info at_type tcl_file wtis
		= (tcl_file,wtis)
Martijn Vervoort's avatar
Martijn Vervoort committed
232
		
Martijn Vervoort's avatar
Martijn Vervoort committed
233
234
instance WriteTypeInfo Type
where
235
	write_type_info (TA type_symb_ident atypes) tcl_file wtis
Martijn Vervoort's avatar
Martijn Vervoort committed
236
		# tcl_file
237
			= fwritec TypeTASCode tcl_file
238
239
240
241
		# (tcl_file,wtis)
			= write_type_info type_symb_ident tcl_file wtis
		# (tcl_file,wtis)
			= write_type_info atypes tcl_file wtis
242
243
		# (tcl_file,wtis)
			= write_type_info NotStrict tcl_file wtis			
244
		= (tcl_file,wtis)
Martijn Vervoort's avatar
Martijn Vervoort committed
245

246
247
	write_type_info (TAS type_symb_ident atypes strictness) tcl_file wtis
		# tcl_file
248
			= fwritec TypeTASCode tcl_file
249
250
251
		# (tcl_file,wtis)
			= write_type_info type_symb_ident tcl_file wtis
		# (tcl_file,wtis)
252
253
254
			= write_type_info atypes tcl_file wtis
		# (tcl_file,wtis)
			= write_type_info strictness tcl_file wtis			
255
256
		= (tcl_file,wtis)

257
	write_type_info (atype1 --> atype2) tcl_file wtis
Martijn Vervoort's avatar
Martijn Vervoort committed
258
259
		# tcl_file
			= fwritec TypeArrowCode tcl_file
260
261
262
263
264
		# (tcl_file,wtis)
			= write_type_info atype1 tcl_file wtis
		# (tcl_file,wtis)
			= write_type_info atype2 tcl_file wtis
		= (tcl_file,wtis)
Martijn Vervoort's avatar
Martijn Vervoort committed
265
		
266
	write_type_info (cons_variable :@: atypes) tcl_file wtis
Martijn Vervoort's avatar
Martijn Vervoort committed
267
268
		# tcl_file
			= fwritec TypeConsApplyCode tcl_file
269
270
271
272
273
		# (tcl_file,wtis)
			= write_type_info cons_variable tcl_file wtis
		# (tcl_file,wtis)
			= write_type_info atypes tcl_file wtis
		= (tcl_file,wtis)
Martijn Vervoort's avatar
Martijn Vervoort committed
274
		
275
276
	write_type_info tb=:(TB basic_type) tcl_file wtis
		# (tcl_file,wtis)
Martijn Vervoort's avatar
Martijn Vervoort committed
277
			= case basic_type of
278
279
280
281
282
283
284
				BT_Int		-> (fwritec BT_IntCode tcl_file,wtis)
				BT_Char		-> (fwritec BT_CharCode tcl_file,wtis)
				BT_Real		-> (fwritec BT_RealCode tcl_file,wtis)
				BT_Bool		-> (fwritec BT_BoolCode tcl_file,wtis)
				BT_Dynamic	-> (fwritec BT_DynamicCode tcl_file,wtis)
				BT_File		-> (fwritec BT_FileCode tcl_file,wtis)
				BT_World	-> (fwritec BT_WorldCode tcl_file,wtis)
Martijn Vervoort's avatar
Martijn Vervoort committed
285
286
287
				BT_String type
					# tcl_file
						= fwritec BT_StringCode tcl_file
288
289
290
291
					# (tcl_file,wtis)
						= write_type_info type tcl_file wtis
					-> (tcl_file,wtis)
		= (tcl_file,wtis)
Martijn Vervoort's avatar
Martijn Vervoort committed
292
	
293
	write_type_info (GTV type_var) tcl_file wtis
Martijn Vervoort's avatar
Martijn Vervoort committed
294
295
		# tcl_file
			= fwritec TypeGTVCode tcl_file
296
297
298
		# (tcl_file,wtis)
			= write_type_info type_var tcl_file wtis
		= (tcl_file,wtis)
Martijn Vervoort's avatar
Martijn Vervoort committed
299

300
	write_type_info (TV type_var) tcl_file wtis
Martijn Vervoort's avatar
Martijn Vervoort committed
301
302
		# tcl_file
			= fwritec TypeTVCode tcl_file
303
304
305
		# (tcl_file,wtis)
			= write_type_info type_var tcl_file wtis
		= (tcl_file,wtis)
Martijn Vervoort's avatar
Martijn Vervoort committed
306
		
307
	write_type_info (TQV type_var) tcl_file wtis
Martijn Vervoort's avatar
Martijn Vervoort committed
308
309
		# tcl_file
			= fwritec TypeTQVCode tcl_file
310
311
312
		# (tcl_file,wtis)
			= write_type_info type_var tcl_file wtis
		= (tcl_file,wtis)	
Martijn Vervoort's avatar
Martijn Vervoort committed
313

314
315
316
317
318
319
320
321
322
323
324
325
326
327
	// FIXME: the universally quantifier and type vars are ignored here
	// this is really just a hack to prevent the compiler from crashing
	// on rank>1 types
	write_type_info (TFA uni_vars type) tcl_file wtis
 		# (th_vars,wtis)
 			= sel_type_var_heap wtis
 		# (_,(_,th_vars))
 			= mapSt normalize_type_var uni_vars (0,th_vars)
  		# wtis
 			= { wtis & wtis_type_heaps.th_vars = th_vars }
		# (tcl_file,wtis)
			= write_type_info type tcl_file wtis
		= (tcl_file,wtis)	

328
	write_type_info TE tcl_file wtis
Martijn Vervoort's avatar
Martijn Vervoort committed
329
330
		# tcl_file
			= fwritec TypeTECode tcl_file
331
		= (tcl_file,wtis)	
Martijn Vervoort's avatar
Martijn Vervoort committed
332
333
334

instance WriteTypeInfo ConsVariable
where
335
	write_type_info (CV type_var) tcl_file wtis
Martijn Vervoort's avatar
Martijn Vervoort committed
336
337
		# tcl_file
			= fwritec ConsVariableCVCode tcl_file
338
339
340
		# (tcl_file,wtis)
			= write_type_info type_var tcl_file wtis
		= (tcl_file,wtis)	
Martijn Vervoort's avatar
Martijn Vervoort committed
341

342
	write_type_info (TempCV temp_var_id) tcl_file wtis
Martijn Vervoort's avatar
Martijn Vervoort committed
343
344
		# tcl_file
			= fwritec ConsVariableTempCVCode tcl_file
345
346
347
		# (tcl_file,wtis)
			= write_type_info temp_var_id tcl_file wtis
		= (tcl_file,wtis)	
Martijn Vervoort's avatar
Martijn Vervoort committed
348
		
349
	write_type_info (TempQCV temp_var_id) tcl_file wtis
Martijn Vervoort's avatar
Martijn Vervoort committed
350
351
		# tcl_file
			= fwritec ConsVariableTempQCVCode tcl_file
352
353
354
		# (tcl_file,wtis)
			= write_type_info temp_var_id tcl_file wtis
		= (tcl_file,wtis)	
Martijn Vervoort's avatar
Martijn Vervoort committed
355
356
357

instance WriteTypeInfo TypeSymbIdent
where
358
359
	write_type_info tsi=:{type_ident,type_arity,type_index={glob_module,glob_object}} tcl_file wtis
		# is_type_without_definition = glob_module == cPredefinedModuleIndex
Martijn Vervoort's avatar
Martijn Vervoort committed
360
361
		# tcl_file
			= fwritec (if is_type_without_definition TypeSymbIdentWithoutDefinition TypeSymbIdentWithDefinition) tcl_file
362
		# (tcl_file,wtis)
363
			= write_type_info type_ident tcl_file wtis
364
		# (tcl_file,wtis)		 
365
			= write_type_info type_arity tcl_file wtis
Martijn Vervoort's avatar
Martijn Vervoort committed
366
367
368
		# (tcl_file,wtis)
			= write_type_info tsi.type_index tcl_file wtis
		= (tcl_file,wtis)
369

Martijn Vervoort's avatar
Martijn Vervoort committed
370
371
372
373
374
375
376
instance WriteTypeInfo (Global object) | WriteTypeInfo object
where
	write_type_info {glob_object,glob_module} tcl_file wtis
		# (tcl_file,wtis)
			= write_type_info glob_object tcl_file wtis
		# (tcl_file,wtis)
			= write_type_info glob_module tcl_file wtis
377
		= (tcl_file,wtis)
378
 
379
380
381
// basic and structural write_type_info's
instance WriteTypeInfo Int 
where
382
383
	write_type_info i tcl_file wtis
		= (fwritei i tcl_file,wtis)
384

385
instance WriteTypeInfo {#b} | Array {#} b & WriteTypeInfo b
386
where
387
	write_type_info unboxed_array tcl_file wtis
388
389
390
		# s_unboxed_array
			= size unboxed_array
		# tcl_file
391
			= fwritei s_unboxed_array tcl_file			
392
		= write_type_info_loop 0 s_unboxed_array tcl_file wtis
393
	where 
394

395
		write_type_info_loop i limit tcl_file wtis
396
			| i == limit
397
398
399
400
				= (tcl_file,wtis)
			# (tcl_file,wtis)
				= write_type_info unboxed_array.[i] tcl_file wtis
			= write_type_info_loop (inc i) limit tcl_file wtis
401
402
403
			
instance WriteTypeInfo [a] | WriteTypeInfo a
where
404
	write_type_info l tcl_file wtis
405
406
		# tcl_file
			= fwritei (length l) tcl_file
407
		= write_type_info_loop l tcl_file wtis
408
	where
409
410
411
412
413
414
		write_type_info_loop []	tcl_file wtis
			= (tcl_file,wtis)
		write_type_info_loop [x:xs] tcl_file wtis
			# (tcl_file,wtis)
				= write_type_info x tcl_file wtis
			= write_type_info_loop xs tcl_file wtis
415
416
417
			
instance WriteTypeInfo Char
where
418
	write_type_info c tcl_file wtis
419
420
		# tcl_file
			= fwritec c tcl_file;
421
		= (tcl_file,wtis);
Martijn Vervoort's avatar
Martijn Vervoort committed
422

423
424
425
426
427
428
429
430
instance WriteTypeInfo (a,b) | WriteTypeInfo a & WriteTypeInfo b
where
	write_type_info (c1,c2) tcl_file wtis
		# (tcl_file,wtis)
			= write_type_info c1 tcl_file wtis
		# (tcl_file,wtis)
			= write_type_info c2 tcl_file wtis
		= (tcl_file,wtis)