Browse Source

[typer] add mk_typedef

Simon Krajewski 3 years ago
parent
commit
0450f55c5a
3 changed files with 31 additions and 54 deletions
  1. 14 0
      src/core/tFunctions.ml
  2. 12 36
      src/core/tOther.ml
  3. 5 18
      src/typing/typeloadModule.ml

+ 14 - 0
src/core/tFunctions.ml

@@ -135,6 +135,20 @@ let mk_class m path pos name_pos =
 		cl_descendants = [];
 	}
 
+let mk_typedef m path pos name_pos t =
+	{
+		t_path = path;
+		t_module = m;
+		t_pos = pos;
+		t_name_pos = name_pos;
+		t_private = false;
+		t_doc = None;
+		t_meta = [];
+		t_params = [];
+		t_using = [];
+		t_type = t;
+	}
+
 let module_extra file sign time kind policy =
 	{
 		m_file = Path.UniqueKey.create_lazy file;

+ 12 - 36
src/core/tOther.ml

@@ -227,44 +227,20 @@ end
 
 let no_meta = []
 
-let class_module_type c = {
-	t_path = [],"Class<" ^ (s_type_path c.cl_path) ^ ">" ;
-	t_module = c.cl_module;
-	t_doc = None;
-	t_pos = c.cl_pos;
-	t_name_pos = null_pos;
-	t_type = mk_anon ~fields:c.cl_statics (ref (Statics c));
-	t_private = true;
-	t_params = [];
-	t_using = [];
-	t_meta = no_meta;
-}
+let class_module_type c =
+	let path = ([],"Class<" ^ (s_type_path c.cl_path) ^ ">") in
+	let t = mk_anon ~fields:c.cl_statics (ref (Statics c)) in
+	{ (mk_typedef c.cl_module path c.cl_pos null_pos t) with t_private = true}
 
-let enum_module_type m path p  = {
-	t_path = [], "Enum<" ^ (s_type_path path) ^ ">";
-	t_module = m;
-	t_doc = None;
-	t_pos = p;
-	t_name_pos = null_pos;
-	t_type = mk_mono();
-	t_private = true;
-	t_params = [];
-	t_using = [];
-	t_meta = [];
-}
+let enum_module_type m path p  =
+	let path = ([], "Enum<" ^ (s_type_path path) ^ ">") in
+	let t = mk_mono() in
+	{(mk_typedef m path p null_pos t) with t_private = true}
 
-let abstract_module_type a tl = {
-	t_path = [],Printf.sprintf "Abstract<%s%s>" (s_type_path a.a_path) (s_type_params (ref []) tl);
-	t_module = a.a_module;
-	t_doc = None;
-	t_pos = a.a_pos;
-	t_name_pos = null_pos;
-	t_type = mk_anon (ref (AbstractStatics a));
-	t_private = true;
-	t_params = [];
-	t_using = [];
-	t_meta = no_meta;
-}
+let abstract_module_type a tl =
+	let path = ([],Printf.sprintf "Abstract<%s%s>" (s_type_path a.a_path) (s_type_params (ref []) tl)) in
+	let t = mk_anon (ref (AbstractStatics a)) in
+	{(mk_typedef a.a_module path a.a_pos null_pos t) with t_private = true}
 
 module TClass = struct
 	let get_member_fields' self_too c0 tl =

+ 5 - 18
src/typing/typeloadModule.ml

@@ -284,16 +284,9 @@ let module_pass_1 ctx m tdecls loadp =
 			has_declaration := true;
 			let priv = List.mem EPrivate d.d_flags in
 			let path = make_path name priv d.d_meta p in
-			let t = {
-				t_path = path;
-				t_module = m;
-				t_pos = p;
-				t_name_pos = pos d.d_name;
+			let t = {(mk_typedef m path p (pos d.d_name) (mk_mono())) with
 				t_doc = d.d_doc;
 				t_private = priv;
-				t_params = [];
-				t_using = [];
-				t_type = mk_mono();
 				t_meta = d.d_meta;
 			} in
 			(* failsafe in case the typedef is not initialized (see #3933) *)
@@ -516,17 +509,11 @@ let init_module_type ctx context_init (decl,p) =
 					typing_error "Type aliases must start with an uppercase letter" p;
 				let _, _, f = ctx.g.do_build_instance ctx t p_type in
 				(* create a temp private typedef, does not register it in module *)
-				let mt = TTypeDecl {
-					t_path = (fst md.m_path @ ["_" ^ snd md.m_path],name);
-					t_module = ctx.m.curmod;
-					t_pos = p;
-					t_name_pos = p;
+				let t_path = (fst md.m_path @ ["_" ^ snd md.m_path],name) in
+				let t_type = f (List.map snd (t_infos t).mt_params) in
+				let mt = TTypeDecl {(mk_typedef ctx.m.curmod t_path p p t_type) with
 					t_private = true;
-					t_doc = None;
-					t_meta = [];
-					t_params = (t_infos t).mt_params;
-					t_using = [];
-					t_type = f (List.map snd (t_infos t).mt_params);
+					t_params = (t_infos t).mt_params
 				} in
 				if ctx.is_display_file && DisplayPosition.display_position#enclosed_in p then
 					DisplayEmitter.display_module_type ctx mt p;