Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cpp] Gencpp Rework Mk II #11819

Open
wants to merge 101 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
49b4220
Initial class, interface, and enum separation
Aidan63 Oct 16, 2024
59e730c
fold init stuff
Aidan63 Oct 16, 2024
354da10
Move interface header generation into its own module
Aidan63 Oct 18, 2024
d9bfebd
reduce some duplication
Aidan63 Oct 18, 2024
249688d
managed interface implementation gen in its own module
Aidan63 Oct 18, 2024
93df328
cache dependencies in the ctx
Aidan63 Oct 18, 2024
36ddd73
move to gencpp with the other context data func
Aidan63 Oct 18, 2024
de409db
remove interface code from class gen modules
Aidan63 Oct 18, 2024
ebaa770
Use option for reference wildcard
Aidan63 Oct 18, 2024
328ab64
path Map
Aidan63 Oct 18, 2024
9fe38ea
Add back objc guard
Aidan63 Oct 18, 2024
145e55f
calculate IDs up front
Aidan63 Oct 19, 2024
ad8bc9a
separate id lookup table
Aidan63 Oct 19, 2024
a9bb142
Object IDs module to hide cache
Aidan63 Oct 19, 2024
631a5d3
remap enum constructors
Aidan63 Oct 19, 2024
9f59edd
more interface dead code removal
Aidan63 Oct 19, 2024
eb7c4bf
tcpp interface type
Aidan63 Oct 20, 2024
95a7407
name, flags, and debug level in tcpp_class
Aidan63 Oct 20, 2024
5200fb3
remove unused constructor var list return item
Aidan63 Oct 20, 2024
fde882b
separate out header field generation
Aidan63 Oct 22, 2024
7e71613
separate managed and native header gen functions
Aidan63 Oct 22, 2024
0a17f2c
move header stuff into shared function
Aidan63 Oct 22, 2024
e35bb2e
break class impl field gen function down
Aidan63 Oct 25, 2024
3bd3028
add converted fields to tcpp_class
Aidan63 Oct 25, 2024
758a507
Initial split for managed and native class impl
Aidan63 Oct 26, 2024
057d790
map for haxe and native implementations
Aidan63 Oct 26, 2024
d190014
use a list for haxe and native implementations
Aidan63 Oct 26, 2024
5d3a415
use tcpp_class field variables
Aidan63 Oct 27, 2024
9829bf2
field rename and meta / rtti filtering
Aidan63 Oct 28, 2024
e5897d4
create fields and default functions at class transform
Aidan63 Oct 28, 2024
736bb08
use container flag
Aidan63 Oct 28, 2024
0edf381
Mark and visit using variables and fix inverted container flag
Aidan63 Oct 29, 2024
4aaac3c
static mark and visit use static variables list
Aidan63 Oct 29, 2024
9871978
print reflective fields
Aidan63 Oct 29, 2024
701f911
don't duplicate boot generation code
Aidan63 Oct 29, 2024
d42df0f
don't duplicate init function generation
Aidan63 Oct 29, 2024
c23b8ff
don't duplicate dynamic function allocation generation
Aidan63 Oct 29, 2024
05fafa7
generate properties and __Field function
Aidan63 Oct 30, 2024
ef924ec
fold right member get fields
Aidan63 Nov 1, 2024
7c365b8
static get fields
Aidan63 Nov 1, 2024
bfb3a74
shared member and static get fold functions
Aidan63 Nov 1, 2024
4034ca5
static set function uses ordered fields
Aidan63 Nov 2, 2024
3344a77
member set uses organised fields
Aidan63 Nov 3, 2024
d3cc1db
turn abstract functions into normal functions
Aidan63 Nov 3, 2024
dc1adbf
cppia gen uses organised functions
Aidan63 Nov 3, 2024
77bc03a
simplify ScriptNamedFunction generation
Aidan63 Nov 3, 2024
7200bcb
bit of simplification of more cppia stuff
Aidan63 Nov 3, 2024
8e8e342
GetFields uses organised fields
Aidan63 Nov 3, 2024
36e5870
move class retyping into retyper
Aidan63 Nov 4, 2024
1d1efee
remap interface functions
Aidan63 Nov 4, 2024
916320b
store interface hash
Aidan63 Nov 4, 2024
4c94f0f
store meta and rtti fields in remapped interface
Aidan63 Nov 4, 2024
1c0bfce
dead code removal
Aidan63 Nov 4, 2024
02e11ce
tcpp interfaces only store their functions
Aidan63 Nov 5, 2024
07ac093
remapped enum fields rename for consistency
Aidan63 Nov 5, 2024
0da3840
calculate interface slots ahead of time
Aidan63 Nov 7, 2024
f683ccd
remap interface arg names
Aidan63 Nov 7, 2024
0c69ea9
retype the interfaces stored on retyped classes
Aidan63 Nov 8, 2024
909cd50
retype tcpp_class supers
Aidan63 Nov 8, 2024
973b4be
find_class_implementation uses tcpp_class
Aidan63 Nov 8, 2024
21744f9
folds for class interface code
Aidan63 Nov 9, 2024
cd77345
other minor cleanup
Aidan63 Nov 9, 2024
36cd2e9
use flags in class generation to selectively generate some funcs
Aidan63 Nov 9, 2024
41d63d4
retype class functions
Aidan63 Nov 10, 2024
5f7b93a
retype class variables
Aidan63 Nov 10, 2024
554d2fa
dedicated var and dyn function boot functions
Aidan63 Nov 10, 2024
399b7de
dead code cleanup
Aidan63 Nov 11, 2024
d28a3bd
use bindings instead of to_list
Aidan63 Nov 11, 2024
a1dff50
own implementation of of_list
Aidan63 Nov 11, 2024
c5b4c62
replace another to_list
Aidan63 Nov 11, 2024
0a0ad1a
Fix pointer and struct wrapping
Aidan63 Nov 11, 2024
317b0cd
fix debug level not being reset between classes
Aidan63 Nov 11, 2024
4f12a08
add space after static
Aidan63 Nov 12, 2024
5834049
remove unused variable
Aidan63 Nov 12, 2024
151c563
ensure SourceFile paths are made absolute
Aidan63 Nov 12, 2024
2787b94
some debugging
Aidan63 Nov 16, 2024
63eb006
remove debugging prints and store class params
Aidan63 Nov 17, 2024
ba5a228
don't use already mangled name when generating the getter name in ref…
Aidan63 Nov 17, 2024
c57a4e5
same for setters
Aidan63 Nov 17, 2024
e3f92b3
dont use new gc references function
Aidan63 Nov 18, 2024
c1b7332
better handling of static and virtual attributes
Aidan63 Nov 18, 2024
f56c0d4
generate scriptable class using tcpp class function lists
Aidan63 Nov 18, 2024
4bf6e1c
go back to type cant be null
Aidan63 Nov 19, 2024
08bce30
Container flag refers to entire inheritance tree
Aidan63 Nov 19, 2024
1da4f13
allow code to easily distinguish between the different container types
Aidan63 Nov 19, 2024
078a5a8
remove accidentally tracked file
Aidan63 Nov 19, 2024
a2f763a
Merge branch 'development' into gencpp_rework_mkii
Aidan63 Nov 20, 2024
ca927c8
Merge branch 'development' into gencpp_rework_mkii
Aidan63 Nov 22, 2024
fe455fd
Merge branch 'development' into gencpp_rework_mkii
Aidan63 Nov 23, 2024
760989c
pass a context object around the retyper
Aidan63 Nov 28, 2024
89bc3b8
closures tracked in retyper ctx
Aidan63 Nov 28, 2024
a9cb03e
injection stored in retyper ctx
Aidan63 Nov 28, 2024
8b1792b
declarations track by retyper ctx
Aidan63 Nov 28, 2024
b800eb0
this handling moved into ctx
Aidan63 Nov 28, 2024
71b4d54
gc stack handled by retyper ctx
Aidan63 Nov 28, 2024
36718d8
function return type handled by retyper ctx
Aidan63 Nov 28, 2024
4b10c5e
move loop goto management into retyper ctx
Aidan63 Nov 28, 2024
b15f527
consistent retyper ctx variable name
Aidan63 Nov 28, 2024
0b41462
move remaining counter into the fold
Aidan63 Nov 29, 2024
4138b6c
Merge branch 'development' into gencpp_rework_mkii
Aidan63 Nov 29, 2024
9a11526
shared string_map_of_list function
Aidan63 Nov 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 146 additions & 1 deletion src/generators/cpp/cppAst.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
open Type
open Globals

