Skip to content

Commit bf3e02e

Browse files
authored
chore: apply code formatting (#1365)
1 parent d82e8a5 commit bf3e02e

File tree

11 files changed

+652
-601
lines changed

11 files changed

+652
-601
lines changed

src/BoxTemplates.hs

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,54 +23,66 @@ boxTy = StructTy (ConcreteNameTy (SymPath [] "Box")) [(VarTy "t")]
2323

2424
-- | Defines a template for initializing Boxes.
2525
init :: (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.
4347
unbox :: (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.
6169
peek :: (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.
7688
copy :: (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"
236248
innerStr _ _ _ = ""
237-

src/Concretize.hs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ import Polymorphism
4343
import Reify
4444
import qualified Set
4545
import ToTemplate
46+
import qualified TypeCandidate as TC
4647
import TypeError
4748
import TypePredicates
4849
import Types
4950
import TypesToC
5051
import Util
5152
import Validate
5253
import Prelude hiding (lookup)
53-
import qualified TypeCandidate as TC
5454

5555
data Level = Toplevel | Inside
5656

@@ -645,22 +645,25 @@ instantiateGenericSumtype typeEnv env originalStructTy@(StructTy _ originalTyVar
645645
rename@(StructTy _ renamedOrig) = evalState (renameVarTys originalStructTy) 0
646646
nameFixedCases = map (renameGenericTypeSymbolsOnSum (zip originalTyVars renamedOrig)) cases
647647
fixLeft l = replaceLeft (FailedToInstantiateGenericType originalStructTy) l
648-
in do mappings <- fixLeft $ solve [Constraint rename genericStructTy fake1 fake2 fake1 OrdMultiSym]
649-
let concretelyTypedCases = map (replaceGenericTypeSymbolsOnCase mappings) nameFixedCases
650-
sname = (getStructName originalStructTy)
651-
deps <- mapM (depsForCase typeEnv env) concretelyTypedCases
652-
candidate <- TC.mkSumtypeCandidate sname renamedOrig typeEnv env concretelyTypedCases (getPathFromStructName sname)
653-
validateType (TC.setRestriction candidate TC.AllowAny)
654-
pure (XObj
655-
( Lst
656-
( XObj (DefSumtype genericStructTy) Nothing Nothing :
657-
XObj (Sym (SymPath [] (tyToC genericStructTy)) Symbol) Nothing Nothing :
658-
concretelyTypedCases
659-
)
660-
)
661-
(Just dummyInfo)
662-
(Just TypeTy) :
663-
concat deps)
648+
in do
649+
mappings <- fixLeft $ solve [Constraint rename genericStructTy fake1 fake2 fake1 OrdMultiSym]
650+
let concretelyTypedCases = map (replaceGenericTypeSymbolsOnCase mappings) nameFixedCases
651+
sname = (getStructName originalStructTy)
652+
deps <- mapM (depsForCase typeEnv env) concretelyTypedCases
653+
candidate <- TC.mkSumtypeCandidate sname renamedOrig typeEnv env concretelyTypedCases (getPathFromStructName sname)
654+
validateType (TC.setRestriction candidate TC.AllowAny)
655+
pure
656+
( XObj
657+
( Lst
658+
( XObj (DefSumtype genericStructTy) Nothing Nothing :
659+
XObj (Sym (SymPath [] (tyToC genericStructTy)) Symbol) Nothing Nothing :
660+
concretelyTypedCases
661+
)
662+
)
663+
(Just dummyInfo)
664+
(Just TypeTy) :
665+
concat deps
666+
)
664667
instantiateGenericSumtype _ _ _ _ _ = error "instantiategenericsumtype"
665668

666669
-- Resolves dependencies for sumtype cases.
@@ -678,8 +681,9 @@ depsForCase _ _ x = Left (InvalidSumtypeCase x)
678681
-- | Replace instances of generic types in type candidate field definitions.
679682
replaceGenericTypeSymbolsOnFields :: Map.Map String Ty -> [TC.TypeField] -> [TC.TypeField]
680683
replaceGenericTypeSymbolsOnFields ms fields = map go fields
681-
where go (TC.StructField name t) = (TC.StructField name (replaceTyVars ms t))
682-
go (TC.SumField name ts) = (TC.SumField name (map (replaceTyVars ms) ts))
684+
where
685+
go (TC.StructField name t) = (TC.StructField name (replaceTyVars ms t))
686+
go (TC.SumField name ts) = (TC.SumField name (map (replaceTyVars ms) ts))
683687

684688
replaceGenericTypeSymbolsOnMembers :: Map.Map String Ty -> [XObj] -> [XObj]
685689
replaceGenericTypeSymbolsOnMembers mappings memberXObjs =

0 commit comments

Comments
 (0)