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/14 14:34:47 $
12 * ------------------------------------------------------------------------*/
20 #include "Assembler.h" /* for wrapping GHC objects */
23 /*#define DEBUG_IFACE*/
26 /* --------------------------------------------------------------------------
27 * (This comment is now out of date. JRS, 991216).
28 * The "addGHC*" functions act as "impedence matchers" between GHC
29 * interface files and Hugs. Their main job is to convert abstract
30 * syntax trees into Hugs' internal representations.
32 * The main trick here is how we deal with mutually recursive interface
35 * o As we read an import decl, we add it to a list of required imports
36 * (unless it's already loaded, of course).
38 * o Processing of declarations is split into two phases:
40 * 1) While reading the interface files, we construct all the Names,
41 * Tycons, etc declared in the interface file but we don't try to
42 * resolve references to any entities the declaration mentions.
44 * This is done by the "addGHC*" functions.
46 * 2) After reading all the interface files, we finish processing the
47 * declarations by resolving any references in the declarations
48 * and doing any other processing that may be required.
50 * This is done by the "finishGHC*" functions which use the
51 * "fixup*" functions to assist them.
53 * The interface between these two phases are the "ghc*Decls" which
54 * contain lists of decls that haven't been completed yet.
56 * ------------------------------------------------------------------------*/
60 New comment, 991216, explaining roughly how it all works.
61 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
63 Interfaces can contain references to unboxed types, and these need to
64 be handled carefully. The following is a summary of how the interface
65 loader now works. It is applied to groups of interfaces simultaneously,
66 viz, the entire Prelude at once:
68 0. Parse interfaces, chasing imports until a complete
69 strongly-connected-component of ifaces has been parsed.
70 All interfaces in this scc are processed together, in
73 1. Throw away any entity not mentioned in the export lists.
75 2. Delete type (not data or newtype) definitions which refer to
76 unknown types in their right hand sides. Because Hugs doesn't
77 know of any unboxed types, this has the side effect of removing
78 all type defns referring to unboxed types. Repeat step 2 until
79 a fixed point is reached.
81 3. Make abstract all data/newtype defns which refer to an unknown
82 type. eg, data Word = MkW Word# becomes data Word, because
83 Word# is unknown. Hugs is happy to know about abstract boxed
84 Words, but not about Word#s.
86 4. Step 2 could delete types referred to by values, instances and
87 classes. So filter all entities, and delete those referring to
88 unknown types _or_ classes. This could cause other entities
89 to become invalid, so iterate step 4 to a fixed point.
91 After step 4, the interfaces no longer contain anything
94 5. Steps 1-4 operate purely on the iface syntax trees. We now start
95 creating symbol table entries. First, create a module table
96 entry for each interface, and locate and read in the corresponding
97 object file. This is done by the startGHCModule function.
99 6. Traverse all interfaces. For each entity, create an entry in
100 the name, tycon, class or instance table, and fill in relevant
101 fields, but do not attempt to link tycon/class/instance/name uses
102 to their symbol table entries. This is done by the startGHC*
105 7. Revisit all symbol table entries created in step 6. We should
106 now be able to replace all references to tycons/classes/instances/
107 names with the relevant symbol table entries. This is done by
108 the finishGHC* functions.
110 8. Traverse all interfaces. For each iface, examine the export lists
111 and use it to build export lists in the module table. Do the
112 implicit 'import Prelude' thing if necessary. Finally, resolve
113 references in the object code for this module. This is done
114 by the finishGHCModule function.
117 /* --------------------------------------------------------------------------
118 * local function prototypes:
119 * ------------------------------------------------------------------------*/
121 static Void startGHCValue ( Int,VarId,Type );
122 static Void finishGHCValue ( VarId );
124 static Void startGHCSynonym ( Int,Cell,List,Type );
125 static Void finishGHCSynonym ( Tycon );
127 static Void startGHCClass ( Int,List,Cell,List,List );
128 static Class finishGHCClass ( Class );
130 static Inst startGHCInstance ( Int,List,Pair,VarId );
131 static Void finishGHCInstance ( Inst );
133 static Void startGHCImports ( ConId,List );
134 static Void finishGHCImports ( ConId,List );
136 static Void startGHCExports ( ConId,List );
137 static Void finishGHCExports ( ConId,List );
139 static Void finishGHCFixdecl ( Cell prec, Cell assoc, ConVarId name );
141 static Void finishGHCModule ( Cell );
142 static Void startGHCModule ( Text, Int, Text );
144 static Void startGHCDataDecl ( Int,List,Cell,List,List );
145 static List finishGHCDataDecl ( ConId tyc );
146 /* Supporting stuff for {start|finish}GHCDataDecl */
147 static List startGHCConstrs ( Int,List,List );
148 static Name startGHCSel ( Int,Pair );
149 static Name startGHCConstr ( Int,Int,Triple );
151 static Void startGHCNewType ( Int,List,Cell,List,Cell );
152 static Void finishGHCNewType ( ConId tyc );
156 static Kinds tvsToKind ( List );
157 static Int arityFromType ( Type );
158 static Int arityInclDictParams ( Type );
159 static Bool allTypesKnown ( Type type,
160 List aktys /* [QualId] */,
163 static List ifTyvarsIn ( Type );
164 static Type tvsToOffsets ( Int,Type,List );
165 static Type conidcellsToTycons ( Int,Type );
171 /* --------------------------------------------------------------------------
172 * Top-level interface processing
173 * ------------------------------------------------------------------------*/
175 /* getIEntityName :: I_IMPORT..I_VALUE -> ConVarId | NIL */
176 static ConVarId getIEntityName ( Cell c )
179 case I_IMPORT: return NIL;
180 case I_INSTIMPORT: return NIL;
181 case I_EXPORT: return NIL;
182 case I_FIXDECL: return zthd3(unap(I_FIXDECL,c));
183 case I_INSTANCE: return NIL;
184 case I_TYPE: return zsel24(unap(I_TYPE,c));
185 case I_DATA: return zsel35(unap(I_DATA,c));
186 case I_NEWTYPE: return zsel35(unap(I_NEWTYPE,c));
187 case I_CLASS: return zsel35(unap(I_CLASS,c));
188 case I_VALUE: return zsnd3(unap(I_VALUE,c));
189 default: internal("getIEntityName");
194 /* Filter the contents of an interface, using the supplied predicate.
195 For flexibility, the predicate is passed as a second arg the value
196 extraArgs. This is a hack to get round the lack of partial applications
197 in C. Pred should not have any side effects. The dumpaction param
198 gives us the chance to print a message or some such for dumped items.
199 When a named entity is deleted, filterInterface also deletes the name
202 static Cell filterInterface ( Cell root,
203 Bool (*pred)(Cell,Cell),
205 Void (*dumpAction)(Cell) )
208 Cell iface = unap(I_INTERFACE,root);
210 List deleted_ids = NIL; /* :: [ConVarId] */
212 for (tops = zsnd(iface); nonNull(tops); tops=tl(tops)) {
213 if (pred(hd(tops),extraArgs)) {
214 tops2 = cons( hd(tops), tops2 );
216 ConVarId deleted_id = getIEntityName ( hd(tops) );
217 if (nonNull(deleted_id))
218 deleted_ids = cons ( deleted_id, deleted_ids );
220 dumpAction ( hd(tops) );
223 tops2 = reverse(tops2);
225 /* Clean up the export list now. */
226 for (tops=tops2; nonNull(tops); tops=tl(tops)) {
227 if (whatIs(hd(tops))==I_EXPORT) {
228 Cell exdecl = unap(I_EXPORT,hd(tops));
229 List exlist = zsnd(exdecl);
231 for (; nonNull(exlist); exlist=tl(exlist)) {
232 Cell ex = hd(exlist);
233 ConVarId exid = isZPair(ex) ? zfst(ex) : ex;
234 assert (isCon(exid) || isVar(exid));
235 if (!varIsMember(textOf(exid),deleted_ids))
236 exlist2 = cons(ex, exlist2);
238 hd(tops) = ap(I_EXPORT,zpair(zfst(exdecl),exlist2));
242 return ap(I_INTERFACE, zpair(zfst(iface),tops2));
246 ZPair readInterface(String fname, Long fileSize)
250 ZPair iface = parseInterface(fname,fileSize);
251 assert (whatIs(iface)==I_INTERFACE);
253 for (tops = zsnd(unap(I_INTERFACE,iface)); nonNull(tops); tops=tl(tops))
254 if (whatIs(hd(tops)) == I_IMPORT) {
255 ZPair imp_decl = unap(I_IMPORT,hd(tops));
256 ConId m_to_imp = zfst(imp_decl);
257 if (textOf(m_to_imp) != findText("PrelGHC")) {
258 imports = cons(m_to_imp,imports);
260 fprintf(stderr, "add iface %s\n",
261 textToStr(textOf(m_to_imp)));
265 return zpair(iface,imports);
269 /* getExportDeclsInIFace :: I_INTERFACE -> [I_EXPORT] */
270 static List getExportDeclsInIFace ( Cell root )
272 Cell iface = unap(I_INTERFACE,root);
273 List decls = zsnd(iface);
276 for (ds=decls; nonNull(ds); ds=tl(ds))
277 if (whatIs(hd(ds))==I_EXPORT)
278 exports = cons(hd(ds), exports);
283 /* Does t start with "$dm" ? */
284 static Bool isIfaceDefaultMethodName ( Text t )
286 String s = textToStr(t);
287 return (s && s[0]=='$' && s[1]=='d' && s[2]=='m' && s[3]);
291 static Bool isExportedIFaceEntity ( Cell ife, List exlist_list )
293 /* ife :: I_IMPORT..I_VALUE */
294 /* exlist_list :: [[ ConVarId | ((ConId, [ConVarId])) ]] */
300 ConVarId ife_id = getIEntityName ( ife );
302 if (isNull(ife_id)) return TRUE;
304 tnm = textOf(ife_id);
306 /* Don't junk default methods, even tho the export list doesn't
309 if (isIfaceDefaultMethodName(tnm)) goto retain;
311 /* for each export list ... */
312 for (; nonNull(exlist_list); exlist_list=tl(exlist_list)) {
313 exlist = hd(exlist_list);
315 /* for each entity in an export list ... */
316 for (t=exlist; nonNull(t); t=tl(t)) {
317 if (isZPair(hd(t))) {
318 /* A pair, which means an export entry
319 of the form ClassName(foo,bar). */
320 List subents = cons(zfst(hd(t)),zsnd(hd(t)));
321 for (; nonNull(subents); subents=tl(subents))
322 if (textOf(hd(subents)) == tnm) goto retain;
324 /* Single name in the list. */
325 if (textOf(hd(t)) == tnm) goto retain;
331 fprintf ( stderr, " dump %s\n", textToStr(tnm) );
337 fprintf ( stderr, " retain %s\n", textToStr(tnm) );
343 static Bool isExportedAbstractly ( ConId ife_id, List exlist_list )
345 /* ife_id :: ConId */
346 /* exlist_list :: [[ ConVarId | ((ConId, [ConVarId])) ]] */
351 assert (isCon(ife_id));
352 tnm = textOf(ife_id);
354 /* for each export list ... */
355 for (; nonNull(exlist_list); exlist_list=tl(exlist_list)) {
356 exlist = hd(exlist_list);
358 /* for each entity in an export list ... */
359 for (t=exlist; nonNull(t); t=tl(t)) {
360 if (isZPair(hd(t))) {
361 /* A pair, which means an export entry
362 of the form ClassName(foo,bar). */
363 if (textOf(zfst(hd(t))) == tnm) return FALSE;
365 if (textOf(hd(t)) == tnm) return TRUE;
369 internal("isExportedAbstractly");
370 return FALSE; /*notreached*/
374 /* Remove entities not mentioned in any of the export lists. */
375 static Cell deleteUnexportedIFaceEntities ( Cell root )
377 Cell iface = unap(I_INTERFACE,root);
378 ConId iname = zfst(iface);
379 List decls = zsnd(iface);
381 List exlist_list = NIL;
385 fprintf(stderr, "\ncleanIFace: %s\n", textToStr(textOf(iname)));
388 exlist_list = getExportDeclsInIFace ( root );
389 /* exlist_list :: [I_EXPORT] */
391 for (t=exlist_list; nonNull(t); t=tl(t))
392 hd(t) = zsnd(unap(I_EXPORT,hd(t)));
393 /* exlist_list :: [[ ConVarId | ((ConId, [ConVarId])) ]] */
395 if (isNull(exlist_list)) {
396 ERRMSG(0) "Can't find any export lists in interface file"
400 return filterInterface ( root, isExportedIFaceEntity,
405 /* addTyconsAndClassesFromIFace :: I_INTERFACE -> [QualId] -> [QualId] */
406 static List addTyconsAndClassesFromIFace ( Cell root, List aktys )
408 Cell iface = unap(I_INTERFACE,root);
409 Text mname = textOf(zfst(iface));
410 List defns = zsnd(iface);
411 for (; nonNull(defns); defns = tl(defns)) {
412 Cell defn = hd(defns);
413 Cell what = whatIs(defn);
414 if (what==I_TYPE || what==I_DATA
415 || what==I_NEWTYPE || what==I_CLASS) {
416 QualId q = mkQCon ( mname, textOf(getIEntityName(defn)) );
417 if (!qualidIsMember ( q, aktys ))
418 aktys = cons ( q, aktys );
425 static Void ifentityAllTypesKnown_dumpmsg ( Cell entity )
427 ConVarId id = getIEntityName ( entity );
430 "dumping %s because of unknown type(s)\n",
431 isNull(id) ? "(nameless entity?!)" : textToStr(textOf(id)) );
436 /* ifentityAllTypesKnown :: I_IMPORT..I_VALUE -> (([QualId], ConId)) -> Bool */
437 /* mod is the current module being processed -- so we can qualify unqual'd
438 names. Strange calling convention for aktys and mod is so we can call this
439 from filterInterface.
441 static Bool ifentityAllTypesKnown ( Cell entity, ZPair aktys_mod )
444 List aktys = zfst ( aktys_mod );
445 ConId mod = zsnd ( aktys_mod );
446 switch (whatIs(entity)) {
453 Cell inst = unap(I_INSTANCE,entity);
454 List ctx = zsel25 ( inst ); /* :: [((QConId,VarId))] */
455 Type cls = zsel35 ( inst ); /* :: Type */
456 for (t = ctx; nonNull(t); t=tl(t))
457 if (!allTypesKnown(zfst(hd(t)),aktys,mod)) return FALSE;
458 if (!allTypesKnown(cls, aktys,mod)) return FALSE;
462 return allTypesKnown( zsel44(unap(I_TYPE,entity)), aktys,mod );
464 Cell data = unap(I_DATA,entity);
465 List ctx = zsel25 ( data ); /* :: [((QConId,VarId))] */
466 List constrs = zsel55 ( data ); /* :: [ ((ConId, [((Type,VarId,Int))] )) ] */
467 for (t = ctx; nonNull(t); t=tl(t))
468 if (!allTypesKnown(zfst(hd(t)),aktys,mod)) return FALSE;
469 for (t = constrs; nonNull(t); t=tl(t))
470 for (u = zsnd(hd(t)); nonNull(u); u=tl(u))
471 if (!allTypesKnown(zfst3(hd(u)),aktys,mod)) return FALSE;
475 Cell newty = unap(I_NEWTYPE,entity);
476 List ctx = zsel25(newty); /* :: [((QConId,VarId))] */
477 ZPair constr = zsel55 ( newty ); /* :: ((ConId,Type)) */
478 for (t = ctx; nonNull(t); t=tl(t))
479 if (!allTypesKnown(zfst(hd(t)),aktys,mod)) return FALSE;
481 && !allTypesKnown(zsnd(constr),aktys,mod)) return FALSE;
485 Cell klass = unap(I_CLASS,entity);
486 List ctx = zsel25(klass); /* :: [((QConId,VarId))] */
487 List sigs = zsel55(klass); /* :: [((VarId,Type))] */
488 for (t = ctx; nonNull(t); t=tl(t))
489 if (!allTypesKnown(zfst(hd(t)),aktys,mod)) return FALSE;
490 for (t = sigs; nonNull(t); t=tl(t))
491 if (!allTypesKnown(zsnd(hd(t)),aktys,mod)) return FALSE;
495 return allTypesKnown( zthd3(unap(I_VALUE,entity)), aktys,mod );
497 internal("ifentityAllTypesKnown");
502 /* ifTypeDoesntRefUnknownTycon :: I_IMPORT..I_VALUE -> (([QualId], ConId)) -> Bool */
503 /* mod is the current module being processed -- so we can qualify unqual'd
504 names. Strange calling convention for aktys and mod is so we can call this
505 from filterInterface.
507 static Bool ifTypeDoesntRefUnknownTycon ( Cell entity, ZPair aktys_mod )
510 List aktys = zfst ( aktys_mod );
511 ConId mod = zsnd ( aktys_mod );
512 if (whatIs(entity) != I_TYPE) {
515 return allTypesKnown( zsel44(unap(I_TYPE,entity)), aktys,mod );
520 static Void ifTypeDoesntRefUnknownTycon_dumpmsg ( Cell entity )
522 ConVarId id = getIEntityName ( entity );
523 assert (whatIs(entity)==I_TYPE);
527 "dumping type %s because of unknown tycon(s)\n",
528 textToStr(textOf(id)) );
533 /* abstractifyExport :: I_EXPORT -> ConId -> I_EXPORT
535 static List abstractifyExDecl ( Cell root, ConId toabs )
537 ZPair exdecl = unap(I_EXPORT,root);
538 List exlist = zsnd(exdecl);
540 for (; nonNull(exlist); exlist = tl(exlist)) {
541 if (isZPair(hd(exlist))
542 && textOf(toabs) == textOf(zfst(hd(exlist)))) {
543 /* it's toabs, exported non-abstractly */
544 res = cons ( zfst(hd(exlist)), res );
546 res = cons ( hd(exlist), res );
549 return ap(I_EXPORT,zpair(zfst(exdecl),reverse(res)));
553 static Void ppModule ( Text modt )
556 fflush(stderr); fflush(stdout);
557 fprintf(stderr, "---------------- MODULE %s ----------------\n",
563 static void* ifFindItblFor ( Name n )
565 /* n is a constructor for which we want to find the GHC info table.
566 First look for a _con_info symbol. If that doesn't exist, _and_
567 this is a nullary constructor, then it's safe to look for the
568 _static_info symbol instead.
574 sprintf ( buf, MAYBE_LEADING_UNDERSCORE_STR("%s_%s_con_info"),
575 textToStr( module(name(n).mod).text ),
576 textToStr( name(n).text ) );
577 t = enZcodeThenFindText(buf);
578 p = lookupOTabName ( name(n).mod, textToStr(t) );
582 if (name(n).arity == 0) {
583 sprintf ( buf, MAYBE_LEADING_UNDERSCORE_STR("%s_%s_static_info"),
584 textToStr( module(name(n).mod).text ),
585 textToStr( name(n).text ) );
586 t = enZcodeThenFindText(buf);
587 p = lookupOTabName ( name(n).mod, textToStr(t) );
591 ERRMSG(0) "Can't find info table %s", textToStr(t)
596 void ifLinkConstrItbl ( Name n )
598 /* name(n) is either a constructor or a field name.
599 If the latter, ignore it. If it is a non-nullary constructor,
600 find its info table in the object code. If it's nullary,
601 we can skip the info table, since all accesses will go via
604 if (islower(textToStr(name(n).text)[0])) return;
605 if (name(n).arity == 0) return;
606 name(n).itbl = ifFindItblFor(n);
610 static void ifSetClassDefaultsAndDCon ( Class c )
618 List defs; /* :: [Name] */
619 List mems; /* :: [Name] */
621 assert(isNull(cclass(c).defaults));
623 /* Create the defaults list by more-or-less cloning the members list. */
625 for (mems=cclass(c).members; nonNull(mems); mems=tl(mems)) {
627 s = textToStr( name(hd(mems)).text );
628 assert(strlen(s) < 95);
630 n = findNameInAnyModule(findText(buf));
635 cclass(c).defaults = defs;
637 /* Create a name table entry for the dictionary datacon.
638 Interface files don't mention them, so it had better not
642 s = textToStr( cclass(c).text );
643 assert( strlen(s) < 96 );
646 n = findNameInAnyModule(t);
652 name(n).arity = cclass(c).numSupers + cclass(c).numMembers;
653 name(n).number = cfunNo(0);
656 /* And finally ... set name(n).itbl to Mod_:DClass_con_info.
657 Because this happens right at the end of loading, we know
658 that we should actually be able to find the symbol in this
659 module's object symbol table. Except that if the dictionary
660 has arity 1, we don't bother, since it will be represented as
661 a newtype and not as a data, so its itbl can remain NULL.
663 if (name(n).arity == 1) {
665 name(n).defn = nameId;
667 p = ifFindItblFor ( n );
673 /* ifaces_outstanding holds a list of parsed interfaces
674 for which we need to load objects and create symbol
677 Return TRUE if Prelude `elem` ifaces_outstanding, else FALSE.
679 Bool processInterfaces ( void )
690 List all_known_types;
693 List cls_list; /* :: List Class */
694 List constructor_list; /* :: List Name */
696 List ifaces = NIL; /* :: List I_INTERFACE */
697 List iface_sizes = NIL; /* :: List Int */
698 List iface_onames = NIL; /* :: List Text */
700 if (isNull(ifaces_outstanding)) return FALSE;
704 "processInterfaces: %d interfaces to process\n",
705 length(ifaces_outstanding) );
708 /* unzip3 ifaces_outstanding into ifaces, iface_sizes, iface_onames */
709 for (xs = ifaces_outstanding; nonNull(xs); xs=tl(xs)) {
710 ifaces = cons ( zfst3(hd(xs)), ifaces );
711 iface_onames = cons ( zsnd3(hd(xs)), iface_onames );
712 iface_sizes = cons ( zthd3(hd(xs)), iface_sizes );
715 ifaces = reverse(ifaces);
716 iface_onames = reverse(iface_onames);
717 iface_sizes = reverse(iface_sizes);
719 /* Clean up interfaces -- dump non-exported value, class, type decls */
720 for (xs = ifaces; nonNull(xs); xs = tl(xs))
721 hd(xs) = deleteUnexportedIFaceEntities(hd(xs));
724 /* Iteratively delete any type declarations which refer to unknown
727 num_known_types = 999999999;
731 /* Construct a list of all known tycons. This is a list of QualIds.
732 Unfortunately it also has to contain all known class names, since
733 allTypesKnown cannot distinguish between tycons and classes -- a
734 deficiency of the iface abs syntax.
736 all_known_types = getAllKnownTyconsAndClasses();
737 for (xs = ifaces; nonNull(xs); xs=tl(xs))
738 all_known_types = addTyconsAndClassesFromIFace ( hd(xs), all_known_types );
740 /* Have we reached a fixed point? */
741 i = length(all_known_types);
744 "\n============= %d known types =============\n", i );
746 if (num_known_types == i) break;
749 /* Delete all entities which refer to unknown tycons. */
750 for (xs = ifaces; nonNull(xs); xs = tl(xs)) {
751 ConId mod = zfst(unap(I_INTERFACE,hd(xs)));
752 assert(nonNull(mod));
753 hd(xs) = filterInterface ( hd(xs),
754 ifTypeDoesntRefUnknownTycon,
755 zpair(all_known_types,mod),
756 ifTypeDoesntRefUnknownTycon_dumpmsg );
760 /* Now abstractify any datas and newtypes which refer to unknown tycons
761 -- including, of course, the type decls just deleted.
763 for (xs = ifaces; nonNull(xs); xs = tl(xs)) {
764 List absify = NIL; /* :: [ConId] */
765 ZPair iface = unap(I_INTERFACE,hd(xs)); /* ((ConId, [I_IMPORT..I_VALUE])) */
766 ConId mod = zfst(iface);
767 List aktys = all_known_types; /* just a renaming */
771 /* Compute into absify the list of all ConIds (tycons) we need to
774 for (es = zsnd(iface); nonNull(es); es=tl(es)) {
776 Bool allKnown = TRUE;
778 if (whatIs(ent)==I_DATA) {
779 Cell data = unap(I_DATA,ent);
780 List ctx = zsel25 ( data ); /* :: [((QConId,VarId))] */
781 List constrs = zsel55 ( data ); /* :: [ ((ConId, [((Type,VarId,Int))] )) ] */
782 for (t = ctx; nonNull(t); t=tl(t))
783 if (!allTypesKnown(zfst(hd(t)),aktys,mod)) allKnown = FALSE;
784 for (t = constrs; nonNull(t); t=tl(t))
785 for (u = zsnd(hd(t)); nonNull(u); u=tl(u))
786 if (!allTypesKnown(zfst3(hd(u)),aktys,mod)) allKnown = FALSE;
788 else if (whatIs(ent)==I_NEWTYPE) {
789 Cell newty = unap(I_NEWTYPE,ent);
790 List ctx = zsel25(newty); /* :: [((QConId,VarId))] */
791 ZPair constr = zsel55 ( newty ); /* :: ((ConId,Type)) */
792 for (t = ctx; nonNull(t); t=tl(t))
793 if (!allTypesKnown(zfst(hd(t)),aktys,mod)) allKnown = FALSE;
794 if (!allTypesKnown(zsnd(constr),aktys,mod)) allKnown = FALSE;
798 absify = cons ( getIEntityName(ent), absify );
801 "abstractifying %s because it uses an unknown type\n",
802 textToStr(textOf(getIEntityName(ent))) );
807 /* mark in exports as abstract all names in absify (modifies iface) */
808 for (; nonNull(absify); absify=tl(absify)) {
809 ConId toAbs = hd(absify);
810 for (es = zsnd(iface); nonNull(es); es=tl(es)) {
811 if (whatIs(hd(es)) != I_EXPORT) continue;
812 hd(es) = abstractifyExDecl ( hd(es), toAbs );
816 /* For each data/newtype in the export list marked as abstract,
817 remove the constructor lists. This catches all abstractification
818 caused by the code above, and it also catches tycons which really
819 were exported abstractly.
822 exlist_list = getExportDeclsInIFace ( ap(I_INTERFACE,iface) );
823 /* exlist_list :: [I_EXPORT] */
824 for (t=exlist_list; nonNull(t); t=tl(t))
825 hd(t) = zsnd(unap(I_EXPORT,hd(t)));
826 /* exlist_list :: [[ ConVarId | ((ConId, [ConVarId])) ]] */
828 for (es = zsnd(iface); nonNull(es); es=tl(es)) {
830 if (whatIs(ent)==I_DATA
831 && isExportedAbstractly ( getIEntityName(ent),
833 Cell data = unap(I_DATA,ent);
834 data = z5ble ( zsel15(data), zsel25(data), zsel35(data),
835 zsel45(data), NIL /* the constr list */ );
836 hd(es) = ap(I_DATA,data);
838 fprintf(stderr, "abstractify data %s\n",
839 textToStr(textOf(getIEntityName(ent))) );
842 else if (whatIs(ent)==I_NEWTYPE
843 && isExportedAbstractly ( getIEntityName(ent),
845 Cell data = unap(I_NEWTYPE,ent);
846 data = z5ble ( zsel15(data), zsel25(data), zsel35(data),
847 zsel45(data), NIL /* the constr-type pair */ );
848 hd(es) = ap(I_NEWTYPE,data);
850 fprintf(stderr, "abstractify newtype %s\n",
851 textToStr(textOf(getIEntityName(ent))) );
856 /* We've finally finished mashing this iface. Update the iface list. */
857 hd(xs) = ap(I_INTERFACE,iface);
861 /* At this point, the interfaces are cleaned up so that no type, data or
862 newtype defn refers to a non-existant type. However, there still may
863 be value defns, classes and instances which refer to unknown types.
864 Delete iteratively until a fixed point is reached.
867 fprintf(stderr,"\n");
869 num_known_types = 999999999;
873 /* Construct a list of all known tycons. This is a list of QualIds.
874 Unfortunately it also has to contain all known class names, since
875 allTypesKnown cannot distinguish between tycons and classes -- a
876 deficiency of the iface abs syntax.
878 all_known_types = getAllKnownTyconsAndClasses();
879 for (xs = ifaces; nonNull(xs); xs=tl(xs))
880 all_known_types = addTyconsAndClassesFromIFace ( hd(xs), all_known_types );
882 /* Have we reached a fixed point? */
883 i = length(all_known_types);
886 "\n------------- %d known types -------------\n", i );
888 if (num_known_types == i) break;
891 /* Delete all entities which refer to unknown tycons. */
892 for (xs = ifaces; nonNull(xs); xs = tl(xs)) {
893 ConId mod = zfst(unap(I_INTERFACE,hd(xs)));
894 assert(nonNull(mod));
896 hd(xs) = filterInterface ( hd(xs),
897 ifentityAllTypesKnown,
898 zpair(all_known_types,mod),
899 ifentityAllTypesKnown_dumpmsg );
904 /* Allocate module table entries and read in object code. */
907 xs=tl(xs), iface_sizes=tl(iface_sizes), iface_onames=tl(iface_onames)) {
908 startGHCModule ( textOf(zfst(unap(I_INTERFACE,hd(xs)))),
909 intOf(hd(iface_sizes)),
912 assert (isNull(iface_sizes));
913 assert (isNull(iface_onames));
916 /* Now work through the decl lists of the modules, and call the
917 startGHC* functions on the entities. This creates names in
918 various tables but doesn't bind them to anything.
921 for (xs = ifaces; nonNull(xs); xs = tl(xs)) {
922 iface = unap(I_INTERFACE,hd(xs));
923 mname = textOf(zfst(iface));
924 mod = findModule(mname);
925 if (isNull(mod)) internal("processInterfaces(4)");
927 ppModule ( module(mod).text );
929 for (decls = zsnd(iface); nonNull(decls); decls = tl(decls)) {
930 Cell decl = hd(decls);
931 switch(whatIs(decl)) {
933 Cell exdecl = unap(I_EXPORT,decl);
934 startGHCExports ( zfst(exdecl), zsnd(exdecl) );
938 Cell imdecl = unap(I_IMPORT,decl);
939 startGHCImports ( zfst(imdecl), zsnd(imdecl) );
946 /* Trying to find the instance table location allocated by
947 startGHCInstance in subsequent processing is a nightmare, so
948 cache it on the tree.
950 Cell instance = unap(I_INSTANCE,decl);
951 Inst in = startGHCInstance ( zsel15(instance), zsel25(instance),
952 zsel35(instance), zsel45(instance) );
953 hd(decls) = ap(I_INSTANCE,
954 z5ble( zsel15(instance), zsel25(instance),
955 zsel35(instance), zsel45(instance), in ));
959 Cell tydecl = unap(I_TYPE,decl);
960 startGHCSynonym ( zsel14(tydecl), zsel24(tydecl),
961 zsel34(tydecl), zsel44(tydecl) );
965 Cell ddecl = unap(I_DATA,decl);
966 startGHCDataDecl ( zsel15(ddecl), zsel25(ddecl),
967 zsel35(ddecl), zsel45(ddecl), zsel55(ddecl) );
971 Cell ntdecl = unap(I_NEWTYPE,decl);
972 startGHCNewType ( zsel15(ntdecl), zsel25(ntdecl),
973 zsel35(ntdecl), zsel45(ntdecl),
978 Cell klass = unap(I_CLASS,decl);
979 startGHCClass ( zsel15(klass), zsel25(klass),
980 zsel35(klass), zsel45(klass),
985 Cell value = unap(I_VALUE,decl);
986 startGHCValue ( zfst3(value), zsnd3(value), zthd3(value) );
990 internal("processInterfaces(1)");
996 fprintf(stderr, "\n============================"
997 "=============================\n");
998 fprintf(stderr, "=============================="
999 "===========================\n");
1002 /* Traverse again the decl lists of the modules, this time
1003 calling the finishGHC* functions. But don't process
1004 the export lists; those must wait for later.
1008 constructor_list = NIL;
1009 for (xs = ifaces; nonNull(xs); xs = tl(xs)) {
1010 iface = unap(I_INTERFACE,hd(xs));
1011 mname = textOf(zfst(iface));
1012 mod = findModule(mname);
1013 if (isNull(mod)) internal("processInterfaces(3)");
1015 ppModule ( module(mod).text );
1017 if (mname == textPrelude) didPrelude = TRUE;
1019 for (decls = zsnd(iface); nonNull(decls); decls = tl(decls)) {
1020 Cell decl = hd(decls);
1021 switch(whatIs(decl)) {
1029 Cell fixdecl = unap(I_FIXDECL,decl);
1030 finishGHCFixdecl ( zfst3(fixdecl), zsnd3(fixdecl), zthd3(fixdecl) );
1034 Cell instance = unap(I_INSTANCE,decl);
1035 finishGHCInstance ( zsel55(instance) );
1039 Cell tydecl = unap(I_TYPE,decl);
1040 finishGHCSynonym ( zsel24(tydecl) );
1044 Cell ddecl = unap(I_DATA,decl);
1045 List constrs = finishGHCDataDecl ( zsel35(ddecl) );
1046 constructor_list = appendOnto ( constrs, constructor_list );
1050 Cell ntdecl = unap(I_NEWTYPE,decl);
1051 finishGHCNewType ( zsel35(ntdecl) );
1055 Cell klass = unap(I_CLASS,decl);
1056 Class cls = finishGHCClass ( zsel35(klass) );
1057 cls_list = cons(cls,cls_list);
1061 Cell value = unap(I_VALUE,decl);
1062 finishGHCValue ( zsnd3(value) );
1066 internal("processInterfaces(2)");
1071 fprintf(stderr, "\n+++++++++++++++++++++++++++++"
1072 "++++++++++++++++++++++++++++\n");
1073 fprintf(stderr, "+++++++++++++++++++++++++++++++"
1074 "++++++++++++++++++++++++++\n");
1077 /* Build the module(m).export lists for each module, by running
1078 through the export lists in the iface. Also, do the implicit
1079 'import Prelude' thing. And finally, do the object code
1082 for (xs = ifaces; nonNull(xs); xs = tl(xs))
1083 finishGHCModule(hd(xs));
1085 mapProc(visitClass,cls_list);
1086 mapProc(ifSetClassDefaultsAndDCon,cls_list);
1087 mapProc(ifLinkConstrItbl,constructor_list);
1090 ifaces_outstanding = NIL;
1096 /* --------------------------------------------------------------------------
1098 * ------------------------------------------------------------------------*/
1100 static void startGHCModule_errMsg ( char* msg )
1102 fprintf ( stderr, "object error: %s\n", msg );
1105 static void* startGHCModule_clientLookup ( char* sym )
1108 /* fprintf ( stderr, "CLIENTLOOKUP %s\n", sym ); */
1110 return lookupObjName ( sym );
1113 static ObjectCode* startGHCModule_partial_load ( String objNm, Int objSz )
1116 = ocNew ( startGHCModule_errMsg,
1117 startGHCModule_clientLookup,
1121 ERRMSG(0) "Storage allocation for object file \"%s\" failed", objNm
1124 if (!ocLoadImage(oc,VERBOSE)) {
1125 ERRMSG(0) "Reading of object file \"%s\" failed", objNm
1128 if (!ocVerifyImage(oc,VERBOSE)) {
1129 ERRMSG(0) "Validation of object file \"%s\" failed", objNm
1132 if (!ocGetNames(oc,VERBOSE)) {
1133 ERRMSG(0) "Reading of symbol names in object file \"%s\" failed", objNm
1139 static Void startGHCModule ( Text mname, Int sizeObj, Text nameObj )
1142 Module m = findModule(mname);
1145 m = newModule(mname);
1147 fprintf ( stderr, "startGHCIface: name %16s objsize %d\n",
1148 textToStr(mname), sizeObj );
1151 if (module(m).fake) {
1152 module(m).fake = FALSE;
1154 ERRMSG(0) "Module \"%s\" already loaded", textToStr(mname)
1159 /* Get hold of the primary object for the module. */
1161 = startGHCModule_partial_load ( textToStr(nameObj), sizeObj );
1163 /* and any extras ... */
1164 for (xts = module(m).objectExtraNames; nonNull(xts); xts=tl(xts)) {
1168 String nm = getExtraObjectInfo ( textToStr(nameObj),
1172 ERRMSG(0) "Can't find extra object file \"%s\"", nm
1175 oc = startGHCModule_partial_load ( nm, size );
1176 oc->next = module(m).objectExtras;
1177 module(m).objectExtras = oc;
1182 /* For the module mod, augment both the export environment (.exports)
1183 and the eval environment (.names, .tycons, .classes)
1184 with the symbols mentioned in exlist. We don't actually need
1185 to modify the names, tycons, classes or instances in the eval
1186 environment, since previous processing of the
1187 top-level decls in the iface should have done this already.
1189 mn is the module mentioned in the export list; it is the "original"
1190 module for the symbols in the export list. We should also record
1191 this info with the symbols, since references to object code need to
1192 refer to the original module in which a symbol was defined, rather
1193 than to some module it has been imported into and then re-exported.
1195 We take the policy that if something mentioned in an export list
1196 can't be found in the symbol tables, it is simply ignored. After all,
1197 previous processing of the iface syntax trees has already removed
1198 everything which Hugs can't handle, so if there is mention of these
1199 things still lurking in export lists somewhere, about the only thing
1200 to do is to ignore it.
1202 Also do an implicit 'import Prelude' thingy for the module,
1207 static Void finishGHCModule ( Cell root )
1209 /* root :: I_INTERFACE */
1210 Cell iface = unap(I_INTERFACE,root);
1211 ConId iname = zfst(iface);
1212 Module mod = findModule(textOf(iname));
1213 List exlist_list = NIL;
1218 fprintf(stderr, "begin finishGHCModule %s\n", textToStr(textOf(iname)));
1221 if (isNull(mod)) internal("finishExports(1)");
1224 exlist_list = getExportDeclsInIFace ( root );
1225 /* exlist_list :: [I_EXPORT] */
1227 for (; nonNull(exlist_list); exlist_list=tl(exlist_list)) {
1228 ZPair exdecl = unap(I_EXPORT,hd(exlist_list));
1229 ConId exmod = zfst(exdecl);
1230 List exlist = zsnd(exdecl);
1231 /* exlist :: [ ConVarId | ((ConId, [ConVarId])) ] */
1233 for (; nonNull(exlist); exlist=tl(exlist)) {
1238 Cell ex = hd(exlist);
1240 switch (whatIs(ex)) {
1242 case VARIDCELL: /* variable */
1243 q = mkQualId(exmod,ex);
1244 c = findQualNameWithoutConsultingExportList ( q );
1245 if (isNull(c)) goto notfound;
1247 fprintf(stderr, " var %s\n", textToStr(textOf(ex)) );
1249 module(mod).exports = cons(c, module(mod).exports);
1253 case CONIDCELL: /* non data tycon */
1254 q = mkQualId(exmod,ex);
1255 c = findQualTyconWithoutConsultingExportList ( q );
1256 if (isNull(c)) goto notfound;
1258 fprintf(stderr, " type %s\n", textToStr(textOf(ex)) );
1260 module(mod).exports = cons(pair(c,NIL), module(mod).exports);
1264 case ZTUP2: /* data T = C1 ... Cn or class C where f1 ... fn */
1265 subents = zsnd(ex); /* :: [ConVarId] */
1266 ex = zfst(ex); /* :: ConId */
1267 q = mkQualId(exmod,ex);
1268 c = findQualTyconWithoutConsultingExportList ( q );
1270 if (nonNull(c)) { /* data */
1272 fprintf(stderr, " data/newtype %s = { ",
1273 textToStr(textOf(ex)) );
1275 assert(tycon(c).what == DATATYPE || tycon(c).what==NEWTYPE);
1276 abstract = isNull(tycon(c).defn);
1277 /* This data/newtype could be abstract even tho the export list
1278 says to export it non-abstractly. That happens if it was
1279 imported from some other module and is now being re-exported,
1280 and previous cleanup phases have abstractified it in the
1281 original (defining) module.
1284 module(mod).exports = cons(pair(c,NIL), module(mod).exports);
1287 fprintf ( stderr, "(abstract) ");
1290 module(mod).exports = cons(pair(c,DOTDOT), module(mod).exports);
1292 for (; nonNull(subents); subents = tl(subents)) {
1293 Cell ent2 = hd(subents);
1294 assert(isCon(ent2) || isVar(ent2));
1295 /* isVar since could be a field name */
1296 q = mkQualId(exmod,ent2);
1297 c = findQualNameWithoutConsultingExportList ( q );
1299 fprintf(stderr, "%s ", textToStr(name(c).text));
1302 /* module(mod).exports = cons(c, module(mod).exports); */
1307 fprintf(stderr, "}\n" );
1309 } else { /* class */
1310 q = mkQualId(exmod,ex);
1311 c = findQualClassWithoutConsultingExportList ( q );
1312 if (isNull(c)) goto notfound;
1314 fprintf(stderr, " class %s { ", textToStr(textOf(ex)) );
1316 module(mod).exports = cons(pair(c,DOTDOT), module(mod).exports);
1318 for (; nonNull(subents); subents = tl(subents)) {
1319 Cell ent2 = hd(subents);
1320 assert(isVar(ent2));
1321 q = mkQualId(exmod,ent2);
1322 c = findQualNameWithoutConsultingExportList ( q );
1324 fprintf(stderr, "%s ", textToStr(name(c).text));
1326 if (isNull(c)) goto notfound;
1327 /* module(mod).exports = cons(c, module(mod).exports); */
1331 fprintf(stderr, "}\n" );
1337 internal("finishExports(2)");
1340 continue; /* so notfound: can be placed after this */
1343 /* q holds what ain't found */
1344 assert(whatIs(q)==QUALIDENT);
1346 fprintf( stderr, " ------ IGNORED: %s.%s\n",
1347 textToStr(qmodOf(q)), textToStr(qtextOf(q)) );
1354 if (preludeLoaded) {
1355 /* do the implicit 'import Prelude' thing */
1356 List pxs = module(modulePrelude).exports;
1357 for (; nonNull(pxs); pxs=tl(pxs)) {
1360 switch (whatIs(px)) {
1365 module(mod).names = cons ( px, module(mod).names );
1368 module(mod).tycons = cons ( px, module(mod).tycons );
1371 module(mod).classes = cons ( px, module(mod).classes );
1374 fprintf(stderr, "finishGHCModule: unknown tag %d\n", whatIs(px));
1375 internal("finishGHCModule -- implicit import Prelude");
1382 /* Last, but by no means least ... */
1383 if (!ocResolve(module(mod).object,VERBOSE))
1384 internal("finishGHCModule: object resolution failed");
1386 for (oc=module(mod).objectExtras; oc; oc=oc->next) {
1387 if (!ocResolve(oc, VERBOSE))
1388 internal("finishGHCModule: extra object resolution failed");
1393 /* --------------------------------------------------------------------------
1395 * ------------------------------------------------------------------------*/
1397 static Void startGHCExports ( ConId mn, List exlist )
1400 fprintf(stderr,"startGHCExports %s\n", textToStr(textOf(mn)) );
1402 /* Nothing to do. */
1405 static Void finishGHCExports ( ConId mn, List exlist )
1408 fprintf(stderr,"finishGHCExports %s\n", textToStr(textOf(mn)) );
1410 /* Nothing to do. */
1414 /* --------------------------------------------------------------------------
1416 * ------------------------------------------------------------------------*/
1418 static Void startGHCImports ( ConId mn, List syms )
1419 /* nm the module to import from */
1420 /* syms [ConId | VarId] -- the names to import */
1423 fprintf(stderr,"startGHCImports %s\n", textToStr(textOf(mn)) );
1425 /* Nothing to do. */
1429 static Void finishGHCImports ( ConId nm, List syms )
1430 /* nm the module to import from */
1431 /* syms [ConId | VarId] -- the names to import */
1434 fprintf(stderr,"finishGHCImports %s\n", textToStr(textOf(nm)) );
1436 /* Nothing to do. */
1440 /* --------------------------------------------------------------------------
1442 * ------------------------------------------------------------------------*/
1444 static Void finishGHCFixdecl ( Cell prec, Cell assoc, ConVarId name )
1446 Int p = intOf(prec);
1447 Int a = intOf(assoc);
1448 Name n = findName(textOf(name));
1449 assert (nonNull(n));
1450 name(n).syntax = mkSyntax ( a, p );
1454 /* --------------------------------------------------------------------------
1456 * ------------------------------------------------------------------------*/
1458 /* convert a leading run of DICTAPs into Hugs' internal Qualtype form, viz:
1459 { C1 a } -> { C2 b } -> T into
1460 ap(QUALTYPE, ( [(C1,a),(C2,b)], T ))
1462 static Type dictapsToQualtype ( Type ty )
1465 List preds, dictaps;
1467 /* break ty into pieces at the top-level arrows */
1468 while (isAp(ty) && isAp(fun(ty)) && fun(fun(ty))==typeArrow) {
1469 pieces = cons ( arg(fun(ty)), pieces );
1472 pieces = cons ( ty, pieces );
1473 pieces = reverse ( pieces );
1476 while (nonNull(pieces) && whatIs(hd(pieces))==DICTAP) {
1477 dictaps = cons ( hd(pieces), dictaps );
1478 pieces = tl(pieces);
1481 /* dictaps holds the predicates, backwards */
1482 /* pieces holds the remainder of the type, forwards */
1483 assert(nonNull(pieces));
1484 pieces = reverse(pieces);
1486 pieces = tl(pieces);
1487 for (; nonNull(pieces); pieces=tl(pieces))
1488 ty = fn(hd(pieces),ty);
1491 for (; nonNull(dictaps); dictaps=tl(dictaps)) {
1492 Cell da = hd(dictaps);
1493 QualId cl = fst(unap(DICTAP,da));
1494 Cell arg = snd(unap(DICTAP,da));
1495 preds = cons ( pair(cl,arg), preds );
1498 if (nonNull(preds)) ty = ap(QUAL, pair(preds,ty));
1504 static void startGHCValue ( Int line, VarId vid, Type ty )
1508 Text v = textOf(vid);
1511 fprintf(stderr,"begin startGHCValue %s\n", textToStr(v));
1516 if (nonNull(n) && name(n).defn != PREDEFINED) {
1517 ERRMSG(line) "Attempt to redefine variable \"%s\"", textToStr(v)
1520 if (isNull(n)) n = newName(v,NIL);
1522 ty = dictapsToQualtype(ty);
1524 tvs = ifTyvarsIn(ty);
1525 for (tmp=tvs; nonNull(tmp); tmp=tl(tmp))
1526 hd(tmp) = zpair(hd(tmp),STAR);
1528 ty = mkPolyType(tvsToKind(tvs),ty);
1530 ty = tvsToOffsets(line,ty,tvs);
1532 name(n).arity = arityInclDictParams(ty);
1533 name(n).line = line;
1538 static void finishGHCValue ( VarId vid )
1540 Name n = findName ( textOf(vid) );
1541 Int line = name(n).line;
1543 fprintf(stderr, "begin finishGHCValue %s\n", textToStr(name(n).text) );
1545 assert(currentModule == name(n).mod);
1546 name(n).type = conidcellsToTycons(line,name(n).type);
1548 if (isIfaceDefaultMethodName(name(n).text)) {
1549 /* ... we need to set .parent to point to the class
1550 ... once we figure out what the class actually is :-)
1552 Type t = name(n).type;
1553 assert(isPolyType(t));
1554 if (isPolyType(t)) t = monotypeOf(t);
1555 assert(isQualType(t));
1556 t = fst(snd(t)); /* t :: [(Class,Offset)] */
1558 assert(nonNull(hd(t)));
1559 assert(isPair(hd(t)));
1560 t = fst(hd(t)); /* t :: Class */
1563 name(n).parent = t; /* phew! */
1568 /* --------------------------------------------------------------------------
1570 * ------------------------------------------------------------------------*/
1572 static Void startGHCSynonym ( Int line, ConId tycon, List tvs, Type ty )
1574 /* tycon :: ConId */
1575 /* tvs :: [((VarId,Kind))] */
1577 Text t = textOf(tycon);
1579 fprintf(stderr, "begin startGHCSynonym %s\n", textToStr(t) );
1582 if (nonNull(findTycon(t))) {
1583 ERRMSG(line) "Repeated definition of type constructor \"%s\"",
1587 Tycon tc = newTycon(t);
1588 tycon(tc).line = line;
1589 tycon(tc).arity = length(tvs);
1590 tycon(tc).what = SYNONYM;
1591 tycon(tc).kind = tvsToKind(tvs);
1593 /* prepare for finishGHCSynonym */
1594 tycon(tc).defn = tvsToOffsets(line,ty,tvs);
1599 static Void finishGHCSynonym ( ConId tyc )
1601 Tycon tc = findTycon(textOf(tyc));
1602 Int line = tycon(tc).line;
1604 fprintf(stderr, "begin finishGHCSynonym %s\n", textToStr(textOf(tyc)) );
1607 assert (currentModule == tycon(tc).mod);
1608 // setCurrModule(tycon(tc).mod);
1609 tycon(tc).defn = conidcellsToTycons(line,tycon(tc).defn);
1611 /* (ADR) ToDo: can't really do this until I've done all synonyms
1612 * and then I have to do them in order
1613 * tycon(tc).defn = fullExpand(ty);
1614 * (JRS) What?!?! i don't understand
1619 /* --------------------------------------------------------------------------
1621 * ------------------------------------------------------------------------*/
1623 static Void startGHCDataDecl(line,ctx0,tycon,ktyvars,constrs0)
1625 List ctx0; /* [((QConId,VarId))] */
1626 Cell tycon; /* ConId */
1627 List ktyvars; /* [((VarId,Kind))] */
1628 List constrs0; /* [((ConId,[((Type,VarId,Int))]))] */
1629 /* The Text is an optional field name
1630 The Int indicates strictness */
1631 /* ToDo: worry about being given a decl for (->) ?
1632 * and worry about qualidents for ()
1635 Type ty, resTy, selTy, conArgTy;
1636 List tmp, conArgs, sels, constrs, fields, tyvarsMentioned;
1640 Pair conArg, ctxElem;
1642 Int conArgStrictness;
1644 Text t = textOf(tycon);
1646 fprintf(stderr, "begin startGHCDataDecl %s\n",textToStr(t));
1650 if (nonNull(findTycon(t))) {
1651 ERRMSG(line) "Repeated definition of type constructor \"%s\"",
1655 Tycon tc = newTycon(t);
1657 tycon(tc).line = line;
1658 tycon(tc).arity = length(ktyvars);
1659 tycon(tc).kind = tvsToKind(ktyvars);
1660 tycon(tc).what = DATATYPE;
1662 /* a list to accumulate selectors in :: [((VarId,Type))] */
1665 /* make resTy the result type of the constr, T v1 ... vn */
1667 for (tmp=ktyvars; nonNull(tmp); tmp=tl(tmp))
1668 resTy = ap(resTy,zfst(hd(tmp)));
1670 /* for each constructor ... */
1671 for (constrs=constrs0; nonNull(constrs); constrs=tl(constrs)) {
1672 constr = hd(constrs);
1673 conid = zfst(constr);
1674 fields = zsnd(constr);
1676 /* Build type of constr and handle any selectors found.
1677 Also collect up tyvars occurring in the constr's arg
1678 types, so we can throw away irrelevant parts of the
1682 tyvarsMentioned = NIL;
1683 /* tyvarsMentioned :: [VarId] */
1685 conArgs = reverse(fields);
1686 for (; nonNull(conArgs); conArgs=tl(conArgs)) {
1687 conArg = hd(conArgs); /* (Type,Text) */
1688 conArgTy = zfst3(conArg);
1689 conArgNm = zsnd3(conArg);
1690 conArgStrictness = intOf(zthd3(conArg));
1691 tyvarsMentioned = dupListOnto(ifTyvarsIn(conArgTy),
1693 if (conArgStrictness > 0) conArgTy = bang(conArgTy);
1694 ty = fn(conArgTy,ty);
1695 if (nonNull(conArgNm)) {
1696 /* a field name is mentioned too */
1697 selTy = fn(resTy,conArgTy);
1698 if (whatIs(tycon(tc).kind) != STAR)
1699 selTy = pair(POLYTYPE,pair(tycon(tc).kind, selTy));
1700 selTy = tvsToOffsets(line,selTy, ktyvars);
1701 sels = cons( zpair(conArgNm,selTy), sels);
1705 /* Now ty is the constructor's type, not including context.
1706 Throw away any parts of the context not mentioned in
1707 tyvarsMentioned, and use it to qualify ty.
1710 for (ctx=ctx0; nonNull(ctx); ctx=tl(ctx)) {
1712 /* ctxElem :: ((QConId,VarId)) */
1713 if (nonNull(cellIsMember(textOf(zsnd(ctxElem)),tyvarsMentioned)))
1714 ctx2 = cons(ctxElem, ctx2);
1717 ty = ap(QUAL,pair(ctx2,ty));
1719 /* stick the tycon's kind on, if not simply STAR */
1720 if (whatIs(tycon(tc).kind) != STAR)
1721 ty = pair(POLYTYPE,pair(tycon(tc).kind, ty));
1723 ty = tvsToOffsets(line,ty, ktyvars);
1725 /* Finally, stick the constructor's type onto it. */
1726 hd(constrs) = ztriple(conid,fields,ty);
1729 /* Final result is that
1730 constrs :: [((ConId,[((Type,Text))],Type))]
1731 lists the constructors and their types
1732 sels :: [((VarId,Type))]
1733 lists the selectors and their types
1735 tycon(tc).defn = startGHCConstrs(line,constrs0,sels);
1740 static List startGHCConstrs ( Int line, List cons, List sels )
1742 /* cons :: [((ConId,[((Type,Text,Int))],Type))] */
1743 /* sels :: [((VarId,Type))] */
1744 /* returns [Name] */
1746 Int conNo = length(cons)>1 ? 1 : 0;
1747 for(cs=cons; nonNull(cs); cs=tl(cs), conNo++) {
1748 Name c = startGHCConstr(line,conNo,hd(cs));
1751 /* cons :: [Name] */
1753 for(ss=sels; nonNull(ss); ss=tl(ss)) {
1754 hd(ss) = startGHCSel(line,hd(ss));
1756 /* sels :: [Name] */
1757 return appendOnto(cons,sels);
1761 static Name startGHCSel ( Int line, ZPair sel )
1763 /* sel :: ((VarId, Type)) */
1764 Text t = textOf(zfst(sel));
1765 Type type = zsnd(sel);
1767 Name n = findName(t);
1769 ERRMSG(line) "Repeated definition for selector \"%s\"",
1775 name(n).line = line;
1776 name(n).number = SELNAME;
1779 name(n).type = type;
1784 static Name startGHCConstr ( Int line, Int conNo, ZTriple constr )
1786 /* constr :: ((ConId,[((Type,Text,Int))],Type)) */
1787 /* (ADR) ToDo: add rank2 annotation and existential annotation
1788 * these affect how constr can be used.
1790 Text con = textOf(zfst3(constr));
1791 Type type = zthd3(constr);
1792 Int arity = arityFromType(type);
1793 Name n = findName(con); /* Allocate constructor fun name */
1795 n = newName(con,NIL);
1796 } else if (name(n).defn!=PREDEFINED) {
1797 ERRMSG(line) "Repeated definition for constructor \"%s\"",
1801 name(n).arity = arity; /* Save constructor fun details */
1802 name(n).line = line;
1803 name(n).number = cfunNo(conNo);
1804 name(n).type = type;
1809 static List finishGHCDataDecl ( ConId tyc )
1812 Tycon tc = findTycon(textOf(tyc));
1814 fprintf ( stderr, "begin finishGHCDataDecl %s\n",
1815 textToStr(textOf(tyc)) );
1817 if (isNull(tc)) internal("finishGHCDataDecl");
1819 for (nms=tycon(tc).defn; nonNull(nms); nms=tl(nms)) {
1821 Int line = name(n).line;
1822 assert(currentModule == name(n).mod);
1823 name(n).type = conidcellsToTycons(line,name(n).type);
1824 name(n).parent = tc; //---????
1827 return tycon(tc).defn;
1831 /* --------------------------------------------------------------------------
1833 * ------------------------------------------------------------------------*/
1835 static Void startGHCNewType ( Int line, List ctx0,
1836 ConId tycon, List tvs, Cell constr )
1838 /* ctx0 :: [((QConId,VarId))] */
1839 /* tycon :: ConId */
1840 /* tvs :: [((VarId,Kind))] */
1841 /* constr :: ((ConId,Type)) or NIL if abstract */
1844 Text t = textOf(tycon);
1846 fprintf(stderr, "begin startGHCNewType %s\n", textToStr(t) );
1851 if (nonNull(findTycon(t))) {
1852 ERRMSG(line) "Repeated definition of type constructor \"%s\"",
1856 Tycon tc = newTycon(t);
1857 tycon(tc).line = line;
1858 tycon(tc).arity = length(tvs);
1859 tycon(tc).what = NEWTYPE;
1860 tycon(tc).kind = tvsToKind(tvs);
1861 /* can't really do this until I've read in all synonyms */
1863 if (isNull(constr)) {
1864 tycon(tc).defn = NIL;
1866 /* constr :: ((ConId,Type)) */
1867 Text con = textOf(zfst(constr));
1868 Type type = zsnd(constr);
1869 Name n = findName(con); /* Allocate constructor fun name */
1871 n = newName(con,NIL);
1872 } else if (name(n).defn!=PREDEFINED) {
1873 ERRMSG(line) "Repeated definition for constructor \"%s\"",
1877 name(n).arity = 1; /* Save constructor fun details */
1878 name(n).line = line;
1879 name(n).number = cfunNo(0);
1880 name(n).defn = nameId;
1881 tycon(tc).defn = singleton(n);
1883 /* make resTy the result type of the constr, T v1 ... vn */
1885 for (tmp=tvs; nonNull(tmp); tmp=tl(tmp))
1886 resTy = ap(resTy,zfst(hd(tmp)));
1887 type = fn(type,resTy);
1889 type = ap(QUAL,pair(ctx0,type));
1890 type = tvsToOffsets(line,type,tvs);
1891 name(n).type = type;
1897 static Void finishGHCNewType ( ConId tyc )
1899 Tycon tc = findTycon(textOf(tyc));
1901 fprintf ( stderr, "begin finishGHCNewType %s\n",
1902 textToStr(textOf(tyc)) );
1905 if (isNull(tc)) internal("finishGHCNewType");
1907 if (isNull(tycon(tc).defn)) {
1908 /* it's an abstract type */
1910 else if (length(tycon(tc).defn) == 1) {
1911 /* As we expect, has a single constructor */
1912 Name n = hd(tycon(tc).defn);
1913 Int line = name(n).line;
1914 assert(currentModule == name(n).mod);
1915 name(n).type = conidcellsToTycons(line,name(n).type);
1917 internal("finishGHCNewType(2)");
1922 /* --------------------------------------------------------------------------
1923 * Class declarations
1924 * ------------------------------------------------------------------------*/
1926 static Void startGHCClass(line,ctxt,tc_name,kinded_tvs,mems0)
1928 List ctxt; /* [((QConId, VarId))] */
1929 ConId tc_name; /* ConId */
1930 List kinded_tvs; /* [((VarId, Kind))] */
1931 List mems0; { /* [((VarId, Type))] */
1933 List mems; /* [((VarId, Type))] */
1934 List tvsInT; /* [VarId] and then [((VarId,Kind))] */
1935 List tvs; /* [((VarId,Kind))] */
1936 List ns; /* [Name] */
1939 ZPair kinded_tv = hd(kinded_tvs);
1940 Text ct = textOf(tc_name);
1941 Pair newCtx = pair(tc_name, zfst(kinded_tv));
1943 fprintf ( stderr, "begin startGHCClass %s\n", textToStr(ct) );
1947 if (length(kinded_tvs) != 1) {
1948 ERRMSG(line) "Cannot presently handle multiparam type classes in ifaces"
1952 if (nonNull(findClass(ct))) {
1953 ERRMSG(line) "Repeated definition of class \"%s\"",
1956 } else if (nonNull(findTycon(ct))) {
1957 ERRMSG(line) "\"%s\" used as both class and type constructor",
1961 Class nw = newClass(ct);
1962 cclass(nw).text = ct;
1963 cclass(nw).line = line;
1964 cclass(nw).arity = 1;
1965 cclass(nw).head = ap(nw,mkOffset(0));
1966 cclass(nw).kinds = singleton( zsnd(kinded_tv) );
1967 cclass(nw).instances = NIL;
1968 cclass(nw).numSupers = length(ctxt);
1970 /* Kludge to map the single tyvar in the context to Offset 0.
1971 Need to do something better for multiparam type classes.
1973 cclass(nw).supers = tvsToOffsets(line,ctxt,
1974 singleton(kinded_tv));
1977 for (mems=mems0; nonNull(mems); mems=tl(mems)) {
1978 ZPair mem = hd(mems);
1979 Type memT = zsnd(mem);
1980 Text mnt = textOf(zfst(mem));
1983 /* Stick the new context on the member type */
1984 memT = dictapsToQualtype(memT);
1985 if (whatIs(memT)==POLYTYPE) internal("startGHCClass");
1986 if (whatIs(memT)==QUAL) {
1988 pair(cons(newCtx,fst(snd(memT))),snd(snd(memT))));
1991 pair(singleton(newCtx),memT));
1994 /* Cook up a kind for the type. */
1995 tvsInT = ifTyvarsIn(memT);
1996 /* tvsInT :: [VarId] */
1998 /* ToDo: maximally bogus. We allow the class tyvar to
1999 have the kind as supplied by the parser, but we just
2000 assume that all others have kind *. It's a kludge.
2002 for (tvs=tvsInT; nonNull(tvs); tvs=tl(tvs)) {
2004 if (textOf(hd(tvs)) == textOf(zfst(kinded_tv)))
2005 k = zsnd(kinded_tv); else
2007 hd(tvs) = zpair(hd(tvs),k);
2009 /* tvsIntT :: [((VarId,Kind))] */
2011 memT = mkPolyType(tvsToKind(tvsInT),memT);
2012 memT = tvsToOffsets(line,memT,tvsInT);
2014 /* Park the type back on the member */
2015 mem = zpair(zfst(mem),memT);
2017 /* Bind code to the member */
2021 "Repeated definition for class method \"%s\"",
2025 mn = newName(mnt,NIL);
2030 cclass(nw).members = mems0;
2031 cclass(nw).numMembers = length(mems0);
2034 for (mno=0; mno<cclass(nw).numSupers; mno++) {
2035 ns = cons(newDSel(nw,mno),ns);
2037 cclass(nw).dsels = rev(ns);
2042 static Class finishGHCClass ( Tycon cls_tyc )
2047 Class nw = findClass ( textOf(cls_tyc) );
2049 fprintf ( stderr, "begin finishGHCClass %s\n", textToStr(cclass(nw).text) );
2051 if (isNull(nw)) internal("finishGHCClass");
2053 line = cclass(nw).line;
2055 assert (currentModule == cclass(nw).mod);
2057 cclass(nw).level = 0;
2058 cclass(nw).head = conidcellsToTycons(line,cclass(nw).head);
2059 cclass(nw).supers = conidcellsToTycons(line,cclass(nw).supers);
2060 cclass(nw).members = conidcellsToTycons(line,cclass(nw).members);
2062 for (mems=cclass(nw).members; nonNull(mems); mems=tl(mems)) {
2063 Pair mem = hd(mems); /* (VarId, Type) */
2064 Text txt = textOf(fst(mem));
2066 Name n = findName(txt);
2069 name(n).line = cclass(nw).line;
2071 name(n).number = ctr--;
2072 name(n).arity = arityInclDictParams(name(n).type);
2073 name(n).parent = nw;
2081 /* --------------------------------------------------------------------------
2083 * ------------------------------------------------------------------------*/
2085 static Inst startGHCInstance (line,ktyvars,cls,var)
2087 List ktyvars; /* [((VarId,Kind))] */
2088 Type cls; /* Type */
2089 VarId var; { /* VarId */
2090 List tmp, tvs, ks, spec;
2095 Inst in = newInst();
2097 fprintf ( stderr, "begin startGHCInstance\n" );
2102 tvs = ifTyvarsIn(cls); /* :: [VarId] */
2104 The order of tvs is important for tvsToOffsets.
2105 tvs should be a permutation of ktyvars. Fish the tyvar kinds
2106 out of ktyvars and attach them to tvs.
2108 for (xs1=tvs; nonNull(xs1); xs1=tl(xs1)) {
2110 for (xs2=ktyvars; nonNull(xs2); xs2=tl(xs2))
2111 if (textOf(hd(xs1)) == textOf(zfst(hd(xs2))))
2113 if (isNull(k)) internal("startGHCInstance: finding kinds");
2114 hd(xs1) = zpair(hd(xs1),k);
2117 cls = tvsToOffsets(line,cls,tvs);
2120 spec = cons(fun(cls),spec);
2123 spec = reverse(spec);
2125 inst(in).line = line;
2126 inst(in).implements = NIL;
2127 inst(in).kinds = simpleKind(length(tvs)); /* do this right */
2128 inst(in).specifics = spec;
2129 inst(in).numSpecifics = length(spec);
2130 inst(in).head = cls;
2132 /* Figure out the name of the class being instanced, and store it
2133 at inst(in).c. finishGHCInstance will resolve it to a real Class. */
2135 Cell cl = inst(in).head;
2136 assert(whatIs(cl)==DICTAP);
2137 cl = unap(DICTAP,cl);
2139 assert ( isQCon(cl) );
2144 Name b = newName( /*inventText()*/ textOf(var),NIL);
2145 name(b).line = line;
2146 name(b).arity = length(spec); /* unused? */ /* and surely wrong */
2147 name(b).number = DFUNNAME;
2148 name(b).parent = in;
2149 inst(in).builder = b;
2150 /* bindNameToClosure(b, lookupGHCClosure(inst(in).mod,var)); */
2157 static Void finishGHCInstance ( Inst in )
2164 fprintf ( stderr, "begin finishGHCInstance\n" );
2167 assert (nonNull(in));
2168 line = inst(in).line;
2169 assert (currentModule==inst(in).mod);
2171 /* inst(in).c is, prior to finishGHCInstance, a ConId or Tuple,
2172 since startGHCInstance couldn't possibly have resolved it to
2173 a Class at that point. We convert it to a Class now.
2177 c = findQualClassWithoutConsultingExportList(c);
2181 inst(in).head = conidcellsToTycons(line,inst(in).head);
2182 inst(in).specifics = conidcellsToTycons(line,inst(in).specifics);
2183 cclass(c).instances = cons(in,cclass(c).instances);
2187 /* --------------------------------------------------------------------------
2189 * ------------------------------------------------------------------------*/
2191 /* This is called from the startGHC* functions. It traverses a structure
2192 and converts varidcells, ie, type variables parsed by the interface
2193 parser, into Offsets, which is how Hugs wants to see them internally.
2194 The Offset for a type variable is determined by its place in the list
2195 passed as the second arg; the associated kinds are irrelevant.
2197 ((t1,t2)) denotes the typed (z-)pair of t1 and t2.
2200 /* tvsToOffsets :: LineNo -> Type -> [((VarId,Kind))] -> Type */
2201 static Type tvsToOffsets(line,type,ktyvars)
2204 List ktyvars; { /* [((VarId,Kind))] */
2205 switch (whatIs(type)) {
2212 case ZTUP2: /* convert to the untyped representation */
2213 return ap( tvsToOffsets(line,zfst(type),ktyvars),
2214 tvsToOffsets(line,zsnd(type),ktyvars) );
2216 return ap( tvsToOffsets(line,fun(type),ktyvars),
2217 tvsToOffsets(line,arg(type),ktyvars) );
2221 tvsToOffsets(line,monotypeOf(type),ktyvars)
2225 return pair(QUAL,pair(tvsToOffsets(line,fst(snd(type)),ktyvars),
2226 tvsToOffsets(line,snd(snd(type)),ktyvars)));
2227 case DICTAP: /* bogus ?? */
2228 return ap(DICTAP, tvsToOffsets(line,snd(type),ktyvars));
2229 case UNBOXEDTUP: /* bogus?? */
2230 return ap(UNBOXEDTUP, tvsToOffsets(line,snd(type),ktyvars));
2231 case BANG: /* bogus?? */
2232 return ap(BANG, tvsToOffsets(line,snd(type),ktyvars));
2233 case VARIDCELL: /* Ha! some real work to do! */
2235 Text tv = textOf(type);
2236 for (; nonNull(ktyvars); i++,ktyvars=tl(ktyvars)) {
2239 assert(isZPair(hd(ktyvars)));
2240 varid = zfst(hd(ktyvars));
2242 if (tv == tt) return mkOffset(i);
2244 ERRMSG(line) "Undefined type variable \"%s\"", textToStr(tv)
2249 fprintf(stderr, "tvsToOffsets: unknown stuff %d\n", whatIs(type));
2251 fprintf(stderr,"\n");
2255 return NIL; /* NOTREACHED */
2259 /* This is called from the finishGHC* functions. It traverses a structure
2260 and converts conidcells, ie, type constructors parsed by the interface
2261 parser, into Tycons (or Classes), which is how Hugs wants to see them
2262 internally. Calls to this fn have to be deferred to the second phase
2263 of interface loading (finishGHC* rather than startGHC*) so that all relevant
2264 Tycons or Classes have been loaded into the symbol tables and can be
2267 static Type conidcellsToTycons ( Int line, Type type )
2269 switch (whatIs(type)) {
2279 { Cell t; /* Tycon or Class */
2280 Text m = qmodOf(type);
2281 Module mod = findModule(m);
2284 "Undefined module in qualified name \"%s\"",
2289 t = findQualTyconWithoutConsultingExportList(type);
2290 if (nonNull(t)) return t;
2291 t = findQualClassWithoutConsultingExportList(type);
2292 if (nonNull(t)) return t;
2294 "Undefined qualified class or type \"%s\"",
2302 cl = findQualClass(type);
2303 if (nonNull(cl)) return cl;
2304 if (textOf(type)==findText("[]"))
2305 /* a hack; magically qualify [] into PrelBase.[] */
2306 return conidcellsToTycons(line,
2307 mkQualId(mkCon(findText("PrelBase")),type));
2308 tc = findQualTycon(type);
2309 if (nonNull(tc)) return tc;
2311 "Undefined class or type constructor \"%s\"",
2317 return ap( conidcellsToTycons(line,fun(type)),
2318 conidcellsToTycons(line,arg(type)) );
2319 case ZTUP2: /* convert to std pair */
2320 return ap( conidcellsToTycons(line,zfst(type)),
2321 conidcellsToTycons(line,zsnd(type)) );
2326 conidcellsToTycons(line,monotypeOf(type))
2330 return pair(QUAL,pair(conidcellsToTycons(line,fst(snd(type))),
2331 conidcellsToTycons(line,snd(snd(type)))));
2332 case DICTAP: /* :: ap(DICTAP, pair(Class,Type))
2333 Not sure if this is really the right place to
2334 convert it to the form Hugs wants, but will do so anyway.
2336 /* return ap(DICTAP, conidcellsToTycons(line, snd(type))); */
2338 Class cl = fst(unap(DICTAP,type));
2339 List args = snd(unap(DICTAP,type));
2341 conidcellsToTycons(line,pair(cl,args));
2344 return ap(UNBOXEDTUP, conidcellsToTycons(line, snd(type)));
2346 return ap(BANG, conidcellsToTycons(line, snd(type)));
2348 fprintf(stderr, "conidcellsToTycons: unknown stuff %d\n",
2351 fprintf(stderr,"\n");
2355 return NIL; /* NOTREACHED */
2359 /* Find out if a type mentions a type constructor not present in
2360 the supplied list of qualified tycons.
2362 static Bool allTypesKnown ( Type type,
2363 List aktys /* [QualId] */,
2366 switch (whatIs(type)) {
2373 return allTypesKnown(fun(type),aktys,thisMod)
2374 && allTypesKnown(arg(type),aktys,thisMod);
2376 return allTypesKnown(zfst(type),aktys,thisMod)
2377 && allTypesKnown(zsnd(type),aktys,thisMod);
2379 return allTypesKnown(unap(DICTAP,type),aktys,thisMod);
2382 if (textOf(type)==findText("[]"))
2383 /* a hack; magically qualify [] into PrelBase.[] */
2384 type = mkQualId(mkCon(findText("PrelBase")),type); else
2385 type = mkQualId(thisMod,type);
2388 if (isNull(qualidIsMember(type,aktys))) goto missing;
2394 fprintf(stderr, "allTypesKnown: unknown stuff %d\n", whatIs(type));
2395 print(type,10);printf("\n");
2396 internal("allTypesKnown");
2397 return TRUE; /*notreached*/
2401 fprintf ( stderr,"allTypesKnown: unknown " ); print(type,10);
2402 fprintf(stderr,"\n");
2408 /* --------------------------------------------------------------------------
2411 * None of these do lookups or require that lookups have been resolved
2412 * so they can be performed while reading interfaces.
2413 * ------------------------------------------------------------------------*/
2415 /* tvsToKind :: [((VarId,Kind))] -> Kinds */
2416 static Kinds tvsToKind(tvs)
2417 List tvs; { /* [((VarId,Kind))] */
2420 for (rs=reverse(tvs); nonNull(rs); rs=tl(rs)) {
2421 if (whatIs(hd(rs)) != ZTUP2) internal("tvsToKind(1)");
2422 if (whatIs(zfst(hd(rs))) != VARIDCELL) internal("tvsToKind(2)");
2423 r = ap(zsnd(hd(rs)),r);
2429 static Int arityInclDictParams ( Type type )
2432 if (isPolyType(type)) type = monotypeOf(type);
2434 if (whatIs(type) == QUAL)
2436 arity += length ( fst(snd(type)) );
2437 type = snd(snd(type));
2439 while (isAp(type) && getHead(type)==typeArrow) {
2446 /* arity of a constructor with this type */
2447 static Int arityFromType(type)
2450 if (isPolyType(type)) {
2451 type = monotypeOf(type);
2453 if (whatIs(type) == QUAL) {
2454 type = snd(snd(type));
2456 if (whatIs(type) == EXIST) {
2457 type = snd(snd(type));
2459 if (whatIs(type)==RANK2) {
2460 type = snd(snd(type));
2462 while (isAp(type) && getHead(type)==typeArrow) {
2470 /* ifTyvarsIn :: Type -> [VarId]
2471 The returned list has no duplicates -- is a set.
2473 static List ifTyvarsIn(type)
2475 List vs = typeVarsIn(type,NIL,NIL,NIL);
2477 for (; nonNull(vs2); vs2=tl(vs2))
2478 if (whatIs(hd(vs2)) != VARIDCELL)
2479 internal("ifTyvarsIn");
2485 /* --------------------------------------------------------------------------
2486 * General object symbol query stuff
2487 * ------------------------------------------------------------------------*/
2489 #define EXTERN_SYMS_ALLPLATFORMS \
2490 Sym(stg_gc_enter_1) \
2491 Sym(stg_gc_noregs) \
2499 Sym(stg_update_PAP) \
2500 Sym(stg_error_entry) \
2501 Sym(__ap_2_upd_info) \
2502 Sym(__ap_3_upd_info) \
2503 Sym(__ap_4_upd_info) \
2504 Sym(__ap_5_upd_info) \
2505 Sym(__ap_6_upd_info) \
2506 Sym(__ap_7_upd_info) \
2507 Sym(__ap_8_upd_info) \
2508 Sym(__sel_0_upd_info) \
2509 Sym(__sel_1_upd_info) \
2510 Sym(__sel_2_upd_info) \
2511 Sym(__sel_3_upd_info) \
2512 Sym(__sel_4_upd_info) \
2513 Sym(__sel_5_upd_info) \
2514 Sym(__sel_6_upd_info) \
2515 Sym(__sel_7_upd_info) \
2516 Sym(__sel_8_upd_info) \
2517 Sym(__sel_9_upd_info) \
2518 Sym(__sel_10_upd_info) \
2519 Sym(__sel_11_upd_info) \
2520 Sym(__sel_12_upd_info) \
2522 Sym(Upd_frame_info) \
2523 Sym(seq_frame_info) \
2524 Sym(CAF_BLACKHOLE_info) \
2525 Sym(IND_STATIC_info) \
2526 Sym(EMPTY_MVAR_info) \
2527 Sym(MUT_ARR_PTRS_FROZEN_info) \
2529 Sym(putMVarzh_fast) \
2530 Sym(newMVarzh_fast) \
2531 Sym(takeMVarzh_fast) \
2536 Sym(killThreadzh_fast) \
2537 Sym(waitReadzh_fast) \
2538 Sym(waitWritezh_fast) \
2539 Sym(CHARLIKE_closure) \
2540 Sym(INTLIKE_closure) \
2541 Sym(suspendThread) \
2543 Sym(stackOverflow) \
2544 Sym(int2Integerzh_fast) \
2545 Sym(stg_gc_unbx_r1) \
2547 Sym(makeForeignObjzh_fast) \
2548 Sym(__encodeDouble) \
2549 Sym(decodeDoublezh_fast) \
2551 Sym(isDoubleInfinite) \
2552 Sym(isDoubleDenormalized) \
2553 Sym(isDoubleNegativeZero) \
2554 Sym(__encodeFloat) \
2555 Sym(decodeFloatzh_fast) \
2557 Sym(isFloatInfinite) \
2558 Sym(isFloatDenormalized) \
2559 Sym(isFloatNegativeZero) \
2560 Sym(__int_encodeFloat) \
2561 Sym(__int_encodeDouble) \
2565 Sym(gcdIntegerzh_fast) \
2566 Sym(newArrayzh_fast) \
2567 Sym(unsafeThawArrayzh_fast) \
2568 Sym(newDoubleArrayzh_fast) \
2569 Sym(newFloatArrayzh_fast) \
2570 Sym(newAddrArrayzh_fast) \
2571 Sym(newWordArrayzh_fast) \
2572 Sym(newIntArrayzh_fast) \
2573 Sym(newCharArrayzh_fast) \
2574 Sym(newMutVarzh_fast) \
2575 Sym(quotRemIntegerzh_fast) \
2576 Sym(quotIntegerzh_fast) \
2577 Sym(remIntegerzh_fast) \
2578 Sym(divExactIntegerzh_fast) \
2579 Sym(divModIntegerzh_fast) \
2580 Sym(timesIntegerzh_fast) \
2581 Sym(minusIntegerzh_fast) \
2582 Sym(plusIntegerzh_fast) \
2583 Sym(addr2Integerzh_fast) \
2584 Sym(mkWeakzh_fast) \
2587 Sym(resetNonBlockingFd) \
2589 Sym(stable_ptr_table) \
2590 Sym(createAdjThunk) \
2591 Sym(shutdownHaskellAndExit) \
2592 Sym(stg_enterStackTop) \
2593 Sym(CAF_UNENTERED_entry) \
2594 Sym(stg_yield_to_Hugs) \
2598 /* needed by libHS_cbits */ \
2637 #define EXTERN_SYMS_cygwin32 \
2638 SymX(GetCurrentProcess) \
2639 SymX(GetProcessTimes) \
2648 Sym(__imp__tzname) \
2649 Sym(__imp__timezone) \
2668 #define EXTERN_SYMS_linux \
2669 Sym(__errno_location) \
2681 #if defined(linux_TARGET_OS)
2682 #define EXTERN_SYMS_THISPLATFORM EXTERN_SYMS_linux
2685 #if defined(solaris2_TARGET_OS)
2686 #define EXTERN_SYMS_THISPLATFORM EXTERN_SYMS_solaris2
2689 #if defined(cygwin32_TARGET_OS)
2690 #define EXTERN_SYMS_THISPLATFORM EXTERN_SYMS_cygwin32
2696 /* entirely bogus claims about types of these symbols */
2697 #define Sym(vvv) extern void (vvv);
2698 #define SymX(vvv) /**/
2699 EXTERN_SYMS_ALLPLATFORMS
2700 EXTERN_SYMS_THISPLATFORM
2705 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
2707 #define SymX(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
2711 EXTERN_SYMS_ALLPLATFORMS
2712 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 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 LEADING_UNDERSCORE
2756 first_real_char = 1;
2758 first_real_char = 0;
2761 /* Maybe it's an __init_Module thing? */
2762 if (strlen(nm2+first_real_char) > 7
2763 && strncmp(nm2+first_real_char, "__init_", 7)==0) {
2764 t = unZcodeThenFindText(nm2+first_real_char+7);
2765 if (t == findText("PrelGHC")) return (4+NULL); /* kludge */
2767 if (isNull(m)) goto not_found;
2768 a = lookupOTabName ( m, nm );
2773 /* if not an RTS name, look in the
2774 relevant module's object symbol table
2776 pp = strchr(nm2+first_real_char, '_');
2777 if (!pp || !isupper(nm2[first_real_char])) goto not_found;
2779 t = unZcodeThenFindText(nm2+first_real_char);
2781 if (isNull(m)) goto not_found;
2783 a = lookupOTabName ( m, nm ); /* RATIONALISE */
2788 "lookupObjName: can't resolve name `%s'\n",
2795 int is_dynamically_loaded_code_or_rodata_ptr ( char* p )
2797 OSectionKind sk = lookupSection(p);
2798 assert (sk != HUGS_SECTIONKIND_NOINFOAVAIL);
2799 return (sk == HUGS_SECTIONKIND_CODE_OR_RODATA);
2803 int is_dynamically_loaded_rwdata_ptr ( char* p )
2805 OSectionKind sk = lookupSection(p);
2806 assert (sk != HUGS_SECTIONKIND_NOINFOAVAIL);
2807 return (sk == HUGS_SECTIONKIND_RWDATA);
2811 int is_not_dynamically_loaded_ptr ( char* p )
2813 OSectionKind sk = lookupSection(p);
2814 assert (sk != HUGS_SECTIONKIND_NOINFOAVAIL);
2815 return (sk == HUGS_SECTIONKIND_OTHER);
2819 /* --------------------------------------------------------------------------
2821 * ------------------------------------------------------------------------*/
2823 Void interface(what)
2826 case POSTPREL: break;
2830 ifaces_outstanding = NIL;
2833 mark(ifaces_outstanding);
2838 /*-------------------------------------------------------------------------*/