package lablqml

  1. Overview
  2. Docs
OCamlfind package and PPX extension to interface OCaml and QtQuick

Install

Dune Dependency

Authors

Maintainers

Sources

0.7.tar.gz
sha256=20d8186990d189613f328d974b5216186783de24cd763d8f3e77939b048a57b5
sha512=30204036ac6f10dcae457b92a8bfee745338ad264f42158bf4b587ad84db6e4c601b6e2367baa3d990cb6ea63219bc764c577f026bfce57375a61c7f91abf011

doc/src/ppx_qt_rewriter/TypeRepr.ml.html

Source file TypeRepr.ml

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
open Format
open Ppxlib
open Base
open PpxQtCfg

(* TODO: add addtitional info about methods *)
(* We need this arginfo because void foo(QString) is not the same as void foo(const QString&) *)
type arg_info = { ai_ref : bool; ai_const : bool }

type meth_info = { mi_virt : bool; mi_const : bool }

let mi_empty = { mi_virt = false; mi_const = false }

let ai_empty = { ai_ref = false; ai_const = false }

let wrap_typ_simple x = (x, ai_empty)

let unref (x, y) = (x, { y with ai_ref = false })

let unconst (x, y) = (x, { y with ai_const = false })

module Arg : sig
  type default = [ `Default ]

  type model = [ `Model ]

  type cppobj = [ `Cppobj ]

  type non_cppobj = [ default | model ]

  type any = [ cppobj | non_cppobj ]

  type _ t =
    | Unit : [> `Default ] t
    | QString : [> `Default ] t
    | Int : [> `Default ] t
    | Bool : [> `Default ] t
    | QVariant : [> `Default ] t
    | QByteArray : [> `Default ] t
    | QList : 'a t -> 'a t
    | QModelIndex : [> `Model ] t
    | Cppobj : [> `Cppobj ] t

  val default_plus_model : default t -> [ default | model ] t

  val model_plus_default : model t -> [ default | model ] t

  val remove_cppobj : any t -> non_cppobj t option
end = struct
  type default = [ `Default ]

  type model = [ `Model ]

  type cppobj = [ `Cppobj ]

  type non_cppobj = [ default | model ]

  type any = [ cppobj | default | model ]

  type _ t =
    | Unit : [> `Default ] t
    | QString : [> `Default ] t
    | Int : [> `Default ] t
    | Bool : [> `Default ] t
    | QVariant : [> `Default ] t
    | QByteArray : [> `Default ] t
    | QList : 'a t -> 'a t
    | QModelIndex : [> `Model ] t
    | Cppobj : [> `Cppobj ] t

  let rec model_plus_default : model t -> [ default | model ] t = function
    | QModelIndex as y -> y
    | QList x -> QList (model_plus_default x)

  let rec cppobj_to_any : cppobj t -> any t = function
    | Cppobj -> Cppobj
    | QList x -> QList (cppobj_to_any x)

  let rec model_to_any : model t -> any t = function
    | QModelIndex -> QModelIndex
    | QList x -> QList (model_to_any x)

  let rec default_plus_model : default t -> [ default | model ] t = function
    | (Unit as y)
    | (QString as y)
    | (Int as y)
    | (Bool as y)
    | (QVariant as y)
    | (QByteArray as y) ->
        y
    | QList x -> QList (default_plus_model x)

  let rec remove_cppobj : any t -> non_cppobj t option = function
    | Cppobj -> None
    | Unit -> Some Unit
    | Int -> Some Int
    | Bool -> Some Bool
    | QByteArray -> Some QByteArray
    | QString -> Some QString
    | QModelIndex -> Some QModelIndex
    | QVariant -> Some QVariant
    | QList xs -> (
        match remove_cppobj xs with None -> None | Some y -> Some (QList y))
end
[@warning "-32"]

open Arg

let type_suits_prop ty =
  let open Result in
  match ty with
  | [%type: int] -> return Arg.Int
  | [%type: bool] -> return Arg.Bool
  | [%type: string] -> return Arg.QString
  | [%type: Variant.t] | [%type: QVariant.t] -> return Arg.QVariant
  | [%type: unit] -> fail "property can't have type 'unit'"
  | { ptyp_desc = Ptyp_constr ({ txt = Lident x; _ }, []); _ } ->
      fail (sprintf "Don't know what to do with '%s'" x)
  | _ -> fail "Type is unknown"

