Skip to content

Commit

Permalink
chore: apply code formatting (#1365)
Browse files Browse the repository at this point in the history
  • Loading branch information
eriksvedang authored Dec 22, 2021
1 parent d82e8a5 commit bf3e02e
Show file tree
Hide file tree
Showing 11 changed files with 652 additions and 601 deletions.
99 changes: 55 additions & 44 deletions src/BoxTemplates.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,54 +23,66 @@ boxTy = StructTy (ConcreteNameTy (SymPath [] "Box")) [(VarTy "t")]

-- | Defines a template for initializing Boxes.
init :: (String, Binder)
init = let path = SymPath ["Box"] "init"
t = FuncTy [(VarTy "t")] boxTy StaticLifetimeTy
docs = "Initializes a box pointing to value t."
decl = templateLiteral "$t* $NAME ($t t)"
body = const (multilineTemplate
[ "$DECL {",
" $t* instance;",
" instance = CARP_MALLOC(sizeof($t));",
" *instance = t;",
" return instance;",
"}"
])
deps = const []
template = TemplateCreator $ \_ _ -> Template t decl body deps
in defineTypeParameterizedTemplate template path t docs
init =
let path = SymPath ["Box"] "init"
t = FuncTy [(VarTy "t")] boxTy StaticLifetimeTy
docs = "Initializes a box pointing to value t."
decl = templateLiteral "$t* $NAME ($t t)"
body =
const
( multilineTemplate
[ "$DECL {",
" $t* instance;",
" instance = CARP_MALLOC(sizeof($t));",
" *instance = t;",
" return instance;",
"}"
]
)
deps = const []
template = TemplateCreator $ \_ _ -> Template t decl body deps
in defineTypeParameterizedTemplate template path t docs

-- | Defines a template for converting a boxed value to a local value.
unbox :: (String, Binder)
unbox = let path = SymPath ["Box"] "unbox"
t = FuncTy [(StructTy (ConcreteNameTy (SymPath [] "Box")) [(VarTy "t")])] (VarTy "t") StaticLifetimeTy
docs = "Converts a boxed value to a reference to the value and delete the box."
decl = templateLiteral "$t $NAME($t* box)"
body = const (multilineTemplate
[ "$DECL {",
" $t local;",
" local = *box;",
" CARP_FREE(box);",
" return local;",
"}"
])
deps = const []
template = TemplateCreator $ \_ _ -> Template t decl body deps
in defineTypeParameterizedTemplate template path t docs
unbox =
let path = SymPath ["Box"] "unbox"
t = FuncTy [(StructTy (ConcreteNameTy (SymPath [] "Box")) [(VarTy "t")])] (VarTy "t") StaticLifetimeTy
docs = "Converts a boxed value to a reference to the value and delete the box."
decl = templateLiteral "$t $NAME($t* box)"
body =
const
( multilineTemplate
[ "$DECL {",
" $t local;",
" local = *box;",
" CARP_FREE(box);",
" return local;",
"}"
]
)
deps = const []
template = TemplateCreator $ \_ _ -> Template t decl body deps
in defineTypeParameterizedTemplate template path t docs

-- | Defines a template for getting a reference to the value stored in a box without performing an additional allocation.
peek :: (String, Binder)
peek = let path = SymPath ["Box"] "peek"
t = FuncTy [(RefTy (StructTy (ConcreteNameTy (SymPath [] "Box")) [(VarTy "t")]) (VarTy "q"))] (RefTy (VarTy "t") (VarTy "q")) StaticLifetimeTy
docs = "Returns a reference to the value stored in a box without performing an additional allocation."
decl = templateLiteral "$t* $NAME($t** box_ref)"
body = const (multilineTemplate
[ "$DECL {",
" return *box_ref;",
"}"
])
deps = const []
template = TemplateCreator $ \_ _ -> Template t decl body deps
in defineTypeParameterizedTemplate template path t docs
peek =
let path = SymPath ["Box"] "peek"
t = FuncTy [(RefTy (StructTy (ConcreteNameTy (SymPath [] "Box")) [(VarTy "t")]) (VarTy "q"))] (RefTy (VarTy "t") (VarTy "q")) StaticLifetimeTy
docs = "Returns a reference to the value stored in a box without performing an additional allocation."
decl = templateLiteral "$t* $NAME($t** box_ref)"
body =
const
( multilineTemplate
[ "$DECL {",
" return *box_ref;",
"}"
]
)
deps = const []
template = TemplateCreator $ \_ _ -> Template t decl body deps
in defineTypeParameterizedTemplate template path t docs

-- | Defines a template for copying a box. The copy will also be heap allocated.
copy :: (String, Binder)
Expand Down Expand Up @@ -142,7 +154,7 @@ delete =
innerDelete tenv env (StructTy (ConcreteNameTy (SymPath [] "Box")) [inner]) =
case findFunctionForMember tenv env "delete" (typesDeleterFunctionType inner) ("Inside box.", inner) of
FunctionFound functionFullName ->
" " ++ functionFullName ++ "(*box);\n"
" " ++ functionFullName ++ "(*box);\n"
++ " CARP_FREE(box);"
FunctionNotFound msg -> error msg
FunctionIgnored ->
Expand Down Expand Up @@ -234,4 +246,3 @@ innerStr tenv env (StructTy _ [t]) =
]
FunctionIgnored -> " /* Ignore type inside Box: '" ++ show t ++ "' ??? */\n"
innerStr _ _ _ = ""

