1{-# LANGUAGE RecordWildCards #-}
2{-# LANGUAGE PatternGuards #-}
3{-# LANGUAGE TemplateHaskell #-}
6-- This module generates code for \"externs\" files, i.e. files containing only foreign import declarations.
8module Language.PureScript.Externs
12 , ExternsDeclaration(..)
14 , applyExternsFileToEnvironment
20import Data.List (find, foldl')
21import Data.Maybe (mapMaybe, maybeToList, fromMaybe)
22import Data.Foldable (fold)
23import Data.Version (showVersion)
26import qualified Data.Map as M
28import Language.PureScript.Crash
29import Language.PureScript.AST
30import Language.PureScript.Environment
31import Language.PureScript.Names
32import Language.PureScript.Types
33import Language.PureScript.Kinds
34import Language.PureScript.TypeClassDictionaries
36import Paths_purescript as Paths
38-- | The data which will be serialized to an externs file
39data ExternsFile = ExternsFile
41 -- | The externs version
44 , efModuleName :: ModuleName
45 -- | List of module exports
46 , efExports :: [DeclarationRef]
47 -- | List of module imports
48 , efImports :: [ExternsImport]
49 -- | List of operators and their fixities
50 , efFixities :: [ExternsFixity]
51 -- | List of type and value declaration
52 , efDeclarations :: [ExternsDeclaration]
53 } deriving (Show, Read)
55-- | A module import in an externs file
56data ExternsImport = ExternsImport
58 -- | The imported module
59 eiModule :: ModuleName
60 -- | The import type: regular, qualified or hiding
61 , eiImportType :: ImportDeclarationType
62 -- | The imported-as name, for qualified imports
63 , eiImportedAs :: Maybe ModuleName
64 } deriving (Show, Read)
66-- | A fixity declaration in an externs file
67data ExternsFixity = ExternsFixity
69 -- | The associativity of the operator
70 efAssociativity :: Associativity
71 -- | The precedence level of the operator
72 , efPrecedence :: Precedence
73 -- | The operator symbol
74 , efOperator :: String
75 -- | The value the operator is an alias for
76 , efAlias :: Maybe (Qualified Ident)
77 } deriving (Show, Read)
79-- | A type or value declaration appearing in an externs file
80data ExternsDeclaration =
81 -- | A type declaration
83 { edTypeName :: ProperName 'TypeName
85 , edTypeDeclarationKind :: TypeKind
89 { edTypeSynonymName :: ProperName 'TypeName
90 , edTypeSynonymArguments :: [(String, Maybe Kind)]
91 , edTypeSynonymType :: Type
93 -- | A data construtor
95 { edDataCtorName :: ProperName 'ConstructorName
96 , edDataCtorOrigin :: DataDeclType
97 , edDataCtorTypeCtor :: ProperName 'TypeName
98 , edDataCtorType :: Type
99 , edDataCtorFields :: [Ident]
101 -- | A value declaration
103 { edValueName :: Ident
104 , edValueType :: Type
106 -- | A type class declaration
108 { edClassName :: ProperName 'ClassName
109 , edClassTypeArguments :: [(String, Maybe Kind)]
110 , edClassMembers :: [(Ident, Type)]
111 , edClassConstraints :: [Constraint]
113 -- | An instance declaration
115 { edInstanceClassName :: Qualified (ProperName 'ClassName)
116 , edInstanceName :: Ident
117 , edInstanceTypes :: [Type]
118 , edInstanceConstraints :: Maybe [Constraint]
120 deriving (Show, Read)
122-- | Convert an externs file back into a module
123applyExternsFileToEnvironment :: ExternsFile -> Environment -> Environment
124applyExternsFileToEnvironment ExternsFile{..} = flip (foldl' applyDecl) efDeclarations
126 applyDecl :: Environment -> ExternsDeclaration -> Environment
127 applyDecl env (EDType pn kind tyKind) = env { types = M.insert (qual pn) (kind, tyKind) (types env) }
128 applyDecl env (EDTypeSynonym pn args ty) = env { typeSynonyms = M.insert (qual pn) (args, ty) (typeSynonyms env) }
129 applyDecl env (EDDataConstructor pn dTy tNm ty nms) = env { dataConstructors = M.insert (qual pn) (dTy, tNm, ty, nms) (dataConstructors env) }
130 applyDecl env (EDValue ident ty) = env { names = M.insert (efModuleName, ident) (ty, External, Defined) (names env) }
131 applyDecl env (EDClass pn args members cs) = env { typeClasses = M.insert (qual pn) (args, members, cs) (typeClasses env) }
132 applyDecl env (EDInstance className ident tys cs) = env { typeClassDictionaries = updateMap (updateMap (M.insert (qual ident) dict) className) (Just efModuleName) (typeClassDictionaries env) }
134 dict :: TypeClassDictionaryInScope
135 dict = TypeClassDictionaryInScope (qual ident) [] className tys cs
137 updateMap :: (Ord k, Monoid a) => (a -> a) -> k -> M.Map k a -> M.Map k a
138 updateMap f = M.alter (Just . f . fold)
140 qual :: a -> Qualified a
141 qual = Qualified (Just efModuleName)
143-- | Generate an externs file for all declarations in a module
144moduleToExternsFile :: Module -> Environment -> ExternsFile
145moduleToExternsFile (Module _ _ _ _ Nothing) _ = internalError "moduleToExternsFile: module exports were not elaborated"
146moduleToExternsFile (Module _ _ mn ds (Just exps)) env = ExternsFile{..}
148 efVersion = showVersion Paths.version
151 efImports = mapMaybe importDecl ds
152 efFixities = mapMaybe fixityDecl ds
153 efDeclarations = concatMap toExternsDeclaration efExports
155 fixityDecl :: Declaration -> Maybe ExternsFixity
156 fixityDecl (FixityDeclaration (Fixity assoc prec) op alias) = fmap (const (ExternsFixity assoc prec op alias)) (find exportsOp exps)
158 exportsOp :: DeclarationRef -> Bool
159 exportsOp (PositionedDeclarationRef _ _ r) = exportsOp r
160 exportsOp (ValueRef ident') = ident' == Op op
162 fixityDecl (PositionedDeclaration _ _ d) = fixityDecl d
163 fixityDecl _ = Nothing
165 importDecl :: Declaration -> Maybe ExternsImport
166 importDecl (ImportDeclaration m mt qmn _) = Just (ExternsImport m mt qmn)
167 importDecl (PositionedDeclaration _ _ d) = importDecl d
168 importDecl _ = Nothing
170 toExternsDeclaration :: DeclarationRef -> [ExternsDeclaration]
171 toExternsDeclaration (PositionedDeclarationRef _ _ r) = toExternsDeclaration r
172 toExternsDeclaration (TypeRef pn dctors) =
173 case Qualified (Just mn) pn `M.lookup` types env of
174 Nothing -> internalError "toExternsDeclaration: no kind in toExternsDeclaration"
175 Just (kind, TypeSynonym)
176 | Just (args, synTy) <- Qualified (Just mn) pn `M.lookup` typeSynonyms env -> [ EDType pn kind TypeSynonym, EDTypeSynonym pn args synTy ]
177 Just (kind, ExternData) -> [ EDType pn kind ExternData ]
178 Just (kind, tk@(DataType _ tys)) ->
179 EDType pn kind tk : [ EDDataConstructor dctor dty pn ty args
180 | dctor <- fromMaybe (map fst tys) dctors
181 , (dty, _, ty, args) <- maybeToList (Qualified (Just mn) dctor `M.lookup` dataConstructors env)
183 _ -> internalError "toExternsDeclaration: Invalid input"
184 toExternsDeclaration (ValueRef ident)
185 | Just (ty, _, _) <- (mn, ident) `M.lookup` names env
186 = [ EDValue ident ty ]
187 toExternsDeclaration (TypeClassRef className)
188 | Just (args, members, implies) <- Qualified (Just mn) className `M.lookup` typeClasses env
189 , Just (kind, TypeSynonym) <- Qualified (Just mn) (coerceProperName className) `M.lookup` types env
190 , Just (_, synTy) <- Qualified (Just mn) (coerceProperName className) `M.lookup` typeSynonyms env
191 = [ EDType (coerceProperName className) kind TypeSynonym
192 , EDTypeSynonym (coerceProperName className) args synTy
193 , EDClass className args members implies
195 toExternsDeclaration (TypeInstanceRef ident)
196 = [ EDInstance tcdClassName ident tcdInstanceTypes tcdDependencies
197 | m1 <- maybeToList (M.lookup (Just mn) (typeClassDictionaries env))
199 , TypeClassDictionaryInScope{..} <- maybeToList (M.lookup (Qualified (Just mn) ident) m2)
201 toExternsDeclaration _ = []
203$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''ExternsImport)
204$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''ExternsFixity)
205$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''ExternsDeclaration)
206$(deriveJSON (defaultOptions { sumEncoding = ObjectWithSingleField }) ''ExternsFile)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list