checksupport.icl 26.7 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
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
6

7
//import RWSDebug
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
8

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

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

19
20
cNeedStdArray	:== 1
cNeedStdEnum	:== 2
21
cNeedStdDynamic :== 4
22
cNeedStdGeneric	:== 8 // AA
23
cNeedStdStrictLists :== 16
24

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

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

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

36
::	CheckStateX = {x_needed_modules :: !BITVECT,x_main_dcl_module_n :: !Int, x_check_dynamic_types :: !Bool }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
37
38
39
40
41
42
43
44

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

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

51
cConversionTableSize	:== 10
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
60
	toInt STE_GenericCase			= cGenericCaseDefs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
61
	toInt STE_Member				= cMemberDefs
62
	toInt (STE_Instance _)			= cInstanceDefs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
63
64
	toInt STE_DclFunction			= cFunctionDefs
	toInt (STE_FunctionOrMacro _)	= cMacroDefs
65
	toInt (STE_DclMacroOrLocalMacroFunction _)= cMacroDefs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
66
67
68
69
	toInt _							= NoIndex

::	CommonDefs =
	{	com_type_defs 		:: !.{# CheckedTypeDef}
70
//	,	com_unexpanded_type_defs :: !{# CheckedTypeDef}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
71
72
73
74
75
	,	com_cons_defs		:: !.{# ConsDef}
	,	com_selector_defs	:: !.{# SelectorDef}
	,	com_class_defs		:: !.{# ClassDef}
	,	com_member_defs		:: !.{# MemberDef}
	,	com_instance_defs	:: !.{# ClassInstance}
76
	,	com_generic_defs	:: !.{# GenericDef} // AA
77
	,	com_gencase_defs 	:: !.{# GenericCaseDef} // AA
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
78
79
	}

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

86
::	*ExplImpInfos :== *{!*{!*ExplImpInfo}}
87
88
89
90
91
92
93
94
95
96
97
98
99

::	ExplImpInfo
		= ExplImpInfo Ident !.DeclaringModulesSet
		| TemporarilyFetchedAway

::	DeclaringModulesSet :== IntKeyHashtable DeclarationInfo

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

100
::	CopiedDefinitions =
101
102
	{	copied_type_defs	:: {#Bool}
	,	copied_class_defs	:: {#Bool}
103
	,	copied_generic_defs :: {#Bool}
104
105
	}
	
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
106
::	IclModule  =
107
108
	{	icl_name				:: !Ident
	,	icl_functions			:: !.{# FunDef }
109
110
	,	icl_global_functions	:: ![IndexRange]
	,	icl_instances			:: ![IndexRange]
111
	,	icl_specials			:: !IndexRange
112
	,	icl_gencases			:: ![IndexRange]
113
114
	,	icl_common				:: !.CommonDefs
	,	icl_import				:: !{!Declaration}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
115
	,	icl_imported_objects	:: ![ImportedObject]
116
	,	icl_used_module_numbers :: !NumberSet
117
	,	icl_copied_from_dcl 	:: !CopiedDefinitions
118
119
120
// RWS ...
	,	icl_modification_time	:: !{#Char}
// ... RWS
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
121
122
123
124
125
126
127
128
	}

::	DclModule =
	{	dcl_name			:: !Ident
	,	dcl_functions		:: !{# FunType }
	,	dcl_instances		:: !IndexRange
	,	dcl_macros			:: !IndexRange
	,	dcl_specials		:: !IndexRange
129
	,	dcl_gencases		:: !IndexRange
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
130
	,	dcl_common			:: !CommonDefs
131
	,	dcl_sizes			:: !{# Int}
132
	,	dcl_dictionary_info	:: !DictionaryInfo
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
133
	,	dcl_declared		:: !Declarations
134
	,	dcl_macro_conversions :: !Optional {#Index}
135
	,	dcl_module_kind		:: !ModuleKind
136
	,	dcl_modification_time:: !{#Char}
137
	,	dcl_imported_module_numbers :: !NumberSet
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
138
139
	}

140
141
::	DictionaryInfo = { n_dictionary_types :: !Int, n_dictionary_constructors :: !Int, n_dictionary_selectors :: !Int }

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
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
176
177
178
179
180
181
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
182
	= { error & ea_file = ea_file <<< "Error " <<< " " <<< id <<< " " <<< mess <<< '\n', ea_ok = False }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
183
checkError id mess error=:{ea_file,ea_loc}
Martin Wierich's avatar
Martin Wierich committed
184
	= { error & ea_file = ea_file <<< "Error " <<< hd ea_loc <<< ": " <<< id  <<< " " <<< mess <<< '\n', ea_ok = False }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
185
186
187

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

clean's avatar
clean committed
192

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

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
197
198
199
200
201
class envLookUp a :: !a !(Env Ident .b) -> (!Bool,.b)

instance envLookUp TypeVar
where
	envLookUp var [bind:binds]
202
		| var.tv_ident == bind.bind_src
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
203
204
205
206
207
208
209
210
			= (True, bind.bind_dst)
			= envLookUp var binds
	envLookUp var []
		= (False, abort "illegal value")
	
instance envLookUp AttributeVar
where
	envLookUp var [bind:binds]
211
		| var.av_ident == bind.bind_src
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
212
213
214
215
216
217
218
219
			= (True, bind.bind_dst)
			= envLookUp var binds
	envLookUp var []
		= (False, abort "illegal value")

instance envLookUp ATypeVar
where
	envLookUp var=:{atv_variable} [bind:binds]
220
		| atv_variable.tv_ident == bind.bind_src
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
221
222
223
224
225
			= (True, bind.bind_dst)
			= envLookUp var binds
	envLookUp var []
		= (False, abort "illegal value")

226
227
228
229
230
231
232

::	ExpressionInfo =
	{	ef_type_defs		:: !.{# CheckedTypeDef}
	,	ef_selector_defs	:: !.{# SelectorDef}
	,	ef_cons_defs		:: !.{# ConsDef}
	,	ef_member_defs		:: !.{# MemberDef}
	,	ef_class_defs		:: !.{# ClassDef}
233
	,	ef_generic_defs		:: !.{# GenericDef}
234
	,	ef_modules			:: !.{# DclModule}
235
	,	ef_macro_defs		:: !.{#.{#FunDef}}
236
237
238
	,	ef_is_macro_fun		:: !Bool
	}

239
/*
240
241
242
243
244
convertIndex :: !Index !Index !(Optional ConversionTable) -> !Index
convertIndex index table_index (Yes tables)
	= tables.[table_index].[index]
convertIndex index table_index No
	= index
245
*/
246
247

retrieveGlobalDefinition :: !SymbolTableEntry !STE_Kind !Index -> (!Index, !Index)
248
retrieveGlobalDefinition {ste_kind = STE_Imported kind decl_index, ste_def_level, ste_index} requ_kind mod_index
249
	| kind == requ_kind
250
		= (ste_index, decl_index)
251
252
253
254
255
256
		= (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)

257

Martin Wierich's avatar
Martin Wierich committed
258
getBelongingSymbols :: !Declaration !v:{#DclModule} -> (!BelongingSymbols, !v:{#DclModule})
259
getBelongingSymbols (Declaration {decl_kind=STE_Imported STE_Type def_mod_index, decl_index}) dcl_modules
260
	# ({td_rhs}, dcl_modules)
261
			= dcl_modules![def_mod_index].dcl_common.com_type_defs.[decl_index]
262
263
264
265
266
267
268
	= case td_rhs of
		AlgType constructors
			-> (BS_Constructors constructors, dcl_modules)
		RecordType {rt_fields}
			-> (BS_Fields rt_fields, dcl_modules)
		_
			-> (BS_Nothing, dcl_modules)
269
getBelongingSymbols (Declaration {decl_kind=STE_Imported STE_Class def_mod_index, decl_index}) dcl_modules
270
	# ({class_members}, dcl_modules)
271
			= dcl_modules![def_mod_index].dcl_common.com_class_defs.[decl_index]
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
	= (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
292

293
removeImportsAndLocalsOfModuleFromSymbolTable :: !Declarations !*(Heap SymbolTableEntry) -> .Heap SymbolTableEntry
Martin Wierich's avatar
Martin Wierich committed
294
removeImportsAndLocalsOfModuleFromSymbolTable {dcls_import,dcls_local_for_import} symbol_table
295
	# symbol_table = remove_declared_symbols_in_array 0 dcls_import symbol_table
Martin Wierich's avatar
Martin Wierich committed
296
	= remove_declared_symbols_in_array 0 dcls_local_for_import symbol_table
297
where
John van Groningen's avatar
John van Groningen committed
298
	remove_declared_symbols_in_array :: !Int !{!Declaration} !*SymbolTable -> *SymbolTable
299
300
	remove_declared_symbols_in_array symbol_index symbols symbol_table
		| symbol_index<size symbols
301
			# symbol = symbols.[symbol_index]
302
			# (Declaration {decl_ident={id_info}})=symbol
303
			# (entry, symbol_table) = readPtr id_info symbol_table
304
305
306
307
308
309
			# {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
310
311
312
						#! declaration = symbols.[symbol_index]
						# (Declaration {decl_index}) = declaration
						-> remove_declared_symbols_in_array (symbol_index+1) symbols (removeFieldFromSelectorDefinition selector_id NoIndex decl_index symbol_table)
313
					STE_Imported (STE_Field selector_id) def_mod
314
315
316
						#! declaration = symbols.[symbol_index]
						# (Declaration {decl_index}) = declaration
						-> remove_declared_symbols_in_array (symbol_index+1) symbols (removeFieldFromSelectorDefinition selector_id def_mod decl_index symbol_table)
317
318
319
320
					_
						-> remove_declared_symbols_in_array (symbol_index+1) symbols symbol_table
			= symbol_table

321
322
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
323
324
	| from_index == to_index
		= (fun_defs, symbol_table, error)	
325
		# (fun_def, fun_defs) = fun_defs![from_index]
326
		# (symbol_table, error) = addDefToSymbolTable level from_index fun_def.fun_ident (STE_FunctionOrMacro []) symbol_table error
327
		| is_macro_fun
328
			# fun_defs = {fun_defs & [from_index].fun_info.fi_properties = fun_def.fun_info.fi_properties bitor FI_IsMacroFun }
329
330
			= 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
331

332
333
334
335
336
addLocalDclMacroDefsToSymbolTable :: !Level !Int !Index !Index !*{#*{#FunDef}} !*SymbolTable !*ErrorAdmin -> (!*{#*{#FunDef}}, !*SymbolTable, !*ErrorAdmin)
addLocalDclMacroDefsToSymbolTable level module_index from_index to_index macro_defs symbol_table error
	| from_index == to_index
		= (macro_defs, symbol_table, error)	
		# (macro_def, macro_defs) = macro_defs![module_index,from_index]
337
		# (symbol_table, error) = addDefToSymbolTable level from_index macro_def.fun_ident (STE_DclMacroOrLocalMacroFunction []) symbol_table error
338
339
340
		# macro_defs = {macro_defs & [module_index].[from_index].fun_info.fi_properties = macro_def.fun_info.fi_properties bitor FI_IsMacroFun }
		= addLocalDclMacroDefsToSymbolTable level module_index (inc from_index) to_index macro_defs symbol_table error

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
341
342
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 })
343

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
344
345
addDefToSymbolTable :: !Level !Index !Ident !STE_Kind !*SymbolTable !*ErrorAdmin -> (!* SymbolTable, !*ErrorAdmin)
addDefToSymbolTable level def_index def_ident=:{id_info} def_kind symbol_table error
346
	# (entry, symbol_table) = readPtr id_info symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
347
348
349
	| 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)
Martin Wierich's avatar
Martin Wierich committed
350
		= (symbol_table, checkError def_ident "already defined" error)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
351

Martin Wierich's avatar
Martin Wierich committed
352
353
354
addDeclarationsOfDclModToSymbolTable :: .Int !{!Declaration} !{!Declaration} !*CheckState -> .CheckState;
addDeclarationsOfDclModToSymbolTable ste_index locals imported cs
	# cs=add_imports_in_array_to_symbol_table 0 imported cs
355
	= addLocalSymbolsForImportToSymbolTable 0 locals ste_index cs
Martin Wierich's avatar
Martin Wierich committed
356
357
358
  where
	add_imports_in_array_to_symbol_table symbol_index symbols cs=:{cs_x}
		| symbol_index<size symbols
359
360
			#! (Declaration {decl_ident,decl_pos,decl_kind},symbols) = symbols![symbol_index]
			= case decl_kind of
Martin Wierich's avatar
Martin Wierich committed
361
				STE_Imported def_kind def_mod
362
363
364
365
366
					#! declaration = symbols.[symbol_index]
					# (Declaration {decl_index}) = declaration
					# (_, cs)
					   		= addSymbol No decl_ident decl_pos decl_kind
					   				def_kind decl_index def_mod cUndef cs
Martin Wierich's avatar
Martin Wierich committed
367
368
					-> add_imports_in_array_to_symbol_table (symbol_index+1) symbols cs
				STE_FunctionOrMacro _
369
370
371
372
					#! declaration = symbols.[symbol_index]
					# (Declaration {decl_index}) = declaration
					# (_, cs)
					   		= addImportedFunctionOrMacro No decl_ident decl_index cs
Martin Wierich's avatar
Martin Wierich committed
373
374
375
376
377
378
					-> 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
379
380
			# (Declaration {decl_ident,decl_pos,decl_kind,decl_index},symbols) = symbols![symbol_index]
			= case decl_kind of
Martin Wierich's avatar
Martin Wierich committed
381
382
				STE_FunctionOrMacro _
					# (_, cs)
383
							= addImportedFunctionOrMacro No decl_ident decl_index cs
Martin Wierich's avatar
Martin Wierich committed
384
385
386
					-> addLocalSymbolsForImportToSymbolTable (symbol_index+1) symbols mod_index cs
				STE_Imported def_kind def_mod
					# (_, cs)
387
388
							= addSymbol No decl_ident decl_pos decl_kind
									def_kind decl_index mod_index cUndef cs
Martin Wierich's avatar
Martin Wierich committed
389
390
391
392
393
					-> 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}
394
395
	# (entry, cs_symbol_table) = readPtr id_info cs_symbol_table
	  cs = { cs & cs_symbol_table = cs_symbol_table }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
396
397
	= case entry.ste_kind of
		STE_Empty
Martin Wierich's avatar
Martin Wierich committed
398
399
			-> (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
400
		STE_FunctionOrMacro _
Martin Wierich's avatar
Martin Wierich committed
401
402
			| 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
403
		_
Martin Wierich's avatar
Martin Wierich committed
404
405
406
407
408
409
410
			-> (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
411
412
413
414
415
416
417
418
419
420

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

Martin Wierich's avatar
Martin Wierich committed
422
addSymbol :: !(Optional a) !Ident !Position !STE_Kind !STE_Kind !.Int !.Int !Int !*CheckState -> (!Bool, !.CheckState)
423
addSymbol yes_for_icl_module ident pos decl_kind def_kind def_index def_mod importing_mod cs=:{cs_symbol_table}
clean's avatar
clean committed
424
	# (entry, cs_symbol_table) = readPtr ident.id_info cs_symbol_table
425
426
	= 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
427
	where
428
		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
429
			# (entry, cs_symbol_table) = readPtr id_info cs_symbol_table
430
			  cs = { cs & cs_symbol_table = NewEntry cs_symbol_table id_info decl_kind def_index cModuleScope entry}
clean's avatar
clean committed
431
432
			= case def_kind of
				STE_Field selector_id
433
					-> (True, addFieldToSelectorDefinition selector_id	{ glob_module = def_mod, glob_object = def_index } cs)
clean's avatar
clean committed
434
				_
435
436
					-> (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
437
			| kind == def_kind && mod_index == def_mod && ste_index == def_index
438
439
440
441
442
443
444
445
446
447
448
449
				= (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
450
451
452
addGlobalDefinitionsToSymbolTable decls cs
	= foldSt add_global_definition decls cs
where
453
	add_global_definition (Declaration {decl_ident=ident=:{id_info},decl_pos,decl_kind,decl_index}) cs=:{cs_symbol_table}
454
		# (entry, cs_symbol_table) = readPtr id_info cs_symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
455
		| entry.ste_def_level < cGlobalScope
456
457
			# cs = { cs & cs_symbol_table = NewEntry cs_symbol_table id_info decl_kind decl_index cGlobalScope entry }
			= case decl_kind of
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
458
				STE_Field selector_id
459
					-> addFieldToSelectorDefinition selector_id	{ glob_module = NoIndex, glob_object = decl_index } cs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
460
461
				_
					-> cs
462
			= { cs & cs_symbol_table = cs_symbol_table, cs_error = checkErrorWithIdentPos (newPosition ident decl_pos) "multiply defined" cs.cs_error}
463

464
removeImportedSymbolsFromSymbolTable :: Declaration !*SymbolTable -> .SymbolTable
465
removeImportedSymbolsFromSymbolTable (Declaration {decl_ident=decl_ident=:{id_info}, decl_index}) symbol_table
466
467
468
469
470
471
	# ({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
472
			-> removeFieldFromSelectorDefinition selector_id def_mod decl_index symbol_table
473
474
475
		_
			-> symbol_table

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
476
477
478
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
479
480
481
482
	= 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
483
484
485
486
487
488
489
490
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 []
		= []

491
removeDeclarationsFromSymbolTable :: ![Declaration] !Int !*SymbolTable -> *SymbolTable
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
492
removeDeclarationsFromSymbolTable decls scope symbol_table
Martin Wierich's avatar
Martin Wierich committed
493
	= foldSt (remove_declaration scope) decls symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
494
where
495
	remove_declaration scope decl=:(Declaration {decl_ident={id_info}, decl_index}) symbol_table
496
497
		# ({ste_kind,ste_previous}, symbol_table)
				= readPtr id_info symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
498
		= case ste_kind of
499
			STE_Field field_id
500
				# symbol_table = removeFieldFromSelectorDefinition field_id NoIndex decl_index symbol_table
501
502
503
504
505
506
507
508
509
				| 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
510
511
512
513
514
515
516

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
517
	# ({ste_previous,ste_def_level}, symbol_table) = readPtr id_info symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
518
519
520
521
	| level <= ste_def_level 
		= symbol_table <:= (id_info,ste_previous) // ---> ("removeIdentFromSymbolTable", id_name)
		= symbol_table // ---> ("NO removeIdentFromSymbolTable", id_name)

522
523
524
removeLocalDclMacrosFromSymbolTable :: !Level !Index !IndexRange !*{#*{#FunDef}} !*(Heap SymbolTableEntry) -> (!.{#.{#FunDef}}, !.Heap SymbolTableEntry)
removeLocalDclMacrosFromSymbolTable level module_index {ir_from,ir_to} defs symbol_table
	= remove_macro_defs_from_symbol_table level ir_from ir_to defs symbol_table
525
where
526
527
528
	remove_macro_defs_from_symbol_table level from_index to_index defs symbol_table
		| from_index == to_index
			= (defs, symbol_table)	
529
530
531
			# (def,defs) = defs![module_index,from_index]
			  id_info = (toIdent def).id_info
			  (entry,symbol_table) = readPtr id_info symbol_table
532
533
534
535
536
537
538
539
540
			| level == entry.ste_def_level
				= remove_macro_defs_from_symbol_table level (inc from_index) to_index defs (symbol_table <:= (id_info, entry.ste_previous))
				= remove_macro_defs_from_symbol_table level (inc from_index) to_index defs symbol_table

removeLocalFunctionsFromSymbolTable :: !Level !IndexRange !*{# FunDef} !*(Heap SymbolTableEntry) -> (!.{# FunDef}, !.Heap SymbolTableEntry)
removeLocalFunctionsFromSymbolTable level {ir_from,ir_to} defs symbol_table
	= remove_fun_defs_from_symbol_table level ir_from ir_to defs symbol_table
where
	remove_fun_defs_from_symbol_table level from_index to_index defs symbol_table
541
542
		| from_index == to_index
			= (defs, symbol_table)	
543
544
			# (def,defs) = defs![from_index]
			  id_info = (toIdent def).id_info
545
			#  (entry, symbol_table) = readPtr id_info symbol_table
546
			| level == entry.ste_def_level
547
548
				= remove_fun_defs_from_symbol_table level (inc from_index) to_index defs (symbol_table <:= (id_info, entry.ste_previous))
				= remove_fun_defs_from_symbol_table level (inc from_index) to_index defs symbol_table
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564

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


565
local_declaration_for_import :: !u:Declaration .Index -> v:Declaration, [u <= v]
566
local_declaration_for_import decl=:(Declaration {decl_kind=STE_FunctionOrMacro _}) module_n
567
	= decl
568
local_declaration_for_import decl=:(Declaration {decl_kind=STE_Imported _ _}) module_n
569
	= abort "local_declaration_for_import"
570
571
local_declaration_for_import decl=:(Declaration declaration_record=:{decl_kind}) module_n
	= Declaration {declaration_record & decl_kind = STE_Imported decl_kind module_n}
572

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
573
574
575
576
class toIdent a :: !a -> Ident

instance toIdent SymbIdent
where
577
	toIdent symb = symb.symb_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
578
579
580

instance toIdent TypeSymbIdent
where
581
	toIdent type_symb = type_symb.type_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
582
583
584

instance toIdent BoundVar
where
585
	toIdent var = var.var_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
586
587
588

instance toIdent TypeVar
where
589
	toIdent tvar = tvar.tv_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
590
591
592

instance toIdent ATypeVar
where
593
	toIdent {atv_variable} = atv_variable.tv_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
594
595
596
597
598
599
600
601


instance toIdent Ident
where
	toIdent id = id

instance toIdent ConsDef
where
602
	toIdent cons = cons.cons_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
603

604
instance toIdent (TypeDef a)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
605
where
606
	toIdent td = td.td_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
607
608
609

instance toIdent ClassDef
where
610
	toIdent cl = cl.class_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
611
612
613

instance toIdent MemberDef
where
614
	toIdent me = me.me_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
615
616
617

instance toIdent FunDef
where
618
	toIdent fun = fun.fun_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
619
620
621

instance toIdent SelectorDef
where
622
	toIdent sd = sd.sd__ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
623
624
625
626
627
628
629
630
631
632
633
634
635
636

/*
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
637
	(==) (STE_FunctionOrMacro _) (STE_DclMacroOrLocalMacroFunction _) = True
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
638
	(==) STE_DclFunction (STE_FunctionOrMacro _)	= True
639
	(==) (STE_DclMacroOrLocalMacroFunction _) (STE_FunctionOrMacro _)	= True
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
640
641
642
643
644
645
646
647
648
649
	(==) 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 <<< ']'
	

650
instance <<< ExplImpInfo
651
  where
652
653
	(<<<) 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
654

655
656
657
658
instance <<< DeclarationInfo
  where
	(<<<) file {di_decl, di_instances}
		= file <<< di_decl <<< di_instances
659

660
661
662
import_ident :: Ident
import_ident =: { id_name = "import", id_info = nilPtr }

663
664
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
/*
ste_kind_to_string :: STE_Kind -> String
ste_kind_to_string ste_kind
 = case ste_kind of
	STE_FunctionOrMacro _
		-> "STE_FunctionOrMacro"
	STE_Type
		-> "STE_Type"
	STE_Constructor
		-> "STE_Constructor"
	STE_Selector _
		-> "STE_Selector"
	STE_Field _
		-> "STE_Field"
	STE_Class
		-> "STE_Class"
	STE_Member
		-> "STE_Member"
	STE_Instance _
		-> "STE_Instance"
	STE_Variable _
		-> "STE_Variable"
	STE_TypeVariable _
		-> "STE_TypeVariable"
	STE_TypeAttribute _
		-> "STE_TypeAttribute"
	STE_BoundTypeVariable _
		-> "STE_BoundTypeVariable"
	STE_Imported ste_kind2 _
		-> "STE_Imported "+++ste_kind_to_string ste_kind2
	STE_DclFunction
		-> "STE_DclFunction"
	STE_Module _
		-> "STE_Module"
	STE_ClosedModule
		-> "STE_ClosedModule"
	STE_Empty
		-> "STE_Empty"
	STE_DictType _
		-> "STE_DictType"
	STE_DictCons _
		-> "STE_DictCons"
	STE_DictField _
		-> "STE_DictField"
	STE_Called _
		-> "STE_Called"
	STE_ExplImpSymbol _
		-> "STE_ExplImpSymbol"
	STE_ExplImpComponentNrs _ _
		-> "STE_ExplImpComponentNrs"
	STE_BelongingSymbol _
		-> "STE_BelongingSymbol"
*/

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