42 changes: 23 additions & 19 deletions src/Concretize.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ import Polymorphism
import Reify
import qualified Set
import ToTemplate
import qualified TypeCandidate as TC
import TypeError
import TypePredicates
import Types
import TypesToC
import Util
import Validate
import Prelude hiding (lookup)
import qualified TypeCandidate as TC

data Level = Toplevel | Inside

Expand Down Expand Up @@ -645,22 +645,25 @@ instantiateGenericSumtype typeEnv env originalStructTy@(StructTy _ originalTyVar
rename@(StructTy _ renamedOrig) = evalState (renameVarTys originalStructTy) 0
nameFixedCases = map (renameGenericTypeSymbolsOnSum (zip originalTyVars renamedOrig)) cases
fixLeft l = replaceLeft (FailedToInstantiateGenericType originalStructTy) l
in do mappings <- fixLeft $ solve [Constraint rename genericStructTy fake1 fake2 fake1 OrdMultiSym]
let concretelyTypedCases = map (replaceGenericTypeSymbolsOnCase mappings) nameFixedCases
sname = (getStructName originalStructTy)
deps <- mapM (depsForCase typeEnv env) concretelyTypedCases
candidate <- TC.mkSumtypeCandidate sname renamedOrig typeEnv env concretelyTypedCases (getPathFromStructName sname)
validateType (TC.setRestriction candidate TC.AllowAny)
pure (XObj
( Lst
( XObj (DefSumtype genericStructTy) Nothing Nothing :
XObj (Sym (SymPath [] (tyToC genericStructTy)) Symbol) Nothing Nothing :
concretelyTypedCases
)
)
(Just dummyInfo)
(Just TypeTy) :
concat deps)
in do
mappings <- fixLeft $ solve [Constraint rename genericStructTy fake1 fake2 fake1 OrdMultiSym]
let concretelyTypedCases = map (replaceGenericTypeSymbolsOnCase mappings) nameFixedCases
sname = (getStructName originalStructTy)
deps <- mapM (depsForCase typeEnv env) concretelyTypedCases
candidate <- TC.mkSumtypeCandidate sname renamedOrig typeEnv env concretelyTypedCases (getPathFromStructName sname)
validateType (TC.setRestriction candidate TC.AllowAny)
pure
( XObj
( Lst
( XObj (DefSumtype genericStructTy) Nothing Nothing :
XObj (Sym (SymPath [] (tyToC genericStructTy)) Symbol) Nothing Nothing :
concretelyTypedCases
)
)
(Just dummyInfo)
(Just TypeTy) :
concat deps
)
instantiateGenericSumtype _ _ _ _ _ = error "instantiategenericsumtype"

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

replaceGenericTypeSymbolsOnMembers :: Map.Map String Ty -> [XObj] -> [XObj]
replaceGenericTypeSymbolsOnMembers mappings memberXObjs =
Expand Down
Loading

0 comments on commit bf3e02e

Please sign in to comment.