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

import StdEnv, compare_constructor
4
import syntax, predef, containers
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
import RWSDebug
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
9

10
::	VarHeap :== Heap VarInfo
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
11

Martin Wierich's avatar
Martin Wierich committed
12
cUndef			:== -1
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
13
14
15
16
17
18
CS_NotChecked 	:== -1
NotFound		:== -1
	
cModuleScope	:== 0
cGlobalScope	:== 1

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

22
23
24
cNeedStdArray	:== 1
cNeedStdEnum	:== 2
cNeedStdDynamics:== 4
25
cNeedStdGeneric	:== 8 // AA
26

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
::	CheckState = { cs_symbol_table :: !.SymbolTable, cs_predef_symbols :: !.PredefinedSymbols, cs_error :: !.ErrorAdmin, cs_x :: !CheckStateX }

Martin Wierich's avatar
Martin Wierich committed
37
::	CheckStateX = {x_needed_modules :: !BITVECT,x_main_dcl_module_n :: !Int /* TD */, x_is_dcl_module :: !Bool, x_type_var_position :: !Int }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
38
39
40
41
42
43
44
45

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

cTypeDefs				:== 0
cConstructorDefs		:== 1
cSelectorDefs			:== 2
cClassDefs				:== 3
cMemberDefs				:== 4
46
47
48
49
cGenericDefs			:== 5 // AA 
cInstanceDefs			:== 6
cFunctionDefs			:== 7
cMacroDefs				:== 8
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
50

51
cConversionTableSize	:== 9 // AA
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
52
53
54
55
56
57
58

instance toInt STE_Kind
where
	toInt STE_Type					= cTypeDefs
	toInt STE_Constructor			= cConstructorDefs
	toInt (STE_Field _)				= cSelectorDefs
	toInt STE_Class					= cClassDefs
59
	toInt STE_Generic				= cGenericDefs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
60
	toInt STE_Member				= cMemberDefs
61
	toInt (STE_Instance _)			= cInstanceDefs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
62
63
64
65
66
67
68
69
70
71
72
	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}
73
	,	com_generic_defs	:: !.{# GenericDef} // AA
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
74
75
	}

76
77
::	Declarations = {
		dcls_import		::!{!Declaration}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
78
	,	dcls_local		::![Declaration]
79
	,	dcls_local_for_import ::!{!Declaration}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
80
	}
81

82
::	*ExplImpInfos :== *{!*{!*ExplImpInfo}}
83
84
85
86
87
88
89
90
91
92
93
94
95

::	ExplImpInfo
		= ExplImpInfo Ident !.DeclaringModulesSet
		| TemporarilyFetchedAway

::	DeclaringModulesSet :== IntKeyHashtable DeclarationInfo

