checksupport.icl 28.4 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 genericsupport import type_cons_to_string
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
7

Martin Wierich's avatar
Martin Wierich committed
8
cUndef			:== -1
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
9
10
11
12
13
14
15

instance toInt STE_Kind
where
	toInt STE_Type					= cTypeDefs
	toInt STE_Constructor			= cConstructorDefs
	toInt (STE_Field _)				= cSelectorDefs
	toInt STE_Class					= cClassDefs
16
	toInt STE_Generic				= cGenericDefs
17
	toInt STE_GenericCase			= cGenericCaseDefs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
18
	toInt STE_Member				= cMemberDefs
19
	toInt STE_Instance				= cInstanceDefs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
20
21
	toInt STE_DclFunction			= cFunctionDefs
	toInt (STE_FunctionOrMacro _)	= cMacroDefs
22
	toInt (STE_DclMacroOrLocalMacroFunction _)= cMacroDefs
23
	toInt STE_GenericDeriveClass	= cGenericCaseDefs
24
	toInt STE_TypeExtension			= cTypeDefs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
	toInt _							= NoIndex

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

49
newPosition :: !Ident !Position -> IdentPos
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
50
51
52
53
54
55
56
57
58
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 = "???" }

59
60
61
62
63
64
65
66
67
68
stringPosition :: !String !Position -> StringPos
stringPosition id (FunPos file_name line_nr _)
	= { sp_name = id, sp_line = line_nr, sp_file = file_name }
stringPosition id (LinePos file_name line_nr)
	= { sp_name = id, sp_line = line_nr, sp_file = file_name }
stringPosition id (PreDefPos file_name)
	= { sp_name = id, sp_line = cNotALineNumber, sp_file = file_name.id_name }
stringPosition id NoPos
	= { sp_name = id, sp_line = cNotALineNumber, sp_file = "???" }

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
69
70
checkError :: !a !b !*ErrorAdmin -> *ErrorAdmin | <<< a & <<< b // PK
checkError id mess error=:{ea_file,ea_loc=[]}
Martin Wierich's avatar
Martin Wierich committed
71
	= { error & ea_file = ea_file <<< "Error " <<< " " <<< id <<< " " <<< mess <<< '\n', ea_ok = False }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
72
checkError id mess error=:{ea_file,ea_loc}
Martin Wierich's avatar
Martin Wierich committed
73
	= { error & ea_file = ea_file <<< "Error " <<< hd ea_loc <<< ": " <<< id  <<< " " <<< mess <<< '\n', ea_ok = False }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
74
75
76

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

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

85
86
87
88
89
checkErrorWithPosition :: !Ident !Position !a !*ErrorAdmin -> .ErrorAdmin | <<< a;
checkErrorWithPosition ident pos mess error=:{ea_file}
	# ident_pos = newPosition ident pos
	= { error & ea_file = ea_file <<< "Error " <<< ident_pos <<< ": " <<< mess <<< '\n', ea_ok = False }

