f79b6b1e7ff7150129bfa0889aed6479a505737e
[ghc.git] / compiler / prelude / PrelInfo.hs
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
7 {-# LANGUAGE CPP #-}
8 module PrelInfo (
9 wiredInIds, ghcPrimIds,
10 primOpRules, builtinRules,
11
12 ghcPrimExports,
13 wiredInThings, knownKeyNames,
14 primOpId,
15
16 -- Random other things
17 maybeCharLikeCon, maybeIntLikeCon,
18
19 -- Class categories
20 isNumericClass, isStandardClass
21
22 ) where
23
24 #include "HsVersions.h"
25
26 import PrelNames
27 import PrelRules
28 import Avail
29 import PrimOp
30 import DataCon
31 import Id
32 import MkId
33 import Name( Name, getName )
34 import TysPrim
35 import TysWiredIn
36 import HscTypes
37 import Class
38 import TyCon
39 import Outputable
40 import UniqFM
41 import Util
42 import {-# SOURCE #-} TcTypeNats ( typeNatTyCons )
43
44 #ifdef GHCI
45 import THNames
46 #endif
47
48 import Data.Array
49
50
51 {- *********************************************************************
52 * *
53 Known key things
54 * *
55 ********************************************************************* -}
56
57 knownKeyNames :: [Name]
58 knownKeyNames =
59 ASSERT2( isNullUFM badNamesUFM, text "badknownKeyNames" <+> ppr badNamesUFM )
60 names
61 where
62 badNamesUFM = filterUFM (\ns -> length ns > 1) namesUFM
63 namesUFM = foldl (\m n -> addToUFM_Acc (:) singleton m n n) emptyUFM names
64 names = concat
65 [ map getName wiredInThings
66 , cTupleTyConNames
67 , basicKnownKeyNames
68 #ifdef GHCI
69 , templateHaskellNames
70 #endif
71 ]
72
73 {- *********************************************************************
74 * *
75 Wired in things
76 * *
77 ************************************************************************
78
79 Notes about wired in things
80 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
81 * Wired-in things are Ids\/TyCons that are completely known to the compiler.
82 They are global values in GHC, (e.g. listTyCon :: TyCon).
83
84 * A wired in Name contains the thing itself inside the Name:
85 see Name.wiredInNameTyThing_maybe
86 (E.g. listTyConName contains listTyCon.
87
88 * The name cache is initialised with (the names of) all wired-in things
89
90 * The type checker sees if the Name is wired in before looking up
91 the name in the type environment. So the type envt itself contains
92 no wired in things.
93
94 * MkIface prunes out wired-in things before putting them in an interface file.
95 So interface files never contain wired-in things.
96 -}
97
98 wiredInThings :: [TyThing]
99 -- This list is used only to initialise HscMain.knownKeyNames
100 -- to ensure that when you say "Prelude.map" in your source code, you
101 -- get a Name with the correct known key (See Note [Known-key names])
102 wiredInThings
103 = concat
104 [ -- Wired in TyCons and their implicit Ids
105 tycon_things
106 , concatMap implicitTyThings tycon_things
107
108 -- Wired in Ids
109 , map AnId wiredInIds
110
111 -- PrimOps
112 , map (AnId . primOpId) allThePrimOps
113 ]
114 where
115 tycon_things = map ATyCon ([funTyCon] ++ primTyCons ++ wiredInTyCons
116 ++ typeNatTyCons)
117
118 {-
119 We let a lot of "non-standard" values be visible, so that we can make
120 sense of them in interface pragmas. It's cool, though they all have
121 "non-standard" names, so they won't get past the parser in user code.
122
123 ************************************************************************
124 * *
125 PrimOpIds
126 * *
127 ************************************************************************
128 -}
129
130 primOpIds :: Array Int Id
131 -- A cache of the PrimOp Ids, indexed by PrimOp tag
132 primOpIds = array (1,maxPrimOpTag) [ (primOpTag op, mkPrimOpId op)
133 | op <- allThePrimOps ]
134
135 primOpId :: PrimOp -> Id
136 primOpId op = primOpIds ! primOpTag op
137
138 {-
139 ************************************************************************
140 * *
141 \subsection{Export lists for pseudo-modules (GHC.Prim)}
142 * *
143 ************************************************************************
144
145 GHC.Prim "exports" all the primops and primitive types, some
146 wired-in Ids.
147 -}
148
149 ghcPrimExports :: [IfaceExport]
150 ghcPrimExports
151 = map (Avail . idName) ghcPrimIds ++
152 map (Avail . idName . primOpId) allThePrimOps ++
153 [ AvailTC n [n] []
154 | tc <- funTyCon : primTyCons, let n = tyConName tc ]
155
156 {-
157 ************************************************************************
158 * *
159 \subsection{Built-in keys}
160 * *
161 ************************************************************************
162
163 ToDo: make it do the ``like'' part properly (as in 0.26 and before).
164 -}
165
166 maybeCharLikeCon, maybeIntLikeCon :: DataCon -> Bool
167 maybeCharLikeCon con = con `hasKey` charDataConKey
168 maybeIntLikeCon con = con `hasKey` intDataConKey
169
170 {-
171 ************************************************************************
172 * *
173 \subsection{Class predicates}
174 * *
175 ************************************************************************
176 -}
177
178 isNumericClass, isStandardClass :: Class -> Bool
179
180 isNumericClass clas = classKey clas `is_elem` numericClassKeys
181 isStandardClass clas = classKey clas `is_elem` standardClassKeys
182
183 is_elem :: Eq a => a -> [a] -> Bool
184 is_elem = isIn "is_X_Class"