checksupport.icl 31.9 KB
Newer Older
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1
2
3
4
implementation module checksupport

import StdEnv, compare_constructor
import syntax, predef
Sjaak Smetsers's avatar
Sjaak Smetsers committed
5
import utilities
6
from check import checkFunctions
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
7
8
9

::	VarHeap :== Heap VarInfo

10
//cIclModIndex 			:== 0
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
11
12
13
14
15
16
17

CS_NotChecked 	:== -1
NotFound		:== -1
	
cModuleScope	:== 0
cGlobalScope	:== 1

Sjaak Smetsers's avatar
Sjaak Smetsers committed
18
19
cIsNotADclModule 	:== False
cIsADclModule 		:== True
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
20

21
22
23
24
25
26
// MW..
cNeedStdArray	:== 1
cNeedStdEnum	:== 2
cNeedStdDynamics:== 4
// ..MW

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
27
28
29
30
31
32
33
34
::	Heaps =
	{	hp_var_heap			::!.VarHeap
	,	hp_expression_heap	::!.ExpressionHeap
	,	hp_type_heaps		::!.TypeHeaps
	}

::	ErrorAdmin = { ea_file :: !.File, ea_loc :: ![IdentPos], ea_ok :: !Bool }

35
36
37
::	CheckState = { cs_symbol_table :: !.SymbolTable, cs_predef_symbols :: !.PredefinedSymbols, cs_error :: !.ErrorAdmin, cs_x :: !CheckStateX }

::	CheckStateX = {x_needed_modules :: !BITVECT,x_main_dcl_module_n :: !Int }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

