on master help sign in
Implement specialize pragma
fab9f15mrkkrp
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
27 ClassOpSig NoExt def names hsib -> p_classOpSig def names hsib
27 ClassOpSig NoExt def names hsib -> p_classOpSig def names hsib
28 FixSig NoExt sig -> p_fixSig sig
28 FixSig NoExt sig -> p_fixSig sig
29 InlineSig NoExt name inlinePragma -> p_inlineSig name inlinePragma
29 InlineSig NoExt name inlinePragma -> p_inlineSig name inlinePragma
30+ SpecSig NoExt name ts inlinePragma -> p_specSig name ts inlinePragma
30 _ -> notImplemented "certain types of signature declarations"
31 _ -> notImplemented "certain types of signature declarations"
31
32
32p_typeSig
33p_typeSig
@@ -76,13 +77,38 @@ p_inlineSig name InlinePragma {..} = pragmaBraces $ do
76 NoInline -> "NOINLINE"
77 NoInline -> "NOINLINE"
77 NoUserInline -> notImplemented "NoUserInline"
78 NoUserInline -> notImplemented "NoUserInline"
78 space
79 space
79− case inl_act of
80+ p_activation inl_act
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
88 p_rdrName name
81 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 @@
1{-# LANGUAGE OverloadedStrings #-}
1{-# LANGUAGE OverloadedStrings #-}
2+{-# LANGUAGE PatternSynonyms #-}
2{-# LANGUAGE RecordWildCards #-}
3{-# LANGUAGE RecordWildCards #-}
3
4
4-- | Rendering of modules.
5-- | Rendering of modules.
@@ -64,10 +65,18 @@ separatedDecls
64 :: HsDecl GhcPs
65 :: HsDecl GhcPs
65 -> HsDecl GhcPs
66 -> HsDecl GhcPs
66 -> Bool
67 -> Bool
67−separatedDecls (SigD NoExt (TypeSig NoExt (n:_) _)) (ValD NoExt (FunBind NoExt n' _ _ _)) =
68+separatedDecls (TypeSignature n) (FunctionBody n') = n /= n'
68− unL n /= unL n'
69+separatedDecls (FunctionBody n) (InlinePragma n') = n /= n'
69−separatedDecls (ValD NoExt (FunBind NoExt n _ _ _)) (SigD NoExt (InlineSig NoExt n' _)) =
70+separatedDecls (InlinePragma n) (TypeSignature n') = n /= n'
70− unL n /= unL n'
71+separatedDecls (FunctionBody n) (SpecializePragma n') = n /= n'
71−separatedDecls (SigD NoExt (InlineSig NoExt n _)) (SigD NoExt (TypeSig NoExt (n':_) _)) =
72+separatedDecls (SpecializePragma n) (TypeSignature n') = n /= n'
72− unL n /= unL n'
73separatedDecls _ _ = True
73separatedDecls _ _ = 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) _ _)