|
| 1 | +-- | Module BoxTemplates defines Carp's Box type, a container for managed, |
| 2 | +-- heap allocated objects. |
| 3 | +module BoxTemplates |
| 4 | + ( delete, |
| 5 | + str, |
| 6 | + prn, |
| 7 | + BoxTemplates.init, |
| 8 | + copy, |
| 9 | + unbox, |
| 10 | + peek, |
| 11 | + ) |
| 12 | +where |
| 13 | + |
| 14 | +import Concretize |
| 15 | +import Obj |
| 16 | +import Polymorphism |
| 17 | +import Template |
| 18 | +import ToTemplate |
| 19 | +import Types |
| 20 | + |
| 21 | +boxTy :: Ty |
| 22 | +boxTy = StructTy (ConcreteNameTy (SymPath [] "Box")) [(VarTy "t")] |
| 23 | + |
| 24 | +-- | Defines a template for initializing Boxes. |
| 25 | +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 |
| 41 | + |
| 42 | +-- | Defines a template for converting a boxed value to a local value. |
| 43 | +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 |
| 59 | + |
| 60 | +-- | Defines a template for getting a reference to the value stored in a box without performing an additional allocation. |
| 61 | +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 |
| 74 | + |
| 75 | +-- | Defines a template for copying a box. The copy will also be heap allocated. |
| 76 | +copy :: (String, Binder) |
| 77 | +copy = |
| 78 | + let path = SymPath ["Box"] "copy" |
| 79 | + t = FuncTy [(RefTy (StructTy (ConcreteNameTy (SymPath [] "Box")) [(VarTy "t")]) (VarTy "q"))] (StructTy (ConcreteNameTy (SymPath [] "Box")) [(VarTy "t")]) StaticLifetimeTy |
| 80 | + docs = "Copies a box." |
| 81 | + decl = (templateLiteral "$t* $NAME ($t** box)") |
| 82 | + template = TemplateCreator $ |
| 83 | + \tenv env -> |
| 84 | + Template |
| 85 | + t |
| 86 | + decl |
| 87 | + ( \(FuncTy [RefTy (StructTy (ConcreteNameTy (SymPath [] "Box")) [inner]) _] _ _) -> |
| 88 | + innerCopy tenv env inner |
| 89 | + ) |
| 90 | + ( \(FuncTy [RefTy boxType@(StructTy (ConcreteNameTy (SymPath [] "Box")) [inner]) _] _ _) -> |
| 91 | + depsForCopyFunc tenv env inner |
| 92 | + ++ depsForDeleteFunc tenv env boxType |
| 93 | + ) |
| 94 | + in defineTypeParameterizedTemplate template path t docs |
| 95 | + where |
| 96 | + innerCopy typeEnv valEnv innerTy = |
| 97 | + case findFunctionForMemberIncludePrimitives typeEnv valEnv "copy" (typesCopyFunctionType innerTy) ("Inside box.", innerTy) of |
| 98 | + FunctionFound functionFullName -> |
| 99 | + multilineTemplate |
| 100 | + [ "$DECL {", |
| 101 | + " $t* copy;", |
| 102 | + " copy = CARP_MALLOC(sizeof($t));", |
| 103 | + " *copy = " ++ functionFullName ++ "(*box);", |
| 104 | + " return copy;", |
| 105 | + "}" |
| 106 | + ] |
| 107 | + _ -> |
| 108 | + multilineTemplate |
| 109 | + [ "$DECL {", |
| 110 | + " $t* copy;", |
| 111 | + " copy = CARP_MALLOC(sizeof($t));", |
| 112 | + " *copy = *box;", |
| 113 | + " return copy;", |
| 114 | + "}" |
| 115 | + ] |
| 116 | + |
| 117 | +-- | Defines a template for deleting a box. |
| 118 | +delete :: (String, Binder) |
| 119 | +delete = |
| 120 | + let path = SymPath ["Box"] "delete" |
| 121 | + t = FuncTy [boxTy] UnitTy StaticLifetimeTy |
| 122 | + docs = "Deletes a box, freeing its associated memory." |
| 123 | + decl = (templateLiteral "void $NAME ($t* box)") |
| 124 | + templateCreator = TemplateCreator $ |
| 125 | + \tenv env -> |
| 126 | + Template |
| 127 | + t |
| 128 | + decl |
| 129 | + ( \(FuncTy [bTy] UnitTy _) -> |
| 130 | + multilineTemplate |
| 131 | + [ "$DECL {", |
| 132 | + " " ++ innerDelete tenv env bTy, |
| 133 | + "}" |
| 134 | + ] |
| 135 | + ) |
| 136 | + ( \(FuncTy [StructTy (ConcreteNameTy (SymPath [] "Box")) [insideType]] UnitTy _) -> |
| 137 | + depsForDeleteFunc tenv env insideType |
| 138 | + ) |
| 139 | + in defineTypeParameterizedTemplate templateCreator path t docs |
| 140 | + where |
| 141 | + innerDelete :: TypeEnv -> Env -> Ty -> String |
| 142 | + innerDelete tenv env (StructTy (ConcreteNameTy (SymPath [] "Box")) [inner]) = |
| 143 | + case findFunctionForMember tenv env "delete" (typesDeleterFunctionType inner) ("Inside box.", inner) of |
| 144 | + FunctionFound functionFullName -> |
| 145 | + " " ++ functionFullName ++ "(*box);\n" |
| 146 | + ++ " CARP_FREE(box);" |
| 147 | + FunctionNotFound msg -> error msg |
| 148 | + FunctionIgnored -> |
| 149 | + " /* Ignore non-managed type inside Box: '" ++ show inner ++ "' */\n" |
| 150 | + ++ " CARP_FREE(box);" |
| 151 | + innerDelete _ _ _ = "" |
| 152 | + |
| 153 | +-- | Defines a template for printing a box as a string. |
| 154 | +prn :: (String, Binder) |
| 155 | +prn = |
| 156 | + let path = SymPath ["Box"] "prn" |
| 157 | + t = FuncTy [boxTy] StringTy StaticLifetimeTy |
| 158 | + docs = "Returns a string representation of a Box." |
| 159 | + decl = templateLiteral "String $NAME ($t* box)" |
| 160 | + templateCreator = |
| 161 | + TemplateCreator $ |
| 162 | + ( \tenv env -> |
| 163 | + Template |
| 164 | + t |
| 165 | + decl |
| 166 | + ( \(FuncTy [boxT] StringTy _) -> |
| 167 | + multilineTemplate |
| 168 | + [ "$DECL {", |
| 169 | + " if(!box){", |
| 170 | + " String buffer = CARP_MALLOC(4);", |
| 171 | + " sprintf(buffer, \"Nil\");", |
| 172 | + " return buffer;", |
| 173 | + " }", |
| 174 | + innerStr tenv env boxT, |
| 175 | + " return buffer;", |
| 176 | + "}" |
| 177 | + ] |
| 178 | + ) |
| 179 | + ( \(FuncTy [(StructTy (ConcreteNameTy (SymPath [] "Box")) [inner])] StringTy _) -> |
| 180 | + depsForPrnFunc tenv env inner |
| 181 | + ) |
| 182 | + ) |
| 183 | + in defineTypeParameterizedTemplate templateCreator path t docs |
| 184 | + |
| 185 | +-- | Defines a template for printing a reference to a box as a string. |
| 186 | +str :: (String, Binder) |
| 187 | +str = |
| 188 | + let path = SymPath ["Box"] "str" |
| 189 | + t = FuncTy [(RefTy boxTy (VarTy "q"))] StringTy StaticLifetimeTy |
| 190 | + docs = "Returns a string representation of a Box." |
| 191 | + templateCreator = |
| 192 | + TemplateCreator $ |
| 193 | + ( \tenv env -> |
| 194 | + Template |
| 195 | + t |
| 196 | + (templateLiteral "String $NAME ($t** box)") |
| 197 | + ( \(FuncTy [RefTy boxT _] StringTy _) -> |
| 198 | + multilineTemplate |
| 199 | + [ "$DECL {", |
| 200 | + " if(!box){", |
| 201 | + " String buffer = CARP_MALLOC(4);", |
| 202 | + " sprintf(buffer, \"Nil\");", |
| 203 | + " return buffer;", |
| 204 | + " }", |
| 205 | + innerStr tenv env boxT, |
| 206 | + " return buffer;", |
| 207 | + "}" |
| 208 | + ] |
| 209 | + ) |
| 210 | + ( \(FuncTy [RefTy (StructTy (ConcreteNameTy (SymPath [] "Box")) [inner]) _] StringTy _) -> |
| 211 | + depsForPrnFunc tenv env inner |
| 212 | + ) |
| 213 | + ) |
| 214 | + in defineTypeParameterizedTemplate templateCreator path t docs |
| 215 | + |
| 216 | +innerStr :: TypeEnv -> Env -> Ty -> String |
| 217 | +innerStr tenv env (StructTy _ [t]) = |
| 218 | + case findFunctionForMemberIncludePrimitives tenv env "prn" (typesStrFunctionType tenv env (RefTy t StaticLifetimeTy)) ("Inside box.", t) of |
| 219 | + FunctionFound functionFullName -> |
| 220 | + unlines |
| 221 | + [ " char* temp = " ++ functionFullName ++ "(*box);", |
| 222 | + " int size = snprintf(NULL, 0, \"(Box %s)\", temp);", |
| 223 | + " String buffer = CARP_MALLOC(size);", |
| 224 | + " sprintf(buffer, \"(Box %s)\", temp);", |
| 225 | + " if(temp) {", |
| 226 | + " CARP_FREE(temp);", |
| 227 | + " temp = NULL;", |
| 228 | + " }" |
| 229 | + ] |
| 230 | + FunctionNotFound _ -> |
| 231 | + unlines |
| 232 | + [ " String buffer = CARP_MALLOC(14);", |
| 233 | + " sprintf(buffer, \"(Box unknown)\");" |
| 234 | + ] |
| 235 | + FunctionIgnored -> " /* Ignore type inside Box: '" ++ show t ++ "' ??? */\n" |
| 236 | +innerStr _ _ _ = "" |
| 237 | + |
0 commit comments