50a668736fa548c556360dda65fce030516cf20b
[ghc-hetmet.git] / ghc / compiler / basicTypes / FieldLabel.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1996-1998
3 %
4 \section[FieldLabel]{The @FieldLabel@ type}
5
6 \begin{code}
7 module FieldLabel(
8         FieldLabel,     -- Abstract
9
10         mkFieldLabel, 
11         fieldLabelName, fieldLabelTyCon, fieldLabelType, fieldLabelTag,
12
13         FieldLabelTag,
14         firstFieldLabelTag, allFieldLabelTags
15   ) where
16
17 #include "HsVersions.h"
18
19 import {-# SOURCE #-}   TypeRep( Type ) -- FieldLabel is compiled very early
20 import {-# SOURCE #-}   TyCon( TyCon )  -- FieldLabel is compiled very early
21
22 import Name             ( Name{-instance Eq/Outputable-}, NamedThing(..), nameUnique )
23 import Outputable
24 import Unique           ( Uniquable(..) )
25 \end{code}
26
27 \begin{code}
28 data FieldLabel
29   = FieldLabel  Name            -- Also used as the Name of the field selector Id
30
31                 TyCon           -- Parent type constructor
32
33                 Type            -- Type of the field; may have free type variables that
34                                 -- are the tyvars of its parent *data* constructor, and
35                                 -- those will be the same as the tyvars of its parent *type* constructor
36                                 -- e.g.  data T a = MkT { op1 :: a -> a, op2 :: a -> Int }
37                                 -- The type in the FieldLabel for op1 will be simply (a->a).
38
39                 FieldLabelTag   -- Indicates position within constructor
40                                 -- (starting with firstFieldLabelTag)
41                                 --
42                                 -- If the same field occurs in more than one constructor
43                                 -- then it'll have a separate FieldLabel on each occasion,
44                                 -- but with a single name (and presumably the same type!)
45
46 type FieldLabelTag = Int
47
48 mkFieldLabel = FieldLabel
49
50 firstFieldLabelTag :: FieldLabelTag
51 firstFieldLabelTag = 1
52
53 allFieldLabelTags :: [FieldLabelTag]
54 allFieldLabelTags = [firstFieldLabelTag..]
55
56 fieldLabelName  (FieldLabel n _ _  _)   = n
57 fieldLabelTyCon (FieldLabel _ tc _ _)   = tc
58 fieldLabelType  (FieldLabel _ _ ty _)   = ty
59 fieldLabelTag   (FieldLabel _ _ _  tag) = tag
60
61 instance Eq FieldLabel where
62     fl1 == fl2 = fieldLabelName fl1 == fieldLabelName fl2
63
64 instance Outputable FieldLabel where
65     ppr fl = ppr (fieldLabelName fl)
66
67 instance NamedThing FieldLabel where
68     getName = fieldLabelName
69
70 instance Uniquable FieldLabel where
71     getUnique fl = nameUnique (fieldLabelName fl)
72 \end{code}