diff options
author | alimarin | 2002-03-25 15:04:33 +0000 |
---|---|---|
committer | alimarin | 2002-03-25 15:04:33 +0000 |
commit | 5ed289050bba7924972700181478cb22e9d69c70 (patch) | |
tree | 43d0c8ebe33e14ad0d4f637ddae3de94acd7bf07 | |
parent | fix version number (diff) |
new implementation of generics
git-svn-id: https://svn.cs.ru.nl/repos/clean-compiler/trunk@1062 1f8540f1-abd5-4d5b-9d24-4c5ce8603e2d
39 files changed, 4606 insertions, 627 deletions
diff --git a/frontend/StdCompare.dcl b/frontend/StdCompare.dcl index ed25a4a..eb08b3f 100644 --- a/frontend/StdCompare.dcl +++ b/frontend/StdCompare.dcl @@ -15,7 +15,7 @@ instance =< Type, SymbIdent instance == BasicType, TypeVar, AttributeVar, AttrInequality, TypeSymbIdent, DefinedSymbol, TypeContext , BasicValue, FunKind, (Global a) | == a, Priority, Assoc, Type, - ConsVariable, SignClassification + ConsVariable, SignClassification, TypeCons instance < MemberDef diff --git a/frontend/StdCompare.icl b/frontend/StdCompare.icl index 7495abf..91593b7 100644 --- a/frontend/StdCompare.icl +++ b/frontend/StdCompare.icl @@ -117,6 +117,11 @@ where instance == SignClassification where (==) sc1 sc2 = sc1.sc_pos_vect == sc2.sc_pos_vect && sc1.sc_neg_vect == sc2.sc_neg_vect +instance == TypeCons where + (==) (TypeConsSymb x) (TypeConsSymb y) = x == y + (==) (TypeConsBasic x) (TypeConsBasic y) = x == y + (==) TypeConsArrow TypeConsArrow = True + :: CompareValue :== Int Smaller :== -1 Greater :== 1 diff --git a/frontend/analtypes.dcl b/frontend/analtypes.dcl index 1f29066..08991fd 100644 --- a/frontend/analtypes.dcl +++ b/frontend/analtypes.dcl @@ -13,7 +13,7 @@ determineKindsOfClasses :: !NumberSet !{#CommonDefs} !*TypeDefInfos !*TypeVarHea -> (!*ClassDefInfos, !*TypeDefInfos, !*TypeVarHeap, !*ErrorAdmin) checkKindsOfCommonDefsAndFunctions :: !Index !Index !NumberSet ![IndexRange] !{#CommonDefs} !u:{# FunDef} !v:{#DclModule} !*TypeDefInfos !*ClassDefInfos - !*TypeVarHeap !*ErrorAdmin -> (!u:{# FunDef}, !v:{#DclModule}, !*TypeDefInfos, !*TypeVarHeap, !*ErrorAdmin) + !*TypeVarHeap !*GenericHeap !*ErrorAdmin -> (!u:{# FunDef}, !v:{#DclModule}, !*TypeDefInfos, !*TypeVarHeap, !*GenericHeap, !*ErrorAdmin) isATopConsVar cv :== cv < 0 encodeTopConsVar cv :== dec (~cv) diff --git a/frontend/analtypes.icl b/frontend/analtypes.icl index e82b354..b24225d 100644 --- a/frontend/analtypes.icl +++ b/frontend/analtypes.icl @@ -831,9 +831,9 @@ where = ( type_var_heap <:= (tv_info_ptr, TVI_TypeKind kind_info_ptr), kind_heap <:= (kind_info_ptr, KI_Var kind_info_ptr)) checkKindsOfCommonDefsAndFunctions :: !Index !Index !NumberSet ![IndexRange] !{#CommonDefs} !u:{# FunDef} !v:{#DclModule} !*TypeDefInfos !*ClassDefInfos - !*TypeVarHeap !*ErrorAdmin -> (!u:{# FunDef}, !v:{#DclModule}, !*TypeDefInfos, !*TypeVarHeap, !*ErrorAdmin) + !*TypeVarHeap !*GenericHeap !*ErrorAdmin -> (!u:{# FunDef}, !v:{#DclModule}, !*TypeDefInfos, !*TypeVarHeap, !*GenericHeap, !*ErrorAdmin) checkKindsOfCommonDefsAndFunctions first_uncached_module main_module_index used_module_numbers icl_fun_def_ranges common_defs icl_fun_defs dcl_modules - type_def_infos class_infos type_var_heap error + type_def_infos class_infos type_var_heap gen_heap error # as = { as_td_infos = type_def_infos , as_type_var_heap = type_var_heap @@ -841,27 +841,29 @@ checkKindsOfCommonDefsAndFunctions first_uncached_module main_module_index used_ , as_error = error } - # (icl_fun_defs, dcl_modules, class_infos, as) + # (icl_fun_defs, dcl_modules, class_infos, gen_heap, as) = iFoldSt (check_kinds_of_module first_uncached_module main_module_index used_module_numbers icl_fun_def_ranges common_defs) - 0 (size common_defs) (icl_fun_defs, dcl_modules, class_infos, as) - = (icl_fun_defs, dcl_modules, as.as_td_infos, as.as_type_var_heap, as.as_error) + 0 (size common_defs) (icl_fun_defs, dcl_modules, class_infos, gen_heap, as) + = (icl_fun_defs, dcl_modules, as.as_td_infos, as.as_type_var_heap, gen_heap, as.as_error) where check_kinds_of_module first_uncached_module main_module_index used_module_numbers icl_fun_def_ranges common_defs module_index - (icl_fun_defs, dcl_modules, class_infos, as) + (icl_fun_defs, dcl_modules, class_infos, gen_heap, as) | inNumberSet module_index used_module_numbers | module_index == main_module_index # (class_infos, as) = check_kinds_of_class_instances common_defs 0 common_defs.[module_index].com_instance_defs class_infos as + # (class_infos, gen_heap, as) = check_kinds_of_generics common_defs 0 common_defs.[module_index].com_generic_defs class_infos gen_heap as # (icl_fun_defs, class_infos, as) = foldSt (check_kinds_of_icl_fuctions common_defs) icl_fun_def_ranges (icl_fun_defs, class_infos, as) with check_kinds_of_icl_fuctions common_defs {ir_from,ir_to} (icl_fun_defs, class_infos, as) = iFoldSt (check_kinds_of_icl_fuction common_defs) ir_from ir_to (icl_fun_defs, class_infos, as) - = (icl_fun_defs, dcl_modules, class_infos, as) + = (icl_fun_defs, dcl_modules, class_infos, gen_heap, as) | module_index >= first_uncached_module # (class_infos, as) = check_kinds_of_class_instances common_defs 0 common_defs.[module_index].com_instance_defs class_infos as + # (class_infos, gen_heap, as) = check_kinds_of_generics common_defs 0 common_defs.[module_index].com_generic_defs class_infos gen_heap as # (dcl_modules, class_infos, as) = check_kinds_of_dcl_fuctions common_defs module_index dcl_modules class_infos as - = (icl_fun_defs, dcl_modules, class_infos, as) - = (icl_fun_defs, dcl_modules, class_infos, as) - = (icl_fun_defs, dcl_modules, class_infos, as) + = (icl_fun_defs, dcl_modules, class_infos, gen_heap, as) + = (icl_fun_defs, dcl_modules, class_infos, gen_heap, as) + = (icl_fun_defs, dcl_modules, class_infos, gen_heap, as) check_kinds_of_class_instances common_defs instance_index instance_defs class_infos as | instance_index == size instance_defs @@ -870,9 +872,9 @@ where = check_kinds_of_class_instances common_defs (inc instance_index) instance_defs class_infos as where check_kinds_of_class_instance :: !{#CommonDefs} !ClassInstance !*ClassDefInfos !*AnalyseState -> (!*ClassDefInfos, !*AnalyseState) - check_kinds_of_class_instance common_defs {ins_is_generic, ins_class,ins_ident,ins_pos,ins_type={it_vars,it_types,it_context}} class_infos + check_kinds_of_class_instance common_defs {ins_generated, ins_class,ins_ident,ins_pos,ins_type={it_vars,it_types,it_context}} class_infos as=:{as_type_var_heap,as_kind_heap,as_error} - | ins_is_generic + | ins_generated // generic instances are cheched in the generic phase = (class_infos, as) # as_error = pushErrorAdmin (newPosition ins_ident ins_pos) as_error @@ -882,6 +884,40 @@ where [{tc_class = ins_class, tc_types = it_types, tc_var = nilPtr} : it_context] class_infos as = (class_infos, { as & as_error = popErrorAdmin as.as_error}) + check_kinds_of_generics common_defs index generic_defs class_infos gen_heap as + | index == size generic_defs + = (class_infos, gen_heap, as) + # (class_infos, gen_heap, as) = check_kinds_of_generic common_defs generic_defs.[index] class_infos gen_heap as + = check_kinds_of_generics common_defs (inc index) generic_defs class_infos gen_heap as + where + check_kinds_of_generic :: !{#CommonDefs} !GenericDef !*ClassDefInfos !*GenericHeap !*AnalyseState -> (!*ClassDefInfos, !*GenericHeap, !*AnalyseState) + check_kinds_of_generic common_defs {gen_type, gen_name, gen_pos, gen_vars, gen_info_ptr} class_infos gen_heap as + # as = {as & as_error = pushErrorAdmin (newPosition gen_name gen_pos) as.as_error} + # (class_infos, as) = check_kinds_of_symbol_type common_defs gen_type class_infos as + # (kinds, as) = mapSt retrieve_tv_kind gen_type.st_vars as + # as = check_kinds_of_generic_vars (take (length gen_vars) kinds) as + # (gen_info, gen_heap) = readPtr gen_info_ptr gen_heap + # gen_heap = writePtr gen_info_ptr {gen_info & gen_var_kinds = kinds} gen_heap + # as = {as & as_error = popErrorAdmin as.as_error} + = (class_infos, gen_heap, as) + + retrieve_tv_kind :: !TypeVar !*AnalyseState -> (!TypeKind, !*AnalyseState) + retrieve_tv_kind tv=:{tv_info_ptr} as=:{as_type_var_heap, as_kind_heap} + #! (TVI_TypeKind kind_info_ptr, as_type_var_heap) = readPtr tv_info_ptr as_type_var_heap + #! (kind_info, as_kind_heap) = readPtr kind_info_ptr as_kind_heap + #! (kind, as_kind_heap) = kindInfoToKind kind_info as_kind_heap + = (kind, {as & as_kind_heap = as_kind_heap, as_type_var_heap = as_type_var_heap}) + + check_kinds_of_generic_vars :: ![TypeKind] !*AnalyseState -> !*AnalyseState + check_kinds_of_generic_vars [gen_kind:gen_kinds] as + | all (\k -> k == gen_kind) gen_kinds + = as + # as_error = checkError + "conflicting kinds: " + "generic variables must have the same kind" + as.as_error + = {as & as_error = as_error} + check_kinds_of_icl_fuction common_defs fun_index (icl_fun_defs, class_infos, as) # ({fun_type,fun_symb,fun_pos}, icl_fun_defs) = icl_fun_defs![fun_index] = case fun_type of @@ -904,7 +940,7 @@ where (class_infos, as) = check_kinds_of_symbol_type common_defs ft_type class_infos { as & as_error = as_error } = (class_infos, { as & as_error = popErrorAdmin as.as_error}) - + check_kinds_of_symbol_type :: !{#CommonDefs} !SymbolType !*ClassDefInfos !*AnalyseState -> (!*ClassDefInfos, !*AnalyseState) check_kinds_of_symbol_type common_defs {st_vars,st_result,st_args,st_context} class_infos as=:{as_type_var_heap,as_kind_heap} # (as_type_var_heap, as_kind_heap) = bindFreshKindVariablesToTypeVars st_vars as_type_var_heap as_kind_heap diff --git a/frontend/check.icl b/frontend/check.icl index 977d24e..a222d25 100644 --- a/frontend/check.icl +++ b/frontend/check.icl @@ -4,6 +4,7 @@ import StdEnv import syntax, typesupport, parse, checksupport, utilities, checktypes, transform, predef import explicitimports, comparedefimp, checkFunctionBodies, containers, portToNewSyntax, compilerSwitches +import genericsupport // import RWSDebug cUndef :== (-1) @@ -13,74 +14,293 @@ isMainModule :: ModuleKind -> Bool isMainModule MK_Main = True isMainModule _ = False -checkGenerics :: !Index !Index !*{#GenericDef} !*{#ClassDef} !*{#CheckedTypeDef} !*{#DclModule} !*TypeHeaps !*CheckState - -> (!*{#GenericDef}, !*{#ClassDef}, !*{#CheckedTypeDef}, !*{#DclModule}, !*TypeHeaps, !*CheckState) -checkGenerics - gen_index module_index generic_defs class_defs type_defs modules - type_heaps=:{th_vars} - cs=:{cs_symbol_table, cs_error} - | gen_index == size generic_defs - = (generic_defs, class_defs, type_defs, modules, type_heaps, cs) - // otherwise - # (generic_def=:{gen_name, gen_type, gen_pos}, generic_defs) = generic_defs![gen_index] - # position = newPosition gen_name gen_pos - # cs_error = setErrorAdmin position cs_error - - // add * for kind-star instances and *->* for arrays - # kinds = - [ KindConst - , KindArrow [KindConst] - ] - # (kinds_ptr, th_vars) = newPtr (TVI_Kinds kinds) th_vars - # (cons_ptr, th_vars) = newPtr (TVI_Empty) th_vars - - # cs = {cs & cs_error = cs_error, cs_symbol_table = cs_symbol_table } - # type_heaps = {type_heaps & th_vars = th_vars} - - # (gt_type, type_defs, class_defs, modules, type_heaps, cs) = - checkMemberType module_index gen_type.gt_type type_defs class_defs modules type_heaps cs - - #! {cs_error} = cs - #! (gt_vars, st_vars, cs_error) = split_vars gen_type.gt_vars gt_type.st_vars cs_error +// AA: new implementation of generics ... +checkGenericDefs :: !Index !(Optional (CopiedDefinitions, Int)) !*{#GenericDef} !*{#CheckedTypeDef} !*{#ClassDef} !*{#DclModule} !*Heaps !*CheckState + -> (!*{#GenericDef}, !*{#CheckedTypeDef}, !*{#ClassDef}, !*{#DclModule}, !*Heaps, !*CheckState) +checkGenericDefs mod_index opt_icl_info gen_defs type_defs class_defs modules heaps cs + = check_generics 0 mod_index opt_icl_info gen_defs type_defs class_defs modules heaps cs +where + check_generics index mod_index opt_icl_info gen_defs type_defs class_defs modules heaps cs + # (n_generics, gen_defs) = usize gen_defs + | index == n_generics + = (gen_defs, type_defs, class_defs, modules, heaps, cs) + # (gen_defs, type_defs, class_defs, modules, heaps, cs) + = check_generic_def index mod_index opt_icl_info gen_defs type_defs class_defs modules heaps cs + = check_generics (inc index) mod_index opt_icl_info gen_defs type_defs class_defs modules heaps cs + + check_generic_def index mod_index opt_icl_info gen_defs type_defs class_defs modules heaps cs + | has_to_be_checked mod_index index opt_icl_info + = check_generic index mod_index gen_defs type_defs class_defs modules heaps cs + //---> ("check_generic", mod_index, index) + = (gen_defs, type_defs, class_defs, modules, heaps, cs) + //---> ("skipped check_generic", mod_index, index) + + has_to_be_checked module_index generic_index No + = True + has_to_be_checked module_index generic_index (Yes ({copied_generic_defs}, n_cached_dcl_mods)) + = not (module_index < n_cached_dcl_mods && generic_index < size copied_generic_defs && copied_generic_defs.[generic_index]) + + check_generic index mod_index gen_defs type_defs class_defs modules heaps cs -/* - #! cs_error = case gt_type.st_context of - [] -> cs_error - _ -> checkError "" "class contexts are not supported in generic types" cs_error -*/ + #(gen_def=:{gen_name, gen_pos}, gen_defs) = gen_defs ! [index] + # cs = pushErrorAdmin (newPosition gen_name gen_pos) cs - #! cs = {cs & cs_error = cs_error} - #! gt_type = {gt_type & st_vars = st_vars} + # (gen_def, heaps) = alloc_gen_info gen_def heaps - # generic_def = - { generic_def & - gen_type = { gen_type & gt_vars = gt_vars, gt_type = gt_type } - , gen_kinds_ptr = kinds_ptr - , gen_cons_ptr = cons_ptr - } + # (gen_def, type_defs, class_defs, modules, heaps, cs) + = check_generic_type gen_def mod_index type_defs class_defs modules heaps cs - # generic_defs = {generic_defs & [gen_index] = generic_def} - = checkGenerics (inc gen_index) module_index generic_defs class_defs type_defs modules type_heaps cs -where - split_vars [] st_vars error - = ([], st_vars, error) - split_vars [gv:gvs] st_vars error - # (gv, st_vars, error) = find gv st_vars error - # (gvs, st_vars, error) = split_vars gvs st_vars error - = ([gv:gvs], st_vars, error) + //# (heaps, cs) = check_generic_vars gen_def heaps cs + + # gen_defs = {gen_defs & [index] = gen_def} + # cs = popErrorAdmin cs + = (gen_defs, type_defs, class_defs, modules, heaps, cs) + //---> ("check_generic", gen_name, gen_def.gen_vars, gen_def.gen_type) + + alloc_gen_info gen_def heaps=:{hp_generic_heap} + # initial_info = + { gen_classes = createArray 32 [] + , gen_cases = [] + , gen_var_kinds = [] + , gen_star_case = {gi_module=NoIndex, gi_index=NoIndex} + } + # (gen_info_ptr, hp_generic_heap) = newPtr initial_info hp_generic_heap + = ( {gen_def & gen_info_ptr = gen_info_ptr}, + {heaps & hp_generic_heap = hp_generic_heap}) + + check_generic_vars {gen_vars,gen_type} heaps=:{hp_type_heaps=hp_type_heaps=:{th_vars}} cs + #! types = [gen_type.st_result:gen_type.st_args] + #! th_vars = performOnTypeVars mark_var types th_vars + #! (th_vars,cs) = foldSt check_var_marked gen_vars (th_vars,cs) + #! th_vars = performOnTypeVars initializeToTVI_Empty types th_vars + = ({heaps & hp_type_heaps={hp_type_heaps&th_vars=th_vars}}, cs) where - find gv [] error = (gv, [], checkError gv.tv_name.id_name "generic variable not used" error) - find gv [st_var:st_vars] error - | st_var.tv_name.id_name == gv.tv_name.id_name - = (st_var, st_vars, error) - # (gv, st_vars, error) = find gv st_vars error - = (gv, [st_var:st_vars], error) + mark_var _ {tv_name,tv_info_ptr} th_vars + = writePtr tv_info_ptr TVI_Used th_vars + check_var_marked {tv_name,tv_info_ptr} (th_vars,cs=:{cs_error}) + #! (tv_info, th_vars) = readPtr tv_info_ptr th_vars + #! cs_error = case tv_info of + TVI_Empty -> checkError tv_name "generic variable not used" cs_error + TVI_Used -> cs_error + = (th_vars, {cs & cs_error = cs_error}) + + check_generic_type gen_def=:{gen_type, gen_vars, gen_name, gen_pos} module_index type_defs class_defs modules heaps=:{hp_type_heaps} cs + + #! (checked_gen_type, _, type_defs, class_defs, modules, hp_type_heaps, cs) = + checkFunctionType module_index gen_type SP_None type_defs class_defs modules hp_type_heaps cs + + #! (checked_gen_vars, cs) = check_generic_vars gen_vars checked_gen_type.st_vars cs + #! checked_gen_type = { checked_gen_type & st_vars = move_gen_vars checked_gen_vars checked_gen_type.st_vars} + + #! (hp_type_heaps, cs) = check_no_generic_vars_in_contexts checked_gen_type checked_gen_vars hp_type_heaps cs + = ( {gen_def & gen_type = checked_gen_type, gen_vars = checked_gen_vars} + , type_defs + , class_defs + , modules + , {heaps & hp_type_heaps = hp_type_heaps} + , cs + ) + //---> ("check_genric_type", gen_vars, checked_gen_vars, checked_gen_type) + where + check_generic_vars gen_vars st_vars cs=:{cs_error} + # (gen_vars, _, cs_error) = foldSt check_generic_var gen_vars ([], st_vars, cs_error) + = (reverse gen_vars, {cs & cs_error = cs_error}) + + // make sure generic variables are first + move_gen_vars gen_vars st_vars + = gen_vars ++ (removeMembers st_vars gen_vars) + + check_generic_var gv (acc_gvs, [], error) + = (acc_gvs, [], checkError gv.tv_name "generic variable not used" error) + check_generic_var gv (acc_gvs, [tv:tvs], error) + | gv.tv_name.id_name == tv.tv_name.id_name + = ([tv:acc_gvs], tvs, error) + # (acc_gvs, tvs, error) = check_generic_var gv (acc_gvs, tvs, error) + = (acc_gvs, [tv:tvs], error) + + // returns reversed variable list + add_vars_to_symbol_table gen_vars type_heaps=:{th_vars} cs=:{cs_error, cs_symbol_table} + #! (rev_gen_vars,cs_symbol_table,th_vars, cs_error) + = foldSt add_var_to_symbol_table gen_vars ([],cs.cs_symbol_table,th_vars, cs_error) + = ( rev_gen_vars, + {type_heaps & th_vars = th_vars}, + {cs & cs_error = cs_error, cs_symbol_table = cs_symbol_table}) + + add_var_to_symbol_table :: !TypeVar !(![TypeVar], !*SymbolTable, !*TypeVarHeap, !*ErrorAdmin) + -> (![TypeVar],!*SymbolTable,!*TypeVarHeap,!*ErrorAdmin) + add_var_to_symbol_table tv=:{tv_name={id_name,id_info}} (rev_class_args, symbol_table, th_vars, error) + #! (entry, symbol_table) = readPtr id_info symbol_table + | entry.ste_kind == STE_Empty || entry.ste_def_level < cGlobalScope + # (new_var_ptr, th_vars) = newPtr TVI_Empty th_vars + # symbol_table = NewEntry symbol_table id_info (STE_TypeVariable new_var_ptr) NoIndex cGlobalScope entry + = ([{ tv & tv_info_ptr = new_var_ptr} : rev_class_args], symbol_table, th_vars, error) + = (rev_class_args, symbol_table, th_vars, checkError id_name "generic variable already defined" error) + + // also reverses variable list (but does not make coffe) + remove_vars_from_symbol_table rev_gen_vars cs=:{cs_symbol_table} + #! (gen_vars, cs_symbol_table) = foldSt remove_var_from_symbol_table rev_gen_vars ([], cs_symbol_table) + = (gen_vars, { cs & cs_symbol_table = cs_symbol_table}) + remove_var_from_symbol_table tv=:{tv_name={id_name,id_info}} (gen_vars, symbol_table) + #! (entry, symbol_table) = readPtr id_info symbol_table + #! symbol_table = writePtr id_info entry.ste_previous symbol_table + =([tv:gen_vars], symbol_table) + + check_no_generic_vars_in_contexts :: !SymbolType ![TypeVar] !*TypeHeaps !*CheckState + -> (!*TypeHeaps, !*CheckState) + check_no_generic_vars_in_contexts gen_type gen_vars th=:{th_vars} cs=:{cs_error} + + #! th_vars = clear_type_vars gen_type.st_vars th_vars + #! th_vars = mark_type_vars_used gen_vars th_vars + #! (th_vars, cs_error) = check_type_vars_not_used gen_type.st_context th_vars cs_error + #! th_vars = clear_type_vars gen_type.st_vars th_vars + + = ({th & th_vars = th_vars}, {cs & cs_error = cs_error}) + where + mark_type_vars_used gen_vars th_vars + = foldSt (write_type_var_info TVI_Used) gen_vars th_vars + clear_type_vars gen_vars th_vars + = foldSt (write_type_var_info TVI_Empty) gen_vars th_vars + write_type_var_info tvi {tv_name, tv_info_ptr} th_vars + = writePtr tv_info_ptr tvi th_vars + + check_type_vars_not_used :: ![TypeContext] !*TypeVarHeap !*ErrorAdmin -> (!*TypeVarHeap, !*ErrorAdmin) + check_type_vars_not_used contexts th_vars cs_error + # types = flatten [tc_types \\ {tc_types} <- contexts] + # atypes = [{at_type=t,at_attribute=TA_None} \\ t <- types] + = performOnTypeVars check_type_var_not_used atypes (th_vars, cs_error) + check_type_var_not_used attr tv=:{tv_name, tv_info_ptr} (th_vars, cs_error) + #! (tv_info, th_vars) = readPtr tv_info_ptr th_vars + = case tv_info of + TVI_Empty + -> (th_vars, cs_error) + TVI_Used + #! cs_error = checkError tv_name "context restrictions on generic variables are not allowed" cs_error + -> (th_vars, cs_error) + _ -> abort ("check_no_generic_vars_in_contexts: wrong TVI" ---> (tv, tv_info)) + +checkGenericCaseDefs :: !Index !*{#GenericCaseDef} !*{#GenericDef} !u:{#CheckedTypeDef} !*{#DclModule} !*Heaps !*CheckState + -> (!*{#GenericCaseDef}, !*{#GenericDef}, !u:{#CheckedTypeDef}, !*{#DclModule},!.Heaps,!.CheckState) +checkGenericCaseDefs mod_index gen_case_defs generic_defs type_defs modules heaps cs + = check_instances 0 mod_index gen_case_defs generic_defs type_defs modules heaps cs +where + check_instances index mod_index gen_case_defs generic_defs type_defs modules heaps cs + # (n_gc, gen_inst_defs) = usize gen_case_defs + | index == n_gc + = (gen_case_defs, generic_defs, type_defs, modules, heaps, cs) + # (gen_case_defs, generic_defs, type_defs, modules, heaps, cs) + = check_instance index mod_index gen_case_defs generic_defs type_defs modules heaps cs + = check_instances (inc index) mod_index gen_case_defs generic_defs type_defs modules heaps cs + + check_instance index mod_index gen_case_defs generic_defs type_defs modules heaps cs + + #! (case_def=:{gc_name,gc_gname,gc_pos,gc_type}, gen_case_defs) = gen_case_defs ! [index] + + #! cs = pushErrorAdmin (newPosition gc_name gc_pos) cs + + #! (gc_type, gc_type_cons, type_defs, modules, heaps, cs) + = check_instance_type mod_index gc_type type_defs modules heaps cs + + #! (generic_gi, cs) = get_generic_index gc_gname mod_index cs + | not cs.cs_error.ea_ok + # cs = popErrorAdmin cs + = (gen_case_defs, generic_defs, type_defs, modules, heaps, cs) + + #! case_def = + { case_def + & gc_generic = generic_gi + , gc_type = gc_type + , gc_type_cons = gc_type_cons + } + #! gen_case_defs = { gen_case_defs & [index] = case_def } + + #! (generic_def, generic_defs, modules) = get_generic_def generic_gi mod_index generic_defs modules + #! gindex = {gi_module=mod_index,gi_index=index} + #! heaps = add_case_to_generic generic_def gindex heaps + + #! (heaps, cs) = check_star_case gc_type_cons generic_def gindex heaps cs + + #! cs = popErrorAdmin cs + = (gen_case_defs, generic_defs, type_defs, modules, heaps, cs) + //---> ("check_generic_case", gc_name, gc_type_cons) + + check_instance_type module_index (TA type_cons []) type_defs modules heaps=:{hp_type_heaps} cs + + # (entry, cs_symbol_table) = readPtr type_cons.type_name.id_info cs.cs_symbol_table + # cs = {cs & cs_symbol_table = cs_symbol_table} + # (type_index, type_module) = retrieveGlobalDefinition entry STE_Type module_index + | type_index == NotFound + # cs_error = checkError type_cons.type_name "generic argument type undefined" cs.cs_error + = (TA type_cons [], TypeConsSymb type_cons, type_defs, modules,{heaps&hp_type_heaps = hp_type_heaps}, {cs&cs_error=cs_error}) + # (type_def, type_defs, modules) + = getTypeDef module_index {glob_module=type_module, glob_object=type_index} type_defs modules + # type_cons = { type_cons & type_index = { glob_object = type_index, glob_module = type_module }} + = (TA type_cons [], TypeConsSymb type_cons, type_defs, modules,{heaps&hp_type_heaps = hp_type_heaps}, cs) + check_instance_type module_index (TB b) type_defs modules heaps cs + = (TB b, TypeConsBasic b, type_defs, modules,heaps, cs) + check_instance_type module_index TArrow type_defs modules heaps cs + = (TArrow, TypeConsArrow, type_defs, modules, heaps , cs) +// General instance .. + check_instance_type module_index (TV tv) type_defs modules heaps=:{hp_type_heaps} cs + # (tv_info_ptr, th_vars) = newPtr TVI_Empty hp_type_heaps.th_vars + # tv = {tv & tv_info_ptr = tv_info_ptr} + = ( TV tv, TypeConsVar tv, type_defs, modules + , {heaps& hp_type_heaps = {hp_type_heaps & th_vars = th_vars}}, cs) + +// .. General instance + check_instance_type module_index ins_type type_defs modules heaps cs=:{cs_error} + # cs_error = checkError {id_name="<>",id_info=nilPtr} "invalid generic type argument" cs_error + = (ins_type, TypeConsArrow, type_defs, modules, heaps, {cs & cs_error=cs_error}) + + get_generic_index :: !Ident !Index !*CheckState -> (!GlobalIndex, !*CheckState) + get_generic_index {id_name,id_info} mod_index cs=:{cs_symbol_table} + # (ste, cs_symbol_table) = readPtr id_info cs_symbol_table + # cs = {cs & cs_symbol_table = cs_symbol_table} + = case ste.ste_kind of + STE_Generic + -> ({gi_module=mod_index,gi_index = ste.ste_index}, cs) + STE_Imported STE_Generic imported_generic_module + -> ({gi_module=imported_generic_module,gi_index = ste.ste_index}, cs) + _ -> //abort "--------------" ---> ("STE_Kind", ste.ste_kind) + ( {gi_module=NoIndex,gi_index = NoIndex} + , {cs & cs_error = checkError id_name "generic undefined" cs.cs_error}) + + get_generic_def :: !GlobalIndex !Int !u:{#GenericDef} !v:{#DclModule} -> (!GenericDef,!u:{#GenericDef},!v:{#DclModule}) + get_generic_def {gi_module, gi_index} mod_index generic_defs modules + | gi_module == mod_index + # (generic_def, generic_defs) = generic_defs![gi_index] + = (generic_def, generic_defs, modules) + # (dcl_mod, modules) = modules![gi_module] + = (dcl_mod.dcl_common.com_generic_defs.[gi_index], generic_defs, modules) + + add_case_to_generic :: !GenericDef !GlobalIndex !*Heaps -> !*Heaps + add_case_to_generic {gen_info_ptr} index heaps=:{hp_generic_heap} + # (info=:{gen_cases}, hp_generic_heap) = readPtr gen_info_ptr hp_generic_heap + # info = { info & gen_cases = [index:gen_cases]} + = { heaps & hp_generic_heap = writePtr gen_info_ptr info hp_generic_heap} + + check_star_case :: !TypeCons !GenericDef !GlobalIndex !*Heaps !*CheckState -> (!*Heaps, !*CheckState) + check_star_case (TypeConsVar _) {gen_name, gen_info_ptr} index heaps=:{hp_generic_heap} cs=:{cs_error} + # (info=:{gen_star_case}, hp_generic_heap) = readPtr gen_info_ptr hp_generic_heap + | gen_star_case.gi_module <> NoIndex + # cs_error = checkError gen_name "general kind-* case is already defined" cs_error + = ({ heaps & hp_generic_heap = hp_generic_heap}, {cs & cs_error = cs_error}) + # info = { info & gen_star_case = index } + # hp_generic_heap = writePtr gen_info_ptr info hp_generic_heap + = ({ heaps & hp_generic_heap = hp_generic_heap}, {cs & cs_error = cs_error}) + check_star_case _ _ _ heaps cs + = (heaps, cs) + + +// ... AA: new implementation of generics + -checkTypeClasses :: !Index !(Optional (CopiedDefinitions, Int)) !*{#ClassDef} !*{#MemberDef} !*{#CheckedTypeDef} !*{#DclModule} !*TypeHeaps !*CheckState - -> (!*{#ClassDef}, !*{#MemberDef}, !*{#CheckedTypeDef}, !*{#DclModule}, !*TypeHeaps, !*CheckState) -checkTypeClasses module_index opt_icl_info class_defs member_defs type_defs modules type_heaps cs +checkTypeClasses :: !Index !(Optional (CopiedDefinitions, Int)) !*{#ClassDef} !*{#MemberDef} !*{#CheckedTypeDef} !*{#DclModule} !*Heaps !*CheckState + -> (!*{#ClassDef}, !*{#MemberDef}, !*{#CheckedTypeDef}, !*{#DclModule}, !*Heaps, !*CheckState) +checkTypeClasses module_index opt_icl_info class_defs member_defs type_defs modules heaps=:{hp_type_heaps} cs #! n_classes = size class_defs - = iFoldSt (check_type_class module_index opt_icl_info) 0 n_classes (class_defs, member_defs, type_defs, modules, type_heaps, cs) + # (class_defs,member_defs,type_defs,modules,hp_type_heaps,cs) + = iFoldSt (check_type_class module_index opt_icl_info) 0 n_classes (class_defs, member_defs, type_defs, modules, hp_type_heaps, cs) + = (class_defs,member_defs,type_defs,modules,{heaps & hp_type_heaps = hp_type_heaps},cs) where check_type_class module_index opt_icl_info class_index (class_defs, member_defs, type_defs, modules, type_heaps, cs=:{cs_symbol_table,cs_error}) | has_to_be_checked module_index opt_icl_info class_index @@ -151,6 +371,7 @@ where # (list_of_specials, (next_inst_index, all_instances, heaps, cs_predef_symbols,cs_error)) = mapSt (checkSpecial mod_index fun_type fun_index) substs (next_inst_index, all_instances, heaps, predef_symbols,error) = (SP_ContextTypes list_of_specials, next_inst_index, all_instances, heaps, cs_predef_symbols,cs_error) + check_specials mod_index fun_type fun_index SP_None next_inst_index all_instances heaps predef_symbols error = (SP_None, next_inst_index, all_instances, heaps, predef_symbols,error) @@ -191,11 +412,13 @@ where checkSpecialsOfInstances mod_index first_mem_index [] next_inst_index all_class_instances all_specials inst_spec_defs all_spec_types heaps predef_symbols error = (next_inst_index, all_class_instances, all_specials, all_spec_types, heaps, predef_symbols,error) -checkMemberTypes :: !Index !(Optional (CopiedDefinitions, Int)) !*{#MemberDef} !*{#CheckedTypeDef} !*{#ClassDef} !*{#DclModule} !*TypeHeaps !*VarHeap !*CheckState - -> (!*{#MemberDef}, !*{#CheckedTypeDef}, !*{#ClassDef}, !*{#DclModule}, !*TypeHeaps, !*VarHeap, !*CheckState) -checkMemberTypes module_index opt_icl_info member_defs type_defs class_defs modules type_heaps var_heap cs +checkMemberTypes :: !Index !(Optional (CopiedDefinitions, Int)) !*{#MemberDef} !*{#CheckedTypeDef} !*{#ClassDef} !*{#DclModule} !*Heaps !*CheckState + -> (!*{#MemberDef}, !*{#CheckedTypeDef}, !*{#ClassDef}, !*{#DclModule}, !*Heaps, !*CheckState) +checkMemberTypes module_index opt_icl_info member_defs type_defs class_defs modules heaps=:{hp_type_heaps,hp_var_heap} cs #! nr_of_members = size member_defs - = iFoldSt (check_class_member module_index opt_icl_info) 0 nr_of_members (member_defs, type_defs, class_defs, modules, type_heaps, var_heap, cs) + # (mds,tds,cds,modules,hp_type_heaps,hp_var_heap,cs) + = iFoldSt (check_class_member module_index opt_icl_info) 0 nr_of_members (member_defs, type_defs, class_defs, modules, hp_type_heaps, hp_var_heap, cs) + = (mds,tds,cds,modules,{heaps & hp_type_heaps = hp_type_heaps,hp_var_heap = hp_var_heap},cs) where check_class_member module_index opt_icl_info member_index (member_defs, type_defs, class_defs, modules, type_heaps, var_heap, cs) # (member_def=:{me_symb,me_type,me_pos,me_class}, member_defs) = member_defs![member_index] @@ -219,17 +442,16 @@ where { is_type_defs :: !.{# CheckedTypeDef} , is_class_defs :: !.{# ClassDef} , is_member_defs :: !.{# MemberDef} - , is_generic_defs :: !.{# GenericDef} // AA , is_modules :: !.{# DclModule} } // AA.. -checkInstanceDefs :: !Index !*{#ClassInstance} !u:{#CheckedTypeDef} !u:{#ClassDef} !u:{#MemberDef} /*AA*/!u:{#GenericDef} !u:{#DclModule} !*TypeHeaps !*CheckState - -> (!.{#ClassInstance},!u:{#CheckedTypeDef},!u:{#ClassDef},!u:{#MemberDef}, /*AA*/!u:{#GenericDef}, !u:{#DclModule},!.TypeHeaps,!.CheckState) -checkInstanceDefs mod_index instance_defs type_defs class_defs member_defs generic_defs modules type_heaps cs - # is = { is_type_defs = type_defs, is_class_defs = class_defs, is_member_defs = member_defs, /*AA*/is_generic_defs = generic_defs, is_modules = modules } - (instance_defs, is, type_heaps, cs) = check_instance_defs 0 mod_index instance_defs is type_heaps cs - = (instance_defs, is.is_type_defs, is.is_class_defs, is.is_member_defs, /*AA*/is.is_generic_defs, is.is_modules, type_heaps, cs) +checkInstanceDefs :: !Index !*{#ClassInstance} !u:{#CheckedTypeDef} !u:{#ClassDef} !u:{#MemberDef} !u:{#DclModule} !*Heaps !*CheckState + -> (!.{#ClassInstance},!u:{#CheckedTypeDef},!u:{#ClassDef},!u:{#MemberDef}, !u:{#DclModule},!.Heaps,!.CheckState) +checkInstanceDefs mod_index instance_defs type_defs class_defs member_defs modules heaps=:{hp_type_heaps} cs + # is = { is_type_defs = type_defs, is_class_defs = class_defs, is_member_defs = member_defs, is_modules = modules } + (instance_defs, is, hp_type_heaps, cs) = check_instance_defs 0 mod_index instance_defs is hp_type_heaps cs + = (instance_defs, is.is_type_defs, is.is_class_defs, is.is_member_defs, is.is_modules, {heaps & hp_type_heaps = hp_type_heaps}, cs) where check_instance_defs :: !Index !Index !*{# ClassInstance} !u:InstanceSymbols !*TypeHeaps !*CheckState -> (!*{# ClassInstance},!u:InstanceSymbols,!*TypeHeaps,!*CheckState) @@ -243,7 +465,7 @@ where check_instance :: !Index !ClassInstance !u:InstanceSymbols !*TypeHeaps !*CheckState -> (!ClassInstance, !u:InstanceSymbols, !*TypeHeaps, !*CheckState) check_instance module_index ins=:{ins_members,ins_class={glob_object = class_name =: {ds_ident = {id_name,id_info},ds_arity}},ins_type,ins_specials,ins_pos,ins_ident} - is=:{is_class_defs,is_generic_defs, is_modules} type_heaps cs=:{cs_symbol_table} + is=:{is_class_defs,is_modules} type_heaps cs=:{cs_symbol_table} # (entry, cs_symbol_table) = readPtr id_info cs_symbol_table # cs = pushErrorAdmin (newPosition ins_ident ins_pos) { cs & cs_symbol_table = cs_symbol_table } # (ins, is, type_heaps, cs) = case entry.ste_kind of @@ -253,13 +475,7 @@ where STE_Imported STE_Class decl_index # (class_def, is) = class_by_module_index decl_index entry.ste_index is -> check_class_instance class_def module_index entry.ste_index decl_index ins is type_heaps cs - STE_Generic - # (generic_def, is) = generic_by_index entry.ste_index is - -> check_generic_instance generic_def module_index entry.ste_index module_index ins is type_heaps cs - STE_Imported STE_Generic decl_index - # (gen_def, is) = generic_by_module_index decl_index entry.ste_index is - -> check_generic_instance gen_def module_index entry.ste_index decl_index ins is type_heaps cs - ste -> (ins, is, type_heaps, { cs & cs_error = checkError id_name "class or generic undefined" cs.cs_error }) + ste -> (ins, is, type_heaps, { cs & cs_error = checkError id_name "class undefined" cs.cs_error }) = (ins, is, type_heaps, popErrorAdmin cs) where @@ -270,20 +486,13 @@ where # (dcl_mod, is_modules) = is_modules![decl_index] class_def = dcl_mod.dcl_common.com_class_defs.[class_index] = (class_def, {is & is_modules = is_modules }) - generic_by_index gen_index is=:{is_generic_defs} - # (gen_def, is_generic_defs) = is_generic_defs![gen_index] - = (gen_def, {is & is_generic_defs = is_generic_defs}) - generic_by_module_index decl_index gen_index is=:{is_modules} - # (dcl_mod, is_modules) = is_modules![decl_index] - gen_def = dcl_mod.dcl_common.com_generic_defs.[gen_index] - = (gen_def, {is & is_modules = is_modules }) - + check_class_instance :: ClassDef !Index !Index !Index !ClassInstance !u:InstanceSymbols !*TypeHeaps !*CheckState -> (!ClassInstance, !u:InstanceSymbols, !*TypeHeaps, !*CheckState) check_class_instance class_def module_index class_index class_mod_index - ins=:{ins_members,ins_class={glob_object = class_name =: {ds_ident = {id_name,id_info},ds_arity}},ins_type,ins_specials,ins_pos,ins_ident,ins_generate} + ins=:{ins_members,ins_class={glob_object = class_name =: {ds_ident = {id_name,id_info},ds_arity}},ins_type,ins_specials,ins_pos,ins_ident,ins_generated} is=:{is_class_defs,is_modules} type_heaps cs=:{cs_symbol_table} - | ins_generate + | ins_generated = ( ins, is, type_heaps , { cs & cs_error = checkError id_name "cannot generate class instance" cs.cs_error } ) @@ -298,40 +507,6 @@ where = ( ins, is, type_heaps , { cs & cs_error = checkError id_name ("wrong arity: expected "+++toString class_def.class_arity+++" found "+++toString ds_arity) cs.cs_error } ) - check_generic_instance :: GenericDef !Index !Index !Index !ClassInstance !u:InstanceSymbols !*TypeHeaps !*CheckState -> (!ClassInstance, !u:InstanceSymbols, !*TypeHeaps, !*CheckState) - check_generic_instance - {gen_member_name} - module_index generic_index generic_module_index - ins=:{ - ins_class={glob_object = class_name =: {ds_ident = {id_name,id_info},ds_arity, ds_index} }, - ins_members, ins_type, ins_specials, ins_pos, ins_ident, ins_is_generic, ins_generate - } - is=:{is_class_defs,is_modules} - type_heaps - cs=:{cs_symbol_table, cs_error} - # class_name = {class_name & ds_index = generic_index} - # ins_class = { glob_object = class_name, glob_module = generic_module_index} - | ds_arity == 1 - # (ins_type, ins_specials, is_type_defs, is_class_defs, is_modules, type_heaps, cs) - = checkInstanceType module_index ins_class ins_type ins_specials - is.is_type_defs is.is_class_defs is.is_modules type_heaps cs - # is = { is & is_type_defs = is_type_defs, is_class_defs = is_class_defs, is_modules = is_modules } - # ins = - { ins - & ins_is_generic = True - , ins_generic = {glob_module = generic_module_index, glob_object = generic_index} - , ins_class = ins_class - , ins_type = ins_type - , ins_specials = ins_specials - , ins_members = if ins_generate - {{ds_arity = 0, ds_index = NoIndex, ds_ident = gen_member_name}} - ins_members - } - = (ins, is, type_heaps, cs) - // otherwise - # cs_error = checkError id_name "arity of a generic instance must be 1" cs_error - # cs = {cs & cs_error = cs_error} - = (ins, is, type_heaps, cs) checkInstances :: !Index !*CommonDefs !u:{# DclModule} !*VarHeap !*TypeHeaps !*CheckState -> (![(Index,SymbolType)], !*CommonDefs, !u:{# DclModule}, !*VarHeap , !*TypeHeaps, !*CheckState) @@ -348,15 +523,16 @@ where -> (![(Index,SymbolType)], !x:{# ClassInstance}, !w:{# ClassDef}, !v:{# MemberDef}, /*AA*/!w:{# GenericDef}, !nerd:{# CheckedTypeDef}, !u:{# DclModule}, !*VarHeap, !*TypeHeaps, !*CheckState) check_instances inst_index mod_index instance_types instance_defs class_defs member_defs generic_defs type_defs modules var_heap type_heaps cs | inst_index < size instance_defs - # (instance_def=:{ins_ident,ins_is_generic, ins_pos}, instance_defs) = instance_defs![inst_index] + # (instance_def=:{ins_ident, ins_pos}, instance_defs) = instance_defs![inst_index] # (instance_types, class_defs, member_defs, generic_defs, type_defs, modules, var_heap, type_heaps, cs) = - (if ins_is_generic check_generic_instance check_class_instance) - instance_def mod_index instance_types class_defs member_defs generic_defs type_defs modules var_heap type_heaps cs + check_class_instance instance_def mod_index instance_types class_defs member_defs generic_defs type_defs modules var_heap type_heaps cs = check_instances (inc inst_index) mod_index instance_types instance_defs class_defs member_defs generic_defs type_defs modules var_heap type_heaps cs // otherwise = (instance_types, instance_defs, class_defs, member_defs, generic_defs, type_defs, modules, var_heap, type_heaps, cs) - check_class_instance {ins_pos,ins_class,ins_members,ins_type} mod_index instance_types class_defs member_defs generic_defs type_defs modules var_heap type_heaps cs + check_class_instance {ins_pos,ins_class,ins_members,ins_type, ins_generated} mod_index instance_types class_defs member_defs generic_defs type_defs modules var_heap type_heaps cs + | ins_generated + = (instance_types, class_defs, member_defs, generic_defs, type_defs, modules, var_heap, type_heaps, cs) # ({class_members,class_name}, class_defs, modules) = getClassDef ins_class mod_index class_defs modules class_size = size class_members | class_size == size ins_members @@ -368,6 +544,7 @@ where # cs = { cs & cs_error = checkError class_name "different number of members specified" cs.cs_error } = (instance_types, class_defs, member_defs, generic_defs, type_defs, modules, var_heap, type_heaps, cs) +/* check_generic_instance {ins_class, ins_members, ins_generate} mod_index instance_types class_defs member_defs generic_defs type_defs modules var_heap type_heaps cs # ({gen_name, gen_member_name}, generic_defs, modules) = getGenericDef ins_class mod_index generic_defs modules //| ins_generate @@ -381,7 +558,7 @@ where = (instance_types, class_defs, member_defs, generic_defs, type_defs, modules, var_heap, type_heaps, cs) // otherwise = (instance_types, class_defs, member_defs, generic_defs, type_defs, modules, var_heap, type_heaps, cs) - +*/ check_member_instances :: !Index !Index !Int !Int !{#DefinedSymbol} !{#DefinedSymbol} Ident !Position !InstanceType ![(Index,SymbolType)] !v:{# MemberDef} !blah:{# CheckedTypeDef} !u:{# DclModule} !*VarHeap !*TypeHeaps !*CheckState -> (![(Index,SymbolType)], !v:{# MemberDef}, !blah:{# CheckedTypeDef}, !u:{# DclModule},!*VarHeap, !*TypeHeaps, !*CheckState) @@ -613,51 +790,41 @@ getTypeDef x_main_dcl_module_n {glob_module,glob_object} type_defs modules = modules![glob_module].dcl_common.com_type_defs.[glob_object] = (type_def, type_defs, modules) -determineTypesOfInstances :: !Index !Index !*{#ClassInstance} !*{# ClassDef} !*{# MemberDef} !*{#GenericDef} +determineTypesOfInstances :: !Index !Index !*{#ClassInstance} !*{# ClassDef} !*{# MemberDef} !*{#DclModule} !*TypeHeaps !*VarHeap !*CheckState - -> (![FunType], !Index, ![ClassInstance], !*{#ClassInstance}, !*{# ClassDef}, !*{# MemberDef}, !*{#GenericDef}, !*{#DclModule}, !*TypeHeaps, !*VarHeap, !*CheckState) -determineTypesOfInstances first_memb_inst_index mod_index com_instance_defs com_class_defs com_member_defs com_generic_defs + -> (![FunType], !Index, ![ClassInstance], !*{#ClassInstance}, !*{# ClassDef}, !*{# MemberDef}, !*{#DclModule}, !*TypeHeaps, !*VarHeap, !*CheckState) +determineTypesOfInstances first_memb_inst_index mod_index com_instance_defs com_class_defs com_member_defs modules type_heaps var_heap cs=:{cs_error,cs_predef_symbols,cs_x={x_main_dcl_module_n}} | cs_error.ea_ok #! nr_of_class_instances = size com_instance_defs - # (memb_inst_defs, next_mem_inst_index, all_class_specials, com_class_defs, com_member_defs, com_generic_defs, modules, com_instance_defs, type_heaps, var_heap, cs_predef_symbols,cs_error) - = determine_types_of_instances x_main_dcl_module_n 0 nr_of_class_instances first_memb_inst_index mod_index [] com_class_defs com_member_defs com_generic_defs + # (memb_inst_defs, next_mem_inst_index, all_class_specials, com_class_defs, com_member_defs, modules, com_instance_defs, type_heaps, var_heap, cs_predef_symbols,cs_error) + = determine_types_of_instances x_main_dcl_module_n 0 nr_of_class_instances first_memb_inst_index mod_index [] com_class_defs com_member_defs modules com_instance_defs type_heaps var_heap cs_predef_symbols cs_error = (memb_inst_defs, next_mem_inst_index, all_class_specials, com_instance_defs, com_class_defs, - com_member_defs, com_generic_defs, modules, type_heaps, var_heap, { cs & cs_predef_symbols=cs_predef_symbols,cs_error = cs_error }) - = ([], first_memb_inst_index, [], com_instance_defs, com_class_defs, com_member_defs, com_generic_defs, modules, type_heaps, var_heap, cs) + com_member_defs, modules, type_heaps, var_heap, { cs & cs_predef_symbols=cs_predef_symbols,cs_error = cs_error }) + = ([], first_memb_inst_index, [], com_instance_defs, com_class_defs, com_member_defs, modules, type_heaps, var_heap, cs) where - determine_types_of_instances :: !Index !Index !Index !Index !Index ![ClassInstance] !v:{#ClassDef} !w:{#MemberDef} !y:{#GenericDef} + determine_types_of_instances :: !Index !Index !Index !Index !Index ![ClassInstance] !v:{#ClassDef} !w:{#MemberDef} !x:{#DclModule} !*{#ClassInstance} !*TypeHeaps !*VarHeap !*PredefinedSymbols !*ErrorAdmin - -> (![FunType], !Index, ![ClassInstance], !v:{#ClassDef}, !w:{#MemberDef}, !y:{#GenericDef}, !x:{#DclModule}, !*{#ClassInstance}, !*TypeHeaps, !*VarHeap, !*PredefinedSymbols,!*ErrorAdmin) + -> (![FunType], !Index, ![ClassInstance], !v:{#ClassDef}, !w:{#MemberDef}, !x:{#DclModule}, !*{#ClassInstance}, !*TypeHeaps, !*VarHeap, !*PredefinedSymbols,!*ErrorAdmin) determine_types_of_instances x_main_dcl_module_n inst_index next_class_inst_index next_mem_inst_index mod_index all_class_specials - class_defs member_defs generic_defs modules instance_defs type_heaps var_heap predef_symbols error + class_defs member_defs modules instance_defs type_heaps var_heap predef_symbols error | inst_index < size instance_defs # (instance_def, instance_defs) = instance_defs![inst_index] - # {ins_class,ins_pos,ins_type,ins_specials, ins_is_generic} = instance_def - | ins_is_generic - # ({gen_member_name}, generic_defs, modules) = getGenericDef ins_class mod_index generic_defs modules - # ins_member = {ds_ident=gen_member_name, ds_arity= -1, ds_index = next_mem_inst_index} - # instance_def = { instance_def & ins_members = {ins_member}} - # class_size = 1 - # (new_info_ptr, var_heap) = newPtr VI_Empty var_heap + # {ins_class,ins_pos,ins_type,ins_specials, ins_generated} = instance_def + | ins_generated + + // REMOVE ins_generated functionality # empty_st = { st_vars = [] , st_args = [] - , st_args_strictness=NotStrict , st_arity = -1 - , st_result = {at_type=TE, at_attribute=TA_None} + , st_result = {at_type=TE, at_attribute=TA_None, at_annotation=AN_None} , st_context = [] , st_attr_vars = [] , st_attr_env = [] - } - # memb_inst_def = MakeNewFunctionType gen_member_name 0 NoPrio empty_st ins_pos SP_None new_info_ptr - # memb_inst_defs1 = [memb_inst_def] - # (memb_inst_defs2, next_mem_inst_index, all_class_specials, class_defs, member_defs, generic_defs, modules, instance_defs, type_heaps, var_heap, predef_symbols,error) - = determine_types_of_instances x_main_dcl_module_n (inc inst_index) next_class_inst_index - (next_mem_inst_index + class_size) mod_index all_class_specials class_defs member_defs generic_defs modules - { instance_defs & [inst_index] = { instance_def & ins_specials = ins_specials }} type_heaps var_heap predef_symbols error - = (memb_inst_defs1 ++ memb_inst_defs2, next_mem_inst_index, all_class_specials, class_defs, member_defs, generic_defs, modules, instance_defs, type_heaps, var_heap ,predef_symbols,error) + } + = undef # ({class_name, class_members}, class_defs, modules) = getClassDef ins_class mod_index class_defs modules class_size = size class_members (ins_members, memb_inst_defs1, member_defs, modules, type_heaps, var_heap, error) @@ -666,12 +833,12 @@ where instance_def = { instance_def & ins_members = { member \\ member <- ins_members }} (ins_specials, next_class_inst_index, all_class_specials, type_heaps, predef_symbols,error) = check_instance_specials mod_index instance_def inst_index ins_specials next_class_inst_index all_class_specials type_heaps predef_symbols error - (memb_inst_defs2, next_mem_inst_index, all_class_specials, class_defs, member_defs, generic_defs, modules, instance_defs, type_heaps, var_heap, predef_symbols,error) + (memb_inst_defs2, next_mem_inst_index, all_class_specials, class_defs, member_defs, modules, instance_defs, type_heaps, var_heap, predef_symbols,error) = determine_types_of_instances x_main_dcl_module_n (inc inst_index) next_class_inst_index (next_mem_inst_index + class_size) mod_index all_class_specials - class_defs member_defs generic_defs modules { instance_defs & [inst_index] = { instance_def & ins_specials = ins_specials }} type_heaps var_heap predef_symbols error + class_defs member_defs modules { instance_defs & [inst_index] = { instance_def & ins_specials = ins_specials }} type_heaps var_heap predef_symbols error - = (memb_inst_defs1 ++ memb_inst_defs2, next_mem_inst_index, all_class_specials, class_defs, member_defs, generic_defs, modules, instance_defs, type_heaps, var_heap, predef_symbols,error) - = ([], next_mem_inst_index, all_class_specials, class_defs, member_defs, generic_defs, modules, instance_defs, type_heaps, var_heap, predef_symbols,error) + = (memb_inst_defs1 ++ memb_inst_defs2, next_mem_inst_index, all_class_specials, class_defs, member_defs, modules, instance_defs, type_heaps, var_heap, predef_symbols,error) + = ([], next_mem_inst_index, all_class_specials, class_defs, member_defs, modules, instance_defs, type_heaps, var_heap, predef_symbols,error) determine_instance_symbols_and_types :: !Index !Index !Index !Index !Index !Int !{#DefinedSymbol} !InstanceType !Specials Ident !Position !w:{#MemberDef} !u:{#DclModule} !*TypeHeaps !*VarHeap !*ErrorAdmin @@ -902,19 +1069,19 @@ checkFunctionBodyIfMacro _ def ea checkFunction :: !FunDef !Index !FunctionOrMacroIndex !Level !Int !*{#FunDef} !*ExpressionInfo !*Heaps !*CheckState -> (!FunDef,!*{#FunDef},!*ExpressionInfo,!*Heaps,!*CheckState); checkFunction fun_def=:{fun_symb,fun_pos,fun_body,fun_type,fun_kind} mod_index fun_index def_level local_functions_index_offset - fun_defs e_info=:{ef_type_defs,ef_modules,ef_class_defs,ef_is_macro_fun} heaps=:{hp_var_heap,hp_expression_heap,hp_type_heaps} cs=:{cs_error} + fun_defs e_info=:{ef_type_defs,ef_modules,ef_class_defs,ef_is_macro_fun} heaps=:{hp_var_heap,hp_expression_heap,hp_type_heaps,hp_generic_heap} cs=:{cs_error} # function_ident_for_errors = ident_for_errors_from_fun_symb_and_fun_kind fun_symb fun_kind # cs = {cs & cs_error = pushErrorAdmin (newPosition function_ident_for_errors fun_pos) cs_error} (fun_type, ef_type_defs, ef_class_defs, ef_modules, hp_var_heap, hp_type_heaps, cs) = check_function_type fun_type mod_index (fun_kind == FK_Caf) ef_type_defs ef_class_defs ef_modules hp_var_heap hp_type_heaps cs e_info = { e_info & ef_type_defs = ef_type_defs, ef_class_defs = ef_class_defs, ef_modules = ef_modules } - e_state = { es_var_heap = hp_var_heap, es_expr_heap = hp_expression_heap, es_type_heaps = hp_type_heaps, - es_dynamics = [], es_calls = [], es_fun_defs = fun_defs } + e_state = { es_var_heap = hp_var_heap, es_expr_heap = hp_expression_heap, es_type_heaps = hp_type_heaps,es_generic_heap=hp_generic_heap, + es_dynamics = [], es_calls = [], es_fun_defs = fun_defs} e_input = { ei_expr_level = inc def_level, ei_fun_index = fun_index, ei_fun_level = inc def_level, ei_mod_index = mod_index, ei_local_functions_index_offset=local_functions_index_offset } (fun_body, free_vars, e_state, e_info, cs) = checkFunctionBodies fun_body function_ident_for_errors e_input e_state e_info cs - # {es_fun_defs,es_calls,es_var_heap,es_expr_heap,es_type_heaps,es_dynamics} = e_state + # {es_fun_defs,es_calls,es_var_heap,es_expr_heap,es_type_heaps,es_generic_heap,es_dynamics} = e_state (ef_type_defs, ef_modules, es_type_heaps, es_expr_heap, cs) = checkDynamicTypes mod_index es_dynamics fun_type e_info.ef_type_defs e_info.ef_modules es_type_heaps es_expr_heap cs (fun_body, cs_error) = checkFunctionBodyIfMacro fun_kind fun_body cs.cs_error @@ -927,7 +1094,7 @@ checkFunction fun_def=:{fun_symb,fun_pos,fun_body,fun_type,fun_kind} mod_index f (fun_defs,macro_defs,cs_symbol_table) = remove_calls_from_symbol_table fun_index def_level es_calls e_state.es_fun_defs e_info.ef_macro_defs cs.cs_symbol_table = (fun_def,fun_defs, { e_info & ef_type_defs = ef_type_defs, ef_modules = ef_modules,ef_macro_defs=macro_defs }, - { heaps & hp_var_heap = es_var_heap, hp_expression_heap = es_expr_heap, hp_type_heaps = es_type_heaps }, + { heaps & hp_var_heap = es_var_heap, hp_expression_heap = es_expr_heap, hp_type_heaps = es_type_heaps,hp_generic_heap=es_generic_heap }, { cs & cs_symbol_table = cs_symbol_table }) where @@ -994,7 +1161,7 @@ checkFunctions mod_index level fun_index to_index local_functions_index_offset f # (fun_def,fun_defs, e_info, heaps, cs) = checkFunction fun_def mod_index (FunctionOrIclMacroIndex fun_index) level local_functions_index_offset fun_defs e_info heaps cs # fun_defs = { fun_defs & [fun_index] = fun_def } = checkFunctions mod_index level (inc fun_index) to_index local_functions_index_offset fun_defs e_info heaps cs - + checkDclMacros :: !Index !Level !Index !Index !*{#FunDef} !*ExpressionInfo !*Heaps !*CheckState -> (!*{#FunDef},!*ExpressionInfo,!*Heaps,!*CheckState) checkDclMacros mod_index level fun_index to_index fun_defs e_info heaps cs @@ -1048,54 +1215,63 @@ where (<) fd1 fd2 = fd1.fun_symb.id_name < fd2.fun_symb.id_name createCommonDefinitions :: (CollectedDefinitions ClassInstance a) -> .CommonDefs; -createCommonDefinitions {def_types,def_constructors,def_selectors,def_classes,def_members,def_instances, def_generics} +createCommonDefinitions {def_types,def_constructors,def_selectors,def_classes,def_members,def_instances, def_generics,def_generic_cases} = { com_type_defs = { type \\ type <- def_types } , com_cons_defs = { cons \\ cons <- def_constructors } , com_selector_defs = { sel \\ sel <- def_selectors } , com_class_defs = { class_def \\ class_def <- def_classes } , com_member_defs = { member \\ member <- def_members } , com_instance_defs = { next_instance \\ next_instance <- def_instances } - , com_generic_defs = { gen \\ gen <- def_generics } + , com_generic_defs = { gen \\ gen <- def_generics } + , com_gencase_defs = { gi \\ gi <- def_generic_cases} } array_plus_list a [] = a array_plus_list a l = arrayPlusList a l -checkCommonDefinitions :: !(Optional (CopiedDefinitions, Int)) !Index !*CommonDefs !*{# DclModule} !*TypeHeaps !*VarHeap !*CheckState - -> (!DictionaryInfo,!*CommonDefs,!*{# DclModule},!*TypeHeaps,!*VarHeap,!*CheckState) -checkCommonDefinitions opt_icl_info module_index common modules type_heaps var_heap cs - # (com_type_defs, com_cons_defs, com_selector_defs, modules, var_heap, type_heaps, cs) +checkCommonDefinitions :: !(Optional (CopiedDefinitions, Int)) !Index !*CommonDefs !*{# DclModule} !*Heaps !*CheckState + -> (!DictionaryInfo,!*CommonDefs,!*{# DclModule},!*Heaps, !*CheckState) +checkCommonDefinitions opt_icl_info module_index common modules heaps cs + # (com_type_defs, com_cons_defs, com_selector_defs, modules, heaps, cs) = checkTypeDefs module_index opt_icl_info - common.com_type_defs common.com_cons_defs common.com_selector_defs modules var_heap type_heaps cs - (com_class_defs, com_member_defs, com_type_defs, modules, type_heaps, cs) - = checkTypeClasses module_index opt_icl_info common.com_class_defs common.com_member_defs com_type_defs modules type_heaps cs - (com_member_defs, com_type_defs, com_class_defs, modules, type_heaps, var_heap, cs) - = checkMemberTypes module_index opt_icl_info com_member_defs com_type_defs com_class_defs modules type_heaps var_heap cs - (com_generic_defs, com_class_defs, com_type_defs, modules, type_heaps, cs) - = checkGenerics 0 module_index common.com_generic_defs com_class_defs com_type_defs modules type_heaps cs - (com_instance_defs, com_type_defs, com_class_defs, com_member_defs, com_generic_defs, modules, type_heaps, cs) - = checkInstanceDefs module_index common.com_instance_defs com_type_defs com_class_defs com_member_defs com_generic_defs modules type_heaps cs + common.com_type_defs common.com_cons_defs common.com_selector_defs modules heaps cs + (com_class_defs, com_member_defs, com_type_defs, modules, heaps, cs) + = checkTypeClasses module_index opt_icl_info common.com_class_defs common.com_member_defs com_type_defs modules heaps cs + (com_member_defs, com_type_defs, com_class_defs, modules, heaps, cs) + = checkMemberTypes module_index opt_icl_info com_member_defs com_type_defs com_class_defs modules heaps cs + (com_instance_defs, com_type_defs, com_class_defs, com_member_defs, modules, heaps, cs) + = checkInstanceDefs module_index common.com_instance_defs com_type_defs com_class_defs com_member_defs modules heaps cs +//AA.. + (com_generic_defs, com_type_defs, com_class_defs, modules, heaps, cs) + = checkGenericDefs module_index opt_icl_info common.com_generic_defs com_type_defs com_class_defs modules heaps cs + (com_gencase_defs, com_generic_defs, com_type_defs, modules, heaps, cs) + = checkGenericCaseDefs module_index common.com_gencase_defs com_generic_defs com_type_defs modules heaps cs +//..AA + (size_com_type_defs,com_type_defs) = usize com_type_defs (size_com_selector_defs,com_selector_defs) = usize com_selector_defs (size_com_cons_defs,com_cons_defs) = usize com_cons_defs - is_dcl = case opt_icl_info of No -> True ; Yes _ -> False - (new_type_defs, new_selector_defs, new_cons_defs,dictionary_info,com_type_defs,com_selector_defs, com_cons_defs, com_class_defs, modules, th_vars, var_heap, cs_symbol_table) + {hp_var_heap, hp_type_heaps=hp_type_heaps=:{th_vars} } = heaps + is_dcl = case opt_icl_info of No -> True ; Yes _ -> False + (new_type_defs, new_selector_defs, new_cons_defs,dictionary_info,com_type_defs,com_selector_defs, com_cons_defs, com_class_defs, modules, th_vars, hp_var_heap, cs_symbol_table) = createClassDictionaries is_dcl module_index size_com_type_defs size_com_selector_defs size_com_cons_defs - com_type_defs com_selector_defs com_cons_defs com_class_defs modules type_heaps.th_vars var_heap cs.cs_symbol_table + com_type_defs com_selector_defs com_cons_defs com_class_defs modules th_vars hp_var_heap cs.cs_symbol_table com_type_defs = array_plus_list com_type_defs new_type_defs com_selector_defs = array_plus_list com_selector_defs new_selector_defs com_cons_defs = array_plus_list com_cons_defs new_cons_defs common = {common & com_type_defs = com_type_defs, com_cons_defs = com_cons_defs, com_selector_defs = com_selector_defs, com_class_defs = com_class_defs, - com_member_defs = com_member_defs, com_instance_defs = com_instance_defs, com_generic_defs = com_generic_defs } - - = (dictionary_info,common, modules, { type_heaps & th_vars = th_vars }, var_heap, { cs & cs_symbol_table = cs_symbol_table }) + com_member_defs = com_member_defs, com_instance_defs = com_instance_defs, + com_generic_defs = com_generic_defs, com_gencase_defs = com_gencase_defs} + heaps = {heaps & hp_var_heap=hp_var_heap,hp_type_heaps={hp_type_heaps & th_vars=th_vars}} + = (dictionary_info,common, modules, heaps, { cs & cs_symbol_table = cs_symbol_table }) + collectCommonfinitions :: !(CollectedDefinitions ClassInstance a) -> (!*{# Int}, ![Declaration]) -collectCommonfinitions {def_types,def_constructors,def_selectors,def_classes,def_members,def_instances, def_generics} +collectCommonfinitions {def_types,def_constructors,def_selectors,def_classes,def_members,def_instances, def_generic_cases, def_generics} // MW: the order in which the declarations appear in the returned list is essential (explicit imports) # sizes = createArray cConversionTableSize 0 (size, defs) = foldSt cons_def_to_dcl def_constructors (0, []) @@ -1112,6 +1288,8 @@ collectCommonfinitions {def_types,def_constructors,def_selectors,def_classes,def sizes = { sizes & [cInstanceDefs] = size } (size, defs) = foldSt generic_def_to_dcl def_generics (0, defs) sizes = { sizes & [cGenericDefs] = size } + (size, defs) = foldSt gen_case_def_to_dcl def_generic_cases (0, defs) + sizes = { sizes & [cGenericCaseDefs] = size } = (sizes, defs) where type_def_to_dcl {td_name, td_pos} (decl_index, decls) @@ -1130,6 +1308,8 @@ where # generic_decl = Declaration { decl_ident = gen_name, decl_pos = gen_pos, decl_kind = STE_Generic, decl_index = decl_index } # member_decl = Declaration { decl_ident = gen_member_name, decl_pos = gen_pos, decl_kind = STE_Generic, decl_index = decl_index } = (inc decl_index, [generic_decl, member_decl : decls]) + gen_case_def_to_dcl {gc_name, gc_pos} (decl_index, decls) + = (inc decl_index, [Declaration { decl_ident = gc_name, decl_pos = gc_pos, decl_kind = STE_GenericCase, decl_index = decl_index } : decls]) collectMacros {ir_from,ir_to} macro_defs sizes_defs = collectGlobalFunctions cMacroDefs ir_from ir_to macro_defs sizes_defs @@ -1168,16 +1348,16 @@ create_icl_to_dcl_index_table :: !ModuleKind !{#Int} IndexRange !Int !(Optional create_icl_to_dcl_index_table MK_Main icl_sizes icl_global_function_range main_dcl_module_n dcl_conversions modules fun_defs = (No,No,modules,fun_defs) create_icl_to_dcl_index_table _ icl_sizes icl_global_function_range main_dcl_module_n old_conversions modules fun_defs - # (size_icl_functions,fun_defs) = usize fun_defs - # icl_sizes = make_icl_sizes + #! (size_icl_functions,fun_defs) = usize fun_defs + #! icl_sizes = make_icl_sizes with make_icl_sizes :: *{#Int} make_icl_sizes => {{icl_sizes.[i] \\ i<-[0..cMacroDefs-1]} & [cFunctionDefs]=size_icl_functions} - # (dcl_mod,modules) = modules![main_dcl_module_n] - # dictionary_info=dcl_mod.dcl_dictionary_info + #! (dcl_mod,modules) = modules![main_dcl_module_n] + #! dictionary_info=dcl_mod.dcl_dictionary_info # (Yes conversion_table) = old_conversions - # icl_to_dcl_index_table = {create_icl_to_dcl_index_table_for_kind table_size dcl_to_icl_table table_kind dictionary_info \\ table_kind<-[0..] & table_size <-: icl_sizes & dcl_to_icl_table <-: conversion_table } - # modules = {modules & [main_dcl_module_n].dcl_macro_conversions=Yes conversion_table.[cMacroDefs]} + #! icl_to_dcl_index_table = {create_icl_to_dcl_index_table_for_kind table_size dcl_to_icl_table table_kind dictionary_info \\ table_kind<-[0..] & table_size <-: icl_sizes & dcl_to_icl_table <-: conversion_table } + #! modules = {modules & [main_dcl_module_n].dcl_macro_conversions=Yes conversion_table.[cMacroDefs]} = (Yes icl_to_dcl_index_table,old_conversions,modules,fun_defs) recompute_icl_to_dcl_index_table_for_functions No dcl_icl_conversions n_functions @@ -1221,6 +1401,22 @@ renumber_member_indexes_of_class_instances (Yes icl_to_dcl_index_table) class_in = renumber_member_indexes_of_class_instances (class_inst_index+1) class_instances = class_instances +renumber_members_of_gencases No gencases + = gencases +renumber_members_of_gencases (Yes icl_to_dcl_index_table) gencases + = renumber 0 gencases +where + function_conversion_table = icl_to_dcl_index_table.[cFunctionDefs] + + renumber gencase_index gencases + | gencase_index < size gencases + # (gencase=:{gc_body = GCB_FunIndex icl_index}, gencases) = gencases ! [gencase_index] + # dcl_index = function_conversion_table.[icl_index] + # gencase = { gencase & gc_body = GCB_FunIndex dcl_index } + # gencases = { gencases & [gencase_index] = gencase } + = renumber (inc gencase_index) gencases + = gencases + renumber_icl_definitions_as_dcl_definitions :: !(Optional {{#Int}}) !{#Int} IndexRange !Int ![Declaration] !*{#DclModule} !*CommonDefs !*{#FunDef} -> (![Declaration],!.{#DclModule},!.CommonDefs,!*{#FunDef}) renumber_icl_definitions_as_dcl_definitions No icl_sizes icl_global_function_range main_dcl_module_n icl_decl_symbols modules cdefs fun_defs @@ -1268,6 +1464,9 @@ renumber_icl_definitions_as_dcl_definitions (Yes icl_to_dcl_index_table) icl_siz = (Declaration {icl_decl_symbol & decl_index=icl_to_dcl_index_table.[cInstanceDefs,decl_index]},cdefs) renumber_icl_decl_symbol (Declaration icl_decl_symbol=:{decl_kind = STE_Generic, decl_index}) cdefs = (Declaration {icl_decl_symbol & decl_index=icl_to_dcl_index_table.[cGenericDefs,decl_index]},cdefs) + renumber_icl_decl_symbol (Declaration icl_decl_symbol=:{decl_kind = STE_GenericCase, decl_index}) cdefs + = (Declaration {icl_decl_symbol & decl_index=icl_to_dcl_index_table.[cGenericCaseDefs,decl_index]},cdefs) + //---> ("renumber generic case", icl_decl_symbol.decl_ident, decl_index, icl_to_dcl_index_table.[cGenericCaseDefs,decl_index]) renumber_icl_decl_symbol icl_decl=:(Declaration icl_decl_symbol=:{decl_kind=STE_FunctionOrMacro _, decl_index}) cdefs // | decl_index>=icl_global_function_range.ir_from && decl_index<icl_global_function_range.ir_to = (Declaration {icl_decl_symbol & decl_index=icl_to_dcl_index_table.[cFunctionDefs,decl_index]},cdefs) @@ -1278,7 +1477,7 @@ renumber_icl_definitions_as_dcl_definitions (Yes icl_to_dcl_index_table) icl_siz # {n_dictionary_types,n_dictionary_selectors,n_dictionary_constructors}=dcl_mod.dcl_dictionary_info # cdefs=reorder_common_definitions cdefs with - reorder_common_definitions {com_type_defs,com_cons_defs,com_selector_defs,com_class_defs,com_member_defs,com_instance_defs,com_generic_defs} + reorder_common_definitions {com_type_defs,com_cons_defs,com_selector_defs,com_class_defs,com_member_defs,com_instance_defs,com_generic_defs,com_gencase_defs} # dummy_ident = {id_name="",id_info=nilPtr} # com_type_defs=reorder_and_enlarge_array com_type_defs n_dictionary_types icl_to_dcl_index_table.[cTypeDefs] {td_name=dummy_ident,td_index= -1,td_arity=0,td_args=[],td_attrs=[],td_context=[],td_rhs=UnknownType,td_attribute=TA_None,td_pos=NoPos,td_used_types=[]} @@ -1291,9 +1490,11 @@ renumber_icl_definitions_as_dcl_definitions (Yes icl_to_dcl_index_table) icl_siz # com_member_defs=reorder_array com_member_defs icl_to_dcl_index_table.[cMemberDefs] # com_instance_defs=reorder_array com_instance_defs icl_to_dcl_index_table.[cInstanceDefs] # com_generic_defs=reorder_array com_generic_defs icl_to_dcl_index_table.[cGenericDefs] + # com_gencase_defs=reorder_array com_gencase_defs icl_to_dcl_index_table.[cGenericCaseDefs] = { com_type_defs=com_type_defs,com_cons_defs=com_cons_defs,com_selector_defs=com_selector_defs, - com_class_defs=com_class_defs,com_member_defs=com_member_defs,com_instance_defs=com_instance_defs,com_generic_defs=com_generic_defs + com_class_defs=com_class_defs,com_member_defs=com_member_defs,com_instance_defs=com_instance_defs, + com_generic_defs=com_generic_defs,com_gencase_defs=com_gencase_defs } # fun_defs = reorder_array fun_defs icl_to_dcl_index_table.[cFunctionDefs] = (icl_decl_symbols,modules,cdefs,fun_defs) @@ -1309,7 +1510,7 @@ renumber_icl_definitions_as_dcl_definitions (Yes icl_to_dcl_index_table) icl_siz combineDclAndIclModule :: ModuleKind *{#DclModule} [Declaration] (CollectedDefinitions a b) *{#Int} *CheckState -> (!CopiedDefinitions,!Optional {#{#Int}},!*{#DclModule},![Declaration],!CollectedDefinitions a b, !*{#Int}, !*CheckState); combineDclAndIclModule MK_Main modules icl_decl_symbols icl_definitions icl_sizes cs - = ({ copied_type_defs = {}, copied_class_defs = {} }, No, modules, icl_decl_symbols, icl_definitions, icl_sizes, cs) + = ({ copied_type_defs = {}, copied_class_defs = {}, copied_generic_defs = {}}, No, modules, icl_decl_symbols, icl_definitions, icl_sizes, cs) combineDclAndIclModule _ modules icl_decl_symbols icl_definitions icl_sizes cs #! main_dcl_module_n=cs.cs_x.x_main_dcl_module_n # (dcl_mod=:{dcl_declared={dcls_local},dcl_macros, dcl_sizes, dcl_common}, modules) = modules![main_dcl_module_n] @@ -1318,8 +1519,8 @@ combineDclAndIclModule _ modules icl_decl_symbols icl_definitions icl_sizes cs (moved_dcl_defs,dcl_cons_and_member_defs,conversion_table, icl_sizes, icl_decl_symbols, cs) = foldSt (add_to_conversion_table dcl_macros.ir_from dcl_common) dcls_local ([],[],{ createArray size NoIndex \\ size <-: dcl_sizes }, icl_sizes, icl_decl_symbols, cs) - (new_type_defs, new_class_defs, new_cons_defs, new_selector_defs, new_member_defs, new_generic_defs, (cop_td_indexes, cop_cd_indexes), conversion_table, icl_sizes, icl_decl_symbols, cs) - = foldSt (add_dcl_definition dcl_common) moved_dcl_defs ([], [], [], [], [], [], ([], []), conversion_table, icl_sizes, icl_decl_symbols, cs) + (new_type_defs, new_class_defs, new_cons_defs, new_selector_defs, new_member_defs, new_generic_defs, (cop_td_indexes, cop_cd_indexes, cop_gd_indexes), conversion_table, icl_sizes, icl_decl_symbols, cs) + = foldSt (add_dcl_definition dcl_common) moved_dcl_defs ([], [], [], [], [], [], ([], [],[]), conversion_table, icl_sizes, icl_decl_symbols, cs) (new_cons_defs,new_member_defs,conversion_table,icl_sizes,icl_decl_symbols,symbol_table) = foldSt (add_all_dcl_cons_and_members_to_conversion_table dcl_common) dcl_cons_and_member_defs (new_cons_defs,new_member_defs,conversion_table,icl_sizes,icl_decl_symbols,cs.cs_symbol_table) @@ -1330,9 +1531,14 @@ combineDclAndIclModule _ modules icl_decl_symbols icl_definitions icl_sizes cs # n_dcl_classes = dcl_sizes.[cClassDefs] # n_dcl_types = dcl_sizes.[cTypeDefs] + # n_dcl_generics = dcl_sizes.[cGenericDefs] # copied_type_defs = mark_copied_definitions n_dcl_types cop_td_indexes # copied_class_defs = mark_copied_definitions n_dcl_classes cop_cd_indexes - = ( { copied_type_defs = copied_type_defs, copied_class_defs = copied_class_defs } + # copied_generic_defs = mark_copied_definitions n_dcl_generics cop_gd_indexes + = ( { copied_type_defs = copied_type_defs + , copied_class_defs = copied_class_defs + , copied_generic_defs = copied_generic_defs + } , Yes conversion_table , { modules & [main_dcl_module_n] = { dcl_mod & dcl_macro_conversions = Yes conversion_table.[cMacroDefs] }} , icl_decl_symbols @@ -1401,11 +1607,11 @@ where ) add_dcl_definition {com_type_defs,com_cons_defs} dcl=:(Declaration {decl_kind = STE_Type, decl_index}) - (new_type_defs, new_class_defs, new_cons_defs, new_selector_defs, new_member_defs, new_generic_defs, (cop_td_indexes, cop_cd_indexes), conversion_table, icl_sizes, icl_decl_symbols, cs) + (new_type_defs, new_class_defs, new_cons_defs, new_selector_defs, new_member_defs, new_generic_defs, (cop_td_indexes, cop_cd_indexes, cop_gd_indexes), conversion_table, icl_sizes, icl_decl_symbols, cs) # type_def = com_type_defs.[decl_index] (new_type_defs,new_cons_defs,new_selector_defs,conversion_table,icl_sizes,icl_decl_symbols,cs) = add_type_def type_def new_type_defs new_cons_defs new_selector_defs conversion_table icl_sizes icl_decl_symbols cs cop_td_indexes = [decl_index : cop_td_indexes] - = (new_type_defs, new_class_defs, new_cons_defs, new_selector_defs, new_member_defs, new_generic_defs, (cop_td_indexes, cop_cd_indexes), conversion_table, icl_sizes, icl_decl_symbols, cs) + = (new_type_defs, new_class_defs, new_cons_defs, new_selector_defs, new_member_defs, new_generic_defs, (cop_td_indexes, cop_cd_indexes, cop_gd_indexes), conversion_table, icl_sizes, icl_decl_symbols, cs) where add_type_def td=:{td_pos, td_rhs = AlgType conses} new_type_defs new_cons_defs new_selector_defs conversion_table icl_sizes icl_decl_symbols cs # (conses,(new_cons_defs,conversion_table,icl_sizes,icl_decl_symbols,cs)) = copy_and_redirect_cons_symbols com_cons_defs td_pos conses (new_cons_defs,conversion_table,icl_sizes,icl_decl_symbols,cs) @@ -1448,11 +1654,11 @@ where (new_type_defs, new_class_defs, new_cons_defs, new_selector_defs, new_member_defs, new_generic_defs, copied_defs, conversion_table, icl_sizes, icl_decl_symbols, cs) = (new_type_defs, new_class_defs, new_cons_defs, [ com_selector_defs.[decl_index] : new_selector_defs ], new_member_defs, new_generic_defs, copied_defs, conversion_table, icl_sizes, icl_decl_symbols, cs) add_dcl_definition {com_class_defs,com_member_defs} dcl=:(Declaration {decl_kind = STE_Class, decl_index, decl_pos}) - (new_type_defs, new_class_defs, new_cons_defs, new_selector_defs, new_member_defs, new_generic_defs, (cop_td_indexes, cop_cd_indexes), conversion_table, icl_sizes, icl_decl_symbols, cs) + (new_type_defs, new_class_defs, new_cons_defs, new_selector_defs, new_member_defs, new_generic_defs, (cop_td_indexes, cop_cd_indexes, cop_gd_indexes), conversion_table, icl_sizes, icl_decl_symbols, cs) # class_def = com_class_defs.[decl_index] cop_cd_indexes = [decl_index : cop_cd_indexes] (new_class_defs,new_member_defs,conversion_table,icl_sizes,icl_decl_symbols,cs) = add_class_def decl_pos class_def new_class_defs new_member_defs conversion_table icl_sizes icl_decl_symbols cs - = (new_type_defs, new_class_defs, new_cons_defs, new_selector_defs, new_member_defs, new_generic_defs, (cop_td_indexes, cop_cd_indexes), conversion_table, icl_sizes, icl_decl_symbols, cs) + = (new_type_defs, new_class_defs, new_cons_defs, new_selector_defs, new_member_defs, new_generic_defs, (cop_td_indexes, cop_cd_indexes, cop_gd_indexes), conversion_table, icl_sizes, icl_decl_symbols, cs) where add_class_def decl_pos cd=:{class_members} new_class_defs new_member_defs conversion_table icl_sizes icl_decl_symbols cs # (new_class_members,(new_member_defs,conversion_table,icl_sizes,icl_decl_symbols,cs)) = copy_and_redirect_member_symbols 0 com_member_defs decl_pos (new_member_defs,conversion_table,icl_sizes,icl_decl_symbols,cs) @@ -1469,6 +1675,8 @@ where add_dcl_definition {com_generic_defs} dcl=:(Declaration {decl_kind = STE_Generic, decl_index, decl_pos}) (new_type_defs, new_class_defs, new_cons_defs, new_selector_defs, new_member_defs, new_generic_defs, copied_defs, conversion_table, icl_sizes, icl_decl_symbols, cs) # generic_def = com_generic_defs.[decl_index] + # (cop_td_indexes, cop_cd_indexes, cop_gd_indexes) = copied_defs + # copied_defs = (cop_td_indexes, cop_cd_indexes, [decl_index:cop_gd_indexes]) = (new_type_defs, new_class_defs, new_cons_defs, new_selector_defs, new_member_defs, [generic_def:new_generic_defs], copied_defs, conversion_table, icl_sizes, icl_decl_symbols, cs) add_dcl_definition _ _ result = result @@ -1853,26 +2061,34 @@ checkDclModuleWithinComponent dcl_imported_module_numbers component_nr is_on_cyc is_on_cycle modules_in_component_set mod ste_index expl_imp_infos dcl_modules icl_functions macro_defs heaps { cs & cs_symbol_table = cs_symbol_table } - -renumber_icl_module :: ModuleKind IndexRange IndexRange Index Int {#Int} (Optional {#{#Int}}) IndexRange *{#FunDef} *CommonDefs [Declaration] *{#DclModule} *ErrorAdmin - -> (![IndexRange],![IndexRange],!Int,!Index,!IndexRange,!*{#FunDef},!*CommonDefs,![Declaration],!*{#DclModule},*ErrorAdmin); -renumber_icl_module mod_type icl_global_function_range icl_instance_range nr_of_functions main_dcl_module_n icl_sizes dcl_conversions def_macro_indices icl_functions icl_common local_defs dcl_modules error - + +renumber_icl_module :: ModuleKind IndexRange IndexRange IndexRange Index Int {#Int} (Optional {#{#Int}}) IndexRange *{#FunDef} *CommonDefs [Declaration] *{#DclModule} *ErrorAdmin + -> (![IndexRange],![IndexRange], ![IndexRange], !Int,!Index,!IndexRange,!*{#FunDef},!*CommonDefs,![Declaration],!*{#DclModule}, *ErrorAdmin); +renumber_icl_module mod_type icl_global_function_range icl_instance_range icl_generic_range nr_of_functions main_dcl_module_n icl_sizes dcl_conversions def_macro_indices icl_functions icl_common local_defs dcl_modules error # (optional_icl_to_dcl_index_table,optional_old_conversion_table,dcl_modules,icl_functions) = create_icl_to_dcl_index_table mod_type icl_sizes icl_global_function_range main_dcl_module_n dcl_conversions dcl_modules icl_functions # (dcl_mod, dcl_modules) = dcl_modules![main_dcl_module_n] # icl_functions = add_dummy_specialized_functions mod_type dcl_mod icl_functions # class_instances = icl_common.com_instance_defs - # (dcl_icl_conversions, class_instances,error) - = add_dcl_instances_to_conversion_table optional_old_conversion_table nr_of_functions dcl_mod class_instances error + # gencase_defs = icl_common.com_gencase_defs + # (dcl_icl_conversions, class_instances, gencase_defs, error) + = add_dcl_instances_to_conversion_table + optional_old_conversion_table nr_of_functions dcl_mod class_instances gencase_defs error | not error.ea_ok - = ([],[],0,0,def_macro_indices,icl_functions,{icl_common & com_instance_defs=class_instances},local_defs,dcl_modules,error) - + = ([],[],[], 0,0,def_macro_indices,icl_functions, + {icl_common & com_instance_defs=class_instances, com_gencase_defs=gencase_defs}, + local_defs,dcl_modules,error) # (n_functions,icl_functions) = usize icl_functions # optional_icl_to_dcl_index_table = recompute_icl_to_dcl_index_table_for_functions optional_icl_to_dcl_index_table dcl_icl_conversions n_functions # class_instances = renumber_member_indexes_of_class_instances optional_icl_to_dcl_index_table class_instances - # icl_common = {icl_common & com_instance_defs = class_instances} + # gencase_defs = renumber_members_of_gencases optional_icl_to_dcl_index_table gencase_defs + + # icl_common = + { icl_common + & com_instance_defs = class_instances + , com_gencase_defs = gencase_defs + } # (local_defs,dcl_modules,icl_common,icl_functions) = renumber_icl_definitions_as_dcl_definitions optional_icl_to_dcl_index_table icl_sizes icl_global_function_range main_dcl_module_n local_defs dcl_modules icl_common icl_functions @@ -1886,7 +2102,12 @@ renumber_icl_module mod_type icl_global_function_range icl_instance_range nr_of_ #! dcl_specials = dcl_mod.dcl_specials # n_dcl_specials = dcl_specials.ir_to-dcl_specials.ir_from - # local_functions_index_offset = n_dcl_instances + n_dcl_specials +//AA.. + # dcl_gencases = dcl_mod.dcl_gencases + # n_dcl_gencases = dcl_gencases.ir_to-dcl_gencases.ir_from +//..AA + + # local_functions_index_offset = n_dcl_instances + n_dcl_specials + n_dcl_gencases # dcl_mod = case dcl_mod of dcl_mod=:{dcl_macro_conversions=Yes conversion_table} @@ -1904,10 +2125,22 @@ renumber_icl_module mod_type icl_global_function_range icl_instance_range nr_of_ # first_macro_index = def_macro_indices.ir_from+local_functions_index_offset # end_macro_indexes = def_macro_indices.ir_to+local_functions_index_offset - # def_macro_indices={ir_from=first_macro_index,ir_to=end_macro_indexes} - # icl_instances_ranges = [dcl_instances,{ir_from=icl_instance_range.ir_from+n_dcl_specials+n_dcl_instances,ir_to=icl_instance_range.ir_to+n_dcl_specials}] + # def_macro_indices={ir_from=first_macro_index,ir_to=end_macro_indexes} + + # n_dcl_specials_and_gencases = n_dcl_specials + n_dcl_gencases + # not_exported_instance_range = + { ir_from=icl_instance_range.ir_from + n_dcl_instances + n_dcl_specials_and_gencases + , ir_to = icl_instance_range.ir_to + n_dcl_specials_and_gencases + } + # icl_instances_ranges = [dcl_instances, not_exported_instance_range] + + # not_exported_generic_range = + { ir_from =icl_generic_range.ir_from + n_dcl_specials_and_gencases + , ir_to = icl_generic_range.ir_to + n_dcl_specials + } + # icl_generic_ranges = [dcl_gencases, not_exported_generic_range] - = (icl_global_functions_ranges,icl_instances_ranges,n_exported_global_functions,local_functions_index_offset,def_macro_indices,icl_functions,icl_common,local_defs,dcl_modules,error) + = (icl_global_functions_ranges, icl_instances_ranges, icl_generic_ranges, n_exported_global_functions,local_functions_index_offset,def_macro_indices,icl_functions,icl_common,local_defs,dcl_modules, error) where add_dummy_specialized_functions MK_Main dcl_mod icl_functions = icl_functions @@ -1918,26 +2151,47 @@ renumber_icl_module mod_type icl_global_function_range icl_instance_range nr_of_ # dummy_function = {fun_symb={id_name="",id_info=nilPtr},fun_arity= -1,fun_priority=NoPrio,fun_body=NoBody,fun_type=No,fun_pos=NoPos,fun_kind=FK_Unknown,fun_lifted=0,fun_info=EmptyFunInfo} = arrayPlusList icl_functions [dummy_function \\ i<-[0..n_specials-1]] - add_dcl_instances_to_conversion_table :: (Optional {#{#Int}}) !Index !DclModule !*{# ClassInstance} *ErrorAdmin -> (!*Optional *{#Index},!*{# ClassInstance},*ErrorAdmin) - add_dcl_instances_to_conversion_table optional_old_conversion_table first_free_index dcl_mod=:{dcl_specials,dcl_functions,dcl_common,dcl_macro_conversions} icl_instances error + add_dcl_instances_to_conversion_table :: (Optional {#{#Int}}) !Index !DclModule !*{# ClassInstance} !*{# GenericCaseDef} *ErrorAdmin + -> (!*Optional *{#Index},!*{# ClassInstance}, !*{# GenericCaseDef},*ErrorAdmin) + add_dcl_instances_to_conversion_table optional_old_conversion_table first_free_index dcl_mod=:{dcl_specials,dcl_functions,dcl_common,dcl_macro_conversions} icl_instances icl_gencases error = case dcl_macro_conversions of Yes _ - # (new_conversion_table, icl_instances,error) + # (new_conversion_table, icl_instances, icl_gencases, error) = build_conversion_table_for_instances_of_dcl_mod dcl_specials first_free_index optional_old_conversion_table - dcl_functions dcl_common.com_instance_defs icl_instances error - -> (Yes new_conversion_table,icl_instances,error) + dcl_functions dcl_common.com_instance_defs icl_instances dcl_common.com_gencase_defs icl_gencases error + -> (Yes new_conversion_table,icl_instances, icl_gencases, error) No - -> (No,icl_instances,error) + -> (No, icl_instances, icl_gencases, error) where - build_conversion_table_for_instances_of_dcl_mod {ir_from,ir_to} first_free_index optional_old_conversion_table dcl_functions dcl_instances icl_instances error + build_conversion_table_for_instances_of_dcl_mod dcl_specials=:{ir_from,ir_to} first_free_index optional_old_conversion_table dcl_functions dcl_instances icl_instances dcl_gencases icl_gencases error #! nr_of_dcl_functions = size dcl_functions # (Yes old_conversion_table) = optional_old_conversion_table - # dcl_instances_table = old_conversion_table.[cInstanceDefs] - dcl_function_table = old_conversion_table.[cFunctionDefs] - new_table = { createArray nr_of_dcl_functions NoIndex & [i] = icl_index \\ icl_index <-: dcl_function_table & i <- [0..] } - index_diff = first_free_index - ir_from - new_table = { new_table & [i] = i + index_diff \\ i <- [ir_from .. ir_to - 1] } - = build_conversion_table_for_instances 0 dcl_instances dcl_instances_table icl_instances new_table error + #! dcl_instances_table = old_conversion_table.[cInstanceDefs] + #! dcl_gencase_table = old_conversion_table.[cGenericCaseDefs] + #! dcl_function_table = old_conversion_table.[cFunctionDefs] + #! new_table = { createArray nr_of_dcl_functions NoIndex & [i] = icl_index \\ icl_index <-: dcl_function_table & i <- [0..] } + #! index_diff = first_free_index - ir_from + #! new_table = { new_table & [i] = i + index_diff \\ i <- [ir_from .. ir_to - 1] } + #! (new_table, icl_instances, error) + = build_conversion_table_for_instances 0 dcl_instances dcl_instances_table icl_instances new_table error + #! (new_table, icl_gencases, error) + = build_conversion_table_for_generic_cases 0 dcl_gencases dcl_gencase_table icl_gencases new_table error + = (new_table, icl_instances, icl_gencases, error) + + build_conversion_table_for_generic_cases dcl_index dcl_gencases gencase_table icl_gencases new_table error + | dcl_index < size gencase_table + #! (new_table, icl_gencases, error) + = build_conversion_table_for_generic_case dcl_index dcl_gencases gencase_table icl_gencases new_table error + = build_conversion_table_for_generic_cases (inc dcl_index) dcl_gencases gencase_table icl_gencases new_table error + = (new_table, icl_gencases, error) + build_conversion_table_for_generic_case dcl_index dcl_gencases gencase_table icl_gencases new_table error + #! icl_index = gencase_table.[dcl_index] + #! (icl_gencase, icl_gencases) = icl_gencases ! [icl_index] + #! dcl_gencase = dcl_gencases.[dcl_index] + # (GCB_FunIndex icl_fun) = icl_gencase.gc_body + # (GCB_FunIndex dcl_fun) = dcl_gencase.gc_body + #! new_table = { new_table & [dcl_fun] = icl_fun } + = (new_table, icl_gencases, error) build_conversion_table_for_instances dcl_class_inst_index dcl_instances class_instances_table icl_instances new_table error | dcl_class_inst_index < size class_instances_table @@ -1971,20 +2225,30 @@ checkModule m icl_global_function_range fun_defs n_functions_and_macros_in_dcl_m 0 # (predef_mod,predef_symbols) = buildPredefinedModule predef_symbols -> (Yes predef_mod,predef_symbols) _ -> (No,predef_symbols) - # (mod_name,mod_imported_objects,mod_imports,mod_type,icl_global_function_range,nr_of_functions,first_inst_index,local_defs,icl_functions,macro_defs,init_dcl_modules,main_dcl_module_n,cdefs,sizes,cs) + # (mod_name,mod_imported_objects,mod_imports,mod_type,icl_global_function_range,nr_of_functions,first_inst_index,first_gen_inst_index, local_defs,icl_functions,macro_defs,init_dcl_modules,main_dcl_module_n,cdefs,sizes,cs) = check_module1 m icl_global_function_range fun_defs optional_dcl_mod optional_pre_def_mod scanned_modules dcl_modules cached_dcl_macros dcl_module_n_in_cache predef_symbols symbol_table err_file - # icl_instance_range = {ir_from = first_inst_index, ir_to = nr_of_functions} - = check_module2 mod_name m.mod_modification_time mod_imported_objects mod_imports mod_type icl_global_function_range icl_instance_range nr_of_functions nr_of_cached_modules optional_pre_def_mod local_defs icl_functions macro_defs init_dcl_modules cdefs sizes heaps cs + # icl_instance_range = {ir_from = first_inst_index, ir_to = first_gen_inst_index/*AA nr_of_functions*/} + # icl_generic_range = {ir_from = first_gen_inst_index, ir_to = nr_of_functions} //AA + = check_module2 mod_name m.mod_modification_time mod_imported_objects mod_imports mod_type icl_global_function_range icl_instance_range icl_generic_range nr_of_functions nr_of_cached_modules optional_pre_def_mod local_defs icl_functions macro_defs init_dcl_modules cdefs sizes heaps cs check_module1 {mod_type,mod_name,mod_imports,mod_imported_objects,mod_defs = cdefs} icl_global_function_range fun_defs optional_dcl_mod optional_pre_def_mod scanned_modules dcl_modules cached_dcl_macros dcl_module_n_in_cache predef_symbols symbol_table err_file # error = {ea_file = err_file, ea_loc = [], ea_ok = True } first_inst_index = length fun_defs - (inst_fun_defs, def_instances) = convert_class_instances cdefs.def_instances first_inst_index + (inst_fun_defs, def_instances) = convert_class_instances cdefs.def_instances first_inst_index - icl_functions = { next_fun \\ next_fun <- fun_defs ++ inst_fun_defs } +// AA.. + first_gen_inst_index = first_inst_index + length inst_fun_defs + (gen_inst_fun_defs, def_generic_cases) = convert_generic_instances cdefs.def_generic_cases first_gen_inst_index +// ..AA + + icl_functions = { next_fun \\ next_fun <- fun_defs ++ inst_fun_defs ++ gen_inst_fun_defs} - cdefs = { cdefs & def_instances = def_instances } + cdefs = + { cdefs + & def_instances = def_instances + , def_generic_cases = def_generic_cases + } #! nr_of_functions = size icl_functions # sizes_and_local_defs = collectCommonfinitions cdefs @@ -2007,7 +2271,7 @@ check_module1 {mod_type,mod_name,mod_imports,mod_imported_objects,mod_defs = cde dcl_modules.[i] init_new_dcl_modules.[i-size dcl_modules] \\ i<-[0..size dcl_modules+size init_new_dcl_modules-1]} - = (mod_name,mod_imported_objects,mod_imports,mod_type,icl_global_function_range,nr_of_functions,first_inst_index,local_defs,icl_functions,macro_defs,init_dcl_modules,main_dcl_module_n,cdefs,sizes,cs) + = (mod_name,mod_imported_objects,mod_imports,mod_type,icl_global_function_range,nr_of_functions,first_inst_index,first_gen_inst_index,local_defs,icl_functions,macro_defs,init_dcl_modules,main_dcl_module_n,cdefs,sizes,cs) where add_dcl_module_predef_module_and_modules_to_symbol_table (Yes dcl_mod) optional_predef_mod modules mod_index cs @@ -2053,7 +2317,8 @@ check_module1 {mod_type,mod_name,mod_imports,mod_imported_objects,mod_defs = cde add_module_to_symbol_table mod=:{mod_defs} mod_index cs=:{cs_symbol_table, cs_error} # def_instances = convert_class_instances mod_defs.def_instances - mod_defs = { mod_defs & def_instances = def_instances } + # def_generic_cases = convert_generic_instances mod_defs.def_generic_cases + mod_defs = { mod_defs & def_instances = def_instances, def_generic_cases = def_generic_cases } sizes_and_defs = collectFunctionTypes mod_defs.def_funtypes (collectCommonfinitions mod_defs) dcl_macro_defs={macro_def \\ macro_def<-mod_defs.def_macros} @@ -2068,6 +2333,11 @@ check_module1 {mod_type,mod_name,mod_imports,mod_imported_objects,mod_defs = cde = [ParsedInstanceToClassInstance pi {} : convert_class_instances pins] convert_class_instances [] = [] + + convert_generic_instances :: ![GenericCaseDef] -> [GenericCaseDef] + convert_generic_instances gcs + // TODO: check what to do here + = gcs //[{ gc & gc_body = gc.gc_body } \\ gc <- gcs] convert_class_instances :: .[ParsedInstance FunDef] Int -> (!.[FunDef],!.[ClassInstance]); convert_class_instances [pi=:{pi_members} : pins] next_fun_index @@ -2078,6 +2348,31 @@ check_module1 {mod_type,mod_name,mod_imports,mod_imported_objects,mod_defs = cde convert_class_instances [] next_fun_index = ([], []) + convert_generic_instances :: !.[GenericCaseDef] !Int -> (!.[FunDef], !.[GenericCaseDef]) + convert_generic_instances [gc=:{gc_name, gc_body=GCB_FunDef fun_def} : gcs] next_fun_index + # (fun_defs, gcs) = convert_generic_instances gcs (inc next_fun_index) + # gc = { gc & gc_body = GCB_FunIndex next_fun_index } + = ([fun_def : fun_defs], [gc:gcs]) + //---> ("convert generic case: user defined function", gc.gc_name, gc.gc_type_cons, next_fun_index) + convert_generic_instances [gc=:{gc_name,gc_pos, gc_type_cons, gc_body=GCB_None} : gcs] next_fun_index + # (fun_defs, gcs) = convert_generic_instances gcs (inc next_fun_index) + # fun_def = + { fun_symb = genericIdentToFunIdent gc_name gc_type_cons + , fun_arity = 0 + , fun_priority = NoPrio + , fun_body = GeneratedBody + , fun_type = No + , fun_pos = gc_pos + , fun_kind = FK_Unknown + , fun_lifted = 0 + , fun_info = EmptyFunInfo + } + # gc = { gc & gc_body = GCB_FunIndex next_fun_index } + = ([fun_def:fun_defs], [gc:gcs]) + //---> ("convert generic case: function to derive ", gc.gc_name, gc.gc_type_cons, next_fun_index) + convert_generic_instances [] next_fun_index + = ([], []) + determine_indexes_of_members [{fun_symb,fun_arity}:members] next_fun_index #! (member_symbols, last_fun_index) = determine_indexes_of_members members (inc next_fun_index) = ([{ds_ident = fun_symb, ds_index = next_fun_index, ds_arity = fun_arity} : member_symbols], last_fun_index) @@ -2105,35 +2400,32 @@ check_module1 {mod_type,mod_name,mod_imports,mod_imported_objects,mod_defs = cde fill_macro_def_array i [dcl_macro_defs:macro_defs] a = fill_macro_def_array (i+1) macro_defs {a & [i]=dcl_macro_defs} -check_module2 :: Ident {#Char} [.ImportedObject] .[Import ImportDeclaration] .ModuleKind !.IndexRange !.IndexRange !Int !Int +check_module2 :: Ident {#Char} [.ImportedObject] .[Import ImportDeclaration] .ModuleKind !.IndexRange !.IndexRange !.IndexRange !Int !Int (Optional (Module a)) [Declaration] *{#FunDef} *{#*{#FunDef}} *{#DclModule} (CollectedDefinitions ClassInstance IndexRange) *{#.Int} *Heaps *CheckState -> (!Bool,.IclModule,!.{#DclModule},.{!Group},!*{#*{#FunDef}},!Int,!.Heaps,!.{#PredefinedSymbol},!.Heap SymbolTableEntry,!.File,[String]); -check_module2 mod_name mod_modification_time mod_imported_objects mod_imports mod_type icl_global_function_range icl_instance_range nr_of_functions nr_of_cached_modules optional_pre_def_mod local_defs icl_functions macro_defs init_dcl_modules cdefs sizes heaps cs +check_module2 mod_name mod_modification_time mod_imported_objects mod_imports mod_type icl_global_function_range icl_instance_range icl_generic_range nr_of_functions nr_of_cached_modules optional_pre_def_mod local_defs icl_functions macro_defs init_dcl_modules cdefs sizes heaps cs # (main_dcl_module_n,cs)=cs!cs_x.x_main_dcl_module_n - + (copied_dcl_defs, dcl_conversions, dcl_modules, local_defs, cdefs, icl_sizes, cs) = combineDclAndIclModule mod_type init_dcl_modules local_defs cdefs sizes cs - | not cs.cs_error.ea_ok = (False, abort "evaluated error 1 (check.icl)", {}, {}, {}, cs.cs_x.x_main_dcl_module_n,heaps, cs.cs_predef_symbols, cs.cs_symbol_table, cs.cs_error.ea_file, []) # icl_common = createCommonDefinitions cdefs - + (dcl_modules, icl_functions, macro_defs, heaps, cs) = check_predefined_module optional_pre_def_mod dcl_modules icl_functions macro_defs heaps cs (nr_of_icl_component, expl_imp_indices, directly_imported_dcl_modules, expl_imp_info, dcl_modules, icl_functions, macro_defs, heaps, cs) = checkDclModules mod_imports dcl_modules icl_functions macro_defs heaps cs - | not cs.cs_error.ea_ok = (False, abort "evaluated error 2 (check.icl)", {}, {}, {}, cs.cs_x.x_main_dcl_module_n,heaps, cs.cs_predef_symbols, cs.cs_symbol_table, cs.cs_error.ea_file, []) # def_macro_indices=cdefs.def_macro_indices - # (icl_global_functions_ranges,icl_instances_ranges,n_exported_global_functions,local_functions_index_offset,def_macro_indices,icl_functions,icl_common,local_defs,dcl_modules,error) - = renumber_icl_module mod_type icl_global_function_range icl_instance_range nr_of_functions main_dcl_module_n icl_sizes dcl_conversions def_macro_indices icl_functions icl_common local_defs dcl_modules cs.cs_error - + # (icl_global_functions_ranges,icl_instances_ranges, icl_generic_ranges, n_exported_global_functions,local_functions_index_offset,def_macro_indices,icl_functions,icl_common,local_defs,dcl_modules, error) + = renumber_icl_module mod_type icl_global_function_range icl_instance_range icl_generic_range nr_of_functions main_dcl_module_n icl_sizes dcl_conversions def_macro_indices icl_functions icl_common local_defs dcl_modules cs.cs_error | not error.ea_ok = (False, abort "evaluated error 3 (check.icl)", {}, {}, {}, cs.cs_x.x_main_dcl_module_n,heaps, cs.cs_predef_symbols, cs.cs_symbol_table, error.ea_file, []) @@ -2185,12 +2477,11 @@ check_module2 mod_name mod_modification_time mod_imported_objects mod_imports mo icl_imported = { el \\ el<-dcls_import_list } - (_,icl_common, dcl_modules, hp_type_heaps, hp_var_heap, cs) - = checkCommonDefinitions (Yes (copied_dcl_defs, nr_of_cached_modules)) main_dcl_module_n icl_common dcl_modules heaps.hp_type_heaps heaps.hp_var_heap cs - + (_,icl_common, dcl_modules, heaps=:{hp_var_heap, hp_type_heaps}, cs) + = checkCommonDefinitions (Yes (copied_dcl_defs, nr_of_cached_modules)) main_dcl_module_n icl_common dcl_modules heaps cs + (instance_types, icl_common, dcl_modules, hp_var_heap, hp_type_heaps, cs) = checkInstances main_dcl_module_n icl_common dcl_modules hp_var_heap hp_type_heaps cs - heaps = { heaps & hp_type_heaps = hp_type_heaps, hp_var_heap = hp_var_heap } e_info = { ef_type_defs = icl_common.com_type_defs, ef_selector_defs = icl_common.com_selector_defs, ef_class_defs = icl_common.com_class_defs, @@ -2203,11 +2494,15 @@ check_module2 mod_name mod_modification_time mod_imported_objects mod_imports mo cs = check_start_rule mod_type mod_name icl_global_functions_ranges cs cs = check_needed_modules_are_imported mod_name ".icl" cs + (icl_functions, e_info, heaps, cs) + = checkGlobalFunctionsInRanges icl_generic_ranges main_dcl_module_n local_functions_index_offset icl_functions e_info heaps cs + (icl_functions, e_info, heaps, {cs_symbol_table, cs_predef_symbols, cs_error,cs_x }) = checkInstanceBodies icl_instances_ranges local_functions_index_offset icl_functions e_info heaps cs - + (icl_functions, hp_type_heaps, cs_error) = foldSt checkSpecifiedInstanceType instance_types (icl_functions, heaps.hp_type_heaps, cs_error) + heaps = { heaps & hp_type_heaps = hp_type_heaps } @@ -2225,7 +2520,7 @@ check_module2 mod_name mod_modification_time mod_imported_objects mod_imports mo (icl_specials,dcl_modules, icl_functions, var_heap, th_vars, expr_heap) = collect_specialized_functions_in_dcl_module mod_type nr_of_functions main_dcl_module_n dcl_modules icl_functions hp_var_heap th_vars hp_expression_heap - + icl_functions = copy_instance_types instance_types icl_functions (dcl_modules, class_instances, icl_functions, cs_predef_symbols) @@ -2236,6 +2531,7 @@ check_module2 mod_name mod_modification_time mod_imported_objects mod_imports mo com_generic_defs = e_info.ef_generic_defs, com_instance_defs = class_instances } icl_mod = { icl_name = mod_name, icl_functions = icl_functions, icl_common = icl_common, icl_global_functions = icl_global_functions_ranges, icl_instances = icl_instances_ranges, icl_specials = icl_specials, + icl_gencases = icl_generic_ranges, icl_imported_objects = mod_imported_objects, icl_used_module_numbers = imported_module_numbers, icl_copied_from_dcl = copied_dcl_defs, icl_import = icl_imported, icl_modification_time = mod_modification_time} @@ -2247,7 +2543,7 @@ check_module2 mod_name mod_modification_time mod_imported_objects mod_imports mo # (predef_symbols_for_transform, cs_predef_symbols) = get_predef_symbols_for_transform cs_predef_symbols (groups, icl_functions, macro_defs, dcl_modules, var_heap, expr_heap, cs_symbol_table, cs_error) - = partitionateAndLiftFunctions (icl_global_functions_ranges++icl_instances_ranges) main_dcl_module_n predef_symbols_for_transform icl_mod.icl_functions macro_defs + = partitionateAndLiftFunctions (icl_global_functions_ranges++icl_instances_ranges++icl_generic_ranges) main_dcl_module_n predef_symbols_for_transform icl_mod.icl_functions macro_defs dcl_modules heaps.hp_var_heap heaps.hp_expression_heap cs_symbol_table cs_error # heaps = {heaps & hp_var_heap=var_heap,hp_expression_heap=expr_heap} @@ -2259,6 +2555,7 @@ check_module2 mod_name mod_modification_time mod_imported_objects mod_imports mo icl_mod = { icl_name = mod_name, icl_functions = icl_functions, icl_common = icl_common, icl_global_functions = icl_global_functions_ranges, icl_instances = icl_instances_ranges, icl_specials = {ir_from = nr_of_functions, ir_to = nr_of_functions}, + icl_gencases = icl_generic_ranges, icl_imported_objects = mod_imported_objects, icl_used_module_numbers = imported_module_numbers, icl_copied_from_dcl = copied_dcl_defs, icl_import = icl_imported ,icl_modification_time = mod_modification_time} = (False, icl_mod, dcl_modules, {}, {}, cs_x.x_main_dcl_module_n,heaps, cs_predef_symbols, cs_symbol_table, cs_error.ea_file, directly_imported_dcl_modules) @@ -2311,6 +2608,7 @@ check_module2 mod_name mod_modification_time mod_imported_objects mod_imports mo # (icl_functions, (var_heap, type_var_heap, expr_heap)) = collect_specialized_functions ir_from ir_to dcl_functions (icl_functions, (var_heap, type_var_heap, expr_heap)) = (dcl_specials,modules, icl_functions, var_heap, type_var_heap, expr_heap) + where collect_specialized_functions spec_index last_index dcl_fun_types (icl_functions, heaps) | spec_index < last_index @@ -2503,6 +2801,7 @@ initialDclModule ({mod_name, mod_modification_time, mod_defs=mod_defs=:{def_funt , dcl_macros = def_macro_indices , dcl_instances = { ir_from = 0, ir_to = 0} , dcl_specials = { ir_from = 0, ir_to = 0 } + , dcl_gencases = { ir_from = 0, ir_to = 0 } , dcl_common = dcl_common , dcl_sizes = sizes , dcl_dictionary_info = { n_dictionary_types=0,n_dictionary_constructors=0,n_dictionary_selectors=0 } @@ -2867,30 +3166,34 @@ where checkInstancesOfDclModule :: !.Int !(!.Int,.Int,.[FunType]) !(!*{#DclModule},!*Heaps,!*CheckState) -> (!.{#DclModule},!.Heaps,!.CheckState); checkInstancesOfDclModule mod_index (nr_of_dcl_functions_and_instances, nr_of_dcl_funs_insts_and_specs, rev_special_defs) (dcl_modules, heaps=:{hp_type_heaps, hp_var_heap}, cs=:{cs_error}) - #! main_dcl_module_n = cs.cs_x.x_main_dcl_module_n # (dcl_mod=:{dcl_functions, dcl_common}, dcl_modules) = dcl_modules![mod_index] nr_of_dcl_functions = size dcl_functions (memb_inst_defs, nr_of_dcl_functions_and_instances2, rev_spec_class_inst, - com_instance_defs, com_class_defs, com_member_defs, com_generic_defs, dcl_modules, hp_type_heaps, hp_var_heap, cs) + com_instance_defs, com_class_defs, com_member_defs, dcl_modules, hp_type_heaps, hp_var_heap, cs) = determineTypesOfInstances nr_of_dcl_functions mod_index {d \\ d<-:dcl_common.com_instance_defs} {d \\ d<-:dcl_common.com_class_defs} {d \\ d<-:dcl_common.com_member_defs} - {d \\ d<-:dcl_common.com_generic_defs} dcl_modules hp_type_heaps hp_var_heap { cs & cs_error = cs_error } heaps = { heaps & hp_type_heaps = hp_type_heaps, hp_var_heap = hp_var_heap } (nr_of_dcl_funs_insts_and_specs, new_class_instances, rev_special_defs, all_spec_types, heaps, cs_predef_symbols,cs_error) = checkSpecialsOfInstances mod_index nr_of_dcl_functions rev_spec_class_inst nr_of_dcl_funs_insts_and_specs [] - rev_special_defs { mem \\ mem <- memb_inst_defs } { [] \\ mem <- memb_inst_defs } heaps cs.cs_predef_symbols cs.cs_error - dcl_functions + rev_special_defs { mem \\ mem <- memb_inst_defs } { [] \\ mem <- memb_inst_defs } heaps cs.cs_predef_symbols cs.cs_error + + #! (nr_of_dcl_funs_insts_specs_and_gencases, gen_funs, com_gencase_defs, heaps) + = create_gencase_funtypes nr_of_dcl_funs_insts_and_specs {d \\ d<-:dcl_common.com_gencase_defs} heaps + + # dcl_functions = arrayPlusList dcl_functions ( [ { mem_inst & ft_specials = if (isEmpty spec_types) SP_None (SP_ContextTypes spec_types) } \\ mem_inst <- memb_inst_defs & spec_types <-: all_spec_types ] ++ reverse rev_special_defs + ++ gen_funs ) - cs = { cs & cs_predef_symbols=cs_predef_symbols,cs_error = cs_error} + + # cs = { cs & cs_predef_symbols=cs_predef_symbols,cs_error = cs_error} #! mod_index_of_std_array = cs.cs_predef_symbols.[PD_StdArray].pds_def # (com_member_defs, com_instance_defs, dcl_functions, cs) = case mod_index_of_std_array==mod_index of @@ -2899,16 +3202,65 @@ checkInstancesOfDclModule mod_index (nr_of_dcl_functions_and_instances, nr_of_dc True -> adjust_instance_types_of_array_functions_in_std_array_dcl mod_index com_member_defs com_instance_defs dcl_functions cs - dcl_mod = { dcl_mod & dcl_functions = dcl_functions, + #! dcl_mod = { dcl_mod & dcl_functions = dcl_functions, dcl_specials = { ir_from = nr_of_dcl_functions_and_instances, ir_to = nr_of_dcl_funs_insts_and_specs }, + dcl_gencases = { ir_from = nr_of_dcl_funs_insts_and_specs + , ir_to = nr_of_dcl_funs_insts_specs_and_gencases}, dcl_common = - { dcl_common & com_instance_defs = com_instance_defs, - com_class_defs = com_class_defs, com_member_defs = com_member_defs, - com_generic_defs = com_generic_defs }} - dcl_modules = { dcl_modules & [mod_index] = dcl_mod } + { dcl_common + & com_instance_defs = com_instance_defs + , com_class_defs = com_class_defs + , com_member_defs = com_member_defs + , com_gencase_defs = com_gencase_defs + }} + + // TODO: update the instance range or create another, generic function range + + dcl_modules = { dcl_modules & [mod_index] = dcl_mod } = (dcl_modules, heaps, cs) where + create_gencase_funtypes :: !Index !*{#GenericCaseDef} !*Heaps + -> (!Index, ![FunType], !*{#GenericCaseDef}, !*Heaps) + create_gencase_funtypes fun_index gencase_defs heaps + #! (fun_index, new_funs, gencase_defs, hp_var_heap) + = create_funs 0 fun_index gencase_defs heaps.hp_var_heap + = (fun_index, new_funs, gencase_defs, {heaps & hp_var_heap = hp_var_heap}) + where + + create_funs gc_index fun_index gencase_defs hp_var_heap + | gc_index == size gencase_defs + = (fun_index, [], gencase_defs, hp_var_heap) + #! (fun, gencase_defs,hp_var_heap) + = create_fun gc_index fun_index gencase_defs hp_var_heap + #! (fun_index, funs, gencase_defs,hp_var_heap) + = create_funs (inc gc_index) (inc fun_index) gencase_defs hp_var_heap + = (fun_index, [fun:funs], gencase_defs, hp_var_heap) + create_fun gc_index fun_index gencase_defs hp_var_heap + # (gencase_def=:{gc_name, gc_pos, gc_type_cons}, gencase_defs) = gencase_defs ! [gc_index] + # gencase_def = { gencase_def & gc_body = GCB_FunIndex fun_index } + # gencase_defs = {gencase_defs & [gc_index] = gencase_def} + + #! fun_ident = genericIdentToFunIdent gc_name gc_type_cons + #! dummy_ds = + { ds_ident = fun_ident + , ds_arity = 0 + , ds_index = NoIndex + } + #! (var_info_ptr, hp_var_heap) = newPtr VI_Empty hp_var_heap + #! fun = + { ft_symb = fun_ident + , ft_arity = 0 + , ft_priority = NoPrio + , ft_type = {st_vars=[],st_attr_vars=[],st_arity=0,st_args=[],st_result={at_type=TE,at_attribute=TA_Multi},st_attr_env=[],st_context=[],st_args_strictness=NotStrict} + , ft_pos = gc_pos + , ft_specials = SP_None + , ft_type_ptr = var_info_ptr + } + + = (fun, gencase_defs, hp_var_heap) + //---> ("create_gencase_funtypes", gc_name, gc_type_cons, gc_index, fun_index) + adjust_instance_types_of_array_functions_in_std_array_dcl array_mod_index class_members class_instances fun_types cs=:{cs_predef_symbols} #! nr_of_instances = size class_instances # ({pds_def}, cs_predef_symbols) = cs_predef_symbols![PD_ArrayClass] @@ -2939,7 +3291,7 @@ checkDclModule :: !NumberSet ![Int] !(IntKeyHashtable SolvedImports) !Int !Bool !(Module (CollectedDefinitions ClassInstance IndexRange)) !Index !*ExplImpInfos !*{#DclModule} !*{#FunDef} !*{#*{#FunDef}} !*Heaps !*CheckState -> (!(!Int,!Index,![FunType]), !(!*ExplImpInfos, !*{#DclModule}, !*{#FunDef},!*{#*{#FunDef}},!*Heaps, !*CheckState)) checkDclModule dcl_imported_module_numbers super_components imports_ikh component_nr is_on_cycle modules_in_component_set - {mod_name,mod_imports,mod_defs} mod_index expl_imp_info modules icl_functions macro_defs heaps=:{hp_var_heap, hp_type_heaps,hp_expression_heap} cs + {mod_name,mod_imports,mod_defs} mod_index expl_imp_info modules icl_functions macro_defs heaps cs // | False--->("checkDclModule", mod_name, mod_index) //, modules.[mod_index].dcl_declared.dcls_local) // = undef # (dcl_mod, modules) = modules![mod_index] @@ -2953,10 +3305,9 @@ checkDclModule dcl_imported_module_numbers super_components imports_ikh componen cs = { cs & cs_x.x_needed_modules = 0 } nr_of_dcl_functions = size dcl_mod.dcl_functions #! main_dcl_module_n = cs.cs_x.x_main_dcl_module_n - # (dictionary_info,dcl_common, modules, hp_type_heaps, hp_var_heap, cs) - = checkCommonDefinitions No mod_index dcl_common modules hp_type_heaps hp_var_heap cs + # (dictionary_info,dcl_common, modules, heaps, cs) + = checkCommonDefinitions No mod_index dcl_common modules heaps cs # dcl_mod = {dcl_mod & dcl_dictionary_info=dictionary_info} - heaps = { heaps & hp_type_heaps = hp_type_heaps, hp_var_heap = hp_var_heap, hp_expression_heap=hp_expression_heap} | not cs.cs_error.ea_ok # cs_symbol_table = removeDeclarationsFromSymbolTable dcl_defined cModuleScope cs.cs_symbol_table # cs_symbol_table = foldlArraySt removeImportedSymbolsFromSymbolTable dcls_import cs_symbol_table @@ -2971,7 +3322,6 @@ checkDclModule dcl_imported_module_numbers super_components imports_ikh componen dcl_common.com_type_defs dcl_common.com_class_defs modules heaps cs dcl_functions = { function \\ function <- reverse rev_function_list } - com_member_defs = dcl_common.com_member_defs e_info = { ef_type_defs = com_type_defs, ef_selector_defs = dcl_common.com_selector_defs, ef_class_defs = com_class_defs, ef_cons_defs = dcl_common.com_cons_defs, ef_member_defs = com_member_defs, ef_generic_defs = dcl_common.com_generic_defs, @@ -3011,6 +3361,7 @@ checkDclModule dcl_imported_module_numbers super_components imports_ikh componen dcl_common = dcl_common, dcl_functions = dcl_functions, dcl_instances = { ir_from = nr_of_dcl_functions, ir_to = nr_of_dcl_functions_and_instances }, dcl_specials = { ir_from = cUndef, ir_to = cUndef }, + dcl_gencases = { ir_from = cUndef, ir_to = cUndef }, dcl_imported_module_numbers = dcl_imported_module_numbers} = ((nr_of_dcl_functions_and_instances, nr_of_dcl_funs_insts_and_specs, rev_special_defs), (expl_imp_info, { modules & [ mod_index ] = dcl_mod }, icl_functions, macro_defs, heaps, { cs & cs_symbol_table = cs_symbol_table })) @@ -3061,13 +3412,13 @@ where <=< adjustPredefSymbol PD_TypeID mod_index STE_Type <=< adjustPredefSymbol PD_ModuleID mod_index STE_Constructor) # (pre_mod, cs_predef_symbols) = cs_predef_symbols![PD_StdGeneric] - # type_iso_ident = predefined_idents.[PD_TypeISO] + # type_bimap = predefined_idents.[PD_TypeBimap] | pre_mod.pds_def == mod_index = (class_members, class_instances, fun_types, { cs & cs_predef_symbols = cs_predef_symbols} - <=< adjustPredefSymbol PD_TypeISO mod_index STE_Type - <=< adjustPredefSymbol PD_ConsISO mod_index STE_Constructor - <=< adjustPredefSymbol PD_iso_from mod_index (STE_Field type_iso_ident) - <=< adjustPredefSymbol PD_iso_to mod_index (STE_Field type_iso_ident) + <=< adjustPredefSymbol PD_TypeBimap mod_index STE_Type + <=< adjustPredefSymbol PD_ConsBimap mod_index STE_Constructor + <=< adjustPredefSymbol PD_map_to mod_index (STE_Field type_bimap) + <=< adjustPredefSymbol PD_map_from mod_index (STE_Field type_bimap) <=< adjustPredefSymbol PD_TypeUNIT mod_index STE_Type <=< adjustPredefSymbol PD_ConsUNIT mod_index STE_Constructor <=< adjustPredefSymbol PD_TypePAIR mod_index STE_Type @@ -3075,20 +3426,8 @@ where <=< adjustPredefSymbol PD_TypeEITHER mod_index STE_Type <=< adjustPredefSymbol PD_ConsLEFT mod_index STE_Constructor <=< adjustPredefSymbol PD_ConsRIGHT mod_index STE_Constructor - <=< adjustPredefSymbol PD_TypeARROW mod_index STE_Type - <=< adjustPredefSymbol PD_ConsARROW mod_index STE_Constructor - <=< adjustPredefSymbol PD_isomap_ARROW_ mod_index STE_DclFunction - <=< adjustPredefSymbol PD_isomap_ID mod_index STE_DclFunction - <=< adjustPredefSymbol PD_TypeConsDefInfo mod_index STE_Type - <=< adjustPredefSymbol PD_ConsConsDefInfo mod_index STE_Constructor - <=< adjustPredefSymbol PD_TypeTypeDefInfo mod_index STE_Type - <=< adjustPredefSymbol PD_ConsTypeDefInfo mod_index STE_Constructor - <=< adjustPredefSymbol PD_TypeCONS mod_index STE_Type - <=< adjustPredefSymbol PD_ConsCONS mod_index STE_Constructor - <=< adjustPredefSymbol PD_cons_info mod_index STE_DclFunction - <=< adjustPredefSymbol PD_TypeType mod_index STE_Type - <=< adjustPredefSymbol PD_ConsTypeApp mod_index STE_Constructor - <=< adjustPredefSymbol PD_ConsTypeVar mod_index STE_Constructor + <=< adjustPredefSymbol PD_GenericBimap mod_index STE_Generic + <=< adjustPredefSymbol PD_bimapId mod_index STE_DclFunction ) # (pre_mod, cs_predef_symbols) = cs_predef_symbols![PD_StdMisc] | pre_mod.pds_def == mod_index @@ -3120,8 +3459,8 @@ where = foldlArraySt (count_members_of_instance mod_index) com_instance_defs (0, com_class_defs, modules) = sum - count_members_of_instance mod_index {ins_class,ins_is_generic} (sum, com_class_defs, modules) - | ins_is_generic + count_members_of_instance mod_index {ins_class,ins_generated} (sum, com_class_defs, modules) + | ins_generated = (1 + sum, com_class_defs, modules) # ({class_members}, com_class_defs, modules) = getClassDef ins_class mod_index com_class_defs modules @@ -3132,6 +3471,7 @@ adjustPredefSymbol predef_index mod_index symb_kind cs=:{cs_symbol_table,cs_erro #! pre_index = determine_index_of_symbol (sreadPtr pre_id.id_info cs_symbol_table) symb_kind | pre_index <> NoIndex = { cs & cs_predef_symbols.[predef_index] = { pds_def = pre_index, pds_module = mod_index }} + //---> ("predef_index", predef_index, size predefined_idents) = { cs & cs_error = checkError pre_id " function not defined" cs_error } where determine_index_of_symbol {ste_kind, ste_index} symb_kind diff --git a/frontend/checkFunctionBodies.dcl b/frontend/checkFunctionBodies.dcl index e1ff150..2006460 100644 --- a/frontend/checkFunctionBodies.dcl +++ b/frontend/checkFunctionBodies.dcl @@ -6,11 +6,12 @@ import syntax, checksupport :: ExpressionState = { es_expr_heap :: !.ExpressionHeap - , es_var_heap :: !.VarHeap - , es_type_heaps :: !.TypeHeaps - , es_calls :: ![FunCall] - , es_dynamics :: !Dynamics - , es_fun_defs :: !.{# FunDef} + , es_var_heap :: !.VarHeap + , es_type_heaps :: !.TypeHeaps + , es_generic_heap :: !.GenericHeap + , es_calls :: ![FunCall] + , es_dynamics :: ![ExprInfoPtr] + , es_fun_defs :: !.{# FunDef} } :: ExpressionInput = diff --git a/frontend/checkFunctionBodies.icl b/frontend/checkFunctionBodies.icl index 579d091..3bd0a78 100644 --- a/frontend/checkFunctionBodies.icl +++ b/frontend/checkFunctionBodies.icl @@ -14,11 +14,12 @@ cEndWithSelection :== False :: ExpressionState = { es_expr_heap :: !.ExpressionHeap - , es_var_heap :: !.VarHeap - , es_type_heaps :: !.TypeHeaps - , es_calls :: ![FunCall] - , es_dynamics :: !Dynamics - , es_fun_defs :: !.{# FunDef} + , es_var_heap :: !.VarHeap + , es_type_heaps :: !.TypeHeaps + , es_generic_heap :: !.GenericHeap + , es_calls :: ![FunCall] + , es_dynamics :: ![ExprInfoPtr] + , es_fun_defs :: !.{# FunDef} } :: ExpressionInput = @@ -308,8 +309,11 @@ where # (var_expr_ptr, expr_heap) = newPtr EI_Empty expr_heap = (Var { var_name = fv_name, var_info_ptr = fv_info_ptr, var_expr_ptr = var_expr_ptr }, result_expr, expr_heap) +checkFunctionBodies GeneratedBody function_ident_for_errors e_input e_state e_info cs + = (GeneratedBody, [], e_state, e_info, cs) + //---> ("checkFunctionBodies: function to derive ", function_ident_for_errors) checkFunctionBodies _ function_ident_for_errors e_input=:{ei_expr_level,ei_mod_index} e_state=:{es_var_heap, es_fun_defs} e_info cs - = abort ("checkFunctionBodies "+++toString function_ident_for_errors) + = abort ("checkFunctionBodies " +++ toString function_ident_for_errors +++ "\n") removeLocalsFromSymbolTable :: !Index !Level ![Ident] !LocalDefs !Int !*{#FunDef} !*{#*{#FunDef}} !*(Heap SymbolTableEntry) @@ -329,11 +333,11 @@ checkRhs free_vars rhs_alts rhs_locals e_input=:{ei_expr_level,ei_mod_index,ei_l (loc_defs, (var_env, array_patterns), e_state, e_info, cs) = checkLhssOfLocalDefs ei_expr_level ei_mod_index rhs_locals ei_local_functions_index_offset e_state e_info cs (es_fun_defs, e_info, heaps, cs) = checkLocalFunctions ei_mod_index ei_expr_level rhs_locals ei_local_functions_index_offset e_state.es_fun_defs e_info - { hp_var_heap = e_state.es_var_heap, hp_expression_heap = e_state.es_expr_heap, hp_type_heaps = e_state.es_type_heaps } cs + { hp_var_heap = e_state.es_var_heap, hp_expression_heap = e_state.es_expr_heap, hp_type_heaps = e_state.es_type_heaps, hp_generic_heap = e_state.es_generic_heap } cs (rhs_expr, free_vars, e_state, e_info, cs) = check_opt_guarded_alts free_vars rhs_alts { e_input & ei_expr_level = ei_expr_level } { e_state & es_fun_defs = es_fun_defs, es_var_heap = heaps.hp_var_heap, es_expr_heap = heaps.hp_expression_heap, - es_type_heaps = heaps.hp_type_heaps } e_info cs + es_type_heaps = heaps.hp_type_heaps,es_generic_heap=heaps.hp_generic_heap } e_info cs (expr, free_vars, e_state, e_info, cs) = addArraySelections array_patterns rhs_expr free_vars e_input e_state e_info cs (expr, free_vars, e_state, e_info, cs) = checkRhssAndTransformLocalDefs free_vars loc_defs expr e_input e_state e_info cs @@ -414,10 +418,11 @@ where = checkRhssAndTransformLocalDefs free_vars loc_defs seq_let_expr e_input { e_state & es_expr_heap = es_expr_heap} e_info cs (es_fun_defs, e_info, heaps, cs) = checkLocalFunctions ei_mod_index rhs_expr_level ewl_locals ei_local_functions_index_offset e_state.es_fun_defs e_info - { hp_var_heap = e_state.es_var_heap, hp_expression_heap = e_state.es_expr_heap, hp_type_heaps = e_state.es_type_heaps } cs + { hp_var_heap = e_state.es_var_heap, hp_expression_heap = e_state.es_expr_heap, hp_type_heaps = e_state.es_type_heaps,hp_generic_heap=e_state.es_generic_heap } cs (es_fun_defs,macro_defs,cs_symbol_table) = removeLocalsFromSymbolTable ei_mod_index this_expr_level var_env ewl_locals ei_local_functions_index_offset es_fun_defs e_info.ef_macro_defs cs.cs_symbol_table = (expr, free_vars, {e_state & es_fun_defs = es_fun_defs, es_var_heap = heaps.hp_var_heap, - es_expr_heap = heaps.hp_expression_heap, es_type_heaps = heaps.hp_type_heaps }, {e_info & ef_macro_defs=macro_defs}, { cs & cs_symbol_table = cs_symbol_table} ) + es_expr_heap = heaps.hp_expression_heap, es_type_heaps = heaps.hp_type_heaps, es_generic_heap=heaps.hp_generic_heap}, + {e_info & ef_macro_defs=macro_defs}, { cs & cs_symbol_table = cs_symbol_table} ) remove_seq_let_vars level [] symbol_table = symbol_table @@ -457,14 +462,14 @@ where (src_expr, free_vars, e_state, e_info, cs) = addArraySelections loc_array_patterns src_expr free_vars e_input e_state e_info cs (src_expr, free_vars, e_state, e_info, cs) = checkRhssAndTransformLocalDefs free_vars loc_defs src_expr e_input e_state e_info cs - (es_fun_defs, e_info, {hp_var_heap,hp_expression_heap,hp_type_heaps}, cs) + (es_fun_defs, e_info, {hp_var_heap,hp_expression_heap,hp_type_heaps,hp_generic_heap}, cs) = checkLocalFunctions ei_mod_index ei_expr_level ndwl_locals ei_local_functions_index_offset e_state.es_fun_defs e_info - { hp_var_heap = e_state.es_var_heap, hp_expression_heap = e_state.es_expr_heap, hp_type_heaps = e_state.es_type_heaps } cs + { hp_var_heap = e_state.es_var_heap, hp_expression_heap = e_state.es_expr_heap, hp_type_heaps = e_state.es_type_heaps,hp_generic_heap=e_state.es_generic_heap} cs (es_fun_defs,macro_defs,cs_symbol_table) = removeLocalsFromSymbolTable ei_mod_index ei_expr_level loc_env ndwl_locals ei_local_functions_index_offset es_fun_defs e_info.ef_macro_defs cs.cs_symbol_table (pattern, accus, {ps_fun_defs,ps_var_heap}, e_info, cs) = checkPattern bind_dst No { pi_def_level = ei_expr_level, pi_mod_index = ei_mod_index, pi_is_node_pattern = True } ([], []) {ps_var_heap = hp_var_heap,ps_fun_defs = es_fun_defs } {e_info & ef_macro_defs=macro_defs} { cs & cs_symbol_table = cs_symbol_table } - e_state = { e_state & es_var_heap = ps_var_heap, es_expr_heap = hp_expression_heap, es_type_heaps = hp_type_heaps,es_fun_defs = ps_fun_defs } + e_state = { e_state & es_var_heap = ps_var_heap, es_expr_heap = hp_expression_heap, es_type_heaps = hp_type_heaps,es_generic_heap=hp_generic_heap,es_fun_defs = ps_fun_defs } = (src_expr, pattern, accus, free_vars, e_state, e_info, popErrorAdmin cs) build_sequential_lets :: ![(![LetBind],![LetBind])] !Expression !Position !*ExpressionHeap -> (!Position, !Expression, !*ExpressionHeap) @@ -606,11 +611,12 @@ checkExpression free_vars (PE_Let strict let_locals expr) e_input=:{ei_expr_leve (expr, free_vars, e_state, e_info, cs) = checkRhssAndTransformLocalDefs free_vars loc_defs expr e_input e_state e_info cs (es_fun_defs, e_info, heaps, cs) = checkLocalFunctions ei_mod_index ei_expr_level let_locals ei_local_functions_index_offset e_state.es_fun_defs e_info - { hp_var_heap = e_state.es_var_heap, hp_expression_heap = e_state.es_expr_heap, hp_type_heaps = e_state.es_type_heaps } cs + { hp_var_heap = e_state.es_var_heap, hp_expression_heap = e_state.es_expr_heap, hp_type_heaps = e_state.es_type_heaps, hp_generic_heap = e_state.es_generic_heap } cs (es_fun_defs,macro_defs,cs_symbol_table) = removeLocalsFromSymbolTable ei_mod_index ei_expr_level var_env let_locals ei_local_functions_index_offset es_fun_defs e_info.ef_macro_defs cs.cs_symbol_table = (expr, free_vars, { e_state & es_fun_defs = es_fun_defs, es_var_heap = heaps.hp_var_heap, es_expr_heap = heaps.hp_expression_heap, - es_type_heaps = heaps.hp_type_heaps }, {e_info & ef_macro_defs=macro_defs}, { cs & cs_symbol_table = cs_symbol_table }) + es_type_heaps = heaps.hp_type_heaps,es_generic_heap = heaps.hp_generic_heap }, + {e_info & ef_macro_defs=macro_defs}, { cs & cs_symbol_table = cs_symbol_table }) checkExpression free_vars (PE_Case case_ident expr alts) e_input e_state e_info cs # (pattern_expr, free_vars, e_state, e_info, cs) = checkExpression free_vars expr e_input e_state e_info cs @@ -1187,13 +1193,14 @@ checkExpression free_vars (PE_Generic id=:{id_name,id_info} kind) e_input e_stat add_kind :: !Index !TypeKind !u:{#GenericDef} !*ExpressionState -> (!u:{#GenericDef}, !*ExpressionState) - add_kind generic_index kind generic_defs e_state=:{es_type_heaps=es_type_heaps=:{th_vars}} - #! (generic_def=:{gen_kinds_ptr}, generic_defs) = generic_defs ! [generic_index] - #! (TVI_Kinds kinds, th_vars) = readPtr gen_kinds_ptr th_vars - #! kinds = eqMerge [kind] kinds - #! th_vars = writePtr gen_kinds_ptr (TVI_Kinds kinds) th_vars - #! e_state = { e_state & es_type_heaps = {es_type_heaps & th_vars = th_vars}} - = (generic_defs, e_state) + add_kind generic_index kind generic_defs e_state=:{es_generic_heap} + /* + #! ({gen_info_ptr}, generic_defs) = generic_defs ! [generic_index] + #! (gen_info, es_generic_heap) = readPtr gen_info_ptr es_generic_heap + #! gen_kinds = eqMerge [(kind,NoIndex)] gen_info.gen_kinds + #! es_generic_heap = writePtr gen_info_ptr {gen_info&gen_kinds=gen_kinds} es_generic_heap + */ + = (generic_defs, {e_state & es_generic_heap = es_generic_heap}) checkExpression free_vars expr e_input e_state e_info cs = abort "checkExpression (checkFunctionBodies.icl, line 868)" // <<- expr diff --git a/frontend/checksupport.dcl b/frontend/checksupport.dcl index c96b9f7..cefb3ee 100644 --- a/frontend/checksupport.dcl +++ b/frontend/checksupport.dcl @@ -22,6 +22,7 @@ cNeedStdStrictLists :== 16 { hp_var_heap ::!.VarHeap , hp_expression_heap ::!.ExpressionHeap , hp_type_heaps ::!.TypeHeaps + , hp_generic_heap ::!.GenericHeap } :: ErrorAdmin = { ea_file :: !.File, ea_loc :: ![IdentPos], ea_ok :: !Bool } @@ -42,11 +43,12 @@ cSelectorDefs :== 2 cClassDefs :== 3 cMemberDefs :== 4 cGenericDefs :== 5 -cInstanceDefs :== 6 -cFunctionDefs :== 7 -cMacroDefs :== 8 +cGenericCaseDefs :== 6 +cInstanceDefs :== 7 +cFunctionDefs :== 8 +cMacroDefs :== 9 -cConversionTableSize :== 9 +cConversionTableSize :== 10 :: CommonDefs = { com_type_defs :: !.{# CheckedTypeDef} @@ -55,7 +57,8 @@ cConversionTableSize :== 9 , com_class_defs :: !.{# ClassDef} , com_member_defs :: !.{# MemberDef} , com_instance_defs :: !.{# ClassInstance} - , com_generic_defs :: !.{# GenericDef} + , com_generic_defs :: !.{# GenericDef} // AA + , com_gencase_defs :: !.{# GenericCaseDef} // AA } :: Declarations = { @@ -81,6 +84,7 @@ cConversionTableSize :== 9 :: CopiedDefinitions = { copied_type_defs :: {#Bool} , copied_class_defs :: {#Bool} + , copied_generic_defs :: {#Bool} } :: IclModule = @@ -89,6 +93,7 @@ cConversionTableSize :== 9 , icl_global_functions :: ![IndexRange] , icl_instances :: ![IndexRange] , icl_specials :: !IndexRange + , icl_gencases :: ![IndexRange] , icl_common :: !.CommonDefs , icl_import :: !{!Declaration} , icl_imported_objects :: ![ImportedObject] @@ -105,6 +110,7 @@ cConversionTableSize :== 9 , dcl_instances :: !IndexRange , dcl_macros :: !IndexRange , dcl_specials :: !IndexRange + , dcl_gencases :: !IndexRange , dcl_common :: !CommonDefs , dcl_sizes :: !{# Int} , dcl_dictionary_info :: !DictionaryInfo diff --git a/frontend/checksupport.icl b/frontend/checksupport.icl index 79d4669..173db80 100644 --- a/frontend/checksupport.icl +++ b/frontend/checksupport.icl @@ -26,6 +26,7 @@ cNeedStdStrictLists :== 16 { hp_var_heap ::!.VarHeap , hp_expression_heap ::!.ExpressionHeap , hp_type_heaps ::!.TypeHeaps + , hp_generic_heap ::!.GenericHeap } :: ErrorAdmin = { ea_file :: !.File, ea_loc :: ![IdentPos], ea_ok :: !Bool } @@ -42,11 +43,12 @@ cSelectorDefs :== 2 cClassDefs :== 3 cMemberDefs :== 4 cGenericDefs :== 5 -cInstanceDefs :== 6 -cFunctionDefs :== 7 -cMacroDefs :== 8 +cGenericCaseDefs :== 6 +cInstanceDefs :== 7 +cFunctionDefs :== 8 +cMacroDefs :== 9 -cConversionTableSize :== 9 +cConversionTableSize :== 10 instance toInt STE_Kind where @@ -55,6 +57,7 @@ where toInt (STE_Field _) = cSelectorDefs toInt STE_Class = cClassDefs toInt STE_Generic = cGenericDefs + toInt STE_GenericCase = cGenericCaseDefs toInt STE_Member = cMemberDefs toInt (STE_Instance _) = cInstanceDefs toInt STE_DclFunction = cFunctionDefs @@ -71,6 +74,7 @@ where , com_member_defs :: !.{# MemberDef} , com_instance_defs :: !.{# ClassInstance} , com_generic_defs :: !.{# GenericDef} // AA + , com_gencase_defs :: !.{# GenericCaseDef} // AA } :: Declarations = { @@ -96,6 +100,7 @@ where :: CopiedDefinitions = { copied_type_defs :: {#Bool} , copied_class_defs :: {#Bool} + , copied_generic_defs :: {#Bool} } :: IclModule = @@ -104,6 +109,7 @@ where , icl_global_functions :: ![IndexRange] , icl_instances :: ![IndexRange] , icl_specials :: !IndexRange + , icl_gencases :: ![IndexRange] , icl_common :: !.CommonDefs , icl_import :: !{!Declaration} , icl_imported_objects :: ![ImportedObject] @@ -120,6 +126,7 @@ where , dcl_instances :: !IndexRange , dcl_macros :: !IndexRange , dcl_specials :: !IndexRange + , dcl_gencases :: !IndexRange , dcl_common :: !CommonDefs , dcl_sizes :: !{# Int} , dcl_dictionary_info :: !DictionaryInfo diff --git a/frontend/checktypes.dcl b/frontend/checktypes.dcl index 1baadf1..8145612 100644 --- a/frontend/checktypes.dcl +++ b/frontend/checktypes.dcl @@ -2,8 +2,8 @@ definition module checktypes import checksupport, typesupport -checkTypeDefs :: !Index !(Optional (CopiedDefinitions, Int)) !*{# CheckedTypeDef} !*{# ConsDef} !*{# SelectorDef} !*{# DclModule} !*VarHeap !*TypeHeaps !*CheckState - -> (!*{# CheckedTypeDef}, !*{# ConsDef}, !*{# SelectorDef}, !*{# DclModule}, !*VarHeap, !*TypeHeaps, !*CheckState) +checkTypeDefs :: !Index !(Optional (CopiedDefinitions, Int)) !*{# CheckedTypeDef} !*{# ConsDef} !*{# SelectorDef} !*{# DclModule} !*Heaps !*CheckState + -> (!*{# CheckedTypeDef}, !*{# ConsDef}, !*{# SelectorDef}, !*{# DclModule}, !*Heaps, !*CheckState) checkFunctionType :: !Index !SymbolType !Specials !u:{# CheckedTypeDef} !v:{# ClassDef} !u:{# DclModule} !*TypeHeaps !*CheckState -> (!SymbolType, !Specials, !u:{# CheckedTypeDef}, !v:{# ClassDef}, !u:{# DclModule}, !*TypeHeaps, !*CheckState) @@ -11,7 +11,12 @@ checkFunctionType :: !Index !SymbolType !Specials !u:{# CheckedTypeDef} !v:{# Cl checkMemberType :: !Index !SymbolType !u:{# CheckedTypeDef} !v:{# ClassDef} !u:{# DclModule} !*TypeHeaps !*CheckState -> (!SymbolType, !u:{# CheckedTypeDef}, !v:{# ClassDef}, !u:{# DclModule}, !*TypeHeaps, !*CheckState) +//1.3 checkInstanceType :: !Index !(Global DefinedSymbol) !InstanceType !Specials !u:{# CheckedTypeDef} !v:{# ClassDef} !u:{# DclModule} !*TypeHeaps !*CheckState +//3.1 +/*2.0 +checkInstanceType :: !Index !(Global DefinedSymbol) !InstanceType !Specials !u:{# CheckedTypeDef} !v:{# ClassDef} !u:{# DclModule} !*TypeHeaps !*CheckState +0.2*/ -> (!InstanceType, !Specials, !u:{# CheckedTypeDef}, !v:{# ClassDef}, !u:{# DclModule}, !*TypeHeaps, !*CheckState) checkSuperClasses :: ![TypeVar] ![TypeContext] !Index !u:{# CheckedTypeDef} !v:{# ClassDef} !u:{# DclModule} !*TypeHeaps !*CheckState diff --git a/frontend/checktypes.icl b/frontend/checktypes.icl index cf8fd38..19f63a7 100644 --- a/frontend/checktypes.icl +++ b/frontend/checktypes.icl @@ -326,15 +326,15 @@ where CS_Checked :== 1 CS_Checking :== 0 -checkTypeDefs :: !Index !(Optional (CopiedDefinitions, Int)) !*{# CheckedTypeDef} !*{# ConsDef} !*{# SelectorDef} !*{# DclModule} !*VarHeap !*TypeHeaps !*CheckState - -> (!*{# CheckedTypeDef}, !*{# ConsDef}, !*{# SelectorDef}, !*{# DclModule}, !*VarHeap, !*TypeHeaps, !*CheckState) -checkTypeDefs module_index opt_icl_info type_defs cons_defs selector_defs modules var_heap type_heaps cs +checkTypeDefs :: !Index !(Optional (CopiedDefinitions, Int)) !*{# CheckedTypeDef} !*{# ConsDef} !*{# SelectorDef} !*{# DclModule} !*Heaps !*CheckState + -> (!*{# CheckedTypeDef}, !*{# ConsDef}, !*{# SelectorDef}, !*{# DclModule}, !*Heaps, !*CheckState) +checkTypeDefs module_index opt_icl_info type_defs cons_defs selector_defs modules heaps=:{hp_type_heaps,hp_var_heap} cs #! nr_of_types = size type_defs # ts = { ts_type_defs = type_defs, ts_cons_defs = cons_defs, ts_selector_defs = selector_defs, ts_modules = modules } - ti = { ti_type_heaps = type_heaps, ti_var_heap = var_heap, ti_used_types = [] } + ti = { ti_type_heaps = hp_type_heaps, ti_var_heap = hp_var_heap, ti_used_types = [] } ({ts_type_defs,ts_cons_defs, ts_selector_defs, ts_modules}, {ti_var_heap,ti_type_heaps}, cs) = iFoldSt (check_type_def module_index opt_icl_info) 0 nr_of_types (ts, ti, cs) - = (ts_type_defs, ts_cons_defs, ts_selector_defs, ts_modules, ti_var_heap, ti_type_heaps, cs) + = (ts_type_defs, ts_cons_defs, ts_selector_defs, ts_modules, {heaps& hp_var_heap=ti_var_heap, hp_type_heaps=ti_type_heaps}, cs) where check_type_def module_index opt_icl_info type_index (ts, ti, cs) | has_to_be_checked module_index opt_icl_info type_index @@ -371,6 +371,11 @@ determineAttributeVariable attr_var=:{av_name=attr_name=:{id_info}} oti=:{oti_he = ({ attr_var & av_info_ptr = attr_ptr}, oti, symbol_table) :: DemandedAttributeKind = DAK_Ignore | DAK_Unique | DAK_None +instance toString DemandedAttributeKind where + toString DAK_Ignore = "DAK_Ignore" + toString DAK_Unique = "DAK_Unique" + toString DAK_None = "DAK_None" + newAttribute :: !DemandedAttributeKind {#Char} TypeAttribute !*OpenTypeInfo !*CheckState -> (!TypeAttribute, !*OpenTypeInfo, !*CheckState) newAttribute DAK_Ignore var_name attr oti cs diff --git a/frontend/comparedefimp.icl b/frontend/comparedefimp.icl index de6729a..a0ffc60 100644 --- a/frontend/comparedefimp.icl +++ b/frontend/comparedefimp.icl @@ -11,6 +11,7 @@ import syntax, checksupport, compare_constructor, utilities, StdCompare, compile type_def_error = "type definition in the impl module conflicts with the def module" class_def_error = "class definition in the impl module conflicts with the def module" instance_def_error = "instance definition in the impl module conflicts with the def module" +generic_def_error = "generic definition in the impl module conflicts with the def module" compareError message pos error_admin = popErrorAdmin (checkError "" message (pushErrorAdmin pos error_admin)) @@ -160,6 +161,27 @@ where // ---> ("compare_instance_defs", dcl_instance_def.ins_ident, dcl_instance_def.ins_type, icl_instance_def.ins_ident, icl_instance_def.ins_type) +compareGenericDefs :: !{# Int} !{#Bool} !{# GenericDef} !u:{# GenericDef} !*CompareState -> (!u:{# GenericDef}, !*CompareState) +compareGenericDefs dcl_sizes copied_from_dcl dcl_generic_defs icl_generic_defs comp_st + # nr_of_dcl_generics = dcl_sizes.[cGenericDefs] + = iFoldSt (compare_generic_defs copied_from_dcl dcl_generic_defs) 0 nr_of_dcl_generics (icl_generic_defs, comp_st) +where + compare_generic_defs :: !{#Bool} !{# GenericDef} !Index (!u:{# GenericDef}, !*CompareState) -> (!u:{# GenericDef}, !*CompareState) + compare_generic_defs copied_from_dcl dcl_generic_defs generic_index (icl_generic_defs, comp_st) + | not copied_from_dcl.[generic_index] + # dcl_generic_def = dcl_generic_defs.[generic_index] + (icl_generic_def, icl_generic_defs) = icl_generic_defs![generic_index] + + # (ok1, comp_st) = compare dcl_generic_def.gen_type icl_generic_def.gen_type comp_st + # (ok2, comp_st) = compare dcl_generic_def.gen_vars icl_generic_def.gen_vars comp_st + | ok1 && ok2 + = (icl_generic_defs, comp_st) + # comp_error = compareError generic_def_error (newPosition icl_generic_def.gen_name icl_generic_def.gen_pos) comp_st.comp_error + = (icl_generic_defs, { comp_st & comp_error = comp_error }) + | otherwise + = (icl_generic_defs, comp_st) + + class compare a :: !a !a !*CompareState -> (!Bool, !*CompareState) @@ -384,13 +406,14 @@ compareDefImp main_dcl_module_n main_dcl_module=:{dcl_macro_conversions=Yes macr // && Trace_array macro_defs.[main_dcl_module_n] # {dcl_functions,dcl_macros,dcl_common} = main_dcl_module - {icl_common, icl_functions, icl_copied_from_dcl = {copied_type_defs,copied_class_defs}} + {icl_common, icl_functions, icl_copied_from_dcl = {copied_type_defs,copied_class_defs,copied_generic_defs}} = icl_module {hp_var_heap, hp_expression_heap, hp_type_heaps={th_vars, th_attrs}} = heaps { com_cons_defs=icl_com_cons_defs, com_type_defs = icl_com_type_defs, com_selector_defs=icl_com_selector_defs, com_class_defs = icl_com_class_defs, - com_member_defs=icl_com_member_defs, com_instance_defs = icl_com_instance_defs } + com_member_defs=icl_com_member_defs, com_instance_defs = icl_com_instance_defs, + com_generic_defs=icl_com_generic_defs} = icl_common comp_st = { comp_type_var_heap = th_vars @@ -408,6 +431,11 @@ compareDefImp main_dcl_module_n main_dcl_module=:{dcl_macro_conversions=Yes macr (icl_com_instance_defs, comp_st) = compareInstanceDefs main_dcl_module.dcl_sizes dcl_common.com_instance_defs icl_com_instance_defs comp_st + (icl_com_generic_defs, comp_st) + = compareGenericDefs + main_dcl_module.dcl_sizes copied_generic_defs + dcl_common.com_generic_defs icl_com_generic_defs comp_st + { comp_type_var_heap = th_vars, comp_attr_var_heap = th_attrs, comp_error = error_admin } = comp_st tc_state @@ -424,9 +452,10 @@ compareDefImp main_dcl_module_n main_dcl_module=:{dcl_macro_conversions=Yes macr icl_common = { icl_common & com_cons_defs=icl_com_cons_defs, com_type_defs = icl_com_type_defs, com_selector_defs=icl_com_selector_defs, com_class_defs=icl_com_class_defs, - com_member_defs=icl_com_member_defs, com_instance_defs = icl_com_instance_defs } - heaps - = { hp_var_heap = hp_var_heap, hp_expression_heap = hp_expression_heap, + com_member_defs=icl_com_member_defs, com_instance_defs = icl_com_instance_defs, + com_generic_defs=icl_com_generic_defs } + heaps + = { heaps & hp_var_heap = hp_var_heap, hp_expression_heap = hp_expression_heap, hp_type_heaps = { th_vars = tc_type_vars.hwn_heap, th_attrs = tc_attr_vars.hwn_heap}} = ({ icl_module & icl_common = icl_common, icl_functions = icl_functions },macro_defs,heaps, error_admin ) diff --git a/frontend/containers.dcl b/frontend/containers.dcl index 3662108..587f884 100644 --- a/frontend/containers.dcl +++ b/frontend/containers.dcl @@ -18,6 +18,9 @@ nsFromTo :: !Int -> NumberSet // all numbers from 0 to (i-1) bitvectToNumberSet :: !LargeBitvect -> .NumberSet +numberSetToList :: !NumberSet -> [Int] + + :: LargeBitvect :== {#Int} bitvectCreate :: !Int -> .LargeBitvect diff --git a/frontend/containers.icl b/frontend/containers.icl index dd4e66e..fae67f1 100644 --- a/frontend/containers.icl +++ b/frontend/containers.icl @@ -512,4 +512,4 @@ instance toString (a, b) | toString a & toString b where toString (a, b) = "("+++toString a+++","+++toString b+++")" -
\ No newline at end of file +
\ No newline at end of file diff --git a/frontend/frontend.icl b/frontend/frontend.icl index f78006f..97f4778 100644 --- a/frontend/frontend.icl +++ b/frontend/frontend.icl @@ -4,7 +4,7 @@ implementation module frontend import scanner, parse, postparse, check, type, trans, convertcases, overloading, utilities, convertDynamics, - convertimportedtypes, /*checkKindCorrectness, */ compilerSwitches, analtypes, generics + convertimportedtypes, /*checkKindCorrectness, */ compilerSwitches, analtypes, generics1 //import print @@ -81,8 +81,8 @@ frontEndInterface options mod_ident search_paths cached_dcl_modules functions_an select_and_remove_icl_functions_from_record :: !*IclModule -> (!.{#FunDef},!.IclModule) select_and_remove_icl_functions_from_record icl_mod=:{icl_functions} = (icl_functions,{icl_mod & icl_functions={}}) - # {icl_global_functions,icl_instances,icl_specials,icl_common,icl_import,icl_name,icl_imported_objects,icl_used_module_numbers,icl_copied_from_dcl} = icl_mod -/* + # {icl_global_functions,icl_instances,icl_gencases, icl_specials, icl_common,icl_import,icl_name,icl_imported_objects,icl_used_module_numbers,icl_copied_from_dcl} = icl_mod +/**/ (_,f,files) = fopen "components" FWriteText files (components, icl_functions, f) = showComponents components 0 True icl_functions f /* @@ -93,11 +93,12 @@ frontEndInterface options mod_ident search_paths cached_dcl_modules functions_an (ok,files) = fclose f files | ok<>ok = abort ""; -*/ +/**/ // # dcl_mods = {{dcl_mod & dcl_declared={dcls_import={},dcls_local=[],dcls_local_for_import={},dcls_explicit={}}}\\ dcl_mod<-:dcl_mods} # var_heap = heaps.hp_var_heap + gen_heap = heaps.hp_generic_heap type_heaps = heaps.hp_type_heaps fun_defs = icl_functions @@ -124,30 +125,30 @@ frontEndInterface options mod_ident search_paths cached_dcl_modules functions_an */ (class_infos, td_infos, th_vars, error_admin) = determineKindsOfClasses icl_used_module_numbers ti_common_defs td_infos th_vars error_admin - # (fun_defs, dcl_mods, td_infos, th_vars, error_admin) + # (fun_defs, dcl_mods, td_infos, th_vars, gen_heap, error_admin) = checkKindsOfCommonDefsAndFunctions n_cached_dcl_modules main_dcl_module_n icl_used_module_numbers icl_global_functions - ti_common_defs fun_defs dcl_mods td_infos class_infos th_vars error_admin + ti_common_defs fun_defs dcl_mods td_infos class_infos th_vars gen_heap error_admin type_heaps = { type_heaps & th_vars = th_vars } - # heaps = { heaps & hp_type_heaps = type_heaps } + # heaps = { heaps & hp_type_heaps = type_heaps, hp_generic_heap = gen_heap } # (saved_main_dcl_common, ti_common_defs) = replace (dcl_common_defs dcl_mods) main_dcl_module_n icl_common with dcl_common_defs :: .{#DclModule} -> .{#CommonDefs} // needed for Clean 2.0 to disambiguate overloading dcl_common_defs dcl_mods = {dcl_common \\ {dcl_common} <-: dcl_mods } - #! (components, ti_common_defs, fun_defs, generic_range, td_infos, heaps, hash_table, predef_symbols, dcl_mods, error_admin) = + #! (ti_common_defs, components, fun_defs, generic_ranges, td_infos, heaps, hash_table, predef_symbols, dcl_mods, error_admin) = SwitchGenerics (case options.feo_generics of True -> convertGenerics - components main_dcl_module_n ti_common_defs fun_defs td_infos + main_dcl_module_n icl_used_module_numbers ti_common_defs components fun_defs td_infos heaps hash_table predef_symbols dcl_mods error_admin False -> - (components, ti_common_defs, fun_defs, {ir_to=0,ir_from=0}, td_infos, heaps, hash_table, predef_symbols, dcl_mods, error_admin) + (ti_common_defs, components, fun_defs, [], td_infos, heaps, hash_table, predef_symbols, dcl_mods, error_admin) ) - (components, ti_common_defs, fun_defs, {ir_to=0,ir_from=0}, td_infos, heaps, hash_table, predef_symbols, dcl_mods, error_admin) + (ti_common_defs, components, fun_defs, [], td_infos, heaps, hash_table, predef_symbols, dcl_mods, error_admin) # (icl_common, ti_common_defs) = replace copied_ti_common_defs main_dcl_module_n saved_main_dcl_common with copied_ti_common_defs :: .{#CommonDefs} // needed for Clean 2.0 to disambiguate overloading of replace @@ -178,7 +179,7 @@ frontEndInterface options mod_ident search_paths cached_dcl_modules functions_an # (fun_def_size, fun_defs) = usize fun_defs - # (components, fun_defs) = partitionateFunctions (fun_defs -*-> "partitionateFunctions") (icl_global_functions++icl_instances++[icl_specials, generic_range]) + # (components, fun_defs) = partitionateFunctions (fun_defs -*-> "partitionateFunctions") (icl_global_functions++icl_instances ++ [icl_specials] ++ icl_gencases ++ generic_ranges) // (components, fun_defs, error) = showTypes components 0 fun_defs error // (components, fun_defs, out) = showComponents components 0 True fun_defs out @@ -195,7 +196,7 @@ frontEndInterface options mod_ident search_paths cached_dcl_modules functions_an // (components, fun_defs, error) = showComponents components 0 True fun_defs error | options.feo_up_to_phase == FrontEndPhaseConvertDynamics - # heaps = {hp_var_heap=var_heap, hp_type_heaps=type_heaps, hp_expression_heap=expression_heap} + # heaps = {hp_var_heap=var_heap, hp_type_heaps=type_heaps, hp_expression_heap=expression_heap, hp_generic_heap=newHeap} = frontSyntaxTree cached_dcl_macros cached_dcl_mods n_functions_and_macros_in_dcl_modules main_dcl_module_n predef_symbols hash_table files error io out tcl_file icl_mod dcl_mods fun_defs components array_instances heaps @@ -217,7 +218,7 @@ frontEndInterface options mod_ident search_paths cached_dcl_modules functions_an = transformGroups cleanup_info main_dcl_module_n stdStrictLists_module_n (components -*-> "Transform") fun_defs acc_args common_defs imported_funs dcl_types used_conses_in_dynamics type_def_infos var_heap type_heaps expression_heap options.feo_fusion | options.feo_up_to_phase == FrontEndPhaseTransformGroups - # heaps = {hp_var_heap=var_heap, hp_type_heaps=type_heaps, hp_expression_heap=expression_heap} + # heaps = {hp_var_heap=var_heap, hp_type_heaps=type_heaps, hp_expression_heap=expression_heap,hp_generic_heap=heaps.hp_generic_heap} = frontSyntaxTree cached_dcl_macros cached_dcl_mods n_functions_and_macros_in_dcl_modules main_dcl_module_n predef_symbols hash_table files error io out tcl_file icl_mod dcl_mods fun_defs components array_instances heaps @@ -227,7 +228,7 @@ frontEndInterface options mod_ident search_paths cached_dcl_modules functions_an // (components, fun_defs, out) = showComponents components 0 False fun_defs out | options.feo_up_to_phase == FrontEndPhaseConvertModules - # heaps = {hp_var_heap=var_heap, hp_type_heaps=type_heaps, hp_expression_heap=expression_heap} + # heaps = {hp_var_heap=var_heap, hp_type_heaps=type_heaps, hp_expression_heap=expression_heap,hp_generic_heap=heaps.hp_generic_heap} = frontSyntaxTree cached_dcl_macros cached_dcl_mods n_functions_and_macros_in_dcl_modules main_dcl_module_n predef_symbols hash_table files error io out tcl_file icl_mod dcl_mods fun_defs components array_instances heaps @@ -250,10 +251,12 @@ frontEndInterface options mod_ident search_paths cached_dcl_modules functions_an */ // # (fun_defs,out,var_heap,predef_symbols) = sa components main_dcl_module_n dcl_mods fun_defs out var_heap predef_symbols; - # heaps = {hp_var_heap = var_heap, hp_expression_heap=expression_heap, hp_type_heaps=type_heaps} + # heaps = {hp_var_heap = var_heap, hp_expression_heap=expression_heap, hp_type_heaps=type_heaps,hp_generic_heap=heaps.hp_generic_heap} # fe ={ fe_icl = // {icl_mod & icl_functions=fun_defs } - {icl_functions=fun_defs,icl_global_functions=icl_global_functions,icl_instances=icl_instances,icl_specials=icl_specials,icl_common=icl_common,icl_import=icl_import, + {icl_functions=fun_defs,icl_global_functions=icl_global_functions,icl_instances=icl_instances,icl_specials=icl_specials, + icl_common=icl_common,icl_import=icl_import, + icl_gencases = icl_gencases ++ generic_ranges, icl_name=icl_name,icl_imported_objects=icl_imported_objects,icl_used_module_numbers=icl_used_module_numbers, icl_copied_from_dcl=icl_copied_from_dcl,icl_modification_time=icl_mod.icl_modification_time} @@ -261,6 +264,7 @@ frontEndInterface options mod_ident search_paths cached_dcl_modules functions_an , fe_components = components , fe_arrayInstances = array_instances } + = (Yes fe,cached_dcl_macros,cached_dcl_mods,n_functions_and_macros_in_dcl_modules,main_dcl_module_n,predef_symbols,hash_table,files,error,io,out,tcl_file,heaps) where copy_dcl_modules dcl_mods diff --git a/frontend/general.dcl b/frontend/general.dcl index 268d9fa..5169ee3 100644 --- a/frontend/general.dcl +++ b/frontend/general.dcl @@ -14,6 +14,8 @@ instance <<< (a,b) | <<< a & <<< b instance <<< (a,b,c) | <<< a & <<< b & <<< c instance <<< (a,b,c,d) | <<< a & <<< b & <<< c & <<< d instance <<< (a,b,c,d,e) | <<< a & <<< b & <<< c & <<< d & <<< e +instance <<< (a,b,c,d,e,f) | <<< a & <<< b & <<< c & <<< d & <<< e & <<< f +instance <<< (a,b,c,d,e,f,g) | <<< a & <<< b & <<< c & <<< d & <<< e & <<< f & <<< g instance <<< [a] | <<< a :: Bind a b = diff --git a/frontend/general.icl b/frontend/general.icl index 4ac4931..3506334 100644 --- a/frontend/general.icl +++ b/frontend/general.icl @@ -44,6 +44,14 @@ instance <<< (a,b,c,d,e) | <<< a & <<< b & <<< c & <<< d & <<< e where (<<<) file (v,w,x,y,z) = file <<< '(' <<< v <<< ", " <<< w <<< ", " <<< x <<< ", " <<< y <<< ", " <<< z <<< ") " +instance <<< (a,b,c,d,e,f) | <<< a & <<< b & <<< c & <<< d & <<< e & <<< f +where + (<<<) file (u,v,w,x,y,z) = file <<< '(' <<< u <<< ", " <<< v <<< ", " <<< w <<< ", " <<< x <<< ", " <<< y <<< ", " <<< z <<< ") " + +instance <<< (a,b,c,d,e,f,g) | <<< a & <<< b & <<< c & <<< d & <<< e & <<< f & <<< g +where + (<<<) file (t,u,v,w,x,y,z) = file <<< '(' <<< t <<< ", " <<< u <<< ", " <<< v <<< ", " <<< w <<< ", " <<< x <<< ", " <<< y <<< ", " <<< z <<< ") " + instance <<< [a] | <<< a where (<<<) file [] = file <<< "[]" diff --git a/frontend/generics1.dcl b/frontend/generics1.dcl new file mode 100644 index 0000000..f0b9dc6 --- /dev/null +++ b/frontend/generics1.dcl @@ -0,0 +1,33 @@ +definition module generics1 + +import checksupport +/*2.0 +from transform import ::Group +0.2*/ +//1.3 +from transform import Group +//3.1 + +convertGenerics :: + !Int + !NumberSet + !{#CommonDefs} + !{!Group} + !*{# FunDef} + !*TypeDefInfos + !*Heaps + !*HashTable + !*PredefinedSymbols + !u:{# DclModule} + !*ErrorAdmin + -> ( !{#CommonDefs} + , !{!Group} + , !*{# FunDef} + , ![IndexRange] + , !*TypeDefInfos + , !*Heaps + , !*HashTable + , !*PredefinedSymbols + , !u:{# DclModule} + , !*ErrorAdmin + ) diff --git a/frontend/generics1.icl b/frontend/generics1.icl new file mode 100644 index 0000000..9c28918 --- /dev/null +++ b/frontend/generics1.icl @@ -0,0 +1,3062 @@ +//************************************************************************************** +// Generic programming features +//************************************************************************************** + +implementation module generics1 + +import StdEnv +import check +from checktypes import createClassDictionaries +/*2.0 +from transform import ::Group +0.2*/ +//1.3 +from transform import Group +//3.1 + +import genericsupport + +//************************************************************************************** +// Data types +//************************************************************************************** + +:: FunDefs :== {#FunDef} +:: Modules :== {#CommonDefs} +:: DclModules :== {#DclModule} +:: Groups :== {!Group} +:: FunsAndGroups :== (!Index, !Index, ![FunDef], ![Group]) + +//************************************************************************************** +// Exported functions +//************************************************************************************** + +convertGenerics :: + !Int // index of the main dcl module + !NumberSet // set of used modules + !{#CommonDefs} // common definitions of all modules + !{!Group} // groups of functions + !*{# FunDef} // functions + !*TypeDefInfos // type definition information of all modules + !*Heaps // all heaps + !*HashTable // needed for what creating class dictionaries + !*PredefinedSymbols // predefined symbols + !u:{# DclModule} // dcl modules + !*ErrorAdmin // to report errors + -> ( !{#CommonDefs} // common definitions of all modules + , !{!Group} // groups of functions + , !*{# FunDef} // function definitions + , ![IndexRange] // index ranges of generated functions + , !*TypeDefInfos // type definition infos + , !*Heaps // all heaps + , !*HashTable // needed for creating class dictinaries + , !*PredefinedSymbols // predefined symbols + , !u:{# DclModule} // dcl modules + , !*ErrorAdmin // to report errors + ) +convertGenerics + main_dcl_module_n + used_module_numbers + modules + groups + funs + td_infos + heaps + hash_table + u_predefs + dcl_modules + error + + //#! td_infos = td_infos ---> "************************* generic phase started ******************** " + //#! funs = dump_funs 0 funs + //#! dcl_modules = dump_dcl_modules 0 dcl_modules + + #! modules = {x \\ x <-: modules} // unique copy + #! dcl_modules = { x \\ x <-: dcl_modules } // unique copy + #! size_predefs = size u_predefs + #! (predefs, u_predefs) = arrayCopyBegin u_predefs size_predefs // non-unique copy + + #! td_infos = clearTypeDefInfos td_infos + //---> ("used module numbers ", main_dcl_module_n, numberSetToList used_module_numbers) + #! (modules, heaps) = clearGenericDefs modules heaps + + #! (iso_range, funs, groups, td_infos, modules, heaps, error) + = buildGenericRepresentations + (main_dcl_module_n /*---> "====================== call buildGenericRepresentations"*/) + predefs + funs groups td_infos modules heaps error + | not error.ea_ok + = (modules, groups, funs, [], td_infos, heaps, hash_table, u_predefs, dcl_modules, error) + + // build classes for each kind of each generic function + #! (modules, dcl_modules, heaps, symbol_table, td_infos, error) + = buildClasses + main_dcl_module_n used_module_numbers + modules dcl_modules heaps hash_table.hte_symbol_heap td_infos error + //---> ("====================== call buildClasses") + #! hash_table = { hash_table & hte_symbol_heap = symbol_table } + | not error.ea_ok + = (modules, groups, funs, [], td_infos, heaps, hash_table, u_predefs, dcl_modules, error) + + #! (instance_range, funs, groups, modules, dcl_modules, td_infos, heaps, error) + = convertGenericCases main_dcl_module_n used_module_numbers predefs funs groups modules dcl_modules td_infos heaps error + //---> ("====================== call convertGenericCases") + + | not error.ea_ok + = (modules, groups, funs, [], td_infos, heaps, hash_table, u_predefs, dcl_modules, error) + + //#! funs = dump_funs 0 funs + //#! dcl_modules = dump_dcl_modules 0 dcl_modules + //#! error = error ---> "************************* generic phase completed ******************** " + //| True = abort "generic phase aborted for testing\n" + = (modules, groups, funs, [iso_range, instance_range], td_infos, heaps, hash_table, u_predefs, dcl_modules, error) +where + + dump_funs n funs + | n == size funs + = funs + #! ({fun_symb, fun_type, fun_body}, funs) = funs ! [n] + #! funs = funs + //---> ("icl function ", fun_symb, n, fun_type, fun_body) + = dump_funs (inc n) funs + dump_dcl_modules n dcl_modules + | n == size dcl_modules + = dcl_modules + # ({dcl_functions}, dcl_modules) = dcl_modules ! [n] + = dump_dcl_modules (inc n) (dump_dcl_funs 0 dcl_functions dcl_modules) + //---> ("dcl module", n) + dump_dcl_funs n dcl_funs dcl_modules + | n == size dcl_funs + = dcl_modules + # {ft_symb, ft_type} = dcl_funs.[n] + = dump_dcl_funs (inc n) dcl_funs dcl_modules + //---> ("dcl function", ft_symb, n, ft_type) + + +//**************************************************************************************** +// clear stuff that might have been left over +// from compilation of other icl modules +//**************************************************************************************** + +clearTypeDefInfos td_infos + = clear_modules 0 td_infos +where + clear_modules n td_infos + | n == size td_infos + = td_infos + #! (td_infos1, td_infos) = replace td_infos n {} + #! td_infos1 = clear_td_infos 0 td_infos1 + #! (_, td_infos) = replace td_infos n td_infos1 + = clear_modules (inc n) td_infos + + clear_td_infos n td_infos + | n == size td_infos + = td_infos + #! (td_info, td_infos) = td_infos![n] + #! td_infos = {td_infos & [n] = {td_info & tdi_gen_rep = No}} + = clear_td_infos (inc n) td_infos + +clearGenericDefs modules heaps + = clear_module 0 modules heaps +where + clear_module n modules heaps + | n == size modules + = (modules, heaps) + #! ({com_generic_defs}, modules) = modules![n] + #! (com_generic_defs, heaps) = updateArraySt clear_generic_def {x\\x<-:com_generic_defs} heaps + #! modules = {modules & [n].com_generic_defs = com_generic_defs} + = clear_module (inc n) modules heaps + + clear_generic_def _ generic_def=:{gen_name,gen_info_ptr} heaps=:{hp_generic_heap} + #! (gen_info, hp_generic_heap) = readPtr gen_info_ptr hp_generic_heap + #! gen_info = + { gen_info + & gen_cases = [] + , gen_classes = createArray 32 [] + } + #! hp_generic_heap = writePtr gen_info_ptr gen_info hp_generic_heap + = (generic_def, {heaps & hp_generic_heap = hp_generic_heap}) + +//**************************************************************************************** +// generic type representation +//**************************************************************************************** + +// generic representation is built for each type argument of +// generic cases of the current module +buildGenericRepresentations :: + !Index + !PredefinedSymbols + !*FunDefs + !Groups + !*TypeDefInfos + !*Modules + !*Heaps + !*ErrorAdmin + -> ( !IndexRange + , !*FunDefs + , !Groups + , !*TypeDefInfos + , !*Modules + , !*Heaps + , !*ErrorAdmin + ) +buildGenericRepresentations main_module_index predefs funs groups td_infos modules heaps error + + #! size_funs = size funs + #! size_groups = size groups + #! ({com_gencase_defs}, modules) = modules ! [main_module_index] + + #! ((new_fun_index, new_group_index, new_funs, new_groups), td_infos, modules, heaps, error) + = foldArraySt on_gencase com_gencase_defs ((size_funs, size_groups, [], []), td_infos, modules, heaps, error) + + #! funs = arrayPlusRevList funs new_funs + #! groups = arrayPlusRevList groups new_groups + + #! range = {ir_from = size_funs, ir_to = new_fun_index} + + = (range, funs, groups, td_infos, modules, heaps, error) +where + + on_gencase index case_def=:{gc_type_cons,gc_name} st + = build_generic_rep_if_needed gc_type_cons st + + build_generic_rep_if_needed :: + !TypeCons !((!Index,!Index,![FunDef],![Group]), !*TypeDefInfos, !*{#CommonDefs}, !*Heaps, !*ErrorAdmin) + -> (!(!Index, !Index, ![FunDef], ![Group]), !*TypeDefInfos, !*{#CommonDefs}, !*Heaps, !*ErrorAdmin) + build_generic_rep_if_needed (TypeConsSymb {type_index={glob_module,glob_object}, type_name}) (funs_and_groups, td_infos, modules, heaps, error) + #! (type_def, modules) = modules![glob_module].com_type_defs.[glob_object] + #! (td_info, td_infos) = td_infos![glob_module, glob_object] + #! type_def_gi = {gi_module=glob_module,gi_index=glob_object} + = case td_info.tdi_gen_rep of + Yes _ + -> (funs_and_groups, td_infos, modules, heaps, error) + //---> ("generic representation is already built", type_name) + No + #! (gen_type_rep, funs_and_groups, modules, heaps, error) + = buildGenericTypeRep type_def_gi main_module_index predefs funs_and_groups modules heaps error + + #! td_info = {td_info & tdi_gen_rep = Yes gen_type_rep} + #! td_infos = {td_infos & [glob_module, glob_object] = td_info} + -> (funs_and_groups, td_infos, modules, heaps, error) + //---> ("build generic representation", type_name) + build_generic_rep_if_needed _ st = st + +buildGenericTypeRep :: + !GlobalIndex // type def index + !Index // main module index + !PredefinedSymbols + !(!Index,!Index,![FunDef],![Group]) + !*{#CommonDefs} + !*Heaps + !*ErrorAdmin + -> ( !GenericTypeRep + , !(!Index, !Index, ![FunDef], ![Group]) + , !*{#CommonDefs} + , !*Heaps + , !*ErrorAdmin + ) +buildGenericTypeRep type_index main_module_index predefs funs_and_groups modules heaps error + # (type_def, modules) = modules![type_index.gi_module].com_type_defs.[type_index.gi_index] + # (atype, modules,error) = buildStructureType type_index predefs modules error + + # (from_fun_ds, funs_and_groups, heaps, error) + = buildConversionFrom type_index.gi_module type_def main_module_index predefs funs_and_groups heaps error + + # (to_fun_ds, funs_and_groups, heaps, error) + = buildConversionTo type_index.gi_module type_def main_module_index predefs funs_and_groups heaps error + + # (iso_fun_ds, funs_and_groups, heaps, error) + = buildConversionIso type_def from_fun_ds to_fun_ds main_module_index predefs funs_and_groups heaps error + + = ({gtr_type=atype,gtr_iso=iso_fun_ds}, funs_and_groups, modules, heaps, error) + //---> ("buildGenericTypeRep", type_def.td_name, atype) + +//======================================================================================== +// the structure type +//======================================================================================== + +buildStructureType :: + !GlobalIndex // type definition module + !PredefinedSymbols + !*{#CommonDefs} + !*ErrorAdmin + -> ( !AType // the structure type + , !*{#CommonDefs} + , !*ErrorAdmin + ) +buildStructureType {gi_module,gi_index} predefs modules error + # (type_def=:{td_name}, modules) = modules![gi_module].com_type_defs.[gi_index] + # (common_defs, modules) = modules ! [gi_module] + # (atype, error) = build_type type_def common_defs error + = (atype, modules, error) + //---> ("buildStructureType", td_name, atype) +where + build_type {td_rhs=(AlgType alts)} common_defs error + # cons_defs = [common_defs.com_cons_defs.[ds_index] \\ {ds_index} <- alts] + # cons_args = [buildProductType cons_def.cons_type.st_args predefs \\ cons_def <- cons_defs] + = (buildSumType cons_args predefs, error) + build_type {td_rhs=(RecordType {rt_constructor={ds_index}})} common_defs error + # cons_def = common_defs.com_cons_defs.[ds_index] + = (buildProductType cons_def.cons_type.st_args predefs, error) + build_type {td_rhs=(SynType type)} common_defs error + = (type /* is that correct ???*/, error) + build_type td=:{td_rhs=(AbstractType _),td_name, td_arity, td_args, td_pos} common_defs error + = (makeAType TE TA_Multi, + reportError td_name td_pos "cannot build a generic representation of an abstract type" error) + +// build a product of types +buildProductType :: ![AType] !PredefinedSymbols -> !AType +buildProductType types predefs + = listToBin build_pair build_unit types +where + build_pair x y = buildPredefTypeApp PD_TypePAIR [x, y] predefs + build_unit = buildPredefTypeApp PD_TypeUNIT [] predefs + +// build a sum of types +buildSumType :: ![AType] !PredefinedSymbols -> !AType +buildSumType types predefs + = listToBin build_either build_void types +where + build_either x y = buildPredefTypeApp PD_TypeEITHER [x, y] predefs + build_void = abort "sum of zero types\n" + +// build a binary representation of a list +listToBin :: (a a -> a) a [a] -> a +listToBin bin tip [] = tip +listToBin bin tip [x] = x +listToBin bin tip xs + # (l,r) = splitAt ((length xs) / 2) xs + = bin (listToBin bin tip l) (listToBin bin tip r) + +// build application of a predefined type constructor +buildPredefTypeApp :: !Int [AType] !PredefinedSymbols -> !AType +buildPredefTypeApp predef_index args predefs + # {pds_module, pds_def} = predefs.[predef_index] + # pds_ident = predefined_idents.[predef_index] + # global_index = {glob_module = pds_module, glob_object = pds_def} + # type_symb = MakeTypeSymbIdent global_index pds_ident (length args) + = makeAType (TA type_symb args) TA_Multi + +//======================================================================================== +// conversions functions +//======================================================================================== + +// buildConversionIso +buildConversionIso :: + !CheckedTypeDef // the type definition + !DefinedSymbol // from fun + !DefinedSymbol // to fun + !Index // main module + !PredefinedSymbols + (!Index, !Index, ![FunDef], ![Group]) + !*Heaps + !*ErrorAdmin + -> ( !DefinedSymbol + , (!Index, !Index, ![FunDef], ![Group]) + , !*Heaps + , !*ErrorAdmin + ) +buildConversionIso + type_def=:{td_name, td_pos} + from_fun + to_fun + main_dcl_module_n + predefs + funs_and_groups + heaps + error + #! (from_expr, heaps) = buildFunApp main_dcl_module_n from_fun [] heaps + #! (to_expr, heaps) = buildFunApp main_dcl_module_n to_fun [] heaps + #! (iso_expr, heaps) = build_iso to_expr from_expr heaps + + #! ident = makeIdent ("iso" +++ td_name.id_name) + #! (def_sym, funs_and_groups) = buildFunAndGroup ident [] iso_expr No main_dcl_module_n td_pos funs_and_groups + = (def_sym, funs_and_groups, heaps, error) + //---> ("buildConversionIso", td_name, let (_,_,fs,_) = funs_and_groups in hd fs) +where + build_iso to_expr from_expr heaps + = buildPredefConsApp PD_ConsBimap [to_expr, from_expr] predefs heaps + +// conversion from type to generic +buildConversionTo :: + !Index // type def module + !CheckedTypeDef // the type def + !Index // main module + !PredefinedSymbols + !(!Index, !Index, ![FunDef], ![Group]) + !*Heaps + !*ErrorAdmin + -> ( !DefinedSymbol + , (!Index, !Index, ![FunDef], ![Group]) + , !*Heaps + , !*ErrorAdmin + ) +buildConversionTo + type_def_mod + type_def=:{td_rhs, td_name, td_index, td_pos} + main_module_index + predefs + funs_and_groups + heaps + error + # (arg_expr, arg_var, heaps) = buildVarExpr "x" heaps + # (body_expr, heaps, error) = + build_expr_for_type_rhs type_def_mod td_index td_rhs arg_expr heaps error + # fun_name = makeIdent ("fromGenericTo" +++ td_name.id_name) + | not error.ea_ok + # (def_sym, funs_and_groups) + = (buildFunAndGroup fun_name [] EE No main_module_index td_pos funs_and_groups) + = (def_sym, funs_and_groups, heaps, error) + //---> ("buildConversionTo failed", td_name) + # (def_sym, funs_and_groups) + = (buildFunAndGroup fun_name [arg_var] body_expr No main_module_index td_pos funs_and_groups) + = (def_sym, funs_and_groups, heaps, error) + //---> ("buildConversionTo", td_name, let (_,_,fs,_) = funs_and_groups in hd fs) +where + // build conversion for type rhs + build_expr_for_type_rhs :: + !Int // type def module + !Int // type def index + !TypeRhs // type def rhs + !Expression // expression of the function argument variable + !*Heaps + !*ErrorAdmin + -> ( !Expression // generated expression + , !*Heaps // state + , !*ErrorAdmin + ) + build_expr_for_type_rhs type_def_mod type_def_index (AlgType def_symbols) arg_expr heaps error + = build_expr_for_conses type_def_mod type_def_index def_symbols arg_expr heaps error + build_expr_for_type_rhs type_def_mod type_def_index (RecordType {rt_constructor}) arg_expr heaps error + = build_expr_for_conses type_def_mod type_def_index [rt_constructor] arg_expr heaps error + build_expr_for_type_rhs type_def_mod type_def_index (AbstractType _) arg_expr heaps error + #! error = checkErrorWithIdentPos (newPosition td_name td_pos) "cannot build isomorphisms for an abstract type" error + = (EE, heaps, error) + build_expr_for_type_rhs type_def_mod type_def_index (SynType _) arg_expr heaps error + #! error = checkErrorWithIdentPos (newPosition td_name td_pos) "cannot build isomorphisms for a synonym type" error + = (EE, heaps, error) + + // build conversion for constructors of a type def + build_expr_for_conses type_def_mod type_def_index cons_def_syms arg_expr heaps error + # (case_alts, heaps, error) = + build_exprs_for_conses 0 (length cons_def_syms) type_def_mod cons_def_syms heaps error + # case_patterns = AlgebraicPatterns {glob_module = type_def_mod, glob_object = type_def_index} case_alts + # (case_expr, heaps) = buildCaseExpr arg_expr case_patterns heaps + = (case_expr, heaps, error) + //---> (free_vars, case_expr) + + // build conversions for a constructor + build_exprs_for_conses :: !Int !Int !Int ![DefinedSymbol] !*Heaps !*ErrorAdmin + -> ([AlgebraicPattern], !*Heaps, !*ErrorAdmin) + build_exprs_for_conses i n type_def_mod [] heaps error = ([], heaps, error) + build_exprs_for_conses i n type_def_mod [cons_def_sym:cons_def_syms] heaps error + #! (alt, heaps, error) = build_expr_for_cons i n type_def_mod cons_def_sym heaps error + #! (alts, heaps, error) = build_exprs_for_conses (i+1) n type_def_mod cons_def_syms heaps error + = ([alt:alts], heaps, error) + + // build conversion for a constructor + build_expr_for_cons :: !Int !Int !Int !DefinedSymbol !*Heaps !*ErrorAdmin + -> (AlgebraicPattern, !*Heaps, !*ErrorAdmin) + build_expr_for_cons + i n type_def_mod def_symbol=:{ds_ident, ds_arity} + heaps error + + #! names = ["x" +++ toString (i+1) +++ toString k \\ k <- [1..ds_arity]] + #! (var_exprs, vars, heaps) = buildVarExprs names heaps + #! (expr, heaps) = build_prod var_exprs predefs heaps + #! (expr, heaps) = build_sum i n expr predefs heaps + + #! alg_pattern = { + ap_symbol = {glob_module = type_def_mod, glob_object = def_symbol}, + ap_vars = vars, + ap_expr = expr, + ap_position = NoPos + } + = (alg_pattern, heaps, error) + + build_sum :: !Int !Int !Expression !PredefinedSymbols !*Heaps -> (!Expression, !*Heaps) + build_sum i n expr predefs heaps + | n == 0 = abort "build sum of zero elements\n" + | i >= n = abort "error building sum" + | n == 1 = (expr, heaps) + | i < (n/2) + # (expr, heaps) = build_sum i (n/2) expr predefs heaps + = build_left expr heaps + | otherwise + # (expr, heaps) = build_sum (i - (n/2)) (n - (n/2)) expr predefs heaps + = build_right expr heaps + where + build_left x heaps = buildPredefConsApp PD_ConsLEFT [x] predefs heaps + build_right x heaps = buildPredefConsApp PD_ConsRIGHT [x] predefs heaps + + build_prod :: ![Expression] !PredefinedSymbols !*Heaps -> (!Expression, !*Heaps) + build_prod [] predefs heaps = build_unit heaps + where + build_unit heaps = buildPredefConsApp PD_ConsUNIT [] predefs heaps + build_prod [expr] predefs heaps = (expr, heaps) + build_prod exprs predefs heaps + # (lexprs, rexprs) = splitAt ((length exprs)/2) exprs + # (lexpr, heaps) = build_prod lexprs predefs heaps + # (rexpr, heaps) = build_prod rexprs predefs heaps + = build_pair lexpr rexpr heaps + where + build_pair x y heaps = buildPredefConsApp PD_ConsPAIR [x, y] predefs heaps + +buildConversionFrom :: + !Index // type def module + !CheckedTypeDef // the type def + !Index // main module + !PredefinedSymbols + !(!Index, !Index, ![FunDef], ![Group]) + !*Heaps + !*ErrorAdmin + -> ( !DefinedSymbol + , (!Index, !Index, ![FunDef], ![Group]) + , !*Heaps + , !*ErrorAdmin + ) +buildConversionFrom + type_def_mod + type_def=:{td_rhs, td_name, td_index, td_pos} + main_module_index + predefs + funs_and_groups + heaps + error + # (body_expr, arg_var, heaps, error) = + build_expr_for_type_rhs type_def_mod td_rhs heaps error + # fun_name = makeIdent ("toGenericFrom" +++ td_name.id_name) + | not error.ea_ok + # (def_sym, funs_and_groups) + = (buildFunAndGroup fun_name [] EE No main_module_index td_pos funs_and_groups) + = (def_sym, funs_and_groups, heaps, error) + //---> ("buildConversionFrom failed", td_name) + # (def_sym, funs_and_groups) + = (buildFunAndGroup fun_name [arg_var] body_expr No main_module_index td_pos funs_and_groups) + = (def_sym, funs_and_groups, heaps, error) + //---> ("buildConversionFrom", td_name, let (_,_,fs,_) = funs_and_groups in hd fs) +where + // build expression for type def rhs + build_expr_for_type_rhs :: + !Index // type def module + !TypeRhs // type rhs + !*Heaps + !*ErrorAdmin + -> ( !Expression // body expresssion + , !FreeVar + , !*Heaps + , !*ErrorAdmin + ) + build_expr_for_type_rhs type_def_mod (AlgType def_symbols) heaps error + = build_sum type_def_mod def_symbols heaps error + build_expr_for_type_rhs type_def_mod (RecordType {rt_constructor}) heaps error + = build_sum type_def_mod [rt_constructor] heaps error + build_expr_for_type_rhs type_def_mod (AbstractType _) heaps error + #! error = reportError td_name td_pos "cannot build isomorphisms for an abstract type" error + = (EE, undef, heaps, error) + build_expr_for_type_rhs type_def_mod (SynType _) heaps error + #! error = reportError td_name td_pos "cannot build isomorphisms for a synonym type" error + = (EE, undef, heaps, error) + + // build expression for sums + build_sum :: + !Index + ![DefinedSymbol] + !*Heaps + !*ErrorAdmin + -> ( !Expression + , !FreeVar // top variable + , !*Heaps + , !*ErrorAdmin + ) + build_sum type_def_mod [] heaps error + = abort "algebraic type with no constructors!\n" + build_sum type_def_mod [def_symbol] heaps error + #! (cons_app_expr, cons_arg_vars, heaps) = build_cons_app type_def_mod def_symbol heaps + #! (alt_expr, var, heaps) = build_prod cons_app_expr cons_arg_vars heaps + = (alt_expr, var, heaps, error) + build_sum type_def_mod def_symbols heaps error + #! (left_def_syms, right_def_syms) = splitAt ((length def_symbols) /2) def_symbols + + #! (left_expr, left_var, heaps, error) + = build_sum type_def_mod left_def_syms heaps error + + #! (right_expr, right_var, heaps, error) + = build_sum type_def_mod right_def_syms heaps error + + #! (case_expr, var, heaps) = + build_case_either left_var left_expr right_var right_expr heaps + = (case_expr, var, heaps, error) + + // build expression for products + build_prod :: + !Expression // result of the case on product + ![FreeVar] // list of variables of the constructor pattern + !*Heaps + -> ( !Expression // generated product + , !FreeVar // top variable + , !*Heaps + ) + build_prod expr [] heaps + = build_case_unit expr heaps + build_prod expr [cons_arg_var] heaps + = (expr, cons_arg_var, heaps) + build_prod expr cons_arg_vars heaps + #! (left_vars, right_vars) = splitAt ((length cons_arg_vars) /2) cons_arg_vars + #! (expr, left_var, heaps) = build_prod expr left_vars heaps + #! (expr, right_var, heaps) = build_prod expr right_vars heaps + #! (case_expr, var, heaps) = build_case_pair left_var right_var expr heaps + = (case_expr, var, heaps) + + // build constructor applicarion expression + build_cons_app :: !Index !DefinedSymbol !*Heaps + -> (!Expression, ![FreeVar], !*Heaps) + build_cons_app cons_mod def_symbol=:{ds_arity} heaps + #! names = ["x" +++ toString k \\ k <- [1..ds_arity]] + #! (var_exprs, vars, heaps) = buildVarExprs names heaps + #! (expr, heaps) = buildConsApp cons_mod def_symbol var_exprs heaps + = (expr, vars, heaps) + + // build case expressions for PAIR, EITHER and UNIT + build_case_unit body_expr heaps + # unit_pat = buildPredefConsPattern PD_ConsUNIT [] body_expr predefs + # {pds_module, pds_def} = predefs.[PD_TypeUNIT] + # case_patterns = AlgebraicPatterns {glob_module = pds_module, glob_object = pds_def} [unit_pat] + = build_case_expr case_patterns heaps + + build_case_pair var1 var2 body_expr heaps + # pair_pat = buildPredefConsPattern PD_ConsPAIR [var1, var2] body_expr predefs + # {pds_module, pds_def} = predefs.[PD_TypePAIR] + # case_patterns = AlgebraicPatterns {glob_module = pds_module, glob_object = pds_def} [pair_pat] + = build_case_expr case_patterns heaps + + build_case_either left_var left_expr right_var right_expr heaps + # left_pat = buildPredefConsPattern PD_ConsLEFT [left_var] left_expr predefs + # right_pat = buildPredefConsPattern PD_ConsRIGHT [right_var] right_expr predefs + # {pds_module, pds_def} = predefs.[PD_TypeEITHER] + # case_patterns = AlgebraicPatterns {glob_module = pds_module, glob_object = pds_def} [left_pat, right_pat] + = build_case_expr case_patterns heaps + + // case with a variable as the selector expression + build_case_expr case_patterns heaps + # (var_expr, var, heaps) = buildVarExpr "c" heaps + # (case_expr, heaps) = buildCaseExpr var_expr case_patterns heaps + = (case_expr, var, heaps) + + +//**************************************************************************************** +// build kind indexed classes +//**************************************************************************************** + +buildClasses :: + !Int + !NumberSet + !*{#CommonDefs} + !*{#.DclModule} + !*Heaps + !*SymbolTable + !*TypeDefInfos + !*ErrorAdmin + -> (.{#CommonDefs} + ,.{#DclModule} + ,.Heaps + ,.SymbolTable + ,.TypeDefInfos + ,.ErrorAdmin + ) +buildClasses main_module_index used_module_numbers modules dcl_modules heaps symbol_table td_infos error + #! (common_defs=:{com_class_defs, com_member_defs}, modules) = modules ! [main_module_index] + #! num_classes = size com_class_defs + #! num_members = size com_member_defs + +/* + #! (modules, (classes, members, new_num_classes, new_num_members, heaps, td_infos, error)) + = mapGenericCaseDefs on_gencase modules ([], [], num_classes, num_members, heaps, td_infos, error) +*/ + #! (modules, (classes, members, new_num_classes, new_num_members, heaps, td_infos, error)) + = build_modules 0 modules ([], [], num_classes, num_members, heaps, td_infos, error) + + // obtain common definitions again because com_gencase_defs are updated + #! (common_defs, modules) = modules ! [main_module_index] + # common_defs = + { common_defs + & com_class_defs = arrayPlusRevList com_class_defs classes + , com_member_defs = arrayPlusRevList com_member_defs members + } + + #! (common_defs, dcl_modules, heaps, symbol_table) + = build_class_dictionaries common_defs dcl_modules heaps symbol_table + + #! modules = {modules & [main_module_index] = common_defs} + = (modules, dcl_modules, heaps, symbol_table, td_infos, error) +where + build_modules module_index modules st + | module_index == size modules + = (modules, st) + #! (common_defs=:{com_gencase_defs}, modules) = modules![module_index] + #! (com_gencase_defs, modules, st) + = build_module module_index com_gencase_defs modules st + #! modules = + { modules + & [module_index] = {common_defs & com_gencase_defs = com_gencase_defs } + } + = build_modules (inc module_index) modules st + + build_module module_index com_gencase_defs modules st + | inNumberSet module_index used_module_numbers + #! com_gencase_defs = {x\\x<-:com_gencase_defs} + = build_module1 module_index 0 com_gencase_defs modules st + = (com_gencase_defs, modules, st) + + build_module1 module_index index com_gencase_defs modules st + | index == size com_gencase_defs + = (com_gencase_defs, modules, st) + #! (gencase, com_gencase_defs) = com_gencase_defs ! [index] + #! (gencase, modules, st) = on_gencase module_index index gencase modules st + #! com_gencase_defs = {com_gencase_defs & [index] = gencase} + = build_module1 module_index (inc index) com_gencase_defs modules st + + on_gencase :: + !Index + !Index + !GenericCaseDef + !*Modules + (![ClassDef], ![MemberDef], !Index, Index, !*Heaps, !*TypeDefInfos, !*ErrorAdmin) + -> ( !GenericCaseDef + , !*Modules + , (![ClassDef], ![MemberDef], !Index, Index, !*Heaps, !*TypeDefInfos, !*ErrorAdmin) + ) + on_gencase + module_index index + gencase=:{gc_name,gc_generic, gc_type_cons} + modules + (classes, members, class_index, member_index, heaps, td_infos, error) + + #! (gen_def, modules) = modules ! [gc_generic.gi_module].com_generic_defs.[gc_generic.gi_index] + #! (kind, td_infos) = get_kind_of_type_cons gc_type_cons td_infos + + //#! kinds = partially_applied_kinds kind + #! st = build_class_if_needed gen_def kind (classes, members, class_index, member_index, modules, heaps, error) + + // build classes needed for shorthand instances + #! (classes, members, class_index, member_index, modules, heaps, error) + = case kind of + KindConst -> st + KindArrow ks + -> foldSt (build_class_if_needed gen_def) [KindConst:ks] st + + #! gencase = { gencase & gc_kind = kind } + = (gencase, modules, (classes, members, class_index, member_index, heaps, td_infos, error)) + + build_class_if_needed gen_def kind (classes, members, class_index, member_index, modules, heaps, error) + #! (opt_class_info, heaps) = lookup_generic_class_info gen_def kind heaps + = case opt_class_info of + No + #! (class_def, member_def, modules, heaps, error) + = buildClassAndMember main_module_index class_index member_index kind gen_def modules heaps error + #! class_info = + { gci_kind = kind + , gci_module = main_module_index + , gci_class = class_index + , gci_member = member_index + } + #! heaps = add_generic_class_info gen_def class_info heaps + -> ([class_def:classes], [member_def:members], inc class_index, inc member_index, modules, heaps, error) + Yes class_info + -> (classes, members, class_index, member_index, modules, heaps, error) + + partially_applied_kinds KindConst + = [KindConst] + partially_applied_kinds (KindArrow kinds) + = do_it kinds + where + do_it [] = [KindConst] + do_it all_ks=:[k:ks] = [(KindArrow all_ks) : do_it ks] + + get_kind_of_type_cons :: !TypeCons !*TypeDefInfos -> (!TypeKind, !*TypeDefInfos) + get_kind_of_type_cons (TypeConsBasic _) td_infos + = (KindConst, td_infos) + get_kind_of_type_cons TypeConsArrow td_infos + = (KindArrow [KindConst,KindConst], td_infos) + get_kind_of_type_cons (TypeConsSymb {type_name, type_index}) td_infos + #! ({tdi_kinds}, td_infos) = td_infos ! [type_index.glob_module,type_index.glob_object] + = (if (isEmpty tdi_kinds) KindConst (KindArrow tdi_kinds), td_infos) + get_kind_of_type_cons (TypeConsVar tv) td_infos + = (KindConst, td_infos) + + lookup_generic_class_info {gen_info_ptr} kind heaps=:{hp_generic_heap} + #! ({gen_classes}, hp_generic_heap) = readPtr gen_info_ptr hp_generic_heap + = (lookupGenericClassInfo kind gen_classes + , {heaps & hp_generic_heap = hp_generic_heap}) + + add_generic_class_info {gen_info_ptr} class_info heaps=:{hp_generic_heap} + #! (gen_info=:{gen_classes}, hp_generic_heap) = readPtr gen_info_ptr hp_generic_heap + #! gen_classes = addGenericClassInfo class_info gen_classes + #! hp_generic_heap = writePtr gen_info_ptr {gen_info&gen_classes=gen_classes} hp_generic_heap + = {heaps & hp_generic_heap = hp_generic_heap} + + build_class_dictionaries + common_defs dcl_modules + heaps=:{hp_type_heaps=hp_type_heaps=:{th_vars}, hp_var_heap} + symbol_table + #! class_defs = { x \\ x <-: common_defs.com_class_defs } // make unique copy + # type_defs = { x \\ x <-: common_defs.com_type_defs } // make unique copy + # cons_defs = { x \\ x <-: common_defs.com_cons_defs } // make unique copy + # selector_defs = { x \\ x <-: common_defs.com_selector_defs } // make unique copy + # (size_type_defs,type_defs) = usize type_defs + #! (new_type_defs, new_selector_defs, new_cons_defs,_,type_defs,selector_defs,cons_defs,class_defs, dcl_modules, th_vars, hp_var_heap, symbol_table) = + createClassDictionaries + False + main_module_index + size_type_defs + (size common_defs.com_selector_defs) + (size common_defs.com_cons_defs) + type_defs selector_defs cons_defs class_defs dcl_modules th_vars hp_var_heap symbol_table + + #! common_defs = { common_defs & + com_class_defs = class_defs, + com_type_defs = arrayPlusList type_defs new_type_defs, + com_selector_defs = arrayPlusList selector_defs new_selector_defs, + com_cons_defs = arrayPlusList cons_defs new_cons_defs} + + #! heaps = {heaps & hp_var_heap = hp_var_heap, hp_type_heaps = {hp_type_heaps & th_vars = th_vars}} + #! modules = { modules & [module_index] = common_defs } + = (common_defs, dcl_modules, heaps, symbol_table) + + +// limitations: +// - context restrictions on generic variables are not allowed +buildMemberType :: + !GenericDef + !TypeKind + !TypeVar + !*Modules + !*TypeHeaps + !*GenericHeap + !*ErrorAdmin + -> ( !SymbolType + , !*Modules + , !*TypeHeaps + , !*GenericHeap + , !*ErrorAdmin + ) +buildMemberType {gen_name,gen_pos,gen_type,gen_vars} kind class_var modules th gh error + #! (kind_indexed_st, gatvs, th, error) + = buildKindIndexedType gen_type gen_vars kind gen_name gen_pos th error + //---> ("buildMemberType called for", gen_name, kind, gen_type) + #! (member_st, th, error) + = replace_generic_vars_with_class_var kind_indexed_st gatvs kind th error + + #! th = assertSymbolType member_st th + #! th = assertSymbolType gen_type th + + = (member_st, modules, th, gh, error) + //---> ("buildMemberType returns", gen_name, kind, member_st) +where + + replace_generic_vars_with_class_var st atvs kind th error + #! th = subst_gvs atvs th + //---> ("replace_generic_vars_with_class_var called for", atvs, st) + #! (new_st, th) = applySubstInSymbolType st th + = (new_st, th, error) + //---> ("replace_generic_vars_with_class_var returns", new_st) + where + subst_gvs atvs th=:{th_vars, th_attrs} + #! tvs = [atv_variable \\ {atv_variable} <- atvs ] + #! avs = [av \\ {atv_attribute=TA_Var av} <- atvs ] + + # th_vars = foldSt subst_tv tvs th_vars + +/* + # th_attrs = case kind of + KindConst -> case avs of + [av:avs] -> foldSt (subst_av av) avs th_attrs + [] -> th_attrs + _ -> th_attrs +*/ + # th_attrs = case avs of + [av:avs] -> foldSt (subst_av av) avs th_attrs + [] -> th_attrs + + = { th & th_vars = th_vars, th_attrs = th_attrs } + + subst_tv {tv_info_ptr} th_vars + = writePtr tv_info_ptr (TVI_Type (TV class_var)) th_vars + + subst_av av {av_info_ptr} th_attrs + = writePtr av_info_ptr (AVI_Attr (TA_Var av)) th_attrs + //---> ("(1) writePtr av_info_ptr", ptrToInt av_info_ptr, av) + +buildClassAndMember + module_index class_index member_index kind + gen_def=:{gen_name, gen_pos} modules heaps error + #! (class_var, heaps) = fresh_class_var heaps + #! (member_def, modules, heaps, error) + = build_class_member class_var modules heaps error + #! class_def = build_class class_var member_def + = (class_def, member_def, modules, heaps, error) + //---> ("buildClassAndMember", gen_def.gen_name, kind) +where + fresh_class_var heaps=:{hp_type_heaps=th=:{th_vars}} + # (tv, th_vars) = freshTypeVar (makeIdent "class_var") th_vars + = (tv, {heaps & hp_type_heaps = { th & th_vars = th_vars }}) + + class_ident = genericIdentToClassIdent gen_def.gen_name kind + member_ident = genericIdentToMemberIdent gen_def.gen_name kind + class_ds = {ds_index = class_index, ds_ident = class_ident, ds_arity = 1} + + build_class_member class_var modules heaps=:{hp_var_heap, hp_type_heaps, hp_generic_heap} error + #! (type_ptr, hp_var_heap) = newPtr VI_Empty hp_var_heap + #! (tc_var_ptr, hp_var_heap) = newPtr VI_Empty hp_var_heap + #! type_context = + { tc_class = {glob_module = module_index, glob_object=class_ds} + , tc_types = [ TV class_var ] + , tc_var = tc_var_ptr + } + #! (member_type, modules, hp_type_heaps, hp_generic_heap, error) + = buildMemberType gen_def kind class_var modules hp_type_heaps hp_generic_heap error + #! member_type = { member_type & st_context = [type_context : member_type.st_context] } + #! member_def = { + me_symb = member_ident, + me_class = {glob_module = module_index, glob_object = class_index}, + me_offset = 0, + me_type = member_type, + me_type_ptr = type_ptr, // empty + me_class_vars = [class_var], // the same variable as in the class + me_pos = gen_pos, + me_priority = NoPrio + } + //---> ("member_type", member_type) + = (member_def, modules, {heaps & hp_type_heaps = hp_type_heaps, hp_var_heap = hp_var_heap, hp_generic_heap = hp_generic_heap}, error) + build_class class_var member_def=:{me_type} + #! class_member = + { ds_ident = member_ident + , ds_index = member_index + , ds_arity = me_type.st_arity + } + #! class_dictionary = + { ds_ident = class_ident + , ds_arity = 0 + , ds_index = NoIndex/*index in the type def table, filled in later*/ + } + #! class_def = { + class_name = class_ident, + class_arity = 1, + class_args = [class_var], + class_context = [], + class_pos = gen_pos, + class_members = createArray 1 class_member, + class_cons_vars = 0, // dotted class variables + class_dictionary = class_dictionary, + class_arg_kinds = [kind] + } + + = class_def + + +//**************************************************************************************** +// Convert generic cases +//**************************************************************************************** +convertGenericCases :: + !Index // current module + !NumberSet // used module numbers + !PredefinedSymbols + !*{#FunDef} + !{!Group} + !*{#CommonDefs} + !*{#DclModule} + !*TypeDefInfos + !*Heaps + !*ErrorAdmin + -> ( !IndexRange // created instance functions + , !*{#FunDef} // added instance functions + , !{!Group} // added instance groups + , !*{#CommonDefs} // added instances + , !*{#DclModule} // updated function types + , !*TypeDefInfos + , !*Heaps + , !*ErrorAdmin + ) +convertGenericCases + main_module_index used_module_numbers + predefs funs groups modules dcl_modules td_infos heaps error + + #! (first_fun_index, funs) = usize funs + #! first_group_index = size groups + #! fun_info = (first_fun_index, first_group_index, [], []) + + #! first_instance_index = size main_module_instances + #! instance_info = (first_instance_index, []) + + #! (modules1, dcl_modules, (fun_info, instance_info, funs, td_infos, heaps, error)) + = convert_modules 0 modules1 dcl_modules (fun_info, instance_info, funs, td_infos, heaps, error) + + #! (fun_index, group_index, new_funs, new_groups) = fun_info + #! funs = arrayPlusRevList funs new_funs + #! groups = arrayPlusRevList groups new_groups + + #! (instance_index, new_instances) = instance_info + #! com_instance_defs = arrayPlusRevList main_module_instances new_instances + + #! main_common_defs = {main_common_defs & com_instance_defs = com_instance_defs} + #! modules1 = {modules1 & [main_module_index] = main_common_defs} + + #! instance_fun_range = {ir_from=first_fun_index, ir_to=fun_index} + = (instance_fun_range, funs, groups, modules1, dcl_modules, td_infos, heaps, error) +where + + (main_common_defs, modules1) = modules ! [main_module_index] + main_module_classes = main_common_defs.com_class_defs + main_module_members = main_common_defs.com_member_defs + main_module_instances = main_common_defs.com_instance_defs + + convert_modules :: + !Index + !*{#CommonDefs} + !*{#DclModule} + ( FunsAndGroups + , (!Index, ![ClassInstance]) + , !*{#FunDef} + , !*TypeDefInfos + , !*Heaps + , !*ErrorAdmin + ) + -> (!*{#CommonDefs} + ,*{#DclModule} + , ( FunsAndGroups + , (!Index, ![ClassInstance]) + , !*{#FunDef} + , !*TypeDefInfos + , !*Heaps + , !*ErrorAdmin + ) + ) + convert_modules module_index modules dcl_modules st + | module_index == size modules + = (modules, dcl_modules, st) + #! (common_defs=:{com_gencase_defs}, modules) = modules ! [module_index] + #! (dcl_module=:{dcl_functions}, dcl_modules) = dcl_modules ! [module_index] + #! (dcl_functions, modules, st) + = convert_module module_index com_gencase_defs {x\\x<-:dcl_functions} modules st + #! dcl_modules = {dcl_modules & [module_index] = {dcl_module & dcl_functions = dcl_functions}} + = convert_modules (inc module_index) modules dcl_modules st + + convert_module module_index com_gencase_defs dcl_functions modules st + | inNumberSet module_index used_module_numbers + #! dcl_functions = {x\\x<-:dcl_functions} + = foldArraySt (convert_gencase module_index) + com_gencase_defs (dcl_functions, modules, st) + = (dcl_functions, modules, st) + + convert_gencase :: + !Index + !Index + !GenericCaseDef + (!*{#FunType} + ,!*Modules + , ( FunsAndGroups + , (!Index, ![ClassInstance]) + , !*{#FunDef} + , !*TypeDefInfos + , !*Heaps + , !*ErrorAdmin + ) + ) + -> (!*{#FunType} + ,!*Modules + , ( FunsAndGroups + , (!Index, ![ClassInstance]) + , !*{#FunDef} + , !*TypeDefInfos + , !*Heaps + , !*ErrorAdmin + ) + ) + convert_gencase module_index gc_index gencase=:{gc_name, gc_type} st + #! st = build_main_instance module_index gc_index gencase st + #! st = build_shorthand_instance_if_needed module_index gc_index gencase st + = st + //---> ("convert gencase", gc_name, gc_type) + + build_main_instance module_index gc_index + gencase=:{gc_name, gc_kind, gc_generic, gc_pos, gc_type, gc_type_cons, gc_body = GCB_FunIndex fun_index} + (dcl_functions, modules, (fun_info, ins_info, fun_defs, td_infos, heaps, error)) + #! ({gen_classes}, modules, heaps) + = get_generic_info gc_generic modules heaps + # (Yes class_info) + = lookupGenericClassInfo gc_kind gen_classes + + #! {class_members} + = main_module_classes . [class_info.gci_class] + #! member_def + = main_module_members . [class_members.[0].ds_index] + + #! ins_type = + { it_vars = case gc_type_cons of + TypeConsVar tv -> [tv] + _ -> [] + , it_types = [gc_type] + , it_attr_vars = [] + , it_context = [] + } + + #! (fun_type, heaps, error) + = determine_type_of_member_instance member_def ins_type heaps error + + #! (dcl_functions, heaps) + = update_dcl_function fun_index gencase fun_type dcl_functions heaps + + #! (fun_info, fun_defs, td_infos, modules, heaps, error) + = update_icl_function_if_needed + module_index + fun_index gencase fun_type + fun_info fun_defs td_infos modules heaps error + + #! (fun_info, ins_info, heaps) + = build_instance_and_member module_index class_info.gci_class gencase fun_type ins_type fun_info ins_info heaps + + = (dcl_functions, modules, (fun_info, ins_info, fun_defs, td_infos, heaps, error)) + + build_shorthand_instance_if_needed module_index gc_index gencase=:{gc_kind=KindConst} st + = st + build_shorthand_instance_if_needed + module_index gc_index + gencase=:{gc_name, gc_generic, gc_kind=KindArrow arg_kinds, gc_type} + (dcl_functions, modules, (fun_info, ins_info, fun_defs, td_infos, heaps, error)) + + #! (star_class_info, (modules, heaps)) + = get_class_for_kind gc_generic KindConst (modules, heaps) + + #! (arg_class_infos, (modules, heaps)) + = mapSt (get_class_for_kind gc_generic) arg_kinds (modules, heaps) + + #! {class_members} + = main_module_classes . [star_class_info.gci_class] + #! member_def + = main_module_members . [class_members.[0].ds_index] + + #! (ins_type, heaps) + = build_instance_type gc_type arg_class_infos heaps + + #! (fun_type, heaps, error) + = determine_type_of_member_instance member_def ins_type heaps error + + #! (memfun_ds, fun_info, heaps) + = build_shorthand_instance_member module_index gencase fun_type arg_class_infos fun_info heaps + + #! ins_info + = build_class_instance star_class_info.gci_class gencase memfun_ds ins_type ins_info + + = (dcl_functions, modules, (fun_info, ins_info, fun_defs, td_infos, heaps, error)) + where + build_instance_type type class_infos heaps=:{hp_type_heaps=th=:{th_vars},hp_var_heap} + #! arity = length class_infos + #! type_var_names = [makeIdent ("a" +++ toString i) \\ i <- [1 .. arity]] + #! (type_vars, th_vars) = mapSt freshTypeVar type_var_names th_vars + #! type_var_types = [TV tv \\ tv <- type_vars] + #! new_type_args = [makeAType t TA_Multi \\ t <- type_var_types] + + #! type = fill_type_args type new_type_args + + #! (contexts, hp_var_heap) + = zipWithSt build_context class_infos type_vars hp_var_heap + + #! ins_type = + { it_vars = type_vars + , it_types = [type] + , it_attr_vars = [] + , it_context = contexts + } + + = (ins_type, {heaps & hp_type_heaps = {th & th_vars = th_vars}, hp_var_heap = hp_var_heap}) + //---> ("instance type for shorthand instance", gc_name, gc_type, ins_type) + where + fill_type_args (TA type_symb_ident=:{type_arity} type_args) new_type_args + #! type_arity = type_arity + length new_type_args + #! type_args = type_args ++ new_type_args + = TA {type_symb_ident & type_arity = type_arity} type_args + fill_type_args TArrow [arg_type, res_type] + = arg_type --> res_type + fill_type_args (TArrow1 arg_type) [res_type] + = arg_type --> res_type + fill_type_args type args + = abort ("fill_type_args\n"---> ("fill_type_args", type, args)) + + build_context {gci_class, gci_module, gci_kind} tv hp_var_heap + # (var_info_ptr, hp_var_heap) = newPtr VI_Empty hp_var_heap + # type_context = + { tc_class = + { glob_module=gci_module // the same as icl module + , glob_object = + { ds_ident = genericIdentToClassIdent gc_name gci_kind + , ds_index = gci_class + , ds_arity = 1 + } + } + , tc_types = [TV tv] + , tc_var = var_info_ptr + } + = (type_context, hp_var_heap) + + get_generic_info {gi_module, gi_index} modules heaps=:{hp_generic_heap} + #! ({gen_info_ptr}, modules) + = modules ! [gi_module] . com_generic_defs . [gi_index] + #! (gen_info, hp_generic_heap) = readPtr gen_info_ptr hp_generic_heap + = (gen_info, modules, {heaps & hp_generic_heap = hp_generic_heap}) + + get_class_for_kind generic_gi kind (modules, heaps) + #! ({gen_classes}, modules, heaps) = get_generic_info generic_gi modules heaps + # (Yes class_info) = lookupGenericClassInfo kind gen_classes + = (class_info, (modules, heaps)) + + + determine_type_of_member_instance :: !MemberDef !InstanceType !*Heaps !*ErrorAdmin + -> (!SymbolType, !*Heaps, !*ErrorAdmin) + determine_type_of_member_instance {me_type, me_class_vars} ins_type heaps=:{hp_type_heaps, hp_var_heap} error + #! (symbol_type, _, hp_type_heaps, _, error) + = determineTypeOfMemberInstance me_type me_class_vars ins_type SP_None hp_type_heaps No error + #! (st_context, hp_var_heap) = initializeContextVariables symbol_type.st_context hp_var_heap + #! hp_type_heaps = clearSymbolType me_type hp_type_heaps + #! symbol_type = {symbol_type & st_context = st_context} + #! heaps = {heaps & hp_type_heaps = hp_type_heaps, hp_var_heap = hp_var_heap} + = (symbol_type, heaps, error) + //---> ("determine_type_of_member_instance", ins_type, symbol_type) + + update_dcl_function :: !Index !GenericCaseDef !SymbolType !*{#FunType} !*Heaps + -> (!*{#FunType}, !*Heaps) + update_dcl_function fun_index {gc_name, gc_type_cons} symbol_type dcl_functions heaps + | fun_index < size dcl_functions + #! (symbol_type, heaps) = fresh_symbol_type symbol_type heaps + #! (fun, dcl_functions) = dcl_functions ! [fun_index] + #! fun = + { fun + & ft_symb = genericIdentToFunIdent gc_name gc_type_cons + , ft_type = symbol_type + } + #! dcl_functions = { dcl_functions & [fun_index] = fun} + = (dcl_functions, heaps) + //---> ("update dcl function", fun.ft_symb, fun_index, symbol_type) + = (dcl_functions, heaps) + //---> ("update dcl function: not in the dcl module", fun_index) + + update_icl_function_if_needed module_index fun_index gencase fun_type fun_info fun_defs td_infos modules heaps error + | module_index == main_module_index // current module + #! (fi, gi, fs, gs) = fun_info + #! (gi, gs, fun_defs, td_infos, modules, heaps, error) + = update_icl_function fun_index gencase fun_type gi gs fun_defs td_infos modules heaps error + = ((fi, gi, fs, gs), fun_defs, td_infos, modules, heaps, error) + = (fun_info, fun_defs, td_infos, modules, heaps, error) + + update_icl_function :: + !Index !GenericCaseDef !SymbolType + !Index ![Group] !*{#FunDef} !*TypeDefInfos !*{#CommonDefs} !*Heaps !*ErrorAdmin + -> (!Index, ![Group], !*{#FunDef}, !*TypeDefInfos, !*{#CommonDefs}, !*Heaps, !*ErrorAdmin) + update_icl_function fun_index gencase=:{gc_name, gc_type_cons, gc_pos} st group_index groups fun_defs td_infos modules heaps error + #! (st, heaps) = fresh_symbol_type st heaps + #! (fun=:{fun_body, fun_arity}, fun_defs) = fun_defs ! [fun_index] + #! fun_ident = genericIdentToFunIdent gc_name gc_type_cons + = case fun_body of + TransformedBody tb // user defined case + | fun_arity <> st.st_arity + # error = reportError gc_name gc_pos + ("incorrect arity: " +++ toString st.st_arity +++ " expected") error + -> (group_index, groups, fun_defs, td_infos, modules, heaps, error) + #! fun = + { fun + & fun_symb = fun_ident + , fun_type = Yes st + , fun_body = fun_body + } + #! fun_defs = { fun_defs & [fun_index] = fun } + -> (group_index, groups, fun_defs, td_infos, modules, heaps, error) + //---> ("update_icl_function, TransformedBody", fun.fun_symb, fun_index, st) + + GeneratedBody // derived case + #! (TransformedBody {tb_args, tb_rhs}, td_infos, modules, heaps, error) + = buildGenericCaseBody main_module_index gencase st predefs td_infos modules heaps error + //---> ("call buildGenericCaseBody\n") + #! fun = makeFunction fun_ident fun_index group_index tb_args tb_rhs (Yes st) main_module_index gc_pos + #! fun_defs = { fun_defs & [fun_index] = fun } + + # group = {group_members=[fun_index]} + + -> (inc group_index, [group:groups], fun_defs, td_infos, modules, heaps, error) + //---> ("update_icl_function, GeneratedBody", fun.fun_symb, fun_index, st) + _ -> abort "update_icl_function: generic case body\n" + + // build wrapping instance for the generic case function + build_instance_and_member :: !Index !Index !GenericCaseDef !SymbolType !InstanceType !FunsAndGroups (!Index, ![ClassInstance]) !*Heaps + -> (!FunsAndGroups, (!Index, ![ClassInstance]), !*Heaps) + build_instance_and_member module_index class_index gencase symbol_type ins_type fun_info ins_info heaps + #! (memfun_ds, fun_info, heaps) + = build_instance_member module_index gencase symbol_type fun_info heaps +/* + #! ins_type = + { it_vars = [] + , it_types = [gencase.gc_type] + , it_attr_vars = [] + , it_context = [] + } +*/ + #! ins_info = build_class_instance class_index gencase memfun_ds ins_type ins_info + = (fun_info, ins_info, heaps) + + // Creates a function that just calls the generic case function + // It is needed because the instance member must be in the same + // module as the instance itself + build_instance_member module_index gencase st fun_info heaps + + # {gc_name, gc_pos, gc_type_cons, gc_kind, gc_body=GCB_FunIndex fun_index} = gencase + #! arg_var_names = ["x" +++ toString i \\ i <- [1..st.st_arity]] + #! (arg_var_exprs, arg_vars, heaps) = buildVarExprs arg_var_names heaps + + #! (expr_info_ptr, hp_expression_heap) = newPtr EI_Empty heaps.hp_expression_heap + #! heaps = {heaps & hp_expression_heap = hp_expression_heap} + #! fun_name = genericIdentToFunIdent gc_name gc_type_cons + #! expr = App + { app_symb = + { symb_name=fun_name + , symb_kind=SK_Function {glob_module=module_index, glob_object=fun_index} + } + , app_args = arg_var_exprs + , app_info_ptr = expr_info_ptr + } + + #! (st, heaps) = fresh_symbol_type st heaps + + #! memfun_name = genericIdentToMemberIdent gc_name gc_kind + #! (fun_ds, fun_info) + = buildFunAndGroup memfun_name arg_vars expr (Yes st) main_module_index gc_pos fun_info + = (fun_ds, fun_info, heaps) + + build_shorthand_instance_member module_index gencase=:{gc_generic, gc_name, gc_kind, gc_pos} st class_infos fun_info heaps + #! arg_var_names = ["x" +++ toString i \\ i <- [1..st.st_arity]] + #! (arg_var_exprs, arg_vars, heaps) = buildVarExprs arg_var_names heaps + + #! (expr_info_ptr, hp_expression_heap) = newPtr EI_Empty heaps.hp_expression_heap + #! heaps = {heaps & hp_expression_heap = hp_expression_heap} + #! fun_name = genericIdentToMemberIdent gc_name KindConst + + # (gen_exprs, heaps) = mapSt (build_generic_app gc_generic gc_name) class_infos heaps + + # (body_expr, heaps) + = buildGenericApp gc_generic.gi_module gc_generic.gi_index + gc_name gc_kind (gen_exprs ++ arg_var_exprs) heaps + + #! (st, heaps) = fresh_symbol_type st heaps + + #! (fun_ds, fun_info) + = buildFunAndGroup fun_name arg_vars body_expr (Yes st) main_module_index gc_pos fun_info + + = (fun_ds, fun_info, heaps) + //---> ("shorthand instance body", body_expr) + where + build_generic_app {gi_module, gi_index} gc_name {gci_kind} heaps + = buildGenericApp gi_module gi_index gc_name gci_kind [] heaps + + build_class_instance class_index gencase member_fun_ds ins_type (ins_index, instances) + + # {gc_pos, gc_name, gc_kind} = gencase + + #! class_name = genericIdentToClassIdent gc_name gc_kind + #! class_ds = {ds_index = class_index, ds_arity=1, ds_ident=class_name} + #! ins = + { ins_class = {glob_module=main_module_index, glob_object=class_ds} + , ins_ident = class_name + , ins_type = ins_type + , ins_members = {member_fun_ds} + , ins_specials = SP_None + , ins_pos = gc_pos + , ins_generated = True + } + + = (inc ins_index, [ins:instances]) + + fresh_symbol_type :: !SymbolType !*Heaps -> (!SymbolType, !*Heaps) + fresh_symbol_type st heaps=:{hp_type_heaps} + # (fresh_st, hp_type_heaps) = freshSymbolType st hp_type_heaps + = (fresh_st, {heaps & hp_type_heaps = hp_type_heaps}) + //---> ("fresh_symbol_type") + +buildGenericCaseBody :: + !Index + !GenericCaseDef + !SymbolType + !PredefinedSymbols + !*TypeDefInfos + !*{#CommonDefs} + !*Heaps + !*ErrorAdmin + -> ( !FunctionBody + , !*TypeDefInfos + , !*{#CommonDefs} + , !*Heaps + , !*ErrorAdmin + ) +buildGenericCaseBody main_module_index {gc_name,gc_pos, gc_generic, gc_type_cons=TypeConsSymb {type_index}} st predefs td_infos modules heaps error + + // get all the data we need + #! (gen_def=:{gen_vars, gen_type, gen_bimap}, modules) + = modules ! [gc_generic.gi_module].com_generic_defs.[gc_generic.gi_index] + #! (td_info=:{tdi_gen_rep}, td_infos) + = td_infos ! [type_index.glob_module, type_index.glob_object] + # ({gtr_iso, gtr_type}) = case tdi_gen_rep of + Yes x -> x + No -> abort "no generic representation\n" + + #! (type_def=:{td_args}, modules) + = modules ! [type_index.glob_module].com_type_defs.[type_index.glob_object] + + #! original_arity = gen_type.st_arity // arity of generic type + #! generated_arity = st.st_arity - original_arity // number of added arguments (arity of the kind) + + // generate variable names and exprs + #! generated_arg_names = [ "f" +++ toString n \\ n <- [1 .. generated_arity]] + #! (generated_arg_exprs, generated_arg_vars, heaps) = buildVarExprs generated_arg_names heaps + #! original_arg_names = [ "x" +++ toString n \\ n <- [1 .. original_arity]] + #! (original_arg_exprs, original_arg_vars, heaps) = buildVarExprs original_arg_names heaps + #! arg_vars = generated_arg_vars ++ original_arg_vars + + // create adaptor + #! (iso_exprs, heaps) + = unfoldnSt (buildFunApp main_module_index gtr_iso []) (length gen_vars) heaps + #! (bimap_id_exprs, heaps) + = unfoldnSt (buildPredefFunApp PD_bimapId [] predefs) (length (gen_type.st_vars -- gen_vars)) heaps + + //#! (bimap_expr, heaps) + // = buildFunApp main_module_index gen_bimap iso_exprs heaps + #! spec_env = + [(tv,expr)\\tv <- gen_vars & expr <- iso_exprs] + ++ + [(tv,expr)\\tv <- gen_type.st_vars -- gen_vars & expr <- bimap_id_exprs] + #! curried_gen_type = curry_symbol_type gen_type + #! {pds_module = bimap_module, pds_def=bimap_index} = predefs.[PD_GenericBimap] + + #! (bimap_expr, (td_infos, heaps, error)) + = buildSpecializedExpr1 + bimap_module bimap_index + curried_gen_type spec_env + gc_name gc_pos + (td_infos, heaps, error) + + #! adaptor_expr = buildRecordSelectionExpr bimap_expr PD_map_from predefs + + // create expression for the generic representation + #! spec_env = [(atv_variable, expr) \\ {atv_variable} <- td_args & expr <- generated_arg_exprs] + #! (specialized_expr, (td_infos, heaps, error)) + = buildSpecializedExpr1 + gc_generic.gi_module gc_generic.gi_index + gtr_type spec_env + gc_name gc_pos + (td_infos, heaps, error) + + // create the body expr + #! body_expr = if (isEmpty original_arg_exprs) + (adaptor_expr @ [specialized_expr]) + ((adaptor_expr @ [specialized_expr]) @ original_arg_exprs) + + = (TransformedBody {tb_args=arg_vars, tb_rhs=body_expr}, td_infos, modules, heaps, error) + //---> (" buildGenericCaseBody", body_expr) +where + curry_symbol_type {st_args, st_result} + = foldr (\x y -> makeAType (x --> y) TA_Multi) st_result st_args + +//buildGenericCaseBody main_module_index {gc_name,gc_pos, gc_generic, gc_type_cons=TypeConsSymb {type_index}} st predefs td_infos modules heaps error +buildGenericCaseBody main_module_index {gc_name,gc_pos} st predefs td_infos modules heaps error + # error = reportError gc_name gc_pos "cannot specialize to this type" error + = (TransformedBody {tb_args=[], tb_rhs=EE}, td_infos, modules, heaps, error) + +//**************************************************************************************** +// specialization +//**************************************************************************************** + +buildSpecializedExpr1 :: + !Index // generic module + !Index // generic index + !AType // type to specialize to + ![(TypeVar, Expression)] // specialization environment + !Ident // generic/generic case + !Position // of generic case + (!*TypeDefInfos, !*Heaps, !*ErrorAdmin) + -> ( !Expression + , !(!*TypeDefInfos, !*Heaps, !*ErrorAdmin) + ) +buildSpecializedExpr1 gen_module gen_index atype spec_env ident pos (td_infos, heaps, error) + + #! heaps = set_tvs spec_env heaps + #! (expr, (td_infos, heaps, error)) + = buildSpecializedExpr gen_module gen_index atype ident pos (td_infos, heaps, error) + + #! heaps = clear_tvs spec_env heaps + = (expr, (td_infos, heaps, error)) +where + set_tvs spec_env heaps=:{hp_type_heaps=hp_type_heaps=:{th_vars}} + #! th_vars = foldSt write_tv spec_env th_vars + with write_tv ({tv_info_ptr}, expr) th_vars + = writePtr tv_info_ptr (TVI_Expr expr) th_vars + = {heaps & hp_type_heaps = {hp_type_heaps & th_vars = th_vars }} + + clear_tvs spec_env heaps=:{hp_type_heaps=hp_type_heaps=:{th_vars}} + #! th_vars = foldSt write_tv spec_env th_vars + with write_tv ({tv_info_ptr}, _) th_vars + = writePtr tv_info_ptr TVI_Empty th_vars + = {heaps & hp_type_heaps = {hp_type_heaps & th_vars = th_vars }} + +// generates an expression that corresponds to a type +buildSpecializedExpr :: + !Index // generic module index + !Index // generic index + !AType // type to specialize to + // tv_info_ptr of type variables must contain expressions + // corresponding to the type variables + !Ident // for error reporting + !Position // for error reporting + !(!*TypeDefInfos, !*Heaps, !*ErrorAdmin) + -> ( !Expression // generated expression + , !(!*TypeDefInfos, !*Heaps, !*ErrorAdmin) + ) +buildSpecializedExpr gen_module gen_index type gen_name pos gs + = spec_atype type gs +where + spec_atype {at_type} gs = spec_type at_type gs + + spec_atypes [] gs = ([], gs) + spec_atypes [type:types] gs + # (expr, gs) = spec_atype type gs + # (exprs, gs) = spec_atypes types gs + = ([expr:exprs], gs) + + spec_type :: !Type !(!*TypeDefInfos, !*Heaps, !*ErrorAdmin) + -> !(Expression, !(!*TypeDefInfos, !*Heaps, !*ErrorAdmin)) + spec_type (TA {type_index, type_name} args) st + # (arg_exprs, st) = spec_atypes args st + # (kind, st) = get_kind type_index st + = build_generic_app kind arg_exprs st + spec_type (TAS {type_index, type_name} args _) st + # (arg_exprs, st) = spec_atypes args st + # (kind, st) = get_kind type_index st + = build_generic_app kind arg_exprs st + spec_type (arg_type --> res_type) st + #! (arg_expr, st) = spec_atype arg_type st + #! (res_expr, st) = spec_atype res_type st + = build_generic_app (KindArrow [KindConst, KindConst]) [arg_expr, res_expr] st + spec_type ((CV type_var) :@: args) gs + #! (expr, gs) = spec_type_var type_var gs + #! (exprs, gs) = spec_atypes args gs + = (expr @ exprs, gs) + spec_type (TB basic_type) st + = build_generic_app KindConst [] st + spec_type (TFA atvs type) (td_infos, heaps, error) + #! error = reportError gen_name pos "cannot specialize to forall types" error + = (EE, (td_infos, heaps, error)) + spec_type (TV type_var) gs = spec_type_var type_var gs + //spec_type (GTV type_var) gs = spec_type_var type_var gs + //spec_type (TQV type_var) gs = spec_type_var type_var gs + //spec_type (TLifted type_var) gs = spec_type_var type_var gs + spec_type _ (td_infos, heaps, error) + #! error = reportError gen_name pos "cannot specialize to this type" error + = (EE, (td_infos, heaps, error)) + + spec_type_var tv=:{tv_info_ptr} (td_infos, heaps=:{hp_type_heaps=th=:{th_vars}}, error) + #! (TVI_Expr expr, th_vars) = readPtr tv_info_ptr th_vars + = (expr, (td_infos, {heaps & hp_type_heaps = {th & th_vars = th_vars}}, error)) + + build_generic_app kind arg_exprs (td_infos, heaps, error) + # (expr, heaps) + = buildGenericApp gen_module gen_index gen_name kind arg_exprs heaps + = (expr, (td_infos, heaps, error)) + + get_kind {glob_module, glob_object} (td_infos, heaps, error) + # (td_info, td_infos) = td_infos ! [glob_module, glob_object] + = (make_kind td_info.tdi_kinds, (td_infos, heaps, error)) + where + make_kind [] = KindConst + make_kind ks = KindArrow ks + +//**************************************************************************************** +// kind indexing of generic types +//**************************************************************************************** + +// kind indexing: +// t_* a1 ... an = t a1 ... an +// t_{k->l} a1 ... an = forall b1...bn.(t_k b1 ... bn) -> (t_l (a1 b1) ... (an bn)) +buildKindIndexedType :: + !SymbolType // symbol type to kind-index + ![TypeVar] // generic type variables + !TypeKind // kind index + !Ident // name for debugging + !Position // position for debugging + !*TypeHeaps // type heaps + !*ErrorAdmin + -> ( !SymbolType // instantiated type + , ![ATypeVar] // fresh generic type variables + , !*TypeHeaps // type heaps + , !*ErrorAdmin + ) +buildKindIndexedType st gtvs kind ident pos th error + + #! th = clearSymbolType st th + //---> ("buildKindIndexedType called for", kind, gtvs, st) + #! (fresh_st, fresh_gtvs, th) = fresh_generic_type st gtvs th + + #! (gatvs, th) = collectAttrsOfTypeVarsInSymbolType fresh_gtvs fresh_st th + + #! (kind_indexed_st, _, th, error) = build_symbol_type fresh_st gatvs kind 1 th error + + #! th = clearSymbolType kind_indexed_st th + #! th = clearSymbolType st th // paranoja + = (kind_indexed_st, gatvs, th, error) + //---> ("buildKindIndexedType returns", kind_indexed_st) +where + + fresh_generic_type st gtvs th + # (fresh_st, th) = freshSymbolType st th + # fresh_gtvs = take (length gtvs) fresh_st.st_vars + = (fresh_st, fresh_gtvs, th) + //---> ("fresh_generic_type", fresh_gtvs, fresh_st) + + build_symbol_type :: + !SymbolType // generic type, + ![ATypeVar] // attributed generic variables + !TypeKind // kind to specialize to + !Int // current order (in the sense of the order of the kind) + !*TypeHeaps + !*ErrorAdmin + -> ( !SymbolType // new generic type + , ![ATypeVar] // fresh copies of generic variables created for the + // generic arguments + , !*TypeHeaps + , !*ErrorAdmin + ) + build_symbol_type st gatvs KindConst order th error + = (st, [], th, error) + build_symbol_type st gatvs (KindArrow kinds) order th error + | order > 2 + //---> ("build_symbol_type called for", (KindArrow kinds), gatvs, st) + # error = reportError ident pos "kinds of order higher then 2 are not supported" error + = (st, [], th, error) + + # (arg_sts, arg_gatvss, th, error) + = build_args st gatvs order kinds th error + + # (body_st, th) + = build_body st gatvs (transpose arg_gatvss) th + + # num_added_args = length kinds + # new_st = + { st_vars = removeDup ( + foldr (++) body_st.st_vars [st_vars \\ {st_vars}<-arg_sts]) + , st_attr_vars = removeDup ( + foldr (++) body_st.st_attr_vars [st_attr_vars \\ {st_attr_vars}<-arg_sts]) + , st_args = [st_result \\ {st_result}<-arg_sts] ++ body_st.st_args + , st_result = body_st.st_result + , st_arity = body_st.st_arity + num_added_args + , st_context = removeDup( + foldr (++) body_st.st_context [st_context \\ {st_context} <- arg_sts]) + , st_attr_env = removeDup( + foldr (++) body_st.st_attr_env [st_attr_env \\ {st_attr_env} <- arg_sts]) + , st_args_strictness = insert_n_lazy_values_at_beginning num_added_args body_st.st_args_strictness + } + + = (new_st, flatten arg_gatvss, th, error) + //---> ("build_symbol_type returns", arg_gatvss, st) + + build_args st gatvs order kinds th error + # (arg_sts_and_gatvss, (_,th,error)) + = mapSt (build_arg st gatvs order) kinds (1,th,error) + # (arg_sts, arg_gatvss) = unzip arg_sts_and_gatvss + = (arg_sts, arg_gatvss, th, error) + + build_arg :: + !SymbolType // current part of the generic type + ![ATypeVar] // generic type variables with their attrs + !Int // order + !TypeKind // kind corrseponding to the arg + ( !Int // the argument number + , !*TypeHeaps + , !*ErrorAdmin + ) + -> ( (!SymbolType, [ATypeVar]) // fresh symbol type and generic variables + , ( !Int // incremented argument number + , !*TypeHeaps + , !*ErrorAdmin + ) + ) + build_arg st gatvs order kind (arg_num, th, error) + #! th = clearSymbolType st th + //---> ("build_arg called for", arg_num, kind, gatvs, st) + #! (fresh_gatvs, th) = mapSt subst_gatv gatvs th + #! (new_st, th) = applySubstInSymbolType st th + + #! (new_st, forall_atvs, th, error) + = build_symbol_type new_st fresh_gatvs kind (inc order) th error + #! (curry_st, th) + = curryGenericArgType1 new_st ("cur" +++ toString order +++ postfix) th + + #! curry_st = adjust_forall curry_st forall_atvs + + = ((curry_st, fresh_gatvs), (inc arg_num, th, error)) + //---> ("build_arg returns", fresh_gatvs, curry_st) + where + postfix = toString arg_num + + subst_gatv atv=:{atv_attribute, atv_variable} th=:{th_attrs, th_vars} + # (tv, th_vars) = subst_gtv atv_variable th_vars + # (attr, th_attrs) = subst_attr atv_attribute th_attrs + = ( {atv & atv_variable = tv, atv_attribute = attr} + , {th & th_vars = th_vars, th_attrs = th_attrs} + ) + + // generic type var is replaced with a fresh one + subst_gtv {tv_info_ptr, tv_name} th_vars + # (tv, th_vars) = freshTypeVar (postfixIdent tv_name postfix) th_vars + = (tv, writePtr tv_info_ptr (TVI_Type (TV tv)) th_vars) + + subst_attr (TA_Var {av_name, av_info_ptr}) th_attrs + # (av, th_attrs) = freshAttrVar (postfixIdent av_name postfix) th_attrs + = (TA_Var av, writePtr av_info_ptr (AVI_Attr (TA_Var av)) th_attrs) + //---> ("(2) writePtr av_info_ptr", ptrToInt av_info_ptr, av) + subst_attr TA_Multi th = (TA_Multi, th) + subst_attr TA_Unique th = (TA_Unique, th) + + adjust_forall curry_st [] = curry_st + adjust_forall curry_st=:{st_result} forall_atvs + #! st_result = {st_result & at_type = TFA forall_atvs st_result.at_type} + = { curry_st + & st_result = st_result + , st_attr_vars + = curry_st.st_attr_vars -- [av \\ {atv_attribute=TA_Var av} <- forall_atvs] + , st_vars + = curry_st.st_vars -- [atv_variable \\ {atv_variable} <- forall_atvs] + } + //---> ("adjust forall", curry_st.st_vars, forall_atvs, curry_st.st_vars -- [atv_variable \\ {atv_variable} <- forall_atvs]) + + build_body :: + !SymbolType + ![ATypeVar] + ![[ATypeVar]] + !*TypeHeaps + -> (!SymbolType + , !*TypeHeaps + ) + build_body st gatvs arg_gatvss th + # th = clearSymbolType st th + # th = fold2St subst_gatv gatvs arg_gatvss th + = applySubstInSymbolType st th + where + subst_gatv gatv=:{atv_variable} arg_gatvs th=:{th_vars} + #! type_args = [ makeAType (TV atv_variable) atv_attribute + \\ {atv_variable, atv_attribute} <- arg_gatvs] + #! type = (CV atv_variable) :@: type_args + #! th_vars = writePtr atv_variable.tv_info_ptr (TVI_Type type) th_vars + = {th & th_vars = th_vars} + +reportError name pos msg error=:{ea_file} + //= checkErrorWithIdentPos (newPosition name pos) msg error + # ea_file = ea_file <<< "Error " <<< (newPosition name pos) <<< ":" <<< msg <<< '\n' + = { error & ea_file = ea_file , ea_ok = False } + +reportWarning name pos msg error=:{ea_file} + # ea_file = ea_file <<< "Warning " <<< (newPosition name pos) <<< ":" <<< msg <<< '\n' + = { error & ea_file = ea_file } + +//**************************************************************************************** +// Type Helpers +//**************************************************************************************** +makeAType :: !Type !TypeAttribute -> !AType +makeAType type attr = { at_attribute = attr, at_type = type } + +makeATypeVar :: !TypeVar !TypeAttribute -> !ATypeVar +makeATypeVar tv attr = {atv_variable = tv, atv_attribute = attr} + +//---------------------------------------------------------------------------------------- +// folding of a AType, depth first +//---------------------------------------------------------------------------------------- + +class foldType t :: (Type .st -> .st) (AType .st -> .st) t .st -> .st + +instance foldType [a] | foldType a where + foldType on_type on_atype types st + = foldSt (foldType on_type on_atype) types st + +instance foldType (a,b) | foldType a & foldType b where + foldType on_type on_atype (x,y) st + = foldType on_type on_atype y (foldType on_type on_atype x st) + +instance foldType Type where + foldType on_type on_atype type st + # st = fold_type type st + = on_type type st + where + fold_type (TA type_symb args) st = foldType on_type on_atype args st + fold_type (TAS type_symb args _) st = foldType on_type on_atype args st + fold_type (l --> r) st = foldType on_type on_atype (l,r) st + fold_type (TArrow) st = st + fold_type (TArrow1 t) st = foldType on_type on_atype t st + fold_type (_ :@: args) st = foldType on_type on_atype args st + fold_type (TB _) st = st + fold_type (TFA tvs type) st = foldType on_type on_atype type st + fold_type (GTV _) st = st + fold_type (TV _) st = st + fold_type t st = abort "foldType: does not match\n" ---> ("type", t) + +instance foldType AType where + foldType on_type on_atype atype=:{at_type} st + # st = foldType on_type on_atype at_type st + = on_atype atype st + +instance foldType TypeContext where + foldType on_type on_atype {tc_types} st + = foldType on_type on_atype tc_types st + +//---------------------------------------------------------------------------------------- +// mapping of a AType, depth first +//---------------------------------------------------------------------------------------- +class mapTypeSt type :: + (Type .st -> (Type, .st)) // called on each type before recursion + (AType .st -> (AType, .st)) // called on each attributed type before recursion + (Type .st -> (Type, .st)) // called on each type after recursion + (AType .st -> (AType, .st)) // called on each attributed type after recursion + type .st -> (type, .st) + +mapTypeBeforeSt :: + (Type .st -> (Type, .st)) // called on each type before recursion + (AType .st -> (AType, .st)) // called on each attributed type before recursion + type .st -> (type, .st) | mapTypeSt type +mapTypeBeforeSt on_type_before on_atype_before type st + = mapTypeSt on_type_before on_atype_before idSt idSt type st + +mapTypeAfterSt :: + (Type .st -> (Type, .st)) // called on each type after recursion + (AType .st -> (AType, .st)) // called on each attributed type after recursion + type .st -> (type, .st) | mapTypeSt type +mapTypeAfterSt on_type_after on_atype_after type st + = mapTypeSt idSt idSt on_type_after on_atype_after type st + +instance mapTypeSt [a] | mapTypeSt a where + mapTypeSt on_type_before on_atype_before on_type_after on_atype_after type st + = mapSt (mapTypeSt on_type_before on_atype_before on_type_after on_atype_after) type st + +instance mapTypeSt (a, b) | mapTypeSt a & mapTypeSt b where + mapTypeSt on_type_before on_atype_before on_type_after on_atype_after (x, y) st + #! (x1, st) = mapTypeSt on_type_before on_atype_before on_type_after on_atype_after x st + #! (y1, st) = mapTypeSt on_type_before on_atype_before on_type_after on_atype_after y st + = ((x1,y1), st) + +instance mapTypeSt Type where + mapTypeSt on_type_before on_atype_before on_type_after on_atype_after type st + #! (type1, st) = on_type_before type st + #! (type2, st) = map_type type1 st + #! (type3, st) = on_type_after type2 st + = (type3, st) + //---> ("mapTypeSt Type", type, type1, type2, type3) + where + + map_type (TA type_symb_ident args) st + #! (args, st) = mapTypeSt on_type_before on_atype_before on_type_after on_atype_after args st + = (TA type_symb_ident args, st) + map_type (TAS type_symb_ident args strictness) st + #! (args, st) = mapTypeSt on_type_before on_atype_before on_type_after on_atype_after args st + = (TAS type_symb_ident args strictness, st) + map_type (l --> r) st + #! ((l,r), st) = mapTypeSt on_type_before on_atype_before on_type_after on_atype_after (l,r) st + = (l --> r, st) + map_type TArrow st = (TArrow, st) + map_type (TArrow1 t) st + #! (t, st) = mapTypeSt on_type_before on_atype_before on_type_after on_atype_after t st + = (TArrow1 t, st) + map_type (cv :@: args) st + #! (args, st) = mapTypeSt on_type_before on_atype_before on_type_after on_atype_after args st + = (cv :@: args, st) + map_type t=:(TB _) st = (t, st) + map_type (TFA tvs type) st + #! (type, st) = mapTypeSt on_type_before on_atype_before on_type_after on_atype_after type st + = (TFA tvs type, st) + map_type t=:(GTV _) st = (t, st) + map_type t=:(TV _) st = (t, st) + map_type t st + = abort "mapTypeSt: type does not match\n" ---> ("type", t) + +instance mapTypeSt AType where + mapTypeSt on_type_before on_atype_before on_type_after on_atype_after atype st + #! (atype, st) = on_atype_before atype st + #! (at_type, st) = mapTypeSt on_type_before on_atype_before on_type_after on_atype_after atype.at_type st + = on_atype_after {atype & at_type = at_type} st + +instance mapTypeSt TypeContext where + mapTypeSt on_type_before on_atype_before on_type_after on_atype_after tc=:{tc_types} st + #! (tc_types, st) = mapTypeSt on_type_before on_atype_before on_type_after on_atype_after tc_types st + = ({tc&tc_types=tc_types}, st) + + +//----------------------------------------------------------------------- +//----------------------------------------------------------------------- + +// allocate fresh type variable +freshTypeVar :: !Ident !*TypeVarHeap -> (!TypeVar, !*TypeVarHeap) +freshTypeVar name th_vars + # (info_ptr, th_vars) = newPtr TVI_Empty th_vars + = ({tv_name = name, tv_info_ptr = info_ptr}, th_vars) + +// allocate fresh attribute variable +freshAttrVar :: !Ident !*AttrVarHeap -> (!AttributeVar, !*AttrVarHeap) +freshAttrVar name th_attrs + # (info_ptr, th_attrs) = newPtr AVI_Empty th_attrs + = ({av_name = name, av_info_ptr = info_ptr}, th_attrs) + + +// take a fresh copy of a SymbolType +freshSymbolType :: + !SymbolType // symbol type to take fresh + !*TypeHeaps // variable storage + -> ( !SymbolType // fresh symbol type + , !*TypeHeaps // variable storage + ) +freshSymbolType st th=:{th_vars, th_attrs} + #! (fresh_st_vars, th_vars) = mapSt subst_type_var st.st_vars th_vars + //---> ("freshSymbolType called for", st) + #! (fresh_st_attr_vars, th_attrs) = mapSt subst_attr_var st.st_attr_vars th_attrs + #! th = {th & th_vars = th_vars, th_attrs = th_attrs} + + #! (fresh_st_args, th) = fresh_type st.st_args th + #! (fresh_st_result, th) = fresh_type st.st_result th + #! (fresh_st_context, th) = fresh_type st.st_context th + #! (fresh_st_attr_env, th) = mapSt fresh_ineq st.st_attr_env th + + #! fresh_st = + { st + & st_args = fresh_st_args + , st_result = fresh_st_result + , st_context = fresh_st_context + , st_attr_env = fresh_st_attr_env + , st_vars = fresh_st_vars + , st_attr_vars = fresh_st_attr_vars + } + + #! th = clearSymbolType fresh_st th + #! th = clearSymbolType st th + + #! th = assertSymbolType fresh_st th + #! th = assertSymbolType st th + + = (fresh_st, th) + //---> ("freshSymbolType returns", fresh_st) +where + subst_type_var :: !TypeVar !*TypeVarHeap -> (!TypeVar, !*TypeVarHeap) + subst_type_var tv=:{tv_info_ptr} th_vars + # (new_ptr, th_vars) = newPtr TVI_Empty th_vars + = ({tv & tv_info_ptr=new_ptr}, writePtr tv_info_ptr (TVI_TypeVar new_ptr) th_vars) + subst_attr_var :: !AttributeVar !*AttrVarHeap -> (!AttributeVar, !*AttrVarHeap) + subst_attr_var av=:{av_info_ptr} th_attrs + # (new_ptr, th_attrs) = newPtr AVI_Empty th_attrs + = ({av & av_info_ptr = new_ptr}, writePtr av_info_ptr (AVI_AttrVar new_ptr) th_attrs) + + fresh_type :: type !*TypeHeaps -> (type, !*TypeHeaps) | mapTypeSt type + fresh_type t st = mapTypeBeforeSt on_type on_atype t st + + on_type (TV tv) th + #! (tv, th) = on_type_var tv th + = (TV tv, th) + on_type (GTV tv) th + #! (tv, th) = on_type_var tv th + = (GTV tv, th) + on_type (CV tv=:{tv_info_ptr} :@: args) th=:{th_vars} + #! (tv, th) = on_type_var tv th + = (CV tv :@: args, th) + on_type (TFA atvs type) th + #! (fresh_atvs, th) = mapSt subst_atv atvs th + // the variables in the type will be substituted by + // the recursive call of mapType + = (TFA fresh_atvs type, th) + where + subst_atv atv=:{atv_variable, atv_attribute} th=:{th_vars, th_attrs} + #! (atv_variable, th_vars) = subst_type_var atv_variable th_vars + # (atv_attribute, th_attrs) = subst_attr atv_attribute th_attrs + = ( {atv & atv_variable = atv_variable, atv_attribute = atv_attribute} + , {th & th_vars = th_vars, th_attrs = th_attrs}) + subst_attr (TA_Var av=:{av_info_ptr}) th_attrs + # (av_info, th_attrs) = readPtr av_info_ptr th_attrs + = case av_info of + AVI_Empty + # (av, th_attrs) = subst_attr_var av th_attrs + -> (TA_Var av, th_attrs) + AVI_AttrVar av_info_ptr + -> (TA_Var {av & av_info_ptr = av_info_ptr}, th_attrs) + subst_attr TA_Unique th_attrs + = (TA_Unique, th_attrs) + subst_attr TA_Multi th_attrs + = (TA_Multi, th_attrs) + on_type type th + = (type, th) + + on_atype atype=:{at_attribute=TA_Var av} th + #! (fresh_av, th) = on_attr_var av th + = ({atype & at_attribute=TA_Var fresh_av}, th) + //---> ("on_atype av", av, fresh_av) + on_atype atype th + = (atype, th) + + fresh_ineq :: !AttrInequality !*TypeHeaps -> (!AttrInequality, !*TypeHeaps) + fresh_ineq ai=:{ai_demanded,ai_offered} th + #! (ai_demanded, th) = on_attr_var ai_demanded th + #! (ai_offered, th) = on_attr_var ai_offered th + = ({ai & ai_demanded = ai_demanded, ai_offered = ai_offered}, th) + + on_type_var tv=:{tv_info_ptr} th=:{th_vars} + #! (tv_info, th_vars) = readPtr tv_info_ptr th_vars + #! tv = case tv_info of + TVI_TypeVar new_ptr -> {tv & tv_info_ptr = new_ptr} + _ -> abort ("freshSymbolType, invalid tv_info\n" ---> tv_info) + = (tv, {th & th_vars = th_vars}) + + on_attr_var av=:{av_info_ptr} th=:{th_attrs} + #! (av_info, th_attrs) = readPtr av_info_ptr th_attrs + #! av = case av_info of + AVI_AttrVar new_ptr -> {av & av_info_ptr = new_ptr} + //---> ("fresh attr var", av.av_name, ptrToInt av_info_ptr, ptrToInt new_ptr) + _ -> abort ("freshSymbolType, invalid av_info\n" ---> av_info) + = ( av, {th & th_attrs = th_attrs}) + +assertSymbolType :: !SymbolType !*TypeHeaps -> !*TypeHeaps +assertSymbolType {st_args, st_result, st_context} th + = foldType on_type on_atype ((st_args, st_result), st_context) th +where + on_type :: !Type !*TypeHeaps -> !*TypeHeaps + on_type (TV tv) th=:{th_vars} + #! (tv_info, th_vars) = readPtr tv.tv_info_ptr th_vars + #! th = {th & th_vars = th_vars} + = case tv_info of + TVI_Empty -> th + _ -> (abort "TV tv_info not empty\n") --->(tv, tv_info) + on_type (CV tv :@: _) th=:{th_vars} + #! (tv_info, th_vars) = readPtr tv.tv_info_ptr th_vars + #! th = {th & th_vars = th_vars} + = case tv_info of + TVI_Empty -> th + _ -> (abort "CV tv_info not empty\n") --->(tv, tv_info) + on_type (TFA atvs type) th=:{th_attrs, th_vars} + #! th_attrs = foldSt on_av [av \\ {atv_attribute=TA_Var av} <- atvs] th_attrs + #! th_vars = foldSt on_tv [atv_variable\\{atv_variable} <- atvs] th_vars + = {th & th_attrs = th_attrs, th_vars = th_vars } + where + on_av av th_attrs + #! (av_info, th_attrs) = readPtr av.av_info_ptr th_attrs + = case av_info of + AVI_Empty -> th_attrs + _ -> (abort "TFA av_info not empty\n") --->(av, av_info) + on_tv tv th_vars + #! (tv_info, th_vars) = readPtr tv.tv_info_ptr th_vars + = case tv_info of + TVI_Empty -> th_vars + _ -> (abort "TFA tv_info not empty\n") --->(tv, tv_info) + on_type _ th = th + + on_atype :: !AType !*TypeHeaps -> !*TypeHeaps + on_atype {at_attribute=TA_Var av} th=:{th_attrs} + #! (av_info, th_attrs) = readPtr av.av_info_ptr th_attrs + #! th = {th & th_attrs = th_attrs} + = case av_info of + AVI_Empty -> th + _ -> (abort "av_info not empty\n") --->(av, av_info) + on_atype _ th = th + + +// build curried type out of SymbolType +buildCurriedType :: ![AType] !AType !TypeAttribute ![AttrInequality] ![AttributeVar] !String !Int !*AttrVarHeap + -> (!AType, ![AttrInequality], ![AttributeVar], !Int, !*AttrVarHeap) +buildCurriedType [] type cum_attr attr_env attr_vars attr_var_name attr_store th_attrs + = (type, attr_env, attr_vars, attr_store, th_attrs) +buildCurriedType [at=:{at_attribute}] type cum_attr attr_env attr_vars attr_var_name attr_store th_attrs + # atype = makeAType (at --> type) cum_attr + = (atype, attr_env, attr_vars, attr_store, th_attrs) +buildCurriedType [at=:{at_attribute}:ats] type cum_attr attr_env attr_vars attr_var_name attr_store th_attrs + # (next_cum_attr, new_attr_env, attr_vars, attr_store, th_attrs) = combine_attributes at_attribute cum_attr attr_env attr_vars attr_store th_attrs + (res_type, attr_env, attr_vars, attr_store, th_attrs) = buildCurriedType ats type next_cum_attr attr_env attr_vars attr_var_name attr_store th_attrs + # atype = makeAType (at --> res_type) cum_attr + = (atype, attr_env, attr_vars, attr_store, th_attrs) +where + combine_attributes TA_Unique cum_attr attr_env attr_vars attr_store th_attrs + = (TA_Unique, attr_env, attr_vars, attr_store, th_attrs) + combine_attributes (TA_Var attr_var) (TA_Var cum_attr_var) attr_env attr_vars attr_store th_attrs + #! (new_attr_var, th_attrs) + = freshAttrVar (makeIdent (attr_var_name +++ toString attr_store)) th_attrs + # attr_env = + [ { ai_demanded = cum_attr_var,ai_offered = new_attr_var } + , { ai_demanded = attr_var, ai_offered = new_attr_var } + : attr_env + ] + = ( TA_Var new_attr_var, attr_env, [new_attr_var:attr_vars], inc attr_store, th_attrs) + combine_attributes (TA_Var _) cum_attr attr_env attr_vars attr_store th_attrs + = (cum_attr, attr_env, attr_vars, attr_store, th_attrs) + combine_attributes _ (TA_Var cum_attr_var) attr_env attr_vars attr_store th_attrs + #! (new_attr_var, th_attrs) + = freshAttrVar (makeIdent (attr_var_name +++ toString attr_store)) th_attrs + # attr_env = [ { ai_demanded = cum_attr_var,ai_offered = new_attr_var }: attr_env] + = ( TA_Var new_attr_var, attr_env, [new_attr_var:attr_vars], inc attr_store, th_attrs) + combine_attributes _ cum_attr attr_env attr_vars attr_store th_attrs + = (cum_attr, attr_env, attr_vars, attr_store, th_attrs) + +// Build curried type out of symbol type. +// Starts with TA_Multi cumulative attribute. +// This is the weakest requirement, +// since we do not know how the generic argument will be used +// in the instance functions. It depends on the instance type. +curryGenericArgType :: !SymbolType !String !*TypeHeaps + -> (!SymbolType, !*TypeHeaps) +curryGenericArgType st=:{st_args, st_result, st_attr_env, st_attr_vars} attr_var_name th=:{th_attrs} + + #! (atype, attr_env, attr_vars, attr_store, th_attrs) + = buildCurriedType st_args st_result TA_Multi st_attr_env st_attr_vars attr_var_name 1 th_attrs + + # curried_st = + { st + & st_args = [] + , st_arity = 0 + , st_result = atype + , st_attr_env = attr_env + , st_attr_vars = attr_vars + } + = (curried_st, {th & th_attrs = th_attrs}) + //---> ("curryGenericArgType", st, curried_st) + + +curryGenericArgType1 :: !SymbolType !String !*TypeHeaps + -> (!SymbolType, !*TypeHeaps) +curryGenericArgType1 st=:{st_args, st_result, st_attr_env, st_attr_vars} attr_var_name th=:{th_attrs} + + # (atype, attr_vars, av_num, th_attrs) = curry st_args st_result 1 th_attrs + + # curried_st = + { st + & st_args = [] + , st_arity = 0 + , st_result = atype + , st_attr_vars = attr_vars + } + = (curried_st, {th & th_attrs = th_attrs}) + //---> ("curryGenericArgType", st, curried_st) +where + // outermost closure gets TA_Multi attribute + curry [] res av_num th_attrs + = (res, [], av_num, th_attrs) + curry [arg:args] res av_num th_attrs + #! (res, avs, av_num, th_attrs) = curry1 args res av_num th_attrs + #! atype = makeAType (arg --> res) TA_Multi + = (atype, avs, av_num, th_attrs) + + // inner closures get TA_Var attributes + curry1 [] res av_num th_attrs + = (res, [], av_num, th_attrs) + curry1 [arg:args] res av_num th_attrs + #! (res, avs, av_num, th_attrs) = curry1 args res av_num th_attrs + #! (av, th_attrs) = freshAttrVar (makeIdent (attr_var_name +++ toString av_num)) th_attrs + #! atype = makeAType (arg --> res) (TA_Var av) + = (atype, [av:avs], inc av_num, th_attrs) + +//---------------------------------------------------------------------------------------- +// write empty value in the variable heaps +//---------------------------------------------------------------------------------------- + +clearType t th + = foldType clear_type clear_atype t th +where + + clear_type (TV tv) th = clear_type_var tv th + clear_type (GTV tv) th = clear_type_var tv th + clear_type (CV tv :@: _) th = clear_type_var tv th + clear_type (TFA atvs type) th + #! th = foldSt clear_attr [atv_attribute \\ {atv_attribute} <- atvs] th + #! th = foldSt clear_type_var [atv_variable \\ {atv_variable} <- atvs] th + = th + + clear_type _ th = th + + clear_atype {at_attribute} th + = clear_attr at_attribute th + + clear_attr (TA_Var av) th = clear_attr_var av th + clear_attr (TA_RootVar av) th = clear_attr_var av th + clear_attr _ th = th + + clear_type_var {tv_info_ptr} th=:{th_vars} + = {th & th_vars = writePtr tv_info_ptr TVI_Empty th_vars} + clear_attr_var {av_info_ptr} th=:{th_attrs} + = {th & th_attrs = writePtr av_info_ptr AVI_Empty th_attrs} + +clearSymbolType st th + // clears not only st_vars and st_attrs, but also TFA variables + = clearType ((st.st_result, st.st_args), st.st_context) th + +//---------------------------------------------------------------------------------------- +// collect variables +//---------------------------------------------------------------------------------------- + +collectTypeVarsAndAttrVars :: + !type + !*TypeHeaps + -> (![TypeVar] + ,![AttributeVar] + ,!*TypeHeaps + ) + | foldType type +collectTypeVarsAndAttrVars type th + #! th = clearType type th + #! (tvs, avs, th) = foldType collect_type_var collect_attr type ([], [], th) + #! th = clearType type th + = (tvs, avs, th) +where + collect_type_var (TV tv) st = add_type_var tv st + collect_type_var (GTV tv) st = add_type_var tv st + collect_type_var (CV tv :@: _) st = add_type_var tv st + collect_type_var (TFA forall_atvs type) (tvs, avs, th_vars) + #! forall_tvs = [atv_variable\\{atv_variable}<-forall_atvs] + #! forall_avs = [av \\ {atv_attribute=TA_Var av}<-forall_atvs] + = (tvs -- forall_tvs, avs -- forall_avs, th_vars) + //---> ("collectTypeVarsAndAttrVars TFA", tvs, forall_tvs, tvs -- forall_tvs) + collect_type_var t st = st + + add_type_var tv (tvs, avs, th=:{th_vars}) + # (was_used, th_vars) = markTypeVarUsed tv th_vars + # th = {th & th_vars = th_vars} + | was_used + = (tvs, avs, th) + //---> ("collectTypeVarsAndAttrVars: TV was used", tv) + = ([tv:tvs], avs, th) + //---> ("collectTypeVarsAndAttrVars: TV was not used", tv) + + collect_attr {at_attribute} st = collect_attr_var at_attribute st + + collect_attr_var (TA_Var av) st = add_attr_var av st + collect_attr_var (TA_RootVar av) st = add_attr_var av st + collect_attr_var _ st = st + + add_attr_var av (atvs, avs, th=:{th_attrs}) + # (was_used, th_attrs) = markAttrVarUsed av th_attrs + # th = {th & th_attrs = th_attrs} + | was_used + = (atvs, avs, th) + = (atvs, [av:avs], th) + +collectTypeVars type th + # (tvs, _, th) = collectTypeVarsAndAttrVars type th + = (tvs, th) +collectAttrVars type th + # (_, avs, th) = collectTypeVarsAndAttrVars type th + = (avs, th) + +collectAttrsOfTypeVars :: ![TypeVar] type !*TypeHeaps -> (![ATypeVar], !*TypeHeaps) | foldType type +collectAttrsOfTypeVars tvs type th + #! (th=:{th_vars}) = clearType type th + //---> ("collectAttrsOfTypeVars called for", tvs) + + # th_vars = foldSt (\{tv_info_ptr} h->writePtr tv_info_ptr TVI_Used h) tvs th_vars + + #! (atvs, th_vars) = foldType on_type on_atype type ([], th_vars) + + # th_vars = foldSt (\{tv_info_ptr} h->writePtr tv_info_ptr TVI_Empty h) tvs th_vars + + #! th = clearType type {th & th_vars= th_vars} + = (atvs, th) + //---> ("collectAttrsOfTypeVars returns", atvs) +where + on_type type st = st + + on_atype {at_type=TV tv, at_attribute} st = on_type_var tv at_attribute st + on_atype {at_type=GTV tv, at_attribute} st = on_type_var tv at_attribute st + on_atype {at_type=(CV tv :@: _), at_attribute} st = on_type_var tv at_attribute st + //??? TFA -- seems that it is not needed + on_atype _ st = st + + on_type_var tv=:{tv_info_ptr} attr (atvs, th_vars) + #! (tvi, th_vars) = readPtr tv_info_ptr th_vars + = case tvi of + TVI_Used + # th_vars = writePtr tv_info_ptr TVI_Empty th_vars + -> ([makeATypeVar tv attr : atvs], th_vars) + TVI_Empty + -> (atvs, th_vars) + +collectAttrsOfTypeVarsInSymbolType tvs {st_args, st_result} th + = collectAttrsOfTypeVars tvs [st_result:st_args] th + +// marks empty type vars used, +// returns whether the type var was already used +markTypeVarUsed tv=:{tv_info_ptr} th_vars + # (tv_info, th_vars) = readPtr tv_info_ptr th_vars + = case tv_info of + TVI_Empty -> (False, writePtr tv_info_ptr TVI_Used th_vars) + TVI_Used -> (True, th_vars) + _ -> (abort "markTypeVarUsed: wrong tv_info ") ---> (tv, tv_info) + +// marks empty attr vars used +// returns whether the attr var was already used +markAttrVarUsed {av_info_ptr} th_attrs + # (av_info, th_attrs) = readPtr av_info_ptr th_attrs + = case av_info of + AVI_Empty -> (False, writePtr av_info_ptr AVI_Used th_attrs) + AVI_Used -> (True, th_attrs) + + +simplifyTypeApp :: !Type ![AType] -> !Type +simplifyTypeApp (TA type_cons=:{type_arity} cons_args) type_args + = TA { type_cons & type_arity = type_arity + length type_args } (cons_args ++ type_args) +simplifyTypeApp (TAS type_cons=:{type_arity} cons_args strictness) type_args + = TAS { type_cons & type_arity = type_arity + length type_args } (cons_args ++ type_args) strictness +simplifyTypeApp (CV tv :@: type_args1) type_args2 = CV tv :@: (type_args1 ++ type_args2) +simplifyTypeApp TArrow [type1, type2] = type1 --> type2 +simplifyTypeApp TArrow [type] = TArrow1 type +simplifyTypeApp (TArrow1 type1) [type2] = type1 --> type2 +simplifyTypeApp (TV tv) type_args = CV tv :@: type_args +simplifyTypeApp (TB _) type_args = TE +simplifyTypeApp (TArrow1 _) type_args = TE + +//---------------------------------------------------------------------------------------- +// substitutions +//---------------------------------------------------------------------------------------- + +// +// Uninitialized variables are not substituted, but left intact +// +// This behaviour is needed for kind indexing generic types, +// where generic variables are substituted and non-generic variables +// are not +// +applySubst :: !type !*TypeHeaps -> (!type, !*TypeHeaps) | mapTypeSt type +applySubst type th + = mapTypeAfterSt on_type on_atype type th +where + on_type type=:(TV {tv_info_ptr}) th=:{th_vars} + # (tv_info, th_vars) = readPtr tv_info_ptr th_vars + # th = {th & th_vars = th_vars} + = case tv_info of + TVI_Type t -> (t, th) + TVI_Empty -> (type, th) + on_type (GTV _) th + = abort "GTV" + on_type type=:(CV {tv_info_ptr} :@: args) th=:{th_vars} + # (tv_info, th_vars) = readPtr tv_info_ptr th_vars + # th = {th & th_vars = th_vars} + = case tv_info of + TVI_Type t -> (simplifyTypeApp t args, th) + TVI_Empty -> (type, th) + + //on_type type=:(TFA atvs t) th=:{th_vars} + // = abort "applySubst TFA" + + on_type type th + = (type, th) + + on_atype atype=:{at_attribute} th=:{th_attrs} + # (at_attribute, th_attrs) = subst_attr at_attribute th_attrs + = ({atype & at_attribute = at_attribute}, {th & th_attrs = th_attrs}) + + subst_attr attr=:(TA_Var {av_info_ptr}) th_attrs + # (av_info, th_attrs) = readPtr av_info_ptr th_attrs + = case av_info of + AVI_Attr a -> (a, th_attrs) + AVI_Empty -> (attr, th_attrs) + subst_attr TA_Multi th = (TA_Multi, th) + subst_attr TA_Unique th = (TA_Unique, th) + +applySubstInSymbolType st=:{st_args, st_result, st_attr_env, st_context} th + #! (new_st_args, th) = applySubst st.st_args th + #! (new_st_result, th) = applySubst st.st_result th + #! (new_st_context, th) = applySubst st.st_context th + #! (new_st_attr_env, th) = mapSt subst_ineq st.st_attr_env th + + #! th = clear_type_vars st.st_vars th + #! th = clear_attr_vars st.st_attr_vars th + + #! (new_st_vars, new_st_attr_vars, th) + = collectTypeVarsAndAttrVars ((new_st_args,new_st_result), new_st_context) th + + #! new_st = + { st + & st_args = new_st_args + , st_result = new_st_result + , st_context = new_st_context + , st_attr_env = new_st_attr_env + , st_vars = new_st_vars + , st_attr_vars = new_st_attr_vars + } + + #! th = clearSymbolType st th + + #! th = assertSymbolType new_st th + #! th = assertSymbolType st th + + = (new_st, th) + //---> ("applySubstInSymbolType", new_st) +where + subst_ineq ai=:{ai_demanded,ai_offered} th + # (ai_demanded, th) = subst_attr_var ai_demanded th + # (ai_offered, th) = subst_attr_var ai_offered th + = ({ai & ai_demanded = ai_demanded, ai_offered = ai_offered}, th) + subst_attr_var av=:{av_info_ptr} th=:{th_attrs} + # (av_info, th_attrs) = readPtr av_info_ptr th_attrs + # th = {th & th_attrs = th_attrs} + = case av_info of + AVI_Attr (TA_Var av1) -> (av1, th) + AVI_Attr _ -> (av, th) + AVI_Empty -> (av, th) + clear_type_vars tvs th=:{th_vars} + #! th_vars = foldSt (\{tv_info_ptr} h->writePtr tv_info_ptr TVI_Empty h) tvs th_vars + = {th & th_vars = th_vars} + clear_attr_vars avs th=:{th_attrs} + #! th_attrs = foldSt (\{av_info_ptr} h->writePtr av_info_ptr AVI_Empty h) avs th_attrs + = {th & th_attrs = th_attrs} + +//**************************************************************************************** +// Function Helpers +//**************************************************************************************** + +makeFunction :: !Ident !Index !Index ![FreeVar] !Expression !(Optional SymbolType) !Index !Position + -> FunDef +makeFunction ident fun_index group_index arg_vars body_expr opt_sym_type main_dcl_module_n fun_pos + + #! (arg_vars, local_vars, free_vars) = collectVars body_expr arg_vars + | not (isEmpty free_vars) + = abort "makeFunction: free_vars is not empty\n" + + = { fun_symb = ident + , fun_arity = length arg_vars + , fun_priority = NoPrio + , fun_body = TransformedBody {tb_args = arg_vars, tb_rhs = body_expr } + , fun_type = opt_sym_type + , fun_pos = fun_pos + , fun_kind = FK_Function cNameNotLocationDependent + , fun_lifted = 0 + , fun_info = + { fi_calls = collectCalls main_dcl_module_n body_expr + , fi_group_index = group_index + , fi_def_level = NotALevel + , fi_free_vars = [] + , fi_local_vars = local_vars + , fi_dynamics = [] + , fi_properties = 0 + } + } + //---> ("makeFunction", ident, fun_index) + +// build function and +buildFunAndGroup :: + !Ident ![FreeVar] !Expression !(Optional SymbolType) !Index !Position + !FunsAndGroups + -> + (!DefinedSymbol, FunsAndGroups) +buildFunAndGroup + ident arg_vars body_expr opt_sym_type main_dcl_module_n fun_pos + (fun_index, group_index, funs, groups) + # fun = makeFunction ident fun_index group_index arg_vars body_expr opt_sym_type main_dcl_module_n fun_pos + # group = {group_members = [fun_index]} + # def_sym = {ds_ident=ident, ds_arity=fun.fun_arity, ds_index=fun_index} + = (def_sym, (inc fun_index, inc group_index, [fun:funs], [group:groups])) + +buildUndefFunAndGroup ident st main_dcl_module_n fun_pos fun_info predefs heaps + #! arg_var_names = [ "x" +++ toString i \\ i <- [1 .. st.st_arity]] + #! (arg_vars,heaps) = mapSt build_free_var arg_var_names heaps + #! (expr, heaps) = buildPredefFunApp PD_undef [] predefs heaps + = buildFunAndGroup ident arg_vars expr (Yes st) main_dcl_module_n fun_pos fun_info +where + build_free_var :: !String !*Heaps -> (!FreeVar, !*Heaps) + build_free_var name heaps=:{hp_var_heap} + # (var_info_ptr, hp_var_heap) = newPtr VI_Empty hp_var_heap + # var_name = { id_name = name, id_info = nilPtr } + # free_var = { fv_def_level = NotALevel, fv_count = 0, fv_info_ptr = var_info_ptr, fv_name = var_name} + = (free_var, {heaps & hp_var_heap = hp_var_heap}) + +/* +buildIdFunction :: + !DefinedSymbol // the desired function name and index + Int // group index + !Index // current module number + !*Heaps // heaps + -> ( !FunDef // created function definition + , !*Heaps // heaps + ) +buildIdFunction def_sym group_index gs_main_dcl_module_n heaps + # (arg_expr, arg_var, heaps) = buildVarExpr "x" heaps + # fun_def = makeFunction def_sym group_index [arg_var] arg_expr No [] gs_main_dcl_module_n NoPos + = (fun_def, heaps) +*/ + +/* +buildUndefFunction :: + !DefinedSymbol // the desired function name and index + !Int // group index + !PredefinedSymbols // predefined symbols + !Index // current module number + !*Heaps // heaps + -> ( !FunDef // created function definition + , !*Heaps // heaps + ) +buildUndefFunction def_sym group_index predefs gs_main_dcl_module_n heaps + # names = [ "x" +++ toString i \\ i <- [1 .. def_sym.ds_arity]] + # (arg_vars, heaps) = mapSt build_free_var names heaps + # (body_expr, heaps) = buildUndefFunApp [] predefs heaps + //# (body_expr, heaps) = buildUNIT predefs heaps + # fun_def = makeFunction def_sym group_index arg_vars body_expr No [] gs_main_dcl_module_n NoPos + = (fun_def, heaps) +where + build_free_var :: !String !*Heaps -> (!FreeVar, !*Heaps) + build_free_var name heaps=:{hp_var_heap} + # (var_info_ptr, hp_var_heap) = newPtr VI_Empty hp_var_heap + # var_name = { id_name = name, id_info = nilPtr } + # free_var = { fv_def_level = NotALevel, fv_count = 0, fv_info_ptr = var_info_ptr, fv_name = var_name} + = (free_var, {heaps & hp_var_heap = hp_var_heap}) +*/ + +//**************************************************************************************** +// Expr Helpers +//**************************************************************************************** + +//======================================================================================== +// Primitive expressions +//======================================================================================== + +makeIntExpr :: Int -> Expression +makeIntExpr value = BasicExpr (BVI (toString value)) + +makeStringExpr :: String !PredefinedSymbols -> Expression +makeStringExpr str predefs + #! {pds_module, pds_def} = predefs.[PD_StringType] + #! pds_ident = predefined_idents.[PD_StringType] + #! type_symb = MakeTypeSymbIdent { glob_module = pds_module, glob_object = pds_def } pds_ident 0 + = BasicExpr (BVS str) + +/* +makeListExpr :: [Expression] !PredefinedSymbols !*Heaps -> (Expression, !*Heaps) +makeListExpr [] predefs heaps + = buildPredefConsApp PD_NilSymbol [] predefs heaps +makeListExpr [expr:exprs] predefs heaps + # (list_expr, heaps) = makeListExpr exprs predefs heaps + = buildPredefConsApp PD_ConsSymbol [expr, list_expr] predefs heaps +*/ + +buildConsApp :: !Index DefinedSymbol ![Expression] !*Heaps + -> (!Expression, !*Heaps) +buildConsApp cons_mod {ds_ident, ds_index, ds_arity} arg_exprs heaps=:{hp_expression_heap} + # (expr_info_ptr, hp_expression_heap) = newPtr EI_Empty hp_expression_heap + # cons_glob = {glob_module = cons_mod, glob_object = ds_index} + # expr = App { + app_symb = { + symb_name = ds_ident, + symb_kind = SK_Constructor cons_glob + }, + app_args = arg_exprs, + app_info_ptr = expr_info_ptr} + # heaps = { heaps & hp_expression_heap = hp_expression_heap } + = (expr, heaps) + +buildFunApp :: !Index !DefinedSymbol ![Expression] !*Heaps + -> (!Expression, !*Heaps) +buildFunApp fun_mod {ds_ident, ds_index} arg_exprs heaps=:{hp_expression_heap} + # (expr_info_ptr, hp_expression_heap) = newPtr EI_Empty hp_expression_heap + # fun_glob = {glob_module = fun_mod, glob_object = ds_index} + # expr = App { + app_symb = { + symb_name = ds_ident, + symb_kind = SK_Function fun_glob + }, + app_args = arg_exprs, + app_info_ptr = expr_info_ptr} + # heaps = { heaps & hp_expression_heap = hp_expression_heap } + = (expr, heaps) + +buildPredefFunApp :: !Int [Expression] !PredefinedSymbols !*Heaps + -> (!Expression, !*Heaps) +buildPredefFunApp predef_index args predefs heaps + # {pds_module, pds_def} = predefs.[predef_index] + # fun_ds = + { ds_index = pds_def + , ds_ident = predefined_idents.[predef_index] + , ds_arity = 0 // not used + } + = buildFunApp pds_module fun_ds args heaps + +buildGenericApp :: !Index !Index !Ident !TypeKind ![Expression] !*Heaps + -> (!Expression, !*Heaps) +buildGenericApp gen_module gen_index gen_name kind arg_exprs heaps=:{hp_expression_heap} + # (expr_info_ptr, hp_expression_heap) = newPtr EI_Empty hp_expression_heap + # glob_index = {glob_module = gen_module, glob_object = gen_index} + # expr = App { + app_symb = { + symb_name = gen_name, + symb_kind = SK_Generic glob_index kind + }, + app_args = arg_exprs, + app_info_ptr = expr_info_ptr} + # heaps = { heaps & hp_expression_heap = hp_expression_heap } + = (expr, heaps) + +buildPredefConsApp :: !Int [Expression] !PredefinedSymbols !*Heaps + -> (!Expression, !*Heaps) +buildPredefConsApp predef_index args predefs heaps=:{hp_expression_heap} + # {pds_module, pds_def} = predefs.[predef_index] + # pds_ident = predefined_idents.[predef_index] + # global_index = {glob_module = pds_module, glob_object = pds_def} + # symb_ident = + { symb_name = pds_ident + , symb_kind = SK_Constructor global_index + } + # (expr_info_ptr, hp_expression_heap) = newPtr EI_Empty hp_expression_heap + # app = App {app_symb = symb_ident, app_args = args, app_info_ptr = expr_info_ptr} + = (app, {heaps & hp_expression_heap = hp_expression_heap}) + +buildPredefConsPattern :: !Int ![FreeVar] !Expression !PredefinedSymbols + -> AlgebraicPattern +buildPredefConsPattern predef_index vars expr predefs + # {pds_module, pds_def} = predefs.[predef_index] + # pds_ident = predefined_idents.[predef_index] + # cons_def_symbol = { + ds_ident = pds_ident, + ds_arity = length vars, + ds_index = pds_def + } + # pattern = { + ap_symbol = {glob_module = pds_module, glob_object = cons_def_symbol}, + ap_vars = vars, + ap_expr = expr, + ap_position = NoPos + } + = pattern + +buildCaseExpr :: Expression CasePatterns !*Heaps + -> (!Expression, !*Heaps) +buildCaseExpr case_arg case_alts heaps=:{hp_expression_heap} + # (expr_info_ptr, hp_expression_heap) = newPtr EI_Empty hp_expression_heap + # expr = Case + { case_expr = case_arg + , case_guards = case_alts + , case_default = No + , case_ident = No + , case_info_ptr = expr_info_ptr + , case_explicit = False + , case_default_pos = NoPos + } + # heaps = { heaps & hp_expression_heap = hp_expression_heap} + = (expr, heaps) + +buildRecordSelectionExpr :: !Expression !Index !PredefinedSymbols -> Expression +buildRecordSelectionExpr record_expr predef_field predefs + # {pds_module, pds_def} = predefs . [predef_field] + # pds_ident = predefined_idents . [predef_field] + # selector = { + glob_module = pds_module, + glob_object = {ds_ident = pds_ident, ds_index = pds_def, ds_arity = 1}} + = Selection NormalSelector record_expr [RecordSelection selector 1] + +//============================================================================= +// variables +//============================================================================= + +// build a new variable and an expression associated with it +buildVarExpr :: + !String // variable name + !*Heaps + -> (!Expression // variable expression + , !FreeVar // variable + , !*Heaps + ) +buildVarExpr name heaps=:{hp_var_heap, hp_expression_heap} + # (expr_info_ptr, hp_expression_heap) = newPtr EI_Empty hp_expression_heap + # (var_info_ptr, hp_var_heap) = newPtr VI_Empty hp_var_heap + # var_name = makeIdent name + # var = Var {var_name = var_name, var_expr_ptr = expr_info_ptr, var_info_ptr = var_info_ptr } + # hp_var_heap = writePtr var_info_ptr (VI_Expression var) hp_var_heap + # heaps = { heaps & hp_var_heap = hp_var_heap, hp_expression_heap = hp_expression_heap } + # fv = {fv_count = 1/* if 0, trans crashes*/, fv_name = var_name, fv_info_ptr = var_info_ptr, fv_def_level = NotALevel} + = (var, fv, heaps) + +buildVarExprs [] heaps = ([], [], heaps) +buildVarExprs [x:xs] heaps + # (y, z, heaps) = buildVarExpr x heaps + # (ys, zs, heaps) = buildVarExprs xs heaps + = ([y:ys], [z:zs], heaps) + +//============================================================================= +// recursion over expressions +//============================================================================= + +//----------------------------------------------------------------------------- +// fold expression applies a function to each node of an expression +// recursively: +// first apply the function, then recurse +//----------------------------------------------------------------------------- +foldExpr :: + (Expression -> .st -> .st) // function to apply at each node + Expression // expression to run throuh + .st // state + -> + .st // updated state +foldExpr f expr=:(App {app_args}) st + # st = f expr st + = foldSt (foldExpr f) app_args st +foldExpr f expr1=:(expr@exprs) st + # st = f expr st + = foldSt (foldExpr f) [expr:exprs] st +foldExpr f expr=:(Let {let_lazy_binds, let_strict_binds, let_expr}) st + # st = f expr st + # st = foldSt (fold_let_binds f) let_strict_binds st + # st = foldSt (fold_let_binds f) let_lazy_binds st + = foldExpr f let_expr st +where + fold_let_binds f {lb_src} st = foldExpr f lb_src st +foldExpr f expr=:(Case {case_expr,case_guards,case_default}) st + # st = f expr st + # st = foldExpr f case_expr st + # st = fold_guards f case_guards st + # st = foldOptional (foldExpr f) case_default st + = st +where + fold_guards f (AlgebraicPatterns gi aps) st = foldSt (foldExpr f) [ap_expr\\{ap_expr}<-aps] st + fold_guards f (BasicPatterns gi bps) st = foldSt (foldExpr f) [bp_expr\\{bp_expr}<-bps] st + fold_guards f (DynamicPatterns dps) st = foldSt (foldExpr f) [dp_rhs\\{dp_rhs}<-dps] st + fold_guards f NoPattern st = st +foldExpr f expr=:(Update expr1 sels expr2) st + # st = f expr st + # st = foldExpr f expr1 st + # st = foldSt (fold_sel f) sels st + # st = foldExpr f expr2 st + = st +where + fold_sel f (RecordSelection _ _) st = st + fold_sel f (ArraySelection _ _ expr) st = foldExpr f expr st + fold_sel f (DictionarySelection _ _ _ expr) st = foldExpr f expr st +foldExpr f expr=:(RecordUpdate _ expr1 binds) st + # st = f expr st + # st = foldExpr f expr1 st + # st = foldSt (foldExpr f) [bind_src\\{bind_src}<-binds] st + = st +foldExpr f expr=:(TupleSelect _ _ expr1) st + # st = f expr st + = foldExpr f expr1 st +foldExpr f expr=:(Conditional {if_cond,if_then,if_else}) st + # st = f expr st + # st = foldExpr f if_cond st + # st = foldExpr f if_then st + # st = foldOptional (foldExpr f) if_else st + = st +foldExpr f expr=:(MatchExpr _ expr1) st + # st = f expr st + = foldExpr f expr1 st +foldExpr f expr=:(DynamicExpr {dyn_expr}) st + # st = f expr st + = foldExpr f dyn_expr st +foldExpr f expr st + = f expr st + +//----------------------------------------------------------------------------- +// map expression applies a function to each node of an expression +// recursively: +// first recurse, then apply the function +//----------------------------------------------------------------------------- +mapExprSt :: + !(Expression -> w:st -> u:(Expression, w:st)) + !Expression + w:st + -> + v: ( Expression + , w:st + ) + , [v<=w,u<=v] +mapExprSt f (App app=:{app_args}) st + # (app_args, st) = mapSt (mapExprSt f) app_args st + = f (App { app & app_args = app_args }) st + +mapExprSt f (Let lad=:{let_lazy_binds, let_strict_binds, let_expr}) st + # (let_lazy_binds, st) = mapSt map_bind let_lazy_binds st + # (let_strict_binds, st) = mapSt map_bind let_strict_binds st + # (let_expr, st) = mapExprSt f let_expr st + # lad = + { lad + & let_expr = let_expr + , let_lazy_binds = let_lazy_binds + , let_strict_binds = let_strict_binds + } + = f (Let lad) st +where + map_bind b=:{lb_src} st + # (lb_src, st) = mapExprSt f lb_src st + = ({b & lb_src = lb_src}, st) + +mapExprSt f (Selection a expr b) st + # (expr, st) = mapExprSt f expr st + = f (Selection a expr b) st + +mapExprSt f (Update e1 x e2) st + # (e1, st) = mapExprSt f e1 st + # (e2, st) = mapExprSt f e2 st + = f (Update e1 x e2) st + +mapExprSt f (RecordUpdate x expr binds) st + # (expr, st) = mapExprSt f expr st + # (binds, st) = mapSt map_bind binds st + = f (RecordUpdate x expr binds) st +where + map_bind b=:{bind_src} st + # (bind_dst, st) = mapExprSt f bind_src st + = ({b & bind_src = bind_src}, st) + +mapExprSt f (TupleSelect x y expr) st + # (expr, st) = mapExprSt f expr st + = f (TupleSelect x y expr) st + +mapExprSt f (Conditional cond=:{if_cond, if_then, if_else}) st + # (if_cond, st) = mapExprSt f if_cond st + # (if_then, st) = mapExprSt f if_then st + # (if_else, st) = mapOptionalSt (mapExprSt f) if_else st +/* + # (if_else, st) = case if_else of + (Yes x) + # (x, st) = mapExprSt f x st + -> (Yes x, st) + No -> (No, st) +*/ + = f (Conditional {cond & if_cond = if_cond, if_then = if_then, if_else = if_else}) st + +mapExprSt f (MatchExpr y expr) st + # (expr, st) = mapExprSt f expr st + = f (MatchExpr y expr) st + +mapExprSt f (DynamicExpr dyn=:{dyn_expr}) st + # (dyn_expr, st) = mapExprSt f dyn_expr st + = f (DynamicExpr {dyn& dyn_expr = dyn_expr}) st + +mapExprSt f (Case c=:{case_expr, case_guards, case_default=case_default}) st + # (case_expr, st) = mapExprSt f case_expr st + # (case_guards, st) = map_patterns case_guards st + # (case_default, st) = case case_default of + (Yes x) + # (x, st) = mapExprSt f x st + -> (Yes x, st) + No -> (No, st) + # new_case = {c & case_expr=case_expr, case_guards=case_guards, case_default=case_default} + = f (Case new_case) st +where + map_patterns (AlgebraicPatterns index pats) st + # (pats, st) = mapSt map_alg_pattern pats st + = (AlgebraicPatterns index pats, st) + map_patterns (BasicPatterns bt pats) st + # (pats, st) = mapSt map_basic_pattern pats st + = (BasicPatterns bt pats, st) + map_patterns (DynamicPatterns pats) st + # (pats, st) = mapSt map_dyn_pattern pats st + = (DynamicPatterns pats, st) + + map_alg_pattern pat=:{ap_expr} st + # (ap_expr, st) = mapExprSt f ap_expr st + = ({pat & ap_expr = ap_expr}, st) + map_basic_pattern pat=:{bp_expr} st + # (bp_expr, st) = mapExprSt f bp_expr st + = ({pat & bp_expr = bp_expr}, st) + map_dyn_pattern pat=:{dp_rhs} st + # (dp_rhs, st) = mapExprSt f dp_rhs st + = ({pat & dp_rhs = dp_rhs}, st) + +mapExprSt f expr st = f expr st + +// needed for collectCalls +instance == FunCall where (==) (FunCall x _) (FunCall y _) = x == y + +// collect function calls made in the expression +collectCalls :: !Index !Expression -> [FunCall] +collectCalls current_module expr = removeDup (foldExpr get_call expr []) +where + get_call (App {app_symb={symb_kind=SK_Function {glob_module,glob_object}}}) indexes + | glob_module == current_module + = [FunCall glob_object NotALevel : indexes] + = indexes + get_call _ indexes = indexes + +// collects variables and computes the refernce counts +collectVars :: + !Expression // expression to collect variables in + ![FreeVar] // function argument variables + -> ( ![FreeVar] // argument variables (with updated ref count) + , ![FreeVar] // local variables + , ![FreeVar] // free_variables + ) +collectVars expr arg_vars + # arg_vars = [ {v & fv_count = 0} \\ v <- arg_vars] + = foldExpr collect_vars expr (arg_vars, [], []) +where + collect_vars (Var {var_name, var_info_ptr}) (arg_vars, local_vars, free_vars) + # var = {fv_name = var_name, fv_count = 1, fv_info_ptr = var_info_ptr, fv_def_level = NotALevel} + # (added, arg_vars) = add_var var arg_vars + | added + = (arg_vars, local_vars, free_vars) + # (added, local_vars) = add_var var local_vars + | added + = (arg_vars, local_vars, free_vars) + # (added, free_vars) = add_var var free_vars + | added + = (arg_vars, local_vars, free_vars) + = (arg_vars, local_vars, [var:free_vars]) + where + add_var var [] = (False, []) + add_var var [v=:{fv_count,fv_info_ptr}:vs] + | var.fv_info_ptr == fv_info_ptr + = (True, [{v&fv_count = inc fv_count}:vs]) + # (added, vs) = add_var var vs + = (added, [v:vs]) + collect_vars (Let {let_lazy_binds, let_strict_binds}) (arg_vars, local_vars, free_vars) + # vars = [{lb_dst&fv_count=0} \\ {lb_dst} <- (let_lazy_binds ++ let_strict_binds)] + # (local_vars, free_vars) = foldSt add_local_var vars (local_vars, free_vars) + = (arg_vars, local_vars, free_vars) + collect_vars (Case {case_guards}) (arg_vars, local_vars, free_vars) + # vars = [{v&fv_count=0} \\ v <- collect case_guards] + # (local_vars, free_vars) = foldSt add_local_var vars (local_vars, free_vars) + = (arg_vars, local_vars, free_vars) + where + collect (AlgebraicPatterns _ aps) = flatten [ap_vars\\{ap_vars}<-aps] + collect (BasicPatterns _ bps) = [] + collect (DynamicPatterns dps) = [dp_var \\ {dp_var}<-dps] + collect NoPattern = [] + collect_vars expr st = st + + add_local_var var (local_vars, []) = ([var:local_vars], []) + add_local_var var (local_vars, free_vars=:[fv:fvs]) + | var.fv_info_ptr == fv.fv_info_ptr + = ([fv:local_vars], fvs) + # (local_vars, fvs1) = add_local_var var (local_vars, fvs) + = (local_vars, [fv:fvs1]) + +//**************************************************************************************** +// Array helpers +//**************************************************************************************** + +//updateArray :: (Int a -> a) *{a} -> *{a} +updateArray f xs + = map_array 0 xs +where + map_array n xs + #! (s, xs) = usize xs + | n == s + = xs + # (x, xs) = xs ! [n] + = map_array (inc n) {xs & [n] = f n x} + +//updateArray1 :: (Int .a -> .a) *{.a} .a -> *{.a} +updateArray1 f xs dummy + # (xs, _) = map_array 0 xs dummy + = xs +where + map_array n xs d + #! (s, xs) = usize xs + | n == s + = (xs, d) + # (x, xs) = replace xs n d + # x = f n x + # (d, xs) = replace xs n x + = map_array (inc n) xs d + +update2dArray f xss + = updateArray1 (\n xs -> updateArray (f n) xs) xss {} + + +//updateArraySt :: (Int a .st -> (a, .st)) *{a} .st -> (*{a}, .st) +updateArraySt f xs st + = map_array 0 xs st +where + map_array n xs st + #! (s, xs) = usize xs + | n == s + = (xs, st) + # (x, xs) = xs![n] + # (x, st) = f n x st + = map_array (inc n) {xs&[n]=x} st + + +//updateArraySt :: (Int .a .st -> (.a, .st)) *{a} .a .st -> (*{a}, .st) +updateArray1St f xs dummy st + # (xs, _, st) = map_array 0 xs dummy st + = (xs, st) +where + map_array n xs d st + #! (s, xs) = usize xs + | n == s + = (xs, d, st) + # (x, xs) = replace xs n d + # (x, st) = f n x st + # (d, xs) = replace xs n x + = map_array (inc n) xs d st + +update2dArraySt f xss st + = updateArray1St (\n xs st -> updateArraySt (f n) xs st) xss {} st + +//foldArraySt :: (Int a .st -> .st) {a} .st -> .st +foldArraySt f xs st + = fold_array 0 xs st +where + fold_array n xs st + #! (s, xs) = usize xs + | n == s + = st + # st = f n xs.[n] st + = fold_array (inc n) xs st + +//foldUArraySt :: (Int a .st -> .st) u:{a} .st -> (u:{a}, .st) +foldUArraySt f array st + = map_array 0 array st +where + map_array n array st + # (s, array) = usize array + | n == s + = (array, st) + # (x, array) = array ! [n] + # st = f x st + = map_array (inc n) array st + +//**************************************************************************************** +// General Helpers +//**************************************************************************************** + +idSt x st = (x, st) + +(--) infixl 5 :: u:[a] .[a] -> u:[a] | Eq a +(--) x y = removeMembers x y + +// should actually be in the standard library +transpose [] = [] +transpose [[] : xss] = transpose xss +transpose [[x:xs] : xss] = + [[x : [hd l \\ l <- xss]] : transpose [xs : [ tl l \\ l <- xss]]] + +unzip3 [] = ([], [], []) +unzip3 [(x1,x2,x3):xs] + # (x1s, x2s, x3s) = unzip3 xs + = ([x1:x1s], [x2:x2s], [x3:x3s]) + +foldOptional f No st = st +foldOptional f (Yes x) st = f x st + +mapOptional f No = No +mapOptional f (Yes x) = Yes (f x) + +mapOptionalSt f No st = (No, st) +mapOptionalSt f (Yes x) st + # (y, st) = f x st + = (Yes y, st) + +mapSt2 f [] st1 st2 = ([], st1, st2) +mapSt2 f [x:xs] st1 st2 + # (y, st1, st2) = f x st1 st2 + # (ys, st1, st2) = mapSt2 f xs st1 st2 + = ([y:ys], st1, st2) + +zipWith f [] [] = [] +zipWith f [x:xs] [y:ys] = [f x y : zipWith f xs ys] + +zipWithSt f [] [] st + = ([], st) +zipWithSt f [x:xs] [y:ys] st + # (z, st) = f x y st + # (zs, st) = zipWithSt f xs ys st + = ([z:zs], st) + +unfoldnSt :: (.st -> (a, .st)) !Int .st -> ([a], .st) +unfoldnSt f 0 st = ([], st) +unfoldnSt f n st + #! (x, st) = f st + #! (xs, st) = unfoldnSt f (dec n) st + = ([x:xs], st) diff --git a/frontend/genericsupport.dcl b/frontend/genericsupport.dcl new file mode 100644 index 0000000..670979c --- /dev/null +++ b/frontend/genericsupport.dcl @@ -0,0 +1,32 @@ +definition module genericsupport + +import syntax, checksupport + +lookupGenericClassInfo :: + !TypeKind + !GenericClassInfos + -> !(Optional GenericClassInfo) + +addGenericClassInfo :: + !GenericClassInfo + !GenericClassInfos + -> !GenericClassInfos + +getGenericMember :: + !(Global Index) // generic + !TypeKind // kind argument + !{#CommonDefs} // modules + !*GenericHeap + -> + ( Optional (Global Index) + , !*GenericHeap + ) + +//**************************************************************************************** +// Ident Helpers +//**************************************************************************************** +makeIdent :: !String -> !Ident +postfixIdent :: !Ident !String -> !Ident +genericIdentToClassIdent :: !Ident !TypeKind -> !Ident +genericIdentToMemberIdent :: !Ident !TypeKind -> !Ident +genericIdentToFunIdent :: !Ident !TypeCons -> !Ident diff --git a/frontend/genericsupport.icl b/frontend/genericsupport.icl new file mode 100644 index 0000000..b9033e2 --- /dev/null +++ b/frontend/genericsupport.icl @@ -0,0 +1,76 @@ +implementation module genericsupport + +import syntax, checksupport + +getGenericMember :: + !(Global Index) // generic + !TypeKind // kind argument + !{#CommonDefs} // modules + !*GenericHeap + -> + ( Optional (Global Index) + , !*GenericHeap + ) +getGenericMember {glob_module, glob_object} kind modules generic_heap + #! (gen_def=:{gen_info_ptr}) = modules.[glob_module].com_generic_defs.[glob_object] + #! ({gen_classes}, generic_heap) = readPtr gen_info_ptr generic_heap + = case lookupGenericClassInfo kind gen_classes of + No -> (No, generic_heap) + Yes {gci_module, gci_member} + #! member_glob = {glob_module = gci_module, glob_object = gci_member} + -> (Yes member_glob, generic_heap) + +lookupGenericClassInfo :: !TypeKind !GenericClassInfos -> !(Optional GenericClassInfo) +lookupGenericClassInfo kind class_infos + #! hash_index = case kind of + KindConst -> 0 + KindArrow kinds -> length kinds + = lookup kind class_infos.[hash_index] +where + lookup kind [] = No + lookup kind [gci:gcis] + | gci.gci_kind == kind = Yes gci + = lookup kind gcis + +addGenericClassInfo :: !GenericClassInfo !GenericClassInfos -> !GenericClassInfos +addGenericClassInfo class_info=:{gci_kind} class_infos + #! hash_index = case gci_kind of + KindConst -> 0 + KindArrow kinds -> length kinds + #! (class_infos1, class_infos) = class_infos ! [hash_index] + #! class_infos1 = [class_info:class_infos1] + = {{x\\x<-:class_infos} & [hash_index] = class_infos1 } + +//**************************************************************************************** +// Ident Helpers +//**************************************************************************************** +makeIdent :: !String -> !Ident +makeIdent str = {id_name = str, id_info = nilPtr} + +postfixIdent :: !Ident !String -> !Ident +postfixIdent {id_name} postfix = makeIdent (id_name +++ postfix) + +genericIdentToClassIdent :: !Ident !TypeKind -> !Ident +genericIdentToClassIdent gen_name kind + = postfixIdent gen_name ("_" +++ kind_to_str kind) +where + kind_to_str KindConst = "s" + kind_to_str (KindArrow kinds) + = kinds_to_str kinds +++ "s" + kinds_to_str [] = "" + kinds_to_str [KindConst:ks] = "s" +++ kinds_to_str ks + kinds_to_str [k:ks] = "o" +++ (kind_to_str k) +++ "c" +++ kinds_to_str ks + +genericIdentToMemberIdent :: !Ident !TypeKind -> !Ident +genericIdentToMemberIdent gen_name kind + = genericIdentToClassIdent gen_name kind + +genericIdentToFunIdent :: !Ident !TypeCons -> !Ident +genericIdentToFunIdent gen_name type_cons + = postfixIdent gen_name ("_" +++ type_cons_to_str type_cons) +where + type_cons_to_str (TypeConsSymb {type_name}) = toString type_name + type_cons_to_str (TypeConsBasic bt) = toString bt + type_cons_to_str TypeConsArrow = "ARROW" + type_cons_to_str (TypeConsVar tv) = tv.tv_name.id_name +
\ No newline at end of file diff --git a/frontend/hashtable.dcl b/frontend/hashtable.dcl index 88db318..7aab4e2 100644 --- a/frontend/hashtable.dcl +++ b/frontend/hashtable.dcl @@ -22,6 +22,8 @@ set_hte_mark :: !Int !*HashTable -> *HashTable | IC_Field !Ident | IC_Selector | IC_Instance ![Type] + | IC_Generic + | IC_GenericCase !Type | IC_Unknown :: BoxedIdent = {boxed_ident::!Ident} diff --git a/frontend/hashtable.icl b/frontend/hashtable.icl index a63393b..3abe810 100644 --- a/frontend/hashtable.icl +++ b/frontend/hashtable.icl @@ -20,6 +20,8 @@ import predef, syntax, StdCompare, compare_constructor | IC_Field !Ident | IC_Selector | IC_Instance ![Type] + | IC_Generic + | IC_GenericCase !Type | IC_Unknown :: BoxedIdent = {boxed_ident::!Ident} @@ -46,6 +48,8 @@ where = Smaller compare_types _ [] = Greater + (=<) (IC_GenericCase type1) (IC_GenericCase type2) + = type1 =< type2 (=<) (IC_Field typ_id1) (IC_Field typ_id2) = typ_id1 =< typ_id2 (=<) ic1 ic2 diff --git a/frontend/overloading.dcl b/frontend/overloading.dcl index c0cc2a6..4070b9b 100644 --- a/frontend/overloading.dcl +++ b/frontend/overloading.dcl @@ -30,6 +30,7 @@ import syntax, check, typesupport { os_type_heaps :: !.TypeHeaps , os_var_heap :: !.VarHeap , os_symbol_heap :: !.ExpressionHeap + , os_generic_heap :: !.GenericHeap , os_predef_symbols :: !.PredefinedSymbols , os_special_instances :: !.SpecialInstances , os_error :: !.ErrorAdmin diff --git a/frontend/overloading.icl b/frontend/overloading.icl index a320597..9f30202 100644 --- a/frontend/overloading.icl +++ b/frontend/overloading.icl @@ -3,7 +3,7 @@ implementation module overloading import StdEnv import syntax, check, type, typesupport, utilities, unitype, predef, checktypes, convertDynamics -import generics, compilerSwitches, type_io_common +import genericsupport, compilerSwitches, type_io_common :: InstanceTree = IT_Node !(Global Index) !InstanceTree !InstanceTree | IT_Empty @@ -61,6 +61,7 @@ import generics, compilerSwitches, type_io_common { os_type_heaps :: !.TypeHeaps , os_var_heap :: !.VarHeap , os_symbol_heap :: !.ExpressionHeap + , os_generic_heap :: !.GenericHeap , os_predef_symbols :: !.PredefinedSymbols , os_special_instances :: !.SpecialInstances , os_error :: !.ErrorAdmin @@ -764,9 +765,9 @@ tryToSolveOverloading ocs main_dcl_module_n defs instance_info coercion_env os d | os.os_error.ea_ok # (contexts, os_var_heap) = foldSt add_spec_contexts ocs (contexts, os.os_var_heap) (contexts, os_type_heaps) = remove_super_classes contexts os.os_type_heaps - ({ hp_var_heap, hp_expression_heap, hp_type_heaps}, dict_types) = foldSt (convert_dictionaries defs contexts) reduced_contexts - ({ hp_var_heap = os_var_heap, hp_expression_heap = os.os_symbol_heap, hp_type_heaps = os_type_heaps}, []) - = (contexts, coercion_env, type_pattern_vars, dict_types, { os & os_type_heaps = hp_type_heaps, os_symbol_heap = hp_expression_heap, os_var_heap = hp_var_heap} ) + ({ hp_var_heap, hp_expression_heap, hp_type_heaps,hp_generic_heap}, dict_types, os_error) = foldSt (convert_dictionaries defs contexts) reduced_contexts + ({ hp_var_heap = os_var_heap, hp_expression_heap = os.os_symbol_heap, hp_type_heaps = os_type_heaps,hp_generic_heap=os.os_generic_heap}, [], os.os_error) + = (contexts, coercion_env, type_pattern_vars, dict_types, { os & os_type_heaps = hp_type_heaps, os_symbol_heap = hp_expression_heap, os_var_heap = hp_var_heap, os_generic_heap = hp_generic_heap, os_error = os_error} ) = ([], coercion_env, type_pattern_vars, [], os) where reduce_contexts defs instance_info (opt_spec_contexts, expr_ptrs, pos, index) rc_state @@ -827,12 +828,12 @@ where = context = [tc : context] - convert_dictionaries :: !{# CommonDefs } ![TypeContext] !(!SymbIdent,!Index,!ExprInfoPtr,![ClassApplication]) !(!*Heaps,!DictionaryTypes) -> (!*Heaps,!DictionaryTypes) - convert_dictionaries defs contexts (oc_symbol, index, over_info_ptr, class_applications) (heaps, dict_types) - # (heaps, ptrs) = convertOverloadedCall defs contexts oc_symbol over_info_ptr class_applications (heaps, []) + convert_dictionaries :: !{# CommonDefs } ![TypeContext] !(!SymbIdent,!Index,!ExprInfoPtr,![ClassApplication]) !(!*Heaps,!DictionaryTypes, !*ErrorAdmin) -> (!*Heaps,!DictionaryTypes, !*ErrorAdmin) + convert_dictionaries defs contexts (oc_symbol, index, over_info_ptr, class_applications) (heaps, dict_types, error) + # (heaps, ptrs, error) = convertOverloadedCall defs contexts oc_symbol over_info_ptr class_applications (heaps, [], error) | isEmpty ptrs - = (heaps, dict_types) - = (heaps, add_to_dict_types index ptrs dict_types) + = (heaps, dict_types, error) + = (heaps, add_to_dict_types index ptrs dict_types, error) add_to_dict_types index ptrs [] = [(index, ptrs)] @@ -851,12 +852,12 @@ getDictionaryTypeAndConstructor {glob_module, glob_object = {ds_ident,ds_index}} (RecordType {rt_constructor}) = defs.[glob_module].com_type_defs.[class_dictionary.ds_index].td_rhs = (class_dictionary, rt_constructor) -convertOverloadedCall :: !{#CommonDefs} ![TypeContext] !SymbIdent !ExprInfoPtr ![ClassApplication] !(!*Heaps, ![ExprInfoPtr]) -> (!*Heaps, ![ExprInfoPtr]) -convertOverloadedCall defs contexts {symb_name,symb_kind = SK_OverloadedFunction {glob_module,glob_object}} expr_ptr [class_appl:class_appls] heaps_and_ptrs +convertOverloadedCall :: !{#CommonDefs} ![TypeContext] !SymbIdent !ExprInfoPtr ![ClassApplication] !(!*Heaps, ![ExprInfoPtr],!*ErrorAdmin) -> (!*Heaps, ![ExprInfoPtr],!*ErrorAdmin) +convertOverloadedCall defs contexts {symb_name,symb_kind = SK_OverloadedFunction {glob_module,glob_object}} expr_ptr [class_appl:class_appls] (heaps,ptrs,error) # mem_def = defs.[glob_module].com_member_defs.[glob_object] - (class_exprs, heaps_and_ptrs) = convertClassApplsToExpressions defs contexts class_appls heaps_and_ptrs + (class_exprs, heaps_and_ptrs) = convertClassApplsToExpressions defs contexts class_appls (heaps, ptrs) (inst_expr, (heaps, ptrs)) = adjust_member_application defs contexts mem_def class_appl class_exprs heaps_and_ptrs - = ({ heaps & hp_expression_heap = heaps.hp_expression_heap <:= (expr_ptr, inst_expr)}, ptrs) + = ({ heaps & hp_expression_heap = heaps.hp_expression_heap <:= (expr_ptr, inst_expr)}, ptrs, error) where adjust_member_application defs contexts {me_symb,me_offset,me_class} (CA_Instance red_contexts) class_exprs heaps_and_ptrs # ({glob_module,glob_object}, red_contexts) = find_instance_of_member me_class me_offset red_contexts @@ -885,20 +886,23 @@ where = find_instance_of_member me_class me_offset {rcs & rcs_constraints_contexts = rcs_constraints_contexts ++ rcss} find_instance_of_member_in_constraints me_class me_offset [] = abort "Error in module overloading: find_instance_of_member_in_constraints\n" -// AA.. -convertOverloadedCall defs contexts symbol=:{symb_kind = SK_Generic gen_glob kind} expr_ptr class_appls heaps_and_ptrs - # (found, member_glob) = getGenericMember gen_glob kind defs - | not found - = abort "convertOverloadedCall: no class for kind" - = convertOverloadedCall defs contexts {symbol & symb_kind = SK_OverloadedFunction member_glob} expr_ptr class_appls heaps_and_ptrs +// AA.. +convertOverloadedCall defs contexts symbol=:{symb_name, symb_kind = SK_Generic gen_glob kind} expr_ptr class_appls (heaps, expr_info_ptrs, error) + #! (opt_member_glob, hp_generic_heap) = getGenericMember gen_glob kind defs heaps.hp_generic_heap + #! heaps = { heaps & hp_generic_heap = hp_generic_heap } + = case opt_member_glob of + No + # error = checkError ("no generic instances of " +++ toString symb_name +++ " for kind") kind error + -> (heaps, expr_info_ptrs, error) + Yes member_glob -> convertOverloadedCall defs contexts {symbol & symb_kind = SK_OverloadedFunction member_glob} expr_ptr class_appls (heaps, expr_info_ptrs, error) // ..AA -convertOverloadedCall defs contexts {symb_name,symb_kind = SK_TypeCode} expr_info_ptr class_appls heaps_and_ptrs - # (class_expressions, (heaps, ptrs)) = convertClassApplsToExpressions defs contexts class_appls heaps_and_ptrs - = ({ heaps & hp_expression_heap = heaps.hp_expression_heap <:= (expr_info_ptr, EI_TypeCodes (map expressionToTypeCodeExpression class_expressions))}, ptrs) -convertOverloadedCall defs contexts {symb_name} expr_info_ptr appls heaps_and_ptrs - # (class_expressions, (heaps, ptrs)) = convertClassApplsToExpressions defs contexts appls heaps_and_ptrs - = ({ heaps & hp_expression_heap = heaps.hp_expression_heap <:= (expr_info_ptr, EI_Context class_expressions)}, ptrs) +convertOverloadedCall defs contexts {symb_name,symb_kind = SK_TypeCode} expr_info_ptr class_appls (heaps, ptrs, error) + # (class_expressions, (heaps, ptrs)) = convertClassApplsToExpressions defs contexts class_appls (heaps, ptrs) + = ({ heaps & hp_expression_heap = heaps.hp_expression_heap <:= (expr_info_ptr, EI_TypeCodes (map expressionToTypeCodeExpression class_expressions))}, ptrs, error) +convertOverloadedCall defs contexts {symb_name} expr_info_ptr appls (heaps,ptrs, error) + # (class_expressions, (heaps, ptrs)) = convertClassApplsToExpressions defs contexts appls (heaps,ptrs) + = ({ heaps & hp_expression_heap = heaps.hp_expression_heap <:= (expr_info_ptr, EI_Context class_expressions)}, ptrs, error) expressionToTypeCodeExpression (TypeCodeExpression texpr) = texpr @@ -1166,7 +1170,7 @@ where # (new_info_ptr, var_heap) = newPtr VI_Empty var_heap -> ([var_info_ptr : variables], var_heap <:= (var_info_ptr, VI_ClassVar (build_var_name id_name) new_info_ptr 0)) _ - -> abort ("determine_class_argument 1 (overloading.icl)")// <<- var_info) + -> abort ("determine_class_argument 1 (overloading.icl)") //<<- var_info) VI_Empty # (new_info_ptr, var_heap) = newPtr VI_Empty var_heap diff --git a/frontend/parse.icl b/frontend/parse.icl index 858505a..6e484d8 100644 --- a/frontend/parse.icl +++ b/frontend/parse.icl @@ -474,24 +474,85 @@ where | ~(isGlobalContext parseContext) = (False,abort "no def(2)",parseError "definition" No "generic definitions are only at the global level" pState) # (gendef, pState) = wantGenericDefinition parseContext pos pState - = (True, gendef, pState) - // ..AA + = (True, gendef, pState) + + try_definition parseContext DeriveToken pos pState + | ~(isGlobalContext parseContext) + = (False,abort "no def(2)",parseError "definition" No "derive declarations are only at the global level" pState) + # (gendef, pState) = wantDeriveDefinition parseContext pos pState + = (True, gendef, pState) + // ..AA + try_definition parseContext InstanceToken pos pState | ~(isGlobalContext parseContext) = (False,abort "no def(2)",parseError "definition" No "instance declarations are only at the global level" pState) # (instdef, pState) = wantInstanceDeclaration parseContext pos pState = (True, instdef, pState) + +// AA : new syntax for generics ... + try_definition parseContext (IdentToken name) pos pState + # (token, pState) = nextToken FunctionContext pState + = case token of + GenericOpenToken // generic function + # (type, pState) = wantType pState + # (type_cons, pState) = get_type_cons type pState + with + get_type_cons (TA type_symb []) pState + = (TypeConsSymb type_symb, pState) + get_type_cons (TB tb) pState + = (TypeConsBasic tb, pState) + get_type_cons TArrow pState + = (TypeConsArrow, pState) + get_type_cons (TV tv) pState + = (TypeConsVar tv, pState) + get_type_cons _ pState + # pState = parseError "generic type" No " invalid" pState + = (abort "no TypeCons", pState) + # pState = wantToken FunctionContext "type argument" GenericCloseToken pState + # (ident, pState) = stringToIdent name (IC_GenericCase type) pState + # (generic_ident, pState) = stringToIdent name IC_Generic pState + + # (args, pState) = parseList trySimpleLhsExpression pState + + // must be EqualToken or HashToken or ??? + //# pState = wantToken FunctionContext "generic definition" EqualToken pState + //# pState = tokenBack pState + + #(ss_useLayout, pState) = accScanState UseLayout pState + # localsExpected = isNotEmpty args || isGlobalContext parseContext || ~ ss_useLayout + # (rhs, _, pState) = wantRhs localsExpected (ruleDefiningRhsSymbol parseContext) pState + + # generic_case = + { gc_name = ident + , gc_gname = generic_ident + , gc_generic = {gi_module=NoIndex,gi_index=NoIndex} + , gc_arity = length args + , gc_pos = pos + , gc_type = type + , gc_type_cons = type_cons + , gc_body = GCB_ParsedBody args rhs + , gc_kind = KindError + } + -> (True, PD_GenericCase generic_case, pState) + _ // normal function + # pState = tokenBack pState + # (lhs, pState) = want_lhs_of_def (IdentToken name) pState + (token, pState) = nextToken FunctionContext pState + (def, pState) = want_rhs_of_def parseContext lhs token (determine_position lhs pos) pState + -> (True, def, pState) +// ... AA + try_definition parseContext token pos pState | isLhsStartToken token # (lhs, pState) = want_lhs_of_def token pState (token, pState) = nextToken FunctionContext pState (def, pState) = want_rhs_of_def parseContext lhs token (determine_position lhs pos) pState = (True, def, pState) - with - determine_position (Yes (name, _), _) (LinePos f l) = FunPos f l name.id_name - determine_position lhs pos = pos = (False, abort "no def(1)", tokenBack pState) + determine_position (Yes (name, _), _) (LinePos f l) = FunPos f l name.id_name + determine_position lhs pos = pos + want_lhs_of_def :: !Token !ParseState -> (!(Optional (Ident, Bool), ![ParsedExpr]), !ParseState) want_lhs_of_def token pState # (succ, fname, is_infix, pState) = try_function_symbol token pState @@ -1240,28 +1301,28 @@ wantInstanceDeclaration parseContext pi_pos pState (pi_ident, pState) = stringToIdent class_name (IC_Instance pi_types) pState // AA.. # (token, pState) = nextToken TypeContext pState +/* | token == GenericToken # pState = wantEndOfDefinition "generic instance declaration" pState = (PD_Instance {pi_class = pi_class, pi_ident = pi_ident, pi_types = pi_types, pi_context = pi_context, - pi_members = [], pi_specials = SP_None, pi_pos = pi_pos, pi_generate = True}, pState) + pi_members = [], pi_specials = SP_None, pi_pos = pi_pos}, pState) +*/ // ..AA | isIclContext parseContext - # // PK pState = tokenBack pState // AA - pState = want_begin_group token pState + # pState = want_begin_group token pState (pi_members, pState) = wantDefinitions (SetClassOrInstanceDefsContext parseContext) pState pState = wantEndGroup "instance" pState = (PD_Instance {pi_class = pi_class, pi_ident = pi_ident, pi_types = pi_types, pi_context = pi_context, - pi_members = pi_members, pi_specials = SP_None, pi_pos = pi_pos, pi_generate = False }, pState) + pi_members = pi_members, pi_specials = SP_None, pi_pos = pi_pos}, pState) // otherwise // ~ (isIclContext parseContext) | token == CommaToken - // AA: # (token, pState) = nextToken TypeContext pState # (pi_types_and_contexts, pState) = want_instance_types pState (idents, pState) = seqList [stringToIdent class_name (IC_Instance type) \\ (type,context) <- pi_types_and_contexts] pState = (PD_Instances // [ { pi_class = pi_class, pi_ident = pi_ident, pi_types = type, pi_context = context // voor martin [ { pi_class = pi_class, pi_ident = ident, pi_types = type, pi_context = context - , pi_members = [], pi_specials = SP_None, pi_pos = pi_pos, pi_generate = False} + , pi_members = [], pi_specials = SP_None, pi_pos = pi_pos} \\ (type,context) <- [ (pi_types, pi_context) : pi_types_and_contexts ] & ident <- [ pi_ident : idents ] ] @@ -1271,7 +1332,7 @@ wantInstanceDeclaration parseContext pi_pos pState # (specials, pState) = optionalSpecials (tokenBack pState) pState = wantEndOfDefinition "instance declaration" pState = (PD_Instance {pi_class = pi_class, pi_ident = pi_ident, pi_types = pi_types, pi_context = pi_context, - pi_members = [], pi_specials = specials, pi_pos = pi_pos, pi_generate = False}, pState) + pi_members = [], pi_specials = specials, pi_pos = pi_pos}, pState) where want_begin_group token pState // For JvG layout @@ -1379,13 +1440,13 @@ optionalCoercions pState wantGenericDefinition :: !ParseContext !Position !ParseState -> (!ParsedDefinition, !ParseState) wantGenericDefinition parseContext pos pState | SwitchGenerics False True - = (PD_Erroneous, parseError "generic definition" No "generics are not supported" pState) + = (PD_Erroneous, parseErrorSimple "generic definition" "generics are not supported by this compiler" pState) | not pState.ps_support_generics - = (PD_Erroneous, parseError "generic definition" No "to enable generics use the command line flag -generics" pState) + = (PD_Erroneous, parseErrorSimple "generic definition" "to enable generics use the command line flag -generics" pState) # (name, pState) = want_name pState | name == "" = (PD_Erroneous, pState) - # (ident, pState) = stringToIdent name IC_Class pState + # (ident, pState) = stringToIdent name IC_Generic/*IC_Class*/ pState # (member_ident, pState) = stringToIdent name IC_Expression pState # (arg_vars, pState) = wantList "generic variable(s)" try_variable pState @@ -1395,16 +1456,15 @@ wantGenericDefinition parseContext pos pState # gen_def = { gen_name = ident , gen_member_name = member_ident - , gen_type = - { gt_type = type - , gt_vars = arg_vars - , gt_arity = length arg_vars - } + , gen_type = type + , gen_vars = arg_vars , gen_pos = pos - , gen_kinds_ptr = nilPtr - , gen_classes = [] - , gen_isomap = MakeDefinedSymbol {id_name="",id_info=nilPtr} NoIndex 0 - , gen_cons_ptr = nilPtr + , gen_info_ptr = nilPtr + , gen_bimap = + { ds_ident = {id_name = "", id_info = nilPtr} + , ds_index = NoIndex + , ds_arity = 0 + } } = (PD_Generic gen_def, pState) where @@ -1419,7 +1479,65 @@ wantGenericDefinition parseContext pos pState try_variable pState # (token, pState) = nextToken TypeContext pState = tryTypeVarT token pState - + +wantDeriveDefinition :: !ParseContext !Position !*ParseState -> (!ParsedDefinition, !*ParseState) +wantDeriveDefinition parseContext pos pState + | SwitchGenerics False True + = (PD_Erroneous, parseErrorSimple "generic definition" "generics are not supported by this compiler" pState) + | not pState.ps_support_generics + = (PD_Erroneous, parseErrorSimple "generic definition" "to enable generics use the command line flag -generics" pState) + # (name, pState) = want_name pState + | name == "" + = (PD_Erroneous, pState) + # (derive_defs, pState) = want_derive_types name pState + = (PD_Derive derive_defs, pState) +where + want_name pState + # (token, pState) = nextToken TypeContext pState + = case token of + IdentToken name -> (name, pState) + _ -> ("", parseError "Generic Definition" (Yes token) "<identifier>" pState) + want_derive_types :: String !*ParseState -> ([GenericCaseDef], !*ParseState) + want_derive_types name pState + # (derive_def, pState) = want_derive_type name pState + # (token, pState) = nextToken TypeContext pState + | token == CommaToken + # (derive_defs, pState) = want_derive_types name pState + = ([derive_def:derive_defs], pState) + = ([derive_def], pState) + + want_derive_type :: String !*ParseState -> (GenericCaseDef, !*ParseState) + want_derive_type name pState + # (type, pState) = wantType pState + # (ident, pState) = stringToIdent name (IC_GenericCase type) pState + # (generic_ident, pState) = stringToIdent name IC_Generic pState + # (type_cons, pState) = get_type_cons type pState + # derive_def = + { gc_name = ident + , gc_gname = generic_ident + , gc_generic = {gi_module=NoIndex,gi_index=NoIndex} + , gc_arity = 0 + , gc_pos = pos + , gc_type = type + , gc_type_cons = type_cons + , gc_body = GCB_None + , gc_kind = KindError + } + = (derive_def, pState) + get_type_cons :: Type !*ParseState -> (TypeCons, !*ParseState) + get_type_cons (TA type_symb []) pState + = (TypeConsSymb type_symb, pState) + get_type_cons (TB tb) pState + = (TypeConsBasic tb, pState) + get_type_cons TArrow pState + = (TypeConsArrow, pState) + get_type_cons (TV tv) pState + | isDclContext parseContext + = (TypeConsVar tv, pState) + get_type_cons type pState + # pState = parseError "generic type" No " type constructor" pState + = (abort "no TypeCons", pState) + // ..AA /* @@ -3542,9 +3660,9 @@ wantBeginGroup msg pState wantKind :: !ParseState -> !(!TypeKind, !ParseState) wantKind pState | SwitchGenerics False True - = (KindConst, parseError "kind" No "generics are not supported" pState) + = (KindConst, parseErrorSimple "kind" "generics are not supported by this compiler" pState) | not pState.ps_support_generics - = (KindConst, parseError "kind" No "to enable generics use -generics command line flag" pState) + = (KindConst, parseErrorSimple "kind" "to enable generics use -generics command line flag" pState) # (token, pState) = nextToken TypeContext pState # (kind, pState) = want_simple_kind token pState # (token, pState) = nextToken TypeContext pState @@ -3670,6 +3788,26 @@ parseError act opt_token msg pState Yes _ -> tokenBack pState No -> pState +parseErrorSimple :: !{# Char} !{# Char} !ParseState -> ParseState +parseErrorSimple act msg pState + | pState.ps_skipping + = pState + | otherwise // not pState.ps_skipping + # (pos,pState) = getPosition pState + (filename,pState=:{ps_error={pea_file}}) = getFilename pState + pea_file = pea_file + <<< "Parse error [" + <<< filename <<< "," + <<< pos + <<< (if (size act > 0) ("," + act) "") <<< "]: " + <<< msg + <<< '\n' + pState = { pState + & ps_skipping = True + , ps_error = { pea_file = pea_file, pea_ok = False } + } + = pState + getFileAndLineNr :: !ParseState -> (!String, !Int, !ParseState) getFileAndLineNr pState =: {ps_scanState} # (filename,scanState) = getFilename ps_scanState diff --git a/frontend/postparse.icl b/frontend/postparse.icl index 2b0d3d2..c4c9ecf 100644 --- a/frontend/postparse.icl +++ b/frontend/postparse.icl @@ -2,8 +2,10 @@ implementation module postparse import StdEnv import syntax, parse, utilities, containers, StdCompare +import genericsupport //import RWSDebug + :: *CollectAdmin = { ca_error :: !*ParseErrorAdmin , ca_fun_count :: !Int @@ -351,6 +353,13 @@ instance collectFunctions (ParsedInstance a) | collectFunctions a where # (pi_members, ca) = collectFunctions pi_members icl_module ca = ({inst & pi_members = pi_members }, ca) +instance collectFunctions GenericCaseDef where + collectFunctions gc=:{gc_body=GCB_FunDef fun_def} icl_module ca + # (fun_def, ca) = collectFunctions fun_def icl_module ca + = ({gc & gc_body = GCB_FunDef fun_def}, ca) + collectFunctions gc=:{gc_body=GCB_None} icl_module ca + = (gc, ca) + instance collectFunctions FunDef where collectFunctions fun_def=:{fun_body = ParsedBody bodies} icl_module ca # (bodies, ca) = collectFunctions bodies icl_module ca @@ -1033,7 +1042,8 @@ where MakeEmptyModule name mod_type :== { mod_name = name, mod_modification_time = "", mod_type = mod_type, mod_imports = [], mod_imported_objects = [], mod_defs = { def_types = [], def_constructors = [], def_selectors = [], def_classes = [], def_macro_indices={ir_from=0,ir_to=0}, - def_macros=[],def_members = [], def_funtypes = [], def_instances = [], def_generics = [] } } + def_macros=[],def_members = [], def_funtypes = [], def_instances = [], + def_generics = [], def_generic_cases = []} } parseAndScanDclModule :: !Ident !Position ![ScannedModule] ![Ident] !SearchPaths !Bool (ModTimeFunction *Files) !*Files !*CollectAdmin -> *(!Bool, ![ScannedModule],!*Files, !*CollectAdmin) @@ -1070,6 +1080,7 @@ scanModule mod=:{mod_name,mod_type,mod_defs = pdefs} cached_modules support_gene , ca_hash_table = hash_table } (fun_defs, defs, imports, imported_objects, ca) = reorganiseDefinitions True pdefs 0 0 0 0 ca + (reorganise_icl_ok, ca) = ca!ca_error.pea_ok (import_dcl_ok, optional_parsed_dcl_mod,dcl_module_n,parsed_modules, cached_modules,files, ca) @@ -1098,10 +1109,13 @@ scanModule mod=:{mod_name,mod_type,mod_defs = pdefs} cached_modules support_gene (macro_defs, ca) = collectFunctions defs.def_macros True ca (macro_range, ca) = addFunctionsRange macro_defs ca (def_instances, ca) = collectFunctions defs.def_instances True ca + (def_generic_cases, ca) = collectFunctions defs.def_generic_cases True ca { ca_error = {pea_file = err_file,pea_ok}, ca_rev_fun_defs, ca_hash_table } = ca - mod = { mod & mod_imports = imports, mod_imported_objects = imported_objects, mod_defs = { defs & def_instances = def_instances, - def_macro_indices = macro_range }} + mod = { mod & mod_imports = imports, mod_imported_objects = imported_objects, + mod_defs = { defs & def_instances = def_instances, + def_generic_cases = def_generic_cases, + def_macro_indices = macro_range }} hash_table = set_hte_mark 0 ca_hash_table @@ -1180,6 +1194,27 @@ collectFunctionBodies fun_name fun_arity fun_prio fun_kind all_defs=:[PD_Functio collectFunctionBodies fun_name fun_arity fun_prio fun_kind defs ca = ([], fun_kind, defs, ca) +collectGenericBodies :: !GenericCaseDef ![ParsedDefinition] !*CollectAdmin + -> (![ParsedBody], ![ParsedDefinition], !*CollectAdmin) +collectGenericBodies first_case all_defs=:[PD_GenericCase gc : defs] ca + | first_case.gc_name == gc.gc_name && first_case.gc_type_cons == gc.gc_type_cons + # (bodies, rest_defs, ca) = collectGenericBodies first_case defs ca + # (GCB_ParsedBody args rhs) = gc.gc_body + # body = + { pb_args = args + , pb_rhs = rhs + , pb_position = gc.gc_pos + } + | first_case.gc_arity == gc.gc_arity + = ([body : bodies ], rest_defs, ca) + # msg = "This alternative has " + toString gc.gc_arity + " argument" + + (if (gc.gc_arity <> 1) "s" "")+" instead of " + toString first_case.gc_arity + # ca = postParseError gc.gc_pos msg ca + = ([body : bodies ], rest_defs, ca) + = ([], all_defs, ca) +collectGenericBodies first_case defs ca + = ([], defs, ca) + strictness_from_fields :: ![ParsedSelector] -> StrictnessList strictness_from_fields fields = add_strictness_for_arguments fields 0 0 NotStrict @@ -1372,10 +1407,35 @@ where = ([], ca) reorganiseDefinitions icl_module [PD_Instances class_instances : defs] cons_count sel_count mem_count type_count ca = reorganiseDefinitions icl_module ([PD_Instance class_instance \\ class_instance <- class_instances] ++ defs) cons_count sel_count mem_count type_count ca +// AA .. reorganiseDefinitions icl_module [PD_Generic gen : defs] cons_count sel_count mem_count type_count ca # (fun_defs, c_defs, imports, imported_objects, ca) = reorganiseDefinitions icl_module defs cons_count sel_count mem_count type_count ca c_defs = {c_defs & def_generics = [gen : c_defs.def_generics]} = (fun_defs, c_defs, imports, imported_objects, ca) +reorganiseDefinitions icl_module [PD_GenericCase gc : defs] cons_count sel_count mem_count type_count ca + #! (bodies, defs, ca) = collectGenericBodies gc defs ca + #! (fun_defs, c_defs, imports, imported_objects, ca) + = reorganiseDefinitions icl_module defs cons_count sel_count mem_count type_count ca + # (GCB_ParsedBody args rhs) = gc.gc_body + # body = + { pb_args = args + , pb_rhs = rhs + , pb_position = gc.gc_pos + } + #! bodies = [body : bodies ] + #! fun_name = genericIdentToFunIdent gc.gc_name gc.gc_type_cons + #! fun = MakeNewImpOrDefFunction fun_name gc.gc_arity bodies (FK_Function cNameNotLocationDependent) NoPrio No gc.gc_pos + #! inst = { gc & gc_body = GCB_FunDef fun } + #! c_defs = {c_defs & def_generic_cases = [inst : c_defs.def_generic_cases]} + ---> ("collected", gc.gc_name, gc.gc_type_cons, length bodies) + = (fun_defs, c_defs, imports, imported_objects, ca) + +reorganiseDefinitions icl_module [PD_Derive derive_defs : defs] cons_count sel_count mem_count type_count ca + #! (fun_defs, c_defs, imports, imported_objects, ca) = reorganiseDefinitions icl_module defs cons_count sel_count mem_count (type_count+1) ca + #! c_defs = { c_defs & def_generic_cases = derive_defs ++ c_defs.def_generic_cases} + = (fun_defs, c_defs, imports, imported_objects, ca) +// .. AA + reorganiseDefinitions icl_module [PD_Import new_imports : defs] cons_count sel_count mem_count type_count ca # (fun_defs, c_defs, imports, imported_objects, ca) = reorganiseDefinitions icl_module defs cons_count sel_count mem_count type_count ca = (fun_defs, c_defs, new_imports ++ imports, imported_objects, ca) @@ -1386,7 +1446,8 @@ reorganiseDefinitions icl_module [def:defs] _ _ _ _ ca = abort ("reorganiseDefinitions does not match" ---> def) reorganiseDefinitions icl_module [] _ _ _ _ ca = ([], { def_types = [], def_constructors = [], def_selectors = [], def_macros = [],def_macro_indices={ir_from=0,ir_to=0},def_classes = [], def_members = [], - def_instances = [], def_funtypes = [], def_generics = [] }, [], [], ca) + def_instances = [], def_funtypes = [], + def_generics = [], def_generic_cases = []}, [], [], ca) belongsToTypeSpec name prio new_name is_infix :== name == new_name && sameFixity prio is_infix diff --git a/frontend/predef.dcl b/frontend/predef.dcl index 39b5fe1..c85e4b1 100644 --- a/frontend/predef.dcl +++ b/frontend/predef.dcl @@ -155,10 +155,10 @@ PD_ModuleConsSymbol :== 176 /* Generics */ PD_StdGeneric :== 177 -PD_TypeISO :== 178 -PD_ConsISO :== 179 -PD_iso_to :== 180 -PD_iso_from :== 181 +PD_TypeBimap :== 178 +PD_ConsBimap :== 179 +PD_map_to :== 180 +PD_map_from :== 181 PD_TypeUNIT :== 182 PD_ConsUNIT :== 183 @@ -167,25 +167,11 @@ PD_ConsLEFT :== 185 PD_ConsRIGHT :== 186 PD_TypePAIR :== 187 PD_ConsPAIR :== 188 -PD_TypeARROW :== 189 -PD_ConsARROW :== 190 -PD_TypeConsDefInfo :== 191 -PD_ConsConsDefInfo :== 192 -PD_TypeTypeDefInfo :== 193 -PD_ConsTypeDefInfo :== 194 -PD_cons_info :== 195 -PD_TypeCONS :== 196 -PD_ConsCONS :== 197 +PD_GenericBimap :== 189 +PD_bimapId :== 190 -PD_isomap_ARROW_ :== 198 -PD_isomap_ID :== 199 - -PD_TypeType :== 200 -PD_ConsTypeApp :== 201 -PD_ConsTypeVar :== 202 - -PD_NrOfPredefSymbols :== 203 +PD_NrOfPredefSymbols :== 191 GetTupleConsIndex tup_arity :== PD_Arity2TupleSymbol + tup_arity - 2 GetTupleTypeIndex tup_arity :== PD_Arity2TupleType + tup_arity - 2 diff --git a/frontend/predef.icl b/frontend/predef.icl index 0d0d706..8c4ee6b 100644 --- a/frontend/predef.icl +++ b/frontend/predef.icl @@ -155,10 +155,10 @@ PD_ModuleConsSymbol :== 176 /* Generics */ PD_StdGeneric :== 177 -PD_TypeISO :== 178 -PD_ConsISO :== 179 -PD_iso_to :== 180 -PD_iso_from :== 181 +PD_TypeBimap :== 178 +PD_ConsBimap :== 179 +PD_map_to :== 180 +PD_map_from :== 181 PD_TypeUNIT :== 182 PD_ConsUNIT :== 183 @@ -167,25 +167,11 @@ PD_ConsLEFT :== 185 PD_ConsRIGHT :== 186 PD_TypePAIR :== 187 PD_ConsPAIR :== 188 -PD_TypeARROW :== 189 -PD_ConsARROW :== 190 -PD_TypeConsDefInfo :== 191 -PD_ConsConsDefInfo :== 192 -PD_TypeTypeDefInfo :== 193 -PD_ConsTypeDefInfo :== 194 -PD_cons_info :== 195 -PD_TypeCONS :== 196 -PD_ConsCONS :== 197 +PD_GenericBimap :== 189 +PD_bimapId :== 190 -PD_isomap_ARROW_ :== 198 -PD_isomap_ID :== 199 - -PD_TypeType :== 200 -PD_ConsTypeApp :== 201 -PD_ConsTypeVar :== 202 - -PD_NrOfPredefSymbols :== 203 +PD_NrOfPredefSymbols :== 191 (<<=) infixl (<<=) symbol_table val @@ -296,9 +282,11 @@ predefined_idents [PD_TypeID] = i "T_ypeID", [PD_ModuleID] = i "ModuleID", - [PD_StdGeneric] = i "StdGeneric", - [PD_TypeISO] = i "ISO", - [PD_ConsISO] = i "_ISO", + [PD_StdGeneric] = i "StdGeneric2", + [PD_TypeBimap] = i "Bimap", + [PD_ConsBimap] = i "_Bimap", + [PD_map_to] = i "map_to", + [PD_map_from] = i "map_from", [PD_TypeUNIT] = i "UNIT", [PD_ConsUNIT] = i "UNIT", [PD_TypeEITHER] = i "EITHER", @@ -306,30 +294,15 @@ predefined_idents [PD_ConsRIGHT] = i "RIGHT", [PD_TypePAIR] = i "PAIR", [PD_ConsPAIR] = i "PAIR", - [PD_TypeARROW] = i "ARROW", - [PD_ConsARROW] = i "ARROW", - [PD_isomap_ARROW_] = i "isomap_ARROW_", - [PD_isomap_ID] = i "isomap_ID", - [PD_TypeConsDefInfo] = i "ConsDefInfo", - [PD_ConsConsDefInfo] = i "_ConsDefInfo", - [PD_TypeTypeDefInfo] = i "TypeDefInfo", - [PD_ConsTypeDefInfo] = i "_TypeDefInfo", - [PD_TypeCONS] = i "CONS", - [PD_ConsCONS] = i "CONS", - [PD_cons_info] = i "CONS_INFO", - [PD_TypeType] = i "Type", - [PD_ConsTypeApp] = i "TypeApp", - [PD_ConsTypeVar] = i "TypeVar", - + [PD_GenericBimap] = i "bimap", + [PD_bimapId] = i "bimapId", + [PD_StdMisc] = i "StdMisc", [PD_abort] = i "abort", [PD_undef] = i "undef", [PD_Start] = i "Start", - - [PD_iso_from] = i "iso_from", - [PD_iso_to] = i "iso_to", - + [PD_DynamicType] = i "type", [PD_DynamicValue] = i "value" } @@ -462,41 +435,29 @@ where <<- (local_predefined_idents, IC_Expression, PD_ModuleID) <<- (local_predefined_idents, IC_Module, PD_StdGeneric) - <<- (local_predefined_idents, IC_Type, PD_TypeISO) - <<- (local_predefined_idents, IC_Expression, PD_ConsISO) + <<- (local_predefined_idents, IC_Type, PD_TypeBimap) + <<- (local_predefined_idents, IC_Expression, PD_ConsBimap) <<- (local_predefined_idents, IC_Type, PD_TypeUNIT) <<- (local_predefined_idents, IC_Expression, PD_ConsUNIT) <<- (local_predefined_idents, IC_Type, PD_TypeEITHER) <<- (local_predefined_idents, IC_Expression, PD_ConsLEFT) <<- (local_predefined_idents, IC_Expression, PD_ConsRIGHT) <<- (local_predefined_idents, IC_Type, PD_TypePAIR) - <<- (local_predefined_idents, IC_Expression, PD_ConsPAIR) - <<- (local_predefined_idents, IC_Type, PD_TypeARROW) - <<- (local_predefined_idents, IC_Expression, PD_ConsARROW) - <<- (local_predefined_idents, IC_Expression, PD_isomap_ARROW_) - <<- (local_predefined_idents, IC_Expression, PD_isomap_ID) - <<- (local_predefined_idents, IC_Type, PD_TypeConsDefInfo) - <<- (local_predefined_idents, IC_Expression, PD_ConsConsDefInfo) - <<- (local_predefined_idents, IC_Type, PD_TypeTypeDefInfo) - <<- (local_predefined_idents, IC_Expression, PD_ConsTypeDefInfo) - <<- (local_predefined_idents, IC_Type, PD_TypeCONS) - <<- (local_predefined_idents, IC_Expression, PD_ConsCONS) - <<- (local_predefined_idents, IC_Expression, PD_cons_info) - <<- (local_predefined_idents, IC_Type, PD_TypeType) - <<- (local_predefined_idents, IC_Expression, PD_ConsTypeApp) - <<- (local_predefined_idents, IC_Expression, PD_ConsTypeVar) - + <<- (local_predefined_idents, IC_Expression, PD_ConsPAIR) + <<- (local_predefined_idents, IC_Generic, PD_GenericBimap) + <<- (local_predefined_idents, IC_Expression, PD_bimapId) + <<- (local_predefined_idents, IC_Module, PD_StdMisc) <<- (local_predefined_idents, IC_Expression, PD_abort) <<- (local_predefined_idents, IC_Expression, PD_undef) <<- (local_predefined_idents, IC_Expression, PD_Start) - # type_iso_ident = local_predefined_idents.[PD_TypeISO] - # hash_table= hash_table - <<- (local_predefined_idents, IC_Field type_iso_ident, PD_iso_from) - <<- (local_predefined_idents, IC_Field type_iso_ident, PD_iso_to) - + # bimap_type = local_predefined_idents.[PD_TypeBimap] + # hash_table = hash_table + <<- (local_predefined_idents, IC_Field bimap_type, PD_map_to) + <<- (local_predefined_idents, IC_Field bimap_type, PD_map_from) + # dynamic_temp_ident = local_predefined_idents.[PD_DynamicTemp] # hash_table = hash_table <<- (local_predefined_idents, IC_Field dynamic_temp_ident, PD_DynamicType) @@ -577,7 +538,8 @@ buildPredefinedModule pre_def_symbols def_constructors = [cons_def,strict_cons_def,unboxed_cons_def,tail_strict_cons_def,strict_tail_strict_cons_def,unboxed_tail_strict_cons_def,overloaded_cons_def, nil_def,strict_nil_def,unboxed_nil_def,tail_strict_nil_def,strict_tail_strict_nil_def,unboxed_tail_strict_nil_def,overloaded_nil_def : cons_defs], def_selectors = [], def_classes = [class_def], - def_macro_indices= { ir_from = 0, ir_to = 0 },def_macros=[],def_members = [member_def], def_funtypes = [alias_dummy_type], def_instances = [], def_generics = [] }}, pre_def_symbols) + def_macro_indices= { ir_from = 0, ir_to = 0 },def_macros=[],def_members = [member_def], def_funtypes = [alias_dummy_type], def_instances = [], + def_generics = [], def_generic_cases = []}}, pre_def_symbols) where add_tuple_defs pre_mod_id tup_arity type_defs cons_defs pre_def_symbols diff --git a/frontend/scanner.dcl b/frontend/scanner.dcl index d083ee5..6399d2a 100644 --- a/frontend/scanner.dcl +++ b/frontend/scanner.dcl @@ -107,6 +107,7 @@ instance <<< FilePosition | ErrorToken String // if an error occured | GenericToken // generic + | DeriveToken // derive | GenericOpenToken // {| | GenericCloseToken // |} diff --git a/frontend/scanner.icl b/frontend/scanner.icl index d266b21..bdd168e 100644 --- a/frontend/scanner.icl +++ b/frontend/scanner.icl @@ -193,6 +193,7 @@ ScanOptionNoNewOffsideForSeqLetBit:==4; | ErrorToken String // an error has occured | GenericToken // generic + | DeriveToken // derive | GenericOpenToken // {| | GenericCloseToken // |} @@ -813,6 +814,7 @@ CheckEveryContext s input "class" -> (ClassToken , input) "instance" -> (InstanceToken , input) "generic" -> (GenericToken , input) + "derive" -> (DeriveToken , input) "otherwise" -> (OtherwiseToken , input) "!" -> (ExclamationToken , input) "*/" -> (ErrorToken "Unexpected end of comment, */", input) diff --git a/frontend/syntax.dcl b/frontend/syntax.dcl index bdbaecd..5fbfe18 100644 --- a/frontend/syntax.dcl +++ b/frontend/syntax.dcl @@ -40,7 +40,8 @@ instance == FunctionOrMacroIndex | STE_Field !Ident | STE_Class | STE_Member - | STE_Generic // AA: For generic declarations + | STE_Generic // AA + | STE_GenericCase // AA | STE_Instance !Ident // argument: the class (used in explicitimports (1.3 syntax only)) | STE_Variable !VarInfoPtr | STE_TypeVariable !TypeVarInfoPtr @@ -115,9 +116,10 @@ instance == FunctionOrMacroIndex , def_macro_indices :: !IndexRange , def_classes :: ![ClassDef] , def_members :: ![MemberDef] - , def_generics :: ![GenericDef] , def_funtypes :: ![FunType] , def_instances :: ![instance_kind] + , def_generics :: ![GenericDef] // AA + , def_generic_cases :: ![GenericCaseDef] // AA } :: LocalDefs = LocalParsedDefs [ParsedDefinition] @@ -167,11 +169,13 @@ cIsNotAFunction :== False | PD_Type ParsedTypeDef | PD_TypeSpec Position Ident Priority (Optional SymbolType) Specials | PD_Class ClassDef [ParsedDefinition] - | PD_Generic GenericDef | PD_Instance (ParsedInstance ParsedDefinition) | PD_Instances [ParsedInstance ParsedDefinition] | PD_Import [ParsedImport] | PD_ImportedObjects [ImportedObject] + | PD_Generic GenericDef // AA + | PD_GenericCase GenericCaseDef // AA + | PD_Derive [GenericCaseDef] // AA | PD_Erroneous :: FunKind = FK_Function !Bool | FK_Macro | FK_Caf | FK_NodeDefOrFunction | FK_Unknown @@ -208,7 +212,6 @@ cNameLocationDependent :== True , pi_pos :: !Position , pi_members :: ![member] , pi_specials :: !Specials - , pi_generate :: !Bool // AA: instance is to be generated } /* @@ -282,31 +285,65 @@ cNameLocationDependent :== True // AA ... -:: GenericDef = - { gen_name :: !Ident // the generics name in the IC_Class - , gen_member_name :: !Ident // the generics name in the IC_Member - , gen_type :: !GenericType +:: GenericDef = + { gen_name :: !Ident // the generics name in IC_Class + , gen_member_name :: !Ident // the generics name in IC_Member , gen_pos :: !Position - , gen_kinds_ptr :: !TypeVarInfoPtr // hack: contains all used kinds - , gen_cons_ptr :: !TypeVarInfoPtr // hack: cons instance function - , gen_classes :: !GenericClassInfos // generated classes - , gen_isomap :: !DefinedSymbol // isomap function + , gen_type :: !SymbolType // Generic type (st_vars include generic type vars) + , gen_vars :: ![TypeVar] // Generic type variables + , gen_info_ptr :: !GenericInfoPtr + , gen_bimap :: !DefinedSymbol // fun def index of the bimap for the generic type } +:: 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_cases :: ![GlobalIndex] + , gen_var_kinds :: ![TypeKind] // kinds of all st_vars of the gen_type + , gen_star_case :: !GlobalIndex // general case for kind-star types + } +:: GenericInfoPtr :== Ptr GenericInfo +:: GenericHeap :== Heap GenericInfo + +:: TypeCons + = TypeConsSymb TypeSymbIdent + | TypeConsBasic BasicType + | TypeConsArrow + | TypeConsVar TypeVar + +:: GenericCaseDef = + { gc_name :: !Ident // name in IC_GenricCase namespace + , gc_gname :: !Ident // name in IC_Generic namespace + , gc_generic :: !GlobalIndex // index of the generic + , gc_arity :: !Int // arity of the function + , gc_pos :: !Position // position in the source file + , gc_type :: !Type // the instance type + , gc_type_cons :: !TypeCons // type constructor of the type argument + , gc_body :: !GenericCaseBody // the body function or NoIndex + , gc_kind :: !TypeKind // kind of the instance type + } +:: GenericCaseBody + = GCB_None // to be generated + | GCB_FunIndex !Index + | GCB_FunDef !FunDef + | GCB_ParsedBody ![ParsedExpr] !Rhs + :: GenericType = { gt_type :: !SymbolType , gt_vars :: ![TypeVar] // generic arguments , gt_arity :: !Int // number of generic arguments } -:: GenericClassInfo = - { gci_kind :: !TypeKind - , gci_class :: !DefinedSymbol - } -:: GenericClassInfos :== [GenericClassInfo] -getGenericClassForKind :: !GenericDef !TypeKind -> (!Bool, DefinedSymbol) -addGenericKind :: !GenericDef !TypeKind -> !GenericDef +//getGenericClassForKind :: !GenericDef !TypeKind -> (!Bool, DefinedSymbol) +//addGenericKind :: !GenericDef !TypeKind -> !GenericDef // ... AA @@ -324,10 +361,7 @@ addGenericKind :: !GenericDef !TypeKind -> !GenericDef , ins_members :: !{# DefinedSymbol} , ins_specials :: !Specials , ins_pos :: !Position - , ins_is_generic :: !Bool //AA - , ins_generate :: !Bool //AA - , ins_partial :: !Bool //AA - , ins_generic :: !Global Index //AA + , ins_generated :: !Bool //AA } /* @@ -395,6 +429,7 @@ cIsAnalysed :== 4 { gi_module ::!Int , gi_index ::!Int } +NoGlobalIndex :== {gi_module=NoIndex,gi_index=NoIndex} :: TypeDef type_rhs = { td_name :: !Ident @@ -418,8 +453,17 @@ cIsAnalysed :== 4 , tdi_cons_vars :: ![Int] , tdi_index_in_group :: !Index , tdi_classification :: !TypeClassification + , tdi_mark :: !Bool //AA + , tdi_gen_rep :: !Optional GenericTypeRep //AA } +// AA.. +:: GenericTypeRep = + { gtr_type :: AType // generic structure type + , gtr_iso :: DefinedSymbol // the conversion isomorphism + } +// ..AA + :: TypeDefInfos :== {# .{# TypeDefInfo}} :: FunType = @@ -495,6 +539,7 @@ FI_HasTypeSpec :== 2 // whether the function has u user defined type | TransformedBody !TransformedBody | Expanding ![FreeVar] // the parameters of the newly generated function | BackendBody ![BackendBody] + | GeneratedBody // the body will be generated automatically - for generics | NoBody :: BackendBody = @@ -900,6 +945,7 @@ cNonRecursiveAppl :== False | TVI_Kind !TypeKind | TVI_ConsInstance !DefinedSymbol //AA: generic cons instance function | TVI_Normalized !Int /* MV - position of type variable in its definition */ + | TVI_Expr !Expression /* AA: Expression corresponding to the type var during generic specialization */ :: TypeVarInfoPtr :== Ptr TypeVarInfo :: TypeVarHeap :== Heap TypeVarInfo @@ -951,7 +997,7 @@ cNonRecursiveAppl :== False :: BasicValue = BVI !String | BVInt !Int |BVC !String | BVB !Bool | BVR !String | BVS !String -:: TypeKind = KindVar !KindInfoPtr | KindConst | KindArrow ![TypeKind] | KindCycle +:: TypeKind = KindVar !KindInfoPtr | KindConst | KindArrow ![TypeKind] | KindCycle | KindError instance toString TypeKind instance <<< TypeKind @@ -1299,13 +1345,20 @@ cNotALineNumber :== -1 instance == ModuleKind, Ident instance <<< (Module a) | <<< a, ParsedDefinition, InstanceType, AttributeVar, TypeVar, SymbolType, Expression, Type, Ident, (Global object) | <<< object, - Position, CaseAlt, AType, FunDef, ParsedExpr, TypeAttribute, (Bind a b) | <<< a & <<< b, ParsedConstructor, (TypeDef a) | <<< a, TypeVarInfo, + Position, CaseAlt, AType, FunDef, ParsedExpr, TypeAttribute, (Bind a b) | <<< a & <<< b, ParsedConstructor, (TypeDef a) | <<< a, TypeVarInfo, AttrVarInfo, BasicValue, ATypeVar, TypeRhs, FunctionPattern, (Import from_symbol) | <<< from_symbol, ImportDeclaration, ImportedIdent, CasePatterns, (Optional a) | <<< a, ConsVariable, BasicType, Annotation, SelectorKind, Selection, SelectorDef, ConsDef, LocalDefs, FreeVar, ClassInstance, SignClassification, - TypeCodeExpression, CoercionPosition, AttrInequality, LetBind, Declaration, STE_Kind, BoundVar + TypeCodeExpression, CoercionPosition, AttrInequality, LetBind, Declaration, STE_Kind, BoundVar, + TypeSymbIdent, + TypeCons, + IndexRange, + FunType, + GenericClassInfo instance <<< FunctionBody +instance toString BasicType + instance == TypeAttribute instance == Annotation instance == GlobalIndex @@ -1321,7 +1374,7 @@ EmptySymbolTableEntryCAF :: BoxedSymbolTableEntry cNotAGroupNumber :== -1 EmptyTypeDefInfo :== { tdi_kinds = [], tdi_properties = cAllBitsClear, tdi_group = [], tdi_group_vars = [], tdi_cons_vars = [], - tdi_classification = EmptyTypeClassification, tdi_group_nr = cNotAGroupNumber, tdi_index_in_group = NoIndex } + tdi_classification = EmptyTypeClassification, tdi_group_nr = cNotAGroupNumber, tdi_index_in_group = NoIndex, tdi_mark=False, tdi_gen_rep = No } MakeTypeVar name :== { tv_name = name, tv_info_ptr = nilPtr } MakeVar name :== { var_name = name, var_info_ptr = nilPtr, var_expr_ptr = nilPtr } @@ -1361,9 +1414,8 @@ ParsedConstructorToConsDef pc :== ParsedInstanceToClassInstance pi members :== { ins_class = {glob_object = MakeDefinedSymbol pi.pi_class NoIndex (length pi.pi_types), glob_module = NoIndex}, ins_ident = pi.pi_ident, ins_type = { it_vars = [], it_types = pi.pi_types, it_attr_vars = [], - it_context = pi.pi_context }, ins_members = members, ins_specials = pi.pi_specials, ins_pos = pi.pi_pos, - ins_is_generic = False, ins_generate = pi.pi_generate, ins_partial = False, - ins_generic = {glob_module = NoIndex, glob_object = NoIndex}} + it_context = pi.pi_context }, + ins_members = members, ins_specials = pi.pi_specials, ins_pos = pi.pi_pos, ins_generated = False} MakeTypeDef name lhs rhs attr contexts pos :== { td_name = name, td_index = -1, td_arity = length lhs, td_args = lhs, td_attrs = [], td_attribute = attr, td_context = contexts, diff --git a/frontend/syntax.icl b/frontend/syntax.icl index 0c6b9aa..4ad082f 100644 --- a/frontend/syntax.icl +++ b/frontend/syntax.icl @@ -48,7 +48,8 @@ instance == FunctionOrMacroIndex | STE_Field !Ident | STE_Class | STE_Member - | STE_Generic // AA: For generic declarations + | STE_Generic // AA + | STE_GenericCase // AA | STE_Instance !Ident // the class (for explicit imports (1.3 syntax only)) | STE_Variable !VarInfoPtr | STE_TypeVariable !TypeVarInfoPtr @@ -116,9 +117,10 @@ instance == FunctionOrMacroIndex , def_macro_indices :: !IndexRange , def_classes :: ![ClassDef] , def_members :: ![MemberDef] - , def_generics :: ![GenericDef] // AA , def_funtypes :: ![FunType] , def_instances :: ![instance_kind] + , def_generics :: ![GenericDef] // AA + , def_generic_cases :: ![GenericCaseDef] // AA } :: LocalDefs = LocalParsedDefs [ParsedDefinition] | CollectedLocalDefs CollectedLocalDefs @@ -165,11 +167,13 @@ cIsNotAFunction :== False | PD_Type ParsedTypeDef | PD_TypeSpec Position Ident Priority (Optional SymbolType) Specials | PD_Class ClassDef [ParsedDefinition] - | PD_Generic GenericDef // AA | PD_Instance (ParsedInstance ParsedDefinition) | PD_Instances [ParsedInstance ParsedDefinition] | PD_Import [ParsedImport] | PD_ImportedObjects [ImportedObject] + | PD_Generic GenericDef // AA + | PD_GenericCase GenericCaseDef // AA + | PD_Derive [GenericCaseDef] // AA | PD_Erroneous :: StrictnessList = NotStrict | Strict !Int | StrictList !Int StrictnessList @@ -206,7 +210,6 @@ cNameLocationDependent :== True , pi_pos :: !Position , pi_members :: ![member] , pi_specials :: !Specials - , pi_generate :: !Bool // AA: instance is to be generated } @@ -277,28 +280,62 @@ cNameLocationDependent :== True // AA.. :: GenericDef = - { gen_name :: !Ident // the generics name in IC_Class - , gen_member_name :: !Ident // the generics name in IC_Member - , gen_type :: !GenericType + { gen_name :: !Ident // the generics name in IC_Class + , gen_member_name :: !Ident // the generics name in IC_Member , gen_pos :: !Position - , gen_kinds_ptr :: !TypeVarInfoPtr // hack: contains all used kinds - , gen_cons_ptr :: !TypeVarInfoPtr // hack: cons instance function - , gen_classes :: !GenericClassInfos // generated classes - , gen_isomap :: !DefinedSymbol // isomap function + , gen_type :: !SymbolType // Generic type (st_vars include generic type vars) + , gen_vars :: ![TypeVar] // Generic type variables + , gen_info_ptr :: !GenericInfoPtr + , gen_bimap :: !DefinedSymbol // fun def index of the bimap for the generic type } +:: 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_cases :: ![GlobalIndex] + , gen_var_kinds :: ![TypeKind] // kinds of all st_vars of the gen_type + , gen_star_case :: !GlobalIndex // general case for kind-star types + } +:: GenericInfoPtr :== Ptr GenericInfo +:: GenericHeap :== Heap GenericInfo + +:: TypeCons + = TypeConsSymb TypeSymbIdent + | TypeConsBasic BasicType + | TypeConsArrow + | TypeConsVar TypeVar + +:: GenericCaseDef = + { gc_name :: !Ident // name in IC_GenricInstance namespace + , gc_gname :: !Ident // name in IC_Generic namespace + , gc_generic :: !GlobalIndex // index of the generic + , gc_arity :: !Int // number of value arguments + , gc_pos :: !Position // position in the source file + , gc_type :: !Type // the type argument + , gc_type_cons :: !TypeCons // type constructor of the type argument + , gc_body :: !GenericCaseBody // the body function or NoIndex + , gc_kind :: !TypeKind // kind of the instance type + } +:: GenericCaseBody + = GCB_None + | GCB_FunIndex !Index + | GCB_FunDef !FunDef + | GCB_ParsedBody ![ParsedExpr] !Rhs + :: GenericType = { gt_type :: !SymbolType , gt_vars :: ![TypeVar] // generic arguments , gt_arity :: !Int // number of generic arguments } -:: GenericClassInfo = - { gci_kind :: !TypeKind - , gci_class :: !DefinedSymbol - } -:: GenericClassInfos :== [GenericClassInfo] - +/* getGenericClassForKind :: !GenericDef !TypeKind -> (!Bool, DefinedSymbol) getGenericClassForKind {gen_classes} kind = get_class gen_classes kind @@ -319,7 +356,7 @@ addGenericKind generic_def=:{gen_name, gen_classes} kind , ds_arity = 1 } = {generic_def & gen_classes = [{gci_kind = kind, gci_class = class_ds}:gen_classes]} - +*/ // ..AA :: InstanceType = @@ -336,10 +373,7 @@ addGenericKind generic_def=:{gen_name, gen_classes} kind , ins_members :: !{# DefinedSymbol} , ins_specials :: !Specials , ins_pos :: !Position - , ins_is_generic :: !Bool //AA - , ins_generate :: !Bool //AA - , ins_partial :: !Bool //AA - , ins_generic :: !Global Index //AA + , ins_generated :: !Bool // AA } :: Import from_symbol = @@ -400,6 +434,7 @@ cIsAbstractType :== 8 { gi_module ::!Int , gi_index ::!Int } +NoGlobalIndex :== {gi_module=NoIndex,gi_index=NoIndex} instance == GlobalIndex where @@ -485,6 +520,7 @@ FI_HasTypeSpec :== 2 // whether the function has u user defined type | TransformedBody !TransformedBody | Expanding ![FreeVar] // the parameters of the newly generated function | BackendBody ![BackendBody] + | GeneratedBody // the body will be generated automatically - for generics | NoBody :: BackendBody = @@ -883,6 +919,7 @@ cNotVarNumber :== -1 | TVI_Kind !TypeKind | TVI_ConsInstance !DefinedSymbol //AA: generic cons instance function | TVI_Normalized !Int /* MV - position of type variable in its definition */ + | TVI_Expr !Expression /* AA: Expression corresponding to the type var during generic specialization */ :: TypeVarInfoPtr :== Ptr TypeVarInfo :: TypeVarHeap :== Heap TypeVarInfo @@ -935,7 +972,7 @@ cNotVarNumber :== -1 :: BasicValue = BVI !String | BVInt !Int |BVC !String | BVB !Bool | BVR !String | BVS !String -:: TypeKind = KindVar !KindInfoPtr | KindConst | KindArrow ![TypeKind] | KindCycle +:: TypeKind = KindVar !KindInfoPtr | KindConst | KindArrow ![TypeKind] | KindCycle | KindError :: PatternVar = { pv_var :: !FreeVar @@ -985,7 +1022,16 @@ cNotVarNumber :== -1 , tdi_cons_vars :: ![Int] , tdi_index_in_group :: !Index , tdi_classification :: !TypeClassification + , tdi_mark :: !Bool //AA + , tdi_gen_rep :: !Optional GenericTypeRep //AA + } + +// AA.. +:: GenericTypeRep = + { gtr_type :: AType // generic structure type + , gtr_iso :: DefinedSymbol // the conversion isomorphism } +// ..AA :: TypeDefInfos :== {# .{# TypeDefInfo}} @@ -1319,16 +1365,19 @@ instance needs_brackets a where needs_brackets _ = False +instance toString BasicType where + toString BT_Int = "Int" + toString BT_Char = "Char" + toString BT_Real = "Real" + toString BT_Bool = "Bool" + toString (BT_String _) = "String" + toString BT_Dynamic = "Dynamic" + toString BT_File = "File" + toString BT_World = "World" + instance <<< BasicType where - (<<<) file BT_Int = file <<< "Int" - (<<<) file BT_Char = file <<< "Char" - (<<<) file BT_Real = file <<< "Real" - (<<<) file BT_Bool = file <<< "Bool" -/* (<<<) file (BT_String _) = file <<< "String" */ - (<<<) file BT_Dynamic = file <<< "Dynamic" - (<<<) file BT_File = file <<< "File" - (<<<) file BT_World = file <<< "World" + (<<<) file bt = file <<< toString bt instance <<< TypeVar where @@ -1350,6 +1399,13 @@ where (<<<) file {at_attribute,at_type} = file <<< at_attribute <<< at_type +instance <<< TypeCons +where + (<<<) file (TypeConsSymb name) = file <<< name + (<<<) file (TypeConsBasic basic_type) = file <<< basic_type + (<<<) file TypeConsArrow = file <<< "(->)" + (<<<) file (TypeConsVar tv) = file <<< tv + instance <<< TypeAttribute where (<<<) file ta @@ -1575,7 +1631,10 @@ instance <<< Expression where (<<<) file (Var ident) = file <<< ident (<<<) file (App {app_symb, app_args, app_info_ptr}) - = file <<< app_symb <<< ' ' <<< app_args + = case app_symb.symb_kind of + SK_Generic _ kind + -> file <<< app_symb <<< kind <<< ' ' <<< app_args + _ -> file <<< app_symb <<< ' ' <<< app_args (<<<) file (f_exp @ a_exp) = file <<< '(' <<< f_exp <<< " @ " <<< a_exp <<< ')' (<<<) file (Let {let_info_ptr, let_strict_binds, let_lazy_binds, let_expr}) = write_binds "" (write_binds "!" (file <<< "let" <<< '\n') let_strict_binds) let_lazy_binds <<< "in\n" <<< let_expr @@ -1781,6 +1840,10 @@ where (<<<) file FK_Caf = file <<< "FK_Caf" (<<<) file FK_Unknown = file <<< "FK_Unknown" +instance <<< FunType +where + (<<<) file {ft_symb,ft_type} = file <<< ft_symb <<< "::" <<< ft_type + instance <<< FunDef where (<<<) file {fun_symb,fun_body=ParsedBody bodies} = file <<< fun_symb <<< '.' <<< ' ' <<< bodies @@ -1806,7 +1869,9 @@ where (<<<) file (TransformedBody {tb_args,tb_rhs}) = file <<< "T " <<< tb_args <<< " = " <<< tb_rhs <<< '\n' (<<<) file (BackendBody body) = file <<< body <<< '\n' (<<<) file (Expanding vars) = file <<< "E " <<< vars + (<<<) file GeneratedBody = file <<< "Generic function\n" (<<<) file NoBody = file <<< "Array function\n" + instance <<< FunCall where @@ -1843,7 +1908,6 @@ where = write_signs (file <<< '+') (sc_pos_vect bitand (bitnot index_bit)) sc_neg_vect (inc index) = write_signs (file <<< 'T') (sc_pos_vect bitand (bitnot index_bit)) (sc_neg_vect bitand (bitnot index_bit)) (inc index) -// AA.. instance toString TypeKind where toString (KindVar _) = "**" @@ -1854,8 +1918,6 @@ where to_string [k] = toString k to_string [k:ks] = (toString k) +++ "->" +++ (to_string ks) -// ..AA - instance <<< TypeKind where @@ -1921,6 +1983,10 @@ where = write_data_defs (file <<< d <<< '\n') ds */ +instance <<< GenericClassInfo +where + (<<<) file {gci_kind, gci_class} = file <<< gci_kind <<< ":" <<< gci_class + instance <<< InstanceType where (<<<) file it = write_contexts it.it_context (file <<< it.it_types) @@ -1981,6 +2047,9 @@ where (<<<) file (PD_NodeDef _ pattern rhs) = file <<< pattern <<< " =: " <<< rhs (<<<) file (PD_TypeSpec _ name prio st sp) = file <<< name <<< st (<<<) file (PD_Type td) = file <<< td + (<<<) file (PD_Generic {gen_name}) = file <<< "generic " <<< gen_name + (<<<) file (PD_GenericCase {gc_name,gc_type_cons}) = file <<< gc_name <<< "{|" <<< gc_type_cons <<< "|}" + (<<<) file _ = file instance <<< Rhs @@ -2027,14 +2096,35 @@ instance <<< TypeVarInfo where (<<<) file TVI_Empty = file <<< "TVI_Empty" (<<<) file (TVI_Type _) = file <<< "TVI_Type" + (<<<) file (TVI_TypeVar ptr) = file <<< (ptrToInt ptr) (<<<) file (TVI_Forward _) = file <<< "TVI_Forward" - (<<<) file (TVI_TypeKind _) = file <<< "TVI_TypeKind" (<<<) file (TVI_SignClass _ _ _) = file <<< "TVI_SignClass" + (<<<) file (TVI_Attribute ta) = file <<< "TVI_Attribute " <<< ta + (<<<) file (TVI_CorrespondenceNumber n) = file <<< "TVI_CorrespondenceNumber " <<< n + (<<<) file (TVI_AType at) = file <<< "TVI_AType " <<< at + (<<<) file TVI_Used = file <<< "TVI_Used" + (<<<) file (TVI_TypeCode _) = file <<< "TVI_TypeCode" + (<<<) file (TVI_CPSLocalTypeVar _) = file <<< "TVI_CPSLocalTypeVar" + (<<<) file (TVI_Kinds _) = file <<< "TVI_Kinds" + (<<<) file (TVI_TypeKind _) = file <<< "TVI_TypeKind" (<<<) file (TVI_PropClass _ _ _) = file <<< "TVI_PropClass" (<<<) file (TVI_TypeKind kind_info_ptr) = file <<< "TVI_TypeKind " <<< (ptrToInt kind_info_ptr) (<<<) file (TVI_Kind kind) = file <<< "TVI_Kind" <<< kind - + (<<<) file (TVI_Expr expr) = file <<< "TVI_Expr " <<< expr +instance <<< AttrVarInfo +where + (<<<) file AVI_Empty = file <<< "AVI_Empty" + (<<<) file (AVI_Attr attr) = file <<< "AVI_Attr " <<< attr + (<<<) file (AVI_AttrVar av_info_ptr) = file <<< "AVI_AttrVar " <<< ptrToInt av_info_ptr + (<<<) file (AVI_Forward temp_attr_id) = file <<< "AVI_Forward " <<< temp_attr_id + (<<<) file (AVI_CorrespondenceNumber n) = file <<< "AVI_CorrespondenceNumber " <<< n + (<<<) file AVI_Used = file <<< "AVI_Used" + (<<<) file (AVI_Count n) = file <<< "AVI_Count " <<< n + (<<<) file (AVI_SequenceNumber n) = file <<< "AVI_SequenceNumber " <<< n + (<<<) file AVI_Collected = file <<< "AVI_Collected" + + instance <<< (Import from_symbol) | <<< from_symbol where (<<<) file {import_module, import_symbols} @@ -2104,6 +2194,9 @@ where (<<<) file STE_Generic = file <<< "STE_Generic" + (<<<) file + STE_GenericCase + = file <<< "STE_GenericCase" // ..AA (<<<) file (STE_Field _) @@ -2196,7 +2289,7 @@ abort_empty_SymbolTableEntry = abort "empty SymbolTableEntry" cNotAGroupNumber :== -1 EmptyTypeDefInfo :== { tdi_kinds = [], tdi_properties = cAllBitsClear, tdi_group = [], tdi_group_vars = [], tdi_cons_vars = [], - tdi_classification = EmptyTypeClassification, tdi_group_nr = cNotAGroupNumber, tdi_index_in_group = NoIndex } + tdi_classification = EmptyTypeClassification, tdi_group_nr = cNotAGroupNumber, tdi_index_in_group = NoIndex, tdi_mark=False, tdi_gen_rep = No } MakeTypeVar name :== { tv_name = name, tv_info_ptr = nilPtr } MakeVar name :== { var_name = name, var_info_ptr = nilPtr, var_expr_ptr = nilPtr } @@ -2241,12 +2334,8 @@ ParsedConstructorToConsDef pc :== ParsedInstanceToClassInstance pi members :== { ins_class = {glob_object = MakeDefinedSymbol pi.pi_class NoIndex (length pi.pi_types), glob_module = NoIndex}, ins_ident = pi.pi_ident, ins_type = { it_vars = [], it_types = pi.pi_types, it_attr_vars = [], - it_context = pi.pi_context }, ins_members = members, ins_specials = pi.pi_specials, ins_pos = pi.pi_pos, - /*AA*/ - ins_is_generic = False, - ins_generate = pi.pi_generate, - ins_partial = False, - ins_generic = {glob_module = NoIndex, glob_object = NoIndex}} + it_context = pi.pi_context }, + ins_members = members, ins_specials = pi.pi_specials, ins_pos = pi.pi_pos, ins_generated = False} MakeTypeDef name lhs rhs attr contexts pos :== { td_name = name, td_index = -1, td_arity = length lhs, td_args = lhs, td_attrs = [], td_attribute = attr, td_context = contexts, diff --git a/frontend/transform.icl b/frontend/transform.icl index d5d3e73..caa94e2 100644 --- a/frontend/transform.icl +++ b/frontend/transform.icl @@ -1341,7 +1341,24 @@ where // {pi & pi_next_group = pi.pi_next_group} )) -> (max_fun_nr, (modules, pi)) - + GeneratedBody +/* + // allocate a group that contains this and only this function + | fun_def.fun_info.fi_group_index == NoIndex + # pi = + { pi + & pi_fun_defs.[fun_index] = + { fun_def + & fun_info.fi_group_index = pi.pi_next_group + } + , pi_groups = [[FunctionOrIclMacroIndex fun_index] : pi.pi_groups] + , pi_next_group = inc pi.pi_next_group + } + -> (max_fun_nr, (modules, pi)) + -> abort ("generated function already has a group index: " +++ toString fun_def.fun_symb +++ " " +++ toString fun_index +++ "\n") +*/ + // do not allocate a group, it will be allocated during generic phase + -> (max_fun_nr, (modules, pi)) partitionate_macro mod_index max_fun_nr macro_module_index macro_index (modules, pi) # (fun_def, pi) = pi!pi_macro_defs.[macro_module_index,macro_index] = case fun_def.fun_body of diff --git a/frontend/type.icl b/frontend/type.icl index aa7f448..b09a9c5 100644 --- a/frontend/type.icl +++ b/frontend/type.icl @@ -3,7 +3,7 @@ implementation module type import StdEnv import syntax, typesupport, check, analtypes, overloading, unitype, refmark, predef, utilities, compare_constructor // , RWSDebug import compilerSwitches -import generics // AA +import genericsupport // AA :: TypeInput = { ti_common_defs :: !{# CommonDefs } @@ -18,6 +18,7 @@ import generics // AA , ts_var_heap :: !.VarHeap , ts_type_heaps :: !.TypeHeaps , ts_expr_heap :: !.ExpressionHeap + , ts_generic_heap :: !.GenericHeap , ts_td_infos :: !.TypeDefInfos , ts_cons_variables :: ![TempVarId] , ts_exis_variables :: ![(CoercionPosition, [TempAttrId])] @@ -570,8 +571,12 @@ freshConsVariable {tv_info_ptr} type_var_heap -> TempCV temp_var_id TempQV temp_var_id -> TempQCV temp_var_id - TV var + TV var -> CV var + _ + -> abort "type.icl: to_constructor_variable, fresh_type\n" ---> fresh_type + to_constructor_variable tvi + = abort "type.icl: to_constructor_variable, tvi\n" ---> tvi instance freshCopy AType where @@ -884,6 +889,7 @@ freshSymbolType is_appl fresh_context_vars st=:{st_vars,st_args,st_result,st_con -> (inc attr_store, [attr_store : exis_variables], [av_info_ptr : bound_attr_vars], attr_heap <:= (av_info_ptr, AVI_Attr (TA_TempVar attr_store))) AVI_Attr (TA_TempVar _) -> (attr_store, exis_variables, bound_attr_vars, attr_heap) + _ -> (abort "invalid av_info") ---> ("freshSymbolType av_info", var, av_info) fresh_attr attr state = state @@ -1235,16 +1241,20 @@ getSymbolType pos ti=:{ti_functions,ti_common_defs,ti_main_dcl_module_n} {symb_k _ -> abort ("getSymbolType SK_LocalMacroFunction: "+++toString symb_name+++" " +++toString glob_object) // -> abort "getSymbolType (type.icl)" ---> (symb_name, glob_object, fun_type) -getSymbolType pos ti=:{ti_common_defs} {symb_kind = SK_OverloadedFunction {glob_module,glob_object}} n_app_args ts +getSymbolType pos ti=:{ti_common_defs} { symb_kind = SK_OverloadedFunction {glob_module,glob_object}} n_app_args ts # {me_symb, me_type,me_type_ptr} = ti_common_defs.[glob_module].com_member_defs.[glob_object] (fun_type_copy, ts) = determineSymbolTypeOfFunction pos me_symb n_app_args me_type me_type_ptr ti_common_defs ts = (fun_type_copy, [], ts) // AA.. -getSymbolType pos ti=:{ti_common_defs} symbol=:{symb_kind = SK_Generic gen_glob kind} n_app_args ts - # (found, member_glob) = getGenericMember gen_glob kind ti_common_defs - | not found - = abort "getSymbolType: no class for kind" - = getSymbolType pos ti {symbol & symb_kind = SK_OverloadedFunction member_glob} n_app_args ts +getSymbolType pos ti=:{ti_common_defs} symbol=:{symb_name, symb_kind = SK_Generic gen_glob kind} n_app_args ts + # (opt_member_glob, ts_generic_heap) = getGenericMember gen_glob kind ti_common_defs ts.ts_generic_heap + # ts = { ts & ts_generic_heap = ts_generic_heap } + = case opt_member_glob of + No + # empty_tst = {tst_args=[], tst_arity=0, tst_lifted=0, tst_result={at_type=TE,at_attribute=TA_Multi}, tst_context=[], tst_attr_env=[]} + # ts_error = checkError ("no generic instances of " +++ toString symb_name +++ " for kind") kind ts.ts_error + -> (empty_tst, [], {ts & ts_error = ts_error}) + Yes member_glob -> getSymbolType pos ti {symbol & symb_kind = SK_OverloadedFunction member_glob} n_app_args ts // ..AA class requirements a :: !TypeInput !a !(!u:Requirements, !*TypeState) -> (!AType, !Optional ExprInfoPtr, !(!u:Requirements, !*TypeState)) @@ -2109,7 +2119,7 @@ ste_kind_to_string s typeProgram ::!{! Group} !Int !*{# FunDef} !IndexRange !(Optional Bool) !CommonDefs ![Declaration] !{# DclModule} !NumberSet !*TypeDefInfos !*Heaps !*PredefinedSymbols !*File !*File !{# DclModule} -> (!Bool, !*{# FunDef}, !ArrayAndListInstances, {! GlobalTCType}, !{# CommonDefs}, !{# {# FunType} }, !*TypeDefInfos, !*Heaps, !*PredefinedSymbols, !*File, !*File) -typeProgram comps main_dcl_module_n fun_defs specials list_inferred_types icl_defs imports modules used_module_numbers td_infos heaps=:{hp_var_heap, hp_expression_heap, hp_type_heaps} predef_symbols file out dcl_modules +typeProgram comps main_dcl_module_n fun_defs specials list_inferred_types icl_defs imports modules used_module_numbers td_infos heaps=:{hp_var_heap, hp_expression_heap, hp_type_heaps,hp_generic_heap} predef_symbols file out dcl_modules #! fun_env_size = size fun_defs @@ -2123,13 +2133,13 @@ typeProgram comps main_dcl_module_n fun_defs specials list_inferred_types icl_de state = collect_imported_instances imports ti_common_defs {} ts_error class_instances hp_type_heaps.th_vars td_infos (_, ts_error, class_instances, th_vars, td_infos) = collect_and_check_instances (size icl_defs.com_instance_defs) ti_common_defs state - ts = { ts_fun_env = InitFunEnv fun_env_size, ts_var_heap = hp_var_heap, ts_expr_heap = hp_expression_heap, ts_var_store = 0, ts_attr_store = FirstAttrVar, ts_cons_variables = [], ts_exis_variables = [], + ts = { ts_fun_env = InitFunEnv fun_env_size, ts_var_heap = hp_var_heap, ts_expr_heap = hp_expression_heap, ts_generic_heap = hp_generic_heap, ts_var_store = 0, ts_attr_store = FirstAttrVar, ts_cons_variables = [], ts_exis_variables = [], ts_type_heaps = { hp_type_heaps & th_vars = th_vars }, ts_td_infos = td_infos, ts_error = ts_error, ts_out = out } ti = { ti_common_defs = ti_common_defs, ti_functions = ti_functions,ti_main_dcl_module_n=main_dcl_module_n } special_instances = { si_next_array_member_index = fun_env_size, si_array_instances = [], si_list_instances = [], si_tail_strict_list_instances = [], si_next_TC_member_index = 0, si_TC_instances = [] } # (type_error, fun_defs, predef_symbols, special_instances, ts) = type_components list_inferred_types 0 comps class_instances ti (False, fun_defs, predef_symbols, special_instances, ts) (fun_defs,ts_fun_env) = update_function_types 0 comps ts.ts_fun_env fun_defs - (type_error, fun_defs, predef_symbols, special_instances, {ts_td_infos,ts_fun_env,ts_error,ts_var_heap, ts_expr_heap, ts_type_heaps, ts_out}) + (type_error, fun_defs, predef_symbols, special_instances, {ts_td_infos,ts_fun_env,ts_error,ts_var_heap, ts_expr_heap, ts_type_heaps, ts_generic_heap,ts_out}) = type_instances list_inferred_types specials.ir_from specials.ir_to class_instances ti (type_error, fun_defs, predef_symbols, special_instances, { ts & ts_fun_env = ts_fun_env }) (array_first_instance_indices,list_first_instance_indices,tail_strict_list_first_instance_indices,fun_defs,type_code_instances,predef_symbols,ts_type_heaps,ts_error) @@ -2141,7 +2151,7 @@ typeProgram comps main_dcl_module_n fun_defs specials list_inferred_types icl_de ali_instances_range={ ir_from = fun_env_size, ir_to = special_instances.si_next_array_member_index } } = (not type_error, fun_defs, array_and_list_instances, type_code_instances, ti_common_defs, ti_functions, - ts_td_infos, {hp_var_heap = ts_var_heap, hp_expression_heap = ts_expr_heap, hp_type_heaps = ts_type_heaps }, + ts_td_infos, {hp_var_heap = ts_var_heap, hp_expression_heap = ts_expr_heap, hp_type_heaps = ts_type_heaps, hp_generic_heap=ts_generic_heap }, predef_symbols, ts_error.ea_file, ts_out) // ---> ("typeProgram", array_inst_types) where @@ -2284,14 +2294,15 @@ where coercion_env = build_initial_coercion_env fun_reqs {coer_demanded = coer_demanded, coer_offered = coer_offered } (over_info, (subst, ts_expr_heap)) = collect_and_expand_overloaded_calls fun_reqs [] (subst, ts_expr_heap) (contexts, coercion_env, local_pattern_variables, dict_types, - { os_type_heaps, os_var_heap, os_symbol_heap, os_predef_symbols, os_special_instances, os_error }) + { os_type_heaps, os_var_heap, os_symbol_heap, os_generic_heap, os_predef_symbols, os_special_instances, os_error }) = tryToSolveOverloading over_info main_dcl_module_n ti_common_defs class_instances coercion_env - { os_type_heaps = ts_type_heaps, os_var_heap = ts_var_heap, os_symbol_heap = ts_expr_heap, + { os_type_heaps = ts_type_heaps, os_var_heap = ts_var_heap, os_symbol_heap = ts_expr_heap, os_generic_heap = ts.ts_generic_heap, os_predef_symbols = predef_symbols, os_error = ts_error, os_special_instances = special_instances } modules + //ts = {ts & ts_generic_heap = os_generic_heap} | not os_error.ea_ok = (True, fun_defs, os_predef_symbols, os_special_instances, create_erroneous_function_types comp { ts & ts_type_heaps = os_type_heaps, ts_error = { os_error & ea_ok = True }, ts_var_store = 0, ts_attr_store = FirstAttrVar, ts_cons_variables = [], ts_exis_variables = [], - ts_td_infos = ts_td_infos, ts_expr_heap = os_symbol_heap, ts_var_heap = os_var_heap }) + ts_td_infos = ts_td_infos, ts_expr_heap = os_symbol_heap, ts_var_heap = os_var_heap,ts_generic_heap=os_generic_heap}) # (fun_defs, coercion_env, subst, ts_td_infos, os_var_heap, os_symbol_heap, os_error) = makeSharedReferencesNonUnique comp fun_defs coercion_env subst ts_td_infos os_var_heap os_symbol_heap os_error (subst, coercions, ts_td_infos, ts_type_heaps, ts_error) @@ -2306,10 +2317,10 @@ where var_env = { subst & [i] = TE \\ i <- [0..dec ts_var_store]} (fun_defs, ts) = cleanUpAndCheckFunctionTypes comp fun_reqs dict_types start_index list_inferred_types ti_common_defs contexts coer_demanded attr_partition var_env attr_var_env (fun_defs, { ts & ts_error = ts_error, ts_fun_env = ts_fun_env, ts_type_heaps = ts_type_heaps, - ts_td_infos = ts_td_infos, ts_var_heap = os_var_heap, ts_expr_heap = os_symbol_heap }) + ts_td_infos = ts_td_infos, ts_var_heap = os_var_heap, ts_expr_heap = os_symbol_heap,ts_generic_heap=os_generic_heap}) | not ts.ts_error.ea_ok = (True, fun_defs, os_predef_symbols, os_special_instances, create_erroneous_function_types comp - { ts & ts_var_store = 0, ts_attr_store = FirstAttrVar, ts_cons_variables = [], ts_exis_variables = [], ts_error = { ts.ts_error & ea_ok = True } }) + { ts & ts_var_store = 0, ts_attr_store = FirstAttrVar, ts_cons_variables = [], ts_exis_variables = [], ts_error = { ts.ts_error & ea_ok = True }}) | isEmpty over_info # ts_type_heaps = ts.ts_type_heaps type_code_info = { tci_next_index = os_special_instances.si_next_TC_member_index, tci_instances = os_special_instances.si_TC_instances, diff --git a/frontend/type_io_common.dcl b/frontend/type_io_common.dcl index f466789..452244c 100644 --- a/frontend/type_io_common.dcl +++ b/frontend/type_io_common.dcl @@ -77,8 +77,6 @@ LowLevelInterfaceModule :== "StdDynamicLowLevelInterface" instance toString GlobalTCType -instance toString BasicType - create_type_string type_name module_name :== type_name +++ (APPEND_DEFINING_TYPE_MODULE_NAMES_TO_TYPE_NAMES ("'" +++ module_name ) "") diff --git a/frontend/type_io_common.icl b/frontend/type_io_common.icl index 4e575a4..fc5283a 100644 --- a/frontend/type_io_common.icl +++ b/frontend/type_io_common.icl @@ -82,17 +82,6 @@ where toString (GTT_Constructor type_symb_indent mod_name) = create_type_string type_symb_indent.type_name.id_name mod_name // +++ (APPEND_DEFINING_TYPE_MODULE_NAMES_TO_TYPE_NAMES ("'" +++ mod_name) "") -instance toString BasicType -where - toString BT_Int = "Int" - toString BT_Char = "Char" - toString BT_Real = "Real" - toString BT_Bool = "Bool" - toString BT_Dynamic = "Dynamic" - toString BT_File = "File" - toString BT_World = "World" - toString (BT_String _) = "String" - create_type_string type_name module_name :== type_name +++ (APPEND_DEFINING_TYPE_MODULE_NAMES_TO_TYPE_NAMES ("'" +++ module_name ) "") diff --git a/frontend/typesupport.icl b/frontend/typesupport.icl index 9ef6cf0..b20f74b 100644 --- a/frontend/typesupport.icl +++ b/frontend/typesupport.icl @@ -655,6 +655,7 @@ where bind_attribute (TA_Var {av_info_ptr}) attr th_attrs = th_attrs <:= (av_info_ptr, AVI_Attr attr) + ---> ("typesupport 1 writePtr av_info_ptr", ptrToInt av_info_ptr, attr) bind_attribute _ _ th_attrs = th_attrs diff --git a/main/compile.icl b/main/compile.icl index 76a7131..84d37f0 100644 --- a/main/compile.icl +++ b/main/compile.icl @@ -23,7 +23,7 @@ from type_io import openTclFile, closeTclFile, baseName, directoryName, splitBy , searchPaths:: SearchPaths , listTypes :: ListTypesOption , compile_for_dynamics :: !Bool - , support_generics :: !Bool + , support_generics :: !Bool , compile_with_fusion :: !Bool , compile_with_generics :: !Bool } @@ -38,9 +38,9 @@ InitialCoclOptions = , searchPaths= {sp_locations = [], sp_paths = []} , listTypes = {lto_showAttributes = True, lto_listTypesKind = ListTypesNone} , compile_for_dynamics = False - , support_generics = False + , support_generics = True //??? , compile_with_fusion = False - , compile_with_generics = False + , compile_with_generics = True } :: DclCache = { @@ -53,7 +53,7 @@ InitialCoclOptions = empty_cache :: *SymbolTable -> *DclCache empty_cache symbol_heap - # heaps = {hp_var_heap = newHeap, hp_expression_heap = newHeap, hp_type_heaps = {th_vars = newHeap, th_attrs = newHeap}} + # heaps = {hp_var_heap = newHeap, hp_expression_heap = newHeap, hp_type_heaps = {th_vars = newHeap, th_attrs = newHeap}, hp_generic_heap = newHeap} # (predef_symbols, hash_table) = buildPredefinedSymbols (newHashTable symbol_heap) = {dcl_modules={},functions_and_macros={},predef_symbols=predef_symbols,hash_table=hash_table,heaps=heaps} |