Skip to content

Commit 33fb5c0

Browse files
authored
[cpp] Marshalling Extern Types (#11981)
* Initial class, interface, and enum separation * fold init stuff * Move interface header generation into its own module * reduce some duplication * managed interface implementation gen in its own module * cache dependencies in the ctx * move to gencpp with the other context data func * remove interface code from class gen modules * Use option for reference wildcard * path Map * Add back objc guard * calculate IDs up front * separate id lookup table * Object IDs module to hide cache * remap enum constructors * more interface dead code removal * tcpp interface type * name, flags, and debug level in tcpp_class * remove unused constructor var list return item * separate out header field generation * separate managed and native header gen functions * move header stuff into shared function * break class impl field gen function down * add converted fields to tcpp_class * Initial split for managed and native class impl * map for haxe and native implementations * use a list for haxe and native implementations * use tcpp_class field variables * field rename and meta / rtti filtering * create fields and default functions at class transform * use container flag * Mark and visit using variables and fix inverted container flag * static mark and visit use static variables list * print reflective fields * don't duplicate boot generation code * don't duplicate init function generation * don't duplicate dynamic function allocation generation * generate properties and __Field function * fold right member get fields * static get fields * shared member and static get fold functions * static set function uses ordered fields * member set uses organised fields * turn abstract functions into normal functions * cppia gen uses organised functions * simplify ScriptNamedFunction generation * bit of simplification of more cppia stuff * GetFields uses organised fields * move class retyping into retyper * remap interface functions * store interface hash * store meta and rtti fields in remapped interface * dead code removal * tcpp interfaces only store their functions * remapped enum fields rename for consistency * calculate interface slots ahead of time * remap interface arg names * retype the interfaces stored on retyped classes * retype tcpp_class supers * find_class_implementation uses tcpp_class * folds for class interface code * other minor cleanup * use flags in class generation to selectively generate some funcs * retype class functions * retype class variables * dedicated var and dyn function boot functions * dead code cleanup * use bindings instead of to_list * own implementation of of_list * replace another to_list * Fix pointer and struct wrapping * fix debug level not being reset between classes * add space after static * remove unused variable * ensure SourceFile paths are made absolute * some debugging * remove debugging prints and store class params * don't use already mangled name when generating the getter name in reflection * same for setters * dont use new gc references function * better handling of static and virtual attributes * generate scriptable class using tcpp class function lists * go back to type cant be null * Container flag refers to entire inheritance tree * allow code to easily distinguish between the different container types * remove accidentally tracked file * pass a context object around the retyper * closures tracked in retyper ctx * injection stored in retyper ctx * declarations track by retyper ctx * this handling moved into ctx * gc stack handled by retyper ctx * function return type handled by retyper ctx * move loop goto management into retyper ctx * consistent retyper ctx variable name * move remaining counter into the fold * shared string_map_of_list function * initial value type support * generic templates and correct return types * Boxing of captured locals and use marshalling struct helper * cast dynamic to a reference object * always use a reference object instead of sometimes directly a boxed object * cast variant to value type * error on to value type field closures * Use dedicated new marshal types * Update forgotten about old cpp reference * use _hx_vt as a prefix * support namespace field * omit brackets for stack constructors which have no arguments * Fix alignment of reference declarations in closures * default to creating boxed value types and deal with the few special cases * some nullable support * move auto cast into a separate file * retype tvar to tcpp specific type * retype tvar to simplify code and fix closure argument assignment * promote function and lambda args if captured * unify closure and function arg generation * initial attempt at assigning value types a state * some cleanup surrounding value type state assignment * remove the two variable creation system * implement class fields and statics * wrap value type arguments for new non based struct value type * cleanup of code and default values * support enums * treat abstract this variable as a reference and cast with a promoted type * pointer type interop support * ensure arrays of value types are boxed * treat nullable value types as promoted * default to heap construction for references * treat extern value type fields of extern value types as refences * avoid double :: when no namespace is specified * another pass to catch some missing :: prefixes * Initial enum support and comparison operator support * correct enum casting * handle extern value type field access and assignment correctly * fallback to stack allocation instead of heap * Allow creating reference state values types on the stack * require reference semantics metadata * Don't use the package if no namespace is provided * add inheritance and casting tests * support star of and dereference native functions * Initial support for pointer types * Add forgotten meta json change * make sure all boxed pointer vars are allocated * handle adding pointer stars once * Use custom marshal pointer type for pointer references * pointer and value specific reference cpp types * don't retype args with dynamic * bits of cleanup and specifying some types more * Remove need for assignment filter, now fixed at source * remove useless argument * retype null promoted pointers into boxed objects * inheritance casting with pointer types as well * Look for include meta on extern abstracts * don't add * to pointer type and references * Don't use subscript operator for pointers of marshal types * less code duplication and unwrap marshal type params * do something for cppia * require reference semantics on pointer types * Don't allow constructors on pointer types * Initial support for managed externs * first pass of the _obj handling * Handle static functions and variable * Fix incorrect template handling with standard naming * another standard naming fix * allow static fields on extern pointers * better static handling and helper function name consistency * Exclude marshal types from having debug info generated * rename marshal type enum for consistency * add managed marshal type to cppia enum * don't follow away marshalling enum abstracts to ensure we see meta * More generic extern abstract following * Add a bunch of errors * Fix error position * Move meta up and fill in doc comment * try a different way of dealing with extern abstracts metadata * actually fix include and abstract issue * Ensure all marshal types are object returns in cppia wrapper classes * I knew arguments were hiding in there somewhere... * use proper marshalling state * deal with args in the return call as well * do same for interfaces * forgotten bracket * fix call dump * rework cppia glue generation * do similar cleanup for interfaces * store the interface return type as a tcpp * store interface args as retyped versions * bloody submodules * require value semantics instead * Deal with generated temporaries which need to be value types Also a quick fix for templated return types * less string maps * Support array access on marshaling types * initial templated function support * Guard against unresolved extern parameters * reinterpreting to value types * Fix pointer to pointer types, and add view extern * static function template types * Document some marshal functions * Document marshal toString functions * ViewExtensions convenience functions * view compare and marshal read / write helpers * bit of code shuffling and checking for stack only types in classes and enums * store errors in one place * make view stack only and use size_t for length * Guard again promoting stack only value type in expressions * endian specific read and write functions, plus ref to view * view based FPHelper * Dodge pointer vs user var fusion issue * Change the public api to use int64 over size_t * throw when converting big view to haxe type * view based byte implementation * test using hxcpp fork * set docgen to use hxcpp fork as well * Revert "set docgen to use hxcpp fork as well" This reverts commit fcd3c1a. * Revert "test using hxcpp fork" This reverts commit 31ecd0b.
1 parent 0dbb355 commit 33fb5c0

25 files changed

+2465
-881
lines changed

src-json/meta.json

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,33 @@
10131013
"platforms": ["cpp"],
10141014
"targets": ["TClass"]
10151015
},
1016+
{
1017+
"name": "NativeArrayAccess",
1018+
"metadata": ":nativeArrayAccess",
1019+
"doc": "When used on an extern class which implements haxe.ArrayAccess native array access syntax will be generated",
1020+
"platforms": ["cpp"]
1021+
},
1022+
{
1023+
"name": "CppValueType",
1024+
"metadata": ":cpp.ValueType",
1025+
"doc": "Indicates that the externed type should be treated as a value type",
1026+
"platforms": ["cpp"],
1027+
"targets": [ "TClass", "TAbstract" ]
1028+
},
1029+
{
1030+
"name": "CppPointerType",
1031+
"metadata": ":cpp.PointerType",
1032+
"doc": "Indicates that the externed type should be treated as a pointer",
1033+
"platforms": ["cpp"],
1034+
"targets": [ "TClass" ]
1035+
},
1036+
{
1037+
"name": "CppManagedType",
1038+
"metadata": ":cpp.ManagedType",
1039+
"doc": "Indicates that the externed type is a sub class of ::hx::Object",
1040+
"platforms": ["cpp"],
1041+
"targets": ["TClass"]
1042+
},
10161043
{
10171044
"name": "StructInit",
10181045
"metadata": ":structInit",
@@ -1138,11 +1165,5 @@
11381165
"metadata": ":exceptionTypeCheck",
11391166
"doc": "Internally used for transformed exception type check wrt to `catch`.",
11401167
"internal": true
1141-
},
1142-
{
1143-
"name": "NativeArrayAccess",
1144-
"metadata": ":nativeArrayAccess",
1145-
"doc": "When used on an extern class which implements haxe.ArrayAccess native array access syntax will be generated",
1146-
"platforms": ["cpp"]
11471168
}
11481169
]

