[@@@warning "-9-27"]
include Ctypes
let lift x = x
open Ctypes_static

let rec field : type t a. t typ -> string -> a typ -> (a, t) field =
  fun s fname ftype -> match s, fname with
  | View { ty; _ }, _ ->
    let { ftype; foffset; fname } = field ty fname ftype in
    { ftype; foffset; fname }
  | _ -> failwith ("Unexpected field "^ fname)

let rec seal : type a. a typ -> unit = function
  | Struct { tag; spec = Complete _; _ } ->
    raise (ModifyingSealedType tag)
  | Union { utag; uspec = Some _; _ } ->
    raise (ModifyingSealedType utag)
  | View { ty; _ } -> seal ty
  | _ ->
    raise (Unsupported "Sealing a non-structured type")

type 'a const = 'a
let constant (type t) name (t : t typ) : t = match t, name with
  | Ctypes_static.Primitive Cstubs_internals.Int, "EVP_MAX_MD_SIZE" ->
    64
  | Ctypes_static.Primitive Cstubs_internals.Int, "X509_FILETYPE_ASN1" ->
    2
  | Ctypes_static.Primitive Cstubs_internals.Int, "X509_FILETYPE_PEM" ->
    1
  | Ctypes_static.Primitive Cstubs_internals.Int, "SSL_ERROR_SSL" ->
    1
  | Ctypes_static.Primitive Cstubs_internals.Int, "SSL_ERROR_SYSCALL" ->
    5
  | Ctypes_static.Primitive Cstubs_internals.Int, "SSL_ERROR_WANT_X509_LOOKUP" ->
    4
  | Ctypes_static.Primitive Cstubs_internals.Int, "SSL_ERROR_WANT_ACCEPT" ->
    8
  | Ctypes_static.Primitive Cstubs_internals.Int, "SSL_ERROR_WANT_CONNECT" ->
    7
  | Ctypes_static.Primitive Cstubs_internals.Int, "SSL_ERROR_WANT_WRITE" ->
    3
  | Ctypes_static.Primitive Cstubs_internals.Int, "SSL_ERROR_WANT_READ" ->
    2
  | Ctypes_static.Primitive Cstubs_internals.Int, "SSL_ERROR_ZERO_RETURN" ->
    6
  | Ctypes_static.Primitive Cstubs_internals.Int, "SSL_ERROR_NONE" ->
    0
  | Ctypes_static.Primitive Cstubs_internals.Long, "X509_V_OK" ->
    Signed.Long.of_string "0"
  | Ctypes_static.Primitive Cstubs_internals.Int, "SSL_VERIFY_CLIENT_ONCE" ->
    4
  | Ctypes_static.Primitive Cstubs_internals.Int, "SSL_VERIFY_FAIL_IF_NO_PEER_CERT" ->
    2
  | Ctypes_static.Primitive Cstubs_internals.Int, "SSL_VERIFY_PEER" ->
    1
  | Ctypes_static.Primitive Cstubs_internals.Int, "SSL_VERIFY_NONE" ->
    0
  | Ctypes_static.Primitive Cstubs_internals.Ulong, "SSL_OP_SINGLE_ECDH_USE" ->
    Unsigned.ULong.of_string "0"
  | Ctypes_static.Primitive Cstubs_internals.Ulong, "SSL_OP_SINGLE_DH_USE" ->
    Unsigned.ULong.of_string "0"
  | Ctypes_static.Primitive Cstubs_internals.Ulong, "SSL_OP_NO_TLSv1_3" ->
    Unsigned.ULong.of_string "536870912"
  | Ctypes_static.Primitive Cstubs_internals.Ulong, "SSL_OP_NO_TLSv1_2" ->
    Unsigned.ULong.of_string "134217728"
  | Ctypes_static.Primitive Cstubs_internals.Ulong, "SSL_OP_NO_TLSv1_1" ->
    Unsigned.ULong.of_string "268435456"
  | Ctypes_static.Primitive Cstubs_internals.Ulong, "SSL_OP_NO_TLSv1" ->
    Unsigned.ULong.of_string "67108864"
  | Ctypes_static.Primitive Cstubs_internals.Ulong, "SSL_OP_NO_SSLv3" ->
    Unsigned.ULong.of_string "33554432"
  | Ctypes_static.Primitive Cstubs_internals.Ulong, "SSL_OP_NO_SSLv2" ->
    Unsigned.ULong.of_string "0"
  | _, s -> failwith ("unmatched constant: "^ s)

let enum (type a) name ?typedef ?unexpected (alist : (a * int64) list) =
  match name with
  | s ->
    failwith ("unmatched enum: "^ s)
