Skip to content

Commit

Permalink
Add a flag for omitting the generation of inlining pragmas in TH
Browse files Browse the repository at this point in the history
  • Loading branch information
mgsloan committed Sep 1, 2012
1 parent a06106a commit 30ee20b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
9 changes: 9 additions & 0 deletions lens.cabal
Expand Up @@ -113,6 +113,12 @@ flag old-inline-pragmas
default: False
manual: True

-- Omit the generation of inlining pragmas in the TH code
-- (used to address compile errors resulting from different versions of that API)
flag omit-inlining
default: False
manual: True

library
build-depends:
base >= 4.3 && < 5,
Expand Down Expand Up @@ -190,6 +196,9 @@ library
if !flag(old-inline-pragmas)
cpp-options: -DNEW_INLINE_PRAGMAS

if flag(omit-inlining)
cpp-options: -DOMIT_INLINING

if impl(ghc>=7.4)
other-extensions: Trustworthy
build-depends: ghc-prim
Expand Down
40 changes: 24 additions & 16 deletions src/Control/Lens/TH.hs
Expand Up @@ -220,25 +220,25 @@ makeIsoLenses cfg ctx tyConName tyArgs0 dataConName maybeFieldName partTy = do
let decl = SigD isoName $ quantified $ isoCon `apps`
if cfg^.simpleLenses then [aty,aty,cty,cty] else [aty,bty,cty,dty]
body <- makeBody isoName dataConName makeIsoFrom makeIsoTo
#if (MIN_VERSION_template_haskell(2,8,0)) && defined(NEW_INLINE_PRAGMAS)
inlining <- pragInlD isoName $ inlineSpecNoPhase Inline False
#if defined(OMIT_INLINING)
return [decl, body]
#else
inlining <- pragInlD isoName $ inlineSpecNoPhase True False
#endif
inlining <- inlinePragma isoName
return [decl, body, inlining]
#endif
accessorDecls <- case mkName <$> (maybeFieldName >>= view lensField cfg . nameBase) of
jfn@(Just lensName)
| (jfn /= maybeIsoName) && (isNothing maybeIsoName || cfg^.singletonAndField) -> do
let decl = SigD lensName $ quantified $ isoCon `apps`
if cfg^.simpleLenses then [cty,cty,aty,aty]
else [cty,dty,aty,bty]
body <- makeBody lensName dataConName makeIsoTo makeIsoFrom
#if (MIN_VERSION_template_haskell(2,8,0)) && defined(NEW_INLINE_PRAGMAS)
inlining <- pragInlD lensName $ inlineSpecNoPhase Inline False
#if defined(OMIT_INLINING)
return [decl, body]
#else
inlining <- pragInlD lensName $ inlineSpecNoPhase True False
#endif
inlining <- inlinePragma lensName
return [decl, body, inlining]
#endif
_ -> return []
return $ isoDecls ++ accessorDecls

Expand Down Expand Up @@ -335,10 +335,8 @@ makeFieldLenses cfg ctx tyConName tyArgs0 cons = do
++ filter (\_ -> cfg^.createInstance)
[ instanceD (return []) (conT clsName `appT` conT tyConName)
[ funD methodName [clause [varP a] (normalB (varE a)) []]
#if (MIN_VERSION_template_haskell(2,8,0)) && defined(NEW_INLINE_PRAGMAS)
, pragInlD methodName $ inlineSpecNoPhase Inline False
#else
, pragInlD methodName $ inlineSpecNoPhase True False
#if !defined(OMIT_INLINING)
, inlinePragma methodName
#endif
]]
bodies <- for (toList fieldMap) $ \ (FieldDesc nm cty bds) ->
Expand All @@ -365,12 +363,12 @@ makeFieldLenses cfg ctx tyConName tyArgs0 cons = do
then [aty,aty,cty,cty]
else [aty,bty,cty,dty]
body <- makeFieldLensBody lensName nm cons $ fmap (mkName . view _2) maybeLensClass
#if (MIN_VERSION_template_haskell(2,8,0)) && defined(NEW_INLINE_PRAGMAS)
inlining <- pragInlD lensName $ inlineSpecNoPhase Inline False
#if defined(OMIT_INLINING)
return [decl, body]
#else
inlining <- pragInlD lensName $ inlineSpecNoPhase True False
#endif
inlining <- inlinePragma lensName
return [decl, body, inlining]
#endif
return $ classDecls ++ Prelude.concat bodies

-- | Build lenses with a custom configuration
Expand Down Expand Up @@ -466,3 +464,13 @@ instance Applicative Q where
pure = return
(<*>) = ap
#endif

#if !defined(OMIT_INLINING)

#if (MIN_VERSION_template_haskell(2,8,0)) && defined(NEW_INLINE_PRAGMAS)
inlinePragma methodName = pragInlD methodName $ inlineSpecNoPhase Inline False
#else
inlinePragma methodName = pragInlD methodName $ inlineSpecNoPhase True False
#endif

#endif

0 comments on commit 30ee20b

Please sign in to comment.