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