471b61ee09b8aaf67046968fee75c2a259dcad63
[ghc.git] / compiler / prelude / PrelInfo.hs
1 {-
2 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3
4 -}
5
6 {-# LANGUAGE CPP #-}
7
8 -- | The @PrelInfo@ interface to the compiler's prelude knowledge.
9 --
10 -- This module serves as the central gathering point for names which the
11 -- compiler knows something about. This includes functions for,
12 --
13 -- * discerning whether a 'Name' is known-key
14 --
15 -- * given a 'Unique', looking up its corresponding known-key 'Name'
16 --
17 -- See Note [Known-key names] and Note [About wired-in things] for information
18 -- about the two types of prelude things in GHC.
19 --
20 module PrelInfo (
21 -- * Known-key names
22 isKnownKeyName,
23 lookupKnownKeyName,
24
25 -- ** Internal use
26 -- | 'knownKeyNames' is exported to seed the original name cache only;
27 -- if you find yourself wanting to look at it you might consider using
28 -- 'lookupKnownKeyName' or 'isKnownKeyName'.
29 knownKeyNames,
30
31 -- * Miscellaneous
32 wiredInIds, ghcPrimIds,
33 primOpRules, builtinRules,
34
35 ghcPrimExports,
36 primOpId,
37
38 -- * Random other things
39 maybeCharLikeCon, maybeIntLikeCon,
40
41 -- * Class categories
42 isNumericClass, isStandardClass
43
44 ) where
45
46 #include "HsVersions.h"
47
48 import KnownUniques
49 import Unique ( isValidKnownKeyUnique )
50
51 import ConLike ( ConLike(..) )
52 import THNames ( templateHaskellNames )
53 import PrelNames
54 import PrelRules
55 import Avail
56 import PrimOp
57 import DataCon
58 import Id
59 import Name
60 import NameEnv
61 import MkId
62 import TysPrim
63 import TysWiredIn
64 import HscTypes
65 import Class
66 import TyCon
67 import UniqFM
68 import Util
69 import Panic
70 import {-# SOURCE #-} TcTypeNats ( typeNatTyCons )
71
72 import Control.Applicative ((<|>))
73 import Data.List ( intercalate )
74 import Data.Array
75 import Data.Maybe
76
77 {-
78 ************************************************************************
79 * *
80 \subsection[builtinNameInfo]{Lookup built-in names}
81 * *
82 ************************************************************************
83
84 Note [About wired-in things]
85 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
86 * Wired-in things are Ids\/TyCons that are completely known to the compiler.
87 They are global values in GHC, (e.g. listTyCon :: TyCon).
88
89 * A wired in Name contains the thing itself inside the Name:
90 see Name.wiredInNameTyThing_maybe
91 (E.g. listTyConName contains listTyCon.
92
93 * The name cache is initialised with (the names of) all wired-in things
94 (except tuples and sums; see Note [Known-])
95
96 * The type environment itself contains no wired in things. The type
97 checker sees if the Name is wired in before looking up the name in
98 the type environment.
99
100 * MkIface prunes out wired-in things before putting them in an interface file.
101 So interface files never contain wired-in things.
102 -}
103
104
105 -- | This list is used to ensure that when you say "Prelude.map" in your source
106 -- code, or in an interface file, you get a Name with the correct known key (See
107 -- Note [Known-key names] in PrelNames)
108 knownKeyNames :: [Name]
109 knownKeyNames
110 | debugIsOn
111 , Just badNamesStr <- knownKeyNamesOkay all_names
112 = panic ("badAllKnownKeyNames:\n" ++ badNamesStr)
113 -- NB: We can't use ppr here, because this is sometimes evaluated in a
114 -- context where there are no DynFlags available, leading to a cryptic
115 -- "<<details unavailable>>" error. (This seems to happen only in the
116 -- stage 2 compiler, for reasons I [Richard] have no clue of.)
117 | otherwise
118 = all_names
119 where
120 all_names =
121 concat [ wired_tycon_kk_names funTyCon
122 , concatMap wired_tycon_kk_names primTyCons
123
124 , concatMap wired_tycon_kk_names wiredInTyCons
125 -- Does not include tuples
126
127 , concatMap wired_tycon_kk_names typeNatTyCons
128
129 , map idName wiredInIds
130 , map (idName . primOpId) allThePrimOps
131 , basicKnownKeyNames
132 , templateHaskellNames
133 ]
134 -- All of the names associated with a wired-in TyCon.
135 -- This includes the TyCon itself, its DataCons and promoted TyCons.
136 wired_tycon_kk_names :: TyCon -> [Name]
137 wired_tycon_kk_names tc =
138 tyConName tc : (rep_names tc ++ implicits)
139 where implicits = concatMap thing_kk_names (implicitTyConThings tc)
140
141 wired_datacon_kk_names :: DataCon -> [Name]
142 wired_datacon_kk_names dc =
143 dataConName dc : rep_names (promoteDataCon dc)
144
145 thing_kk_names :: TyThing -> [Name]
146 thing_kk_names (ATyCon tc) = wired_tycon_kk_names tc
147 thing_kk_names (AConLike (RealDataCon dc)) = wired_datacon_kk_names dc
148 thing_kk_names thing = [getName thing]
149
150 -- The TyConRepName for a known-key TyCon has a known key,
151 -- but isn't itself an implicit thing. Yurgh.
152 -- NB: if any of the wired-in TyCons had record fields, the record
153 -- field names would be in a similar situation. Ditto class ops.
154 -- But it happens that there aren't any
155 rep_names tc = case tyConRepName_maybe tc of
156 Just n -> [n]
157 Nothing -> []
158
159 -- | Check the known-key names list of consistency.
160 knownKeyNamesOkay :: [Name] -> Maybe String
161 knownKeyNamesOkay all_names
162 | ns@(_:_) <- filter (not . isValidKnownKeyUnique . getUnique) all_names
163 = Just $ " Out-of-range known-key uniques: ["
164 ++ intercalate ", " (map (occNameString . nameOccName) ns) ++
165 "]"
166 | null badNamesPairs
167 = Nothing
168 | otherwise
169 = Just badNamesStr
170 where
171 namesEnv = foldl (\m n -> extendNameEnv_Acc (:) singleton m n n)
172 emptyUFM all_names
173 badNamesEnv = filterNameEnv (\ns -> length ns > 1) namesEnv
174 badNamesPairs = nonDetUFMToList badNamesEnv
175 -- It's OK to use nonDetUFMToList here because the ordering only affects
176 -- the message when we get a panic
177 badNamesStrs = map pairToStr badNamesPairs
178 badNamesStr = unlines badNamesStrs
179
180 pairToStr (uniq, ns) = " " ++
181 show uniq ++
182 ": [" ++
183 intercalate ", " (map (occNameString . nameOccName) ns) ++
184 "]"
185
186 -- | Given a 'Unique' lookup its associated 'Name' if it corresponds to a
187 -- known-key thing.
188 lookupKnownKeyName :: Unique -> Maybe Name
189 lookupKnownKeyName u =
190 knownUniqueName u <|> lookupUFM knownKeysMap u
191
192 -- | Is a 'Name' known-key?
193 isKnownKeyName :: Name -> Bool
194 isKnownKeyName n =
195 isJust (knownUniqueName $ nameUnique n) || elemUFM n knownKeysMap
196
197 knownKeysMap :: UniqFM Name
198 knownKeysMap = listToUFM [ (nameUnique n, n) | n <- knownKeyNames ]
199
200 {-
201 We let a lot of "non-standard" values be visible, so that we can make
202 sense of them in interface pragmas. It's cool, though they all have
203 "non-standard" names, so they won't get past the parser in user code.
204
205 ************************************************************************
206 * *
207 PrimOpIds
208 * *
209 ************************************************************************
210 -}
211
212 primOpIds :: Array Int Id
213 -- A cache of the PrimOp Ids, indexed by PrimOp tag
214 primOpIds = array (1,maxPrimOpTag) [ (primOpTag op, mkPrimOpId op)
215 | op <- allThePrimOps ]
216
217 primOpId :: PrimOp -> Id
218 primOpId op = primOpIds ! primOpTag op
219
220 {-
221 ************************************************************************
222 * *
223 Export lists for pseudo-modules (GHC.Prim)
224 * *
225 ************************************************************************
226
227 GHC.Prim "exports" all the primops and primitive types, some
228 wired-in Ids.
229 -}
230
231 ghcPrimExports :: [IfaceExport]
232 ghcPrimExports
233 = map (avail . idName) ghcPrimIds ++
234 map (avail . idName . primOpId) allThePrimOps ++
235 [ AvailTC n [n] []
236 | tc <- funTyCon : primTyCons, let n = tyConName tc ]
237
238 {-
239 ************************************************************************
240 * *
241 Built-in keys
242 * *
243 ************************************************************************
244
245 ToDo: make it do the ``like'' part properly (as in 0.26 and before).
246 -}
247
248 maybeCharLikeCon, maybeIntLikeCon :: DataCon -> Bool
249 maybeCharLikeCon con = con `hasKey` charDataConKey
250 maybeIntLikeCon con = con `hasKey` intDataConKey
251
252 {-
253 ************************************************************************
254 * *
255 Class predicates
256 * *
257 ************************************************************************
258 -}
259
260 isNumericClass, isStandardClass :: Class -> Bool
261
262 isNumericClass clas = classKey clas `is_elem` numericClassKeys
263 isStandardClass clas = classKey clas `is_elem` standardClassKeys
264
265 is_elem :: Eq a => a -> [a] -> Bool
266 is_elem = isIn "is_X_Class"