::	DeclarationInfo =
	{	di_decl			::	!Declaration
	,	di_instances	::	![Declaration]
	,	di_belonging	::	!NumberSet
	}

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
96
97
98
99
100
101
::	IclModule  =
	{	icl_name			:: !Ident
	,	icl_functions		:: !.{# FunDef }
	,	icl_instances		:: !IndexRange
	,	icl_specials		:: !IndexRange
	,	icl_common			:: !.CommonDefs
102
103
//	,	icl_declared		:: !Declarations
	,	icl_import		:: !{!Declaration}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
104
	,	icl_imported_objects	:: ![ImportedObject]
105
	,	icl_used_module_numbers :: !NumberSet
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
106
107
108
109
110
111
112
113
114
	}

::	DclModule =
	{	dcl_name			:: !Ident
	,	dcl_functions		:: !{# FunType }
	,	dcl_instances		:: !IndexRange
	,	dcl_macros			:: !IndexRange
	,	dcl_specials		:: !IndexRange
	,	dcl_common			:: !CommonDefs
115
	,	dcl_sizes			:: !{# Int}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
116
117
118
	,	dcl_declared		:: !Declarations
	,	dcl_conversions		:: !Optional ConversionTable
	,	dcl_is_system		:: !Bool
119
	,	dcl_imported_module_numbers :: !NumberSet
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
120
121
122
123
124
125
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
	}

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=[]}
Martin Wierich's avatar
Martin Wierich committed
162
	= { error & ea_file = ea_file <<< "Error " <<< "\"" <<< id <<< "\" " <<< mess <<< '\n', ea_ok = False }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
163
checkError id mess error=:{ea_file,ea_loc}
Martin Wierich's avatar
Martin Wierich committed
164
	= { error & ea_file = ea_file <<< "Error " <<< hd ea_loc <<< ":\"" <<< id  <<< "\" " <<< mess <<< '\n', ea_ok = False }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
165
166
167

checkWarning :: !a !b !*ErrorAdmin -> *ErrorAdmin | <<< a & <<< b // PK
checkWarning id mess error=:{ea_file,ea_loc=[]}
Martin Wierich's avatar
Martin Wierich committed
168
	= { error & ea_file = ea_file <<< "Warning " <<< "\"" <<< id <<< "\" " <<< mess <<< '\n' }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
169
checkWarning id mess error=:{ea_file,ea_loc}
Martin Wierich's avatar
Martin Wierich committed
170
	= { error & ea_file = ea_file <<< "Warning " <<< hd ea_loc <<< ":\"" <<< id  <<< "\" " <<< mess <<< '\n' }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
171

clean's avatar
clean committed
172

Martin Wierich's avatar
Martin Wierich committed
173
checkErrorWithIdentPos :: !IdentPos !a !*ErrorAdmin -> .ErrorAdmin | <<< a;
Martin Wierich's avatar
Martin Wierich committed
174
checkErrorWithIdentPos ident_pos mess error=:{ea_file}
Martin Wierich's avatar
Martin Wierich committed
175
	= { error & ea_file = ea_file <<< "Error " <<< ident_pos <<< ":" <<< mess <<< '\n', ea_ok = False }
Martin Wierich's avatar
Martin Wierich committed
176

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
177
178
179
180
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
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")

206
207
208
209
210
211
212

::	ExpressionInfo =
	{	ef_type_defs		:: !.{# CheckedTypeDef}
	,	ef_selector_defs	:: !.{# SelectorDef}
	,	ef_cons_defs		:: !.{# ConsDef}
	,	ef_member_defs		:: !.{# MemberDef}
	,	ef_class_defs		:: !.{# ClassDef}
213
	,	ef_generic_defs		:: !.{# GenericDef} // AA
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
	,	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)

242

Martin Wierich's avatar
Martin Wierich committed
243
getBelongingSymbols :: !Declaration !v:{#DclModule} -> (!BelongingSymbols, !v:{#DclModule})
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
getBelongingSymbols {dcl_kind=STE_Imported STE_Type def_mod_index, dcl_index} dcl_modules
	# ({td_rhs}, dcl_modules)
			= dcl_modules![def_mod_index].dcl_common.com_type_defs.[dcl_index]
	= case td_rhs of
		AlgType constructors
			-> (BS_Constructors constructors, dcl_modules)
		RecordType {rt_fields}
			-> (BS_Fields rt_fields, dcl_modules)
		_
			-> (BS_Nothing, dcl_modules)
getBelongingSymbols {dcl_kind=STE_Imported STE_Class def_mod_index, dcl_index} dcl_modules
	# ({class_members}, dcl_modules)
			= dcl_modules![def_mod_index].dcl_common.com_class_defs.[dcl_index]
	= (BS_Members class_members, dcl_modules)
getBelongingSymbols _ dcl_modules
	= (BS_Nothing, dcl_modules)

nrOfBelongingSymbols :: !BelongingSymbols -> Int
nrOfBelongingSymbols (BS_Constructors constructors)
	= length constructors
nrOfBelongingSymbols (BS_Fields fields)
	= size fields
nrOfBelongingSymbols (BS_Members members)
	= size members
nrOfBelongingSymbols BS_Nothing
	= 0

:: BelongingSymbols
	=	BS_Constructors ![DefinedSymbol]
	|	BS_Fields !{#FieldSymbol}
	|	BS_Members !{#DefinedSymbol}
	|	BS_Nothing

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
277

278
removeImportsAndLocalsOfModuleFromSymbolTable :: !Declarations !*(Heap SymbolTableEntry) -> .Heap SymbolTableEntry
Martin Wierich's avatar
Martin Wierich committed
279
removeImportsAndLocalsOfModuleFromSymbolTable {dcls_import,dcls_local_for_import} symbol_table
280
	# symbol_table = remove_declared_symbols_in_array 0 dcls_import symbol_table
Martin Wierich's avatar
Martin Wierich committed
281
	= remove_declared_symbols_in_array 0 dcls_local_for_import symbol_table
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
where
	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

304
305
addLocalFunctionDefsToSymbolTable :: !Level !Index !Index !Bool !*{#FunDef} !*SymbolTable !*ErrorAdmin -> (!*{# FunDef}, !*SymbolTable, !*ErrorAdmin)
addLocalFunctionDefsToSymbolTable level from_index to_index is_macro_fun fun_defs symbol_table error
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
306
307
	| from_index == to_index
		= (fun_defs, symbol_table, error)	
308
		# (fun_def, fun_defs) = fun_defs![from_index]
309
310
		# (symbol_table, error) = addDefToSymbolTable level from_index fun_def.fun_symb (STE_FunctionOrMacro []) symbol_table error
		| is_macro_fun
311
			# fun_defs = {fun_defs & [from_index].fun_info.fi_properties = fun_def.fun_info.fi_properties bitor FI_IsMacroFun }
312
313
			= addLocalFunctionDefsToSymbolTable level (inc from_index) to_index is_macro_fun fun_defs symbol_table error
			= addLocalFunctionDefsToSymbolTable level (inc from_index) to_index is_macro_fun fun_defs symbol_table error
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
314
315
316

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 })
317

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
318
319
320
321
322
323
324
325
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)

Martin Wierich's avatar
Martin Wierich committed
326
327
328
addDeclarationsOfDclModToSymbolTable :: .Int !{!Declaration} !{!Declaration} !*CheckState -> .CheckState;
addDeclarationsOfDclModToSymbolTable ste_index locals imported cs
	# cs=add_imports_in_array_to_symbol_table 0 imported cs
329
	= addLocalSymbolsForImportToSymbolTable 0 locals ste_index cs
Martin Wierich's avatar
Martin Wierich committed
330
331
332
333
334
335
  where
	add_imports_in_array_to_symbol_table symbol_index 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
336
					#! dcl_index= symbols.[symbol_index].dcl_index
Martin Wierich's avatar
Martin Wierich committed
337
338
339
340
341
					   (_, cs)
					   		= addSymbol No dcl_ident dcl_pos dcl_kind
					   				def_kind dcl_index def_mod cUndef cs
					-> add_imports_in_array_to_symbol_table (symbol_index+1) symbols cs
				STE_FunctionOrMacro _
342
					#! dcl_index= symbols.[symbol_index].dcl_index
Martin Wierich's avatar
Martin Wierich committed
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
					   (_, cs)
					   		= addImportedFunctionOrMacro No dcl_ident dcl_index cs
					-> add_imports_in_array_to_symbol_table (symbol_index+1) symbols 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 _
					# (_, cs)
							= addImportedFunctionOrMacro No dcl_ident dcl_index cs
					-> addLocalSymbolsForImportToSymbolTable (symbol_index+1) symbols mod_index cs
				STE_Imported def_kind def_mod
					# (_, cs)
							= addSymbol No dcl_ident dcl_pos dcl_kind
									def_kind dcl_index mod_index cUndef cs
					-> addLocalSymbolsForImportToSymbolTable (symbol_index+1) symbols mod_index cs
			= cs
	
addImportedFunctionOrMacro :: !(Optional IndexRange) !Ident !Int !*CheckState -> (!Bool, !.CheckState)
addImportedFunctionOrMacro opt_dcl_macro_range ident=:{id_info} def_index cs=:{cs_symbol_table}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
366
367
368
	#! entry = sreadPtr id_info cs_symbol_table
	= case entry.ste_kind of
		STE_Empty
Martin Wierich's avatar
Martin Wierich committed
369
370
			-> (True, { cs & cs_symbol_table = NewEntry cs.cs_symbol_table id_info (STE_FunctionOrMacro []) 
													def_index cModuleScope entry})
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
371
		STE_FunctionOrMacro _
Martin Wierich's avatar
Martin Wierich committed
372
373
			| entry.ste_index == def_index || within_opt_range opt_dcl_macro_range def_index
				-> (False, cs)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
374
		_
Martin Wierich's avatar
Martin Wierich committed
375
376
377
378
379
380
381
			-> (False, { cs & cs_error = checkError ident "multiply defined" cs.cs_error})
  where
	within_opt_range (Yes {ir_from, ir_to}) i
		= ir_from<=i && i<ir_to
	within_opt_range No _
		= False

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
382
383
384
385
386
387
388
389
390
391

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 }
392

Martin Wierich's avatar
Martin Wierich committed
393
394
addSymbol :: !(Optional a) !Ident !Position !STE_Kind !STE_Kind !.Int !.Int !Int !*CheckState -> (!Bool, !.CheckState)
addSymbol yes_for_icl_module ident pos dcl_kind def_kind def_index def_mod importing_mod cs=:{cs_symbol_table}
clean's avatar
clean committed
395
	# (entry, cs_symbol_table) = readPtr ident.id_info cs_symbol_table
396
397
	= add_indirectly_imported_symbol yes_for_icl_module entry ident pos def_kind def_index def_mod 
			importing_mod { cs & cs_symbol_table = cs_symbol_table }
clean's avatar
clean committed
398
	where
399
		add_indirectly_imported_symbol _ {ste_kind = STE_Empty} {id_info} _ def_kind def_index def_mod _ cs=:{cs_symbol_table}
clean's avatar
clean committed
400
			# (entry, cs_symbol_table) = readPtr id_info cs_symbol_table
401
			  cs = { cs & cs_symbol_table = NewEntry cs_symbol_table id_info dcl_kind def_index cModuleScope entry}
clean's avatar
clean committed
402
403
			= case def_kind of
				STE_Field selector_id
404
					-> (True, addFieldToSelectorDefinition selector_id	{ glob_module = def_mod, glob_object = def_index } cs)
clean's avatar
clean committed
405
				_
406
407
					-> (True, cs)
		add_indirectly_imported_symbol _ {ste_kind = STE_Imported kind mod_index, ste_index} _ _ def_kind def_index def_mod _ cs
clean's avatar
clean committed
408
			| kind == def_kind && mod_index == def_mod && ste_index == def_index
409
410
411
412
413
414
415
416
417
418
419
420
				= (False, cs)
		add_indirectly_imported_symbol (Yes _) _ _ _ def_kind def_index def_mod _ cs
			| def_mod == cs.cs_x.x_main_dcl_module_n
				// an icl module imports one of it's definitions from the dcl module
				= (False, cs)
		add_indirectly_imported_symbol _ _ _ _ def_kind def_index def_mod importing_mod cs
			| importing_mod==def_mod // a dcl module imports a definition from itself (cycle)
				= (False, cs)
		add_indirectly_imported_symbol _ entry ident pos def_kind def_index def_mod _ cs=:{cs_error}
			= (False, { cs & cs_error = checkError ident "multiply defined" cs_error})

addGlobalDefinitionsToSymbolTable :: ![Declaration] !*CheckState -> .CheckState;
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
421
422
423
addGlobalDefinitionsToSymbolTable decls cs
	= foldSt add_global_definition decls cs
where
Martin Wierich's avatar
Martin Wierich committed
424
	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
425
426
427
428
429
		#! 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
430
					-> addFieldToSelectorDefinition selector_id	{ glob_module = NoIndex, glob_object = dcl_index } cs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
431
432
				_
					-> cs
433
434
			= { cs & cs_error = checkErrorWithIdentPos (newPosition ident dcl_pos) " multiply defined" cs.cs_error}

435
436
437
438
439
440
441
442
443
444
445
446
removeImportedSymbolsFromSymbolTable :: Declaration !*SymbolTable -> .SymbolTable
removeImportedSymbolsFromSymbolTable {dcl_ident=dcl_ident=:{id_info}, dcl_index} symbol_table
	# ({ste_kind,ste_def_level,ste_previous}, symbol_table)
			= readPtr id_info symbol_table
	  symbol_table
	  		= symbol_table <:= (id_info, ste_previous)
	= case ste_kind of
		STE_Imported (STE_Field selector_id) def_mod
			-> removeFieldFromSelectorDefinition selector_id def_mod dcl_index symbol_table
		_
			-> symbol_table

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
447
448
449
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
450
451
452
453
	= 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
454
455
456
457
458
459
460
461
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 []
		= []

462
removeDeclarationsFromSymbolTable :: ![Declaration] !Int !*SymbolTable -> *SymbolTable
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
463
removeDeclarationsFromSymbolTable decls scope symbol_table
Martin Wierich's avatar
Martin Wierich committed
464
	= foldSt (remove_declaration scope) decls symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
465
where
Martin Wierich's avatar
Martin Wierich committed
466
	remove_declaration scope decl=:{dcl_ident={id_info}, dcl_index} symbol_table
467
468
		# ({ste_kind,ste_previous}, symbol_table)
				= readPtr id_info symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
469
		= case ste_kind of
470
471
472
473
474
475
476
477
478
479
480
			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
481
482
483
484
485
486
487
488
489
490
491
492

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)

493
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
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])


524
525
526
527
528
529
530
531
local_declaration_for_import :: !u:Declaration .Index -> v:Declaration, [u <= v]
local_declaration_for_import decl=:{dcl_kind=STE_FunctionOrMacro _} module_n
	= decl
local_declaration_for_import decl=:{dcl_kind=STE_Imported _ _} module_n
	= abort "local_declaration_for_import"
local_declaration_for_import decl=:{dcl_kind} module_n
	= {decl & dcl_kind = STE_Imported dcl_kind module_n}

532
	
533
534
535
536
537
538
539
540
541
542
543
544
545
get_ident :: !ImportDeclaration -> Ident
get_ident (ID_Function {ii_ident})						= ii_ident
get_ident (ID_Class {ii_ident} _)						= ii_ident
get_ident (ID_Type {ii_ident} _)						= ii_ident
get_ident (ID_Record {ii_ident} _)						= ii_ident
get_ident (ID_Instance class_ident instance_ident _)	= instance_ident

getBelongingSymbolsFromID :: !ImportDeclaration -> Optional [ImportedIdent]
getBelongingSymbolsFromID (ID_Class _ x)						= x
getBelongingSymbolsFromID (ID_Type _ x)						= x
getBelongingSymbolsFromID (ID_Record _ x)						= x
getBelongingSymbolsFromID _									= No

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
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

577
instance toIdent (TypeDef a)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
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 <<< ']'
	

621
instance <<< ExplImpInfo
622
  where
623
624
	(<<<) file (ExplImpInfo eii_ident eii_declaring_modules)
		= file <<< eii_ident //<<< " is declared in " <<< eii_declaring_modules
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
625

626
627
628
629
630
631
632
633
634
635
636
637
638
instance <<< DeclarationInfo
  where
	(<<<) file {di_decl, di_instances}
		= file <<< di_decl <<< di_instances
		
import_ident :: Ident
import_ident =: { id_name = "import", id_info = nilPtr }

restoreHeap :: !Ident !*SymbolTable -> .SymbolTable
restoreHeap {id_info} cs_symbol_table
	# ({ste_previous}, cs_symbol_table)
			= readPtr id_info cs_symbol_table
	= writePtr id_info ste_previous cs_symbol_table