Refactor named wildcards (again)
[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 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 Constants ( mAX_TUPLE_SIZE )
27 import BasicTypes ( Boxity(..) )
28 import ConLike ( ConLike(..) )
29 import PrelNames
30 import PrelRules
31 import Avail
32 import PrimOp
33 import DataCon
34 import Id
35 import Name
36 import MkId
37 import NameEnv
38 import TysPrim
39 import TysWiredIn
40 import HscTypes
41 import UniqFM
42 import Class
43 import TyCon
44 import Util
45 import Panic ( panic )
46 import {-# SOURCE #-} TcTypeNats ( typeNatTyCons )
47
48 import Data.List ( intercalate )
49 import Data.Array
50
51 {-
52 ************************************************************************
53 * *
54 \subsection[builtinNameInfo]{Lookup built-in names}
55 * *
56 ************************************************************************
57
58 Notes about wired in things
59 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
60 * Wired-in things are Ids\/TyCons that are completely known to the compiler.
61 They are global values in GHC, (e.g. listTyCon :: TyCon).
62
63 * A wired in Name contains the thing itself inside the Name:
64 see Name.wiredInNameTyThing_maybe
65 (E.g. listTyConName contains listTyCon.
66
67 * The name cache is initialised with (the names of) all wired-in things
68
69 * The type environment itself contains no wired in things. The type
70 checker sees if the Name is wired in before looking up the name in
71 the type environment.
72
73 * MkIface prunes out wired-in things before putting them in an interface file.
74 So interface files never contain wired-in things.
75 -}
76
77
78 knownKeyNames :: [Name]
79 -- This list is used to ensure that when you say "Prelude.map"
80 -- in your source code, or in an interface file,
81 -- you get a Name with the correct known key
82 -- (See Note [Known-key names] in PrelNames)
83 knownKeyNames
84 | debugIsOn
85 , not (isNullUFM badNamesEnv)
86 = panic ("badKnownKeyNames:\n" ++ badNamesStr)
87 -- NB: We can't use ppr here, because this is sometimes evaluated in a
88 -- context where there are no DynFlags available, leading to a cryptic
89 -- "<<details unavailable>>" error. (This seems to happen only in the
90 -- stage 2 compiler, for reasons I [Richard] have no clue of.)
91
92 | otherwise
93 = names
94 where
95 names =
96 concat [ tycon_kk_names funTyCon
97 , concatMap tycon_kk_names primTyCons
98
99 , concatMap tycon_kk_names wiredInTyCons
100 -- Does not include tuples
101
102 , concatMap tycon_kk_names typeNatTyCons
103
104 , concatMap (rep_names . tupleTyCon Boxed) [2..mAX_TUPLE_SIZE] -- Yuk
105
106 , cTupleTyConNames
107 -- Constraint tuples are known-key but not wired-in
108 -- They can't show up in source code, but can appear
109 -- in intreface files
110
111 , map idName wiredInIds
112 , map (idName . primOpId) allThePrimOps
113 , basicKnownKeyNames ]
114
115 -- "kk" short for "known-key"
116 tycon_kk_names :: TyCon -> [Name]
117 tycon_kk_names tc = tyConName tc : (rep_names tc ++ concatMap thing_kk_names (implicitTyConThings tc))
118
119 datacon_kk_names dc
120 = dataConName dc : rep_names (promoteDataCon dc)
121
122 thing_kk_names :: TyThing -> [Name]
123 thing_kk_names (ATyCon tc) = tycon_kk_names tc
124 thing_kk_names (AConLike (RealDataCon dc)) = datacon_kk_names dc
125 thing_kk_names thing = [getName thing]
126
127 -- The TyConRepName for a known-key TyCon has a known key,
128 -- but isn't itself an implicit thing. Yurgh.
129 -- NB: if any of the wired-in TyCons had record fields, the record
130 -- field names would be in a similar situation. Ditto class ops.
131 -- But it happens that there aren't any
132 rep_names tc = case tyConRepName_maybe tc of
133 Just n -> [n]
134 Nothing -> []
135
136 namesEnv = foldl (\m n -> extendNameEnv_Acc (:) singleton m n n)
137 emptyUFM names
138 badNamesEnv = filterNameEnv (\ns -> length ns > 1) namesEnv
139 badNamesPairs = nameEnvUniqueElts badNamesEnv
140 badNamesStrs = map pairToStr badNamesPairs
141 badNamesStr = unlines badNamesStrs
142
143 pairToStr (uniq, ns) = " " ++
144 show uniq ++
145 ": [" ++
146 intercalate ", " (map (occNameString . nameOccName) ns) ++
147 "]"
148
149 {-
150 We let a lot of "non-standard" values be visible, so that we can make
151 sense of them in interface pragmas. It's cool, though they all have
152 "non-standard" names, so they won't get past the parser in user code.
153
154 ************************************************************************
155 * *
156 PrimOpIds
157 * *
158 ************************************************************************
159 -}
160
161 primOpIds :: Array Int Id
162 -- A cache of the PrimOp Ids, indexed by PrimOp tag
163 primOpIds = array (1,maxPrimOpTag) [ (primOpTag op, mkPrimOpId op)
164 | op <- allThePrimOps ]
165
166 primOpId :: PrimOp -> Id
167 primOpId op = primOpIds ! primOpTag op
168
169 {-
170 ************************************************************************
171 * *
172 \subsection{Export lists for pseudo-modules (GHC.Prim)}
173 * *
174 ************************************************************************
175
176 GHC.Prim "exports" all the primops and primitive types, some
177 wired-in Ids.
178 -}
179
180 ghcPrimExports :: [IfaceExport]
181 ghcPrimExports
182 = map (avail . idName) ghcPrimIds ++
183 map (avail . idName . primOpId) allThePrimOps ++
184 [ AvailTC n [n] []
185 | tc <- funTyCon : primTyCons, let n = tyConName tc ]
186
187 {-
188 ************************************************************************
189 * *
190 \subsection{Built-in keys}
191 * *
192 ************************************************************************
193
194 ToDo: make it do the ``like'' part properly (as in 0.26 and before).
195 -}
196
197 maybeCharLikeCon, maybeIntLikeCon :: DataCon -> Bool
198 maybeCharLikeCon con = con `hasKey` charDataConKey
199 maybeIntLikeCon con = con `hasKey` intDataConKey
200
201 {-
202 ************************************************************************
203 * *
204 \subsection{Class predicates}
205 * *
206 ************************************************************************
207 -}
208
209 isNumericClass, isStandardClass :: Class -> Bool
210
211 isNumericClass clas = classKey clas `is_elem` numericClassKeys
212 isStandardClass clas = classKey clas `is_elem` standardClassKeys
213
214 is_elem :: Eq a => a -> [a] -> Bool
215 is_elem = isIn "is_X_Class"