summaryrefslogtreecommitdiff
path: root/src/Language/Fiddle/Compiler/ImportResolution.hs
blob: a9c4c8e41b52dc4f30f9c1068a2f40b81dec541a (plain) (blame)
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
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralisedNewtypeDeriving #-}

module Language.Fiddle.Compiler.ImportResolution
  ( resolveImports,
    getImportResolutionState,
    importResolutionPhase,
  )
where

import qualified Codec.Compression.GZip as GZip
import Control.Arrow (Arrow (second))
import Control.Monad (filterM, when)
import Control.Monad.Identity (Identity)
import Control.Monad.State (put)
import Control.Monad.Trans.Maybe (MaybeT (MaybeT, runMaybeT))
import Control.Monad.Writer.Lazy (MonadTrans (lift), MonadWriter (tell), WriterT (..), execWriterT)
import Data.Aeson (eitherDecode, encode)
import qualified Data.ByteString.Lazy as BL
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Maybe (fromMaybe, isNothing)
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Typeable
import Language.Fiddle.Ast
import Language.Fiddle.Compiler
import Language.Fiddle.Compiler.Expansion ()
import Language.Fiddle.Internal.UnitInterface
import Language.Fiddle.Types
import Options.Applicative
import System.Directory
import System.FilePath
import Text.Printf (printf)

data Flags = Flags
  { importDirectories :: [FilePath],
    interfaceDirectory :: Maybe FilePath
  }

parseFlags :: Parser Flags
parseFlags =
  Flags
    <$> many
      ( strOption
          ( long "import"
              <> short 'I'
              <> metavar "DIRECTORY"
              <> help "Directory to add to the import search path"
          )
      )
    <*> optional
      ( strOption
          ( long "intf-dir"
              <> short 'i'
              <> metavar "INTF_DIR"
              <> help "Directory to dump interface files to."
          )
      )

importResolutionPhase ::
  ( FilePath ->
    IO ([Diagnostic], [Artifact], Maybe (TreeType FiddleUnit Parsed))
  ) ->
  ( TreeType FiddleUnit Parsed ->
    IO ([Diagnostic], [Artifact], Maybe (TreeType FiddleUnit Checked))
  ) ->
  CompilationPhase CurrentStage ImportsResolved
importResolutionPhase parseFile compileToChecked =
  CompilationPhase parseFlags (getImportResolutionState parseFile compileToChecked) resolveImports

type GlobalState = Bool

type LocalState = ResolvedImports

type M = Compile GlobalState

type Annot = Commented SourceSpan

newtype ResolvedImports = ResolvedImports
  { importMap :: Map Text ([Diagnostic], Maybe UnitInterface)
  }
  deriving newtype (Semigroup, Monoid)

type CurrentStage = Parsed

type I = Identity

instance CompilationStage CurrentStage where
  type StageAfter CurrentStage = ImportsResolved
  type StageMonad CurrentStage = M
  type StageState CurrentStage = LocalState
  type StageFunctor CurrentStage = Identity
  type StageAnnotation CurrentStage = Annot

resolveImports ::
  Flags ->
  ResolvedImports ->
  FiddleUnit CurrentStage I Annot ->
  Compile () (FiddleUnit ImportsResolved I Annot)
resolveImports _ localState t = do
  (b, a) <- subCompile False $ advanceStage localState t
  if b
    then compilationFailure
    else return a

-- | Mark the current compilation as failed, but allows the import resolution to
-- continue in order to allow all import failures to be reported at the same
-- time rather than as piecemeal.
markFatal :: Compile Bool ()
markFatal = put True

deriving instance AdvanceStage CurrentStage ObjTypeBody

deriving instance AdvanceStage CurrentStage DeferredRegisterBody

deriving instance AdvanceStage CurrentStage RegisterBody

deriving instance AdvanceStage CurrentStage AnonymousBitsType

deriving instance AdvanceStage CurrentStage BitType

deriving instance AdvanceStage CurrentStage (ConstExpression u)

deriving instance AdvanceStage CurrentStage EnumBody

deriving instance AdvanceStage CurrentStage EnumConstantDecl

deriving instance AdvanceStage CurrentStage RegisterBitsDecl

deriving instance AdvanceStage CurrentStage PackageBody

deriving instance AdvanceStage CurrentStage ObjTypeDecl

deriving instance AdvanceStage CurrentStage FiddleUnit

deriving instance AdvanceStage CurrentStage (Expression u)

deriving instance AdvanceStage CurrentStage RegisterBitsTypeRef

deriving instance AdvanceStage CurrentStage ObjType

deriving instance (AdvanceStage CurrentStage t) => AdvanceStage CurrentStage (Directed t)

deriving instance AdvanceStage CurrentStage FiddleDecl

instance AdvanceStage CurrentStage ImportStatement where
  advanceStage s (ImportStatement path list _ a) = do
    let what = Map.lookup path (importMap s)
        empty = UnitInterface mempty mempty

    v <- case what of
      Nothing -> do
        emitDiagnosticError "Failed to lookup imports (This is a bug)" a
        markFatal
        return empty
      Just (diags, val) -> do
        tell diags
        when (isNothing val) markFatal
        return $ fromMaybe empty val

    return $ ImportStatement path list (Present v) a

getImportResolutionState ::
  ( FilePath ->
    IO ([Diagnostic], [Artifact], Maybe (TreeType FiddleUnit Parsed))
  ) ->
  ( TreeType FiddleUnit Parsed ->
    IO ([Diagnostic], [Artifact], Maybe (TreeType FiddleUnit Checked))
  ) ->
  Flags ->
  FiddleUnit CurrentStage Identity Annot ->
  IO ([Diagnostic], Maybe ResolvedImports)
