3fa99f11876b7a596aafb21c81c3d60ac451a0cc
[ghc-hetmet.git] / compiler / prelude / PrelInfo.lhs
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 \begin{code}
7 {-# OPTIONS -w #-}
8 -- The above warning supression flag is a temporary kludge.
9 -- While working on this module you are encouraged to remove it and fix
10 -- any warnings in the module. See
11 --     http://hackage.haskell.org/trac/ghc/wiki/CodingStyle#Warnings
12 -- for details
13
14 module PrelInfo (
15         module MkId,
16
17         ghcPrimExports,
18         wiredInThings, basicKnownKeyNames,
19         primOpId,
20         
21         -- Random other things
22         maybeCharLikeCon, maybeIntLikeCon,
23
24         -- Class categories
25         isNumericClass, isStandardClass
26
27     ) where
28
29 #include "HsVersions.h"
30
31 import PrelNames        ( basicKnownKeyNames, 
32                           hasKey, charDataConKey, intDataConKey,
33                           numericClassKeys, standardClassKeys )
34
35 import PrimOp           ( PrimOp, allThePrimOps, primOpOcc, primOpTag, maxPrimOpTag )
36 import DataCon          ( DataCon )
37 import Id               ( Id, idName )
38 import MkId             -- All of it, for re-export
39 import Name             ( nameOccName )
40 import TysPrim          ( primTyCons )
41 import TysWiredIn       ( wiredInTyCons )
42 import HscTypes         ( TyThing(..), implicitTyThings, GenAvailInfo(..), RdrAvailInfo )
43 import Class            ( Class, classKey )
44 import Type             ( funTyCon )
45 import TyCon            ( tyConName )
46 import Util             ( isIn )
47
48 import Array            ( Array, array, (!) )
49 \end{code}
50
51 %************************************************************************
52 %*                                                                      *
53 \subsection[builtinNameInfo]{Lookup built-in names}
54 %*                                                                      *
55 %************************************************************************
56
57 Notes about wired in things
58 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
59 * Wired-in things are Ids/TyCons that are completely known to the compiler.
60   They are global values in GHC, (e.g.  listTyCon :: TyCon).
61
62 * A wired in Name contains the thing itself inside the Name: 
63         see Name.wiredInNameTyThing_maybe
64   (E.g. listTyConName contains listTyCon. 
65
66 * The name cache is initialised with (the names of) all wired-in things
67
68 * The type checker sees if the Name is wired in before looking up 
69   the name in the type environment.  So the type envt itself contains
70   no wired in things.
71
72 * MkIface prunes out wired-in things before putting them in an interface file.
73   So interface files never contain wired-in things.
74
75
76 \begin{code}
77 wiredInThings :: [TyThing]
78 -- This list is used only to initialise HscMain.knownKeyNames
79 -- to ensure that when you say "Prelude.map" in your source code, you
80 -- get a Name with the correct known key
81 wiredInThings           
82   = concat
83     [           -- Wired in TyCons and their implicit Ids
84           tycon_things
85         , concatMap implicitTyThings tycon_things
86
87                 -- Wired in Ids
88         , map AnId wiredInIds
89
90                 -- PrimOps
91         , map (AnId . mkPrimOpId) allThePrimOps
92     ]
93   where
94     tycon_things = map ATyCon ([funTyCon] ++ primTyCons ++ wiredInTyCons)
95 \end{code}
96
97 We let a lot of "non-standard" values be visible, so that we can make
98 sense of them in interface pragmas. It's cool, though they all have
99 "non-standard" names, so they won't get past the parser in user code.
100
101 %************************************************************************
102 %*                                                                      *
103                 PrimOpIds
104 %*                                                                      *
105 %************************************************************************
106
107 \begin{code}
108 primOpIds :: Array Int Id       -- Indexed by PrimOp tag
109 primOpIds = array (1,maxPrimOpTag) [ (primOpTag op, mkPrimOpId op) 
110                                    | op <- allThePrimOps]
111
112 primOpId :: PrimOp -> Id
113 primOpId op = primOpIds ! primOpTag op
114 \end{code}
115
116
117 %************************************************************************
118 %*                                                                      *
119 \subsection{Export lists for pseudo-modules (GHC.Prim)}
120 %*                                                                      *
121 %************************************************************************
122
123 GHC.Prim "exports" all the primops and primitive types, some 
124 wired-in Ids.
125
126 \begin{code}
127 ghcPrimExports :: [RdrAvailInfo]
128 ghcPrimExports
129  = map (Avail . nameOccName . idName) ghcPrimIds ++
130    map (Avail . primOpOcc) allThePrimOps ++
131    [ AvailTC occ [occ] |
132      n <- funTyCon : primTyCons, let occ = nameOccName (tyConName n) 
133    ]
134 \end{code}
135
136
137 %************************************************************************
138 %*                                                                      *
139 \subsection{Built-in keys}
140 %*                                                                      *
141 %************************************************************************
142
143 ToDo: make it do the ``like'' part properly (as in 0.26 and before).
144
145 \begin{code}
146 maybeCharLikeCon, maybeIntLikeCon :: DataCon -> Bool
147 maybeCharLikeCon con = con `hasKey` charDataConKey
148 maybeIntLikeCon  con = con `hasKey` intDataConKey
149 \end{code}
150
151
152 %************************************************************************
153 %*                                                                      *
154 \subsection{Class predicates}
155 %*                                                                      *
156 %************************************************************************
157
158 \begin{code}
159 isNumericClass, isStandardClass :: Class -> Bool
160
161 isNumericClass     clas = classKey clas `is_elem` numericClassKeys
162 isStandardClass    clas = classKey clas `is_elem` standardClassKeys
163 is_elem = isIn "is_X_Class"
164 \end{code}