::	ConversionTable		:== {# .{# Int }}

cTypeDefs				:== 0
cConstructorDefs		:== 1
cSelectorDefs			:== 2
cClassDefs				:== 3
cMemberDefs				:== 4
cInstanceDefs			:== 5
cFunctionDefs			:== 6
cMacroDefs				:== 7

cConversionTableSize	:== 8

instance toInt STE_Kind
where
	toInt STE_Type					= cTypeDefs
	toInt STE_Constructor			= cConstructorDefs
	toInt (STE_Field _)				= cSelectorDefs
	toInt STE_Class					= cClassDefs
	toInt STE_Member				= cMemberDefs
	toInt STE_Instance				= cInstanceDefs
	toInt STE_DclFunction			= cFunctionDefs
	toInt (STE_FunctionOrMacro _)	= cMacroDefs
	toInt _							= NoIndex

::	CommonDefs =
	{	com_type_defs 		:: !.{# CheckedTypeDef}
	,	com_cons_defs		:: !.{# ConsDef}
	,	com_selector_defs	:: !.{# SelectorDef}
	,	com_class_defs		:: !.{# ClassDef}
	,	com_member_defs		:: !.{# MemberDef}
	,	com_instance_defs	:: !.{# ClassInstance}
	}

::	Declaration =
	{	dcl_ident	:: !Ident
75
	,	dcl_pos		:: !Position
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
76
77
78
79
	,	dcl_kind	:: !STE_Kind
	,	dcl_index	:: !Index
	}

80
81
::	Declarations = {
		dcls_import		::!{!Declaration}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
82
	,	dcls_local		::![Declaration]
83
84
	,	dcls_local_for_import ::!{!Declaration}
	,	dcls_explicit	::!{!ExplicitImport}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
85
	}
86
87

:: ExplicitImport = ExplicitImport !Declaration !LineNr;
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
88
89
90
91
92
93
94
	
::	IclModule  =
	{	icl_name			:: !Ident
	,	icl_functions		:: !.{# FunDef }
	,	icl_instances		:: !IndexRange
	,	icl_specials		:: !IndexRange
	,	icl_common			:: !.CommonDefs
95
96
//	,	icl_declared		:: !Declarations
	,	icl_import		:: !{!Declaration}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
97
	,	icl_imported_objects	:: ![ImportedObject]
98
	,	icl_used_module_numbers :: !ModuleNumberSet
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
99
100
101
102
103
104
105
106
107
108
	}

::	DclModule =
	{	dcl_name			:: !Ident
	,	dcl_functions		:: !{# FunType }
	,	dcl_instances		:: !IndexRange
	,	dcl_macros			:: !IndexRange
	,	dcl_class_specials	:: !IndexRange
	,	dcl_specials		:: !IndexRange
	,	dcl_common			:: !CommonDefs
109
	,	dcl_sizes			:: !{# Int}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
110
111
112
	,	dcl_declared		:: !Declarations
	,	dcl_conversions		:: !Optional ConversionTable
	,	dcl_is_system		:: !Bool
113
	,	dcl_imported_module_numbers :: !ModuleNumberSet
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
114
115
	}

116
117
118
119
120
121
122
123
124
125
:: ModuleNumberSet = ModuleNumbers !Int !ModuleNumberSet | EndModuleNumbers;

in_module_number_set :: !Int !ModuleNumberSet -> Bool
in_module_number_set n EndModuleNumbers
	= False;
in_module_number_set n (ModuleNumbers module_numbers rest_module_numbers)
	| n<32
		= (module_numbers bitand (1<<n))<>0
		= in_module_number_set (n-32) rest_module_numbers

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
class Erroradmin state // PK...
where
	pushErrorAdmin :: !IdentPos *state -> *state
	setErrorAdmin :: !IdentPos *state -> *state
	popErrorAdmin  :: *state -> *state

instance Erroradmin ErrorAdmin
where
	pushErrorAdmin pos error=:{ea_loc}
		= { error & ea_loc = [pos : ea_loc] } 
	
	setErrorAdmin pos error
		= { error & ea_loc = [pos] } 
	
	popErrorAdmin error=:{ea_loc = [_:ea_locs]}
		=  { error & ea_loc = ea_locs }

instance Erroradmin CheckState
where
	pushErrorAdmin pos cs=:{cs_error}
		= {cs & cs_error = pushErrorAdmin pos cs_error } 
	
	setErrorAdmin pos cs=:{cs_error}
		= {cs & cs_error = setErrorAdmin pos cs_error }
	
	popErrorAdmin cs=:{cs_error}
		= {cs & cs_error = popErrorAdmin cs_error } //...PK

newPosition :: !Ident  !Position -> IdentPos 
newPosition id (FunPos file_name line_nr _)
	= { ip_ident = id, ip_line = line_nr, ip_file = file_name }
newPosition id (LinePos file_name line_nr)
	= { ip_ident = id, ip_line = line_nr, ip_file = file_name }
newPosition id (PreDefPos file_name)
	= { ip_ident = id, ip_line = cNotALineNumber, ip_file = file_name.id_name }
newPosition id NoPos
	= { ip_ident = id, ip_line = cNotALineNumber, ip_file = "???" }

checkError :: !a !b !*ErrorAdmin -> *ErrorAdmin | <<< a & <<< b // PK
checkError id mess error=:{ea_file,ea_loc=[]}
	= { error & ea_file = ea_file <<< "Check Error " <<< "\"" <<< id <<< "\" " <<< mess <<< '\n', ea_ok = False }
checkError id mess error=:{ea_file,ea_loc}
	= { error & ea_file = ea_file <<< "Check Error " <<< hd ea_loc <<< ":\"" <<< id  <<< "\" " <<< mess <<< '\n', ea_ok = False }

checkWarning :: !a !b !*ErrorAdmin -> *ErrorAdmin | <<< a & <<< b // PK
checkWarning id mess error=:{ea_file,ea_loc=[]}
	= { error & ea_file = ea_file <<< "Check Warning " <<< "\"" <<< id <<< "\" " <<< mess <<< '\n' }
checkWarning id mess error=:{ea_file,ea_loc}
	= { error & ea_file = ea_file <<< "Check Warning " <<< hd ea_loc <<< ":\"" <<< id  <<< "\" " <<< mess <<< '\n' }

clean's avatar
clean committed
176

Martin Wierich's avatar
Martin Wierich committed
177
checkErrorWithIdentPos :: !IdentPos !a !*ErrorAdmin -> .ErrorAdmin | <<< a;
Martin Wierich's avatar
Martin Wierich committed
178
179
checkErrorWithIdentPos ident_pos mess error=:{ea_file}
	= { error & ea_file = ea_file <<< "Check Error " <<< ident_pos <<< ":" <<< mess <<< '\n', ea_ok = False }
Martin Wierich's avatar
Martin Wierich committed
180

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
class envLookUp a :: !a !(Env Ident .b) -> (!Bool,.b)

instance envLookUp TypeVar
where
	envLookUp var [bind:binds]
		| var.tv_name == bind.bind_src
			= (True, bind.bind_dst)
			= envLookUp var binds
	envLookUp var []
		= (False, abort "illegal value")
	
instance envLookUp AttributeVar
where
	envLookUp var [bind:binds]
		| var.av_name == bind.bind_src
			= (True, bind.bind_dst)
			= envLookUp var binds
	envLookUp var []
		= (False, abort "illegal value")

instance envLookUp ATypeVar
where
	envLookUp var=:{atv_variable} [bind:binds]
		| atv_variable.tv_name == bind.bind_src
			= (True, bind.bind_dst)
			= envLookUp var binds
	envLookUp var []
		= (False, abort "illegal value")

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244

::	ExpressionInfo =
	{	ef_type_defs		:: !.{# CheckedTypeDef}
	,	ef_selector_defs	:: !.{# SelectorDef}
	,	ef_cons_defs		:: !.{# ConsDef}
	,	ef_member_defs		:: !.{# MemberDef}
	,	ef_class_defs		:: !.{# ClassDef}
	,	ef_modules			:: !.{# DclModule}
	,	ef_is_macro_fun		:: !Bool
	}

checkLocalFunctions :: !Index !Level !LocalDefs !*{#FunDef} !*ExpressionInfo !*Heaps !*CheckState
					-> (!.{#FunDef},!.ExpressionInfo,!.Heaps,!.CheckState);
checkLocalFunctions mod_index level (CollectedLocalDefs {loc_functions={ir_from,ir_to}}) fun_defs e_info heaps cs
	= checkFunctions mod_index level ir_from ir_to fun_defs e_info heaps cs


convertIndex :: !Index !Index !(Optional ConversionTable) -> !Index
convertIndex index table_index (Yes tables)
	= tables.[table_index].[index]
convertIndex index table_index No
	= index



retrieveGlobalDefinition :: !SymbolTableEntry !STE_Kind !Index -> (!Index, !Index)
retrieveGlobalDefinition {ste_kind = STE_Imported kind dcl_index, ste_def_level, ste_index} requ_kind mod_index
	| kind == requ_kind
		= (ste_index, dcl_index)
		= (NotFound, mod_index)
retrieveGlobalDefinition {ste_kind,ste_def_level,ste_index} requ_kind mod_index
	| ste_kind == requ_kind && ste_def_level == cGlobalScope
		= (ste_index, mod_index)
		= (NotFound, mod_index)

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
245
retrieveAndRemoveImportsFromSymbolTable :: ![(.a,.Declarations)] [Declaration] *(Heap SymbolTableEntry) -> ([Declaration],.Heap SymbolTableEntry);
246
247
248
retrieveAndRemoveImportsFromSymbolTable [(_, {dcls_import,dcls_local,dcls_local_for_import}) : imports] all_decls symbol_table
//	# (all_decls, symbol_table) = retrieveAndRemoveImportsOfModuleFromSymbolTable dcls_import dcls_local all_decls symbol_table
	# (all_decls, symbol_table) = retrieveAndRemoveImportsOfModuleFromSymbolTable2 dcls_import dcls_local_for_import all_decls symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
249
250
251
	= retrieveAndRemoveImportsFromSymbolTable imports all_decls symbol_table
retrieveAndRemoveImportsFromSymbolTable []  all_decls symbol_table
	= (all_decls, symbol_table)
252
253
254
255
256
257
258

retrieveAndRemoveImportsOfModuleFromSymbolTable2 :: !{!.Declaration} !{!.Declaration} ![.Declaration] !*(Heap SymbolTableEntry) -> ([Declaration],.Heap SymbolTableEntry);
retrieveAndRemoveImportsOfModuleFromSymbolTable2 imports locals_for_import all_decls symbol_table
	# (all_decls, symbol_table) = retrieve_declared_symbols_in_array 0 imports all_decls symbol_table
	= retrieve_declared_symbols_in_array 0 locals_for_import all_decls symbol_table

retrieveAndRemoveImportsOfModuleFromSymbolTable :: !{!.Declaration} ![.Declaration] ![.Declaration] !*(Heap SymbolTableEntry) -> ([Declaration],.Heap SymbolTableEntry);
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
259
retrieveAndRemoveImportsOfModuleFromSymbolTable imports locals all_decls symbol_table
260
	# (all_decls, symbol_table) = retrieve_declared_symbols_in_array 0 imports all_decls symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
261
262
	= retrieve_declared_symbols locals all_decls symbol_table
where
clean's avatar
clean committed
263
	retrieve_declared_symbols :: ![Declaration] ![Declaration] !*SymbolTable -> (![Declaration], !*SymbolTable)
264
	retrieve_declared_symbols [declaration=:{dcl_ident=ident=:{id_info},dcl_kind,dcl_index}:symbols] decls symbol_table
clean's avatar
clean committed
265
266
267
268
269
270
271
272
273
274
		#! entry = sreadPtr id_info symbol_table
		# {ste_kind,ste_def_level} = entry
		| ste_kind == STE_Empty || ste_def_level > cModuleScope
			= retrieve_declared_symbols symbols decls symbol_table
			# symbol_table = symbol_table <:= (id_info, entry.ste_previous)
			= case ste_kind of
				STE_Field selector_id
					| case dcl_kind of
							STE_Field f -> f==selector_id
							_ -> False
275
276
277
						-> retrieve_declared_symbols symbols [declaration : decls ] (removeFieldFromSelectorDefinition selector_id NoIndex dcl_index symbol_table)
						#! declaration = { declaration & dcl_kind = ste_kind }
						-> retrieve_declared_symbols symbols [declaration : decls ] (removeFieldFromSelectorDefinition selector_id NoIndex dcl_index symbol_table)
clean's avatar
clean committed
278
279
280
281
				STE_Imported (STE_Field selector_id) def_mod
					| case dcl_kind of
							STE_Imported (STE_Field f) d -> d==def_mod && f==selector_id
							_ -> False
282
283
284
						-> retrieve_declared_symbols symbols [declaration : decls ] (removeFieldFromSelectorDefinition selector_id def_mod dcl_index symbol_table)
						#! declaration = { declaration & dcl_kind = ste_kind }
						-> retrieve_declared_symbols symbols [declaration : decls ] (removeFieldFromSelectorDefinition selector_id def_mod dcl_index symbol_table)
clean's avatar
clean committed
285
286
				_
					| same_STE_Kind ste_kind dcl_kind
287
288
289
						-> retrieve_declared_symbols symbols [declaration : decls ] symbol_table					
						#! declaration = { declaration & dcl_kind = ste_kind }
						-> retrieve_declared_symbols symbols [declaration : decls ] symbol_table
clean's avatar
clean committed
290
291
	retrieve_declared_symbols [] decls symbol_table
		= (decls, symbol_table)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
292

293
294
295
296
297
retrieve_declared_symbols_in_array :: !Int !{!Declaration} ![Declaration] !*SymbolTable -> (![Declaration], !*SymbolTable)
retrieve_declared_symbols_in_array symbol_index symbols decls symbol_table
	| symbol_index<size symbols
		#! (declaration,symbols) = symbols![symbol_index]
		# {dcl_ident=ident=:{id_info},dcl_kind}=declaration
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
298
		#! entry = sreadPtr id_info symbol_table
clean's avatar
clean committed
299
		# {ste_kind,ste_def_level} = entry
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
300
		| ste_kind == STE_Empty || ste_def_level > cModuleScope
301
302
			= retrieve_declared_symbols_in_array (symbol_index+1) symbols decls symbol_table
			# symbol_table = symbol_table <:= (id_info, entry.ste_previous)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
303
304
			= case ste_kind of
				STE_Field selector_id
305
306
307
308
309
310
311
312
313
314
					| case dcl_kind of
							STE_Field f -> f==selector_id
							_ -> False
						#! (declaration,symbols) = symbols![symbol_index]
						#! dcl_index = symbols.[symbol_index].dcl_index
						-> retrieve_declared_symbols_in_array (symbol_index+1) symbols [declaration : decls ] (removeFieldFromSelectorDefinition selector_id NoIndex dcl_index symbol_table)
						#! (declaration,symbols) = symbols![symbol_index]
						#! dcl_index = declaration.dcl_index
						#! declaration = { declaration & dcl_kind = ste_kind }
						-> retrieve_declared_symbols_in_array (symbol_index+1) symbols [declaration : decls ] (removeFieldFromSelectorDefinition selector_id NoIndex dcl_index symbol_table)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
315
				STE_Imported (STE_Field selector_id) def_mod
316
317
318
319
320
321
322
323
324
325
					| case dcl_kind of
							STE_Imported (STE_Field f) d -> d==def_mod && f==selector_id
							_ -> False
						#! (declaration,symbols) = symbols![symbol_index]
						#! dcl_index = symbols.[symbol_index].dcl_index
						-> retrieve_declared_symbols_in_array (symbol_index+1) symbols [declaration : decls ] (removeFieldFromSelectorDefinition selector_id def_mod dcl_index symbol_table)
						#! (declaration,symbols) = symbols![symbol_index]
						#! dcl_index = declaration.dcl_index
						#! declaration = { declaration & dcl_kind = ste_kind }
						-> retrieve_declared_symbols_in_array (symbol_index+1) symbols [declaration : decls ] (removeFieldFromSelectorDefinition selector_id def_mod dcl_index symbol_table)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
326
				_
327
328
329
330
331
332
333
					| same_STE_Kind ste_kind dcl_kind
						#! (declaration,symbols) = symbols![symbol_index]
						-> retrieve_declared_symbols_in_array (symbol_index+1) symbols [declaration : decls ] symbol_table					
						#! (declaration,symbols) = symbols![symbol_index]
						#! declaration = { declaration & dcl_kind = ste_kind }
						-> retrieve_declared_symbols_in_array (symbol_index+1) symbols [declaration : decls ] symbol_table
		= (decls, symbol_table)
clean's avatar
clean committed
334
335
336

same_STE_Kind (STE_Imported s1 i1) (STE_Imported s2 i2) = i1==i2 && same_STE_Kind s1 s2
same_STE_Kind STE_DclFunction STE_DclFunction = True
337
same_STE_Kind (STE_FunctionOrMacro []) (STE_FunctionOrMacro []) = True
clean's avatar
clean committed
338
same_STE_Kind STE_Type STE_Type = True
339
340
same_STE_Kind STE_Constructor STE_Constructor = True
same_STE_Kind (STE_Field f1) (STE_Field f2) = f1==f2
clean's avatar
clean committed
341
342
343
344
same_STE_Kind STE_Instance STE_Instance = True
same_STE_Kind STE_Member STE_Member = True
same_STE_Kind STE_Class STE_Class = True
same_STE_Kind _ _ = False
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
345

346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
removeImportsAndLocalsOfModuleFromSymbolTable :: !Declarations !*(Heap SymbolTableEntry) -> .Heap SymbolTableEntry
removeImportsAndLocalsOfModuleFromSymbolTable {dcls_import,dcls_local} symbol_table
	# symbol_table = remove_declared_symbols_in_array 0 dcls_import symbol_table
	= remove_declared_symbols dcls_local symbol_table
where
	remove_declared_symbols :: ![Declaration] !*SymbolTable -> !*SymbolTable
	remove_declared_symbols [symbol=:{dcl_ident={id_info},dcl_index}:symbols] symbol_table
		#! entry = sreadPtr id_info symbol_table
		# {ste_kind,ste_def_level} = entry
		| ste_kind == STE_Empty || ste_def_level > cModuleScope
			= remove_declared_symbols symbols symbol_table
			# symbol_table = symbol_table <:= (id_info, entry.ste_previous)
			= case ste_kind of
				STE_Field selector_id
					-> remove_declared_symbols symbols (removeFieldFromSelectorDefinition selector_id NoIndex dcl_index symbol_table)
				STE_Imported (STE_Field selector_id) def_mod
					-> remove_declared_symbols symbols (removeFieldFromSelectorDefinition selector_id def_mod dcl_index symbol_table)
				_
					-> remove_declared_symbols symbols symbol_table					
	remove_declared_symbols [] symbol_table
		= symbol_table

	remove_declared_symbols_in_array :: !Int !{!Declaration} !*SymbolTable -> !*SymbolTable
	remove_declared_symbols_in_array symbol_index symbols symbol_table
		| symbol_index<size symbols
			#! (symbol,symbols) = symbols![symbol_index]
			# {dcl_ident={id_info}}=symbol
			#! entry = sreadPtr id_info symbol_table
			# {ste_kind,ste_def_level} = entry
			| ste_kind == STE_Empty || ste_def_level > cModuleScope
				= remove_declared_symbols_in_array (symbol_index+1) symbols symbol_table
				# symbol_table = symbol_table <:= (id_info, entry.ste_previous)
				= case ste_kind of
					STE_Field selector_id
						#! dcl_index = symbols.[symbol_index].dcl_index
						-> remove_declared_symbols_in_array (symbol_index+1) symbols (removeFieldFromSelectorDefinition selector_id NoIndex dcl_index symbol_table)
					STE_Imported (STE_Field selector_id) def_mod
						#! dcl_index = symbols.[symbol_index].dcl_index
						-> remove_declared_symbols_in_array (symbol_index+1) symbols (removeFieldFromSelectorDefinition selector_id def_mod dcl_index symbol_table)
					_
						-> remove_declared_symbols_in_array (symbol_index+1) symbols symbol_table
			= symbol_table

389
addLocalFunctionDefsToSymbolTable :: !Level !Index !Index !u:{#FunDef} !*SymbolTable !*ErrorAdmin -> (!u:{# FunDef}, !*SymbolTable, !*ErrorAdmin)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
390
391
392
addLocalFunctionDefsToSymbolTable level from_index to_index fun_defs symbol_table error
	| from_index == to_index
		= (fun_defs, symbol_table, error)	
393
394
		# (fun_def, fun_defs) = fun_defs![from_index]
		  (symbol_table, error) = addDefToSymbolTable level from_index fun_def.fun_symb (STE_FunctionOrMacro []) symbol_table error
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
395
396
397
398
399
400
401
402
403
404
405
406
407
408
		= addLocalFunctionDefsToSymbolTable level (inc from_index) to_index fun_defs symbol_table error

NewEntry symbol_table symb_ptr def_kind def_index level previous :==
	 symbol_table <:= (symb_ptr,{  ste_kind = def_kind, ste_index = def_index, ste_def_level = level, ste_previous = previous })
	
		
addDefToSymbolTable :: !Level !Index !Ident !STE_Kind !*SymbolTable !*ErrorAdmin -> (!* SymbolTable, !*ErrorAdmin)
addDefToSymbolTable level def_index def_ident=:{id_info} def_kind symbol_table error
	#! entry = sreadPtr id_info symbol_table
	| entry.ste_kind == STE_Empty || entry.ste_def_level <> level
		# entry = {ste_index = def_index, ste_kind = def_kind, ste_def_level = level, ste_previous = entry }
		= (symbol_table <:= (id_info,entry), error)
		= (symbol_table, checkError def_ident " already defined" error)

409
410
411
412
413
414
addDeclaredSymbolsToSymbolTable2 :: .Bool .Int !{!.Declaration} !{!.Declaration} !*CheckState -> .CheckState;
addDeclaredSymbolsToSymbolTable2 is_dcl_mod ste_index locals imported cs
	# cs=add_imports_in_array_to_symbol_table 0 is_dcl_mod imported cs
	= addLocalSymbolsForImportToSymbolTable 0 locals ste_index cs

addDeclaredSymbolsToSymbolTable :: .Bool .Int ![.Declaration] !{!.Declaration} !*CheckState -> .CheckState;
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
415
addDeclaredSymbolsToSymbolTable is_dcl_mod ste_index locals imported cs
416
417
	# cs=add_imports_in_array_to_symbol_table 0 is_dcl_mod imported cs
	= addLocalSymbolsToSymbolTable locals ste_index cs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
418
where
419
	add_imports_to_symbol_table is_dcl_mod [{dcl_ident,dcl_pos,dcl_kind,dcl_index} : symbols] cs=:{cs_x}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
420
421
		= case dcl_kind of
			STE_Imported def_kind def_mod
422
423
				| is_dcl_mod || def_mod <> cs_x.x_main_dcl_module_n
	//				-> add_imports_to_symbol_table is_dcl_mod symbols (addImportedSymbol dcl_ident dcl_pos def_kind dcl_index def_mod cs)
clean's avatar
clean committed
424
					-> add_imports_to_symbol_table is_dcl_mod symbols (addIndirectlyImportedSymbol dcl_ident dcl_pos dcl_kind def_kind dcl_index def_mod cs)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
425
426
427
428
429
430
					-> add_imports_to_symbol_table is_dcl_mod symbols cs
			STE_FunctionOrMacro _
				-> add_imports_to_symbol_table is_dcl_mod symbols (addImportedFunctionOrMacro dcl_ident dcl_index cs)
	add_imports_to_symbol_table is_dcl_mod [] cs
		= cs

431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
add_imports_in_array_to_symbol_table symbol_index is_dcl_mod symbols cs=:{cs_x}
	| symbol_index<size symbols
		#! ({dcl_ident,dcl_pos,dcl_kind},symbols) = symbols![symbol_index]
		= case dcl_kind of
			STE_Imported def_kind def_mod
//					| is_dcl_mod || def_mod <> cIclModIndex
				| is_dcl_mod || def_mod <> cs_x.x_main_dcl_module_n
//					-> add_imports_in_array_to_symbol_table (symbol_index+1) is_dcl_mod symbols (addImportedSymbol dcl_ident dcl_pos def_kind dcl_index def_mod cs)
					#! dcl_index= symbols.[symbol_index].dcl_index
					-> add_imports_in_array_to_symbol_table (symbol_index+1) is_dcl_mod symbols (addIndirectlyImportedSymbol dcl_ident dcl_pos dcl_kind def_kind dcl_index def_mod cs)
					-> add_imports_in_array_to_symbol_table (symbol_index+1) is_dcl_mod symbols cs
			STE_FunctionOrMacro _
					#! dcl_index= symbols.[symbol_index].dcl_index
				-> add_imports_in_array_to_symbol_table (symbol_index+1) is_dcl_mod symbols (addImportedFunctionOrMacro dcl_ident dcl_index cs)
		= cs

addLocalSymbolsForImportToSymbolTable :: !Int !{!.Declaration} Int !*CheckState -> .CheckState;
addLocalSymbolsForImportToSymbolTable symbol_index symbols mod_index cs
	| symbol_index<size symbols
		# ({dcl_ident,dcl_pos,dcl_kind,dcl_index},symbols) = symbols![symbol_index]
		= case dcl_kind of
			STE_FunctionOrMacro _
				-> addLocalSymbolsForImportToSymbolTable (symbol_index+1) symbols mod_index (addImportedFunctionOrMacro dcl_ident dcl_index cs)
			STE_Imported def_kind def_mod
				-> addLocalSymbolsForImportToSymbolTable (symbol_index+1) symbols mod_index (addIndirectlyImportedSymbol dcl_ident dcl_pos dcl_kind def_kind dcl_index mod_index cs)
		= cs

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
458
addLocalSymbolsToSymbolTable :: ![.Declaration] Int !*CheckState -> .CheckState;
Martin Wierich's avatar
Martin Wierich committed
459
addLocalSymbolsToSymbolTable [{dcl_ident,dcl_pos,dcl_kind,dcl_index} : symbols] mod_index cs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
460
461
462
463
	= case dcl_kind of
		STE_FunctionOrMacro _
			-> addLocalSymbolsToSymbolTable symbols mod_index (addImportedFunctionOrMacro dcl_ident dcl_index cs)
		_
464
			-> addLocalSymbolsToSymbolTable symbols mod_index (addImportedSymbol dcl_ident dcl_pos dcl_kind dcl_index mod_index cs)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
addLocalSymbolsToSymbolTable [] mod_index cs
	= cs

addImportedFunctionOrMacro :: !Ident .Int !*CheckState -> .CheckState;
addImportedFunctionOrMacro ident=:{id_info} def_index cs=:{cs_symbol_table}
	#! entry = sreadPtr id_info cs_symbol_table
	= case entry.ste_kind of
		STE_Empty
			-> { cs & cs_symbol_table = NewEntry cs.cs_symbol_table id_info (STE_FunctionOrMacro []) def_index cModuleScope entry}
		STE_FunctionOrMacro _
			| entry.ste_index == def_index
				-> cs
		_
			-> { cs & cs_error = checkError ident " multiply imported" cs.cs_error}

addFieldToSelectorDefinition :: !Ident (Global .Int) !*CheckState -> .CheckState;
addFieldToSelectorDefinition {id_info} glob_field_index cs=:{cs_symbol_table}
	# (entry, cs_symbol_table) = readPtr id_info cs_symbol_table 
	  cs = { cs & cs_symbol_table = cs_symbol_table }
	= case entry.ste_kind of
		STE_Selector selector_list
			-> { cs & cs_symbol_table = cs.cs_symbol_table <:= (id_info, { entry & ste_kind = STE_Selector [ glob_field_index : selector_list ] })}
		_
			-> { cs & cs_symbol_table = NewEntry cs.cs_symbol_table id_info (STE_Selector [glob_field_index]) NoIndex cModuleScope entry }
489

Martin Wierich's avatar
Martin Wierich committed
490
491
addImportedSymbol :: !Ident !Position !STE_Kind !.Int !.Int !*CheckState -> .CheckState;
addImportedSymbol ident pos def_kind def_index def_mod cs=:{cs_symbol_table}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
492
	# (entry, cs_symbol_table) = readPtr ident.id_info cs_symbol_table 
Martin Wierich's avatar
Martin Wierich committed
493
	= add_imported_symbol entry ident pos def_kind def_index def_mod { cs & cs_symbol_table = cs_symbol_table }
clean's avatar
clean committed
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
	where
		add_imported_symbol /*entry=:*/{ste_kind = STE_Empty} {id_name,id_info} _ def_kind def_index def_mod cs=:{cs_symbol_table}
			// JVG: read the entry again, because it is boxed
			# (entry, cs_symbol_table) = readPtr id_info cs_symbol_table
			# cs = { cs & cs_symbol_table = NewEntry cs_symbol_table id_info (STE_Imported def_kind def_mod) def_index cModuleScope entry}
			= case def_kind of
				STE_Field selector_id
					-> addFieldToSelectorDefinition selector_id	{ glob_module = def_mod, glob_object = def_index } cs
				_
					-> cs
		add_imported_symbol	/*entry=:*/{ste_kind = STE_Imported kind mod_index, ste_index} ident=:{id_info} _ def_kind def_index def_mod cs
			| kind == def_kind && mod_index == def_mod && ste_index == def_index
				= cs
		add_imported_symbol	entry ident pos def_kind def_index def_mod cs=:{cs_error}
			= { cs & cs_error = checkErrorWithIdentPos (newPosition ident pos) " multiply imported" cs_error}

// same as addImportedSymbol but does not create a new STE_Imported
addIndirectlyImportedSymbol :: !Ident !Position !STE_Kind !STE_Kind !.Int !.Int !*CheckState -> .CheckState;
addIndirectlyImportedSymbol ident pos dcl_kind def_kind def_index def_mod cs=:{cs_symbol_table}
	# (entry, cs_symbol_table) = readPtr ident.id_info cs_symbol_table
	= add_indirectly_imported_symbol entry ident pos def_kind def_index def_mod { cs & cs_symbol_table = cs_symbol_table }
	where
		add_indirectly_imported_symbol /*entry=:*/{ste_kind = STE_Empty} {id_name,id_info} _ def_kind def_index def_mod cs=:{cs_symbol_table}
			// JVG: read the entry again, because it is boxed
			# (entry, cs_symbol_table) = readPtr id_info cs_symbol_table
			# cs = { cs & cs_symbol_table = NewEntry cs_symbol_table id_info dcl_kind def_index cModuleScope entry}
			= case def_kind of
				STE_Field selector_id
					-> addFieldToSelectorDefinition selector_id	{ glob_module = def_mod, glob_object = def_index } cs
				_
					-> cs
		add_indirectly_imported_symbol	/*entry=:*/{ste_kind = STE_Imported kind mod_index, ste_index} ident=:{id_info} _ def_kind def_index def_mod cs
			| kind == def_kind && mod_index == def_mod && ste_index == def_index
				= cs
		add_indirectly_imported_symbol	entry ident pos def_kind def_index def_mod cs=:{cs_error}
			= { cs & cs_error = checkErrorWithIdentPos (newPosition ident pos) " multiply imported" cs_error}
Martin Wierich's avatar
Martin Wierich committed
530

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
531
532
533
534
addGlobalDefinitionsToSymbolTable :: ![.Declaration] !*CheckState -> .CheckState;
addGlobalDefinitionsToSymbolTable decls cs
	= foldSt add_global_definition decls cs
where
Martin Wierich's avatar
Martin Wierich committed
535
	add_global_definition {dcl_ident=ident=:{id_info},dcl_pos,dcl_kind,dcl_index} cs=:{cs_symbol_table}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
536
537
538
539
540
		#! entry = sreadPtr id_info cs_symbol_table
		| entry.ste_def_level < cGlobalScope
			# cs = { cs & cs_symbol_table = NewEntry cs_symbol_table id_info dcl_kind dcl_index cGlobalScope entry }
			= case dcl_kind of
				STE_Field selector_id
Sjaak Smetsers's avatar
Sjaak Smetsers committed
541
					-> addFieldToSelectorDefinition selector_id	{ glob_module = NoIndex, glob_object = dcl_index } cs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
542
543
				_
					-> cs
Martin Wierich's avatar
Martin Wierich committed
544
			= { cs & cs_error = checkErrorWithIdentPos (newPosition ident dcl_pos) "(global definition) already defined" cs.cs_error}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
545
546
547

retrieveImportsFromSymbolTable :: ![Import ImportDeclaration] ![Declaration] !*{#DclModule} !*(Heap SymbolTableEntry) -> *(![Declaration],!*{#DclModule},!*Heap SymbolTableEntry);
retrieveImportsFromSymbolTable [{import_module=import_module=:{id_info},import_symbols} : mods ] decls modules symbol_table
548
	# ({ste_index}, symbol_table) = readPtr id_info symbol_table
549
550
551
	  ({dcl_declared={dcls_import,dcls_local,dcls_local_for_import}}, modules) = modules![ste_index]	
//	  (decls, symbol_table) = retrieveAndRemoveImportsOfModuleFromSymbolTable dcls_import dcls_local decls symbol_table
	  (decls, symbol_table) = retrieveAndRemoveImportsOfModuleFromSymbolTable2 dcls_import dcls_local_for_import decls symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
552
553
554
	= retrieveImportsFromSymbolTable mods decls modules symbol_table
retrieveImportsFromSymbolTable [] decls modules symbol_table
	= (decls, modules, symbol_table)
555
 
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
556
557
558
removeFieldFromSelectorDefinition :: !Ident .Int .Int !*(Heap SymbolTableEntry) -> .Heap SymbolTableEntry;
removeFieldFromSelectorDefinition {id_info} field_mod field_index symbol_table 
	# (entry, symbol_table) = readPtr id_info symbol_table
Martin Wierich's avatar
Martin Wierich committed
559
560
561
562
	= case entry.ste_kind of
	  	STE_Selector selector_list
			-> symbol_table <:= (id_info, { entry & ste_kind = STE_Selector (remove_field field_mod field_index selector_list) })
		_	-> symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
563
564
565
566
567
568
569
570
571
572
573
574
where	
	remove_field field_mod field_index [field=:{glob_module, glob_object} : fields]
		| field_mod == glob_module && field_index == glob_object
			= fields
			= [field : remove_field field_mod field_index fields]
	remove_field field_mod field_index []
		= []

removeDeclarationsFromSymbolTable :: ![Declaration] !Int !*(Heap SymbolTableEntry) -> *Heap SymbolTableEntry;
removeDeclarationsFromSymbolTable decls scope symbol_table
	= foldSt (remove_declaration scope) decls symbol_table
where
Sjaak Smetsers's avatar
Sjaak Smetsers committed
575
	remove_declaration scope {dcl_ident={id_name,id_info}, dcl_index} symbol_table
576
		# ({ste_kind,ste_previous}, symbol_table) = readPtr id_info symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
577
		= case ste_kind of
578
579
580
581
582
583
584
585
586
587
588
			STE_Field field_id
				# symbol_table = removeFieldFromSelectorDefinition field_id NoIndex dcl_index symbol_table
				| ste_previous.ste_def_level == scope
					-> symbol_table <:= (id_info, ste_previous.ste_previous)
					-> symbol_table <:= (id_info, ste_previous)
			STE_Empty
				-> symbol_table
			_
				| ste_previous.ste_def_level == scope
					-> symbol_table <:= (id_info, ste_previous.ste_previous)
					-> symbol_table <:= (id_info, ste_previous)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
589
590
591
592
593
594
595
596
597
598
599
600

removeLocalIdentsFromSymbolTable :: .Int !.[Ident] !*(Heap SymbolTableEntry) -> .Heap SymbolTableEntry;
removeLocalIdentsFromSymbolTable level idents symbol_table
	= foldSt (removeIdentFromSymbolTable level) idents symbol_table

removeIdentFromSymbolTable :: !.Int !Ident !*(Heap SymbolTableEntry) -> .Heap SymbolTableEntry;
removeIdentFromSymbolTable level {id_name,id_info} symbol_table
	#! {ste_previous,ste_def_level} = sreadPtr id_info symbol_table
	| level <= ste_def_level 
		= symbol_table <:= (id_info,ste_previous) // ---> ("removeIdentFromSymbolTable", id_name)
		= symbol_table // ---> ("NO removeIdentFromSymbolTable", id_name)

601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
removeLocalsFromSymbolTable :: !Level ![Ident] !LocalDefs !u:{# FunDef} !*(Heap SymbolTableEntry)
			-> (!u:{# FunDef}, !.Heap SymbolTableEntry)
removeLocalsFromSymbolTable level loc_vars (CollectedLocalDefs {loc_functions={ir_from,ir_to}}) defs symbol_table
	= remove_defs_from_symbol_table level ir_from ir_to defs (removeLocalIdentsFromSymbolTable level loc_vars symbol_table)
where
	remove_defs_from_symbol_table level from_index to_index defs symbol_table
		| from_index == to_index
			= (defs, symbol_table)	
			#! def = defs.[from_index]
			   id_info = (toIdent def).id_info
			   entry = sreadPtr id_info symbol_table
			| level == entry.ste_def_level
				= remove_defs_from_symbol_table level (inc from_index) to_index defs (symbol_table <:= (id_info, entry.ste_previous))
				= remove_defs_from_symbol_table level (inc from_index) to_index defs symbol_table


newFreeVariable :: !FreeVar ![FreeVar] ->(!Bool, ![FreeVar])
newFreeVariable new_var vars=:[free_var=:{fv_def_level,fv_info_ptr}: free_vars]
	| new_var.fv_def_level > fv_def_level
		= (True, [new_var : vars])
	| new_var.fv_def_level == fv_def_level
		| new_var.fv_info_ptr == fv_info_ptr
			= (False, vars)
			#! (free_var_added, free_vars) = newFreeVariable new_var free_vars
			= (free_var_added, [free_var : free_vars])
		#! (free_var_added, free_vars) = newFreeVariable new_var free_vars
		= (free_var_added, [free_var : free_vars])
newFreeVariable new_var []
	= (True, [new_var])


	
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
class toIdent a :: !a -> Ident

instance toIdent SymbIdent
where
	toIdent symb = symb.symb_name

instance toIdent TypeSymbIdent
where
	toIdent type_symb = type_symb.type_name

instance toIdent BoundVar
where
	toIdent var = var.var_name

instance toIdent TypeVar
where
	toIdent tvar = tvar.tv_name

instance toIdent ATypeVar
where
	toIdent {atv_variable} = atv_variable.tv_name


instance toIdent Ident
where
	toIdent id = id

instance toIdent ConsDef
where
	toIdent cons = cons.cons_symb

664
instance toIdent (TypeDef a)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
where
	toIdent td = td.td_name

instance toIdent ClassDef
where
	toIdent cl = cl.class_name

instance toIdent MemberDef
where
	toIdent me = me.me_symb

instance toIdent FunDef
where
	toIdent fun = fun.fun_symb

instance toIdent SelectorDef
where
	toIdent sd = sd.sd_symb

/*
instance toIdent DeltaRule
where
	toIdent delta = delta.delta_name
*/

instance toIdent (a,b) | toIdent a
where
	toIdent (x,y) = toIdent x

instance == STE_Kind
where
	(==) (STE_FunctionOrMacro _) STE_DclFunction	= True
	(==) STE_DclFunction (STE_FunctionOrMacro _)	= True
	(==) sk1 sk2 									= equal_constructor sk1 sk2

instance <<< IdentPos
where
	(<<<) file {ip_file,ip_line,ip_ident}
	| ip_line == cNotALineNumber
		= file <<< '[' <<< ip_file <<< ',' <<< ip_ident <<< ']'
		= file <<< '[' <<< ip_file <<< ',' <<< ip_line <<< ',' <<< ip_ident <<< ']'
	

instance <<< STE_Kind
where
	(<<<) file
		(STE_FunctionOrMacro _)
			= file <<< "STE_FunctionOrMacro"
	(<<<) file
		STE_Type
			= file <<< "STE_Type"
	(<<<) file
		STE_Constructor
			= file <<< "STE_Constructor"
	(<<<) file
		(STE_Selector _)
			= file <<< "STE_Selector"
	(<<<) file
		STE_Class
			= file <<< "STE_Class"
	(<<<) file
		STE_Member
			= file <<< "STE_Member"
	(<<<) file
		STE_Instance
			= file <<< "STE_Instance"
	(<<<) file
		(STE_Variable _) 
			= file <<< "STE_Variable"
	(<<<) file
		(STE_TypeVariable _) 
			= file <<< "STE_TypeVariable"
	(<<<) file
		(STE_TypeAttribute _)
			= file <<< "STE_TypeAttribute"
	(<<<) file
		(STE_BoundTypeVariable _)
			= file <<< "STE_BoundTypeVariable"
	(<<<) file
		(STE_Imported _ _)
			= file <<< "STE_Imported"
	(<<<) file
		STE_DclFunction
			= file <<< "STE_DclFunction"
	(<<<) file
		(STE_Module _)
			= file <<< "STE_Module"
	(<<<) file
		(STE_OpenModule _ _)
			= file <<< "STE_OpenModule"
	(<<<) file
		STE_ClosedModule
			= file <<< "STE_ClosedModule"
	(<<<) file
		STE_LockedModule
			= file <<< "STE_LockedModule"
	(<<<) file
		STE_Empty 
			= file <<< "STE_Empty"

765
766
767
768
instance <<< Declaration
  where
	(<<<) file { dcl_ident } 
		= file <<< dcl_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
769