[project @ 2002-09-13 15:02:25 by simonpj]
[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         -- Random other things
17         maybeCharLikeCon, maybeIntLikeCon,
18
19         -- Class categories
20         isCcallishClass, isCreturnableClass, isNoDictClass, 
21         isNumericClass, isStandardClass
22
23     ) where
24
25 #include "HsVersions.h"
26
27 import PrelNames        -- Prelude module names
28
29 import PrimOp           ( allThePrimOps, primOpOcc )
30 import DataCon          ( DataCon )
31 import Id               ( idName )
32 import MkId             ( mkPrimOpId, wiredInIds )
33 import MkId             -- All of it, for re-export
34 import Name             ( nameOccName )
35 import RdrName          ( mkRdrUnqual, getRdrName )
36 import HsSyn            ( HsTyVarBndr(..), TyClDecl(..), HsType(..) )
37 import OccName          ( mkVarOcc )
38 import TysPrim          ( primTyCons )
39 import TysWiredIn       ( wiredInTyCons )
40 import RdrHsSyn         ( mkClassDecl )
41 import HscTypes         ( TyThing(..), implicitTyThingIds, TypeEnv, mkTypeEnv,
42                           GenAvailInfo(..), RdrAvailInfo )
43 import Class            ( Class, classKey )
44 import Type             ( funTyCon, openTypeKind, liftedTypeKind )
45 import TyCon            ( tyConName )
46 import SrcLoc           ( noSrcLoc )
47 import Util             ( isIn )
48 \end{code}
49
50 %************************************************************************
51 %*                                                                      *
52 \subsection[builtinNameInfo]{Lookup built-in names}
53 %*                                                                      *
54 %************************************************************************
55
56 We have two ``builtin name funs,'' one to look up @TyCons@ and
57 @Classes@, the other to look up values.
58
59 \begin{code}
60 wiredInThings :: [TyThing]
61 wiredInThings
62   = concat
63     [           -- Wired in TyCons and their implicit Ids
64           tycon_things
65         , map AnId (implicitTyThingIds tycon_things)
66
67                 -- Wired in Ids
68         , map AnId wiredInIds
69
70                 -- PrimOps
71         , map (AnId . mkPrimOpId) allThePrimOps
72     ]
73   where
74     tycon_things = map ATyCon ([funTyCon] ++ primTyCons ++ wiredInTyCons)
75
76 wiredInThingEnv :: TypeEnv
77 wiredInThingEnv = mkTypeEnv wiredInThings
78 \end{code}
79
80 We let a lot of "non-standard" values be visible, so that we can make
81 sense of them in interface pragmas. It's cool, though they all have
82 "non-standard" names, so they won't get past the parser in user code.
83
84 %************************************************************************
85 %*                                                                      *
86 \subsection{Export lists for pseudo-modules (GHC.Prim)}
87 %*                                                                      *
88 %************************************************************************
89
90 GHC.Prim "exports" all the primops and primitive types, some 
91 wired-in Ids, and the CCallable & CReturnable classes.
92
93 \begin{code}
94 ghcPrimExports :: [RdrAvailInfo]
95  = AvailTC cCallableOcc [ cCallableOcc ] :
96    AvailTC cReturnableOcc [ cReturnableOcc ] :
97    map (Avail . nameOccName . idName) ghcPrimIds ++
98    map (Avail . primOpOcc) allThePrimOps ++
99    [ AvailTC occ [occ] |
100      n <- funTyCon : primTyCons, let occ = nameOccName (tyConName n) 
101    ]
102  where
103    cCallableOcc = nameOccName cCallableClassName
104    cReturnableOcc = nameOccName cReturnableClassName
105
106 assertDecl
107   = IfaceSig { 
108         tcdName = getRdrName assertName,
109         tcdType = HsForAllTy (Just [liftedAlpha]) [] (HsTyVar alpha),
110         tcdIdInfo = [],
111         tcdLoc = noSrcLoc
112     }
113
114 cCallableClassDecl
115   = mkClassDecl
116     ([], getRdrName cCallableClassName, [openAlpha])
117     [] -- no fds
118     [] -- no sigs
119     Nothing -- no mbinds
120     noSrcLoc
121
122 cReturnableClassDecl
123   = mkClassDecl
124     ([], getRdrName cReturnableClassName, [openAlpha])
125     [] -- no fds
126     [] -- no sigs
127     Nothing -- no mbinds
128     noSrcLoc
129
130 alpha = mkRdrUnqual (mkVarOcc FSLIT("a"))
131 openAlpha = IfaceTyVar alpha openTypeKind
132 liftedAlpha = IfaceTyVar alpha liftedTypeKind
133 \end{code}
134
135
136 %************************************************************************
137 %*                                                                      *
138 \subsection{Built-in keys}
139 %*                                                                      *
140 %************************************************************************
141
142 ToDo: make it do the ``like'' part properly (as in 0.26 and before).
143
144 \begin{code}
145 maybeCharLikeCon, maybeIntLikeCon :: DataCon -> Bool
146 maybeCharLikeCon con = con `hasKey` charDataConKey
147 maybeIntLikeCon  con = con `hasKey` intDataConKey
148 \end{code}
149
150
151 %************************************************************************
152 %*                                                                      *
153 \subsection{Class predicates}
154 %*                                                                      *
155 %************************************************************************
156
157 \begin{code}
158 isCcallishClass, isCreturnableClass, isNoDictClass, 
159   isNumericClass, isStandardClass :: Class -> Bool
160
161 isNumericClass     clas = classKey clas `is_elem` numericClassKeys
162 isStandardClass    clas = classKey clas `is_elem` standardClassKeys
163 isCcallishClass    clas = classKey clas `is_elem` cCallishClassKeys
164 isCreturnableClass clas = classKey clas == cReturnableClassKey
165 isNoDictClass      clas = classKey clas `is_elem` noDictClassKeys
166 is_elem = isIn "is_X_Class"
167 \end{code}