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