src/generators/cpp/cppAst.ml

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,17 @@ module InterfaceSlots = struct
4040
StringMap.find_opt name slots.hash
4141
end
4242

43-
type tcpp =
43+
type marshal_type_state =
44+
| Stack
45+
| Promoted
46+
| Reference
47+
48+
and native_type =
49+
| ValueClass of tclass * tcpp list
50+
| ValueEnum of tabstract
51+
| Pointer of tclass * tcpp list
52+
53+
and tcpp =
4454
| TCppDynamic
4555
| TCppUnchanged
4656
| TCppObject
@@ -58,6 +68,8 @@ type tcpp =
5868
| TCppRest of tcpp
5969
| TCppReference of tcpp
6070
| TCppStruct of tcpp
71+
| TCppMarshalNativeType of native_type * marshal_type_state
72+
| TCppMarshalManagedType of tclass * tcpp list
6173
| TCppStar of tcpp * bool
6274
| TCppVoidStar
6375
| TCppVarArg
@@ -78,12 +90,19 @@ type tcpp =
7890

7991
and tcppexpr = { cppexpr : tcpp_expr_expr; cpptype : tcpp; cpppos : pos }
8092

93+
and tcppvar = {
94+
tcppv_type : tcpp;
95+
tcppv_var : tvar;
96+
tcppv_name : string;
97+
tcppv_debug_name : string;
98+
}
99+
81100
and tcpp_closure = {
82101
close_type : tcpp;
83-
close_args : (tvar * texpr option) list;
102+
close_args : (tcppvar * texpr option) list;
84103
close_expr : tcppexpr;
85104
close_id : int;
86-
close_undeclared : tvar StringMap.t;
105+
close_undeclared : tcppvar IntMap.t;
87106
close_this : tcppthis option;
88107
}
89108