module PathMap = Map.Make(struct type t = path let compare i1 i2 = String.compare (s_type_path i2) (s_type_path i1) end)

module ObjectIds = struct
type t = {
ids : int32 PathMap.t;
cache : unit Int32Map.t;
}

let empty = { ids = PathMap.empty; cache = Int32Map.empty }

let add path id store =
{ ids = PathMap.add path id store.ids; cache = Int32Map.add id () store.cache }

let find_opt path store =
PathMap.find_opt path store.ids

let collision id store =
Int32Map.mem id store.cache
end

module InterfaceSlots = struct
type t = {
hash : int StringMap.t;
next : int;
}

let empty = { hash = StringMap.empty; next = 2 }

let add name slots =
match StringMap.find_opt name slots.hash with
| Some slot ->
slots
| None ->
{ hash = StringMap.add name slots.next slots.hash; next = slots.next + 1 }

let find_opt name slots =
StringMap.find_opt name slots.hash
end

type tcpp =
| TCppDynamic
| TCppUnchanged
Expand Down Expand Up @@ -44,7 +83,7 @@ and tcpp_closure = {
close_args : (tvar * texpr option) list;
close_expr : tcppexpr;
close_id : int;
close_undeclared : (string, tvar) Hashtbl.t;
close_undeclared : tvar StringMap.t;
close_this : tcppthis option;
}

