@@ -154,6 +154,7 @@ data TopLevel
154154 | HFunDef (FunDef Var Exp0 )
155155 | HMain (Maybe (Exp0 , Ty0 ))
156156 | HInline Var
157+ | MemLayoutTy TyCon MemoryLayout
157158 deriving (Show , Eq )
158159
159160type TopTyEnv = TyEnv Var TyScheme
@@ -170,7 +171,8 @@ desugarModule cfg pstate_ref import_route dir (Module _ head_mb _pragmas imports
170171 imported_progs :: [PassM Prog0 ] <- mapM (processImport cfg pstate_ref (mod_name : import_route) dir) imports
171172 let prog = do
172173 toplevels <- catMaybes <$> mapM (collectTopLevel type_syns funtys) decls
173- let (defs,_vars,funs,inlines,main) = foldr classify init_acc toplevels
174+ let (defs,_vars,funs,inlines,main, memlayouts) = foldr classify init_acc toplevels
175+ defs' = updateMemoryLayout defs memlayouts
174176 funs' = foldr (\ v acc -> M. update (\ fn@ (FunDef {funMeta}) -> Just (fn { funMeta = funMeta { funInline = Inline }})) v acc) funs inlines
175177 imported_progs' <- mapM id imported_progs
176178 let (defs0,funs0) =
@@ -200,29 +202,44 @@ desugarModule cfg pstate_ref import_route dir (Module _ head_mb _pragmas imports
200202 ([] , [] ) -> (M. union ddefs defs1, M. union fundefs funs1)
201203 (_x: _xs,_) -> error $ " Conflicting definitions of " ++ show conflicts1 ++ " found in " ++ mod_name
202204 (_,_x: _xs) -> error $ " Conflicting definitions of " ++ show (S. toList em2) ++ " found in " ++ mod_name)
203- (defs, funs')
205+ (defs' , funs')
204206 imported_progs'
205207 pure (Prog defs0 funs0 main)
206208 pure prog
207209 where
208- init_acc = (M. empty, M. empty, M. empty, S. empty, Nothing )
210+ init_acc = (M. empty, M. empty, M. empty, S. empty, Nothing , M. empty )
209211 mod_name = moduleName head_mb
210212
211213 moduleName :: Maybe (ModuleHead a ) -> String
212214 moduleName Nothing = " Main"
213215 moduleName (Just (ModuleHead _ mod_name1 _warnings _exports)) =
214216 mnameToStr mod_name1
215217
216- classify thing (defs,vars,funs,inlines,main) =
218+ classify thing (defs,vars,funs,inlines,main, memlayouts ) =
217219 case thing of
218- HDDef d -> (M. insert (tyName d) d defs, vars, funs, inlines, main)
219- HFunDef f -> (defs, vars, M. insert (funName f) f funs, inlines, main)
220+ HDDef d -> (M. insert (tyName d) d defs, vars, funs, inlines, main, memlayouts )
221+ HFunDef f -> (defs, vars, M. insert (funName f) f funs, inlines, main, memlayouts )
220222 HMain m ->
221223 case main of
222- Nothing -> (defs, vars, funs, inlines, m)
224+ Nothing -> (defs, vars, funs, inlines, m, memlayouts )
223225 Just _ -> error $ " A module cannot have two main expressions."
224226 ++ show mod_name
225- HInline v -> (defs,vars,funs,S. insert v inlines,main)
227+ HInline v -> (defs,vars,funs,S. insert v inlines,main, memlayouts)
228+ MemLayoutTy tycon l -> (defs,vars,funs,inlines,main, M. insert tycon l memlayouts)
229+
230+ updateMemoryLayout indefs memlayouts =
231+ let defs'' = M. mapWithKey (\ k v -> let tyName = fromVar k
232+ layout = M. lookup tyName memlayouts
233+ in case layout of
234+ Just val -> let
235+ v' = v{memLayout= val}
236+ in v'
237+ Nothing -> let
238+ v' = v{memLayout= Linear }
239+ in v'
240+ ) indefs
241+ in defs''
242+
226243desugarModule _ _ _ _ m = error $ " desugarModule: " ++ prettyPrint m
227244
228245stdlibModules :: [String ]
@@ -938,12 +955,20 @@ collectTopLevel type_syns env decl =
938955 -- 'collectTypeSynonyms'.
939956 TypeDecl {} -> pure Nothing
940957
958+ AnnPragma _ annotation ->
959+ case annotation of
960+ TypeAnn _ (Ident _ tycon) (Lit _ (String _ " Factored" _)) -> pure $ Just (MemLayoutTy tycon FullyFactored )
961+ TypeAnn _ (Ident _ tycon) (Lit _ (String _ " Linear" _)) -> pure $ Just (MemLayoutTy tycon Linear )
962+ _ -> error " Memory Layout not yet supported!"
963+
964+
941965 DataDecl _ (DataType _) _ctx decl_head cons _deriving_binds -> do
942966 let (ty_name, ty_args) = desugarDeclHead decl_head
943967 cons' = map (desugarConstr type_syns) cons
944968 if ty_name `S.member` builtinTys
945969 then error $ sdoc ty_name ++ " is a built-in type."
946- else pure $ Just $ HDDef (DDef ty_name ty_args cons')
970+ -- Default to Linear memory layout but we update it using Ann pragmas if available.
971+ else pure $ Just $ HDDef (DDef ty_name ty_args cons' Linear )
947972
948973 -- Reserved for HS.
949974 PatBind _ (PVar _ (Ident _ " main" )) (UnGuardedRhs _ _) _binds ->
0 commit comments