Generate Typeable info at definition sites
[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 TysPrim
38 import TysWiredIn
39 import HscTypes
40 import Class
41 import TyCon
42 import Util
43 import {-# SOURCE #-} TcTypeNats ( typeNatTyCons )
44
45 import Data.Array
46
47 {-
48 ************************************************************************
49 * *
50 \subsection[builtinNameInfo]{Lookup built-in names}
51 * *
52 ************************************************************************
53
54 Notes about wired in things
55 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
56 * Wired-in things are Ids\/TyCons that are completely known to the compiler.
57 They are global values in GHC, (e.g. listTyCon :: TyCon).
58
59 * A wired in Name contains the thing itself inside the Name:
60 see Name.wiredInNameTyThing_maybe
61 (E.g. listTyConName contains listTyCon.
62
63 * The name cache is initialised with (the names of) all wired-in things
64
65 * The type environment itself contains no wired in things. The type
66 checker sees if the Name is wired in before looking up the name in
67 the type environment.
68
69 * MkIface prunes out wired-in things before putting them in an interface file.
70 So interface files never contain wired-in things.
71 -}
72
73
74 knownKeyNames :: [Name]
75 -- This list is used to ensure that when you say "Prelude.map"
76 -- in your source code, or in an interface file,
77 -- you get a Name with the correct known key
78 -- (See Note [Known-key names] in PrelNames)
79 knownKeyNames
80 = concat [ tycon_kk_names funTyCon
81 , concatMap tycon_kk_names primTyCons
82
83 , concatMap tycon_kk_names wiredInTyCons
84 -- Does not include tuples
85
86 , concatMap tycon_kk_names typeNatTyCons
87
88 , concatMap (rep_names . tupleTyCon Boxed) [2..mAX_TUPLE_SIZE] -- Yuk
89
90 , cTupleTyConNames
91 -- Constraint tuples are known-key but not wired-in
92 -- They can't show up in source code, but can appear
93 -- in intreface files
94
95 , map idName wiredInIds
96 , map (idName . primOpId) allThePrimOps
97 , basicKnownKeyNames ]
98 where
99 -- "kk" short for "known-key"
100 tycon_kk_names :: TyCon -> [Name]
101 tycon_kk_names tc = tyConName tc : (rep_names tc ++ concatMap thing_kk_names (implicitTyConThings tc))
102
103 datacon_kk_names dc
104 | Promoted tc <- promoteDataCon_maybe dc = dataConName dc : rep_names tc
105 | otherwise = [dataConName dc]
106
107 thing_kk_names :: TyThing -> [Name]
108 thing_kk_names (ATyCon tc) = tycon_kk_names tc
109 thing_kk_names (AConLike (RealDataCon dc)) = datacon_kk_names dc
110 thing_kk_names thing = [getName thing]
111
112 -- The TyConRepName for a known-key TyCon has a known key,
113 -- but isn't itself an implicit thing. Yurgh.
114 -- NB: if any of the wired-in TyCons had record fields, the record
115 -- field names would be in a similar situation. Ditto class ops.
116 -- But it happens that there aren't any
117 rep_names tc = case tyConRepName_maybe tc of
118 Just n -> [n]
119 Nothing -> []
120
121 {-
122 We let a lot of "non-standard" values be visible, so that we can make
123 sense of them in interface pragmas. It's cool, though they all have
124 "non-standard" names, so they won't get past the parser in user code.
125
126 ************************************************************************
127 * *
128 PrimOpIds
129 * *
130 ************************************************************************
131 -}
132
133 primOpIds :: Array Int Id
134 -- A cache of the PrimOp Ids, indexed by PrimOp tag
135 primOpIds = array (1,maxPrimOpTag) [ (primOpTag op, mkPrimOpId op)
136 | op <- allThePrimOps ]
137
138 primOpId :: PrimOp -> Id
139 primOpId op = primOpIds ! primOpTag op
140
141 {-
142 ************************************************************************
143 * *
144 \subsection{Export lists for pseudo-modules (GHC.Prim)}
145 * *
146 ************************************************************************
147
148 GHC.Prim "exports" all the primops and primitive types, some
149 wired-in Ids.
150 -}
151
152 ghcPrimExports :: [IfaceExport]
153 ghcPrimExports
154 = map (Avail . idName) ghcPrimIds ++
155 map (Avail . idName . primOpId) allThePrimOps ++
156 [ AvailTC n [n] []
157 | tc <- funTyCon : primTyCons, let n = tyConName tc ]
158
159 {-
160 ************************************************************************
161 * *
162 \subsection{Built-in keys}
163 * *
164 ************************************************************************
165
166 ToDo: make it do the ``like'' part properly (as in 0.26 and before).
167 -}
168
169 maybeCharLikeCon, maybeIntLikeCon :: DataCon -> Bool
170 maybeCharLikeCon con = con `hasKey` charDataConKey
171 maybeIntLikeCon con = con `hasKey` intDataConKey
172
173 {-
174 ************************************************************************
175 * *
176 \subsection{Class predicates}
177 * *
178 ************************************************************************
179 -}
180
181 isNumericClass, isStandardClass :: Class -> Bool
182
183 isNumericClass clas = classKey clas `is_elem` numericClassKeys
184 isStandardClass clas = classKey clas `is_elem` standardClassKeys
185
186 is_elem :: Eq a => a -> [a] -> Bool
187 is_elem = isIn "is_X_Class"