File: enumdecl.ml

package info (click to toggle)
camlidl 1.12-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,592 kB
  • sloc: ml: 5,238; ansic: 945; cpp: 908; makefile: 358; xml: 213; sh: 74
file content (101 lines) | stat: -rw-r--r-- 3,366 bytes parent folder | download | duplicates (3)
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
(***********************************************************************)
(*                                                                     *)
(*                              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: enumdecl.ml,v 1.11 2000-08-19 11:04:56 xleroy Exp $ *)

(* Generation of converters for enums *)

open Printf
open Utils
open Variables
open Idltypes
open Cvttyp
open Cvtval
open Enum

(* Convert an IDL enum declaration to an ML datatype declaration *)

let ml_declaration oc en =
  if en.en_name = ""
  then fprintf oc "enum_%d =\n" en.en_stamp
  else fprintf oc "%s =\n" (String.uncapitalize_ascii en.en_name);
  List.iter
    (fun c -> fprintf oc "  | %s\n" (String.capitalize_ascii c.const_name))
    en.en_consts

(* Convert an IDL enum declaration to a C enum declaration *)

let c_declaration oc en =
  out_enum oc en; fprintf oc ";\n\n"

(* External (forward) declaration of the translation functions *)

let declare_transl oc en =
  fprintf oc "extern int camlidl_ml2c_%s_enum_%s(value);\n"
             en.en_mod en.en_name;
  fprintf oc "extern value camlidl_c2ml_%s_enum_%s(int);\n\n"
             en.en_mod en.en_name;
  fprintf oc "extern int camlidl_transl_table_%s_enum_%s[];\n\n"
             en.en_mod en.en_name

(* Translation function from an ML datatype to a C enum *)

let emit_transl_table oc en =
  fprintf oc "int camlidl_transl_table_%s_enum_%d[%d] = {\n"
             en.en_mod en.en_stamp (List.length en.en_consts);
  List.iter
    (fun c -> fprintf oc "  %s,\n" c.const_name)
    en.en_consts;
  fprintf oc "};\n\n"

let transl_ml_to_c oc en =
  current_function := sprintf "enum %s" en.en_name;
  let v = new_var "_v" in
  fprintf oc "int camlidl_ml2c_%s_enum_%s(value %s)\n"
             en.en_mod en.en_name v;
  fprintf oc "{\n";
  let pc = divert_output() in
  increase_indent();
  let c = new_c_variable (Type_int(Int, Iunboxed)) in
  enum_ml_to_c ml_to_c pc en v c;
  iprintf pc "return %s;\n" c;
  output_variable_declarations oc;
  end_diversion oc;
  decrease_indent();
  fprintf oc "}\n\n";
  current_function := ""

(* Translation function from a C enum to an ML datatype *)

let transl_c_to_ml oc en =
  current_function := sprintf "enum %s" en.en_name;
  let c = new_var "_c" in
  fprintf oc "value camlidl_c2ml_%s_enum_%s(int %s)\n"
             en.en_mod en.en_name c;
  fprintf oc "{\n";
  let pc = divert_output() in
  increase_indent();
  let v = new_ml_variable() in
  enum_c_to_ml c_to_ml pc en c v;
  iprintf pc "return %s;\n" v;
  output_variable_declarations oc;
  end_diversion oc;
  decrease_indent();
  fprintf oc "}\n\n";
  current_function := ""

(* Emit the translation functions *)

let emit_transl oc en =
  emit_transl_table oc en;
  transl_ml_to_c oc en;
  transl_c_to_ml oc en