getImportResolutionState parseFile compileToChecked flags unit = do
  fmap
    ( lookForFailures
        . second Just
    )
    $ execWriterT
    $ walk doWalk unit ()
  where
    doWalk :: forall t'. (Walk t', Typeable t') => t' Identity Annot -> () -> WriterT ([Diagnostic], ResolvedImports) IO (WalkContinuation ())
    doWalk u () = do
      case () of
        ()
          | Just
              (ImportStatement {importPath = path, importStatementAnnot = (unCommented -> a)}) <-
              castTS u -> do
              (diagnostics, _, unitInterface) <-
                lift $
                  ioGetImportInterface a flags (Text.unpack path)

              tell
                ( [],
                  ResolvedImports $ Map.singleton path (diagnostics, unitInterface)
                )
        _ -> return ()
      return $ Continue ()

    castTS ::
      ( Typeable t',
        Typeable t,
        Typeable f,
        Typeable a
      ) =>
      t' f a ->
      Maybe (t CurrentStage f a)
    castTS = cast

    lookForFailures :: ([Diagnostic], Maybe a) -> ([Diagnostic], Maybe a)
    lookForFailures (diags, a) = do
      if any (\(Diagnostic e _ _) -> e == Error) diags
        then (diags, Nothing)
        else (diags, a)

    findInterfaceFile :: Maybe FilePath -> FilePath -> FilePath -> IO FilePath
    findInterfaceFile Nothing foundPath _ = return $ interfaceFile foundPath
    findInterfaceFile (Just dir) _ p = return $ interfaceFile $ dir </> p

    ioGetImportInterface :: SourceSpan -> Flags -> FilePath -> IO ([Diagnostic], [Artifact], Maybe UnitInterface)
    ioGetImportInterface srcSpan flags fp = runCompl $ do
      let imports = importDirectories flags
          intfDir = interfaceDirectory flags
      path <- findFileInImportPath srcSpan imports fp
      intf <- lift2 $ findInterfaceFile intfDir path fp
      -- let intf = interfaceFile path
      valid <- lift2 $ interfaceFileValid path intf

      let doFullCompile = do
            parsed <- bump (parseFile path)
            unitInterface <- addDependency path . unwrap . fiddleUnitInterface <$> bump (compileToChecked parsed)
            lift2 $ writeInterfaceFile intf unitInterface
            return unitInterface

      if valid
        then do
          e <- lift2 (readInterfaceFile intf)
          case e of
            Right val -> do
              needFullRecompile <- lift2 $ checkNeedFullRecompile intf val
              if needFullRecompile
                then doFullCompile
                else return val
            Left err -> do
              tell ([Diagnostic Warning "Doing recompile" srcSpan], [])
              tell ([Diagnostic Warning err srcSpan], [])
              doFullCompile
        else doFullCompile

    addDependency path unitInterface =
      unitInterface {dependencies = path : dependencies unitInterface}

    interfaceFile filePath = takeDirectory filePath </> takeBaseName filePath <.> "fdi"

    checkNeedFullRecompile intfFile (UnitInterface {dependencies = dependencies}) =
      anyM
        ( \depfile -> do
            timeDep <- getModificationTime depfile
            timeIntf <- getModificationTime intfFile
            return (timeIntf < timeDep)
        )
        dependencies

    readInterfaceFile intfile =
      eitherDecode . GZip.decompress <$> BL.readFile intfile

    writeInterfaceFile intfile val = do
      createDirectoryIfMissing True (takeDirectory intfile)
      BL.writeFile intfile $ GZip.compress (encode val)

    interfaceFileValid :: FilePath -> FilePath -> IO Bool
    interfaceFileValid originalPath intfPath = do
      exists <- doesFileExist intfPath
      if exists
        then do
          timeOrig <- getModificationTime originalPath
          timeIntf <- getModificationTime intfPath
          return (timeIntf > timeOrig)
        else return False

    findFileInImportPath :: SourceSpan -> [FilePath] -> FilePath -> Compl FilePath
    findFileInImportPath sourceSpan paths path = do
      canonicalPaths <- lift2 $ mapM (canonicalizePath . (++ ("/" ++ path))) paths
      realPaths <- lift2 $ filterM doesFileExist canonicalPaths

      case realPaths of
        [] -> do
          lift $ tell ([Diagnostic Error (printf "Cannot find %s on path" path) sourceSpan], [])
          MaybeT (return Nothing)
        (a : _) -> return a

bump :: IO ([Diagnostic], [Artifact], Maybe a) -> Compl a
bump x = do
  (diags, artifacts, ma) <- lift2 x
  lift $ tell (diags, artifacts)
  MaybeT (return ma)

lift2 :: (Monad m, MonadTrans t0, MonadTrans t1) => m a -> t0 (t1 m) a
lift2 = lift . lift

runCompl :: Compl a -> IO ([Diagnostic], [Artifact], Maybe a)
runCompl c = (\(x, (y, z)) -> (y, z, x)) <$> runWriterT (runMaybeT c)

type Compl a = MaybeT (WriterT ([Diagnostic], [Artifact]) IO) a

allM :: (Monad m) => (a -> m Bool) -> [a] -> m Bool
allM _ [] = return True
allM fn (a : as) = do
  b <- fn a
  if b then allM fn as else return False

anyM :: (Monad m) => (a -> m Bool) -> [a] -> m Bool
anyM _ [] = return True
anyM fn (a : as) = do
  b <- fn a
  if b then return True else anyM fn as