Skip to content
This repository
tag: v466
Fetching contributors…

Cannot retrieve contributors at this time

file 271 lines (240 sloc) 10.033 kb
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
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA 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 Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)

(* see .mli *)

(* depends *)
module Format = Base.Format
module List = BaseList

(* shorthands *)
module Q = QmlAst

(* aliases *)
module TypeIdent = QmlAst.TypeIdent
module TypeIdentSet = QmlAst.TypeIdentSet
module TypeVarSet = QmlTypeVars.TypeVarSet

(* refactoring in progress *)
let (|>) = InfixOperator.(|>)
let (@*) = InfixOperator.(@*)

(* go *)

module Basic = struct
  let string = Q.TypeConst(Q.TyString)
end

module Inspect =
struct
(** Way too many functions with very close names !! Which one should I use ? Needs doc !! *)
  (* Here there are many function in "*_no_sum" version, which are
intended for compatibility with old qmlkernel (where there were lists
instead of sums). There is no good reason to use them any more.
*)

  (* Can raise exception [QmlTyperException.Exception]. *)
  let find_and_specialize gamma typeident args =
    (* This function is used by the back-end and needs to access representation
of types anyway. The typechecking will have to have ensured that types
even if not legally visible were used in a consistent way. *)
    let (typescheme, _) =
      QmlTypes.Env.TypeIdent.find ~visibility_applies: false typeident gamma in
    let out = QmlTypes.Scheme.specialize ~typeident ~ty: args typescheme in
    begin
      #<If:TYPER>
        let compare_record (a, _) (b, _) = Pervasives.compare a b in
        let sort_record r = List.sort compare_record r in
        let compare_sum a b =
          let a = sort_record a in
          let b = sort_record b in
          (List.make_compare compare_record) a b
        in
        let check compare print li =
          match li with
          | hd::tl ->
              if List.fold_left
                (fun (b, s) s2 ->
                  let c = compare s s2 in
                  if c >= 0 then (true, s2)
                  else (b, s2)
              )
                (false, hd) tl
                |> fst
              then (
                print ();
                let err_ctxt = QmlError.Context.ty out in
                QmlError.i_error
                  None err_ctxt
                  "the typename : %s has non-ordonned fields"
                  (TypeIdent.to_string typeident)
              )
              else ()
          | _ -> ()
        in
        begin match out with
        | Q.TypeSum (Q.TyCol (li, _)) ->
            check compare_sum (fun () ->
              List.iter (fun li ->
                prerr_endline (String.concat ", " (List.map fst li))
              ) li
            ) li
        | Q.TypeRecord (Q.TyRow (li, _) ) ->
            check compare_record (fun () -> List.iter (prerr_endline @* fst) li) li
        | _ -> ()
        end;
        #<End>;
      out
    end

  let follow_alias gamma = function
    | Q.TypeName (l, s)-> Some (find_and_specialize gamma s l)
    | _ -> None

  let follow_alias_noopt gamma t =
    let rec aux memo t =
      match t with
      | Q.TypeName (l, s) ->
          if TypeIdentSet.mem s memo then t (* infinite named type *) else
          let memo = TypeIdentSet.add s memo in
          aux memo (find_and_specialize gamma s l)
      | ty -> ty
    in
    aux TypeIdentSet.empty t

  let follow_alias_noopt_private gamma t =
    let rec aux memo t =
      match t with
      | Q.TypeName (l, s) ->
          if TypeIdentSet.mem s memo then t (* infinite named type *) else
          let memo = TypeIdentSet.add s memo in
          aux memo (find_and_specialize gamma s l)
      | ty -> ty
    in
    aux TypeIdentSet.empty t



  (* ************************************************************************ *)
  (** {b Descr}: See .mli file.
{b Visibility}: Exported outside this module. *)
  (* ************************************************************************ *)
  exception Escaping_private_type of QmlAst.ty



  (* ************************************************************************ *)
  (** {b Descr}: See .mli file.
This function makes a descent on the type. Each time it finds a named type
it looks in the environment for the bound type definition and visibility.
If the visibility is @private, an error is raise.
If the visibility is @abstract, then since the abstraction hides anything,
it also hides possibly present private types that are hence not visible
anymore.
If it is @public, it continues descending on the body of the scheme
bound to the name. Note that we do not specialize the scheme: we only
inspect the arguments of the named type expression and the body of the
scheme, telling that if there is no escaping type in the arguments, then
when these arguments instantiate the scheme, they wont add any escaping
possibility. And by inspecting the body of the scheme, we inspect its
structure without boring about the arguments since they were already
checked. If the structure of the scheme's body doesn't cause escaping,
then finally all is fine.
{b Visibility}: Exported outside this module. *)
  (* ************************************************************************ *)
  let check_no_private_type_escaping gamma initial_ty =
    let rec inspect memo ty =
      match ty with
      | Q.TypeConst _ | Q.TypeVar _ | Q.TypeAbstract -> ()
      | Q.TypeArrow (args_ty, res_ty) ->
          List.iter (inspect memo) args_ty ;
          inspect memo res_ty
      | Q.TypeRecord (Q.TyRow (fields, _)) ->
          List.iter (fun (_, field_ty) -> inspect memo field_ty) fields
      | Q.TypeSum (Q.TyCol (cases, _)) ->
          List.iter
            (fun fields ->
              List.iter (fun (_, field_ty) -> inspect memo field_ty) fields)
            cases
      | Q.TypeSumSugar _ -> assert false (* Should not remain some. *)
      | Q.TypeName (args_ty, name) ->
          let memo' =
            if not (TypeIdentSet.mem name memo) then (
              (* Recover the definition bound to this name. *)
              let (sch, _, visibility) =
                QmlTypes.Env.TypeIdent.raw_find name gamma in
              let extended_memo = TypeIdentSet.add name memo in
              (match visibility with
              | Q.TDV_public ->
                  (* As writen in the header of this function, since we always
inspect the arguments of the named type expression, we
don't need to explicitely instantiate the scheme and
directly inspect its body. Instantiation would only have
grafted in the body the arguments types. And because these
ones will be checked apart, no need to do the job twice. *)
                  let (_, body, _) = QmlGenericScheme.export_unsafe sch in
                  inspect extended_memo body
              | Q.TDV_private _ ->
                  (* This type being private, we can't allow it. *)
                  raise (Escaping_private_type ty)
              | Q.TDV_abstract _ ->
                  (* Ok, no need to descend deeper since the abstraction hides
anything, also possibly present private types. *)
                  ()) ;
              extended_memo
             )
            else memo in
          (* In any case, inspect the arguments of the named type expression. *)
          List.iter (inspect memo') args_ty
      | Q.TypeForall (_, _, _, body_ty) -> inspect memo body_ty in
    inspect TypeIdentSet.empty initial_ty

  let rec get_deeper_typename gamma ty =
    match ty with
    | Q.TypeName (args, n) -> (
        let (_, _, vis) = QmlTypes.Env.TypeIdent.raw_find n gamma in
        match vis with
        | QmlAst.TDV_public -> (
            let aliased_ty = find_and_specialize gamma n args in
            match aliased_ty with
            | Q.TypeName _ -> get_deeper_typename gamma aliased_ty
            | _ -> ty
           )
        | QmlAst.TDV_abstract _ | QmlAst.TDV_private _ -> ty
       )
    | _ -> ty

  let rec get_deeper_type_until gamma f ty =
    if f ty then ty
    else
      match follow_alias gamma ty with
      | Some ty -> get_deeper_type_until gamma f ty
      | None -> ty

  let rec is_type_arrow gamma ty =
    match follow_alias_noopt gamma ty with
    | Q.TypeArrow _ -> true
    | _ -> false

  let is_type_void gamma ty =
    match follow_alias_noopt gamma ty with
    | Q.TypeRecord (Q.TyRow ([], None)) ->
        true
    | Q.TypeSum (Q.TyCol ([ [ ] ], None)) ->
        true
    | _ -> false

  let rec is_type_bool gamma ty =
    match follow_alias_noopt gamma ty with
    | Q.TypeRecord (Q.TyRow ([ ( "false" | "true" ), ty], None)) ->
        is_type_void gamma ty
    | Q.TypeSum (Q.TyCol ([ ["false", tyf ] ; ["true", tyt ] ], None)) ->
        (is_type_void gamma tyt) && (is_type_void gamma tyf)
    | _ -> false

  let get_arrow_params gamma ty =
    match follow_alias_noopt gamma ty with
    | Q.TypeArrow (tl, _t2) -> Some tl
    | _ -> None

  let rec get_arrow_through_alias_and_private gamma ty =
    match follow_alias_noopt gamma ty with
    | Q.TypeArrow (tl, t2) -> Some (tl,t2)
    | _ -> None
end

module TypeArrow =
struct
  type 'a type_arrow_utils = Q.ty list -> Q.ty -> 'a

  let curryfied_arity args ty =
    let rec aux cpt = function
      | Q.TypeArrow (args,t) -> aux (cpt + (List.length args)) t
      | _ -> cpt
    in
    aux (List.length args) ty
end
Something went wrong with that request. Please try again.