2 /* --------------------------------------------------------------------------
3 * GHC interface file processing for Hugs
5 * Copyright (c) The University of Nottingham and Yale University, 1994-1997.
6 * All rights reserved. See NOTICE for details and conditions of use etc...
7 * Hugs version 1.4, December 1997
9 * $RCSfile: interface.c,v $
11 * $Date: 2000/03/10 14:53:00 $
12 * ------------------------------------------------------------------------*/
20 #include "Assembler.h" /* for wrapping GHC objects */
24 /*#define DEBUG_IFACE*/
27 /* --------------------------------------------------------------------------
28 * (This comment is now out of date. JRS, 991216).
29 * The "addGHC*" functions act as "impedence matchers" between GHC
30 * interface files and Hugs. Their main job is to convert abstract
31 * syntax trees into Hugs' internal representations.
33 * The main trick here is how we deal with mutually recursive interface
36 * o As we read an import decl, we add it to a list of required imports
37 * (unless it's already loaded, of course).
39 * o Processing of declarations is split into two phases:
41 * 1) While reading the interface files, we construct all the Names,
42 * Tycons, etc declared in the interface file but we don't try to
43 * resolve references to any entities the declaration mentions.
45 * This is done by the "addGHC*" functions.
47 * 2) After reading all the interface files, we finish processing the
48 * declarations by resolving any references in the declarations
49 * and doing any other processing that may be required.
51 * This is done by the "finishGHC*" functions which use the
52 * "fixup*" functions to assist them.
54 * The interface between these two phases are the "ghc*Decls" which
55 * contain lists of decls that haven't been completed yet.
57 * ------------------------------------------------------------------------*/
61 New comment, 991216, explaining roughly how it all works.
62 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64 Interfaces can contain references to unboxed types, and these need to
65 be handled carefully. The following is a summary of how the interface
66 loader now works. It is applied to groups of interfaces simultaneously,
67 viz, the entire Prelude at once:
69 0. Parse interfaces, chasing imports until a complete
70 strongly-connected-component of ifaces has been parsed.
71 All interfaces in this scc are processed together, in
74 1. Throw away any entity not mentioned in the export lists.
76 2. Delete type (not data or newtype) definitions which refer to
77 unknown types in their right hand sides. Because Hugs doesn't
78 know of any unboxed types, this has the side effect of removing
79 all type defns referring to unboxed types. Repeat step 2 until
80 a fixed point is reached.
82 3. Make abstract all data/newtype defns which refer to an unknown
83 type. eg, data Word = MkW Word# becomes data Word, because
84 Word# is unknown. Hugs is happy to know about abstract boxed
85 Words, but not about Word#s.
87 4. Step 2 could delete types referred to by values, instances and
88 classes. So filter all entities, and delete those referring to
89 unknown types _or_ classes. This could cause other entities
90 to become invalid, so iterate step 4 to a fixed point.
92 After step 4, the interfaces no longer contain anything
95 5. Steps 1-4 operate purely on the iface syntax trees. We now start
96 creating symbol table entries. First, create a module table
97 entry for each interface, and locate and read in the corresponding
98 object file. This is done by the startGHCModule function.
100 6. Traverse all interfaces. For each entity, create an entry in
101 the name, tycon, class or instance table, and fill in relevant
102 fields, but do not attempt to link tycon/class/instance/name uses
103 to their symbol table entries. This is done by the startGHC*
106 7. Revisit all symbol table entries created in step 6. We should
107 now be able to replace all references to tycons/classes/instances/
108 names with the relevant symbol table entries. This is done by
109 the finishGHC* functions.
111 8. Traverse all interfaces. For each iface, examine the export lists
112 and use it to build export lists in the module table. Do the
113 implicit 'import Prelude' thing if necessary. Finally, resolve
114 references in the object code for this module. This is done
115 by the finishGHCModule function.
118 /* --------------------------------------------------------------------------
119 * local function prototypes:
120 * ------------------------------------------------------------------------*/
122 static Void startGHCValue Args((Int,VarId,Type));
123 static Void finishGHCValue Args((VarId));
125 static Void startGHCSynonym Args((Int,Cell,List,Type));
126 static Void finishGHCSynonym Args((Tycon));
128 static Void startGHCClass Args((Int,List,Cell,List,List));
129 static Class finishGHCClass Args((Class));
131 static Inst startGHCInstance Args((Int,List,Pair,VarId));
132 static Void finishGHCInstance Args((Inst));
134 static Void startGHCImports Args((ConId,List));
135 static Void finishGHCImports Args((ConId,List));
137 static Void startGHCExports Args((ConId,List));
138 static Void finishGHCExports Args((ConId,List));
140 static Void finishGHCFixdecl ( Cell prec, Cell assoc, ConVarId name );
142 static Void finishGHCModule Args((Cell));
143 static Void startGHCModule Args((Text, Int, Text));
145 static Void startGHCDataDecl Args((Int,List,Cell,List,List));
146 static List finishGHCDataDecl ( ConId tyc );
148 static Void startGHCNewType Args((Int,List,Cell,List,Cell));
149 static Void finishGHCNewType ( ConId tyc );
152 /* Supporting stuff for {start|finish}GHCDataDecl */
153 static List startGHCConstrs Args((Int,List,List));
154 static Name startGHCSel Args((Int,Pair));
155 static Name startGHCConstr Args((Int,Int,Triple));
159 static Kinds tvsToKind Args((List));
160 static Int arityFromType Args((Type));
161 static Int arityInclDictParams Args((Type));
162 static Bool allTypesKnown ( Type type, List aktys /* [QualId] */, ConId thisMod );
164 static List ifTyvarsIn Args((Type));
166 static Type tvsToOffsets Args((Int,Type,List));
167 static Type conidcellsToTycons Args((Int,Type));
169 static void* lookupObjName ( char* );
175 /* --------------------------------------------------------------------------
176 * Top-level interface processing
177 * ------------------------------------------------------------------------*/
179 /* getIEntityName :: I_IMPORT..I_VALUE -> ConVarId | NIL */
180 static ConVarId getIEntityName ( Cell c )
183 case I_IMPORT: return NIL;
184 case I_INSTIMPORT: return NIL;
185 case I_EXPORT: return NIL;
186 case I_FIXDECL: return zthd3(unap(I_FIXDECL,c));
187 case I_INSTANCE: return NIL;
188 case I_TYPE: return zsel24(unap(I_TYPE,c));
189 case I_DATA: return zsel35(unap(I_DATA,c));
190 case I_NEWTYPE: return zsel35(unap(I_NEWTYPE,c));
191 case I_CLASS: return zsel35(unap(I_CLASS,c));
192 case I_VALUE: return zsnd3(unap(I_VALUE,c));
193 default: internal("getIEntityName");
198 /* Filter the contents of an interface, using the supplied predicate.
199 For flexibility, the predicate is passed as a second arg the value
200 extraArgs. This is a hack to get round the lack of partial applications
201 in C. Pred should not have any side effects. The dumpaction param
202 gives us the chance to print a message or some such for dumped items.
203 When a named entity is deleted, filterInterface also deletes the name
206 static Cell filterInterface ( Cell root,
207 Bool (*pred)(Cell,Cell),
209 Void (*dumpAction)(Cell) )
212 Cell iface = unap(I_INTERFACE,root);
214 List deleted_ids = NIL; /* :: [ConVarId] */
216 for (tops = zsnd(iface); nonNull(tops); tops=tl(tops)) {
217 if (pred(hd(tops),extraArgs)) {
218 tops2 = cons( hd(tops), tops2 );
220 ConVarId deleted_id = getIEntityName ( hd(tops) );
221 if (nonNull(deleted_id))
222 deleted_ids = cons ( deleted_id, deleted_ids );
224 dumpAction ( hd(tops) );
227 tops2 = reverse(tops2);
229 /* Clean up the export list now. */
230 for (tops=tops2; nonNull(tops); tops=tl(tops)) {
231 if (whatIs(hd(tops))==I_EXPORT) {
232 Cell exdecl = unap(I_EXPORT,hd(tops));
233 List exlist = zsnd(exdecl);
235 for (; nonNull(exlist); exlist=tl(exlist)) {
236 Cell ex = hd(exlist);
237 ConVarId exid = isZPair(ex) ? zfst(ex) : ex;
238 assert (isCon(exid) || isVar(exid));
239 if (!varIsMember(textOf(exid),deleted_ids))
240 exlist2 = cons(ex, exlist2);
242 hd(tops) = ap(I_EXPORT,zpair(zfst(exdecl),exlist2));
246 return ap(I_INTERFACE, zpair(zfst(iface),tops2));
250 ZPair readInterface(String fname, Long fileSize)
254 ZPair iface = parseInterface(fname,fileSize);
255 assert (whatIs(iface)==I_INTERFACE);
257 for (tops = zsnd(unap(I_INTERFACE,iface)); nonNull(tops); tops=tl(tops))
258 if (whatIs(hd(tops)) == I_IMPORT) {
259 ZPair imp_decl = unap(I_IMPORT,hd(tops));
260 ConId m_to_imp = zfst(imp_decl);
261 if (textOf(m_to_imp) != findText("PrelGHC")) {
262 imports = cons(m_to_imp,imports);
264 fprintf(stderr, "add iface %s\n",
265 textToStr(textOf(m_to_imp)));
269 return zpair(iface,imports);
273 /* getExportDeclsInIFace :: I_INTERFACE -> [I_EXPORT] */
274 static List getExportDeclsInIFace ( Cell root )
276 Cell iface = unap(I_INTERFACE,root);
277 List decls = zsnd(iface);
280 for (ds=decls; nonNull(ds); ds=tl(ds))
281 if (whatIs(hd(ds))==I_EXPORT)
282 exports = cons(hd(ds), exports);
287 /* Does t start with "$dm" ? */
288 static Bool isIfaceDefaultMethodName ( Text t )
290 String s = textToStr(t);
291 return (s && s[0]=='$' && s[1]=='d' && s[2]=='m' && s[3]);
295 static Bool isExportedIFaceEntity ( Cell ife, List exlist_list )
297 /* ife :: I_IMPORT..I_VALUE */
298 /* exlist_list :: [[ ConVarId | ((ConId, [ConVarId])) ]] */
304 ConVarId ife_id = getIEntityName ( ife );
306 if (isNull(ife_id)) return TRUE;
308 tnm = textOf(ife_id);
310 /* Don't junk default methods, even tho the export list doesn't
313 if (isIfaceDefaultMethodName(tnm)) goto retain;
315 /* for each export list ... */
316 for (; nonNull(exlist_list); exlist_list=tl(exlist_list)) {
317 exlist = hd(exlist_list);
319 /* for each entity in an export list ... */
320 for (t=exlist; nonNull(t); t=tl(t)) {
321 if (isZPair(hd(t))) {
322 /* A pair, which means an export entry
323 of the form ClassName(foo,bar). */
324 List subents = cons(zfst(hd(t)),zsnd(hd(t)));
325 for (; nonNull(subents); subents=tl(subents))
326 if (textOf(hd(subents)) == tnm) goto retain;
328 /* Single name in the list. */
329 if (textOf(hd(t)) == tnm) goto retain;
335 fprintf ( stderr, " dump %s\n", textToStr(tnm) );
341 fprintf ( stderr, " retain %s\n", textToStr(tnm) );
347 static Bool isExportedAbstractly ( ConId ife_id, List exlist_list )
349 /* ife_id :: ConId */
350 /* exlist_list :: [[ ConVarId | ((ConId, [ConVarId])) ]] */
355 assert (isCon(ife_id));
356 tnm = textOf(ife_id);
358 /* for each export list ... */
359 for (; nonNull(exlist_list); exlist_list=tl(exlist_list)) {
360 exlist = hd(exlist_list);
362 /* for each entity in an export list ... */
363 for (t=exlist; nonNull(t); t=tl(t)) {
364 if (isZPair(hd(t))) {
365 /* A pair, which means an export entry
366 of the form ClassName(foo,bar). */
367 if (textOf(zfst(hd(t))) == tnm) return FALSE;
369 if (textOf(hd(t)) == tnm) return TRUE;
373 internal("isExportedAbstractly");
374 return FALSE; /*notreached*/
378 /* Remove entities not mentioned in any of the export lists. */
379 static Cell deleteUnexportedIFaceEntities ( Cell root )
381 Cell iface = unap(I_INTERFACE,root);
382 ConId iname = zfst(iface);
383 List decls = zsnd(iface);
385 List exlist_list = NIL;
389 fprintf(stderr, "\ncleanIFace: %s\n", textToStr(textOf(iname)));
392 exlist_list = getExportDeclsInIFace ( root );
393 /* exlist_list :: [I_EXPORT] */
395 for (t=exlist_list; nonNull(t); t=tl(t))
396 hd(t) = zsnd(unap(I_EXPORT,hd(t)));
397 /* exlist_list :: [[ ConVarId | ((ConId, [ConVarId])) ]] */
399 if (isNull(exlist_list)) {
400 ERRMSG(0) "Can't find any export lists in interface file"
404 return filterInterface ( root, isExportedIFaceEntity,
409 /* addTyconsAndClassesFromIFace :: I_INTERFACE -> [QualId] -> [QualId] */
410 static List addTyconsAndClassesFromIFace ( Cell root, List aktys )
412 Cell iface = unap(I_INTERFACE,root);
413 Text mname = textOf(zfst(iface));
414 List defns = zsnd(iface);
415 for (; nonNull(defns); defns = tl(defns)) {
416 Cell defn = hd(defns);
417 Cell what = whatIs(defn);
418 if (what==I_TYPE || what==I_DATA
419 || what==I_NEWTYPE || what==I_CLASS) {
420 QualId q = mkQCon ( mname, textOf(getIEntityName(defn)) );
421 if (!qualidIsMember ( q, aktys ))
422 aktys = cons ( q, aktys );
429 static Void ifentityAllTypesKnown_dumpmsg ( Cell entity )
431 ConVarId id = getIEntityName ( entity );
434 "dumping %s because of unknown type(s)\n",
435 isNull(id) ? "(nameless entity?!)" : textToStr(textOf(id)) );
440 /* ifentityAllTypesKnown :: I_IMPORT..I_VALUE -> (([QualId], ConId)) -> Bool */
441 /* mod is the current module being processed -- so we can qualify unqual'd
442 names. Strange calling convention for aktys and mod is so we can call this
443 from filterInterface.
445 static Bool ifentityAllTypesKnown ( Cell entity, ZPair aktys_mod )
448 List aktys = zfst ( aktys_mod );
449 ConId mod = zsnd ( aktys_mod );
450 switch (whatIs(entity)) {
457 Cell inst = unap(I_INSTANCE,entity);
458 List ctx = zsel25 ( inst ); /* :: [((QConId,VarId))] */
459 Type cls = zsel35 ( inst ); /* :: Type */
460 for (t = ctx; nonNull(t); t=tl(t))
461 if (!allTypesKnown(zfst(hd(t)),aktys,mod)) return FALSE;
462 if (!allTypesKnown(cls, aktys,mod)) return FALSE;
466 return allTypesKnown( zsel44(unap(I_TYPE,entity)), aktys,mod );
468 Cell data = unap(I_DATA,entity);
469 List ctx = zsel25 ( data ); /* :: [((QConId,VarId))] */
470 List constrs = zsel55 ( data ); /* :: [ ((ConId, [((Type,VarId,Int))] )) ] */
471 for (t = ctx; nonNull(t); t=tl(t))
472 if (!allTypesKnown(zfst(hd(t)),aktys,mod)) return FALSE;
473 for (t = constrs; nonNull(t); t=tl(t))
474 for (u = zsnd(hd(t)); nonNull(u); u=tl(u))
475 if (!allTypesKnown(zfst3(hd(u)),aktys,mod)) return FALSE;
479 Cell newty = unap(I_NEWTYPE,entity);
480 List ctx = zsel25(newty); /* :: [((QConId,VarId))] */
481 ZPair constr = zsel55 ( newty ); /* :: ((ConId,Type)) */
482 for (t = ctx; nonNull(t); t=tl(t))
483 if (!allTypesKnown(zfst(hd(t)),aktys,mod)) return FALSE;
485 && !allTypesKnown(zsnd(constr),aktys,mod)) return FALSE;
489 Cell klass = unap(I_CLASS,entity);
490 List ctx = zsel25(klass); /* :: [((QConId,VarId))] */
491 List sigs = zsel55(klass); /* :: [((VarId,Type))] */
492 for (t = ctx; nonNull(t); t=tl(t))
493 if (!allTypesKnown(zfst(hd(t)),aktys,mod)) return FALSE;
494 for (t = sigs; nonNull(t); t=tl(t))
495 if (!allTypesKnown(zsnd(hd(t)),aktys,mod)) return FALSE;
499 return allTypesKnown( zthd3(unap(I_VALUE,entity)), aktys,mod );
501 internal("ifentityAllTypesKnown");
506 /* ifTypeDoesntRefUnknownTycon :: I_IMPORT..I_VALUE -> (([QualId], ConId)) -> Bool */
507 /* mod is the current module being processed -- so we can qualify unqual'd
508 names. Strange calling convention for aktys and mod is so we can call this
509 from filterInterface.
511 static Bool ifTypeDoesntRefUnknownTycon ( Cell entity, ZPair aktys_mod )
514 List aktys = zfst ( aktys_mod );
515 ConId mod = zsnd ( aktys_mod );
516 if (whatIs(entity) != I_TYPE) {
519 return allTypesKnown( zsel44(unap(I_TYPE,entity)), aktys,mod );
524 static Void ifTypeDoesntRefUnknownTycon_dumpmsg ( Cell entity )
526 ConVarId id = getIEntityName ( entity );
527 assert (whatIs(entity)==I_TYPE);
531 "dumping type %s because of unknown tycon(s)\n",
532 textToStr(textOf(id)) );
537 /* abstractifyExport :: I_EXPORT -> ConId -> I_EXPORT
539 static List abstractifyExDecl ( Cell root, ConId toabs )
541 ZPair exdecl = unap(I_EXPORT,root);
542 List exlist = zsnd(exdecl);
544 for (; nonNull(exlist); exlist = tl(exlist)) {
545 if (isZPair(hd(exlist))
546 && textOf(toabs) == textOf(zfst(hd(exlist)))) {
547 /* it's toabs, exported non-abstractly */
548 res = cons ( zfst(hd(exlist)), res );
550 res = cons ( hd(exlist), res );
553 return ap(I_EXPORT,zpair(zfst(exdecl),reverse(res)));
557 static Void ppModule ( Text modt )
560 fflush(stderr); fflush(stdout);
561 fprintf(stderr, "---------------- MODULE %s ----------------\n",
567 static void* ifFindItblFor ( Name n )
569 /* n is a constructor for which we want to find the GHC info table.
570 First look for a _con_info symbol. If that doesn't exist, _and_
571 this is a nullary constructor, then it's safe to look for the
572 _static_info symbol instead.
578 sprintf ( buf, MAYBE_LEADING_UNDERSCORE_STR("%s_%s_con_info"),
579 textToStr( module(name(n).mod).text ),
580 textToStr( name(n).text ) );
581 t = enZcodeThenFindText(buf);
582 p = lookupOTabName ( name(n).mod, textToStr(t) );
586 if (name(n).arity == 0) {
587 sprintf ( buf, MAYBE_LEADING_UNDERSCORE_STR("%s_%s_static_info"),
588 textToStr( module(name(n).mod).text ),
589 textToStr( name(n).text ) );
590 t = enZcodeThenFindText(buf);
591 p = lookupOTabName ( name(n).mod, textToStr(t) );
595 ERRMSG(0) "Can't find info table %s", textToStr(t)
600 void ifLinkConstrItbl ( Name n )
602 /* name(n) is either a constructor or a field name.
603 If the latter, ignore it. If it is a non-nullary constructor,
604 find its info table in the object code. If it's nullary,
605 we can skip the info table, since all accesses will go via
608 if (islower(textToStr(name(n).text)[0])) return;
609 if (name(n).arity == 0) return;
610 name(n).itbl = ifFindItblFor(n);
614 static void ifSetClassDefaultsAndDCon ( Class c )
622 List defs; /* :: [Name] */
623 List mems; /* :: [Name] */
625 assert(isNull(cclass(c).defaults));
627 /* Create the defaults list by more-or-less cloning the members list. */
629 for (mems=cclass(c).members; nonNull(mems); mems=tl(mems)) {
631 s = textToStr( name(hd(mems)).text );
632 assert(strlen(s) < 95);
634 n = findNameInAnyModule(findText(buf));
639 cclass(c).defaults = defs;
641 /* Create a name table entry for the dictionary datacon.
642 Interface files don't mention them, so it had better not
646 s = textToStr( cclass(c).text );
647 assert( strlen(s) < 96 );
650 n = findNameInAnyModule(t);
656 name(n).arity = cclass(c).numSupers + cclass(c).numMembers;
657 name(n).number = cfunNo(0);
660 /* And finally ... set name(n).itbl to Mod_:DClass_con_info.
661 Because this happens right at the end of loading, we know
662 that we should actually be able to find the symbol in this
663 module's object symbol table. Except that if the dictionary
664 has arity 1, we don't bother, since it will be represented as
665 a newtype and not as a data, so its itbl can remain NULL.
667 if (name(n).arity == 1) {
669 name(n).defn = nameId;
671 p = ifFindItblFor ( n );
677 /* ifaces_outstanding holds a list of parsed interfaces
678 for which we need to load objects and create symbol
681 Return TRUE if Prelude `elem` ifaces_outstanding, else FALSE.
683 Bool processInterfaces ( void )
694 List all_known_types;
697 List cls_list; /* :: List Class */
698 List constructor_list; /* :: List Name */
700 List ifaces = NIL; /* :: List I_INTERFACE */
701 List iface_sizes = NIL; /* :: List Int */
702 List iface_onames = NIL; /* :: List Text */
704 if (isNull(ifaces_outstanding)) return FALSE;
708 "processInterfaces: %d interfaces to process\n",
709 length(ifaces_outstanding) );
712 /* unzip3 ifaces_outstanding into ifaces, iface_sizes, iface_onames */
713 for (xs = ifaces_outstanding; nonNull(xs); xs=tl(xs)) {
714 ifaces = cons ( zfst3(hd(xs)), ifaces );
715 iface_onames = cons ( zsnd3(hd(xs)), iface_onames );
716 iface_sizes = cons ( zthd3(hd(xs)), iface_sizes );
719 ifaces = reverse(ifaces);
720 iface_onames = reverse(iface_onames);
721 iface_sizes = reverse(iface_sizes);
723 /* Clean up interfaces -- dump non-exported value, class, type decls */
724 for (xs = ifaces; nonNull(xs); xs = tl(xs))
725 hd(xs) = deleteUnexportedIFaceEntities(hd(xs));
728 /* Iteratively delete any type declarations which refer to unknown
731 num_known_types = 999999999;
735 /* Construct a list of all known tycons. This is a list of QualIds.
736 Unfortunately it also has to contain all known class names, since
737 allTypesKnown cannot distinguish between tycons and classes -- a
738 deficiency of the iface abs syntax.
740 all_known_types = getAllKnownTyconsAndClasses();
741 for (xs = ifaces; nonNull(xs); xs=tl(xs))
742 all_known_types = addTyconsAndClassesFromIFace ( hd(xs), all_known_types );
744 /* Have we reached a fixed point? */
745 i = length(all_known_types);
748 "\n============= %d known types =============\n", i );
750 if (num_known_types == i) break;
753 /* Delete all entities which refer to unknown tycons. */
754 for (xs = ifaces; nonNull(xs); xs = tl(xs)) {
755 ConId mod = zfst(unap(I_INTERFACE,hd(xs)));
756 assert(nonNull(mod));
757 hd(xs) = filterInterface ( hd(xs),
758 ifTypeDoesntRefUnknownTycon,
759 zpair(all_known_types,mod),
760 ifTypeDoesntRefUnknownTycon_dumpmsg );
764 /* Now abstractify any datas and newtypes which refer to unknown tycons
765 -- including, of course, the type decls just deleted.
767 for (xs = ifaces; nonNull(xs); xs = tl(xs)) {
768 List absify = NIL; /* :: [ConId] */
769 ZPair iface = unap(I_INTERFACE,hd(xs)); /* ((ConId, [I_IMPORT..I_VALUE])) */
770 ConId mod = zfst(iface);
771 List aktys = all_known_types; /* just a renaming */
775 /* Compute into absify the list of all ConIds (tycons) we need to
778 for (es = zsnd(iface); nonNull(es); es=tl(es)) {
780 Bool allKnown = TRUE;
782 if (whatIs(ent)==I_DATA) {
783 Cell data = unap(I_DATA,ent);
784 List ctx = zsel25 ( data ); /* :: [((QConId,VarId))] */
785 List constrs = zsel55 ( data ); /* :: [ ((ConId, [((Type,VarId,Int))] )) ] */
786 for (t = ctx; nonNull(t); t=tl(t))
787 if (!allTypesKnown(zfst(hd(t)),aktys,mod)) allKnown = FALSE;
788 for (t = constrs; nonNull(t); t=tl(t))
789 for (u = zsnd(hd(t)); nonNull(u); u=tl(u))
790 if (!allTypesKnown(zfst3(hd(u)),aktys,mod)) allKnown = FALSE;
792 else if (whatIs(ent)==I_NEWTYPE) {
793 Cell newty = unap(I_NEWTYPE,ent);
794 List ctx = zsel25(newty); /* :: [((QConId,VarId))] */
795 ZPair constr = zsel55 ( newty ); /* :: ((ConId,Type)) */
796 for (t = ctx; nonNull(t); t=tl(t))
797 if (!allTypesKnown(zfst(hd(t)),aktys,mod)) allKnown = FALSE;
798 if (!allTypesKnown(zsnd(constr),aktys,mod)) allKnown = FALSE;
802 absify = cons ( getIEntityName(ent), absify );
805 "abstractifying %s because it uses an unknown type\n",
806 textToStr(textOf(getIEntityName(ent))) );
811 /* mark in exports as abstract all names in absify (modifies iface) */
812 for (; nonNull(absify); absify=tl(absify)) {
813 ConId toAbs = hd(absify);
814 for (es = zsnd(iface); nonNull(es); es=tl(es)) {
815 if (whatIs(hd(es)) != I_EXPORT) continue;
816 hd(es) = abstractifyExDecl ( hd(es), toAbs );
820 /* For each data/newtype in the export list marked as abstract,
821 remove the constructor lists. This catches all abstractification
822 caused by the code above, and it also catches tycons which really
823 were exported abstractly.
826 exlist_list = getExportDeclsInIFace ( ap(I_INTERFACE,iface) );
827 /* exlist_list :: [I_EXPORT] */
828 for (t=exlist_list; nonNull(t); t=tl(t))
829 hd(t) = zsnd(unap(I_EXPORT,hd(t)));
830 /* exlist_list :: [[ ConVarId | ((ConId, [ConVarId])) ]] */
832 for (es = zsnd(iface); nonNull(es); es=tl(es)) {
834 if (whatIs(ent)==I_DATA
835 && isExportedAbstractly ( getIEntityName(ent),
837 Cell data = unap(I_DATA,ent);
838 data = z5ble ( zsel15(data), zsel25(data), zsel35(data),
839 zsel45(data), NIL /* the constr list */ );
840 hd(es) = ap(I_DATA,data);
842 fprintf(stderr, "abstractify data %s\n",
843 textToStr(textOf(getIEntityName(ent))) );
846 else if (whatIs(ent)==I_NEWTYPE
847 && isExportedAbstractly ( getIEntityName(ent),
849 Cell data = unap(I_NEWTYPE,ent);
850 data = z5ble ( zsel15(data), zsel25(data), zsel35(data),
851 zsel45(data), NIL /* the constr-type pair */ );
852 hd(es) = ap(I_NEWTYPE,data);
854 fprintf(stderr, "abstractify newtype %s\n",
855 textToStr(textOf(getIEntityName(ent))) );
860 /* We've finally finished mashing this iface. Update the iface list. */
861 hd(xs) = ap(I_INTERFACE,iface);
865 /* At this point, the interfaces are cleaned up so that no type, data or
866 newtype defn refers to a non-existant type. However, there still may
867 be value defns, classes and instances which refer to unknown types.
868 Delete iteratively until a fixed point is reached.
871 fprintf(stderr,"\n");
873 num_known_types = 999999999;
877 /* Construct a list of all known tycons. This is a list of QualIds.
878 Unfortunately it also has to contain all known class names, since
879 allTypesKnown cannot distinguish between tycons and classes -- a
880 deficiency of the iface abs syntax.
882 all_known_types = getAllKnownTyconsAndClasses();
883 for (xs = ifaces; nonNull(xs); xs=tl(xs))
884 all_known_types = addTyconsAndClassesFromIFace ( hd(xs), all_known_types );
886 /* Have we reached a fixed point? */
887 i = length(all_known_types);
890 "\n------------- %d known types -------------\n", i );
892 if (num_known_types == i) break;
895 /* Delete all entities which refer to unknown tycons. */
896 for (xs = ifaces; nonNull(xs); xs = tl(xs)) {
897 ConId mod = zfst(unap(I_INTERFACE,hd(xs)));
898 assert(nonNull(mod));
900 hd(xs) = filterInterface ( hd(xs),
901 ifentityAllTypesKnown,
902 zpair(all_known_types,mod),
903 ifentityAllTypesKnown_dumpmsg );
908 /* Allocate module table entries and read in object code. */
911 xs=tl(xs), iface_sizes=tl(iface_sizes), iface_onames=tl(iface_onames)) {
912 startGHCModule ( textOf(zfst(unap(I_INTERFACE,hd(xs)))),
913 intOf(hd(iface_sizes)),
916 assert (isNull(iface_sizes));
917 assert (isNull(iface_onames));
920 /* Now work through the decl lists of the modules, and call the
921 startGHC* functions on the entities. This creates names in
922 various tables but doesn't bind them to anything.
925 for (xs = ifaces; nonNull(xs); xs = tl(xs)) {
926 iface = unap(I_INTERFACE,hd(xs));
927 mname = textOf(zfst(iface));
928 mod = findModule(mname);
929 if (isNull(mod)) internal("processInterfaces(4)");
931 ppModule ( module(mod).text );
933 for (decls = zsnd(iface); nonNull(decls); decls = tl(decls)) {
934 Cell decl = hd(decls);
935 switch(whatIs(decl)) {
937 Cell exdecl = unap(I_EXPORT,decl);
938 startGHCExports ( zfst(exdecl), zsnd(exdecl) );
942 Cell imdecl = unap(I_IMPORT,decl);
943 startGHCImports ( zfst(imdecl), zsnd(imdecl) );
950 /* Trying to find the instance table location allocated by
951 startGHCInstance in subsequent processing is a nightmare, so
952 cache it on the tree.
954 Cell instance = unap(I_INSTANCE,decl);
955 Inst in = startGHCInstance ( zsel15(instance), zsel25(instance),
956 zsel35(instance), zsel45(instance) );
957 hd(decls) = ap(I_INSTANCE,
958 z5ble( zsel15(instance), zsel25(instance),
959 zsel35(instance), zsel45(instance), in ));
963 Cell tydecl = unap(I_TYPE,decl);
964 startGHCSynonym ( zsel14(tydecl), zsel24(tydecl),
965 zsel34(tydecl), zsel44(tydecl) );
969 Cell ddecl = unap(I_DATA,decl);
970 startGHCDataDecl ( zsel15(ddecl), zsel25(ddecl),
971 zsel35(ddecl), zsel45(ddecl), zsel55(ddecl) );
975 Cell ntdecl = unap(I_NEWTYPE,decl);
976 startGHCNewType ( zsel15(ntdecl), zsel25(ntdecl),
977 zsel35(ntdecl), zsel45(ntdecl),
982 Cell klass = unap(I_CLASS,decl);
983 startGHCClass ( zsel15(klass), zsel25(klass),
984 zsel35(klass), zsel45(klass),
989 Cell value = unap(I_VALUE,decl);
990 startGHCValue ( zfst3(value), zsnd3(value), zthd3(value) );
994 internal("processInterfaces(1)");
1000 fprintf(stderr, "\n============================"
1001 "=============================\n");
1002 fprintf(stderr, "=============================="
1003 "===========================\n");
1006 /* Traverse again the decl lists of the modules, this time
1007 calling the finishGHC* functions. But don't process
1008 the export lists; those must wait for later.
1012 constructor_list = NIL;
1013 for (xs = ifaces; nonNull(xs); xs = tl(xs)) {
1014 iface = unap(I_INTERFACE,hd(xs));
1015 mname = textOf(zfst(iface));
1016 mod = findModule(mname);
1017 if (isNull(mod)) internal("processInterfaces(3)");
1019 ppModule ( module(mod).text );
1021 if (mname == textPrelude) didPrelude = TRUE;
1023 for (decls = zsnd(iface); nonNull(decls); decls = tl(decls)) {
1024 Cell decl = hd(decls);
1025 switch(whatIs(decl)) {
1033 Cell fixdecl = unap(I_FIXDECL,decl);
1034 finishGHCFixdecl ( zfst3(fixdecl), zsnd3(fixdecl), zthd3(fixdecl) );
1038 Cell instance = unap(I_INSTANCE,decl);
1039 finishGHCInstance ( zsel55(instance) );
1043 Cell tydecl = unap(I_TYPE,decl);
1044 finishGHCSynonym ( zsel24(tydecl) );
1048 Cell ddecl = unap(I_DATA,decl);
1049 List constrs = finishGHCDataDecl ( zsel35(ddecl) );
1050 constructor_list = appendOnto ( constrs, constructor_list );
1054 Cell ntdecl = unap(I_NEWTYPE,decl);
1055 finishGHCNewType ( zsel35(ntdecl) );
1059 Cell klass = unap(I_CLASS,decl);
1060 Class cls = finishGHCClass ( zsel35(klass) );
1061 cls_list = cons(cls,cls_list);
1065 Cell value = unap(I_VALUE,decl);
1066 finishGHCValue ( zsnd3(value) );
1070 internal("processInterfaces(2)");
1075 fprintf(stderr, "\n+++++++++++++++++++++++++++++"
1076 "++++++++++++++++++++++++++++\n");
1077 fprintf(stderr, "+++++++++++++++++++++++++++++++"
1078 "++++++++++++++++++++++++++\n");
1081 /* Build the module(m).export lists for each module, by running
1082 through the export lists in the iface. Also, do the implicit
1083 'import Prelude' thing. And finally, do the object code
1086 for (xs = ifaces; nonNull(xs); xs = tl(xs))
1087 finishGHCModule(hd(xs));
1089 mapProc(visitClass,cls_list);
1090 mapProc(ifSetClassDefaultsAndDCon,cls_list);
1091 mapProc(ifLinkConstrItbl,constructor_list);
1094 ifaces_outstanding = NIL;
1100 /* --------------------------------------------------------------------------
1102 * ------------------------------------------------------------------------*/
1104 static void startGHCModule_errMsg ( char* msg )
1106 fprintf ( stderr, "object error: %s\n", msg );
1109 static void* startGHCModule_clientLookup ( char* sym )
1112 /* fprintf ( stderr, "CLIENTLOOKUP %s\n", sym ); */
1114 return lookupObjName ( sym );
1117 static ObjectCode* startGHCModule_partial_load ( String objNm, Int objSz )
1120 = ocNew ( startGHCModule_errMsg,
1121 startGHCModule_clientLookup,
1125 ERRMSG(0) "Storage allocation for object file \"%s\" failed", objNm
1128 if (!ocLoadImage(oc,VERBOSE)) {
1129 ERRMSG(0) "Reading of object file \"%s\" failed", objNm
1132 if (!ocVerifyImage(oc,VERBOSE)) {
1133 ERRMSG(0) "Validation of object file \"%s\" failed", objNm
1136 if (!ocGetNames(oc,VERBOSE)) {
1137 ERRMSG(0) "Reading of symbol names in object file \"%s\" failed", objNm
1143 static Void startGHCModule ( Text mname, Int sizeObj, Text nameObj )
1146 Module m = findModule(mname);
1149 m = newModule(mname);
1151 fprintf ( stderr, "startGHCIface: name %16s objsize %d\n",
1152 textToStr(mname), sizeObj );
1155 if (module(m).fake) {
1156 module(m).fake = FALSE;
1158 ERRMSG(0) "Module \"%s\" already loaded", textToStr(mname)
1163 /* Get hold of the primary object for the module. */
1165 = startGHCModule_partial_load ( textToStr(nameObj), sizeObj );
1167 /* and any extras ... */
1168 for (xts = module(m).objectExtraNames; nonNull(xts); xts=tl(xts)) {
1172 String nm = getExtraObjectInfo ( textToStr(nameObj),
1176 ERRMSG(0) "Can't find extra object file \"%s\"", nm
1179 oc = startGHCModule_partial_load ( nm, size );
1180 oc->next = module(m).objectExtras;
1181 module(m).objectExtras = oc;
1186 /* For the module mod, augment both the export environment (.exports)
1187 and the eval environment (.names, .tycons, .classes)
1188 with the symbols mentioned in exlist. We don't actually need
1189 to modify the names, tycons, classes or instances in the eval
1190 environment, since previous processing of the
1191 top-level decls in the iface should have done this already.
1193 mn is the module mentioned in the export list; it is the "original"
1194 module for the symbols in the export list. We should also record
1195 this info with the symbols, since references to object code need to
1196 refer to the original module in which a symbol was defined, rather
1197 than to some module it has been imported into and then re-exported.
1199 We take the policy that if something mentioned in an export list
1200 can't be found in the symbol tables, it is simply ignored. After all,
1201 previous processing of the iface syntax trees has already removed
1202 everything which Hugs can't handle, so if there is mention of these
1203 things still lurking in export lists somewhere, about the only thing
1204 to do is to ignore it.
1206 Also do an implicit 'import Prelude' thingy for the module,
1211 static Void finishGHCModule ( Cell root )
1213 /* root :: I_INTERFACE */
1214 Cell iface = unap(I_INTERFACE,root);
1215 ConId iname = zfst(iface);
1216 Module mod = findModule(textOf(iname));
1217 List exlist_list = NIL;
1222 fprintf(stderr, "begin finishGHCModule %s\n", textToStr(textOf(iname)));
1225 if (isNull(mod)) internal("finishExports(1)");
1228 exlist_list = getExportDeclsInIFace ( root );
1229 /* exlist_list :: [I_EXPORT] */
1231 for (; nonNull(exlist_list); exlist_list=tl(exlist_list)) {
1232 ZPair exdecl = unap(I_EXPORT,hd(exlist_list));
1233 ConId exmod = zfst(exdecl);
1234 List exlist = zsnd(exdecl);
1235 /* exlist :: [ ConVarId | ((ConId, [ConVarId])) ] */
1237 for (; nonNull(exlist); exlist=tl(exlist)) {
1242 Cell ex = hd(exlist);
1244 switch (whatIs(ex)) {
1246 case VARIDCELL: /* variable */
1247 q = mkQualId(exmod,ex);
1248 c = findQualNameWithoutConsultingExportList ( q );
1249 if (isNull(c)) goto notfound;
1251 fprintf(stderr, " var %s\n", textToStr(textOf(ex)) );
1253 module(mod).exports = cons(c, module(mod).exports);
1257 case CONIDCELL: /* non data tycon */
1258 q = mkQualId(exmod,ex);
1259 c = findQualTyconWithoutConsultingExportList ( q );
1260 if (isNull(c)) goto notfound;
1262 fprintf(stderr, " type %s\n", textToStr(textOf(ex)) );
1264 module(mod).exports = cons(pair(c,NIL), module(mod).exports);
1268 case ZTUP2: /* data T = C1 ... Cn or class C where f1 ... fn */
1269 subents = zsnd(ex); /* :: [ConVarId] */
1270 ex = zfst(ex); /* :: ConId */
1271 q = mkQualId(exmod,ex);
1272 c = findQualTyconWithoutConsultingExportList ( q );
1274 if (nonNull(c)) { /* data */
1276 fprintf(stderr, " data/newtype %s = { ",
1277 textToStr(textOf(ex)) );
1279 assert(tycon(c).what == DATATYPE || tycon(c).what==NEWTYPE);
1280 abstract = isNull(tycon(c).defn);
1281 /* This data/newtype could be abstract even tho the export list
1282 says to export it non-abstractly. That happens if it was
1283 imported from some other module and is now being re-exported,
1284 and previous cleanup phases have abstractified it in the
1285 original (defining) module.
1288 module(mod).exports = cons(pair(c,NIL), module(mod).exports);
1291 fprintf ( stderr, "(abstract) ");
1294 module(mod).exports = cons(pair(c,DOTDOT), module(mod).exports);
1296 for (; nonNull(subents); subents = tl(subents)) {
1297 Cell ent2 = hd(subents);
1298 assert(isCon(ent2) || isVar(ent2));
1299 /* isVar since could be a field name */
1300 q = mkQualId(exmod,ent2);
1301 c = findQualNameWithoutConsultingExportList ( q );
1303 fprintf(stderr, "%s ", textToStr(name(c).text));
1306 /* module(mod).exports = cons(c, module(mod).exports); */
1311 fprintf(stderr, "}\n" );
1313 } else { /* class */
1314 q = mkQualId(exmod,ex);
1315 c = findQualClassWithoutConsultingExportList ( q );
1316 if (isNull(c)) goto notfound;
1318 fprintf(stderr, " class %s { ", textToStr(textOf(ex)) );
1320 module(mod).exports = cons(pair(c,DOTDOT), module(mod).exports);
1322 for (; nonNull(subents); subents = tl(subents)) {
1323 Cell ent2 = hd(subents);
1324 assert(isVar(ent2));
1325 q = mkQualId(exmod,ent2);
1326 c = findQualNameWithoutConsultingExportList ( q );
1328 fprintf(stderr, "%s ", textToStr(name(c).text));
1330 if (isNull(c)) goto notfound;
1331 /* module(mod).exports = cons(c, module(mod).exports); */
1335 fprintf(stderr, "}\n" );
1341 internal("finishExports(2)");
1344 continue; /* so notfound: can be placed after this */
1347 /* q holds what ain't found */
1348 assert(whatIs(q)==QUALIDENT);
1350 fprintf( stderr, " ------ IGNORED: %s.%s\n",
1351 textToStr(qmodOf(q)), textToStr(qtextOf(q)) );
1358 if (preludeLoaded) {
1359 /* do the implicit 'import Prelude' thing */
1360 List pxs = module(modulePrelude).exports;
1361 for (; nonNull(pxs); pxs=tl(pxs)) {
1364 switch (whatIs(px)) {
1369 module(mod).names = cons ( px, module(mod).names );
1372 module(mod).tycons = cons ( px, module(mod).tycons );
1375 module(mod).classes = cons ( px, module(mod).classes );
1378 fprintf(stderr, "finishGHCModule: unknown tag %d\n", whatIs(px));
1379 internal("finishGHCModule -- implicit import Prelude");
1386 /* Last, but by no means least ... */
1387 if (!ocResolve(module(mod).object,VERBOSE))
1388 internal("finishGHCModule: object resolution failed");
1390 for (oc=module(mod).objectExtras; oc; oc=oc->next) {
1391 if (!ocResolve(oc, VERBOSE))
1392 internal("finishGHCModule: extra object resolution failed");
1397 /* --------------------------------------------------------------------------
1399 * ------------------------------------------------------------------------*/
1401 static Void startGHCExports ( ConId mn, List exlist )
1404 fprintf(stderr,"startGHCExports %s\n", textToStr(textOf(mn)) );
1406 /* Nothing to do. */
1409 static Void finishGHCExports ( ConId mn, List exlist )
1412 fprintf(stderr,"finishGHCExports %s\n", textToStr(textOf(mn)) );
1414 /* Nothing to do. */
1418 /* --------------------------------------------------------------------------
1420 * ------------------------------------------------------------------------*/
1422 static Void startGHCImports ( ConId mn, List syms )
1423 /* nm the module to import from */
1424 /* syms [ConId | VarId] -- the names to import */
1427 fprintf(stderr,"startGHCImports %s\n", textToStr(textOf(mn)) );
1429 /* Nothing to do. */
1433 static Void finishGHCImports ( ConId nm, List syms )
1434 /* nm the module to import from */
1435 /* syms [ConId | VarId] -- the names to import */
1438 fprintf(stderr,"finishGHCImports %s\n", textToStr(textOf(nm)) );
1440 /* Nothing to do. */
1444 /* --------------------------------------------------------------------------
1446 * ------------------------------------------------------------------------*/
1448 static Void finishGHCFixdecl ( Cell prec, Cell assoc, ConVarId name )
1450 Int p = intOf(prec);
1451 Int a = intOf(assoc);
1452 Name n = findName(textOf(name));
1453 assert (nonNull(n));
1454 name(n).syntax = mkSyntax ( a, p );
1458 /* --------------------------------------------------------------------------
1460 * ------------------------------------------------------------------------*/
1462 /* convert a leading run of DICTAPs into Hugs' internal Qualtype form, viz:
1463 { C1 a } -> { C2 b } -> T into
1464 ap(QUALTYPE, ( [(C1,a),(C2,b)], T ))
1466 static Type dictapsToQualtype ( Type ty )
1469 List preds, dictaps;
1471 /* break ty into pieces at the top-level arrows */
1472 while (isAp(ty) && isAp(fun(ty)) && fun(fun(ty))==typeArrow) {
1473 pieces = cons ( arg(fun(ty)), pieces );
1476 pieces = cons ( ty, pieces );
1477 pieces = reverse ( pieces );
1480 while (nonNull(pieces) && whatIs(hd(pieces))==DICTAP) {
1481 dictaps = cons ( hd(pieces), dictaps );
1482 pieces = tl(pieces);
1485 /* dictaps holds the predicates, backwards */
1486 /* pieces holds the remainder of the type, forwards */
1487 assert(nonNull(pieces));
1488 pieces = reverse(pieces);
1490 pieces = tl(pieces);
1491 for (; nonNull(pieces); pieces=tl(pieces))
1492 ty = fn(hd(pieces),ty);
1495 for (; nonNull(dictaps); dictaps=tl(dictaps)) {
1496 Cell da = hd(dictaps);
1497 QualId cl = fst(unap(DICTAP,da));
1498 Cell arg = snd(unap(DICTAP,da));
1499 preds = cons ( pair(cl,arg), preds );
1502 if (nonNull(preds)) ty = ap(QUAL, pair(preds,ty));
1508 static void startGHCValue ( Int line, VarId vid, Type ty )
1512 Text v = textOf(vid);
1515 fprintf(stderr,"begin startGHCValue %s\n", textToStr(v));
1520 if (nonNull(n) && name(n).defn != PREDEFINED) {
1521 ERRMSG(line) "Attempt to redefine variable \"%s\"", textToStr(v)
1524 if (isNull(n)) n = newName(v,NIL);
1526 ty = dictapsToQualtype(ty);
1528 tvs = ifTyvarsIn(ty);
1529 for (tmp=tvs; nonNull(tmp); tmp=tl(tmp))
1530 hd(tmp) = zpair(hd(tmp),STAR);
1532 ty = mkPolyType(tvsToKind(tvs),ty);
1534 ty = tvsToOffsets(line,ty,tvs);
1536 name(n).arity = arityInclDictParams(ty);
1537 name(n).line = line;
1542 static void finishGHCValue ( VarId vid )
1544 Name n = findName ( textOf(vid) );
1545 Int line = name(n).line;
1547 fprintf(stderr, "begin finishGHCValue %s\n", textToStr(name(n).text) );
1549 assert(currentModule == name(n).mod);
1550 name(n).type = conidcellsToTycons(line,name(n).type);
1552 if (isIfaceDefaultMethodName(name(n).text)) {
1553 /* ... we need to set .parent to point to the class
1554 ... once we figure out what the class actually is :-)
1556 Type t = name(n).type;
1557 assert(isPolyType(t));
1558 if (isPolyType(t)) t = monotypeOf(t);
1559 assert(isQualType(t));
1560 t = fst(snd(t)); /* t :: [(Class,Offset)] */
1562 assert(nonNull(hd(t)));
1563 assert(isPair(hd(t)));
1564 t = fst(hd(t)); /* t :: Class */
1567 name(n).parent = t; /* phew! */
1572 /* --------------------------------------------------------------------------
1574 * ------------------------------------------------------------------------*/
1576 static Void startGHCSynonym ( Int line, ConId tycon, List tvs, Type ty )
1578 /* tycon :: ConId */
1579 /* tvs :: [((VarId,Kind))] */
1581 Text t = textOf(tycon);
1583 fprintf(stderr, "begin startGHCSynonym %s\n", textToStr(t) );
1586 if (nonNull(findTycon(t))) {
1587 ERRMSG(line) "Repeated definition of type constructor \"%s\"",
1591 Tycon tc = newTycon(t);
1592 tycon(tc).line = line;
1593 tycon(tc).arity = length(tvs);
1594 tycon(tc).what = SYNONYM;
1595 tycon(tc).kind = tvsToKind(tvs);
1597 /* prepare for finishGHCSynonym */
1598 tycon(tc).defn = tvsToOffsets(line,ty,tvs);
1603 static Void finishGHCSynonym ( ConId tyc )
1605 Tycon tc = findTycon(textOf(tyc));
1606 Int line = tycon(tc).line;
1608 fprintf(stderr, "begin finishGHCSynonym %s\n", textToStr(textOf(tyc)) );
1611 assert (currentModule == tycon(tc).mod);
1612 // setCurrModule(tycon(tc).mod);
1613 tycon(tc).defn = conidcellsToTycons(line,tycon(tc).defn);
1615 /* (ADR) ToDo: can't really do this until I've done all synonyms
1616 * and then I have to do them in order
1617 * tycon(tc).defn = fullExpand(ty);
1618 * (JRS) What?!?! i don't understand
1623 /* --------------------------------------------------------------------------
1625 * ------------------------------------------------------------------------*/
1627 static Void startGHCDataDecl(line,ctx0,tycon,ktyvars,constrs0)
1629 List ctx0; /* [((QConId,VarId))] */
1630 Cell tycon; /* ConId */
1631 List ktyvars; /* [((VarId,Kind))] */
1632 List constrs0; /* [((ConId,[((Type,VarId,Int))]))] */
1633 /* The Text is an optional field name
1634 The Int indicates strictness */
1635 /* ToDo: worry about being given a decl for (->) ?
1636 * and worry about qualidents for ()
1639 Type ty, resTy, selTy, conArgTy;
1640 List tmp, conArgs, sels, constrs, fields, tyvarsMentioned;
1644 Pair conArg, ctxElem;
1646 Int conArgStrictness;
1648 Text t = textOf(tycon);
1650 fprintf(stderr, "begin startGHCDataDecl %s\n",textToStr(t));
1654 if (nonNull(findTycon(t))) {
1655 ERRMSG(line) "Repeated definition of type constructor \"%s\"",
1659 Tycon tc = newTycon(t);
1661 tycon(tc).line = line;
1662 tycon(tc).arity = length(ktyvars);
1663 tycon(tc).kind = tvsToKind(ktyvars);
1664 tycon(tc).what = DATATYPE;
1666 /* a list to accumulate selectors in :: [((VarId,Type))] */
1669 /* make resTy the result type of the constr, T v1 ... vn */
1671 for (tmp=ktyvars; nonNull(tmp); tmp=tl(tmp))
1672 resTy = ap(resTy,zfst(hd(tmp)));
1674 /* for each constructor ... */
1675 for (constrs=constrs0; nonNull(constrs); constrs=tl(constrs)) {
1676 constr = hd(constrs);
1677 conid = zfst(constr);
1678 fields = zsnd(constr);
1680 /* Build type of constr and handle any selectors found.
1681 Also collect up tyvars occurring in the constr's arg
1682 types, so we can throw away irrelevant parts of the
1686 tyvarsMentioned = NIL;
1687 /* tyvarsMentioned :: [VarId] */
1689 conArgs = reverse(fields);
1690 for (; nonNull(conArgs); conArgs=tl(conArgs)) {
1691 conArg = hd(conArgs); /* (Type,Text) */
1692 conArgTy = zfst3(conArg);
1693 conArgNm = zsnd3(conArg);
1694 conArgStrictness = intOf(zthd3(conArg));
1695 tyvarsMentioned = dupListOnto(ifTyvarsIn(conArgTy),
1697 if (conArgStrictness > 0) conArgTy = bang(conArgTy);
1698 ty = fn(conArgTy,ty);
1699 if (nonNull(conArgNm)) {
1700 /* a field name is mentioned too */
1701 selTy = fn(resTy,conArgTy);
1702 if (whatIs(tycon(tc).kind) != STAR)
1703 selTy = pair(POLYTYPE,pair(tycon(tc).kind, selTy));
1704 selTy = tvsToOffsets(line,selTy, ktyvars);
1705 sels = cons( zpair(conArgNm,selTy), sels);
1709 /* Now ty is the constructor's type, not including context.
1710 Throw away any parts of the context not mentioned in
1711 tyvarsMentioned, and use it to qualify ty.
1714 for (ctx=ctx0; nonNull(ctx); ctx=tl(ctx)) {
1716 /* ctxElem :: ((QConId,VarId)) */
1717 if (nonNull(cellIsMember(textOf(zsnd(ctxElem)),tyvarsMentioned)))
1718 ctx2 = cons(ctxElem, ctx2);
1721 ty = ap(QUAL,pair(ctx2,ty));
1723 /* stick the tycon's kind on, if not simply STAR */
1724 if (whatIs(tycon(tc).kind) != STAR)
1725 ty = pair(POLYTYPE,pair(tycon(tc).kind, ty));
1727 ty = tvsToOffsets(line,ty, ktyvars);
1729 /* Finally, stick the constructor's type onto it. */
1730 hd(constrs) = ztriple(conid,fields,ty);
1733 /* Final result is that
1734 constrs :: [((ConId,[((Type,Text))],Type))]
1735 lists the constructors and their types
1736 sels :: [((VarId,Type))]
1737 lists the selectors and their types
1739 tycon(tc).defn = startGHCConstrs(line,constrs0,sels);
1744 static List startGHCConstrs ( Int line, List cons, List sels )
1746 /* cons :: [((ConId,[((Type,Text,Int))],Type))] */
1747 /* sels :: [((VarId,Type))] */
1748 /* returns [Name] */
1750 Int conNo = length(cons)>1 ? 1 : 0;
1751 for(cs=cons; nonNull(cs); cs=tl(cs), conNo++) {
1752 Name c = startGHCConstr(line,conNo,hd(cs));
1755 /* cons :: [Name] */
1757 for(ss=sels; nonNull(ss); ss=tl(ss)) {
1758 hd(ss) = startGHCSel(line,hd(ss));
1760 /* sels :: [Name] */
1761 return appendOnto(cons,sels);
1765 static Name startGHCSel ( Int line, ZPair sel )
1767 /* sel :: ((VarId, Type)) */
1768 Text t = textOf(zfst(sel));
1769 Type type = zsnd(sel);
1771 Name n = findName(t);
1773 ERRMSG(line) "Repeated definition for selector \"%s\"",
1779 name(n).line = line;
1780 name(n).number = SELNAME;
1783 name(n).type = type;
1788 static Name startGHCConstr ( Int line, Int conNo, ZTriple constr )
1790 /* constr :: ((ConId,[((Type,Text,Int))],Type)) */
1791 /* (ADR) ToDo: add rank2 annotation and existential annotation
1792 * these affect how constr can be used.
1794 Text con = textOf(zfst3(constr));
1795 Type type = zthd3(constr);
1796 Int arity = arityFromType(type);
1797 Name n = findName(con); /* Allocate constructor fun name */
1799 n = newName(con,NIL);
1800 } else if (name(n).defn!=PREDEFINED) {
1801 ERRMSG(line) "Repeated definition for constructor \"%s\"",
1805 name(n).arity = arity; /* Save constructor fun details */
1806 name(n).line = line;
1807 name(n).number = cfunNo(conNo);
1808 name(n).type = type;
1813 static List finishGHCDataDecl ( ConId tyc )
1816 Tycon tc = findTycon(textOf(tyc));
1818 fprintf ( stderr, "begin finishGHCDataDecl %s\n",
1819 textToStr(textOf(tyc)) );
1821 if (isNull(tc)) internal("finishGHCDataDecl");
1823 for (nms=tycon(tc).defn; nonNull(nms); nms=tl(nms)) {
1825 Int line = name(n).line;
1826 assert(currentModule == name(n).mod);
1827 name(n).type = conidcellsToTycons(line,name(n).type);
1828 name(n).parent = tc; //---????
1831 return tycon(tc).defn;
1835 /* --------------------------------------------------------------------------
1837 * ------------------------------------------------------------------------*/
1839 static Void startGHCNewType ( Int line, List ctx0,
1840 ConId tycon, List tvs, Cell constr )
1842 /* ctx0 :: [((QConId,VarId))] */
1843 /* tycon :: ConId */
1844 /* tvs :: [((VarId,Kind))] */
1845 /* constr :: ((ConId,Type)) or NIL if abstract */
1848 Text t = textOf(tycon);
1850 fprintf(stderr, "begin startGHCNewType %s\n", textToStr(t) );
1855 if (nonNull(findTycon(t))) {
1856 ERRMSG(line) "Repeated definition of type constructor \"%s\"",
1860 Tycon tc = newTycon(t);
1861 tycon(tc).line = line;
1862 tycon(tc).arity = length(tvs);
1863 tycon(tc).what = NEWTYPE;
1864 tycon(tc).kind = tvsToKind(tvs);
1865 /* can't really do this until I've read in all synonyms */
1867 if (isNull(constr)) {
1868 tycon(tc).defn = NIL;
1870 /* constr :: ((ConId,Type)) */
1871 Text con = textOf(zfst(constr));
1872 Type type = zsnd(constr);
1873 Name n = findName(con); /* Allocate constructor fun name */
1875 n = newName(con,NIL);
1876 } else if (name(n).defn!=PREDEFINED) {
1877 ERRMSG(line) "Repeated definition for constructor \"%s\"",
1881 name(n).arity = 1; /* Save constructor fun details */
1882 name(n).line = line;
1883 name(n).number = cfunNo(0);
1884 name(n).defn = nameId;
1885 tycon(tc).defn = singleton(n);
1887 /* make resTy the result type of the constr, T v1 ... vn */
1889 for (tmp=tvs; nonNull(tmp); tmp=tl(tmp))
1890 resTy = ap(resTy,zfst(hd(tmp)));
1891 type = fn(type,resTy);
1893 type = ap(QUAL,pair(ctx0,type));
1894 type = tvsToOffsets(line,type,tvs);
1895 name(n).type = type;
1901 static Void finishGHCNewType ( ConId tyc )
1903 Tycon tc = findTycon(textOf(tyc));
1905 fprintf ( stderr, "begin finishGHCNewType %s\n",
1906 textToStr(textOf(tyc)) );
1909 if (isNull(tc)) internal("finishGHCNewType");
1911 if (isNull(tycon(tc).defn)) {
1912 /* it's an abstract type */
1914 else if (length(tycon(tc).defn) == 1) {
1915 /* As we expect, has a single constructor */
1916 Name n = hd(tycon(tc).defn);
1917 Int line = name(n).line;
1918 assert(currentModule == name(n).mod);
1919 name(n).type = conidcellsToTycons(line,name(n).type);
1921 internal("finishGHCNewType(2)");
1926 /* --------------------------------------------------------------------------
1927 * Class declarations
1928 * ------------------------------------------------------------------------*/
1930 static Void startGHCClass(line,ctxt,tc_name,kinded_tvs,mems0)
1932 List ctxt; /* [((QConId, VarId))] */
1933 ConId tc_name; /* ConId */
1934 List kinded_tvs; /* [((VarId, Kind))] */
1935 List mems0; { /* [((VarId, Type))] */
1937 List mems; /* [((VarId, Type))] */
1938 List tvsInT; /* [VarId] and then [((VarId,Kind))] */
1939 List tvs; /* [((VarId,Kind))] */
1940 List ns; /* [Name] */
1943 ZPair kinded_tv = hd(kinded_tvs);
1944 Text ct = textOf(tc_name);
1945 Pair newCtx = pair(tc_name, zfst(kinded_tv));
1947 fprintf ( stderr, "begin startGHCClass %s\n", textToStr(ct) );
1951 if (length(kinded_tvs) != 1) {
1952 ERRMSG(line) "Cannot presently handle multiparam type classes in ifaces"
1956 if (nonNull(findClass(ct))) {
1957 ERRMSG(line) "Repeated definition of class \"%s\"",
1960 } else if (nonNull(findTycon(ct))) {
1961 ERRMSG(line) "\"%s\" used as both class and type constructor",
1965 Class nw = newClass(ct);
1966 cclass(nw).text = ct;
1967 cclass(nw).line = line;
1968 cclass(nw).arity = 1;
1969 cclass(nw).head = ap(nw,mkOffset(0));
1970 cclass(nw).kinds = singleton( zsnd(kinded_tv) );
1971 cclass(nw).instances = NIL;
1972 cclass(nw).numSupers = length(ctxt);
1974 /* Kludge to map the single tyvar in the context to Offset 0.
1975 Need to do something better for multiparam type classes.
1977 cclass(nw).supers = tvsToOffsets(line,ctxt,
1978 singleton(kinded_tv));
1981 for (mems=mems0; nonNull(mems); mems=tl(mems)) {
1982 ZPair mem = hd(mems);
1983 Type memT = zsnd(mem);
1984 Text mnt = textOf(zfst(mem));
1987 /* Stick the new context on the member type */
1988 memT = dictapsToQualtype(memT);
1989 if (whatIs(memT)==POLYTYPE) internal("startGHCClass");
1990 if (whatIs(memT)==QUAL) {
1992 pair(cons(newCtx,fst(snd(memT))),snd(snd(memT))));
1995 pair(singleton(newCtx),memT));
1998 /* Cook up a kind for the type. */
1999 tvsInT = ifTyvarsIn(memT);
2000 /* tvsInT :: [VarId] */
2002 /* ToDo: maximally bogus. We allow the class tyvar to
2003 have the kind as supplied by the parser, but we just
2004 assume that all others have kind *. It's a kludge.
2006 for (tvs=tvsInT; nonNull(tvs); tvs=tl(tvs)) {
2008 if (textOf(hd(tvs)) == textOf(zfst(kinded_tv)))
2009 k = zsnd(kinded_tv); else
2011 hd(tvs) = zpair(hd(tvs),k);
2013 /* tvsIntT :: [((VarId,Kind))] */
2015 memT = mkPolyType(tvsToKind(tvsInT),memT);
2016 memT = tvsToOffsets(line,memT,tvsInT);
2018 /* Park the type back on the member */
2019 mem = zpair(zfst(mem),memT);
2021 /* Bind code to the member */
2025 "Repeated definition for class method \"%s\"",
2029 mn = newName(mnt,NIL);
2034 cclass(nw).members = mems0;
2035 cclass(nw).numMembers = length(mems0);
2038 for (mno=0; mno<cclass(nw).numSupers; mno++) {
2039 ns = cons(newDSel(nw,mno),ns);
2041 cclass(nw).dsels = rev(ns);
2046 static Class finishGHCClass ( Tycon cls_tyc )
2051 Class nw = findClass ( textOf(cls_tyc) );
2053 fprintf ( stderr, "begin finishGHCClass %s\n", textToStr(cclass(nw).text) );
2055 if (isNull(nw)) internal("finishGHCClass");
2057 line = cclass(nw).line;
2059 assert (currentModule == cclass(nw).mod);
2061 cclass(nw).level = 0;
2062 cclass(nw).head = conidcellsToTycons(line,cclass(nw).head);
2063 cclass(nw).supers = conidcellsToTycons(line,cclass(nw).supers);
2064 cclass(nw).members = conidcellsToTycons(line,cclass(nw).members);
2066 for (mems=cclass(nw).members; nonNull(mems); mems=tl(mems)) {
2067 Pair mem = hd(mems); /* (VarId, Type) */
2068 Text txt = textOf(fst(mem));
2070 Name n = findName(txt);
2073 name(n).line = cclass(nw).line;
2075 name(n).number = ctr--;
2076 name(n).arity = arityInclDictParams(name(n).type);
2077 name(n).parent = nw;
2085 /* --------------------------------------------------------------------------
2087 * ------------------------------------------------------------------------*/
2089 static Inst startGHCInstance (line,ktyvars,cls,var)
2091 List ktyvars; /* [((VarId,Kind))] */
2092 Type cls; /* Type */
2093 VarId var; { /* VarId */
2094 List tmp, tvs, ks, spec;
2099 Inst in = newInst();
2101 fprintf ( stderr, "begin startGHCInstance\n" );
2106 tvs = ifTyvarsIn(cls); /* :: [VarId] */
2108 The order of tvs is important for tvsToOffsets.
2109 tvs should be a permutation of ktyvars. Fish the tyvar kinds
2110 out of ktyvars and attach them to tvs.
2112 for (xs1=tvs; nonNull(xs1); xs1=tl(xs1)) {
2114 for (xs2=ktyvars; nonNull(xs2); xs2=tl(xs2))
2115 if (textOf(hd(xs1)) == textOf(zfst(hd(xs2))))
2117 if (isNull(k)) internal("startGHCInstance: finding kinds");
2118 hd(xs1) = zpair(hd(xs1),k);
2121 cls = tvsToOffsets(line,cls,tvs);
2124 spec = cons(fun(cls),spec);
2127 spec = reverse(spec);
2129 inst(in).line = line;
2130 inst(in).implements = NIL;
2131 inst(in).kinds = simpleKind(length(tvs)); /* do this right */
2132 inst(in).specifics = spec;
2133 inst(in).numSpecifics = length(spec);
2134 inst(in).head = cls;
2136 /* Figure out the name of the class being instanced, and store it
2137 at inst(in).c. finishGHCInstance will resolve it to a real Class. */
2139 Cell cl = inst(in).head;
2140 assert(whatIs(cl)==DICTAP);
2141 cl = unap(DICTAP,cl);
2143 assert ( isQCon(cl) );
2148 Name b = newName( /*inventText()*/ textOf(var),NIL);
2149 name(b).line = line;
2150 name(b).arity = length(spec); /* unused? */ /* and surely wrong */
2151 name(b).number = DFUNNAME;
2152 name(b).parent = in;
2153 inst(in).builder = b;
2154 /* bindNameToClosure(b, lookupGHCClosure(inst(in).mod,var)); */
2161 static Void finishGHCInstance ( Inst in )
2168 fprintf ( stderr, "begin finishGHCInstance\n" );
2171 assert (nonNull(in));
2172 line = inst(in).line;
2173 assert (currentModule==inst(in).mod);
2175 /* inst(in).c is, prior to finishGHCInstance, a ConId or Tuple,
2176 since startGHCInstance couldn't possibly have resolved it to
2177 a Class at that point. We convert it to a Class now.
2181 c = findQualClassWithoutConsultingExportList(c);
2185 inst(in).head = conidcellsToTycons(line,inst(in).head);
2186 inst(in).specifics = conidcellsToTycons(line,inst(in).specifics);
2187 cclass(c).instances = cons(in,cclass(c).instances);
2191 /* --------------------------------------------------------------------------
2193 * ------------------------------------------------------------------------*/
2195 /* This is called from the startGHC* functions. It traverses a structure
2196 and converts varidcells, ie, type variables parsed by the interface
2197 parser, into Offsets, which is how Hugs wants to see them internally.
2198 The Offset for a type variable is determined by its place in the list
2199 passed as the second arg; the associated kinds are irrelevant.
2201 ((t1,t2)) denotes the typed (z-)pair of t1 and t2.
2204 /* tvsToOffsets :: LineNo -> Type -> [((VarId,Kind))] -> Type */
2205 static Type tvsToOffsets(line,type,ktyvars)
2208 List ktyvars; { /* [((VarId,Kind))] */
2209 switch (whatIs(type)) {
2216 case ZTUP2: /* convert to the untyped representation */
2217 return ap( tvsToOffsets(line,zfst(type),ktyvars),
2218 tvsToOffsets(line,zsnd(type),ktyvars) );
2220 return ap( tvsToOffsets(line,fun(type),ktyvars),
2221 tvsToOffsets(line,arg(type),ktyvars) );
2225 tvsToOffsets(line,monotypeOf(type),ktyvars)
2229 return pair(QUAL,pair(tvsToOffsets(line,fst(snd(type)),ktyvars),
2230 tvsToOffsets(line,snd(snd(type)),ktyvars)));
2231 case DICTAP: /* bogus ?? */
2232 return ap(DICTAP, tvsToOffsets(line,snd(type),ktyvars));
2233 case UNBOXEDTUP: /* bogus?? */
2234 return ap(UNBOXEDTUP, tvsToOffsets(line,snd(type),ktyvars));
2235 case BANG: /* bogus?? */
2236 return ap(BANG, tvsToOffsets(line,snd(type),ktyvars));
2237 case VARIDCELL: /* Ha! some real work to do! */
2239 Text tv = textOf(type);
2240 for (; nonNull(ktyvars); i++,ktyvars=tl(ktyvars)) {
2243 assert(isZPair(hd(ktyvars)));
2244 varid = zfst(hd(ktyvars));
2246 if (tv == tt) return mkOffset(i);
2248 ERRMSG(line) "Undefined type variable \"%s\"", textToStr(tv)
2253 fprintf(stderr, "tvsToOffsets: unknown stuff %d\n", whatIs(type));
2255 fprintf(stderr,"\n");
2259 return NIL; /* NOTREACHED */
2263 /* This is called from the finishGHC* functions. It traverses a structure
2264 and converts conidcells, ie, type constructors parsed by the interface
2265 parser, into Tycons (or Classes), which is how Hugs wants to see them
2266 internally. Calls to this fn have to be deferred to the second phase
2267 of interface loading (finishGHC* rather than startGHC*) so that all relevant
2268 Tycons or Classes have been loaded into the symbol tables and can be
2271 static Type conidcellsToTycons ( Int line, Type type )
2273 switch (whatIs(type)) {
2283 { Cell t; /* Tycon or Class */
2284 Text m = qmodOf(type);
2285 Module mod = findModule(m);
2288 "Undefined module in qualified name \"%s\"",
2293 t = findQualTyconWithoutConsultingExportList(type);
2294 if (nonNull(t)) return t;
2295 t = findQualClassWithoutConsultingExportList(type);
2296 if (nonNull(t)) return t;
2298 "Undefined qualified class or type \"%s\"",
2306 cl = findQualClass(type);
2307 if (nonNull(cl)) return cl;
2308 if (textOf(type)==findText("[]"))
2309 /* a hack; magically qualify [] into PrelBase.[] */
2310 return conidcellsToTycons(line,
2311 mkQualId(mkCon(findText("PrelBase")),type));
2312 tc = findQualTycon(type);
2313 if (nonNull(tc)) return tc;
2315 "Undefined class or type constructor \"%s\"",
2321 return ap( conidcellsToTycons(line,fun(type)),
2322 conidcellsToTycons(line,arg(type)) );
2323 case ZTUP2: /* convert to std pair */
2324 return ap( conidcellsToTycons(line,zfst(type)),
2325 conidcellsToTycons(line,zsnd(type)) );
2330 conidcellsToTycons(line,monotypeOf(type))
2334 return pair(QUAL,pair(conidcellsToTycons(line,fst(snd(type))),
2335 conidcellsToTycons(line,snd(snd(type)))));
2336 case DICTAP: /* :: ap(DICTAP, pair(Class,Type))
2337 Not sure if this is really the right place to
2338 convert it to the form Hugs wants, but will do so anyway.
2340 /* return ap(DICTAP, conidcellsToTycons(line, snd(type))); */
2342 Class cl = fst(unap(DICTAP,type));
2343 List args = snd(unap(DICTAP,type));
2345 conidcellsToTycons(line,pair(cl,args));
2348 return ap(UNBOXEDTUP, conidcellsToTycons(line, snd(type)));
2350 return ap(BANG, conidcellsToTycons(line, snd(type)));
2352 fprintf(stderr, "conidcellsToTycons: unknown stuff %d\n",
2355 fprintf(stderr,"\n");
2359 return NIL; /* NOTREACHED */
2363 /* Find out if a type mentions a type constructor not present in
2364 the supplied list of qualified tycons.
2366 static Bool allTypesKnown ( Type type,
2367 List aktys /* [QualId] */,
2370 switch (whatIs(type)) {
2377 return allTypesKnown(fun(type),aktys,thisMod)
2378 && allTypesKnown(arg(type),aktys,thisMod);
2380 return allTypesKnown(zfst(type),aktys,thisMod)
2381 && allTypesKnown(zsnd(type),aktys,thisMod);
2383 return allTypesKnown(unap(DICTAP,type),aktys,thisMod);
2386 if (textOf(type)==findText("[]"))
2387 /* a hack; magically qualify [] into PrelBase.[] */
2388 type = mkQualId(mkCon(findText("PrelBase")),type); else
2389 type = mkQualId(thisMod,type);
2392 if (isNull(qualidIsMember(type,aktys))) goto missing;
2398 fprintf(stderr, "allTypesKnown: unknown stuff %d\n", whatIs(type));
2399 print(type,10);printf("\n");
2400 internal("allTypesKnown");
2401 return TRUE; /*notreached*/
2405 fprintf ( stderr,"allTypesKnown: unknown " ); print(type,10);
2406 fprintf(stderr,"\n");
2412 /* --------------------------------------------------------------------------
2415 * None of these do lookups or require that lookups have been resolved
2416 * so they can be performed while reading interfaces.
2417 * ------------------------------------------------------------------------*/
2419 /* tvsToKind :: [((VarId,Kind))] -> Kinds */
2420 static Kinds tvsToKind(tvs)
2421 List tvs; { /* [((VarId,Kind))] */
2424 for (rs=reverse(tvs); nonNull(rs); rs=tl(rs)) {
2425 if (whatIs(hd(rs)) != ZTUP2) internal("tvsToKind(1)");
2426 if (whatIs(zfst(hd(rs))) != VARIDCELL) internal("tvsToKind(2)");
2427 r = ap(zsnd(hd(rs)),r);
2433 static Int arityInclDictParams ( Type type )
2436 if (isPolyType(type)) type = monotypeOf(type);
2438 if (whatIs(type) == QUAL)
2440 arity += length ( fst(snd(type)) );
2441 type = snd(snd(type));
2443 while (isAp(type) && getHead(type)==typeArrow) {
2450 /* arity of a constructor with this type */
2451 static Int arityFromType(type)
2454 if (isPolyType(type)) {
2455 type = monotypeOf(type);
2457 if (whatIs(type) == QUAL) {
2458 type = snd(snd(type));
2460 if (whatIs(type) == EXIST) {
2461 type = snd(snd(type));
2463 if (whatIs(type)==RANK2) {
2464 type = snd(snd(type));
2466 while (isAp(type) && getHead(type)==typeArrow) {
2474 /* ifTyvarsIn :: Type -> [VarId]
2475 The returned list has no duplicates -- is a set.
2477 static List ifTyvarsIn(type)
2479 List vs = typeVarsIn(type,NIL,NIL,NIL);
2481 for (; nonNull(vs2); vs2=tl(vs2))
2482 if (whatIs(hd(vs2)) != VARIDCELL)
2483 internal("ifTyvarsIn");
2489 /* --------------------------------------------------------------------------
2490 * General object symbol query stuff
2491 * ------------------------------------------------------------------------*/
2493 #define EXTERN_SYMS_ALLPLATFORMS \
2494 Sym(stg_gc_enter_1) \
2495 Sym(stg_gc_noregs) \
2503 Sym(stg_update_PAP) \
2504 Sym(stg_error_entry) \
2505 Sym(__ap_2_upd_info) \
2506 Sym(__ap_3_upd_info) \
2507 Sym(__ap_4_upd_info) \
2508 Sym(__ap_5_upd_info) \
2509 Sym(__ap_6_upd_info) \
2510 Sym(__ap_7_upd_info) \
2511 Sym(__ap_8_upd_info) \
2512 Sym(__sel_0_upd_info) \
2513 Sym(__sel_1_upd_info) \
2514 Sym(__sel_2_upd_info) \
2515 Sym(__sel_3_upd_info) \
2516 Sym(__sel_4_upd_info) \
2517 Sym(__sel_5_upd_info) \
2518 Sym(__sel_6_upd_info) \
2519 Sym(__sel_7_upd_info) \
2520 Sym(__sel_8_upd_info) \
2521 Sym(__sel_9_upd_info) \
2522 Sym(__sel_10_upd_info) \
2523 Sym(__sel_11_upd_info) \
2524 Sym(__sel_12_upd_info) \
2526 Sym(Upd_frame_info) \
2527 Sym(seq_frame_info) \
2528 Sym(CAF_BLACKHOLE_info) \
2529 Sym(IND_STATIC_info) \
2530 Sym(EMPTY_MVAR_info) \
2531 Sym(MUT_ARR_PTRS_FROZEN_info) \
2533 Sym(putMVarzh_fast) \
2534 Sym(newMVarzh_fast) \
2535 Sym(takeMVarzh_fast) \
2540 Sym(killThreadzh_fast) \
2541 Sym(waitReadzh_fast) \
2542 Sym(waitWritezh_fast) \
2543 Sym(CHARLIKE_closure) \
2544 Sym(INTLIKE_closure) \
2545 Sym(suspendThread) \
2547 Sym(stackOverflow) \
2548 Sym(int2Integerzh_fast) \
2549 Sym(stg_gc_unbx_r1) \
2551 Sym(makeForeignObjzh_fast) \
2552 Sym(__encodeDouble) \
2553 Sym(decodeDoublezh_fast) \
2555 Sym(isDoubleInfinite) \
2556 Sym(isDoubleDenormalized) \
2557 Sym(isDoubleNegativeZero) \
2558 Sym(__encodeFloat) \
2559 Sym(decodeFloatzh_fast) \
2561 Sym(isFloatInfinite) \
2562 Sym(isFloatDenormalized) \
2563 Sym(isFloatNegativeZero) \
2564 Sym(__int_encodeFloat) \
2565 Sym(__int_encodeDouble) \
2569 Sym(gcdIntegerzh_fast) \
2570 Sym(newArrayzh_fast) \
2571 Sym(unsafeThawArrayzh_fast) \
2572 Sym(newDoubleArrayzh_fast) \
2573 Sym(newFloatArrayzh_fast) \
2574 Sym(newAddrArrayzh_fast) \
2575 Sym(newWordArrayzh_fast) \
2576 Sym(newIntArrayzh_fast) \
2577 Sym(newCharArrayzh_fast) \
2578 Sym(newMutVarzh_fast) \
2579 Sym(quotRemIntegerzh_fast) \
2580 Sym(quotIntegerzh_fast) \
2581 Sym(remIntegerzh_fast) \
2582 Sym(divExactIntegerzh_fast) \
2583 Sym(divModIntegerzh_fast) \
2584 Sym(timesIntegerzh_fast) \
2585 Sym(minusIntegerzh_fast) \
2586 Sym(plusIntegerzh_fast) \
2587 Sym(addr2Integerzh_fast) \
2588 Sym(mkWeakzh_fast) \
2591 Sym(resetNonBlockingFd) \
2593 Sym(stable_ptr_table) \
2594 Sym(createAdjThunk) \
2595 Sym(shutdownHaskellAndExit) \
2596 Sym(stg_enterStackTop) \
2597 Sym(CAF_UNENTERED_entry) \
2598 Sym(stg_yield_to_Hugs) \
2601 /* needed by libHS_cbits */ \
2640 #define EXTERN_SYMS_cygwin32 \
2641 SymX(GetCurrentProcess) \
2642 SymX(GetProcessTimes) \
2651 Sym(__imp__tzname) \
2652 Sym(__imp__timezone) \
2671 #define EXTERN_SYMS_linux \
2672 Sym(__errno_location) \
2684 #if defined(linux_TARGET_OS)
2685 #define EXTERN_SYMS_THISPLATFORM EXTERN_SYMS_linux
2688 #if defined(solaris2_TARGET_OS)
2689 #define EXTERN_SYMS_THISPLATFORM EXTERN_SYMS_solaris2
2692 #if defined(cygwin32_TARGET_OS)
2693 #define EXTERN_SYMS_THISPLATFORM EXTERN_SYMS_cygwin32
2699 /* entirely bogus claims about types of these symbols */
2700 #define Sym(vvv) extern void (vvv);
2701 #define SymX(vvv) /**/
2702 EXTERN_SYMS_ALLPLATFORMS
2703 EXTERN_SYMS_THISPLATFORM
2708 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
2710 #define SymX(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
2714 EXTERN_SYMS_ALLPLATFORMS
2715 EXTERN_SYMS_THISPLATFORM
2722 /* A kludge to assist Win32 debugging. */
2723 char* nameFromStaticOPtr ( void* ptr )
2726 for (k = 0; rtsTab[k].nm; k++)
2727 if (ptr == rtsTab[k].ad)
2728 return rtsTab[k].nm;
2733 static void* lookupObjName ( char* nm )
2741 int first_real_char;
2744 strncpy(nm2,nm,200);
2746 /* first see if it's an RTS name */
2747 for (k = 0; rtsTab[k].nm; k++)
2748 if (0==strcmp(nm2,rtsTab[k].nm))
2749 return rtsTab[k].ad;
2751 /* perhaps an extra-symbol ? */
2752 a = lookupOExtraTabName ( nm );
2755 /* if not an RTS name, look in the
2756 relevant module's object symbol table
2758 # if LEADING_UNDERSCORE
2759 first_real_char = 1;
2761 first_real_char = 0;
2763 pp = strchr(nm2+first_real_char, '_');
2764 if (!pp || !isupper(nm2[first_real_char])) goto not_found;
2766 t = unZcodeThenFindText(nm2+first_real_char);
2768 if (isNull(m)) goto not_found;
2770 a = lookupOTabName ( m, nm ); /* RATIONALISE */
2775 "lookupObjName: can't resolve name `%s'\n",
2782 int is_dynamically_loaded_code_or_rodata_ptr ( char* p )
2784 OSectionKind sk = lookupSection(p);
2785 assert (sk != HUGS_SECTIONKIND_NOINFOAVAIL);
2786 return (sk == HUGS_SECTIONKIND_CODE_OR_RODATA);
2790 int is_dynamically_loaded_rwdata_ptr ( char* p )
2792 OSectionKind sk = lookupSection(p);
2793 assert (sk != HUGS_SECTIONKIND_NOINFOAVAIL);
2794 return (sk == HUGS_SECTIONKIND_RWDATA);
2798 int is_not_dynamically_loaded_ptr ( char* p )
2800 OSectionKind sk = lookupSection(p);
2801 assert (sk != HUGS_SECTIONKIND_NOINFOAVAIL);
2802 return (sk == HUGS_SECTIONKIND_OTHER);
2806 /* --------------------------------------------------------------------------
2808 * ------------------------------------------------------------------------*/
2810 Void interface(what)
2813 case POSTPREL: break;
2817 ifaces_outstanding = NIL;
2820 mark(ifaces_outstanding);
2825 /*-------------------------------------------------------------------------*/