syntax.dcl 50.4 KB
Newer Older
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
1 2 3 4 5
definition module syntax

import StdEnv

import scanner, general, typeproperties, Heap
6
import IndexType
7
from containers import ::NumberSet
8
from convertcases import :: LetVarInfo, :: LetExpressionInfo, :: RefCountsInCase, :: SplitsInCase
9

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
::	Ident =
	{ 	id_name		:: !String
	,	id_info 	:: !SymbolPtr
	}

instance toString Ident

/*	Each Identifier is equipped with a pointer to a SymbolTableEntry that is
	used for binding the identifier with its definition.
*/

::	SymbolTable			:== Heap SymbolTableEntry
::	SymbolPtr 			:== Ptr SymbolTableEntry

::	SymbolTableEntry = 
	{	ste_kind		:: !STE_Kind
	,	ste_index		:: !Index
	,	ste_def_level	:: !Level
	,	ste_previous	:: SymbolTableEntry
	}

31 32 33 34
:: FunctionOrMacroIndex = FunctionOrIclMacroIndex !Int | DclMacroIndex /*module_n*/ !Int /*macro_n_in_module*/ !Int;

instance == FunctionOrMacroIndex

35
::	STE_BoundTypeVariable	= { stv_attribute :: !TypeAttribute, stv_info_ptr :: !TypeVarInfoPtr }
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
36

37 38
::	STE_Kind	= STE_FunctionOrMacro ![FunctionOrMacroIndex]
				| STE_DclMacroOrLocalMacroFunction ![FunctionOrMacroIndex]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
39 40 41 42 43 44
				| STE_Type
				| STE_Constructor
				| STE_Selector ![Global Index]
				| STE_Field !Ident
				| STE_Class
				| STE_Member
45 46
				| STE_Generic
				| STE_GenericCase
47
				| STE_GenericDeriveClass
48
				| STE_Instance
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
49 50 51 52
				| STE_Variable !VarInfoPtr
				| STE_TypeVariable !TypeVarInfoPtr
				| STE_TypeAttribute !AttrVarInfoPtr
				| STE_BoundTypeVariable !STE_BoundTypeVariable
53
				| STE_Imported !STE_Kind !ModuleN
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
54
				| STE_DclFunction
55
				| STE_Module !(Module (CollectedDefinitions ClassInstance))
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
56
				| STE_ClosedModule
57
				| STE_ModuleQualifiedImports !SortedQualifiedImports
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
58 59 60 61 62
				| STE_Empty
					/* for creating class dictionaries */
				| STE_DictType !CheckedTypeDef
				| STE_DictCons !ConsDef
				| STE_DictField !SelectorDef
63
				| STE_Called ![FunctionOrMacroIndex] /* used during macro expansion to indicate that this function is called */
64
				| STE_ExplImpSymbol !Int
65
				| STE_ExplImpComponentNrs ![ComponentNrAndIndex]
66
					/*	stores the numbers of all module components that import the symbol from
67
						the "actual" dcl module.
68 69
					*/
				| STE_BelongingSymbol !Int
70 71 72 73 74 75
				| STE_ExplImpSymbolNotImported !ModuleN !STE_Kind
				| STE_ImportedQualified !Declaration !STE_Kind

				| STE_UsedType !ModuleN !STE_Kind
					/* used during binding of types to mark types that have been applied. */
				| STE_UsedQualifiedType !ModuleN !Index !STE_Kind
76 77
				| STE_BelongingSymbolExported
				| STE_BelongingSymbolForExportedSymbol
78
				| STE_TypeExtension
79

80 81 82 83
::	ModuleN:==Int;

::	SortedQualifiedImports	= SortedQualifiedImports !Declaration !SortedQualifiedImports !SortedQualifiedImports
							| EmptySortedQualifiedImports
84

85 86 87 88 89 90 91
::	Declaration = Declaration !DeclarationRecord

::	DeclarationRecord =
	{	decl_ident	:: !Ident
	,	decl_pos	:: !Position
	,	decl_kind	:: !STE_Kind
	,	decl_index	:: !Index
92 93 94 95 96 97 98
	}

::	ComponentNrAndIndex =
	{	cai_component_nr	:: !Int
	,	cai_index			:: !Int // points into ExplImpInfos
	}

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
99 100 101 102
::	Global object =
	{	glob_object	:: !object
	,	glob_module	:: !Index
	}
