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