|
| 1 | +{-# LANGUAGE FlexibleContexts #-} |
1 | 2 | {-# LANGUAGE FlexibleInstances #-} |
2 | 3 | {-# LANGUAGE GADTs #-} |
3 | 4 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} |
4 | 5 | {-# LANGUAGE MultiParamTypeClasses #-} |
| 6 | +{-# LANGUAGE RankNTypes #-} |
| 7 | +{-# LANGUAGE TypeApplications #-} |
5 | 8 | {-# LANGUAGE TypeOperators #-} |
6 | 9 | {-# LANGUAGE UndecidableInstances #-} |
7 | 10 | module Analysis.Analysis.Exception |
8 | 11 | ( Exception(..) |
9 | 12 | , ExcSet(..) |
| 13 | +, exceptionTracing |
10 | 14 | , fromExceptions |
11 | 15 | , var |
12 | 16 | , exc |
13 | 17 | -- * Exception tracing analysis |
14 | 18 | , ExcC(..) |
15 | 19 | ) where |
16 | 20 |
|
| 21 | +import qualified Analysis.Carrier.Statement.State as A |
| 22 | +import qualified Analysis.Carrier.Store.Monovariant as A |
17 | 23 | import Analysis.Effect.Domain |
| 24 | +import Analysis.Effect.Env (Env) |
| 25 | +import Analysis.Effect.Store |
| 26 | +import Analysis.File |
| 27 | +import Analysis.FlowInsensitive (cacheTerm, convergeTerm) |
| 28 | +import Analysis.Module |
18 | 29 | import Analysis.Name |
19 | 30 | import Control.Algebra |
20 | 31 | import Control.Applicative (Alternative (..)) |
| 32 | +import Control.Effect.Labelled |
| 33 | +import Control.Effect.State |
21 | 34 | import qualified Data.Foldable as Foldable |
| 35 | +import Data.Function (fix) |
22 | 36 | import qualified Data.Set as Set |
| 37 | +import qualified Data.Text as Text |
23 | 38 |
|
24 | 39 | -- | Names of exceptions thrown in the guest language and recorded by this analysis. |
25 | 40 | -- |
@@ -47,6 +62,35 @@ exc :: Exception -> ExcSet |
47 | 62 | exc e = ExcSet mempty (Set.singleton e) |
48 | 63 |
|
49 | 64 |
|
| 65 | +exceptionTracing |
| 66 | + :: Ord term |
| 67 | + => ( forall sig m |
| 68 | + . (Has (Env A.MAddr) sig m, HasLabelled Store (Store A.MAddr ExcSet) sig m, Has (Dom ExcSet) sig m, Has A.Statement sig m) |
| 69 | + => (term -> m ExcSet) |
| 70 | + -> (term -> m ExcSet) ) |
| 71 | + -> [File term] |
| 72 | + -> (A.MStore ExcSet, [File (Module ExcSet)]) |
| 73 | +exceptionTracing eval = A.runFiles (runFile eval) |
| 74 | + |
| 75 | +runFile |
| 76 | + :: ( Has (State (A.MStore ExcSet)) sig m |
| 77 | + , Ord term ) |
| 78 | + => ( forall sig m |
| 79 | + . (Has (Env A.MAddr) sig m, HasLabelled Store (Store A.MAddr ExcSet) sig m, Has (Dom ExcSet) sig m, Has A.Statement sig m) |
| 80 | + => (term -> m ExcSet) |
| 81 | + -> (term -> m ExcSet) ) |
| 82 | + -> File term |
| 83 | + -> m (File (Module ExcSet)) |
| 84 | +runFile eval = traverse run where |
| 85 | + run |
| 86 | + = A.runStatement result |
| 87 | + . A.runEnv @ExcSet |
| 88 | + . convergeTerm (A.runStore @ExcSet . runExcC . fix (cacheTerm . eval)) |
| 89 | + result msgs sets = do |
| 90 | + let set = Foldable.fold sets |
| 91 | + imports = Set.fromList (map (\ (A.Import components) -> name (Text.intercalate (Text.pack ".") (Foldable.toList components))) msgs) |
| 92 | + pure (Module (const set) imports mempty (freeVariables set)) |
| 93 | + |
50 | 94 | newtype ExcC m a = ExcC { runExcC :: m a } |
51 | 95 | deriving (Alternative, Applicative, Functor, Monad) |
52 | 96 |
|
|
0 commit comments