@@ -23,54 +23,66 @@ boxTy = StructTy (ConcreteNameTy (SymPath [] "Box")) [(VarTy "t")]
2323
2424-- | Defines a template for initializing Boxes.
2525init :: (String , Binder )
26- init = let path = SymPath [" Box" ] " init"
27- t = FuncTy [(VarTy " t" )] boxTy StaticLifetimeTy
28- docs = " Initializes a box pointing to value t."
29- decl = templateLiteral " $t* $NAME ($t t)"
30- body = const (multilineTemplate
31- [ " $DECL {" ,
32- " $t* instance;" ,
33- " instance = CARP_MALLOC(sizeof($t));" ,
34- " *instance = t;" ,
35- " return instance;" ,
36- " }"
37- ])
38- deps = const []
39- template = TemplateCreator $ \ _ _ -> Template t decl body deps
40- in defineTypeParameterizedTemplate template path t docs
26+ init =
27+ let path = SymPath [" Box" ] " init"
28+ t = FuncTy [(VarTy " t" )] boxTy StaticLifetimeTy
29+ docs = " Initializes a box pointing to value t."
30+ decl = templateLiteral " $t* $NAME ($t t)"
31+ body =
32+ const
33+ ( multilineTemplate
34+ [ " $DECL {" ,
35+ " $t* instance;" ,
36+ " instance = CARP_MALLOC(sizeof($t));" ,
37+ " *instance = t;" ,
38+ " return instance;" ,
39+ " }"
40+ ]
41+ )
42+ deps = const []
43+ template = TemplateCreator $ \ _ _ -> Template t decl body deps
44+ in defineTypeParameterizedTemplate template path t docs
4145
4246-- | Defines a template for converting a boxed value to a local value.
4347unbox :: (String , Binder )
44- unbox = let path = SymPath [" Box" ] " unbox"
45- t = FuncTy [(StructTy (ConcreteNameTy (SymPath [] " Box" )) [(VarTy " t" )])] (VarTy " t" ) StaticLifetimeTy
46- docs = " Converts a boxed value to a reference to the value and delete the box."
47- decl = templateLiteral " $t $NAME($t* box)"
48- body = const (multilineTemplate
49- [ " $DECL {" ,
50- " $t local;" ,
51- " local = *box;" ,
52- " CARP_FREE(box);" ,
53- " return local;" ,
54- " }"
55- ])
56- deps = const []
57- template = TemplateCreator $ \ _ _ -> Template t decl body deps
58- in defineTypeParameterizedTemplate template path t docs
48+ unbox =
49+ let path = SymPath [" Box" ] " unbox"
50+ t = FuncTy [(StructTy (ConcreteNameTy (SymPath [] " Box" )) [(VarTy " t" )])] (VarTy " t" ) StaticLifetimeTy
51+ docs = " Converts a boxed value to a reference to the value and delete the box."
52+ decl = templateLiteral " $t $NAME($t* box)"
53+ body =
54+ const
55+ ( multilineTemplate
56+ [ " $DECL {" ,
57+ " $t local;" ,
58+ " local = *box;" ,
59+ " CARP_FREE(box);" ,
60+ " return local;" ,
61+ " }"
62+ ]
63+ )
64+ deps = const []
65+ template = TemplateCreator $ \ _ _ -> Template t decl body deps
66+ in defineTypeParameterizedTemplate template path t docs
5967
6068-- | Defines a template for getting a reference to the value stored in a box without performing an additional allocation.
6169peek :: (String , Binder )
62- peek = let path = SymPath [" Box" ] " peek"
63- t = FuncTy [(RefTy (StructTy (ConcreteNameTy (SymPath [] " Box" )) [(VarTy " t" )]) (VarTy " q" ))] (RefTy (VarTy " t" ) (VarTy " q" )) StaticLifetimeTy
64- docs = " Returns a reference to the value stored in a box without performing an additional allocation."
65- decl = templateLiteral " $t* $NAME($t** box_ref)"
66- body = const (multilineTemplate
67- [ " $DECL {" ,
68- " return *box_ref;" ,
69- " }"
70- ])
71- deps = const []
72- template = TemplateCreator $ \ _ _ -> Template t decl body deps
73- in defineTypeParameterizedTemplate template path t docs
70+ peek =
71+ let path = SymPath [" Box" ] " peek"
72+ t = FuncTy [(RefTy (StructTy (ConcreteNameTy (SymPath [] " Box" )) [(VarTy " t" )]) (VarTy " q" ))] (RefTy (VarTy " t" ) (VarTy " q" )) StaticLifetimeTy
73+ docs = " Returns a reference to the value stored in a box without performing an additional allocation."
74+ decl = templateLiteral " $t* $NAME($t** box_ref)"
75+ body =
76+ const
77+ ( multilineTemplate
78+ [ " $DECL {" ,
79+ " return *box_ref;" ,
80+ " }"
81+ ]
82+ )
83+ deps = const []
84+ template = TemplateCreator $ \ _ _ -> Template t decl body deps
85+ in defineTypeParameterizedTemplate template path t docs
7486
7587-- | Defines a template for copying a box. The copy will also be heap allocated.
7688copy :: (String , Binder )
@@ -142,7 +154,7 @@ delete =
142154 innerDelete tenv env (StructTy (ConcreteNameTy (SymPath [] " Box" )) [inner]) =
143155 case findFunctionForMember tenv env " delete" (typesDeleterFunctionType inner) (" Inside box." , inner) of
144156 FunctionFound functionFullName ->
145- " " ++ functionFullName ++ " (*box);\n "
157+ " " ++ functionFullName ++ " (*box);\n "
146158 ++ " CARP_FREE(box);"
147159 FunctionNotFound msg -> error msg
148160 FunctionIgnored ->
@@ -234,4 +246,3 @@ innerStr tenv env (StructTy _ [t]) =
234246 ]
235247 FunctionIgnored -> " /* Ignore type inside Box: '" ++ show t ++ " ' ??? */\n "
236248innerStr _ _ _ = " "
237-
0 commit comments