3
* Copyright (c)2005-2008 Nicolas Cannasse
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
mutable api : context_type_api;
26
types_module : (path, path) Hashtbl.t;
27
modules : (path , module_def) Hashtbl.t;
28
delays : (unit -> unit) list list ref;
29
constructs : (path , Ast.access list * Ast.type_param list * Ast.func) Hashtbl.t;
31
mutable std : module_def;
32
mutable untyped : bool;
33
mutable super_call : bool;
36
mutable local_types : module_type list;
38
mutable curclass : tclass;
40
mutable type_params : (string * t) list;
42
mutable curmethod : string;
43
mutable in_constructor : bool;
44
mutable in_static : bool;
45
mutable in_loop : bool;
46
mutable in_display : bool;
48
mutable locals : (string, t) PMap.t;
49
mutable locals_map : (string, string) PMap.t;
50
mutable locals_map_inv : (string, string) PMap.t;
51
mutable opened : anon_status ref list;
52
mutable param_type : t option;
56
| Module_not_found of path
57
| Unify of unify_error list
59
| Protect of error_msg
60
| Unknown_ident of string
61
| Invalid_enum_matching
62
| Stack of error_msg * error_msg
64
exception Error of error_msg * pos
66
let type_expr_ref : (typer -> Ast.expr -> bool -> texpr) ref = ref (fun _ _ _ -> assert false)
67
let build_inheritance : (typer -> Type.tclass -> Ast.pos -> Ast.class_flag -> bool) ref = ref (fun _ _ _ _ -> true)
69
let unify_error_msg ctx = function
70
| Cannot_unify (t1,t2) ->
71
s_type ctx t1 ^ " should be " ^ s_type ctx t2
72
| Invalid_field_type s ->
73
"Invalid type for field " ^ s ^ " :"
74
| Has_no_field (t,n) ->
75
s_type ctx t ^ " has no field " ^ n
76
| Has_extra_field (t,n) ->
77
s_type ctx t ^ " has extra field " ^ n
78
| Invalid_access (f,get,a,b) ->
79
"Inconsistent " ^ (if get then "getter" else "setter") ^ " for field " ^ f ^ " : " ^ s_access a ^ " should be " ^ s_access b
80
| Invalid_visibility n ->
81
"The field " ^ n ^ " is not public"
82
| Not_matching_optional n ->
83
"Optional attribute of parameter " ^ n ^ " differs"
84
| Cant_force_optional ->
85
"Optional parameters can't be forced"
87
let rec error_msg = function
88
| Module_not_found m -> "Class not found : " ^ Ast.s_type_path m
90
let ctx = print_context() in
91
String.concat "\n" (List.map (unify_error_msg ctx) l)
92
| Unknown_ident s -> "Unknown identifier : " ^ s
93
| Invalid_enum_matching -> "Invalid enum matching case"
95
| Stack (m1,m2) -> error_msg m1 ^ "\n" ^ error_msg m2
96
| Protect m -> error_msg m
98
let display_error ctx msg p = ctx.com.error msg p
100
let error msg p = raise (Error (Custom msg,p))
102
let type_expr ctx e need_val = (!type_expr_ref) ctx e need_val
104
let unify ctx t1 t2 p =
109
if not ctx.untyped then display_error ctx (error_msg (Unify l)) p
111
let unify_raise ctx t1 t2 p =
116
(* no untyped check *)
117
raise (Error (Unify l,p))
119
let save_locals ctx =
120
let locals = ctx.locals in
121
let map = ctx.locals_map in
122
let inv = ctx.locals_map_inv in
124
ctx.locals <- locals;
125
ctx.locals_map <- map;
126
ctx.locals_map_inv <- inv;
129
let add_local ctx v t =
131
let nv = (if n = 0 then v else v ^ string_of_int n) in
132
if PMap.mem nv ctx.locals || PMap.mem nv ctx.locals_map_inv then
135
ctx.locals <- PMap.add v t ctx.locals;
137
ctx.locals_map <- PMap.add v nv ctx.locals_map;
138
ctx.locals_map_inv <- PMap.add nv v ctx.locals_map_inv;
145
let gen_local ctx t =
147
let nv = (if n = 0 then "_g" else "_g" ^ string_of_int n) in
148
if PMap.mem nv ctx.locals || PMap.mem nv ctx.locals_map_inv then
153
add_local ctx (loop 0) t
155
let rec is_nullable = function
157
(match !r with None -> true | Some t -> is_nullable t)
158
| TType ({ t_path = ([],"Null") },[_]) ->
163
is_nullable (apply_params t.t_types tl t.t_type)
166
| TInst ({ cl_path = (["haxe"],"Int32") },[])
167
| TInst ({ cl_path = ([],"Int") },[])
168
| TInst ({ cl_path = ([],"Float") },[])
169
| TEnum ({ e_path = ([],"Bool") },[]) -> true
173
let rec is_null = function
175
(match !r with None -> false | Some t -> is_null t)
176
| TType ({ t_path = ([],"Null") },[t]) ->
181
is_null (apply_params t.t_types tl t.t_type)
185
let not_opened = ref Closed
186
let mk_anon fl = TAnon { a_fields = fl; a_status = not_opened; }
188
let mk_field name t = {
193
cf_get = NormalAccess;
194
cf_set = NormalAccess;