Spelling fixes in comments [ci skip]
[ghc.git] / compiler / basicTypes / FieldLabel.hs
1 {-
2 %
3 % (c) Adam Gundry 2013-2015
4 %
5
6 This module defines the representation of FieldLabels as stored in
7 TyCons. As well as a selector name, these have some extra structure
8 to support the DuplicateRecordFields extension.
9
10 In the normal case (with NoDuplicateRecordFields), a datatype like
11
12 data T = MkT { foo :: Int }
13
14 has
15
16 FieldLabel { flLabel = "foo"
17 , flIsOverloaded = False
18 , flSelector = foo }.
19
20 In particular, the Name of the selector has the same string
21 representation as the label. If DuplicateRecordFields
22 is enabled, however, the same declaration instead gives
23
24 FieldLabel { flLabel = "foo"
25 , flIsOverloaded = True
26 , flSelector = $sel:foo:MkT }.
27
28 Now the name of the selector ($sel:foo:MkT) does not match the label of
29 the field (foo). We must be careful not to show the selector name to
30 the user! The point of mangling the selector name is to allow a
31 module to define the same field label in different datatypes:
32
33 data T = MkT { foo :: Int }
34 data U = MkU { foo :: Bool }
35
36 Now there will be two FieldLabel values for 'foo', one in T and one in
37 U. They share the same label (FieldLabelString), but the selector
38 functions differ.
39
40 See also Note [Representing fields in AvailInfo] in Avail.
41
42 Note [Why selector names include data constructors]
43 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44
45 As explained above, a selector name includes the name of the first
46 data constructor in the type, so that the same label can appear
47 multiple times in the same module. (This is irrespective of whether
48 the first constructor has that field, for simplicity.)
49
50 We use a data constructor name, rather than the type constructor name,
51 because data family instances do not have a representation type
52 constructor name generated until relatively late in the typechecking
53 process.
54
55 Of course, datatypes with no constructors cannot have any fields.
56
57 -}
58
59 {-# LANGUAGE DeriveDataTypeable #-}
60 {-# LANGUAGE DeriveFunctor #-}
61 {-# LANGUAGE DeriveFoldable #-}
62 {-# LANGUAGE DeriveTraversable #-}
63 {-# LANGUAGE StandaloneDeriving #-}
64
65 module FieldLabel ( FieldLabelString
66 , FieldLabelEnv
67 , FieldLbl(..)
68 , FieldLabel
69 , mkFieldLabelOccs
70 ) where
71
72 import OccName
73 import Name
74
75 import FastString
76 import FastStringEnv
77 import Outputable
78 import Binary
79
80 import Data.Data
81
82 -- | Field labels are just represented as strings;
83 -- they are not necessarily unique (even within a module)
84 type FieldLabelString = FastString
85
86 -- | A map from labels to all the auxiliary information
87 type FieldLabelEnv = DFastStringEnv FieldLabel
88
89
90 type FieldLabel = FieldLbl Name
91
92 -- | Fields in an algebraic record type
93 data FieldLbl a = FieldLabel {
94 flLabel :: FieldLabelString, -- ^ User-visible label of the field
95 flIsOverloaded :: Bool, -- ^ Was DuplicateRecordFields on
96 -- in the defining module for this datatype?
97 flSelector :: a -- ^ Record selector function
98 }
99 deriving (Eq, Functor, Foldable, Traversable)
100 deriving instance Data a => Data (FieldLbl a)
101
102 instance Outputable a => Outputable (FieldLbl a) where
103 ppr fl = ppr (flLabel fl) <> braces (ppr (flSelector fl))
104
105 instance Binary a => Binary (FieldLbl a) where
106 put_ bh (FieldLabel aa ab ac) = do
107 put_ bh aa
108 put_ bh ab
109 put_ bh ac
110 get bh = do
111 ab <- get bh
112 ac <- get bh
113 ad <- get bh
114 return (FieldLabel ab ac ad)
115
116
117 -- | Record selector OccNames are built from the underlying field name
118 -- and the name of the first data constructor of the type, to support
119 -- duplicate record field names.
120 -- See Note [Why selector names include data constructors].
121 mkFieldLabelOccs :: FieldLabelString -> OccName -> Bool -> FieldLbl OccName
122 mkFieldLabelOccs lbl dc is_overloaded
123 = FieldLabel { flLabel = lbl, flIsOverloaded = is_overloaded
124 , flSelector = sel_occ }
125 where
126 str = ":" ++ unpackFS lbl ++ ":" ++ occNameString dc
127 sel_occ | is_overloaded = mkRecFldSelOcc str
128 | otherwise = mkVarOccFS lbl