on master help sign in
Implement specialize pragma
commitfab9f15ad6e34165a57b0151de3503710b82898a authormrkkrp <mark.karpov@tweag.io> dateSun 9 Jun 2019, 14:59 (7 years ago) parentd9d4219
4 files changed+79−15
data/examples/declaration/signature/specialize/specialize-out.hs+14 data/examples/declaration/signature/specialize/specialize.hs+15 src/Ormolu/Printer/Meat/Declaration/Signature.hs+35−9 src/Ormolu/Printer/Meat/Module.hs+15−6
data/examples/declaration/signature/specialize/specialize-out.hsnew file+14
@@ -0,0 +1,14 @@
1+foo :: Num a => a -> a
2+foo = id
3+{-# SPECIALIZE foo :: Int -> Int #-}
4+
5+{-# SPECIALIZE [2] bar :: Int -> Int #-}
6+bar :: Num a => a -> a
7+bar = id
8+
9+baz :: Num a => a -> a
10+baz = id
11+{-# SPECIALIZE [~2] baz
12+ :: Int
13+ -> Int
14+ #-}
data/examples/declaration/signature/specialize/specialize.hsnew file+15
@@ -0,0 +1,15 @@
1+foo :: Num a => a -> a
2+foo = id
3+
4+{-# SPECIALIZE foo :: Int -> Int #-}
5+
6+{-# SPECIALIZE [2] bar :: Int -> Int #-}
7+
8+bar :: Num a => a -> a
9+bar = id
10+
11+baz :: Num a => a -> a
12+baz = id
13+{-# SPECIALIZE [~2] baz
14+ :: Int
15+ -> Int #-}
src/Ormolu/Printer/Meat/Declaration/Signature.hs+35−9
@@ -27,6 +27,7 @@ p_sigDecl' = \case
2727 ClassOpSig NoExt def names hsib -> p_classOpSig def names hsib
2828 FixSig NoExt sig -> p_fixSig sig
2929 InlineSig NoExt name inlinePragma -> p_inlineSig name inlinePragma
30+ SpecSig NoExt name ts inlinePragma -> p_specSig name ts inlinePragma
3031 _ -> notImplemented "certain types of signature declarations"
3132
3233p_typeSig
@@ -76,13 +77,38 @@ p_inlineSig name InlinePragma {..} = pragmaBraces $ do
7677 NoInline -> "NOINLINE"
7778 NoUserInline -> notImplemented "NoUserInline"
7879 space
79− case inl_act of
80− NeverActive -> return ()
81− AlwaysActive -> return ()
82− ActiveBefore _ n -> do
83− brackets (txt "~" >> atom n)
84− space
85− ActiveAfter _ n -> do
86− brackets (atom n)
87− space
80+ p_activation inl_act
8881 p_rdrName name
82+
83+p_specSig
84+ :: Located RdrName -- ^ Name
85+ -> [LHsSigType GhcPs] -- ^ The types to specialize to
86+ -> InlinePragma -- ^ For specialize inline
87+ -> R ()
88+p_specSig name ts InlinePragma {..} = pragmaBraces $ do
89+ txt "SPECIALIZE"
90+ space
91+ p_activation inl_act
92+ p_rdrName name
93+ breakpoint
94+ inci $ do
95+ txt ":: "
96+ -- XXX Not at all sure why ts is a list of @LHsSigType GhcPs@ things, it
97+ -- appears that we only can give one type to specialize to per pragma.
98+ -- Maybe I'm mistaken.
99+ located (hsib_body (head ts)) p_hsType
100+
101+p_activation :: Activation -> R ()
102+p_activation = \case
103+ NeverActive -> return ()
104+ AlwaysActive -> return ()
105+ ActiveBefore _ n -> do
106+ txt "[~"
107+ atom n
108+ txt "]"
109+ space
110+ ActiveAfter _ n -> do
111+ txt "["
112+ atom n
113+ txt "]"
114+ space
src/Ormolu/Printer/Meat/Module.hs+15−6
@@ -1,4 +1,5 @@
11{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE PatternSynonyms #-}
23{-# LANGUAGE RecordWildCards #-}
34
45-- | Rendering of modules.
@@ -64,10 +65,18 @@ separatedDecls
6465 :: HsDecl GhcPs
6566 -> HsDecl GhcPs
6667 -> Bool
67−separatedDecls (SigD NoExt (TypeSig NoExt (n:_) _)) (ValD NoExt (FunBind NoExt n' _ _ _)) =
68− unL n /= unL n'
69−separatedDecls (ValD NoExt (FunBind NoExt n _ _ _)) (SigD NoExt (InlineSig NoExt n' _)) =
70− unL n /= unL n'
71−separatedDecls (SigD NoExt (InlineSig NoExt n _)) (SigD NoExt (TypeSig NoExt (n':_) _)) =
72− unL n /= unL n'
68+separatedDecls (TypeSignature n) (FunctionBody n') = n /= n'
69+separatedDecls (FunctionBody n) (InlinePragma n') = n /= n'
70+separatedDecls (InlinePragma n) (TypeSignature n') = n /= n'
71+separatedDecls (FunctionBody n) (SpecializePragma n') = n /= n'
72+separatedDecls (SpecializePragma n) (TypeSignature n') = n /= n'
7373separatedDecls _ _ = True
74+
75+pattern TypeSignature
76+ , FunctionBody
77+ , InlinePragma
78+ , SpecializePragma :: RdrName -> HsDecl GhcPs
79+pattern TypeSignature n <- SigD NoExt (TypeSig NoExt ((L _ n):_) _)
80+pattern FunctionBody n <- ValD NoExt (FunBind NoExt (L _ n) _ _ _)
81+pattern InlinePragma n <- SigD NoExt (InlineSig NoExt (L _ n) _)
82+pattern SpecializePragma n <- SigD NoExt (SpecSig NoExt (L _ n) _ _)