Expand Down Expand Up @@ -152,3 +191,109 @@ and tcpp_expr_expr =
| CppCastObjCBlock of tcppexpr * tcpp list * tcpp
| CppCastProtocol of tcppexpr * tclass
| CppCastNative of tcppexpr

and tcpp_class_container =
| Current (* If the current class holds GC variables *)
| Parent (* If one of the current classes parents holds GC variables *)

and tcpp_class_flags =
| QuickAlloc
| Scriptable
| MemberGet
| MemberSet
| StaticGet
| StaticSet
| GetFields
| Compare
| Boot

and tcpp_class_function = {
tcf_field : tclass_field;
tcf_name : string;
tcf_func : tfunc;

tcf_is_virtual : bool;
tcf_is_reflective : bool;
tcf_is_external : bool;
tcf_is_scriptable : bool;
tcf_is_overriding : bool;
}

and tcpp_class_variable = {
tcv_field : tclass_field;
tcv_name : string;
tcv_type : t;
tcv_default : texpr option;

tcv_is_stackonly : bool;
tcv_is_gc_element : bool;
tcv_is_reflective : bool;
}

and tcpp_class = {
tcl_class : tclass;
tcl_params : tparams;
tcl_name : string;
tcl_id : int32;
tcl_flags : int;
tcl_debug_level : int;
tcl_super : tcpp_class option;
tcl_container : tcpp_class_container option;

tcl_haxe_interfaces : tcpp_interface list;
tcl_native_interfaces : tcpp_interface list;

tcl_static_variables : tcpp_class_variable list;
tcl_static_properties : tcpp_class_variable list;
tcl_static_functions : tcpp_class_function list;
tcl_static_dynamic_functions : tcpp_class_function list;

tcl_variables : tcpp_class_variable list;
tcl_properties : tcpp_class_variable list;
tcl_functions : tcpp_class_function list;
tcl_dynamic_functions : tcpp_class_function list;

tcl_meta : texpr option;
tcl_rtti : texpr option;
tcl_init : texpr option;
}

and tcpp_interface_function = {
iff_field : tclass_field;
iff_name : string;
iff_args : (string * bool * t) list;
iff_return : t;
iff_script_slot : int option;
}

and tcpp_interface = {
if_class : tclass;
if_name : string;
if_hash : string;
if_debug_level : int;
if_functions : tcpp_interface_function list;
if_variables : tclass_field list;
if_extends : tcpp_interface option;
if_meta : texpr option;
if_rtti : texpr option;
if_scriptable : bool;
}

and tcpp_enum_field = {
tef_field : tenum_field;
tef_name : string;
tef_hash : string;
}

and tcpp_enum = {
te_enum : tenum;
te_id : int32;
te_constructors : tcpp_enum_field list;
}

and tcpp_decl =
| ManagedClass of tcpp_class
| NativeClass of tcpp_class
| ManagedInterface of tcpp_interface
| NativeInterface of tcpp_interface
| Enum of tcpp_enum
62 changes: 50 additions & 12 deletions src/generators/cpp/cppAstTools.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ open CppTypeUtils

let follow = Abstract.follow_with_abstracts

let string_map_of_list bs = List.fold_left (fun m (k, v) -> StringMap.add k v m) StringMap.empty bs

