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