90
91
92
93
94
checkStringErrorWithPosition :: !{#Char} !Position !a !*ErrorAdmin -> *ErrorAdmin | <<< a;
checkStringErrorWithPosition string pos mess error=:{ea_file}
	# string_pos = stringPosition string pos
	= { error & ea_file = ea_file <<< "Error " <<< string_pos <<< ": " <<< mess <<< '\n', ea_ok = False }

95
96
97
98
checkWarningWithPosition :: !Ident !Position !a !*ErrorAdmin -> .ErrorAdmin | <<< a;
checkWarningWithPosition ident pos mess error=:{ea_file}
	# ident_pos = newPosition ident pos
	= { error & ea_file = ea_file <<< "Warning " <<< ident_pos <<< ": " <<< mess <<< '\n' }
Martin Wierich's avatar
Martin Wierich committed
99

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
100
101
102
103
104
class envLookUp a :: !a !(Env Ident .b) -> (!Bool,.b)

instance envLookUp TypeVar
where
	envLookUp var [bind:binds]
105
		| var.tv_ident == bind.bind_src
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
106
107
108
109
110
111
112
113
			= (True, bind.bind_dst)
			= envLookUp var binds
	envLookUp var []
		= (False, abort "illegal value")
	
instance envLookUp AttributeVar
where
	envLookUp var [bind:binds]
114
		| var.av_ident == bind.bind_src
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
115
116
117
118
119
120
121
122
			= (True, bind.bind_dst)
			= envLookUp var binds
	envLookUp var []
		= (False, abort "illegal value")

instance envLookUp ATypeVar
where
	envLookUp var=:{atv_variable} [bind:binds]
123
		| atv_variable.tv_ident == bind.bind_src
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
124
125
126
127
128
			= (True, bind.bind_dst)
			= envLookUp var binds
	envLookUp var []
		= (False, abort "illegal value")

129
retrieveGlobalDefinition :: !SymbolTableEntry !STE_Kind !Index -> (!Index, !Index)
130
retrieveGlobalDefinition {ste_kind = STE_Imported kind decl_index, ste_def_level, ste_index} requ_kind mod_index
131
	| kind == requ_kind
132
		= (ste_index, decl_index)
133
134
135
136
137
138
		= (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)

Martin Wierich's avatar
Martin Wierich committed
139
getBelongingSymbols :: !Declaration !v:{#DclModule} -> (!BelongingSymbols, !v:{#DclModule})
140
getBelongingSymbols (Declaration {decl_kind=STE_Imported STE_Type def_mod_index, decl_index}) dcl_modules
141
	# (td_rhs,dcl_modules) = dcl_modules![def_mod_index].dcl_common.com_type_defs.[decl_index].td_rhs
142
143
144
145
146
	= case td_rhs of
		AlgType constructors
			-> (BS_Constructors constructors, dcl_modules)
		RecordType {rt_fields}
			-> (BS_Fields rt_fields, dcl_modules)
147
148
		NewType constructor
			-> (BS_Constructors [constructor], dcl_modules)
149
150
		_
			-> (BS_Nothing, dcl_modules)
151
getBelongingSymbols (Declaration {decl_kind=STE_Imported STE_Class def_mod_index, decl_index}) dcl_modules
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
182
183
184
185
186
	# ({class_members,class_macro_members},dcl_modules) = dcl_modules![def_mod_index].dcl_common.com_class_defs.[decl_index]
	# (members,dcl_modules) = add_default_class_member_macros 0 dcl_modules
	  with
		add_default_class_member_macros class_member_n dcl_modules
			| class_member_n<size class_members
				# member_index=class_members.[class_member_n].ds_index
				  ({me_default_implementation},dcl_modules)
					= dcl_modules![def_mod_index].dcl_common.com_member_defs.[member_index]
				= case me_default_implementation of
					No
						-> add_default_class_member_macros (class_member_n+1) dcl_modules
					Yes default_class_member_macro_ident_and_index
						#! n_members = size class_members
						# default_member_indexes = createArray n_members -1
						  default_member_indexes & [class_member_n] = 0
						-> add_more_default_class_member_macros (class_member_n+1) [|default_class_member_macro_ident_and_index] 1 default_member_indexes dcl_modules
				| size class_macro_members==0
					= (BS_Members class_members,dcl_modules)
					= (BS_MembersAndMacros class_members class_macro_members {} {},dcl_modules)

		add_more_default_class_member_macros class_member_n default_class_members default_member_n default_member_indexes dcl_modules
			| class_member_n<size class_members
				# member_index=class_members.[class_member_n].ds_index
				  ({me_default_implementation}, dcl_modules)
					= dcl_modules![def_mod_index].dcl_common.com_member_defs.[member_index]
				= case me_default_implementation of
					No
						-> add_more_default_class_member_macros (class_member_n+1) default_class_members default_member_n default_member_indexes dcl_modules
					Yes default_class_member_macro_ident_and_index
						# default_class_members = [|default_class_member_macro_ident_and_index:default_class_members]
  						  default_member_indexes & [class_member_n] = default_member_n
						-> add_more_default_class_member_macros (class_member_n+1) default_class_members (default_member_n+1) default_member_indexes dcl_modules
				# default_class_members = {default_class_member\\default_class_member<-reverse default_class_members}
				= (BS_MembersAndMacros class_members class_macro_members default_member_indexes default_class_members,dcl_modules)
	= (members,dcl_modules)
187
188
189
190
191
192
193
194
195
196
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
197
198
nrOfBelongingSymbols (BS_MembersAndMacros members macro_members _ default_macros)
	= size members+size macro_members+size default_macros
199
200
201
nrOfBelongingSymbols BS_Nothing
	= 0

202
removeImportsAndLocalsOfModuleFromSymbolTable :: !Declarations !*(Heap SymbolTableEntry) -> .Heap SymbolTableEntry
Martin Wierich's avatar
Martin Wierich committed
203
removeImportsAndLocalsOfModuleFromSymbolTable {dcls_import,dcls_local_for_import} symbol_table
204
	# symbol_table = remove_declared_symbols_in_array 0 dcls_import symbol_table
Martin Wierich's avatar
Martin Wierich committed
205
	= remove_declared_symbols_in_array 0 dcls_local_for_import symbol_table
206
where
John van Groningen's avatar
John van Groningen committed
207
	remove_declared_symbols_in_array :: !Int !{!Declaration} !*SymbolTable -> *SymbolTable
208
209
	remove_declared_symbols_in_array symbol_index symbols symbol_table
		| symbol_index<size symbols
210
			# symbol = symbols.[symbol_index]
211
			# (Declaration {decl_ident={id_info}})=symbol
212
			# (entry, symbol_table) = readPtr id_info symbol_table
213
214
215
216
217
218
			# {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
219
220
221
						#! 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)
222
					STE_Imported (STE_Field selector_id) def_mod
223
224
225
						#! 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)
226
227
228
229
					_
						-> remove_declared_symbols_in_array (symbol_index+1) symbols symbol_table
			= symbol_table

230
231
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
232
233
	| from_index == to_index
		= (fun_defs, symbol_table, error)	
234
		# (fun_def, fun_defs) = fun_defs![from_index]
235
		# (symbol_table, error) = addDefToSymbolTable level from_index fun_def.fun_ident (STE_FunctionOrMacro []) symbol_table error
236
		| is_macro_fun
237
			# fun_defs = {fun_defs & [from_index].fun_info.fi_properties = fun_def.fun_info.fi_properties bitor FI_IsMacroFun }
238
239
			= 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
240

241
242
243
244
245
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]
246
		# (symbol_table, error) = addDefToSymbolTable level from_index macro_def.fun_ident (STE_DclMacroOrLocalMacroFunction []) symbol_table error
247
248
249
		# 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
250
251
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 })
252

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
253
254
addDefToSymbolTable :: !Level !Index !Ident !STE_Kind !*SymbolTable !*ErrorAdmin -> (!* SymbolTable, !*ErrorAdmin)
addDefToSymbolTable level def_index def_ident=:{id_info} def_kind symbol_table error
255
	# (entry, symbol_table) = readPtr id_info symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
