[project @ 2002-03-05 14:18:53 by simonmar]
[ghc-hetmet.git] / ghc / compiler / prelude / PrelInfo.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[PrelInfo]{The @PrelInfo@ interface to the compiler's prelude knowledge}
5
6 \begin{code}
7 module PrelInfo (
8         module PrelNames,
9         module MkId,
10
11         wiredInThings,  -- Names of wired in things
12         wiredInThingEnv,
13         ghcPrimExports,
14         cCallableClassDecl, cReturnableClassDecl, assertDecl,
15         
16         -- Primop RdrNames
17         eqH_Char_RDR,   ltH_Char_RDR,   eqH_Word_RDR,  ltH_Word_RDR, 
18         eqH_Addr_RDR,   ltH_Addr_RDR,   eqH_Float_RDR, ltH_Float_RDR, 
19         eqH_Double_RDR, ltH_Double_RDR, eqH_Int_RDR,   ltH_Int_RDR,
20         geH_RDR, leH_RDR, minusH_RDR, tagToEnumH_RDR, 
21
22         -- Random other things
23         maybeCharLikeCon, maybeIntLikeCon,
24
25         -- Class categories
26         isCcallishClass, isCreturnableClass, isNoDictClass, 
27         isNumericClass, isStandardClass
28
29     ) where
30
31 #include "HsVersions.h"
32
33 import PrelNames        -- Prelude module names
34
35 import PrimOp           ( PrimOp(..), allThePrimOps, primOpRdrName, primOpOcc )
36 import DataCon          ( DataCon )
37 import Id               ( idName )
38 import MkId             ( mkPrimOpId, wiredInIds )
39 import MkId             -- All of it, for re-export
40 import Name             ( nameOccName, nameRdrName )
41 import RdrName          ( mkRdrUnqual )
42 import HsSyn            ( HsTyVarBndr(..), TyClDecl(..), HsType(..) )
43 import OccName          ( mkVarOcc )
44 import TysPrim          ( primTyCons )
45 import TysWiredIn       ( wiredInTyCons )
46 import RdrHsSyn         ( mkClassDecl )
47 import HscTypes         ( TyThing(..), implicitTyThingIds, TypeEnv, mkTypeEnv,
48                           GenAvailInfo(..), RdrAvailInfo )
49 import Class            ( Class, classKey )
50 import Type             ( funTyCon, openTypeKind, liftedTypeKind )
51 import TyCon            ( tyConName )
52 import SrcLoc           ( noSrcLoc )
53 import Util             ( isIn )
54 \end{code}
55
56 %************************************************************************
57 %*                                                                      *
58 \subsection[builtinNameInfo]{Lookup built-in names}
59 %*                                                                      *
60 %************************************************************************
61
62 We have two ``builtin name funs,'' one to look up @TyCons@ and
63 @Classes@, the other to look up values.
64
65 \begin{code}
66 wiredInThings :: [TyThing]
67 wiredInThings
68   = concat
69     [           -- Wired in TyCons and their implicit Ids
70           tycon_things
71         , map AnId (implicitTyThingIds tycon_things)
72
73                 -- Wired in Ids
74         , map AnId wiredInIds
75
76                 -- PrimOps
77         , map (AnId . mkPrimOpId) allThePrimOps
78     ]
79   where
80     tycon_things = map ATyCon ([funTyCon] ++ primTyCons ++ wiredInTyCons)
81
82 wiredInThingEnv :: TypeEnv
83 wiredInThingEnv = mkTypeEnv wiredInThings
84 \end{code}
85
86 We let a lot of "non-standard" values be visible, so that we can make
87 sense of them in interface pragmas. It's cool, though they all have
88 "non-standard" names, so they won't get past the parser in user code.
89
90 %************************************************************************
91 %*                                                                      *
92 \subsection{Export lists for pseudo-modules (GHC.Prim)}
93 %*                                                                      *
94 %************************************************************************
95
96 GHC.Prim "exports" all the primops and primitive types, some 
97 wired-in Ids, and the CCallable & CReturnable classes.
98
99 \begin{code}
100 ghcPrimExports :: [RdrAvailInfo]
101  = AvailTC cCallableOcc [ cCallableOcc ] :
102    AvailTC cReturnableOcc [ cReturnableOcc ] :
103    Avail (nameOccName assertName) :     -- doesn't have an Id
104    map (Avail . nameOccName . idName) ghcPrimIds ++
105    map (Avail . primOpOcc) allThePrimOps ++
106    [ AvailTC occ [occ] |
107      n <- funTyCon : primTyCons, let occ = nameOccName (tyConName n) 
108    ]
109  where
110    cCallableOcc = nameOccName cCallableClassName
111    cReturnableOcc = nameOccName cReturnableClassName
112
113 assertDecl
114   = IfaceSig { 
115         tcdName = nameRdrName assertName,
116         tcdType = HsForAllTy (Just [liftedAlpha]) [] (HsTyVar alpha),
117         tcdIdInfo = [],
118         tcdLoc = noSrcLoc
119     }
120
121 cCallableClassDecl
122   = mkClassDecl
123     ([], nameRdrName cCallableClassName, [openAlpha])
124     [] -- no fds
125     [] -- no sigs
126     Nothing -- no mbinds
127     noSrcLoc
128
129 cReturnableClassDecl
130   = mkClassDecl
131     ([], nameRdrName cReturnableClassName, [openAlpha])
132     [] -- no fds
133     [] -- no sigs
134     Nothing -- no mbinds
135     noSrcLoc
136
137 alpha = mkRdrUnqual (mkVarOcc FSLIT("a"))
138 openAlpha = IfaceTyVar alpha openTypeKind
139 liftedAlpha = IfaceTyVar alpha liftedTypeKind
140 \end{code}
141
142 %************************************************************************
143 %*                                                                      *
144 \subsection{RdrNames for the primops}
145 %*                                                                      *
146 %************************************************************************
147
148 These can't be in PrelNames, because we get the RdrName from the PrimOp,
149 which is above PrelNames in the module hierarchy.
150
151 \begin{code}
152 eqH_Char_RDR    = primOpRdrName CharEqOp
153 ltH_Char_RDR    = primOpRdrName CharLtOp
154 eqH_Word_RDR    = primOpRdrName WordEqOp
155 ltH_Word_RDR    = primOpRdrName WordLtOp
156 eqH_Addr_RDR    = primOpRdrName AddrEqOp
157 ltH_Addr_RDR    = primOpRdrName AddrLtOp
158 eqH_Float_RDR   = primOpRdrName FloatEqOp
159 ltH_Float_RDR   = primOpRdrName FloatLtOp
160 eqH_Double_RDR  = primOpRdrName DoubleEqOp
161 ltH_Double_RDR  = primOpRdrName DoubleLtOp
162 eqH_Int_RDR     = primOpRdrName IntEqOp
163 ltH_Int_RDR     = primOpRdrName IntLtOp
164 geH_RDR         = primOpRdrName IntGeOp
165 leH_RDR         = primOpRdrName IntLeOp
166 minusH_RDR      = primOpRdrName IntSubOp
167
168 tagToEnumH_RDR  = primOpRdrName TagToEnumOp
169 \end{code}
170
171
172 %************************************************************************
173 %*                                                                      *
174 \subsection{Built-in keys}
175 %*                                                                      *
176 %************************************************************************
177
178 ToDo: make it do the ``like'' part properly (as in 0.26 and before).
179
180 \begin{code}
181 maybeCharLikeCon, maybeIntLikeCon :: DataCon -> Bool
182 maybeCharLikeCon con = con `hasKey` charDataConKey
183 maybeIntLikeCon  con = con `hasKey` intDataConKey
184 \end{code}
185
186
187 %************************************************************************
188 %*                                                                      *
189 \subsection{Class predicates}
190 %*                                                                      *
191 %************************************************************************
192
193 \begin{code}
194 isCcallishClass, isCreturnableClass, isNoDictClass, 
195   isNumericClass, isStandardClass :: Class -> Bool
196
197 isNumericClass     clas = classKey clas `is_elem` numericClassKeys
198 isStandardClass    clas = classKey clas `is_elem` standardClassKeys
199 isCcallishClass    clas = classKey clas `is_elem` cCallishClassKeys
200 isCreturnableClass clas = classKey clas == cReturnableClassKey
201 isNoDictClass      clas = classKey clas `is_elem` noDictClassKeys
202 is_elem = isIn "is_X_Class"
203 \end{code}