1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
(*
Copyright (c) 2000
Cambridge University Technical Services Limited
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*)
(**********************************************************************
*
* Functor Application
*
**********************************************************************)
functor STRUCT_CONVERSIONALS (
structure CallWithConv : CallWithConvSig
structure Struct : StructSig
structure ForeignException : ForeignExceptionSig
structure LowerLevel : LowerLevelSig
sharing type CallWithConv.vol = Struct.vol = LowerLevel.vol
sharing Struct.Ctype = CallWithConv.Ctype = LowerLevel.Ctype
) : StructConversionalsSig =
struct
open Struct;
open CallWithConv;
open Ctype;
open ForeignException;
open LowerLevel;
exception Zip;
fun zip [] [] = []
| zip (x::xs) (y::ys) = (x,y) :: zip xs ys
| zip _ _ = raise Zip;
exception Break
fun break2 ts v = case break_struct ts v of [a,b] => (a,b) | _ => raise Break
fun break3 ts v = case break_struct ts v of [a,b,c] => (a,b,c) | _ => raise Break
fun break4 ts v = case break_struct ts v of [a,b,c,d] => (a,b,c,d) | _ => raise Break
fun break5 ts v = case break_struct ts v of [a,b,c,d,e] => (a,b,c,d,e) | _ => raise Break
fun break6 ts v = case break_struct ts v of [a,b,c,d,e,f] => (a,b,c,d,e,f) | _ => raise Break
fun break7 ts v = case break_struct ts v of [a,b,c,d,e,f,g] => (a,b,c,d,e,f,g) | _ => raise Break
fun break8 ts v = case break_struct ts v of [a,b,c,d,e,f,g,h] => (a,b,c,d,e,f,g,h) | _ => raise Break
fun break9 ts v = case break_struct ts v of [a,b,c,d,e,f,g,h,i] => (a,b,c,d,e,f,g,h,i) | _ => raise Break
fun STRUCT2 (c1,c2) =
let
val (from1,to1,ctype1) = breakConversion c1
val (from2,to2,ctype2) = breakConversion c2
val ctypes = [ctype1,ctype2]
fun from v =
let val (v1,v2) = break2 ctypes v
in (from1 v1,from2 v2)
end
fun to (a,b) = make_struct (zip ctypes [to1 a,to2 b])
in
mkConversion from to (Cstruct ctypes)
end
fun STRUCT3 (c1,c2,c3) =
let
val (from1,to1,ctype1) = breakConversion c1
val (from2,to2,ctype2) = breakConversion c2
val (from3,to3,ctype3) = breakConversion c3
val ctypes = [ctype1,ctype2,ctype3]
fun from v =
let val (v1,v2,v3) = break3 ctypes v
in (from1 v1,from2 v2,from3 v3)
end
fun to (a,b,c) = make_struct (zip ctypes [to1 a,to2 b,to3 c])
in
mkConversion from to (Cstruct ctypes)
end
fun STRUCT4 (c1,c2,c3,c4) =
let
val (from1,to1,ctype1) = breakConversion c1
val (from2,to2,ctype2) = breakConversion c2
val (from3,to3,ctype3) = breakConversion c3
val (from4,to4,ctype4) = breakConversion c4
val ctypes = [ctype1,ctype2,ctype3,ctype4]
fun from v =
let val (v1,v2,v3,v4) = break4 ctypes v
in (from1 v1,from2 v2,from3 v3,from4 v4)
end
fun to (a,b,c,d) = make_struct (zip ctypes [to1 a,to2 b,to3 c,to4 d])
in
mkConversion from to (Cstruct ctypes)
end
fun STRUCT5 (c1,c2,c3,c4,c5) =
let
val (from1,to1,ctype1) = breakConversion c1
val (from2,to2,ctype2) = breakConversion c2
val (from3,to3,ctype3) = breakConversion c3
val (from4,to4,ctype4) = breakConversion c4
val (from5,to5,ctype5) = breakConversion c5
val ctypes = [ctype1,ctype2,ctype3,ctype4,ctype5]
fun from v =
let val (v1,v2,v3,v4,v5) = break5 ctypes v
in (from1 v1,from2 v2,from3 v3,from4 v4,from5 v5)
end
fun to (a,b,c,d,e) = make_struct (zip ctypes [to1 a,to2 b,to3 c,to4 d,to5 e])
in
mkConversion from to (Cstruct ctypes)
end
fun STRUCT6 (c1,c2,c3,c4,c5,c6) =
let
val (from1,to1,ctype1) = breakConversion c1
val (from2,to2,ctype2) = breakConversion c2
val (from3,to3,ctype3) = breakConversion c3
val (from4,to4,ctype4) = breakConversion c4
val (from5,to5,ctype5) = breakConversion c5
val (from6,to6,ctype6) = breakConversion c6
val ctypes = [ctype1,ctype2,ctype3,ctype4,ctype5,ctype6]
fun from v =
let val (v1,v2,v3,v4,v5,v6) = break6 ctypes v
in (from1 v1,from2 v2,from3 v3,from4 v4,from5 v5,from6 v6)
end
fun to (a,b,c,d,e,f) = make_struct (zip ctypes [to1 a,to2 b,to3 c,to4 d,to5 e,to6 f])
in
mkConversion from to (Cstruct ctypes)
end
fun STRUCT7 (c1,c2,c3,c4,c5,c6,c7) =
let
val (from1,to1,ctype1) = breakConversion c1
val (from2,to2,ctype2) = breakConversion c2
val (from3,to3,ctype3) = breakConversion c3
val (from4,to4,ctype4) = breakConversion c4
val (from5,to5,ctype5) = breakConversion c5
val (from6,to6,ctype6) = breakConversion c6
val (from7,to7,ctype7) = breakConversion c7
val ctypes = [ctype1,ctype2,ctype3,ctype4,ctype5,ctype6,ctype7]
fun from v =
let val (v1,v2,v3,v4,v5,v6,v7) = break7 ctypes v
in (from1 v1,from2 v2,from3 v3,from4 v4,from5 v5,from6 v6,from7 v7)
end
fun to (a,b,c,d,e,f,g) = make_struct (zip ctypes [to1 a,to2 b,to3 c,to4 d,to5 e,to6 f,to7 g])
in
mkConversion from to (Cstruct ctypes)
end
fun STRUCT8 (c1,c2,c3,c4,c5,c6,c7,c8) =
let
val (from1,to1,ctype1) = breakConversion c1
val (from2,to2,ctype2) = breakConversion c2
val (from3,to3,ctype3) = breakConversion c3
val (from4,to4,ctype4) = breakConversion c4
val (from5,to5,ctype5) = breakConversion c5
val (from6,to6,ctype6) = breakConversion c6
val (from7,to7,ctype7) = breakConversion c7
val (from8,to8,ctype8) = breakConversion c8
val ctypes = [ctype1,ctype2,ctype3,ctype4,ctype5,ctype6,ctype7,ctype8]
fun from v =
let val (v1,v2,v3,v4,v5,v6,v7,v8) = break8 ctypes v
in (from1 v1,from2 v2,from3 v3,from4 v4,from5 v5,from6 v6,from7 v7,from8 v8)
end
fun to (a,b,c,d,e,f,g,h) = make_struct (zip ctypes [to1 a,to2 b,to3 c,to4 d,to5 e,to6 f,to7 g,to8 h])
in
mkConversion from to (Cstruct ctypes)
end
fun STRUCT9 (c1,c2,c3,c4,c5,c6,c7,c8,c9) =
let
val (from1,to1,ctype1) = breakConversion c1
val (from2,to2,ctype2) = breakConversion c2
val (from3,to3,ctype3) = breakConversion c3
val (from4,to4,ctype4) = breakConversion c4
val (from5,to5,ctype5) = breakConversion c5
val (from6,to6,ctype6) = breakConversion c6
val (from7,to7,ctype7) = breakConversion c7
val (from8,to8,ctype8) = breakConversion c8
val (from9,to9,ctype9) = breakConversion c9
val ctypes = [ctype1,ctype2,ctype3,ctype4,ctype5,ctype6,ctype7,ctype8,ctype9]
fun from v =
let val (v1,v2,v3,v4,v5,v6,v7,v8,v9) = break9 ctypes v
in (from1 v1,from2 v2,from3 v3,from4 v4,from5 v5,from6 v6,from7 v7,from8 v8,from9 v9)
end
fun to (a,b,c,d,e,f,g,h,i) = make_struct (zip ctypes [to1 a,to2 b,to3 c,to4 d,to5 e,to6 f,to7 g,to8 h,to9 i])
in
mkConversion from to (Cstruct ctypes)
end
fun FUNCTION0 () (cr: 'a Conversion) =
let
val (_, tor, ctyper) = breakConversion cr (* Convert the result type. *)
fun from _ = raise Foreign "Conversion from C function to ML function is not implemented"
(* Construct a wrapper function. The argument is ignored and the result is converted into a vol.*)
fun convF f _ = tor(f())
fun to (f: unit->'a) : vol = toCfunction [] ctyper (convF f)
in
mkConversion from to (Cfunction ([], ctyper))
end;
fun PASCALFUNCTION0 () cr =
let
val (_, tor, ctyper) = breakConversion cr (* Convert the result type. *)
fun from _ = raise Foreign "Conversion from C function to ML function is not implemented"
fun convF f _ = tor(f())
fun to f = toPascalfunction [] ctyper (convF f)
in
mkConversion from to (Cfunction ([], ctyper))
end;
fun FUNCTION1 c1 cr =
let
val (from1,_,ctype1) = breakConversion c1
val (_,tor,ctyper) = breakConversion cr
fun from _ = raise Foreign "Conversion from C function to ML function is not implemented"
(* The wrapper function. We need a single argument. *)
fun convF f [a] = tor(f(from1 a))
| convF _ _ = raise Union.Never "arg mismatch"
fun to f : vol = toCfunction [ctype1] ctyper (convF f)
in
mkConversion from to (Cfunction ([ctype1], ctyper))
end;
fun PASCALFUNCTION1 c1 cr =
let
val (from1,_,ctype1) = breakConversion c1
val (_,tor,ctyper) = breakConversion cr
fun from _ = raise Foreign "Conversion from C function to ML function is not implemented"
fun convF f [a] = tor(f(from1 a))
| convF _ _ = raise Union.Never "arg mismatch"
fun to f : vol = toPascalfunction [ctype1] ctyper (convF f)
in
mkConversion from to (Cfunction ([ctype1], ctyper))
end;
fun FUNCTION2 (c1, c2) cr =
let
val (from1,_,ctype1) = breakConversion c1
val (from2,_,ctype2) = breakConversion c2
val (_,tor,ctyper) = breakConversion cr
fun from _ = raise Foreign "Conversion from C function to ML function is not implemented"
fun convF f [a,b] = tor(f(from1 a, from2 b))
| convF _ _ = raise Union.Never "arg mismatch"
fun to f = toCfunction [ctype1, ctype2] ctyper (convF f)
in
mkConversion from to (Cfunction ([ctype1, ctype2], ctyper))
end;
fun PASCALFUNCTION2 (c1, c2) cr =
let
val (from1,_,ctype1) = breakConversion c1
val (from2,_,ctype2) = breakConversion c2
val (_,tor,ctyper) = breakConversion cr
fun from _ = raise Foreign "Conversion from C function to ML function is not implemented"
fun convF f [a,b] = tor(f(from1 a, from2 b))
| convF _ _ = raise Union.Never "arg mismatch"
fun to f = toPascalfunction [ctype1, ctype2] ctyper (convF f)
in
mkConversion from to (Cfunction ([ctype1, ctype2], ctyper))
end;
fun FUNCTION3 (c1, c2, c3) cr =
let
val (from1,_,ctype1) = breakConversion c1
val (from2,_,ctype2) = breakConversion c2
val (from3,_,ctype3) = breakConversion c3
val (_,tor,ctyper) = breakConversion cr
fun from _ = raise Foreign "Conversion from C function to ML function is not implemented"
fun convF f [a,b,c] = tor(f(from1 a, from2 b, from3 c))
| convF _ _ = raise Union.Never "arg mismatch"
fun to f = toCfunction [ctype1, ctype2, ctype3] ctyper (convF f)
in
mkConversion from to (Cfunction ([ctype1, ctype2, ctype3], ctyper))
end;
fun PASCALFUNCTION3 (c1, c2, c3) cr =
let
val (from1,_,ctype1) = breakConversion c1
val (from2,_,ctype2) = breakConversion c2
val (from3,_,ctype3) = breakConversion c3
val (_,tor,ctyper) = breakConversion cr
fun from _ = raise Foreign "Conversion from C function to ML function is not implemented"
fun convF f [a,b,c] = tor(f(from1 a, from2 b, from3 c))
| convF _ _ = raise Union.Never "arg mismatch"
fun to f = toPascalfunction [ctype1, ctype2, ctype3] ctyper (convF f)
in
mkConversion from to (Cfunction ([ctype1, ctype2, ctype3], ctyper))
end;
fun FUNCTION4 (c1, c2, c3, c4) cr =
let
val (from1,_,ctype1) = breakConversion c1
val (from2,_,ctype2) = breakConversion c2
val (from3,_,ctype3) = breakConversion c3
val (from4,_,ctype4) = breakConversion c4
val (_,tor,ctyper) = breakConversion cr
fun from _ = raise Foreign "Conversion from C function to ML function is not implemented"
fun convF f [a,b,c,d] = tor(f(from1 a, from2 b, from3 c, from4 d))
| convF _ _ = raise Union.Never "arg mismatch"
fun to f = toCfunction [ctype1, ctype2, ctype3, ctype4] ctyper (convF f)
in
mkConversion from to (Cfunction ([ctype1, ctype2, ctype3, ctype4], ctyper))
end;
fun PASCALFUNCTION4 (c1, c2, c3, c4) cr =
let
val (from1,_,ctype1) = breakConversion c1
val (from2,_,ctype2) = breakConversion c2
val (from3,_,ctype3) = breakConversion c3
val (from4,_,ctype4) = breakConversion c4
val (_,tor,ctyper) = breakConversion cr
fun from _ = raise Foreign "Conversion from C function to ML function is not implemented"
fun convF f [a,b,c,d] = tor(f(from1 a, from2 b, from3 c, from4 d))
| convF _ _ = raise Union.Never "arg mismatch"
fun to f = toPascalfunction [ctype1, ctype2, ctype3, ctype4] ctyper (convF f)
in
mkConversion from to (Cfunction ([ctype1, ctype2, ctype3, ctype4], ctyper))
end;
(*
val FUNCTION : 'a Conversion * 'b Conversion -> ( 'a -> 'b ) Conversion
val PASCALFUNCTION : 'a Conversion * 'b Conversion -> ( 'a -> 'b ) Conversion *)
end (* struct *)
|