@@ -92,8 +111,8 @@ and tcppunop = CppNeg | CppNegBits | CppNot
92111
and tcppthis = ThisReal | ThisFake | ThisDynamic
93112

94113
and tcppvarloc =
95-
| VarLocal of tvar
96-
| VarClosure of tvar
114+
| VarLocal of tcppvar
115+
| VarClosure of tcppvar
97116
| VarThis of tclass_field * tcpp
98117
| VarInstance of tcppexpr * tclass_field * string * string
99118
| VarInterface of tcppexpr * tclass_field
@@ -104,8 +123,8 @@ and tcppinst = InstPtr | InstObjC | InstStruct
104123

105124
and tcppfuncloc =
106125
| FuncThis of tclass_field * tcpp
107-
| FuncInstance of tcppexpr * tcppinst * tclass_field
108-
| FuncStatic of tclass * bool * tclass_field
126+
| FuncInstance of tcppexpr * tcppinst * tclass_field * tcpp list
127+
| FuncStatic of tclass * bool * tclass_field * tcpp list
109128
| FuncTemplate of tclass * tclass_field * path * bool
110129
| FuncInterface of tcppexpr * tclass * tclass_field
111130
| FuncEnumConstruct of tenum * tenum_field
@@ -166,15 +185,15 @@ and tcpp_expr_expr =
166185
| CppPosition of string * int32 * string * string
167186
| CppArrayDecl of tcppexpr list
168187
| CppUnop of tcppunop * tcppexpr
169-
| CppVarDecl of tvar * tcppexpr option
188+
| CppVarDecl of tcppvar * tcppexpr option
170189
| CppBlock of tcppexpr list * tcpp_closure list * bool
171-
| CppFor of tvar * tcppexpr * tcppexpr
190+
| CppFor of tcppvar * tcppexpr * tcppexpr
172191
| CppIf of tcppexpr * tcppexpr * tcppexpr option
173192
| CppWhile of tcppexpr * tcppexpr * Ast.while_flag * int
174193
| CppIntSwitch of tcppexpr * (Int32.t list * tcppexpr) list * tcppexpr option
175194
| CppSwitch of
176195
tcppexpr * tcpp * (tcppexpr list * tcppexpr) list * tcppexpr option * int
177-
| CppTry of tcppexpr * (tvar * tcppexpr) list
196+
| CppTry of tcppexpr * (tcppvar * tcppexpr) list
178197
| CppBreak
179198
| CppContinue
180199
| CppClassOf of path * bool
@@ -211,6 +230,8 @@ and tcpp_class_function = {
211230
tcf_field : tclass_field;
212231
tcf_name : string;
213232
tcf_func : tfunc;
233+
tcf_args : (tcppvar * texpr option) list;
234+
tcf_return : tcpp;
214235

215236
tcf_is_virtual : bool;
216237
tcf_is_reflective : bool;
@@ -240,6 +261,7 @@ and tcpp_class = {
240261
tcl_debug_level : int;
241262
tcl_super : tcpp_class option;
242263
tcl_container : tcpp_class_container option;
264+
tcl_constructor : tcpp_class_function option;
243265

244266
tcl_haxe_interfaces : tcpp_interface list;
245267
tcl_native_interfaces : tcpp_interface list;
@@ -262,8 +284,8 @@ and tcpp_class = {
262284
and tcpp_interface_function = {
263285
iff_field : tclass_field;
264286
iff_name : string;
265-
iff_args : (string * bool * t) list;
266-
iff_return : t;
287+
iff_args : tcpp_tfun_arg list;
288+
iff_return : tcpp;
267289
iff_script_slot : int option;
268290
}
269291

@@ -280,9 +302,16 @@ and tcpp_interface = {
280302
if_scriptable : bool;
281303
}
282304

305+
and tcpp_tfun_arg = {
306+
tfa_name : string;
307+
tfa_type : tcpp;
308+
tfa_optional : bool;
309+
}
310+
283311
and tcpp_enum_field = {
284312
tef_field : tenum_field;
285313
tef_name : string;
314+
tef_args : tcpp_tfun_arg list option;
286315
tef_hash : string;
287316
}
288317

0 commit comments

Comments
 (0)