-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathFramework.hs
More file actions
521 lines (473 loc) · 15.2 KB
/
Framework.hs
File metadata and controls
521 lines (473 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TupleSections #-}
{-# OPTIONS_GHC -Wno-missing-fields #-}
{-
Copyright 2020 The CodeWorld Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-}
module CodeWorld.Compile.Framework where
import qualified "ghc" Config as GHC
import Control.Applicative
import Control.Concurrent
import Control.Exception (evaluate)
import Control.Monad
import Control.Monad.Catch
import Control.Monad.IO.Class
import Control.Monad.State
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.Functor.Identity
import Data.Generics
import Data.List (foldl', intercalate, isPrefixOf)
import Data.Map (Map)
import qualified Data.Map as M
import Data.Maybe
import Data.Monoid
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
import qualified Data.Text.IO as T
import qualified "ghc" DynFlags as GHC
import ErrorSanitizer
import qualified "ghc" FastString as GHC
import qualified "ghc" Fingerprint as GHC
import qualified "ghc-boot-th" GHC.LanguageExtensions.Type as GHC
import qualified "ghc" HeaderInfo as GHC
import qualified "ghc" GHC.Hs.Extension as GHC
import qualified "ghc" GHC.Hs as GHC
import qualified "ghc" RdrHsSyn as GHC
import qualified "ghc" TcHsSyn as GHC
import qualified "ghc" HscTypes as GHC
import Language.Haskell.Exts
import qualified "ghc" Lexer as GHC
import qualified "ghc" Fingerprint as GHC
import qualified "ghc" Module as GHC
import qualified "ghc" Outputable as GHC
import qualified "ghc" Panic as GHC
import qualified "ghc" Parser as GHC
import qualified "ghc" GhcNameVersion as GHC
import qualified "ghc" FileSettings as GHC
import qualified "ghc" ToolSettings as GHC
import qualified "ghc-boot" GHC.Platform as GHC
import qualified "ghc" SrcLoc as GHC
import qualified "ghc" StringBuffer as GHC
import System.Directory
import System.Exit (ExitCode (..))
import System.FilePath
import System.IO
import System.IO.Temp (withSystemTempDirectory)
import System.Process
import Text.Regex.TDFA
import Text.Regex.TDFA.Text
data Stage
= ErrorCheck
| -- | Output file location
FullBuild FilePath
| GenBase
String
-- ^ Base module name
FilePath
-- ^ Base module file location
FilePath
-- ^ Output file location
FilePath
-- ^ Symbol file location
| UseBase
FilePath
-- ^ Output file location
FilePath
-- ^ Symbol file location
String
-- ^ URL of the base JavaScript bundle
deriving (Eq, Show)
data CompileStatus
= CompileSuccess
| CompileError
| CompileAborted
deriving (Eq, Show, Ord)
data CompileState = CompileState
{ compileMode :: SourceMode,
compileStage :: Stage,
compileBuildDir :: FilePath,
compileSourcePaths :: [FilePath],
compileModuleFinder :: String -> IO (Maybe FilePath),
compileOutputPath :: FilePath,
compileVerbose :: Bool,
compileTimeout :: Int,
compileStatus :: CompileStatus,
compileErrors :: [Diagnostic],
compileMainSourcePath :: Maybe FilePath,
compileReadSource :: Map FilePath ByteString,
compileParsedSource :: Map FilePath ParsedCode,
compileGHCParsedSource :: Map FilePath GHCParsedCode,
compileImportLocations :: Map FilePath SrcSpanInfo
}
type MonadCompile m = (MonadState CompileState m, MonadIO m, MonadMask m)
type SourceMode = String -- typically "codeworld" or "haskell"
type Diagnostic = (SrcSpanInfo, CompileStatus, String)
data ParsedCode = Parsed (Module SrcSpanInfo) | NoParse
deriving (Typeable, Data, Show)
data GHCParsedCode = GHCParsed (GHC.HsModule GHC.GhcPs) | GHCNoParse
deriving (Typeable, Data)
getSourceCode :: MonadCompile m => FilePath -> m ByteString
getSourceCode src = do
cached <- gets compileReadSource
case M.lookup src cached of
Just source -> return source
Nothing -> do
source <- liftIO $ B.readFile src
modify $ \state -> state {compileReadSource = M.insert src source cached}
return source
gmapT' :: GenericT -> GenericT
gmapT' f x0 = runIdentity (gfoldl k Identity x0)
where
k :: Data d => Identity (d -> b) -> d -> Identity b
k (Identity c) x = Identity $! c $! f x
strictify :: GenericT
strictify = gmapT' strictify
getParsedCode :: MonadCompile m => FilePath -> m ParsedCode
getParsedCode src = do
cached <- gets compileParsedSource
case M.lookup src cached of
Just parsed -> return parsed
Nothing -> do
source <- getSourceCode src
parsed <- parseCode ["TupleSections"] (decodeUtf8 source)
parsed <-
liftIO $
catch
(evaluate (strictify parsed))
(\(e :: SomeException) -> (return NoParse))
modify $ \state -> state {compileParsedSource = M.insert src parsed cached}
return parsed
getGHCParsedCode :: MonadCompile m => FilePath -> m GHCParsedCode
getGHCParsedCode src = do
cached <- gets compileGHCParsedSource
case M.lookup src cached of
Just parsed -> return parsed
Nothing -> do
source <- getSourceCode src
parsed <- ghcParseCode ["TupleSections"] (decodeUtf8 source)
parsed <-
liftIO $
catch
(evaluate (strictify parsed))
(\(e :: SomeException) -> (return GHCNoParse))
modify $ \state -> state {compileGHCParsedSource = M.insert src parsed cached}
return parsed
getMainSourcePath :: MonadCompile m => m FilePath
getMainSourcePath = do
mainPath <- gets compileMainSourcePath
case mainPath of
Just path -> return path
Nothing -> do
srcs <- gets compileSourcePaths
parsed <- mapM getGHCParsedCode srcs
let matched =
[ src | (src, GHCParsed mod) <- zip srcs parsed, isMainModName (GHC.hsmodName mod)
]
let result = head (matched ++ srcs)
modify $ \state -> state {compileMainSourcePath = Just result}
return result
where
isMainModName Nothing = True
isMainModName (Just (GHC.L _ modName)) =
GHC.moduleNameString modName == "Main"
getMainSourceCode :: MonadCompile m => m ByteString
getMainSourceCode = getMainSourcePath >>= getSourceCode
getMainParsedCode :: MonadCompile m => m ParsedCode
getMainParsedCode = getMainSourcePath >>= getParsedCode
getMainGHCParsedCode :: MonadCompile m => m GHCParsedCode
getMainGHCParsedCode = getMainSourcePath >>= getGHCParsedCode
getMainModuleName :: MonadCompile m => m String
getMainModuleName = do
result <- getMainGHCParsedCode
return $ case result of
GHCNoParse -> "Main"
GHCParsed mod -> case GHC.hsmodName mod of
Nothing -> "Main"
Just (GHC.L _ name) -> GHC.moduleNameString name
getDiagnostics :: MonadCompile m => m [Diagnostic]
getDiagnostics = do
diags <- gets compileErrors
return diags
codeworldModeExts :: [String]
codeworldModeExts =
[ "BangPatterns",
"DisambiguateRecordFields",
"EmptyDataDecls",
"ExistentialQuantification",
"ForeignFunctionInterface",
"GADTs",
"JavaScriptFFI",
"KindSignatures",
"LiberalTypeSynonyms",
"NamedFieldPuns",
"NoMonomorphismRestriction",
"NoQuasiQuotes",
"NoTemplateHaskell",
"NoUndecidableInstances",
"OverloadedStrings",
"PackageImports",
"ParallelListComp",
"PartialTypeSignatures",
"PatternGuards",
"RankNTypes",
"RebindableSyntax",
"RecordWildCards",
"ScopedTypeVariables",
"TypeOperators",
"ViewPatterns"
]
parseCode :: MonadCompile m => [String] -> Text -> m ParsedCode
parseCode extraExts src = do
sourceMode <- gets compileMode
let exts
| sourceMode == "codeworld" = codeworldModeExts ++ extraExts
| otherwise = extraExts
mode =
defaultParseMode
{ parseFilename = "program.hs",
extensions = map parseExtension exts
}
return $ case parseFileContentsWithMode mode (T.unpack src) of
ParseOk mod -> Parsed mod
ParseFailed _ _ -> NoParse
ghcExtensionsByName :: Map String GHC.Extension
ghcExtensionsByName =
M.fromList
[ (GHC.flagSpecName spec, GHC.flagSpecFlag spec)
| spec <- GHC.xFlags
]
applyExtensionToFlags :: GHC.DynFlags -> String -> GHC.DynFlags
applyExtensionToFlags dflags name
| "No" `isPrefixOf` name =
GHC.xopt_unset dflags $ fromJust $ M.lookup (drop 2 name) ghcExtensionsByName
| otherwise =
GHC.xopt_set dflags $ fromJust $ M.lookup name ghcExtensionsByName
ghcParseCode :: MonadCompile m => [String] -> Text -> m GHCParsedCode
ghcParseCode extraExts src = do
sourceMode <- gets compileMode
let buffer = GHC.stringToStringBuffer (T.unpack src)
exts
| sourceMode == "codeworld" = codeworldModeExts ++ extraExts
| otherwise = extraExts
dflags = foldl' applyExtensionToFlags fakeDynFlags exts
dflagsWithPragmas <-
liftIO $
fromMaybe dflags <$> parsePragmasIntoDynFlags dflags "program.hs" buffer
let location = GHC.mkRealSrcLoc (GHC.mkFastString "program.hs") 1 1
state = GHC.mkPState dflagsWithPragmas buffer location
return $ case GHC.unP GHC.parseModule state of
GHC.POk _ (GHC.L _ mod) -> GHCParsed mod
GHC.PFailed _ -> GHCNoParse
fakeDynFlags :: GHC.DynFlags
fakeDynFlags = GHC.defaultDynFlags fakeSettings fakeLlvmConfig
fakePlatformMisc :: GHC.PlatformMisc
fakePlatformMisc = GHC.PlatformMisc
mempty
mempty
GHC.IntegerSimple
False
False
False
mempty
False
False
False
False
False
False
mempty
fakeToolSettings :: GHC.ToolSettings
fakeToolSettings = GHC.ToolSettings
True
True
True
True
True
mempty
mempty
mempty
mempty
mempty
mempty
mempty
mempty
mempty
mempty
mempty
mempty
mempty
mempty
mempty
mempty
mempty
mempty
mempty
mempty
mempty
(GHC.Fingerprint (read mempty) (read mempty))
mempty
mempty
mempty
mempty
mempty
mempty
mempty
mempty
mempty
mempty
mempty
mempty
fakeSettings :: GHC.Settings
fakeSettings =
GHC.Settings
{ GHC.sGhcNameVersion = GHC.GhcNameVersion "foo" "foo",
GHC.sFileSettings = GHC.FileSettings mempty mempty Nothing mempty mempty mempty,
GHC.sToolSettings = fakeToolSettings,
GHC.sTargetPlatform =
GHC.Platform
{ GHC.platformWordSize = GHC.PW8,
GHC.platformMini = GHC.PlatformMini GHC.ArchX86_64 GHC.OSUnknown,
GHC.platformUnregisterised = True,
GHC.platformHasGnuNonexecStack = True,
GHC.platformIsCrossCompiling = True,
GHC.platformHasSubsectionsViaSymbols = True
},
GHC.sPlatformConstants =
GHC.PlatformConstants
{ GHC.pc_DYNAMIC_BY_DEFAULT = False,
GHC.pc_WORD_SIZE = 8
},
GHC.sPlatformMisc = fakePlatformMisc
}
fakeLlvmConfig :: GHC.LlvmConfig
fakeLlvmConfig = GHC.LlvmConfig mempty mempty
parsePragmasIntoDynFlags ::
GHC.DynFlags ->
FilePath ->
GHC.StringBuffer ->
IO (Maybe GHC.DynFlags)
parsePragmasIntoDynFlags dflags f src =
GHC.handleGhcException (const $ return Nothing) $
GHC.handleSourceError (const $ return Nothing) $
do
let opts = GHC.getOptions dflags src f
(dflagsWithPragmas, _, _) <- GHC.parseDynamicFilePragma dflags opts
return $ Just dflagsWithPragmas
copyModuleWithTransform ::
MonadCompile m =>
FilePath ->
(GHC.HsModule GHC.GhcPs -> GHC.HsModule GHC.GhcPs) ->
m (Maybe FilePath)
copyModuleWithTransform f transform = do
src <- liftIO $ decodeUtf8 <$> B.readFile f
let savedPragmas = concatMap pragmasToSave (T.lines src)
parseResult <- ghcParseCode [] src
case parseResult of
GHCNoParse -> return Nothing
GHCParsed mod -> do
buildDir <- gets compileBuildDir
liftIO $ do
(out, h) <- openTempFile buildDir "imported.hs"
T.hPutStrLn h (T.unlines savedPragmas)
GHC.printForUser fakeDynFlags h GHC.neverQualify $ GHC.ppr (transform mod)
hClose h
return (Just out)
where
pragmasToSave :: Text -> [Text]
pragmasToSave ln =
[ decl :: Text
| [decl, name] <- ln =~ ("{-#[ \\t]+([A-Z]+)\\b.*#-}" :: Text),
not (name `elem` ["INLINE", "RULES"])
]
addDiagnostics :: MonadCompile m => [Diagnostic] -> m ()
addDiagnostics diags = modify $ \state ->
state
{ compileErrors = compileErrors state ++ diags,
compileStatus =
maximum
(compileStatus state : map (\(_, s, _) -> s) diags)
}
failCompile :: MonadCompile m => m ()
failCompile = do
modify $ \state ->
state
{ compileStatus = max CompileError (compileStatus state)
}
ifSucceeding :: MonadCompile m => m () -> m ()
ifSucceeding m = do
status <- gets compileStatus
when (status == CompileSuccess) m
withTimeout :: forall a. Int -> IO a -> IO (Maybe a)
withTimeout micros action = do
result :: MVar (Maybe (Either SomeException a)) <- newEmptyMVar
killer <- forkIO $ threadDelay micros >> putMVar result Nothing
runner <- forkIO $ do
try action >>= putMVar result . Just
r <- takeMVar result
killThread killer
killThread runner
sequence $ either throwM return <$> r
-- Runs a command, where if the thread terminates, the subprocess is automatically
-- killed.
runSync :: FilePath -> String -> [String] -> IO (ExitCode, Text)
runSync dir cmd args = mask $ \restore -> do
(Nothing, Just outh, Nothing, pid) <-
createProcess
(shell (intercalate " " (cmd : args) ++ " 2>&1"))
{ cwd = Just dir,
std_in = NoStream,
std_out = CreatePipe,
std_err = NoStream,
close_fds = True
}
let cleanup (e :: SomeException) = terminateProcess pid >> throwM e
handle cleanup $
restore $ do
result <- decodeUtf8 <$> B.hGetContents outh
exitCode <- waitForProcess pid
return (exitCode, result)
formatLocation :: SrcSpanInfo -> String
formatLocation spn@(SrcSpanInfo (SrcSpan fn l1 c1 l2 c2) _)
| spn == noSrcSpan = ""
| l1 /= l2 =
fn ++ ":(" ++ show l1 ++ "," ++ show c1 ++ ")-("
++ show l2
++ ","
++ show (max 1 (c2 - 1))
++ ")"
| c1 < c2 - 1 =
fn ++ ":" ++ show l1 ++ ":" ++ show c1 ++ "-"
++ show (max 1 (c2 - 1))
| otherwise = fn ++ ":" ++ show l1 ++ ":" ++ show c1
srcSpanFor :: FilePath -> Text -> Int -> Int -> SrcSpanInfo
srcSpanFor f src off len =
SrcSpanInfo (SrcSpan (takeFileName f) ln1 col1 ln2 col2) []
where
(_, ln1, col1) = T.foldl' next (off, 1, 1) pre
(_, ln2, col2) = T.foldl' next (len, ln1, col1) mid
(pre, post) = T.splitAt off src
mid = T.take len post
next (!n, !ln, !col) '\r' = (n - 1, ln, col)
next (!n, !ln, !col) '\n' = (n - 1, ln + 1, 1)
next (!n, !ln, !col) '\t' = (n - 1, ln, col + 8 - (col - 1) `mod` 8)
next (!n, !ln, !col) _ = (n - 1, ln, col + 1)