on 9d9e439 help sign in
elk/src/AST/Module.hs
166 lines 3.8 kB history raw
1module AST.Module
2 ( Interfaces
3 , Types, Aliases, ADTs
4 , AdtInfo, CanonicalAdt
5 , SourceModule, ValidModule, CanonicalModule, Optimized
6 , Module(..), Body(..)
7 , Header(..)
8 , Name, nameToString, nameIsNative
9 , Interface(..), toInterface
10 , UserImport, DefaultImport, ImportMethod(..)
11 ) where
13import Control.Applicative ((<$>),(<*>))
14import Data.Binary
15import qualified Data.List as List
16import qualified Data.Map as Map
18import qualified AST.Declaration as Decl
19import qualified AST.Expression.Canonical as Canonical
20import qualified AST.Expression.Optimized as Optimized
21import qualified AST.Type as Type
22import qualified AST.Variable as Var
23import qualified Docs.AST as Docs
24import qualified Elm.Compiler.Version as Compiler
25import qualified Reporting.Annotation as A
28-- HELPFUL TYPE ALIASES
30type Interfaces = Map.Map Name Interface
32type Types = Map.Map String Type.Canonical
33type Aliases = Map.Map String ([String], Type.Canonical)
34type ADTs = Map.Map String (AdtInfo String)
36type AdtInfo v = ( [String], [(v, [Type.Canonical])] )
37type CanonicalAdt = (Var.Canonical, AdtInfo Var.Canonical)
40-- MODULES
42type SourceModule =
43 Module
44 String
45 [UserImport]
46 (Var.Listing (A.Located Var.Value))
47 [Decl.SourceDecl]
50type ValidModule =
51 Module
52 String
53 ([DefaultImport], [UserImport])
54 (Var.Listing (A.Located Var.Value))
55 [Decl.ValidDecl]
58type CanonicalModule =
59 Module Docs.Centralized [Name] [Var.Value] (Body Canonical.Expr)
62type Optimized =
63 Module Docs.Centralized [Name] [Var.Value] (Body Optimized.Expr)
66data Module docs imports exports body = Module
67 { names :: Name
68 , path :: FilePath
69 , docs :: A.Located (Maybe docs)
70 , exports :: exports
71 , imports :: imports
72 , body :: body
73 }
76data Body expr = Body
77 { program :: expr
78 , types :: Types
79 , fixities :: [(Decl.Assoc, Int, String)]
80 , aliases :: Aliases
81 , datatypes :: ADTs
82 , ports :: [String]
83 }
86-- HEADERS
88{-| Basic info needed to identify modules and determine dependencies. -}
89data Header imports = Header
90 { _names :: Name
91 , _docs :: A.Located (Maybe String)
92 , _exports :: Var.Listing (A.Located Var.Value)
93 , _imports :: imports
94 }
97type Name = [String] -- must be non-empty
100nameToString :: Name -> String
101nameToString =
102 List.intercalate "."
105nameIsNative :: Name -> Bool
106nameIsNative name =
107 case name of
108 "Native" : _ -> True
109 _ -> False
112-- IMPORTs
114type UserImport = A.Located (Name, ImportMethod)
117type DefaultImport = (Name, ImportMethod)
120data ImportMethod = ImportMethod
121 { alias :: Maybe String
122 , exposedVars :: !(Var.Listing Var.Value)
123 }
126-- INTERFACES
128{-| Key facts about a module, used when reading info from .elmi files. -}
129data Interface = Interface
130 { iVersion :: String
131 , iExports :: [Var.Value]
132 , iTypes :: Types
133 , iImports :: [Name]
134 , iAdts :: ADTs
135 , iAliases :: Aliases
136 , iFixities :: [(Decl.Assoc, Int, String)]
137 , iPorts :: [String]
138 }
141toInterface :: CanonicalModule -> Interface
142toInterface modul =
143 let body' = body modul in
144 Interface
145 { iVersion = Compiler.version
146 , iExports = exports modul
147 , iTypes = types body'
148 , iImports = imports modul
149 , iAdts = datatypes body'
150 , iAliases = aliases body'
151 , iFixities = fixities body'
152 , iPorts = ports body'
153 }
156instance Binary Interface where
157 get = Interface <$> get <*> get <*> get <*> get <*> get <*> get <*> get <*> get
158 put modul = do
159 put (iVersion modul)
160 put (iExports modul)
161 put (iTypes modul)
162 put (iImports modul)
163 put (iAdts modul)
164 put (iAliases modul)
165 put (iFixities modul)
166 put (iPorts modul)