[project @ 1996-06-05 06:44:31 by partain]
[ghc-hetmet.git] / ghc / compiler / prelude / PrelInfo.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[PrelInfo]{The @PrelInfo@ interface to the compiler's prelude knowledge}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module PrelInfo (
10
11         -- finite maps for built-in things (for the renamer and typechecker):
12         builtinNameInfo, BuiltinNames(..),
13         BuiltinKeys(..), BuiltinIdInfos(..),
14
15         maybeCharLikeTyCon, maybeIntLikeTyCon
16     ) where
17
18 IMP_Ubiq()
19 IMPORT_DELOOPER(PrelLoop)               ( primOpNameInfo )
20
21 -- friends:
22 import PrelMods         -- Prelude module names
23 import PrelVals         -- VALUES
24 import PrimOp           ( PrimOp(..), allThePrimOps )
25 import PrimRep          ( PrimRep(..) )
26 import TysPrim          -- TYPES
27 import TysWiredIn
28
29 -- others:
30 import CmdLineOpts      ( opt_HideBuiltinNames,
31                           opt_HideMostBuiltinNames,
32                           opt_ForConcurrent
33                         )
34 import FiniteMap        ( FiniteMap, emptyFM, listToFM )
35 import Id               ( mkTupleCon, GenId, Id(..) )
36 import Maybes           ( catMaybes )
37 import Name             ( moduleNamePair )
38 import RnHsSyn          ( RnName(..) )
39 import TyCon            ( tyConDataCons, mkFunTyCon, mkTupleTyCon, TyCon )
40 import Type
41 import UniqFM           ( UniqFM, emptyUFM, listToUFM )
42 import Unique           -- *Key stuff
43 import Util             ( nOfThem, panic )
44 \end{code}
45
46 %************************************************************************
47 %*                                                                      *
48 \subsection[builtinNameInfo]{Lookup built-in names}
49 %*                                                                      *
50 %************************************************************************
51
52 We have two ``builtin name funs,'' one to look up @TyCons@ and
53 @Classes@, the other to look up values.
54
55 \begin{code}
56 builtinNameInfo :: ( BuiltinNames, BuiltinKeys, BuiltinIdInfos )
57
58 type BuiltinNames   = (FiniteMap (FAST_STRING,Module) RnName, -- WiredIn Ids
59                        FiniteMap (FAST_STRING,Module) RnName) -- WiredIn TyCons
60                         -- Two maps because "[]" is in both...
61
62 type BuiltinKeys    = FiniteMap (FAST_STRING,Module) (Unique, Name -> RnName)
63                                                      -- Names with known uniques
64
65 type BuiltinIdInfos = UniqFM IdInfo                  -- Info for known unique Ids
66
67 builtinNameInfo
68   = if opt_HideBuiltinNames then
69         (
70          (emptyFM, emptyFM),
71          emptyFM,
72          emptyUFM
73         )
74     else if opt_HideMostBuiltinNames then
75         (
76          (listToFM min_assoc_val_wired, listToFM min_assoc_tc_wired),
77          emptyFM,
78          emptyUFM
79         )
80     else
81         (
82          (listToFM assoc_val_wired, listToFM assoc_tc_wired),
83          listToFM assoc_keys,
84          listToUFM assoc_id_infos
85         )
86
87   where
88     min_assoc_val_wired -- min needed when compiling bits of Prelude
89       = concat [
90             -- data constrs
91             concat (map pcDataConWiredInInfo g_con_tycons),
92             concat (map pcDataConWiredInInfo min_nonprim_tycon_list),
93
94             -- values
95             map pcIdWiredInInfo wired_in_ids,
96             primop_ids
97          ]
98     min_assoc_tc_wired
99       = concat [
100             -- tycons
101             map pcTyConWiredInInfo prim_tycons,
102             map pcTyConWiredInInfo g_tycons,
103             map pcTyConWiredInInfo min_nonprim_tycon_list
104          ]
105
106     assoc_val_wired
107         = concat [
108             -- data constrs
109             concat (map pcDataConWiredInInfo g_con_tycons),
110             concat (map pcDataConWiredInInfo data_tycons),
111
112             -- values
113             map pcIdWiredInInfo wired_in_ids,
114             map pcIdWiredInInfo parallel_ids,
115             primop_ids
116           ]
117     assoc_tc_wired
118         = concat [
119             -- tycons
120             map pcTyConWiredInInfo prim_tycons,
121             map pcTyConWiredInInfo g_tycons,
122             map pcTyConWiredInInfo data_tycons
123           ]
124
125     assoc_keys
126         = concat
127           [
128             id_keys,
129             tysyn_keys,
130             class_keys,
131             class_op_keys
132           ]
133
134     id_keys = map id_key id_keys_infos
135     id_key (str_mod, uniq, info) = (str_mod, (uniq, RnImplicit))
136
137     assoc_id_infos = catMaybes (map assoc_info id_keys_infos)
138     assoc_info (str_mod, uniq, Just info) = Just (uniq, info)
139     assoc_info (str_mod, uniq, Nothing)   = Nothing
140 \end{code}
141
142
143 We let a lot of "non-standard" values be visible, so that we can make
144 sense of them in interface pragmas. It's cool, though they all have
145 "non-standard" names, so they won't get past the parser in user code.
146
147 The WiredIn TyCons and DataCons ...
148 \begin{code}
149
150 prim_tycons
151   = [ addrPrimTyCon
152     , arrayPrimTyCon
153     , byteArrayPrimTyCon
154     , charPrimTyCon
155     , doublePrimTyCon
156     , floatPrimTyCon
157     , intPrimTyCon
158     , foreignObjPrimTyCon
159     , mutableArrayPrimTyCon
160     , mutableByteArrayPrimTyCon
161     , synchVarPrimTyCon
162     , realWorldTyCon
163     , stablePtrPrimTyCon
164     , statePrimTyCon
165     , wordPrimTyCon
166     ]
167
168 g_tycons
169   = mkFunTyCon : g_con_tycons
170
171 g_con_tycons
172   = listTyCon : mkTupleTyCon 0 : [mkTupleTyCon i | i <- [2..32] ]
173
174 min_nonprim_tycon_list  -- used w/ HideMostBuiltinNames
175   = [ boolTyCon
176     , charTyCon
177     , intTyCon
178     , floatTyCon
179     , doubleTyCon
180     , integerTyCon
181     , liftTyCon
182     , return2GMPsTyCon  -- ADR asked for these last two (WDP 94/11)
183     , returnIntAndGMPTyCon
184     ]
185
186
187 data_tycons
188   = [ addrTyCon
189     , boolTyCon
190     , charTyCon
191     , doubleTyCon
192     , floatTyCon
193     , foreignObjTyCon
194     , intTyCon
195     , integerTyCon
196     , liftTyCon
197     , primIoTyCon
198     , return2GMPsTyCon
199     , returnIntAndGMPTyCon
200     , stTyCon
201     , stablePtrTyCon
202     , stateAndAddrPrimTyCon
203     , stateAndArrayPrimTyCon
204     , stateAndByteArrayPrimTyCon
205     , stateAndCharPrimTyCon
206     , stateAndDoublePrimTyCon
207     , stateAndFloatPrimTyCon
208     , stateAndForeignObjPrimTyCon
209     , stateAndIntPrimTyCon
210     , stateAndMutableArrayPrimTyCon
211     , stateAndMutableByteArrayPrimTyCon
212     , stateAndPtrPrimTyCon
213     , stateAndStablePtrPrimTyCon
214     , stateAndSynchVarPrimTyCon
215     , stateAndWordPrimTyCon
216     , stateTyCon
217     , wordTyCon
218     ]
219 \end{code}
220
221 The WiredIn Ids ...
222 ToDo: Some of these should be moved to id_keys_infos!
223 \begin{code}
224 wired_in_ids
225   = [ eRROR_ID
226     , pAT_ERROR_ID      -- occurs in i/faces
227     , pAR_ERROR_ID      -- ditto
228     , tRACE_ID
229  
230     , runSTId
231     , seqId
232     , realWorldPrimId
233
234       -- foldr/build Ids have magic unfoldings
235     , buildId
236     , augmentId
237     , foldlId
238     , foldrId
239     , unpackCStringAppendId
240     , unpackCStringFoldrId
241     ]
242
243 parallel_ids
244   = if not opt_ForConcurrent then
245         []
246     else
247         [ parId
248         , forkId
249         , copyableId
250         , noFollowId
251         , parAtAbsId
252         , parAtForNowId
253         , parAtId
254         , parAtRelId
255         , parGlobalId
256         , parLocalId
257         ]
258
259
260 pcTyConWiredInInfo :: TyCon -> ((FAST_STRING,Module), RnName)
261 pcTyConWiredInInfo tc = (swap (moduleNamePair tc), WiredInTyCon tc)
262
263 pcDataConWiredInInfo :: TyCon -> [((FAST_STRING,Module), RnName)]
264 pcDataConWiredInInfo tycon
265   = [ (swap (moduleNamePair con), WiredInId con) | con <- tyConDataCons tycon ]
266
267 pcIdWiredInInfo :: Id -> ((FAST_STRING,Module), RnName)
268 pcIdWiredInInfo id = (swap (moduleNamePair id), WiredInId id)
269
270 swap (x,y) = (y,x)
271 \end{code}
272
273 WiredIn primitive numeric operations ...
274 \begin{code}
275 primop_ids
276   = map prim_fn allThePrimOps ++ map funny_fn funny_name_primops
277   where
278     prim_fn  op     = case (primOpNameInfo op) of (s,n) -> ((s,pRELUDE),n)
279     funny_fn (op,s) = case (primOpNameInfo op) of (_,n) -> ((s,pRELUDE),n)
280
281 funny_name_primops
282   = [ (IntAddOp,      SLIT("+#"))
283     , (IntSubOp,      SLIT("-#"))
284     , (IntMulOp,      SLIT("*#"))
285     , (IntGtOp,       SLIT(">#"))
286     , (IntGeOp,       SLIT(">=#"))
287     , (IntEqOp,       SLIT("==#"))
288     , (IntNeOp,       SLIT("/=#"))
289     , (IntLtOp,       SLIT("<#"))
290     , (IntLeOp,       SLIT("<=#"))
291     , (DoubleAddOp,   SLIT("+##"))
292     , (DoubleSubOp,   SLIT("-##"))
293     , (DoubleMulOp,   SLIT("*##"))
294     , (DoubleDivOp,   SLIT("/##"))
295     , (DoublePowerOp, SLIT("**##"))
296     , (DoubleGtOp,    SLIT(">##"))
297     , (DoubleGeOp,    SLIT(">=##"))
298     , (DoubleEqOp,    SLIT("==##"))
299     , (DoubleNeOp,    SLIT("/=##"))
300     , (DoubleLtOp,    SLIT("<##"))
301     , (DoubleLeOp,    SLIT("<=##"))
302     ]
303 \end{code}
304
305
306 Ids, Synonyms, Classes and ClassOps with builtin keys.
307 For the Ids we may also have some builtin IdInfo.
308 \begin{code}
309 id_keys_infos :: [((FAST_STRING,Module), Unique, Maybe IdInfo)]
310 id_keys_infos
311   = [ -- here so we can check the type of main/mainPrimIO
312       ((SLIT("main"),SLIT("Main")),       mainIdKey,       Nothing)
313     , ((SLIT("mainPrimIO"),SLIT("Main")), mainPrimIOIdKey, Nothing)
314
315       -- here because we use them in derived instances
316     , ((SLIT("&&"),          pRELUDE),  andandIdKey,    Nothing)
317     , ((SLIT("."),           pRELUDE),  composeIdKey,   Nothing)
318     , ((SLIT("lex"),         pRELUDE),  lexIdKey,       Nothing)
319     , ((SLIT("not"),         pRELUDE),  notIdKey,       Nothing)
320     , ((SLIT("readParen"),   pRELUDE),  readParenIdKey, Nothing)
321     , ((SLIT("showParen"),   pRELUDE),  showParenIdKey, Nothing)
322     , ((SLIT("showString"),  pRELUDE),  showStringIdKey,Nothing)
323     , ((SLIT("__readList"),  pRELUDE),  ureadListIdKey, Nothing)
324     , ((SLIT("__showList"),  pRELUDE),  ushowListIdKey, Nothing)
325     , ((SLIT("__showSpace"), pRELUDE),  showSpaceIdKey, Nothing)
326     ]
327
328 tysyn_keys
329   = [ ((SLIT("IO"),pRELUDE),       (iOTyConKey, RnImplicitTyCon))
330     , ((SLIT("Rational"),rATIO),   (rationalTyConKey, RnImplicitTyCon))
331     , ((SLIT("Ratio"),rATIO),      (ratioTyConKey, RnImplicitTyCon))
332     , ((SLIT("Ordering"),pRELUDE), (orderingTyConKey, RnImplicitTyCon))
333     ]
334
335 -- this "class_keys" list *must* include:
336 --  classes that are grabbed by key (e.g., eqClassKey)
337 --  classes in "Class.standardClassKeys" (quite a few)
338
339 class_keys
340   = [ (str_mod, (k, RnImplicitClass)) | (str_mod,k) <-
341     [ ((SLIT("Eq"),pRELUDE),            eqClassKey)             -- mentioned, derivable
342     , ((SLIT("Eval"),pRELUDE),          evalClassKey)           -- mentioned
343     , ((SLIT("Ord"),pRELUDE),           ordClassKey)            -- derivable
344     , ((SLIT("Num"),pRELUDE),           numClassKey)            -- mentioned, numeric
345     , ((SLIT("Real"),pRELUDE),          realClassKey)           -- numeric
346     , ((SLIT("Integral"),pRELUDE),      integralClassKey)       -- numeric
347     , ((SLIT("Fractional"),pRELUDE),    fractionalClassKey)     -- numeric
348     , ((SLIT("Floating"),pRELUDE),      floatingClassKey)       -- numeric
349     , ((SLIT("RealFrac"),pRELUDE),      realFracClassKey)       -- numeric
350     , ((SLIT("RealFloat"),pRELUDE),     realFloatClassKey)      -- numeric
351     , ((SLIT("Ix"),iX),                 ixClassKey)             -- derivable (but it isn't Prelude.Ix; hmmm)
352     , ((SLIT("Bounded"),pRELUDE),       boundedClassKey)        -- derivable
353     , ((SLIT("Enum"),pRELUDE),          enumClassKey)           -- derivable
354     , ((SLIT("Show"),pRELUDE),          showClassKey)           -- derivable
355     , ((SLIT("Read"),pRELUDE),          readClassKey)           -- derivable
356     , ((SLIT("Monad"),pRELUDE),         monadClassKey)
357     , ((SLIT("MonadZero"),pRELUDE),     monadZeroClassKey)
358     , ((SLIT("MonadPlus"),pRELUDE),     monadPlusClassKey)
359     , ((SLIT("Functor"),pRELUDE),       functorClassKey)
360     , ((SLIT("_CCallable"),pRELUDE),    cCallableClassKey)      -- mentioned, ccallish
361     , ((SLIT("_CReturnable"),pRELUDE),  cReturnableClassKey)    -- mentioned, ccallish
362     ]]
363
364 class_op_keys
365   = [ (str_mod, (k, RnImplicit)) | (str_mod,k) <-
366     [ ((SLIT("fromInt"),pRELUDE),       fromIntClassOpKey)
367     , ((SLIT("fromInteger"),pRELUDE),   fromIntegerClassOpKey)
368     , ((SLIT("fromRational"),pRELUDE),  fromRationalClassOpKey)
369     , ((SLIT("enumFrom"),pRELUDE),      enumFromClassOpKey)
370     , ((SLIT("enumFromThen"),pRELUDE),  enumFromThenClassOpKey)
371     , ((SLIT("enumFromTo"),pRELUDE),    enumFromToClassOpKey)
372     , ((SLIT("enumFromThenTo"),pRELUDE),enumFromThenToClassOpKey)
373     , ((SLIT("=="),pRELUDE),            eqClassOpKey)
374     , ((SLIT(">>="),pRELUDE),           thenMClassOpKey)
375     , ((SLIT("zero"),pRELUDE),          zeroClassOpKey)
376     ]]
377 \end{code}
378
379 ToDo: make it do the ``like'' part properly (as in 0.26 and before).
380 \begin{code}
381 maybeCharLikeTyCon tc = if (uniqueOf tc == charDataConKey) then Just charDataCon else Nothing
382 maybeIntLikeTyCon  tc = if (uniqueOf tc == intDataConKey)  then Just intDataCon  else Nothing
383 \end{code}