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
|
(***********************************************************************)
(* *)
(* CamlIDL *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1999 Institut National de Recherche en Informatique et *)
(* en Automatique. All rights reserved. This file is distributed *)
(* under the terms of the GNU Lesser General Public License LGPL v2.1 *)
(* *)
(***********************************************************************)
(* $Id: union.ml,v 1.8 2002-01-16 09:42:04 xleroy Exp $ *)
(* Handling of unions *)
open Printf
open Utils
open Variables
open Idltypes
open Cvttyp
(* Translate an ML datatype [v] and store its argument in the C union [c]
and its discriminant in the C integer [discr]. *)
let union_ml_to_c ml_to_c oc onstack pref ud v c discr =
let tag_constant = ref 0
and tag_constr = ref 0 in
let emit_case = function
{case_field = None; case_labels = []} -> (* default case, no arg *)
iprintf oc "case %d: /* default */\n" !tag_constr;
incr tag_constr;
iprintf oc " %s = Int_val(Field(%s, 0));\n" discr v;
iprintf oc " break;\n"
| {case_field = Some{field_name = n; field_typ = ty}; case_labels = []} ->
(* default case, one arg *)
iprintf oc "case %d: /* default */\n" !tag_constr;
incr tag_constr;
increase_indent();
iprintf oc "%s = Int_val(Field(%s, 0));\n" discr v;
let v' = new_ml_variable() in
iprintf oc "%s = Field(%s, 1);\n" v' v;
ml_to_c oc onstack pref ty v' (sprintf "%s.%s" c n);
iprintf oc "break;\n";
decrease_indent()
| {case_field = None; case_labels = lbls} -> (* named case, no args *)
List.iter
(fun lbl ->
iprintf oc "case %d: /* %s */\n" !tag_constant lbl;
incr tag_constant;
iprintf oc " %s = %s;\n" discr lbl;
iprintf oc " break;\n")
lbls
| {case_field = Some{field_name = n; field_typ = ty}; case_labels = lbls} ->
(* named case, one arg *)
List.iter
(fun lbl ->
iprintf oc "case %d: /* %s */\n" !tag_constr lbl;
incr tag_constr;
increase_indent();
iprintf oc "%s = %s;\n" discr lbl;
let v' = new_ml_variable() in
iprintf oc "%s = Field(%s, 0);\n" v' v;
ml_to_c oc onstack pref ty v' (sprintf "%s.%s" c n);
iprintf oc "break;\n";
decrease_indent())
lbls in
let (constant_cases, constr_cases) =
list_partition
(fun c -> c.case_field = None && c.case_labels <> [])
ud.ud_cases in
if constant_cases <> [] && constr_cases <> [] then begin
iprintf oc "if (Is_long(%s)) {\n" v;
increase_indent()
end;
if constant_cases <> [] then begin
iprintf oc "switch (Int_val(%s)) {\n" v;
List.iter emit_case constant_cases;
iprintf oc "}\n"
end;
if constant_cases <> [] && constr_cases <> [] then begin
decrease_indent();
iprintf oc "} else {\n";
increase_indent()
end;
if constr_cases <> [] then begin
iprintf oc "switch (Tag_val(%s)) {\n" v;
List.iter emit_case constr_cases;
iprintf oc "}\n"
end;
if constant_cases <> [] && constr_cases <> [] then begin
decrease_indent();
iprintf oc "}\n"
end
(* Translate a C union [c] with its discriminant [discr]
to an ML datatype [v]. *)
let union_c_to_ml c_to_ml oc pref ud c v discr =
let tag_constant = ref 0 and tag_constr = ref 0 in
let have_default = ref false in
let emit_case = function
{case_field = None; case_labels = []} -> (* default case, no arg *)
iprintf oc "default:\n";
increase_indent();
iprintf oc "%s = camlidl_alloc_small(1, %d);\n" v !tag_constr;
incr tag_constr;
iprintf oc "Field(%s, 0) = Val_int(%s);\n" v discr;
iprintf oc "break;\n";
decrease_indent();
have_default := true
| {case_field = Some{field_name = n; field_typ = ty}; case_labels = []} ->
(* default case, one arg *)
iprintf oc "default:\n";
increase_indent();
let v' = new_ml_variable() in
c_to_ml oc pref ty (sprintf "%s.%s" c n) v';
iprintf oc "Begin_root(%s)\n" v';
increase_indent();
iprintf oc "%s = camlidl_alloc_small(2, %d);\n" v !tag_constr;
incr tag_constr;
iprintf oc "Field(%s, 0) = Val_int(%s);\n" v discr;
iprintf oc "Field(%s, 1) = %s;\n" v v';
decrease_indent();
iprintf oc "End_roots()\n";
iprintf oc "break;\n";
decrease_indent();
have_default := true
| {case_field = None; case_labels = lbls} -> (* named cases, no arg *)
List.iter
(fun lbl ->
iprintf oc "case %s:\n" lbl;
iprintf oc " %s = Val_int(%d);\n" v !tag_constant;
incr tag_constant;
iprintf oc " break;\n")
lbls;
| {case_field = Some{field_name = n; field_typ = ty}; case_labels = lbls} ->
(* named cases, one arg *)
List.iter
(fun lbl ->
iprintf oc "case %s:\n" lbl;
increase_indent();
let v' = new_ml_variable() in
c_to_ml oc pref ty (sprintf "%s.%s" c n) v';
iprintf oc "Begin_root(%s)\n" v';
increase_indent();
iprintf oc "%s = camlidl_alloc_small(1, %d);\n" v !tag_constr;
incr tag_constr;
iprintf oc "Field(%s, 0) = %s;\n" v v';
decrease_indent();
iprintf oc "End_roots()\n";
iprintf oc "break;\n";
decrease_indent())
lbls in
iprintf oc "switch (%s) {\n" discr;
List.iter emit_case ud.ud_cases;
if not !have_default then begin
iprintf oc "default:\n";
iprintf oc " caml_invalid_argument(\"%s: bad discriminant for union %s\");\n"
!current_function ud.ud_name
end;
iprintf oc "}\n"
|