summaryrefslogtreecommitdiff
path: root/libraries
diff options
context:
space:
mode:
authorRyan Scott <ryan.gl.scott@gmail.com>2016-12-18 10:41:33 -0500
committerRyan Scott <ryan.gl.scott@gmail.com>2016-12-18 10:41:33 -0500
commitb5d788aa0e73fdf22cca3f88962e7652b07073cc (patch)
treed058f32e58bc561c350643f2f694cb547217f3c4 /libraries
parent630cfc382084c48c8df84a2ac59c76710ae7e0e8 (diff)
downloadhaskell-b5d788aa0e73fdf22cca3f88962e7652b07073cc.tar.gz
Introduce unboxedSum{Data,Type}Name to template-haskell
Summary: In D2448 (which introduced Template Haskell support for unboxed sums), I neglected to add `unboxedSumDataName` and `unboxedSumTypeName` functions, since there wasn't any way you could write unboxed sum data or type constructors in prefix form to begin with (see #12514). But even if you can't write these `Name`s directly in source code, it would still be nice to be able to use these `Name`s in Template Haskell (for instance, to be able to treat unboxed sum type constructors like any other type constructors). Along the way, this uncovered a minor bug in `isBuiltInOcc_maybe` in `TysWiredIn`, which was calculating the arity of unboxed sum data constructors incorrectly. Test Plan: make test TEST=T12478_5 Reviewers: osa1, goldfire, austin, bgamari Subscribers: thomie Differential Revision: https://phabricator.haskell.org/D2854 GHC Trac Issues: #12478, #12514
Diffstat (limited to 'libraries')
-rw-r--r--libraries/template-haskell/Language/Haskell/TH.hs2
-rw-r--r--libraries/template-haskell/Language/Haskell/TH/Syntax.hs43
2 files changed, 45 insertions, 0 deletions
diff --git a/libraries/template-haskell/Language/Haskell/TH.hs b/libraries/template-haskell/Language/Haskell/TH.hs
index 5a497936ae..fd5c06f2f1 100644
--- a/libraries/template-haskell/Language/Haskell/TH.hs
+++ b/libraries/template-haskell/Language/Haskell/TH.hs
@@ -60,6 +60,8 @@ module Language.Haskell.TH(
-- ** Built-in names
tupleTypeName, tupleDataName, -- Int -> Name
unboxedTupleTypeName, unboxedTupleDataName, -- :: Int -> Name
+ unboxedSumTypeName, -- :: SumArity -> Name
+ unboxedSumDataName, -- :: SumAlt -> SumArity -> Name
-- * The algebraic data types
-- | The lowercase versions (/syntax operators/) of these constructors are
diff --git a/libraries/template-haskell/Language/Haskell/TH/Syntax.hs b/libraries/template-haskell/Language/Haskell/TH/Syntax.hs
index c9bccf665f..9de531ab9e 100644
--- a/libraries/template-haskell/Language/Haskell/TH/Syntax.hs
+++ b/libraries/template-haskell/Language/Haskell/TH/Syntax.hs
@@ -1199,6 +1199,49 @@ mk_unboxed_tup_name n space
n_commas = n - 1
tup_mod = mkModName "GHC.Tuple"
+-- Unboxed sum data and type constructors
+-- | Unboxed sum data constructor
+unboxedSumDataName :: SumAlt -> SumArity -> Name
+-- | Unboxed sum type constructor
+unboxedSumTypeName :: SumArity -> Name
+
+unboxedSumDataName alt arity
+ | alt > arity
+ = error $ prefix ++ "Index out of bounds." ++ debug_info
+
+ | alt <= 0
+ = error $ prefix ++ "Alt must be > 0." ++ debug_info
+
+ | arity < 2
+ = error $ prefix ++ "Arity must be >= 2." ++ debug_info
+
+ | otherwise
+ = Name (mkOccName sum_occ)
+ (NameG DataName (mkPkgName "ghc-prim") (mkModName "GHC.Prim"))
+
+ where
+ prefix = "unboxedSumDataName: "
+ debug_info = " (alt: " ++ show alt ++ ", arity: " ++ show arity ++ ")"
+
+ -- Synced with the definition of mkSumDataConOcc in TysWiredIn
+ sum_occ = '(' : '#' : bars nbars_before ++ '_' : bars nbars_after ++ "#)"
+ bars i = replicate i '|'
+ nbars_before = alt - 1
+ nbars_after = arity - alt
+
+unboxedSumTypeName arity
+ | arity < 2
+ = error $ "unboxedSumTypeName: Arity must be >= 2."
+ ++ " (arity: " ++ show arity ++ ")"
+
+ | otherwise
+ = Name (mkOccName sum_occ)
+ (NameG TcClsName (mkPkgName "ghc-prim") (mkModName "GHC.Prim"))
+
+ where
+ -- Synced with the definition of mkSumTyConOcc in TysWiredIn
+ sum_occ = '(' : '#' : replicate (arity - 1) '|' ++ "#)"
+
-----------------------------------------------------
-- Locations
-----------------------------------------------------