| 1 |
/* |
| 2 |
* Copyright (C)2005-2022 Haxe Foundation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 |
* DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
type pos = Lexer.pos |
| 24 |
|
| 25 |
type mutflag { |
| 26 |
Mutable; |
| 27 |
Immutable; |
| 28 |
} |
| 29 |
|
| 30 |
type t; |
| 31 |
|
| 32 |
type type_expr { |
| 33 |
TAbstract; |
| 34 |
TMono : int; |
| 35 |
TPoly; |
| 36 |
TRecord : (string , mutflag , t) list; |
| 37 |
TUnion : (int , (string , t) list); |
| 38 |
TTuple : t list; |
| 39 |
TLink : t; |
| 40 |
TFun : (t list , t); |
| 41 |
TNamed : (string list , t list , t); |
| 42 |
} |
| 43 |
|
| 44 |
type t { |
| 45 |
mutable tid : int; |
| 46 |
mutable texpr : type_expr; |
| 47 |
} |
| 48 |
|
| 49 |
type tconstant { |
| 50 |
TVoid; |
| 51 |
TInt : int; |
| 52 |
TBool : bool; |
| 53 |
TFloat : string; |
| 54 |
TString : string; |
| 55 |
TChar : char; |
| 56 |
TIdent : string; |
| 57 |
TConstr : string; |
| 58 |
TModule : (string list, tconstant); |
| 59 |
} |
| 60 |
|
| 61 |
type texpr; |
| 62 |
|
| 63 |
type match_op { |
| 64 |
MRoot; |
| 65 |
MFailure; |
| 66 |
MHandle : (match_op, match_op); |
| 67 |
MExecute : (texpr, bool); |
| 68 |
MConstants : (match_op, (tconstant, match_op) list); |
| 69 |
MField : (match_op, int); |
| 70 |
MTuple : (match_op, int); |
| 71 |
MToken : (match_op, int); |
| 72 |
MRecordField : (match_op, string, t); |
| 73 |
MJunk : (match_op, int, match_op); |
| 74 |
MSwitch : (match_op, (tconstant, match_op) list); |
| 75 |
MBind : (string, match_op, match_op); |
| 76 |
MWhen : (texpr, match_op); |
| 77 |
MNext : (match_op, match_op); |
| 78 |
} |
| 79 |
|
| 80 |
type texpr_decl { |
| 81 |
TConst : tconstant; |
| 82 |
TBlock : texpr list; |
| 83 |
TParenthesis : texpr; |
| 84 |
TCall : (texpr, texpr list); |
| 85 |
TField : (texpr, string); |
| 86 |
TArray : (texpr, texpr); |
| 87 |
TVar : (string list, texpr); |
| 88 |
TIf : (texpr, texpr, texpr option); |
| 89 |
TFunction : (bool, string, (string, t) list, texpr); |
| 90 |
TBinop : (string, texpr, texpr); |
| 91 |
TTupleDecl : texpr list; |
| 92 |
TTypeDecl : t; |
| 93 |
TMut : texpr ref; |
| 94 |
TRecordDecl : (string, texpr) list; |
| 95 |
TListDecl : texpr list; |
| 96 |
TUnop : (string, texpr); |
| 97 |
TMatch : (texpr, match_op, bool); |
| 98 |
TTry : (texpr, match_op); |
| 99 |
TTupleGet : (texpr, int); |
| 100 |
TErrorDecl : (string, t); |
| 101 |
TWhile : (texpr, texpr); |
| 102 |
} |
| 103 |
|
| 104 |
type texpr { |
| 105 |
edecl : texpr_decl; |
| 106 |
etype : t; |
| 107 |
epos : pos; |
| 108 |
} |
| 109 |
|
| 110 |
type id_gen = int ref |
| 111 |
|
| 112 |
function pos(e) { |
| 113 |
e.epos |
| 114 |
} |
| 115 |
|
| 116 |
function rec tlinks(name,t) { |
| 117 |
match t.texpr { |
| 118 |
| TLink t -> tlinks name t |
| 119 |
| TNamed (_,_,t) when !name -> tlinks name t |
| 120 |
| _ -> t.texpr |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
function etype(name,e) { |
| 125 |
tlinks name e.etype |
| 126 |
} |
| 127 |
|
| 128 |
function genid(i) { |
| 129 |
i := *i + 1; |
| 130 |
*i |
| 131 |
} |
| 132 |
|
| 133 |
function generator() { |
| 134 |
&0 |
| 135 |
} |
| 136 |
|
| 137 |
function mk(e,t,p) { |
| 138 |
{ |
| 139 |
edecl = e; |
| 140 |
etype = t; |
| 141 |
epos = p; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
var t_abstract = { tid = -1; texpr = TAbstract } |
| 146 |
|
| 147 |
function abstract(s) { |
| 148 |
tid = -1; |
| 149 |
texpr = TNamed [s] [] t_abstract; |
| 150 |
} |
| 151 |
|
| 152 |
// theses are not really unique since they're declared |
| 153 |
// also in Core, so it's not possible to use physeq |
| 154 |
var t_void = abstract "void"; |
| 155 |
var t_int = abstract "int"; |
| 156 |
var t_float = abstract "float"; |
| 157 |
var t_char = abstract "char"; |
| 158 |
var t_error = abstract "error"; |
| 159 |
var t_string = abstract "string" |
| 160 |
var t_bool = { |
| 161 |
tid = -1; |
| 162 |
texpr = TNamed ["bool"] [] ({ |
| 163 |
tid = -1; |
| 164 |
texpr = TUnion 2 [ |
| 165 |
("true",t_abstract); |
| 166 |
("false",t_abstract) |
| 167 |
]; |
| 168 |
}); |
| 169 |
} |
| 170 |
|
| 171 |
function rec is_int(t) { |
| 172 |
match t.texpr { |
| 173 |
| TNamed(["int"],_,_) -> true |
| 174 |
| TLink t -> is_int t |
| 175 |
| _ -> false |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
function t_mono(g) { |
| 180 |
{ |
| 181 |
tid = -2; |
| 182 |
texpr = TMono (genid g); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
function t_polymorph(g) { |
| 187 |
{ |
| 188 |
tid = genid g; |
| 189 |
texpr = TPoly; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
function t_poly(g,name) { |
| 194 |
var param = t_mono g; |
| 195 |
({ |
| 196 |
tid = genid g; |
| 197 |
texpr = TNamed [name] [param] ({ tid = -1; texpr = TAbstract }); |
| 198 |
} , param) |
| 199 |
} |
| 200 |
|
| 201 |
function mk_fun(g,params,ret) { |
| 202 |
{ |
| 203 |
tid = if List.exists (function(t) { t.tid != -1 }) (ret :: params) then genid g else -1; |
| 204 |
texpr = TFun params ret; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
function mk_tup(g,l) { |
| 209 |
{ |
| 210 |
tid = if List.exists (function(t) { t.tid != -1 }) l then genid g else -1; |
| 211 |
texpr = TTuple l; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
function mk_record(g,fl) { |
| 216 |
{ |
| 217 |
tid = if List.exists (function((_,_,t)) { t.tid != -1 }) fl then genid g else -1; |
| 218 |
texpr = TRecord fl; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
function mk_union(g,fl) { |
| 223 |
{ |
| 224 |
tid = if List.exists (function((_,t)) { t.tid != -1 }) fl then genid g else -1; |
| 225 |
texpr = TUnion List.length(fl) fl; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
function rec file_name(m : string list,ext) { |
| 230 |
match m { |
| 231 |
| [] -> invalid_arg() |
| 232 |
| [x] -> x + ext |
| 233 |
| p :: l -> |
| 234 |
var c = String.get p 0; |
| 235 |
var p = if c < 'A' || c > 'Z' then p else { |
| 236 |
var p = String.sub p 0 (String.length p); |
| 237 |
String.set p 0 chr(ord c - ord 'A' + ord 'a'); |
| 238 |
p |
| 239 |
} |
| 240 |
p + "/" + file_name l ext |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
type print_infos { |
| 245 |
mutable pi_mcount : int; |
| 246 |
mutable pi_pcount : int; |
| 247 |
mutable pi_ml : (t, int) list; |
| 248 |
mutable pi_ph : (int, int) Hashtbl.t; |
| 249 |
} |
| 250 |
|
| 251 |
function s_context() { |
| 252 |
{ |
| 253 |
pi_mcount = 0; |
| 254 |
pi_pcount = 0; |
| 255 |
pi_ml = []; |
| 256 |
pi_ph = Hashtbl.create(); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
function poly_id(n) { |
| 261 |
if n < 26 then |
| 262 |
String.make 1 chr(ord 'a' + n) |
| 263 |
else |
| 264 |
string (n - 25) |
| 265 |
} |
| 266 |
|
| 267 |
function s_mutable(m) { |
| 268 |
match m { |
| 269 |
| Mutable -> "mutable " |
| 270 |
| Immutable -> "" |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
function rec s_type(ext,h,t) { |
| 275 |
match t.texpr { |
| 276 |
| TAbstract -> "<abstract>" |
| 277 |
| TMono _ -> |
| 278 |
sprintf "'_%s" poly_id(try |
| 279 |
if t.tid != -2 then assert(); |
| 280 |
List.phys t h.pi_ml; |
| 281 |
catch { Not_found -> |
| 282 |
var k = h.pi_mcount; |
| 283 |
h.pi_mcount := h.pi_mcount + 1; |
| 284 |
h.pi_ml := (t,k) :: h.pi_ml; |
| 285 |
k |
| 286 |
}) |
| 287 |
| TPoly -> |
| 288 |
sprintf "'%s" poly_id(try |
| 289 |
if t.tid == -1 then assert(); |
| 290 |
Hashtbl.find h.pi_ph t.tid |
| 291 |
catch { Not_found -> |
| 292 |
var k = h.pi_pcount; |
| 293 |
h.pi_pcount := h.pi_pcount + 1; |
| 294 |
Hashtbl.add h.pi_ph t.tid k; |
| 295 |
k |
| 296 |
}) |
| 297 |
| TRecord fl -> |
| 298 |
sprintf "{ %s }" (String.concat "; " (List.map (function((f,m,t)) { s_mutable m + f + " : " + s_type false h t }) fl)) |
| 299 |
| TUnion (_,fl) -> |
| 300 |
sprintf "{ %s }" (String.concat "; " (List.map (function((f,t)) { f + " : " + s_type false h t }) fl)) |
| 301 |
| TTuple l -> |
| 302 |
sprintf "(%s)" (String.concat ", " (List.map (s_type false h) l)) |
| 303 |
| TLink t -> |
| 304 |
s_type ext h t |
| 305 |
| TFun (tl,r) -> |
| 306 |
var l = String.concat " -> " (List.map (s_fun false h) tl) + " -> "; |
| 307 |
l + s_type false h r |
| 308 |
| TNamed (name,params,t) -> |
| 309 |
var s = match params { |
| 310 |
| [] -> "" |
| 311 |
| [p] -> s_type false h p + " " |
| 312 |
| l -> "(" + String.concat ", " (List.map (s_type false h) l) + ") " |
| 313 |
}; |
| 314 |
var name = String.concat "." name; |
| 315 |
if ext then |
| 316 |
s + name + " = " + s_type false h t |
| 317 |
else |
| 318 |
s + name |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
function rec s_fun(ext,h,t) { |
| 323 |
match t.texpr { |
| 324 |
| TLink t -> s_fun ext h t |
| 325 |
| TFun _ -> "(" + s_type ext h t + ")" |
| 326 |
| _ -> s_type ext h t |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
function rec duplicate(g,h,t) { |
| 331 |
if t.tid < 0 then |
| 332 |
t |
| 333 |
else try |
| 334 |
Hashtbl.find h t.tid |
| 335 |
catch { Not_found -> |
| 336 |
var t2 = { |
| 337 |
tid = genid g; |
| 338 |
texpr = TAbstract; |
| 339 |
}; |
| 340 |
Hashtbl.add h t.tid t2; |
| 341 |
t2.texpr := match t.texpr { |
| 342 |
| TAbstract -> TAbstract |
| 343 |
| TMono _ -> assert() |
| 344 |
| TPoly -> t2.tid := -2; TMono (genid g) |
| 345 |
| TRecord tl -> TRecord (List.map (function((n,m,t)) { (n , m, duplicate g h t) }) tl) |
| 346 |
| TUnion (n,tl) -> TUnion n (List.map (function((n,t)) { (n , duplicate g h t) }) tl) |
| 347 |
| TTuple tl -> TTuple (List.map (duplicate g h) tl) |
| 348 |
| TLink t -> TLink (duplicate g h t) |
| 349 |
| TFun (tl,t) -> TFun (List.map (duplicate g h) tl) (duplicate g h t) |
| 350 |
| TNamed (n,p,t) -> TNamed n (List.map (duplicate g h) p) (duplicate g h t) |
| 351 |
}; |
| 352 |
t2 |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
function rec polymorphize(g,mink,t) { |
| 357 |
if t.tid == -1 then |
| 358 |
() |
| 359 |
else match t.texpr { |
| 360 |
| TAbstract -> () |
| 361 |
| TMono k -> if k > mink then { t.texpr := TPoly; t.tid := genid g } |
| 362 |
| TPoly -> () |
| 363 |
| TRecord fl -> List.iter (function((_,_,t)) { polymorphize g mink t }) fl |
| 364 |
| TUnion (_,fl) -> List.iter (function((_,t)) { polymorphize g mink t }) fl |
| 365 |
| TTuple tl -> List.iter (polymorphize g mink) tl |
| 366 |
| TLink t -> polymorphize g mink t |
| 367 |
| TFun (tl,t) -> List.iter (polymorphize g mink) tl; polymorphize g mink t |
| 368 |
| TNamed (_,tl,t) -> List.iter (polymorphize g mink) tl |
| 369 |
} |
| 370 |
} |