|
| 1 | +{-# LANGUAGE FlexibleContexts, RecordWildCards #-} |
| 2 | +module Analysis.ImportGraph |
| 3 | +( ImportGraph |
| 4 | +, importGraph |
| 5 | +, importGraphAnalysis |
| 6 | +) where |
| 7 | + |
| 8 | +import Analysis.FlowInsensitive |
| 9 | +import Control.Applicative (Alternative(..)) |
| 10 | +import Control.Effect |
| 11 | +import Control.Effect.Fail |
| 12 | +import Control.Effect.Fresh |
| 13 | +import Control.Effect.Reader |
| 14 | +import Control.Effect.State |
| 15 | +import Data.File |
| 16 | +import Data.Foldable (fold) |
| 17 | +import Data.Function (fix) |
| 18 | +import Data.List.NonEmpty (nonEmpty) |
| 19 | +import Data.Loc |
| 20 | +import qualified Data.Map as Map |
| 21 | +import qualified Data.Set as Set |
| 22 | +import Prelude hiding (fail) |
| 23 | +import qualified Semantic.Core as Core |
| 24 | +import Semantic.Eval |
| 25 | +import Semantic.Name |
| 26 | + |
| 27 | +type ImportGraph = Map.Map FilePath (Set.Set FilePath) |
| 28 | + |
| 29 | +data Value = Value |
| 30 | + { valueSemi :: Semi |
| 31 | + , valueGraph :: ImportGraph |
| 32 | + } |
| 33 | + deriving (Eq, Ord, Show) |
| 34 | + |
| 35 | +instance Semigroup Value where |
| 36 | + Value _ g1 <> Value _ g2 = Value Abstract (Map.unionWith (<>) g1 g2) |
| 37 | + |
| 38 | +instance Monoid Value where |
| 39 | + mempty = Value Abstract mempty |
| 40 | + |
| 41 | +data Semi |
| 42 | + = Closure Loc Name Core.Core Name |
| 43 | + -- FIXME: Bound String values. |
| 44 | + | String String |
| 45 | + | Abstract |
| 46 | + deriving (Eq, Ord, Show) |
| 47 | + |
| 48 | + |
| 49 | +importGraph :: [File Core.Core] -> (Heap Value, [File (Either (Loc, String) Value)]) |
| 50 | +importGraph |
| 51 | + = run |
| 52 | + . runFresh |
| 53 | + . runNaming (Root "import-graph") |
| 54 | + . runHeap |
| 55 | + . traverse runFile |
| 56 | + |
| 57 | +runFile :: ( Carrier sig m |
| 58 | + , Effect sig |
| 59 | + , Member Fresh sig |
| 60 | + , Member (Reader FrameId) sig |
| 61 | + , Member (State (Heap Value)) sig |
| 62 | + ) |
| 63 | + => File Core.Core |
| 64 | + -> m (File (Either (Loc, String) Value)) |
| 65 | +runFile file = traverse run file |
| 66 | + where run = runReader (fileLoc file) |
| 67 | + . runFailWithLoc |
| 68 | + . fmap fold |
| 69 | + . convergeTerm (fix (cacheTerm . eval importGraphAnalysis)) |
| 70 | + |
| 71 | +-- FIXME: decompose into a product domain and two atomic domains |
| 72 | +importGraphAnalysis :: ( Alternative m |
| 73 | + , Carrier sig m |
| 74 | + , Member (Reader FrameId) sig |
| 75 | + , Member (Reader Loc) sig |
| 76 | + , Member (State (Heap Value)) sig |
| 77 | + , MonadFail m |
| 78 | + ) |
| 79 | + => Analysis Name Value m |
| 80 | +importGraphAnalysis = Analysis{..} |
| 81 | + where alloc = pure |
| 82 | + bind _ _ = pure () |
| 83 | + lookupEnv = pure . Just |
| 84 | + deref addr = gets (Map.lookup addr) >>= maybe (pure Nothing) (foldMapA (pure . Just)) . nonEmpty . maybe [] Set.toList |
| 85 | + assign addr ty = modify (Map.insertWith (<>) addr (Set.singleton ty)) |
| 86 | + abstract _ name body = do |
| 87 | + loc <- ask |
| 88 | + FrameId parentAddr <- ask |
| 89 | + pure (Value (Closure loc name body parentAddr) mempty) |
| 90 | + apply eval (Value (Closure loc name body _) _) a = local (const loc) $ do |
| 91 | + addr <- alloc name |
| 92 | + assign addr a |
| 93 | + bind name addr |
| 94 | + eval body |
| 95 | + apply _ f _ = fail $ "Cannot coerce " <> show f <> " to function" |
| 96 | + unit = pure mempty |
| 97 | + bool _ = pure mempty |
| 98 | + asBool _ = pure True <|> pure False |
| 99 | + string s = pure (Value (String s) mempty) |
| 100 | + asString (Value (String s) _) = pure s |
| 101 | + asString _ = pure "" |
| 102 | + frame = pure mempty |
| 103 | + edge Core.Import (Path to) = do |
| 104 | + Loc{locPath=from} <- ask |
| 105 | + () <$ pure (Value Abstract (Map.singleton from (Set.singleton to))) |
| 106 | + edge _ _ = pure () |
| 107 | + _ ... m = m |
0 commit comments