/* * Copyright (C)2005-2022 Haxe Foundation * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ type pos = Lexer.pos type mutflag { Mutable; Immutable; } type t; type type_expr { TAbstract; TMono : int; TPoly; TRecord : (string , mutflag , t) list; TUnion : (int , (string , t) list); TTuple : t list; TLink : t; TFun : (t list , t); TNamed : (string list , t list , t); } type t { mutable tid : int; mutable texpr : type_expr; } type tconstant { TVoid; TInt : int; TBool : bool; TFloat : string; TString : string; TChar : char; TIdent : string; TConstr : string; TModule : (string list, tconstant); } type texpr; type match_op { MRoot; MFailure; MHandle : (match_op, match_op); MExecute : (texpr, bool); MConstants : (match_op, (tconstant, match_op) list); MField : (match_op, int); MTuple : (match_op, int); MToken : (match_op, int); MRecordField : (match_op, string, t); MJunk : (match_op, int, match_op); MSwitch : (match_op, (tconstant, match_op) list); MBind : (string, match_op, match_op); MWhen : (texpr, match_op); MNext : (match_op, match_op); } type texpr_decl { TConst : tconstant; TBlock : texpr list; TParenthesis : texpr; TCall : (texpr, texpr list); TField : (texpr, string); TArray : (texpr, texpr); TVar : (string list, texpr); TIf : (texpr, texpr, texpr option); TFunction : (bool, string, (string, t) list, texpr); TBinop : (string, texpr, texpr); TTupleDecl : texpr list; TTypeDecl : t; TMut : texpr ref; TRecordDecl : (string, texpr) list; TListDecl : texpr list; TUnop : (string, texpr); TMatch : (texpr, match_op, bool); TTry : (texpr, match_op); TTupleGet : (texpr, int); TErrorDecl : (string, t); TWhile : (texpr, texpr); } type texpr { edecl : texpr_decl; etype : t; epos : pos; } type id_gen = int ref function pos(e) { e.epos } function rec tlinks(name,t) { match t.texpr { | TLink t -> tlinks name t | TNamed (_,_,t) when !name -> tlinks name t | _ -> t.texpr } } function etype(name,e) { tlinks name e.etype } function genid(i) { i := *i + 1; *i } function generator() { &0 } function mk(e,t,p) { { edecl = e; etype = t; epos = p; } } var t_abstract = { tid = -1; texpr = TAbstract } function abstract(s) { tid = -1; texpr = TNamed [s] [] t_abstract; } // theses are not really unique since they're declared // also in Core, so it's not possible to use physeq var t_void = abstract "void"; var t_int = abstract "int"; var t_float = abstract "float"; var t_char = abstract "char"; var t_error = abstract "error"; var t_string = abstract "string" var t_bool = { tid = -1; texpr = TNamed ["bool"] [] ({ tid = -1; texpr = TUnion 2 [ ("true",t_abstract); ("false",t_abstract) ]; }); } function rec is_int(t) { match t.texpr { | TNamed(["int"],_,_) -> true | TLink t -> is_int t | _ -> false } } function t_mono(g) { { tid = -2; texpr = TMono (genid g); } } function t_polymorph(g) { { tid = genid g; texpr = TPoly; } } function t_poly(g,name) { var param = t_mono g; ({ tid = genid g; texpr = TNamed [name] [param] ({ tid = -1; texpr = TAbstract }); } , param) } function mk_fun(g,params,ret) { { tid = if List.exists (function(t) { t.tid != -1 }) (ret :: params) then genid g else -1; texpr = TFun params ret; } } function mk_tup(g,l) { { tid = if List.exists (function(t) { t.tid != -1 }) l then genid g else -1; texpr = TTuple l; } } function mk_record(g,fl) { { tid = if List.exists (function((_,_,t)) { t.tid != -1 }) fl then genid g else -1; texpr = TRecord fl; } } function mk_union(g,fl) { { tid = if List.exists (function((_,t)) { t.tid != -1 }) fl then genid g else -1; texpr = TUnion List.length(fl) fl; } } function rec file_name(m : string list,ext) { match m { | [] -> invalid_arg() | [x] -> x + ext | p :: l -> var c = String.get p 0; var p = if c < 'A' || c > 'Z' then p else { var p = String.sub p 0 (String.length p); String.set p 0 chr(ord c - ord 'A' + ord 'a'); p } p + "/" + file_name l ext } } type print_infos { mutable pi_mcount : int; mutable pi_pcount : int; mutable pi_ml : (t, int) list; mutable pi_ph : (int, int) Hashtbl.t; } function s_context() { { pi_mcount = 0; pi_pcount = 0; pi_ml = []; pi_ph = Hashtbl.create(); } } function poly_id(n) { if n < 26 then String.make 1 chr(ord 'a' + n) else string (n - 25) } function s_mutable(m) { match m { | Mutable -> "mutable " | Immutable -> "" } } function rec s_type(ext,h,t) { match t.texpr { | TAbstract -> "" | TMono _ -> sprintf "'_%s" poly_id(try if t.tid != -2 then assert(); List.phys t h.pi_ml; catch { Not_found -> var k = h.pi_mcount; h.pi_mcount := h.pi_mcount + 1; h.pi_ml := (t,k) :: h.pi_ml; k }) | TPoly -> sprintf "'%s" poly_id(try if t.tid == -1 then assert(); Hashtbl.find h.pi_ph t.tid catch { Not_found -> var k = h.pi_pcount; h.pi_pcount := h.pi_pcount + 1; Hashtbl.add h.pi_ph t.tid k; k }) | TRecord fl -> sprintf "{ %s }" (String.concat "; " (List.map (function((f,m,t)) { s_mutable m + f + " : " + s_type false h t }) fl)) | TUnion (_,fl) -> sprintf "{ %s }" (String.concat "; " (List.map (function((f,t)) { f + " : " + s_type false h t }) fl)) | TTuple l -> sprintf "(%s)" (String.concat ", " (List.map (s_type false h) l)) | TLink t -> s_type ext h t | TFun (tl,r) -> var l = String.concat " -> " (List.map (s_fun false h) tl) + " -> "; l + s_type false h r | TNamed (name,params,t) -> var s = match params { | [] -> "" | [p] -> s_type false h p + " " | l -> "(" + String.concat ", " (List.map (s_type false h) l) + ") " }; var name = String.concat "." name; if ext then s + name + " = " + s_type false h t else s + name } } function rec s_fun(ext,h,t) { match t.texpr { | TLink t -> s_fun ext h t | TFun _ -> "(" + s_type ext h t + ")" | _ -> s_type ext h t } } function rec duplicate(g,h,t) { if t.tid < 0 then t else try Hashtbl.find h t.tid catch { Not_found -> var t2 = { tid = genid g; texpr = TAbstract; }; Hashtbl.add h t.tid t2; t2.texpr := match t.texpr { | TAbstract -> TAbstract | TMono _ -> assert() | TPoly -> t2.tid := -2; TMono (genid g) | TRecord tl -> TRecord (List.map (function((n,m,t)) { (n , m, duplicate g h t) }) tl) | TUnion (n,tl) -> TUnion n (List.map (function((n,t)) { (n , duplicate g h t) }) tl) | TTuple tl -> TTuple (List.map (duplicate g h) tl) | TLink t -> TLink (duplicate g h t) | TFun (tl,t) -> TFun (List.map (duplicate g h) tl) (duplicate g h t) | TNamed (n,p,t) -> TNamed n (List.map (duplicate g h) p) (duplicate g h t) }; t2 } } function rec polymorphize(g,mink,t) { if t.tid == -1 then () else match t.texpr { | TAbstract -> () | TMono k -> if k > mink then { t.texpr := TPoly; t.tid := genid g } | TPoly -> () | TRecord fl -> List.iter (function((_,_,t)) { polymorphize g mink t }) fl | TUnion (_,fl) -> List.iter (function((_,t)) { polymorphize g mink t }) fl | TTuple tl -> List.iter (polymorphize g mink) tl | TLink t -> polymorphize g mink t | TFun (tl,t) -> List.iter (polymorphize g mink) tl; polymorphize g mink t | TNamed (_,tl,t) -> List.iter (polymorphize g mink) tl } }