Skip to content

Commit 33ef72a

Browse files
committed
Qualified import of GHC.Exts in generated code
We can import the identifiers with `#` supported by all GHC versions unqualified from `GHC.Exts`, not expecting any clash with user code. Identifiers that are only known in newer versions of GHC, in conditional code, are referred to qualified, e.g. `GHC.Exts.foo`.
1 parent 06b8f3d commit 33ef72a

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

data/AlexTemplate.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
# define FAST_INT Int#
1414
-- Do not remove this comment. Required to fix CPP parsing when using GCC and a clang-compiled alex.
1515
# if __GLASGOW_HASKELL__ > 706
16-
# define GTE(n,m) (tagToEnum# (n >=# m))
17-
# define EQ(n,m) (tagToEnum# (n ==# m))
16+
# define GTE(n,m) (GHC.Exts.tagToEnum# (n >=# m))
17+
# define EQ(n,m) (GHC.Exts.tagToEnum# (n ==# m))
1818
# else
1919
# define GTE(n,m) (n >=# m)
2020
# define EQ(n,m) (n ==# m)
@@ -53,7 +53,7 @@ alexIndexInt16OffAddr (AlexA# arr) off =
5353
off' = off *# 2#
5454
#else
5555
#if __GLASGOW_HASKELL__ >= 901
56-
int16ToInt#
56+
GHC.Exts.int16ToInt#
5757
#endif
5858
(indexInt16OffAddr# arr off)
5959
#endif
@@ -78,7 +78,7 @@ alexIndexInt32OffAddr (AlexA# arr) off =
7878
off' = off *# 4#
7979
#else
8080
#if __GLASGOW_HASKELL__ >= 901
81-
int32ToInt#
81+
GHC.Exts.int32ToInt#
8282
#endif
8383
(indexInt32OffAddr# arr off)
8484
#endif

src/Main.hs

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import Paths_alex ( version, getDataDir )
2626
import Control.Exception ( bracketOnError )
2727
import Control.Monad ( when, liftM )
2828
import Data.Char ( chr )
29-
import Data.List ( isSuffixOf, nub )
29+
import Data.List ( intercalate, isSuffixOf, nub )
3030
import Data.Map ( Map )
3131
import Data.Version ( showVersion )
3232
import System.Console.GetOpt ( getOpt, usageInfo, ArgOrder(..), OptDescr(..), ArgDescr(..) )
@@ -385,33 +385,63 @@ optNoWarnings =
385385
]
386386

387387
importsToInject :: Target -> [CLIFlags] -> String
388-
importsToInject _ cli = always_imports ++ debug_imports ++ glaexts_import
388+
importsToInject _ cli = unlines $
389+
always_imports ++ debug_imports ++ glaexts_import
389390
where
390391
glaexts_import | OptGhcTarget `elem` cli = import_glaexts
391-
| otherwise = ""
392+
| otherwise = []
392393

393394
debug_imports | OptDebugParser `elem` cli = import_debug
394-
| otherwise = ""
395+
| otherwise = []
395396

396397
-- We need to #include "ghcconfig.h" to get hold of
397398
-- WORDS_BIGENDIAN (see AlexTemplate.hs).
398399

399-
always_imports :: String
400-
always_imports = "#include \"ghcconfig.h\"\n" ++
401-
"import Data.Array\n" ++
402-
""
403-
404-
import_glaexts :: String
405-
import_glaexts = "import Data.Array.Base (unsafeAt)\n" ++
406-
"import GHC.Exts\n" ++
407-
""
408-
409-
import_debug :: String
410-
import_debug = "import Data.Char (chr)\n" ++
411-
"import System.IO\n" ++
412-
"import System.IO.Unsafe\n" ++
413-
"import Debug.Trace\n" ++
414-
""
400+
always_imports :: [String]
401+
always_imports =
402+
[ "#include \"ghcconfig.h\""
403+
, "import Data.Array"
404+
]
405+
406+
import_glaexts :: [String]
407+
import_glaexts =
408+
[ "import Data.Array.Base (unsafeAt)"
409+
, "import GHC.Exts (" ++ imports ++ ")"
410+
, "import qualified GHC.Exts"
411+
]
412+
where
413+
-- We can import anything mentioning # safely,
414+
-- assuming the user code does not make use of
415+
-- MagicHash.
416+
imports = intercalate ","
417+
[ "Addr#"
418+
, "Int#"
419+
, "Int(I#)"
420+
, "(*#)"
421+
, "(+#)"
422+
, "(-#)"
423+
, "(==#)"
424+
, "(>=#)"
425+
, "indexCharOffAddr#"
426+
, "indexInt16OffAddr#"
427+
, "indexInt32OffAddr#"
428+
, "int2Word#"
429+
, "narrow16Int#"
430+
, "narrow32Int#"
431+
, "negateInt#"
432+
, "or#"
433+
, "ord#"
434+
, "uncheckedShiftL#"
435+
, "word2Int#"
436+
]
437+
438+
import_debug :: [String]
439+
import_debug =
440+
[ "import Data.Char (chr)"
441+
, "import System.IO"
442+
, "import System.IO.Unsafe"
443+
, "import Debug.Trace"
444+
]
415445

416446
templateDir :: IO FilePath -> [CLIFlags] -> IO FilePath
417447
templateDir def cli

0 commit comments

Comments
 (0)