Browse Source

add @:forward.accessOnAbstract and use it for cs.PointerAccess

Simon Krajewski 1 year ago
parent
commit
bafb5fe4b2
3 changed files with 23 additions and 4 deletions
  1. 6 0
      src-json/meta.json
  2. 14 3
      src/typing/fields.ml
  3. 3 1
      std/cs/Pointer.hx

+ 6 - 0
src-json/meta.json

@@ -394,6 +394,12 @@
 		"targets": ["TAbstract"],
 		"links": ["https://haxe.org/manual/types-abstract-forward.html"]
 	},
+	{
+		"name": "ForwardAccessOnAbstract",
+		"metadata": ":forward.accessOnAbstract",
+		"doc": "Generates @:forward field access on the abstract itself instead of the underlying type.",
+		"targets": ["TAbstract"]
+	},
 	{
 		"name": "ForwardNew",
 		"metadata": ":forward.new",

+ 14 - 3
src/typing/fields.ml

@@ -272,7 +272,7 @@ let type_field cfg ctx e i p mode (with_type : WithType.t) =
 		| None -> raise Not_found
 	in
 	let type_field_by_et f e t =
-		f (mk (TCast(e,None)) t e.epos) (follow_without_type t)
+		f e (follow_without_type t)
 	in
 	let type_field_by_e f e =
 		f e (follow_without_type e.etype)
@@ -293,7 +293,15 @@ let type_field cfg ctx e i p mode (with_type : WithType.t) =
 		type_field_by_forward f Meta.ForwardStatics a
 	in
 	let type_field_by_forward_member f e a tl =
-		let f () = type_field_by_et f e (Abstract.get_underlying_type ~return_first:true a tl) in
+		let f () =
+			let t = Abstract.get_underlying_type ~return_first:true a tl in
+			let e = if Meta.has Meta.ForwardAccessOnAbstract a.a_meta then
+				e
+			else
+				mk (TCast(e,None)) t e.epos
+			in
+			type_field_by_et f e t
+		in
 		type_field_by_forward f Meta.Forward a
 	in
 	let type_field_by_typedef f e td tl =
@@ -372,7 +380,10 @@ let type_field cfg ctx e i p mode (with_type : WithType.t) =
 					field_access f FHAnon
 				)
 			| CTypes tl ->
-				type_field_by_list (fun (t,_) -> type_field_by_et type_field_by_type e t) tl
+				type_field_by_list (fun (t,_) ->
+					let e = mk (TCast(e,None)) t e.epos in
+					type_field_by_et type_field_by_type e t
+				) tl
 			| CUnknown ->
 				if not (List.exists (fun (m,_) -> m == r) ctx.monomorphs.perfunction) && not (ctx.untyped && ctx.com.platform = Neko) then
 					ctx.monomorphs.perfunction <- (r,p) :: ctx.monomorphs.perfunction;

+ 3 - 1
std/cs/Pointer.hx

@@ -129,5 +129,7 @@ import cs.StdTypes.Int64;
 	@:arrayAccess public static function setp<T>(p:Pointer<T>, at:Int64, val:T):T;
 }
 
-@:forward abstract PointerAccess<T>(T) {}
+@:forward
+@:forward.accessOnAbstract
+abstract PointerAccess<T>(T) {}
 #end