103

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
104
::	Module defs = 
105
	{	mod_ident		:: !Ident
106
	,	mod_modification_time		:: {#Char}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
107 108 109
	,	mod_type		:: !ModuleKind
	, 	mod_imports		:: ![ParsedImport]
	,	mod_imported_objects :: ![ImportedObject]
110
	,	mod_foreign_exports :: ![ParsedForeignExport]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
111 112 113
	,	mod_defs		:: !defs
	}

114 115 116 117 118 119 120
::	ParsedForeignExport =
	{	pfe_ident	:: !Ident
	,	pfe_line	:: !Int
	,	pfe_file	:: !FileName
	,	pfe_stdcall	:: !Bool
	}

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
121
::	ParsedModule	:== Module  [ParsedDefinition]
122
::	ScannedModule 	:== Module  (CollectedDefinitions (ScannedInstanceAndMembersR FunDef))
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
123
	
124
::	ModuleKind		= MK_Main | MK_Module | MK_System | MK_None | MK_NoMainDcl
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
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
::	FunDefIndex:==Int

::	IclFunctionIndices =
	{	ifi_global_function_indices	:: ![IndexRange]
	,	ifi_local_function_indices	:: !IndexRange
	,	ifi_instance_indices		:: ![IndexRange]
	,	ifi_specials_indices		:: !IndexRange
	,	ifi_gencase_indices			:: ![IndexRange]
	,	ifi_type_function_indices	:: ![IndexRange]
	}

::	IclModule  =
	{	icl_name				:: !Ident
	,	icl_functions			:: !.{# FunDef }
	,	icl_function_indices	:: !IclFunctionIndices
	,	icl_common				:: !.CommonDefs
	,	icl_import				:: !{!Declaration}
	,	icl_qualified_imports	:: ![([Declaration], ModuleN, Position)]
	,	icl_imported_objects	:: ![ImportedObject]
	,	icl_foreign_exports		:: ![ForeignExport]
	,	icl_used_module_numbers :: !NumberSet
	,	icl_modification_time	:: !{#Char}
	}

::	DclModule =
	{	dcl_name			:: !Ident
	,	dcl_functions		:: !{# FunType }
	,	dcl_instances		:: !IndexRange
	,	dcl_macros			:: !IndexRange
	,	dcl_specials		:: !IndexRange
	,	dcl_gencases		:: !IndexRange
	,	dcl_type_funs		:: !IndexRange
	,	dcl_common			:: !CommonDefs
	,	dcl_sizes			:: !{# Int}
	,	dcl_dictionary_info	:: !DictionaryInfo
	,	dcl_declared		:: !Declarations
162
	,	dcl_has_macro_conversions :: !Bool
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	,	dcl_module_kind		:: !ModuleKind
	,	dcl_modification_time:: !{#Char}
	,	dcl_imported_module_numbers :: !NumberSet
	}

:: ForeignExport = {fe_fd_index :: !FunDefIndex, fe_stdcall :: !Bool}

::	CopiedDefinitions =
	{	copied_type_defs	:: {#Bool}
	,	copied_class_defs	:: {#Bool}
	,	copied_generic_defs :: {#Bool}
	}

::	Declarations = {
		dcls_import	::!{!Declaration}
	,	dcls_local		::![Declaration]
	,	dcls_local_for_import ::!{!Declaration}
	}

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

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
184
::	RhsDefsOfType	= ConsList ![ParsedConstructor]
185
					| SelectorList !Ident ![ATypeVar] !Bool /*is_boxed_record*/ ![ParsedSelector]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
186
					| TypeSpec !AType
John van Groningen's avatar
John van Groningen committed
187
					| NewTypeCons !ParsedConstructor
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
188
					| EmptyRhs !BITVECT
189
					| AbstractTypeSpec !BITVECT !AType
190
					| ExtensibleConses ![ParsedConstructor]
191
					| MoreConses !Ident ![ParsedConstructor]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
192

193
::	CollectedDefinitions instance_kind =
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
194
	{	def_types 			:: ![TypeDef TypeRhs]
195 196
	,	def_constructors	:: ![ConsDef]
	,	def_selectors		:: ![SelectorDef]
197 198
	,	def_macros			:: ![FunDef]
	,	def_macro_indices	:: !IndexRange
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
199 200 201 202
	,	def_classes			:: ![ClassDef]
	,	def_members			:: ![MemberDef]
	,	def_funtypes		:: ![FunType]
	,	def_instances		:: ![instance_kind]
203 204 205 206 207 208 209 210 211 212 213 214 215
	,	def_generics		:: ![GenericDef]
	, 	def_generic_cases 	:: ![GenericCaseDef]
	}

::	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}
	,	com_generic_defs	:: !.{# GenericDef}
	,	com_gencase_defs 	:: !.{# GenericCaseDef}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
216 217 218 219 220 221 222
	}

::	LocalDefs	= LocalParsedDefs [ParsedDefinition]
				| CollectedLocalDefs CollectedLocalDefs

::	IndexRange	= { ir_from :: !Index, ir_to :: !Index }

223 224 225 226 227 228 229
::	ArrayAndListInstances = {
		ali_array_first_instance_indices :: ![Int],
		ali_list_first_instance_indices :: ![Int],
		ali_tail_strict_list_first_instance_indices :: ![Int],
		ali_instances_range :: !IndexRange
	}

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
230 231 232 233 234 235 236 237
NoIndex		:== -1


::  Level	:== Int
NotALevel 	:==  -1

::	CollectedLocalDefs =
	{	loc_functions	:: !IndexRange
238
	,	loc_nodes		:: ![NodeDef ParsedExpr]
239
	,	loc_in_icl_module :: !Bool // False for local functions in macros in dcl modules, otherwise True
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
240 241 242 243 244
	}

::	NodeDef dst =
	{	nd_dst		::!dst,
		nd_alts		::!OptGuardedAlts,
245 246
		nd_locals	::!LocalDefs,
		nd_position	::!Position		
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
247 248 249 250 251 252 253 254 255 256 257 258
	}

::	Rhs =
	{	rhs_alts	:: !OptGuardedAlts
	,	rhs_locals	:: !LocalDefs
	}


cIsAFunction	:== True
cIsNotAFunction :== False

::	ParsedDefinition 
259 260
	=	PD_Function Position Ident Bool [ParsedExpr] Rhs FunKind
	|	PD_NodeDef Position ParsedExpr Rhs
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
261
	|	PD_Type ParsedTypeDef
262
	|	PD_TypeSpec Position Ident Priority (Optional SymbolType) FunSpecials
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
263
	|	PD_Class ClassDef [ParsedDefinition]
264 265
	|	PD_Instance ParsedInstanceAndMembers
	|	PD_Instances [ParsedInstanceAndMembers]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
266 267
	|	PD_Import [ParsedImport]
	|	PD_ImportedObjects [ImportedObject]
268
	|	PD_ForeignExport !Ident !{#Char} !Int !Bool /* if stdcall */
269
	|	PD_Generic GenericDef
270
	| 	PD_GenericCase GenericCaseDef Ident
271
	|	PD_Derive [GenericCaseDef]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
272 273
	|	PD_Erroneous

274
::	FunKind = FK_Function !Bool | FK_Macro | FK_Caf | FK_NodeDefOrFunction | FK_Unknown
275

276 277
::	StrictnessList = NotStrict | Strict !Int | StrictList !Int StrictnessList

278 279
cNameNotLocationDependent :== False
cNameLocationDependent :== True
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
280 281

::	ParsedSelector =
282 283
	{	ps_field_ident		:: !Ident
	,	ps_selector_ident	:: !Ident
284
	,	ps_field_annotation	:: !Annotation
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
285 286 287 288 289 290
	,	ps_field_type		:: !AType
	,	ps_field_var		:: !Ident
	,	ps_field_pos		:: !Position
	}

::	ParsedConstructor =
291
	{	pc_cons_ident 	:: !Ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
292 293 294
	,	pc_cons_arity	:: !Int
	,	pc_exi_vars		:: ![ATypeVar]
	,	pc_arg_types	:: ![AType]
295
	,	pc_args_strictness	:: !StrictnessList
296
	,	pc_context		:: ![TypeContext]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
297 298 299
	,	pc_cons_prio	:: !Priority
	,	pc_cons_pos		:: !Position
	}
300 301

::	ParsedInstance =
302
	{	pi_class 	:: !IdentOrQualifiedIdent
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
303 304 305 306 307 308 309
	,	pi_ident	:: !Ident
	,	pi_types	:: ![Type]
	,	pi_context	:: ![TypeContext]
	,	pi_pos		:: !Position
	,	pi_specials	:: !Specials
	}

310 311 312 313 314 315 316 317 318 319 320
::	ParsedInstanceAndMembers =
	{	pim_pi 		:: !ParsedInstance
	,	pim_members	:: ![ParsedDefinition]
	}

::	ScannedInstanceAndMembersR icl_member =
	{	sim_pi 				:: !ParsedInstance
	,	sim_members			:: ![icl_member]	// for .icl
	,	sim_member_types	:: ![FunType]	// for .dcl
	}

321 322 323 324
::	IdentOrQualifiedIdent
	= Ident !Ident
	| QualifiedIdent /*module*/!Ident !String

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
325 326 327 328 329
/*
	Objects of type Specials are used to specify specialized instances of overloaded functions.
	These can only occur in definition modules. After parsing the SP_ParsedSubstitutions alternative
	is used to indicate the specific instantiation. The SP_Substitutions alternative is used to deduce
	the type of the specialized version. Finally the SP_ContextTypes alternative is set and used during 
330
	the typing to check whether this instance has been used. The auxiliary FSP_FunIndex alternative is used
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
331 332 333 334 335 336 337
	to store the index of the function that has been specialized.
*/

::	Specials
	= SP_ParsedSubstitutions 	![Env Type TypeVar]
	| SP_Substitutions 		 	![SpecialSubstitution]
	| SP_ContextTypes			![Special]
338
	| SP_TypeOffset				!Int					// index in SP_Substitutions for specialized instance
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
339 340
	| SP_None

341 342
::	FunSpecials
	= FSP_ParsedSubstitutions 	![Env Type TypeVar]
343
	| FSP_Substitutions			![SpecialSubstitution]
344 345 346 347
	| FSP_ContextTypes			![Special]
	| FSP_FunIndex				!Index
	| FSP_None

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
::	SpecialSubstitution =
	{	ss_environ	:: !Env Type TypeVar
	,	ss_context	:: ![TypeContext]
	,	ss_vars		:: ![TypeVar]
	,	ss_attrs	:: ![AttributeVar]
	}

::	Special =
	{	spec_index	:: !Global Index
	,	spec_types	:: ![[Type]]
	,	spec_vars	:: ![TypeVar]
	, 	spec_attrs	:: ![AttributeVar]
	}

::	AttrInequality =
	{	ai_demanded :: !AttributeVar
	,	ai_offered	:: !AttributeVar
	}

::	DefinedSymbol = 
	{	ds_ident		:: !Ident
	,	ds_arity		:: !Int
	,	ds_index		:: !Index
	}

::	ClassDef =
374
 	{	class_ident			:: !Ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
375 376 377 378 379 380 381 382 383
	,	class_arity			:: !Int
	,	class_args			:: ![TypeVar]
	,	class_context		:: ![TypeContext]
	,	class_members		:: !{# DefinedSymbol}
	,	class_dictionary	:: !DefinedSymbol
	,	class_pos			:: !Position
	,	class_cons_vars		:: !BITVECT
	}

384 385
::	ClassDefInfos :== {# .{! [TypeKind]}}

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
386
::	MemberDef =
John van Groningen's avatar
John van Groningen committed
387
	{	me_ident		:: !Ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
388 389 390 391 392 393 394 395 396
	,	me_class		:: !Global Index
	,	me_offset		:: !Index
	,	me_type			:: !SymbolType
	,	me_type_ptr		:: !VarInfoPtr
	,	me_class_vars	:: ![TypeVar]
	,	me_pos			:: !Position
	,	me_priority 	:: !Priority
	}

397
:: GenericDef = 
John van Groningen's avatar
John van Groningen committed
398
	{	gen_ident		:: !Ident		// the generics name in IC_Generic
399
	,	gen_member_ident	:: !Ident	// the generics name in IC_Expression
400
	, 	gen_pos			:: !Position
401 402
	,	gen_type		:: !SymbolType	// Generic type (st_vars include generic type vars)
	,	gen_vars		:: ![TypeVar]	// Generic type variables
403
	,	gen_deps		:: ![GenericDependency]	   // Generic function dependencies
404
	,	gen_info_ptr	:: !GenericInfoPtr
405
	}
Artem Alimarine's avatar
Artem Alimarine committed
406

407 408 409 410 411 412 413 414 415
:: GenericDependency =
	{	gd_ident		:: !IdentOrQualifiedIdent
	, 	gd_index		:: !GlobalIndex
	, 	gd_vars			:: ![TypeVar]
	,	gd_nums			:: ![Int] // Mapping from dependency variable to generic type variable
	}

instance == GenericDependency

416 417 418 419 420 421 422 423 424 425 426
:: GenericClassInfo = 
	{	gci_kind	:: !TypeKind	// the kind
	,	gci_module 	:: !Index		// filled with main_module_index
	,	gci_class	:: !Index		// class_index in the main module
	,	gci_member	:: !Index 		// the class member index 
	}
:: GenericClassInfos :== {[GenericClassInfo]}

:: GenericInfo = 
	{	gen_classes		:: !GenericClassInfos
	,	gen_var_kinds	:: ![TypeKind]  	// kinds of all st_vars of the gen_type
427 428
	,	gen_rep_conses	:: !{!GenericRepresentationConstructor}
	//	OBJECT, CONS, RECORD, FIELD, PAIR, EITHER, UNIT
429
	}
430

431
::	GenericRepresentationConstructor =
432 433 434 435 436 437 438
	{	grc_module	:: !Int
	,	grc_index	:: !GenericCaseBody // GCB_FunIndex, GCB_FunAndMacroIndex or GCB_None
	,	grc_local_fun_index :: !Int
	,	grc_ident	:: !Ident
	,	grc_generic_info :: !Int
	,	grc_generic_instance_deps :: !GenericInstanceDependencies
	,	grc_optional_fun_type :: !Optional SymbolType
439 440
	}

441 442 443 444 445 446 447 448 449
:: GenericInfoPtr :== Ptr GenericInfo	
:: GenericHeap :== Heap GenericInfo

:: TypeCons 
	= TypeConsSymb TypeSymbIdent 
	| TypeConsBasic BasicType 
	| TypeConsArrow
	| TypeConsVar TypeVar 

450 451
:: GenericCaseDef =
	{	gc_pos			:: !Position			// position in the source file
452 453
	,	gc_type			:: !Type				// the instance type
	,   gc_type_cons	:: !TypeCons			// type constructor of the type argument
454
	,	gc_gcf			:: !GenericCaseFunctions
455
	}
456 457 458

::	GenericCaseFunctions
	= GCF !Ident !GCF
459 460
	| GCFS ![!GCF!]
	| GCFC !Ident !Ident	// IC_GenericDeriveClass IC_Class
461 462

::	GCF = {
463
		gcf_gident	:: !Ident,	  			// name in IC_Generic namespace
464 465
		gcf_generic	:: !GlobalIndex,		// index of the generic
		gcf_arity	:: !Int,				// arity of the function
466
		gcf_generic_info :: !Int,			// 0 = no, -1 = all, generic info for CONS, OBJECT, RECORD or FIELD
467
		gcf_body	:: !GenericCaseBody,	// the body function or NoIndex
468 469
		gcf_kind	:: !TypeKind,			// kind of the instance type
		gcf_generic_instance_deps :: !GenericInstanceDependencies
470 471
	}

472 473
:: GenericCaseBody 
	= GCB_None 									// to be generated
474 475 476 477 478 479 480 481 482 483
	| GCB_FunIndex !Index
	| GCB_FunAndMacroIndex !Index !Index
	| GCB_MacroIndex !Index
	| GCB_FunDef !FunDef
	| GCB_ParsedBody ![ParsedExpr] !Rhs

::	GenericInstanceDependencies
	= AllGenericInstanceDependencies
	| GenericInstanceDependencies !Int /*n_deps*/ !Int /*deps*/
	| GenericInstanceUsedArgs !Int /*n_deps*/ !Int /*deps*/
484

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
485 486 487 488 489 490 491 492
::	InstanceType =
	{	it_vars			:: [TypeVar]
	,	it_types		:: ![Type]
	,	it_attr_vars	:: [AttributeVar]
	,	it_context		:: ![TypeContext]
	}

::	ClassInstance =
John van Groningen's avatar
John van Groningen committed
493 494
 	{	ins_class_index	:: !GlobalIndex
	,	ins_class_ident	:: !ClassIdent
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
495 496
	,	ins_ident		:: !Ident
	,	ins_type		:: !InstanceType
497
	,	ins_member_types :: ![FunType]	// for .dcl
498
	,	ins_members		:: !{#ClassInstanceMember}
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
499 500 501 502
	,	ins_specials	:: !Specials
	,	ins_pos			:: !Position
	}

John van Groningen's avatar
John van Groningen committed
503
::	ClassIdent =
504
	{	ci_ident		:: !IdentOrQualifiedIdent
John van Groningen's avatar
John van Groningen committed
505 506 507
	,	ci_arity		:: !Int
	}

508 509 510 511 512 513
::	ClassInstanceMember = 
	{	cim_ident		:: !Ident
	,	cim_arity		:: !Int		// module number if cim_index<0
	,	cim_index		:: !Index	// or -1-index
	}

514
::	Import =
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
515
	{	import_module		:: !Ident
516
	,	import_symbols		:: !ImportSymbols [ImportDeclaration]
Martin Wierich's avatar
Martin Wierich committed
517
	,	import_file_position:: !Position	// for error messages
518
	,	import_qualified	:: !ImportQualified
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
519 520
	}

521 522 523 524
::	ImportSymbols import_declarations
	= ImportSymbolsAll
	| ImportSymbolsOnly !import_declarations

525 526
::	ImportQualified = NotQualified | Qualified

527
instance toString Import, AttributeVar, TypeAttribute, Annotation
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
528

529
::	ParsedImport		:== Import
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
530

531 532 533 534 535
::	ImportDeclaration	= ID_Function !Ident
						| ID_Class !Ident !(Optional [Ident])
						| ID_Type !Ident !(Optional [Ident])
						| ID_Record !Ident !(Optional [Ident])
						| ID_Instance !Ident !Ident !(![Type],![TypeContext])
536
						| ID_Generic !Ident !Ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
537 538 539

cIsImportedLibrary :== True
cIsImportedObject :== False
540

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
541 542 543 544 545 546 547 548
:: ImportedObject =
	{	io_is_library :: !Bool
	,	io_name    :: !{#Char}
	}

::	RecordType =
	{	rt_constructor	:: !DefinedSymbol
	,	rt_fields		:: !{# FieldSymbol}
549
	,	rt_is_boxed_record :: !Bool
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
550
	}
551

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
552
::	FieldSymbol =
553
	{	fs_ident		:: !Ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
554 555 556 557 558 559 560
	,	fs_var			:: !Ident
	,	fs_index		:: !Index
	}

::	TypeRhs	= AlgType ![DefinedSymbol]
			| SynType !AType
			| RecordType !RecordType
John van Groningen's avatar
John van Groningen committed
561
			| NewType !DefinedSymbol
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
562
			| AbstractType !BITVECT
563
			| AbstractSynType !BITVECT !AType
564
			| ExtensibleAlgType ![DefinedSymbol]
565 566
			| AlgConses ![DefinedSymbol] !GlobalIndex
			| UncheckedAlgConses !Ident ![DefinedSymbol]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
567 568 569 570 571 572 573 574
			| UnknownType

::	ParsedTypeDef	:== TypeDef RhsDefsOfType
::	CheckedTypeDef	:== TypeDef TypeRhs

cAllBitsClear			:== 0
cIsHyperStrict			:== 1
cIsNonCoercible			:== 2
575 576 577 578 579 580
cIsAnalysed				:== 4

::	GlobalIndex =
	{	gi_module	::!Int
	,	gi_index	::!Int
	}
581
NoGlobalIndex :== {gi_module=NoIndex,gi_index=NoIndex}	
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
582 583

::	TypeDef type_rhs =
584
	{	td_ident		:: !Ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
585 586 587 588 589 590 591
	,	td_index		:: !Int
	,	td_arity		:: !Int
	,	td_args			:: ![ATypeVar]
	,	td_attrs		:: ![AttributeVar]
	,	td_rhs			:: !type_rhs
	,	td_attribute	:: !TypeAttribute
	,	td_pos			:: !Position
592
	,	td_used_types	:: ![GlobalIndex]
593
	,	td_fun_index	:: !Index
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
594 595 596 597 598
	}

::	TypeDefInfo =
	{	tdi_kinds			:: ![TypeKind]
	,	tdi_properties		:: !BITVECT
599
	,	tdi_group			:: ![GlobalIndex]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
600 601 602
	,	tdi_group_nr		:: !Int
	,	tdi_group_vars		:: ![Int]
	,	tdi_cons_vars		:: ![Int]
603
	,	tdi_index_in_group	:: !Index
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
604
	,	tdi_classification	:: !TypeClassification
605
	,	tdi_gen_rep 		:: !Optional GenericTypeRep
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
606 607
	}

608 609 610 611 612
// type structure is used to specialize a generic to a type
:: GenTypeStruct 
	= GTSAppCons TypeKind [GenTypeStruct]
	| GTSAppVar TypeVar [GenTypeStruct] 
	| GTSVar TypeVar
613 614 615 616 617 618 619 620 621 622 623 624 625
 	| GTSCons !DefinedSymbol !GlobalIndex !DefinedSymbol !DefinedSymbol !GenTypeStruct
 	| GTSRecord !DefinedSymbol !GlobalIndex !DefinedSymbol !DefinedSymbol !GenTypeStruct
 	| GTSField !DefinedSymbol !GlobalIndex !DefinedSymbol !GenTypeStruct
 	| GTSObject !DefinedSymbol !GlobalIndex !DefinedSymbol !GenTypeStruct
	| GTSE
	// the following constructors are used for optimizing bimaps
	| GTSPair !GenTypeStruct !GenTypeStruct
	| GTSEither !GenTypeStruct !GenTypeStruct
	| GTSUnit
	| GTSArrow GenTypeStruct GenTypeStruct
 	| GTSAppConsBimapKindConst
	| GTSAppBimap TypeKind [GenTypeStruct]
	| GTSAppConsSimpleType !GlobalIndex !TypeKind ![GenTypeStruct]
626

627
:: GenericTypeRep = 
628
	{ gtr_type :: GenTypeStruct		// generic structure type
629 630 631
	, gtr_iso  :: !DefinedSymbol	// the conversion isomorphism
	, gtr_to   :: !DefinedSymbol
	, gtr_from :: !DefinedSymbol
632 633
	}

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
634 635 636
::	TypeDefInfos :== {# .{# TypeDefInfo}}

::	FunType =
637
	{	ft_ident		:: !Ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
638 639 640 641
	,	ft_arity		:: !Int
	,	ft_priority		:: !Priority
	,	ft_type			:: !SymbolType
	,	ft_pos			:: !Position
642
	,	ft_specials		:: !FunSpecials
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
643 644 645 646 647
	,	ft_type_ptr		:: !VarInfoPtr
	}

::	FreeVar =
	{	fv_def_level	:: !Level
648
	,	fv_ident		:: !Ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
649 650 651
	,	fv_info_ptr		:: !VarInfoPtr
	,	fv_count		:: !Int
	}
652 653 654 655

::	ModuleIndex:==Index;
::	DclFunctionIndex:==Index;

656 657 658 659
::	FunCall	= FunCall !Index !Level
			| MacroCall !ModuleIndex !Index Level
			| DclFunCall !ModuleIndex !DclFunctionIndex
			| GeneratedFunCall !Index !FunctionInfoPtr;
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
660

661 662
FI_IsMacroFun	:== 1			// whether the function is a local function of a macro
FI_HasTypeSpec	:== 2			// whether the function has u user defined type
663 664
FI_IsNonRecursive :== 4			// used in trans.icl and partition.icl
FI_IsUnboxedListOfRecordsConsOrNil :== 8
665
FI_MemberInstanceRequiresTypeInDefMod :== 16
666 667 668
FI_GenericFun :== 32
FI_Unused :== 64				// used in module trans
FI_UnusedUsed :== 128			// used in module trans
669
FI_HasTypeCodes :== 256
670

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
671 672 673 674 675 676 677
::	FunInfo =
	{	fi_calls			:: ![FunCall]
	,	fi_group_index		:: !Index
	,	fi_def_level		:: !Level
	,	fi_free_vars		:: ![FreeVar]
	,	fi_local_vars		:: ![FreeVar]
	,	fi_dynamics			:: ![ExprInfoPtr]
678
	,	fi_properties		:: !BITVECT
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
679 680 681
	}

::	ParsedBody =
682 683 684
	{	pb_args		:: ![ParsedExpr]
	,	pb_rhs		:: !Rhs
	,	pb_position	:: !Position
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
685 686 687
	}

::	CheckedBody =
688 689
	{	cb_args		:: ![FreeVar]
	,	cb_rhs		:: ![CheckedAlternative]
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
690 691
	}

692 693 694 695 696 697
::	CheckedAlternative =
	{	ca_rhs		:: !Expression
	,	ca_position	:: !Position	// the position is NoPos iff the position information for this
	}								// alternative is already stored in a case alternative
									// (in ap_position, bp_position or dp_position)

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
698 699 700 701 702 703 704 705
::	TransformedBody =
	{	tb_args			:: ![FreeVar]
	,	tb_rhs			:: !Expression
	}

::	FunctionBody	= ParsedBody ![ParsedBody]
					| CheckedBody !CheckedBody
	/* The next three constructors are used during macro expansion (module transform) */
706 707
					| PartitioningMacro
					| PartitioningFunction !CheckedBody !Int
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
708 709 710
					| RhsMacroBody !CheckedBody
	/* macro expansion transforms a CheckedBody into a TransformedBody */
					| TransformedBody !TransformedBody
711
					| Expanding ![FreeVar] // the parameters of the newly generated function
712
					| GeneratedBody // the body will be generated automatically - for generics
713
					| NoBody
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
714 715

::	FunDef =
716
	{	fun_ident		:: !Ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
717 718 719 720 721
	,	fun_arity		:: !Int
	,	fun_priority	:: !Priority
	,	fun_body		:: !FunctionBody
	,	fun_type		:: !Optional SymbolType
	,	fun_pos			:: !Position
722
	,	fun_kind		:: !FunKind
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
723 724 725 726 727 728 729 730
	,	fun_lifted		:: !Int
	,	fun_info		:: !FunInfo
	}

cIsAGlobalVar	:== True
cIsALocalVar	:== False

::	ConsClasses =
731
	{	cc_size			::!Int
732
	,	cc_args			::![ConsClass]
733
	,	cc_linear_bits	::![#Bool!]
734
	,	cc_producer		::!ProdClass
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
735
	}
736

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
737 738
::	ConsClass	:== Int

739 740 741 742
::	ProdClass	:== Bool

pIsSafe			:== True

743 744 745 746
::	ImportedConstructors	:== [Global Index]
::	ImportedFunctions		:== [Global Index]
::	ImportedTypes			:== {#{# CheckedTypeDef}}

Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
747 748 749
::	OptionalVariable :== Optional (Bind Ident VarInfoPtr)

:: 	AuxiliaryPattern
750
		= AP_Algebraic !(Global DefinedSymbol) !GlobalIndex ![AuxiliaryPattern] !OptionalVariable
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
751 752
		| AP_Variable !Ident !VarInfoPtr OptionalVariable
		| AP_Basic !BasicValue OptionalVariable
John van Groningen's avatar
John van Groningen committed
753
		| AP_NewType !(Global DefinedSymbol) !Index AuxiliaryPattern OptionalVariable
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
754 755
		| AP_Dynamic !AuxiliaryPattern !DynamicType !OptionalVariable
		| AP_Constant !AP_Kind !(Global DefinedSymbol) !Priority
756
		| AP_WildCard !OptionalVariable
757
		| AP_Empty
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
758

759
:: AP_Kind = APK_Constructor !GlobalIndex | APK_NewTypeConstructor !Index | APK_Macro !Bool // is_dcl_macro
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
760

761 762
::	VI_TypeInfo	= VITI_Empty
				| VITI_Coercion		CoercionPosition
763
				| VITI_PatternType	[AType] /*module*/!Index /*constructor*/!Index VI_TypeInfo
764 765

//::	VarInfo  =	VI_Empty | VI_Type !AType !(Optional CoercionPosition) | VI_FAType ![ATypeVar] !AType !(Optional CoercionPosition) |
766 767
::	VarInfo  =	VI_Empty | VI_Type !AType !VI_TypeInfo |
				VI_FAType ![ATypeVar] !AType !VI_TypeInfo |
768
				VI_FATypeC ![ATypeVar] !AType ![TypeContext] !VI_TypeInfo | VI_FPC |
769
				VI_Occurrence !Occurrence | VI_UsedVar !Ident |
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
770 771
				VI_Expression !Expression | VI_Variable !Ident !VarInfoPtr | VI_LiftedVariable !VarInfoPtr |
				VI_Count !Int /* the reference count of a variable */ !Bool /* true if the variable is global, false otherwise */ |
772
				VI_AccVar !ConsClass !ArgumentPosition /* used during fusion to determine accumulating parameters of functions */ |
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
773 774
				VI_Alias !BoundVar /* used for resolving aliases just before type checking (in transform) */ |
				 /* used during elimination and lifting of cases */
775 776 777 778
				VI_RefFromTupleSel0 !Int |
				VI_RefFromArrayUpdate !Int ![Selection] |
				VI_RefFromArrayUpdateToTupleSelector2 !Int ![Selection] !VarInfoPtr |
				VI_RefFromArrayUpdateOfTupleElem2 !Int ![Selection] |
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
779
				VI_FreeVar !Ident !VarInfoPtr !Int !AType | VI_BoundVar !AType | VI_LocalVar |
Sjaak Smetsers's avatar
Sjaak Smetsers committed
780
				VI_ClassVar !Ident !VarInfoPtr !Int | /* to hold dictionary variables during overloading */
781
				VI_EmptyConstructorClassVar |
Sjaak Smetsers's avatar
Sjaak Smetsers committed
782
				VI_ForwardClassVar !VarInfoPtr | /* to hold the dictionary variable generated during overloading */
783
				VI_Forward !BoundVar | VI_LetVar !LetVarInfo | VI_LetExpression !LetExpressionInfo |
784
				VI_CaseOrStrictLetVar !VarInfoPtr | VI_StrictLetVar |
785
				VI_CorrespondenceNumber !Int | /* it is assumed that this alternative is _only_ used in module comparedefimp */
786
				VI_SequenceNumber !Int | VI_AliasSequenceNumber !BoundVar |
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
787 788 789 790 791
				VI_Used | /* for indicating that an imported function has been used */
				VI_PropagationType !SymbolType | /* for storing the type with propagation environment of an imported function */
				VI_ExpandedType !SymbolType | /* for storing the (expanded) type of an imported function */
				VI_Record ![AuxiliaryPattern] |
				VI_Pattern !AuxiliaryPattern |
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
792
				VI_TypeCodeVariable !TypeCodeVariableInfo |
793
				VI_DynamicValueAlias !BoundVar |
794 795
				VI_Body !SymbIdent !TransformedBody ![FreeVar] ![TypeVar] ![TypeVar] | /* used during fusion */
				VI_ExpressionOrBody !Expression !SymbIdent !TransformedBody ![FreeVar] ![TypeVar] ![TypeVar] | /* used during fusion */
796
				VI_Dictionary !SymbIdent ![Expression] !Type | /* used during fusion */
Sjaak Smetsers's avatar
Sjaak Smetsers committed
797
				VI_Extended !ExtendedVarInfo !VarInfo |
798
				VI_NotUsed |
799
// MdM
800
				VI_CPSExprVar !CheatCompiler /* a pointer to a variable in CleanProverSystem is stored here, using a cast */
801
// ... MdM
802
				| VI_Labelled_Empty !{#Char} // RWS debugging
803
				| VI_LocalLetVar // RWS, mark Let vars during case transformation
804

805 806 807 808 809
::	TypeCodeVariableInfo
	= TCI_TypeVar !Expression
	| TCI_TypePatternVar !Expression
	| TCI_SelectionsTypePatternVar ![(Expression,[Selection])]

810
::	ExtendedVarInfo = EVI_VarType !AType
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
811

812 813
::	ArgumentPosition :== Int

814
::	VarHeap :== Heap VarInfo
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
815 816 817 818 819
::	VarInfoPtr	:== Ptr VarInfo

cNotVarNumber :== -1

::	BoundVar = 
820
	{	var_ident		:: !Ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
821 822 823 824 825
	,	var_info_ptr	:: !VarInfoPtr
	,	var_expr_ptr	:: !ExprInfoPtr
	}

::	TypeSymbIdent =
826
	{	type_ident		:: !Ident
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
827 828 829 830 831 832 833 834 835 836 837 838 839
	,	type_arity		:: !Int
	,	type_index		:: !Global Index
	,	type_prop		:: !TypeSymbProperties
	}

::	TypeSymbProperties =
	{	tsp_sign		:: !SignClassification
	,	tsp_propagation	:: !PropClassification
	,	tsp_coercible	:: !Bool
	}

::	SymbKind	= SK_Unknown
				| SK_Function !(Global Index)
840
				| SK_IclMacro !Index
841
				| SK_LocalMacroFunction !Index
842 843
				| SK_DclMacro !(Global Index)
				| SK_LocalDclMacroFunction !(Global Index)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
844 845
				| SK_OverloadedFunction !(Global Index)
				| SK_GeneratedFunction !FunctionInfoPtr !Index
846
				| SK_Constructor !(Global Index)
John van Groningen's avatar
John van Groningen committed
847
				| SK_NewTypeConstructor !GlobalIndex
848
				| SK_Generic !(Global Index) !TypeKind
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
849
				| SK_TypeCode
850 851 852 853
				| SK_OverloadedConstructor !(Global Index)
				| SK_TFACVar !ExprInfoPtr
				| SK_VarContexts !(VarContexts TypeContext)
				| SK_TypeCodeAndContexts !(VarContexts TypeContext)
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
854 855 856 857 858 859 860 861 862 863 864 865 866

/*	Some auxiliary type definitions used during fusion. Actually, these definitions
	should have been given in seperate module. Unfortunately, Clean's module system
	forbids cyclic dependencies between def modules.
*/

::	FunctionHeap 	:== Heap FunctionInfo

::	FunctionInfoPtr	:== Ptr FunctionInfo

::	FunctionInfo	= FI_Empty | FI_Function !GeneratedFunction

::	Producer	= PR_Empty
867
				| PR_Function !SymbIdent !Int !Index
Martin Wierich's avatar
Martin Wierich committed
868
				| PR_Class !App ![(BoundVar, Type)] !Type
869 870 871
				| PR_Constructor !SymbIdent !Int ![Expression]
				| PR_GeneratedFunction !SymbIdent !Int !Index
				| PR_Curried !SymbIdent !Int
872
				| PR_Unused
873
				| PR_CurriedFunction !SymbIdent !Int !Index
874 875 876 877
				| PR_String !{#Char}
				| PR_Int !Int
				| PR_Equal !Int
				| PR_EqualRemove !Int
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902

::	InstanceInfo = II_Empty | II_Node !{! Producer} !FunctionInfoPtr !InstanceInfo !InstanceInfo

::	GeneratedFunction = 
	{	gf_fun_def			:: !FunDef
	,	gf_instance_info	:: !InstanceInfo
	,	gf_cons_args		:: !ConsClasses
	,	gf_fun_index		:: !Index
	}
	
/*	... main type definitions continued .... */

::	ExpressionHeap 	:== Heap ExprInfo

::	ExprInfoPtr		:== Ptr ExprInfo

::	TempLocalVar	:== Int

::	DynamicPtr		:== ExprInfoPtr

::	ExprInfo		= EI_Empty

		/* For handling overloading */

					| EI_Overloaded !OverloadedCall 						/* initial, set by the type checker */
903
					| EI_OverloadedWithVarContexts !OverloadedCallWithVarContexts /* initial, set by the type checker */
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
904
					| EI_Instance 	!(Global DefinedSymbol) ![Expression]	/* intermedediate, used during resolving of overloading */ 
Sjaak Smetsers's avatar
Sjaak Smetsers committed
905
					| EI_Selection 	![Selection] !VarInfoPtr ![Expression]	/* intermedediate, used during resolving of overloading */
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
906
					| EI_Context 	![Expression]							/* intermedediate, used during resolving of overloading */
907 908
					| EI_ContextWithVarContexts ![Expression] !(VarContexts DictionaryAndClassType) /* intermedediate, used during resolving of overloading */
					| EI_FPContext	![Expression] !ExprInfoPtr				/* intermedediate, used during resolving of overloading */
Ronny Wichers Schreur's avatar
Ronny Wichers Schreur committed
909 910 911

		/* For handling dynamics */

John van Groningen's avatar