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
--------------------------------------------------------------------------------
--- This module contains helper functions for dealing with AbstractCurry. In
--- particular, it contains functions that can read modules from a package and
--- its dependencies with all dependencies available to the Curry frontend.
--------------------------------------------------------------------------------

module CPM.AbstractCurry
  ( loadPathForPackage
  , readAbstractCurryFromPackagePath
  , readAbstractCurryFromDeps
  , transformAbstractCurryInDeps
  , applyModuleRenames
  ) where

import Data.List           ( intercalate, nub )
import System.FilePath     ( (</>), (<.>), takeFileName, replaceExtension )
import System.Process

import AbstractCurry.Files  ( readAbstractCurryFile, writeAbstractCurryFile )
import AbstractCurry.Pretty ( showCProg )
import AbstractCurry.Select ( imports )
import AbstractCurry.Transform
import AbstractCurry.Types
import System.CurryPath     ( sysLibPath, inCurrySubdir, modNameToPath
                            , inCurrySubdirModule, lookupModuleSource )
import System.FrontendExec  ( FrontendTarget (..), FrontendParams (..)
                            , defaultParams, callFrontendWithParams
                            , setQuiet, setFullPath )

import CPM.ErrorLogger
import qualified CPM.PackageCache.Runtime as RuntimeCache
import CPM.Package (Package, loadPackageSpec, sourceDirsOf)

--- Returns the load path for a package stored in some directory
--- w.r.t. the dependent packages.
---
--- @param - pkg - the package
--- @param - pkgDir - the directory containing this package
--- @param - deps - the resolved dependencies of the package
--- @return the full load path for modules in the package or dependent packages
loadPathForPackage :: Package -> String -> [Package] -> [String]
loadPathForPackage pkg pkgDir deps =
  (map (pkgDir </>) (sourceDirsOf pkg) ++
  RuntimeCache.dependencyPathsSeparate deps pkgDir)

--- Returns the full load path for a package stored in some directory.
---
--- @param - pkg - the package
--- @param - pkgDir - the directory containing this package
--- @param - deps - the resolved dependencies of the package
--- @return the full load path for modules in the package or dependent packages
fullLoadPathForPackage :: Package -> String -> [Package] -> [String]
fullLoadPathForPackage pkg pkgDir deps =
  loadPathForPackage pkg pkgDir deps ++ sysLibPath
  -- here we assume that the system libs are identical for each Curry system

--- Reads an AbstractCurry module from a package.
---
--- @param - dir the package's directory
--- @param - deps the resolved dependencies of the package
--- @param - mod the module to read
readAbstractCurryFromPackagePath :: Package -> String -> [Package] -> String
                                 -> IO CurryProg
readAbstractCurryFromPackagePath pkg pkgDir deps modname = do
  let loadPath = fullLoadPathForPackage pkg pkgDir deps
  let params = setQuiet True (setFullPath loadPath defaultParams)
  callFrontendWithParams ACY params modname
  src <- lookupModuleSource loadPath modname
  let acyName = case src of
        Nothing -> error $ "Module not found: " ++ modname
        Just (_, file) -> replaceExtension (inCurrySubdirModule modname file) ".acy"
  readAbstractCurryFile acyName >>= return . addPrimTypes
 where
  -- work-around for missing Prelude.Char|Int|Float declarations:
  addPrimTypes p@(CurryProg mname imports dfltdecl clsdecls instdecls
                            typedecls funcdecls opdecls)
   | mname == pre && primType "Int" `notElem` typedecls
   = CurryProg mname imports dfltdecl clsdecls instdecls
               (map primType ["Int","Float","Char"] ++ typedecls)
               funcdecls opdecls
   | otherwise = p
   where pre = "Prelude"
         primType n = CType ("Prelude",n) Public [] [] []

--- Reads an AbstractCurry module from a package or one of its dependencies.
---
--- @param dir - the package's directory
--- @param deps - the resolved dependencies of the package
--- @param mod - the module to read
readAbstractCurryFromDeps :: String -> [Package] -> String -> ErrorLogger CurryProg
readAbstractCurryFromDeps pkgDir deps modname = do
  pkg <- loadPackageSpec pkgDir
  liftIOEL $ readAbstractCurryFromPackagePath pkg pkgDir deps modname

--- Applies a transformation function to a module from a package or one of its
--- dependencies and writes the modified module to a file in Curry form.
---
--- @param dir - the package's directory
--- @param deps - the resolved dependencies of the package
--- @param f - the transformation function
--- @param mod - the module to transform
--- @param dest - the destination file for the transformed module
transformAbstractCurryInDeps :: String -> [Package] -> (CurryProg -> CurryProg)
                             -> String -> String -> ErrorLogger ()
transformAbstractCurryInDeps pkgDir deps transform modname destFile = do
  acy <- readAbstractCurryFromDeps pkgDir deps modname
  liftIOEL $ writeFile destFile $ showCProg (transform acy)

--- Renames all references to some modules in a Curry program.
---
--- @param mods - a map from old to new module names
--- @param prog - the program to modify
applyModuleRenames :: [(String, String)] -> CurryProg -> CurryProg
applyModuleRenames names prog =
  updCProg maybeRename (map maybeRename) id id id id id id
           (updQNamesInCProg rnm prog)
 where
  maybeRename n = case lookup n names of
    Just n' -> n'
    Nothing -> n
  rnm mn@(mod, n) = case lookup mod names of
    Just mod' -> (mod', n)
    Nothing   -> mn