(*
A class_path is made from a package (array of strings) and a class name.
Join these together, inclding a separator. eg, "/" for includes : pack1/pack2/Name or "::"
Expand Down Expand Up @@ -588,6 +590,19 @@ and array_element_type haxe_type =
| _ -> "::Dynamic"

and cpp_function_signature tfun abi =
let gen_interface_arg_type_name name opt typ =
let type_str = (type_string typ) in
(* type_str may have already converted Null<X> to Dynamic because of NotNull tag ... *)
(if (opt && (cant_be_null typ) && type_str<>"Dynamic" ) then
"::hx::Null< " ^ type_str ^ " > "
else
type_str ) ^ " " ^ (keyword_remap name)
in

let gen_tfun_interface_arg_list args =
String.concat "," (List.map (fun (name,opt,typ) -> gen_interface_arg_type_name name opt typ) args)
in

match follow tfun with
| TFun(args,ret) -> (type_string ret) ^ " " ^ abi ^ "(" ^ (gen_tfun_interface_arg_list args) ^ ")"
| _ -> "void *"
Expand All @@ -600,18 +615,7 @@ and cpp_function_signature_params params = match params with
| _ ->
print_endline ("Params:" ^ (String.concat "," (List.map type_string params) ));
die "" __LOC__;

and gen_interface_arg_type_name name opt typ =
let type_str = (type_string typ) in
(* type_str may have already converted Null<X> to Dynamic because of NotNull tag ... *)
(if (opt && (cant_be_null typ) && type_str<>"Dynamic" ) then
"::hx::Null< " ^ type_str ^ " > "
else
type_str ) ^ " " ^ (keyword_remap name)

and gen_tfun_interface_arg_list args =
String.concat "," (List.map (fun (name,opt,typ) -> gen_interface_arg_type_name name opt typ) args)


and cant_be_null haxe_type =
is_numeric haxe_type || (type_has_meta_key Meta.NotNull haxe_type )

Expand Down Expand Up @@ -725,3 +729,37 @@ let enum_getter_type t =
| TCppScalar "bool" -> "Bool"
| TCppScalar x -> x
| _ -> "Object"

let int_of_tcpp_class_flag (flag:tcpp_class_flags) =
Obj.magic flag

let set_tcpp_class_flag flags c =
set_flag flags (int_of_tcpp_class_flag c)

let has_tcpp_class_flag c flag =
has_flag c.tcl_flags (int_of_tcpp_class_flag flag)

let all_interface_functions tcpp_interface =
let add_interface_functions existing interface =
let folder acc cur =
if List.exists (fun f -> f.iff_name = cur.iff_name) acc then
acc
else
cur :: acc
in
List.fold_left folder existing interface.if_functions
in

let rec visit_interface existing interface =
let initial =
match interface.if_extends with
| None ->
existing
| Some super ->
visit_interface existing super
in

add_interface_functions initial interface
in

visit_interface [] tcpp_interface |> List.rev
16 changes: 8 additions & 8 deletions src/generators/cpp/cppContext.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
open Gctx
open Globals
open Type
open CppAstTools

(* CPP code generation context *)
Expand All @@ -17,20 +19,19 @@ type context = {
mutable ctx_debug_level : int;
(* cached as required *)
mutable ctx_file_info : (string, string) PMap.t ref;
ctx_type_ids : (string, Int32.t) Hashtbl.t;
(* Per file *)
ctx_output : string -> unit;
ctx_writer : CppSourceWriter.source_writer;
ctx_file_id : int ref;
ctx_is_header : bool;
ctx_interface_slot : (string, int) Hashtbl.t ref;
ctx_interface_slot_count : int ref;
ctx_super_deps : path list CppAst.PathMap.t;
ctx_constructor_deps : tclass_field CppAst.PathMap.t;
ctx_class_member_types : string StringMap.t;
(* This is for returning from the child nodes of TSwitch && TTry *)
mutable ctx_real_this_ptr : bool;
mutable ctx_class_member_types : (string, string) Hashtbl.t;
}

let new_context common_ctx debug file_info member_types =
let new_context common_ctx debug file_info member_types super_deps constructor_deps =
let null_file =
new CppSourceWriter.source_writer common_ctx ignore ignore (fun () -> ())
in
Expand All @@ -40,18 +41,17 @@ let new_context common_ctx debug file_info member_types =
ctx_common = common_ctx;
ctx_writer = null_file;
ctx_file_id = ref (-1);
ctx_type_ids = Hashtbl.create 0;
ctx_is_header = false;
ctx_output = null_file#write;
ctx_interface_slot = ref (Hashtbl.create 0);
ctx_interface_slot_count = ref 2;
ctx_debug_level =
(if has_def Define.AnnotateSource then 3
else if has_def Define.HxcppDebugger then 2
else debug);
ctx_real_this_ptr = true;
ctx_class_member_types = member_types;
ctx_file_info = file_info;
ctx_super_deps = super_deps;
ctx_constructor_deps = constructor_deps;
}
in
result
Expand Down
19 changes: 0 additions & 19 deletions src/generators/cpp/cppExprUtils.ml

This file was deleted.

Loading
Loading