256
257
258
	| 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
259
		= (symbol_table, checkError def_ident "already defined" error)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
260

261
262
263
264
addDeclarationsOfDclModToSymbolTable :: Int !{!Declaration} !{!Declaration} !*{#DclModule} !*CheckState -> (!*{#DclModule},!*CheckState)
addDeclarationsOfDclModToSymbolTable ste_index locals imported dcl_modules cs
	# (dcl_modules,cs) = add_imports_in_array_to_symbol_table 0 imported dcl_modules cs
	= addLocalSymbolsForImportToSymbolTable 0 locals ste_index dcl_modules cs
Martin Wierich's avatar
Martin Wierich committed
265
  where
266
267
	add_imports_in_array_to_symbol_table :: !Int !{!Declaration} !*{#DclModule} !*CheckState -> (!*{#DclModule},!*CheckState)
	add_imports_in_array_to_symbol_table symbol_index symbols dcl_modules cs=:{cs_x}
Martin Wierich's avatar
Martin Wierich committed
268
		| symbol_index<size symbols
269
270
			#! (Declaration {decl_ident,decl_pos,decl_kind},symbols) = symbols![symbol_index]
			= case decl_kind of
Martin Wierich's avatar
Martin Wierich committed
271
				STE_Imported def_kind def_mod
272
273
					#! declaration = symbols.[symbol_index]
					# (Declaration {decl_index}) = declaration
274
275
					# (_,dcl_modules,cs) = addSymbol No decl_ident decl_pos decl_kind def_kind decl_index def_mod cUndef dcl_modules cs
					-> add_imports_in_array_to_symbol_table (symbol_index+1) symbols dcl_modules cs
Martin Wierich's avatar
Martin Wierich committed
276
				STE_FunctionOrMacro _
277
278
					#! declaration = symbols.[symbol_index]
					# (Declaration {decl_index}) = declaration
279
280
281
					# (_,dcl_modules,cs) = addImportedFunctionOrMacro No decl_ident decl_index dcl_modules cs
					-> add_imports_in_array_to_symbol_table (symbol_index+1) symbols dcl_modules cs
			= (dcl_modules,cs)
Martin Wierich's avatar
Martin Wierich committed
282

283
284
	addLocalSymbolsForImportToSymbolTable :: !Int !{!Declaration} Int !*{#DclModule} !*CheckState -> (!*{#DclModule},!*CheckState)
	addLocalSymbolsForImportToSymbolTable symbol_index symbols mod_index dcl_modules cs
Martin Wierich's avatar
Martin Wierich committed
285
		| symbol_index<size symbols
286
287
			# (Declaration {decl_ident,decl_pos,decl_kind,decl_index},symbols) = symbols![symbol_index]
			= case decl_kind of
Martin Wierich's avatar
Martin Wierich committed
288
				STE_FunctionOrMacro _
289
290
					# (_,dcl_modules,cs) = addImportedFunctionOrMacro No decl_ident decl_index dcl_modules cs
					-> addLocalSymbolsForImportToSymbolTable (symbol_index+1) symbols mod_index dcl_modules cs
Martin Wierich's avatar
Martin Wierich committed
291
				STE_Imported def_kind def_mod
292
293
294
295
296
297
					# (_,dcl_modules,cs) = addSymbol No decl_ident decl_pos decl_kind def_kind decl_index mod_index cUndef dcl_modules cs
					-> addLocalSymbolsForImportToSymbolTable (symbol_index+1) symbols mod_index dcl_modules cs
			= (dcl_modules,cs)

addImportedFunctionOrMacro :: !(Optional IndexRange) !Ident !Int !*{#DclModule} !*CheckState -> (!Bool,!*{#DclModule},!.CheckState)
addImportedFunctionOrMacro opt_dcl_macro_range ident=:{id_info} def_index dcl_modules cs=:{cs_symbol_table}
298
299
	# (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
300
301
	= case entry.ste_kind of
		STE_Empty
302
			-> (True, dcl_modules, { cs & cs_symbol_table = NewEntry cs.cs_symbol_table id_info (STE_FunctionOrMacro []) 
Martin Wierich's avatar
Martin Wierich committed
303
													def_index cModuleScope entry})
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
304
		STE_FunctionOrMacro _
Martin Wierich's avatar
Martin Wierich committed
305
			| entry.ste_index == def_index || within_opt_range opt_dcl_macro_range def_index
306
307
308
309
310
311
				-> (False, dcl_modules, cs)
		STE_Imported _ module_n
			| module_n>=0 && module_n<size dcl_modules
				# (dcl_name,dcl_modules) = dcl_modules![module_n].dcl_name
				  cs & cs_error = checkError ident ("multiply defined (also defined in module "+++toString dcl_name+++")") cs.cs_error
				-> (False, dcl_modules, cs)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
312
		_
313
			-> (False, dcl_modules, { cs & cs_error = checkError ident "multiply defined" cs.cs_error})
Martin Wierich's avatar
Martin Wierich committed
314
315
316
317
318
319
  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
320
321
322
323
324
325
326
327
328
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 }
329

330
331
addSymbol :: !(Optional a) !Ident !Position !STE_Kind !STE_Kind !.Int !.Int !Int !*{#DclModule} !*CheckState -> (!Bool,!*{#DclModule},!*CheckState)
addSymbol yes_for_icl_module ident pos decl_kind def_kind def_index def_mod importing_mod dcl_modules cs=:{cs_symbol_table}
clean's avatar
clean committed
332
	# (entry, cs_symbol_table) = readPtr ident.id_info cs_symbol_table
333
	= add_indirectly_imported_symbol yes_for_icl_module entry ident pos def_kind def_index def_mod 
334
			importing_mod dcl_modules {cs & cs_symbol_table = cs_symbol_table}
clean's avatar
clean committed
335
	where
336
		add_indirectly_imported_symbol _ {ste_kind = STE_Empty} {id_info} _ def_kind def_index def_mod _ dcl_modules cs=:{cs_symbol_table}
clean's avatar
clean committed
337
			# (entry, cs_symbol_table) = readPtr id_info cs_symbol_table
338
			  cs = { cs & cs_symbol_table = NewEntry cs_symbol_table id_info decl_kind def_index cModuleScope entry}
clean's avatar
clean committed
339
340
			= case def_kind of
				STE_Field selector_id
341
					-> (True, dcl_modules, addFieldToSelectorDefinition selector_id	{ glob_module = def_mod, glob_object = def_index } cs)
clean's avatar
clean committed
342
				_
343
344
					-> (True, dcl_modules, cs)
		add_indirectly_imported_symbol _ {ste_kind = STE_Imported kind mod_index, ste_index} _ _ def_kind def_index def_mod _ dcl_modules cs
clean's avatar
clean committed
345
			| kind == def_kind && mod_index == def_mod && ste_index == def_index
346
347
				= (False, dcl_modules, cs)
		add_indirectly_imported_symbol (Yes _) _ _ _ def_kind def_index def_mod _ dcl_modules cs
348
349
			| def_mod == cs.cs_x.x_main_dcl_module_n
				// an icl module imports one of it's definitions from the dcl module
350
351
				= (False, dcl_modules, cs)
		add_indirectly_imported_symbol _ _ _ _ def_kind def_index def_mod importing_mod dcl_modules cs
352
			| importing_mod==def_mod // a dcl module imports a definition from itself (cycle)
353
354
355
356
357
358
359
360
				= (False, dcl_modules, cs)
		add_indirectly_imported_symbol _ {ste_kind = STE_Imported _ mod_index, ste_index} _ _ def_kind def_index def_mod _ dcl_modules cs=:{cs_error}
			| mod_index>=0 && mod_index<size dcl_modules && def_mod>=0 && def_mod<size dcl_modules
				# (dcl_name1,dcl_modules) = dcl_modules![def_mod].dcl_name
				  (dcl_name2,dcl_modules) = dcl_modules![mod_index].dcl_name
				  cs & cs_error = checkError ident ("multiply defined (in module "+++toString dcl_name1+++" and already in module "+++toString dcl_name2+++")") cs_error
				= (False, dcl_modules, cs)
		add_indirectly_imported_symbol _ entry ident pos def_kind def_index def_mod _ dcl_modules cs=:{cs_error}
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
			= case decl_kind of
				STE_Imported ste_kind mod_index
					| mod_index>=0 && mod_index<size dcl_modules
						# ({dcl_name,dcl_common={com_gencase_defs}},dcl_modules) = dcl_modules![mod_index]
						-> case (ste_kind,entry) of
							(STE_GenericDeriveClass,{ste_kind=STE_GenericDeriveClass,ste_index})
								| ste_index>=0 && ste_index<size com_gencase_defs
									# gc_type_cons = com_gencase_defs.[ste_index].gc_type_cons
									# ident = "derived generic instances for class "+++ident.id_name+++" for type "+++type_cons_to_string gc_type_cons
									-> (False, dcl_modules, {cs & cs_error = checkError ident ("multiply defined (also imported from module "+++toString dcl_name+++")") cs_error})
							(STE_Instance,{ste_kind=STE_Instance,ste_index})
								-> (False, dcl_modules, {cs & cs_error = checkError ("instance of class "+++toString ident) ("multiply defined (also imported from module "+++toString dcl_name+++")") cs_error})
							_
								-> (False, dcl_modules, {cs & cs_error = checkError ident ("multiply defined (also imported from module "+++toString dcl_name+++")") cs_error})
				_
					# cs_error = case entry.ste_kind of
									STE_GenericDeriveClass
										-> checkError ("derive of class "+++toString ident) "multiply defined" cs_error
									STE_Instance
										-> checkError ("instance of class "+++toString ident) "multiply defined" cs_error
									_
										-> checkError ident "multiply defined" cs_error
					-> (False, dcl_modules, {cs & cs_error=cs_error})
384
385

addGlobalDefinitionsToSymbolTable :: ![Declaration] !*CheckState -> .CheckState;
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
386
387
388
addGlobalDefinitionsToSymbolTable decls cs
	= foldSt add_global_definition decls cs
where
389
	add_global_definition (Declaration {decl_ident=ident=:{id_info},decl_pos,decl_kind,decl_index}) cs=:{cs_symbol_table}
390
		# (entry, cs_symbol_table) = readPtr id_info cs_symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
391
		| entry.ste_def_level < cGlobalScope
392
393
			# 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
394
				STE_Field selector_id
395
					-> addFieldToSelectorDefinition selector_id	{ glob_module = NoIndex, glob_object = decl_index } cs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
396
397
				_
					-> cs
398
			= { cs & cs_symbol_table = cs_symbol_table, cs_error = checkErrorWithIdentPos (newPosition ident decl_pos) "multiply defined" cs.cs_error}
399

400
removeImportedSymbolsFromSymbolTable :: Declaration !*SymbolTable -> .SymbolTable
401
removeImportedSymbolsFromSymbolTable (Declaration {decl_ident=decl_ident=:{id_info}, decl_index}) symbol_table
402
403
	# ({ste_kind,ste_def_level,ste_previous}, symbol_table) = readPtr id_info symbol_table
	  symbol_table = symbol_table <:= (id_info, ste_previous)
404
405
	= case ste_kind of
		STE_Imported (STE_Field selector_id) def_mod
406
			-> removeFieldFromSelectorDefinition selector_id def_mod decl_index symbol_table
407
408
409
		_
			-> symbol_table

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
410
411
412
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
413
414
415
416
	= 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
417
418
419
420
421
422
423
424
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 []
		= []

425
removeDeclarationsFromSymbolTable :: ![Declaration] !Int !*SymbolTable -> *SymbolTable
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
426
removeDeclarationsFromSymbolTable decls scope symbol_table
Martin Wierich's avatar
Martin Wierich committed
427
	= foldSt (remove_declaration scope) decls symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
428
where
429
	remove_declaration scope decl=:(Declaration {decl_ident={id_info}, decl_index}) symbol_table
430
		# ({ste_kind,ste_previous,ste_def_level}, symbol_table) = readPtr id_info symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
431
		= case ste_kind of
432
433
			STE_Field selector_id
				# symbol_table = removeFieldFromSelectorDefinition selector_id NoIndex decl_index symbol_table
434
435
				| ste_def_level<>scope && scope==cGlobalScope
					-> symbol_table
436
437
438
439
440
441
				| ste_previous.ste_def_level == scope
					-> symbol_table <:= (id_info, ste_previous.ste_previous)
					-> symbol_table <:= (id_info, ste_previous)
			STE_Empty
				-> symbol_table
			_
442
443
				| ste_def_level<>scope && scope==cGlobalScope
					-> symbol_table
444
445
446
				| 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
447
448
449
450
451
452
453

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
454
	# ({ste_previous,ste_def_level}, symbol_table) = readPtr id_info symbol_table
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
455
456
457
458
	| level <= ste_def_level 
		= symbol_table <:= (id_info,ste_previous) // ---> ("removeIdentFromSymbolTable", id_name)
		= symbol_table // ---> ("NO removeIdentFromSymbolTable", id_name)

459
460
461
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
462
where
463
464
465
	remove_macro_defs_from_symbol_table level from_index to_index defs symbol_table
		| from_index == to_index
			= (defs, symbol_table)	
466
467
468
			# (def,defs) = defs![module_index,from_index]
			  id_info = (toIdent def).id_info
			  (entry,symbol_table) = readPtr id_info symbol_table
469
470
471
472
473
474
475
476
477
			| 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
478
479
		| from_index == to_index
			= (defs, symbol_table)	
480
481
			# (def,defs) = defs![from_index]
			  id_info = (toIdent def).id_info
482
			#  (entry, symbol_table) = readPtr id_info symbol_table
483
			| level == entry.ste_def_level
484
485
				= 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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500

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

501
local_declaration_for_import :: !u:Declaration .Index -> v:Declaration, [u <= v]
502
local_declaration_for_import decl=:(Declaration {decl_kind=STE_FunctionOrMacro _}) module_n
503
	= decl
504
local_declaration_for_import decl=:(Declaration {decl_kind=STE_Imported _ _}) module_n
505
	= abort "local_declaration_for_import"
506
507
local_declaration_for_import decl=:(Declaration declaration_record=:{decl_kind}) module_n
	= Declaration {declaration_record & decl_kind = STE_Imported decl_kind module_n}
508

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
509
510
instance toIdent SymbIdent
where
511
	toIdent symb = symb.symb_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
512
513
514

instance toIdent TypeSymbIdent
where
515
	toIdent type_symb = type_symb.type_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
516
517
518

instance toIdent BoundVar
where
519
	toIdent var = var.var_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
520
521
522

instance toIdent TypeVar
where
523
	toIdent tvar = tvar.tv_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
524
525
526

instance toIdent ATypeVar
where
527
	toIdent {atv_variable} = atv_variable.tv_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
528
529
530
531
532
533
534

instance toIdent Ident
where
	toIdent id = id

instance toIdent ConsDef
where
535
	toIdent cons = cons.cons_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
536

537
instance toIdent (TypeDef a)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
538
where
539
	toIdent td = td.td_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
540
541
542

instance toIdent ClassDef
where
543
	toIdent cl = cl.class_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
544
545
546

instance toIdent MemberDef
where
547
	toIdent me = me.me_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
548
549
550

instance toIdent FunDef
where
551
	toIdent fun = fun.fun_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
552
553
554

instance toIdent SelectorDef
where
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
555
	toIdent sd = sd.sd_ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
556
557
558
559
560
561
562
563

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

instance == STE_Kind
where
	(==) (STE_FunctionOrMacro _) STE_DclFunction	= True
564
	(==) (STE_FunctionOrMacro _) (STE_DclMacroOrLocalMacroFunction _) = True
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
565
	(==) STE_DclFunction (STE_FunctionOrMacro _)	= True
566
	(==) (STE_DclMacroOrLocalMacroFunction _) (STE_FunctionOrMacro _)	= True
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
567
568
569
570
571
572
573
574
575
	(==) 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 <<< ']'

576
577
578
579
580
581
instance <<< StringPos where
	(<<<) file {sp_file,sp_line,sp_name}
	| sp_line == cNotALineNumber
		= file <<< '[' <<< sp_file <<< ',' <<< sp_name <<< ']'
		= file <<< '[' <<< sp_file <<< ',' <<< sp_line <<< ',' <<< sp_name <<< ']'

582
583
584
import_ident :: Ident
import_ident =: { id_name = "import", id_info = nilPtr }

585
586
587
588
589
590
591
restoreIdentsSymbolPtrs :: ![Ident] !*SymbolTable -> *SymbolTable
restoreIdentsSymbolPtrs idents symbol_table = foldSt restoreIdentSymbolPtr idents symbol_table

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