(* return list of pairs. 1st is argument label. 2nd is actual type *)
let eval_meth_typ_gen =
  let open Result in
  let rec parse_one t =
    match t.ptyp_desc with
    | Ptyp_constr ({ txt = Lident "list"; _ }, [ typarg ]) ->
        parse_one typarg >>= fun xs -> return @@ Arg.QList xs
    | Ptyp_constr ({ txt = Lident "string"; _ }, _) -> return @@ Arg.QString
    | Ptyp_constr ({ txt = Lident "unit"; _ }, _) -> return @@ Arg.Unit
    | Ptyp_constr ({ txt = Lident "int"; _ }, _) -> return @@ Arg.Int
    | Ptyp_constr ({ txt = Lident "variant"; _ }, _)
    | Ptyp_constr ({ txt = Ldot (Lident "Variant", "t"); _ }, _)
    | Ptyp_constr ({ txt = Ldot (Lident "QVariant", "t"); _ }, _) ->
        return Arg.QVariant
    | _ -> fail ("can't eval type", t.ptyp_loc)
  in
  let rec helper t =
    match t.ptyp_desc with
    | Ptyp_arrow (name, l, r) ->
        parse_one l >>= fun rez ->
        helper r >>= fun tl -> return ((name, rez) :: tl)
        (* [ name, parse_one l ] @ helper r *)
    | _ -> parse_one t >>= fun typ -> return [ (Nolabel, typ) ]
  in
  helper

let parse_arrow_type_exn typ =
  match eval_meth_typ_gen typ with
  | Result.Ok xs ->
      assert (List.length xs > 1);
      xs
  | Result.Error (msg, loc) -> ppxqt_failed ~loc msg

(* how many additional variables needed to convert C++ value to OCaml one *)
let aux_variables_count =
  let rec helper : non_cppobj Arg.t -> int = function
    | QByteArray | Bool | Int | Unit | QString | QModelIndex -> 0
    | QList x -> helper x + 2
    | QVariant -> 2
  in
  helper

(* how many additional variables needed to convert OCaml value to C++ one *)
let aux_variables_count_to_cpp =
  let rec helper : non_cppobj Arg.t -> int = function
    | QVariant | QByteArray | Bool | Int | Unit | QString | QModelIndex -> 0
    | QList x -> helper x + 2
  in
  helper

let rec ocaml_ast_of_typ : any Arg.t -> Longident.t =
 fun x ->
  let open Longident in
  match x with
  | Cppobj -> Lident "cppobj"
  | QVariant -> Ldot (Lident "QVariant", "t")
  | QModelIndex -> Ldot (Lident "QModelIndex", "t")
  | Bool -> Lident "bool"
  | Unit -> Lident "unit"
  | QByteArray | QString -> Lident "string"
  | Int -> Lident "int"
  | QList x -> Lapply (Lident "list", ocaml_ast_of_typ x)

let cpptyp_of_typ =
  let rec helper : non_cppobj Arg.t * arg_info -> string =
   fun (x, ai) ->
    match x with
    | Bool -> "bool"
    | Int -> "int"
    | QVariant -> "QVariant"
    | QString -> "QString"
    | QByteArray -> "QByteArray"
    | Unit -> "void"
    | QModelIndex ->
        sprintf "%sQModelIndex%s"
          (if ai.ai_const then "const " else "")
          (if ai.ai_ref then "&" else "")
    | QList x ->
        sprintf "%sQList<%s>%s"
          (if ai.ai_const then "const " else "")
          (helper (x, ai_empty))
          (if ai.ai_ref then "&" else "")
  in
  helper

let rec cpptyp_of_proptyp : default Arg.t * arg_info -> string =
 fun (typ, ai) ->
  let upcasted = (default_plus_model typ, ai) in
  let cppname =
    match typ with
    | Bool -> cpptyp_of_typ upcasted
    | Int -> cpptyp_of_typ upcasted
    | QVariant -> cpptyp_of_typ upcasted
    | QByteArray -> cpptyp_of_typ upcasted
    | QString -> cpptyp_of_typ upcasted
    | Unit -> failwith "should not happen"
    | QList x -> sprintf "QList<%s>" (cpptyp_of_proptyp (x, ai_empty))
  in
  sprintf "%s%s%s"
    (if ai.ai_const then "const " else "")
    cppname
    (if ai.ai_ref then "&" else "")

let string_suites_prop s =
  match s with
  | "int" -> Result.return Arg.Int
  | "bool" -> Result.return Arg.Bool
  | "string" -> Result.return Arg.QString
  | "variant" -> Result.return Arg.QVariant
  | _ -> Result.fail (Printf.sprintf "Type '%s' is unknown" s)
OCaml

Innovation. Community. Security.