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] Marshalling Extern Types #11981

Open
wants to merge 182 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 181 commits
Commits
Show all changes
182 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
6033946
initial value type support
Aidan63 Dec 7, 2024
696dd29
generic templates and correct return types
Aidan63 Dec 7, 2024
1d46e65
Boxing of captured locals and use marshalling struct helper
Aidan63 Dec 8, 2024
b4734a2
cast dynamic to a reference object
Aidan63 Dec 8, 2024
994377f
always use a reference object instead of sometimes directly a boxed o…
Aidan63 Dec 9, 2024
54f20b3
cast variant to value type
Aidan63 Dec 11, 2024
8f6a2f6
error on to value type field closures
Aidan63 Dec 11, 2024
57c1f96
Use dedicated new marshal types
Aidan63 Dec 12, 2024
416b42d
Update forgotten about old cpp reference
Aidan63 Dec 14, 2024
2206949
use _hx_vt as a prefix
Aidan63 Dec 15, 2024
e2e0866
support namespace field
Aidan63 Dec 15, 2024
6ee58ef
omit brackets for stack constructors which have no arguments
Aidan63 Dec 15, 2024
c1a1962
Fix alignment of reference declarations in closures
Aidan63 Dec 15, 2024
fb74f7b
default to creating boxed value types and deal with the few special c…
Aidan63 Dec 15, 2024
580c989
some nullable support
Aidan63 Dec 20, 2024
82a80dd
move auto cast into a separate file
Aidan63 Dec 24, 2024
9c99937
retype tvar to tcpp specific type
Aidan63 Dec 26, 2024
10c3cdf
retype tvar to simplify code and fix closure argument assignment
Aidan63 Dec 26, 2024
53628ad
promote function and lambda args if captured
Aidan63 Dec 27, 2024
da02ed2
unify closure and function arg generation
Aidan63 Dec 27, 2024
d9d1c09
initial attempt at assigning value types a state
Aidan63 Dec 28, 2024
386d335
some cleanup surrounding value type state assignment
Aidan63 Dec 29, 2024
83f38b5
remove the two variable creation system
Aidan63 Dec 30, 2024
4b3913c
implement class fields and statics
Aidan63 Dec 30, 2024
c692353
wrap value type arguments for new non based struct value type
Aidan63 Dec 30, 2024
c70f62f
cleanup of code and default values
Aidan63 Dec 31, 2024
62658ca
support enums
Aidan63 Dec 31, 2024
c23394e
treat abstract this variable as a reference and cast with a promoted …
Aidan63 Jan 1, 2025
65a6616
pointer type interop support
Aidan63 Jan 1, 2025
8b1616e
ensure arrays of value types are boxed
Aidan63 Jan 1, 2025
36b8111
treat nullable value types as promoted
Aidan63 Jan 1, 2025
cfd53f1
default to heap construction for references
Aidan63 Jan 2, 2025
67aac7e
treat extern value type fields of extern value types as refences
Aidan63 Jan 2, 2025
84e06e5
avoid double :: when no namespace is specified
Aidan63 Jan 2, 2025
dc9dc27
Merge branch 'development' into gencpp_rework_mkii
Aidan63 Jan 2, 2025
44fd82b
another pass to catch some missing :: prefixes
Aidan63 Jan 2, 2025
a4e267a
Merge branch 'gencpp_rework_mkii' into cpp-value-types
Aidan63 Jan 2, 2025
6fbf7c6
Initial enum support and comparison operator support
Aidan63 Jan 3, 2025
146758d
correct enum casting
Aidan63 Jan 3, 2025
c5e285c
handle extern value type field access and assignment correctly
Aidan63 Jan 4, 2025
40d2c8d
fallback to stack allocation instead of heap
Aidan63 Jan 4, 2025
ecc238e
Allow creating reference state values types on the stack
Aidan63 Jan 5, 2025
6d58e63
require reference semantics metadata
Aidan63 Jan 5, 2025
9637d01
Don't use the package if no namespace is provided
Aidan63 Jan 7, 2025
72521c2
add inheritance and casting tests
Aidan63 Jan 7, 2025
8b8006c
support star of and dereference native functions
Aidan63 Jan 7, 2025
d60f8aa
Initial support for pointer types
Aidan63 Jan 10, 2025
6e1fbe6
Add forgotten meta json change
Aidan63 Jan 10, 2025
dab99bc
make sure all boxed pointer vars are allocated
Aidan63 Jan 10, 2025
a4e790b
handle adding pointer stars once
Aidan63 Jan 10, 2025
7a4774d
Use custom marshal pointer type for pointer references
Aidan63 Jan 11, 2025
3562f93
pointer and value specific reference cpp types
Aidan63 Jan 11, 2025
4714cc3
don't retype args with dynamic
Aidan63 Jan 11, 2025
52f22ba
bits of cleanup and specifying some types more
Aidan63 Jan 12, 2025
26b7441
Remove need for assignment filter, now fixed at source
Aidan63 Jan 12, 2025
27da419
remove useless argument
Aidan63 Jan 14, 2025
c0386b0
retype null promoted pointers into boxed objects
Aidan63 Jan 16, 2025
5c9fdc1
inheritance casting with pointer types as well
Aidan63 Jan 19, 2025
ddbdf01
Look for include meta on extern abstracts
Aidan63 Jan 19, 2025
c84c65b
don't add * to pointer type and references
Aidan63 Jan 23, 2025
aabb69d
Don't use subscript operator for pointers of marshal types
Aidan63 Jan 24, 2025
ec76d2e
less code duplication and unwrap marshal type params
Aidan63 Jan 25, 2025
2e39547
do something for cppia
Aidan63 Jan 25, 2025
96ba95d
require reference semantics on pointer types
Aidan63 Jan 25, 2025
e15f84c
Don't allow constructors on pointer types
Aidan63 Jan 26, 2025
4829fde
Initial support for managed externs
Aidan63 Jan 27, 2025
8e9a114
first pass of the _obj handling
Aidan63 Jan 31, 2025
7bd1413
Handle static functions and variable
Aidan63 Feb 1, 2025
d2ae7c5
Fix incorrect template handling with standard naming
Aidan63 Feb 1, 2025
e507af1
another standard naming fix
Aidan63 Feb 1, 2025
e602fb0
allow static fields on extern pointers
Aidan63 Feb 2, 2025
9822463
better static handling and helper function name consistency
Aidan63 Feb 2, 2025
d0b27e2
Exclude marshal types from having debug info generated
Aidan63 Feb 2, 2025
3864521
rename marshal type enum for consistency
Aidan63 Feb 2, 2025
cd38211
add managed marshal type to cppia enum
Aidan63 Feb 2, 2025
370a963
don't follow away marshalling enum abstracts to ensure we see meta
Aidan63 Feb 2, 2025
818c7b7
More generic extern abstract following
Aidan63 Feb 3, 2025
74ef478
Add a bunch of errors
Aidan63 Feb 3, 2025
802a601
Fix error position
Aidan63 Feb 3, 2025
528ee1b
dev merge
Aidan63 Feb 5, 2025
774f772
Move meta up and fill in doc comment
Aidan63 Feb 6, 2025
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
21 changes: 21 additions & 0 deletions src-json/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -1130,5 +1130,26 @@
"metadata": ":nativeArrayAccess",
"doc": "When used on an extern class which implements haxe.ArrayAccess native array access syntax will be generated",
"platforms": ["cpp"]
},
{
"name": "CppValueType",
"metadata": ":cpp.ValueType",
"doc": "",
"platforms": ["cpp"],
"targets": [ "TClass", "TAbstract" ]
},
{
"name": "CppPointerType",
"metadata": ":cpp.PointerType",
"doc": "",
"platforms": ["cpp"],
"targets": [ "TClass" ]
},
{
"name": "CppManagedType",
"metadata": ":cpp.ManagedType",
"doc": "",
"platforms": ["cpp"],
"targets": ["TClass"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be better to move those up with other Cpp* metadata, and add doc :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other cpp meta seems to be a bit all over the place, but I've moved the new ones up to be next to a few others and filled in the doc comment.

}
]
45 changes: 37 additions & 8 deletions src/generators/cpp/cppAst.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,17 @@ module InterfaceSlots = struct
StringMap.find_opt name slots.hash
end

type tcpp =
type marshal_type_state =
| Stack
| Promoted
| Reference

and native_type =
| ValueClass of tclass * tcpp list
| ValueEnum of tabstract
| Pointer of tclass * tcpp list

and tcpp =
| TCppDynamic
| TCppUnchanged
| TCppObject
Expand All @@ -58,6 +68,8 @@ type tcpp =
| TCppRest of tcpp
| TCppReference of tcpp
| TCppStruct of tcpp
| TCppMarshalNativeType of native_type * marshal_type_state
| TCppMarshalManagedType of tclass * tcpp list
| TCppStar of tcpp * bool
| TCppVoidStar
| TCppVarArg
Expand All @@ -78,12 +90,19 @@ type tcpp =

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

and tcppvar = {
tcppv_type : tcpp;
tcppv_var : tvar;
tcppv_name : string;
tcppv_debug_name : string;
}

and tcpp_closure = {
close_type : tcpp;
close_args : (tvar * texpr option) list;
close_args : (tcppvar * texpr option) list;
close_expr : tcppexpr;
close_id : int;
close_undeclared : tvar StringMap.t;
close_undeclared : tcppvar StringMap.t;
close_this : tcppthis option;
}

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

and tcppvarloc =
| VarLocal of tvar
| VarClosure of tvar
| VarLocal of tcppvar
| VarClosure of tcppvar
| VarThis of tclass_field * tcpp
| VarInstance of tcppexpr * tclass_field * string * string
| VarInterface of tcppexpr * tclass_field
Expand Down Expand Up @@ -166,15 +185,15 @@ and tcpp_expr_expr =
| CppPosition of string * int32 * string * string
| CppArrayDecl of tcppexpr list
| CppUnop of tcppunop * tcppexpr
| CppVarDecl of tvar * tcppexpr option
| CppVarDecl of tcppvar * tcppexpr option
| CppBlock of tcppexpr list * tcpp_closure list * bool
| CppFor of tvar * tcppexpr * tcppexpr
| CppFor of tcppvar * tcppexpr * tcppexpr
| CppIf of tcppexpr * tcppexpr * tcppexpr option
| CppWhile of tcppexpr * tcppexpr * Ast.while_flag * int
| CppIntSwitch of tcppexpr * (Int32.t list * tcppexpr) list * tcppexpr option
| CppSwitch of
tcppexpr * tcpp * (tcppexpr list * tcppexpr) list * tcppexpr option * int
| CppTry of tcppexpr * (tvar * tcppexpr) list
| CppTry of tcppexpr * (tcppvar * tcppexpr) list
| CppBreak
| CppContinue
| CppClassOf of path * bool
Expand Down Expand Up @@ -211,6 +230,8 @@ and tcpp_class_function = {
tcf_field : tclass_field;
tcf_name : string;
tcf_func : tfunc;
tcf_args : (tcppvar * texpr option) list;
tcf_return : tcpp;

tcf_is_virtual : bool;
tcf_is_reflective : bool;
Expand Down Expand Up @@ -239,6 +260,7 @@ and tcpp_class = {
tcl_debug_level : int;
tcl_super : tcpp_class option;
tcl_container : tcpp_class_container option;
tcl_constructor : tcpp_class_function option;

tcl_haxe_interfaces : tcpp_interface list;
tcl_native_interfaces : tcpp_interface list;
Expand Down Expand Up @@ -279,9 +301,16 @@ and tcpp_interface = {
if_scriptable : bool;
}

and tcpp_tfun_arg = {
tfa_name : string;
tfa_type : tcpp;
tfa_optional : bool;
}

and tcpp_enum_field = {
tef_field : tenum_field;
tef_name : string;
tef_args : tcpp_tfun_arg list option;
tef_hash : string;
}

Expand Down
155 changes: 144 additions & 11 deletions src/generators/cpp/cppAstTools.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
open Ast
open Type
open Globals
open Error
open CppAst
open CppTypeUtils

Expand Down Expand Up @@ -249,6 +250,16 @@ let keyword_remap name =
"_hx_" ^ name
| x -> x

let cpp_var_name_of var =
match get_meta_string var.v_meta Meta.Native with
| Some n -> n
| None -> keyword_remap var.v_name

let cpp_var_debug_name_of v =
match get_meta_string v.v_meta Meta.RealPath with
| Some n -> n
| None -> v.v_name

let remap_class_path class_path =
let path_remap with_keywords name =
let len = String.length name in
Expand Down Expand Up @@ -357,8 +368,11 @@ and tcpp_to_string_suffix suffix tcpp =
| TCppUnchanged -> " ::Dynamic/*Unchanged*/"
| TCppObject -> " ::Dynamic"
| TCppObjectPtr -> " ::hx::Object *"
| TCppReference t -> tcpp_to_string t ^ " &"
| TCppStruct t -> "cpp::Struct< " ^ tcpp_to_string t ^ " >"
| TCppReference (TCppMarshalNativeType (value_type, _)) -> Printf.sprintf "%s&" (get_native_marshalled_type value_type)
| TCppReference t -> tcpp_to_string t ^ " &"
| TCppStar (TCppMarshalNativeType (value_type, _), const) ->
Printf.sprintf "%s%s*" (if const then "const " else "") (get_native_marshalled_type value_type)
| TCppStar (t, const) ->
(if const then "const " else "") ^ tcpp_to_string t ^ " *"
| TCppVoid -> "void"
Expand All @@ -372,8 +386,12 @@ and tcpp_to_string_suffix suffix tcpp =
| TCppString -> "::String"
| TCppFastIterator it ->
"::cpp::FastIterator" ^ suffix ^ "< " ^ tcpp_to_string it ^ " >"
| TCppPointer (ptrType, TCppMarshalNativeType (value_type, _)) ->
Printf.sprintf "::cpp::%s< %s >" ptrType (get_native_marshalled_type value_type)
| TCppPointer (ptrType, valueType) ->
"::cpp::" ^ ptrType ^ "< " ^ tcpp_to_string valueType ^ " >"
| TCppRawPointer (constName, TCppMarshalNativeType (value_type, _)) ->
Printf.sprintf "%s%s*" constName (get_native_marshalled_type value_type)
| TCppRawPointer (constName, valueType) ->
constName ^ tcpp_to_string valueType ^ "*"
| TCppFunction (argTypes, retType, abi) ->
Expand Down Expand Up @@ -411,6 +429,110 @@ and tcpp_to_string_suffix suffix tcpp =
| TCppNull -> " ::Dynamic"
| TCppCode _ -> "Code"

| TCppMarshalManagedType (cls, params) ->
let type_str, flags = build_type cls.cl_path cls.cl_pos params cls.cl_meta Meta.CppManagedType tcpp_to_string in
let standard_naming = List.exists (fun f -> f = "StandardNaming") flags in
let wrapped =
if suffix = "_obj" then
if standard_naming then
type_str ^ suffix
else
type_str
else
if standard_naming then
type_str
else
Printf.sprintf "::hx::ObjectPtr< %s >" type_str
in
wrapped

| TCppMarshalNativeType (Pointer _ as value_type, Promoted) ->
get_native_marshalled_type value_type |> Printf.sprintf "::cpp::marshal::Boxed< %s* >"
| TCppMarshalNativeType (Pointer _ as value_type, Reference) ->
get_native_marshalled_type value_type |> Printf.sprintf "::cpp::marshal::PointerReference< %s >"
| TCppMarshalNativeType (Pointer _ as value_type, Stack) ->
get_native_marshalled_type value_type |> Printf.sprintf "::cpp::marshal::PointerType< %s >"

| TCppMarshalNativeType ((ValueClass _ | ValueEnum _) as value_type, Promoted) ->
get_native_marshalled_type value_type |> Printf.sprintf "::cpp::marshal::Boxed< %s >"
| TCppMarshalNativeType ((ValueClass _ | ValueEnum _) as value_type, Reference) ->
get_native_marshalled_type value_type |> Printf.sprintf "::cpp::marshal::ValueReference< %s >"
| TCppMarshalNativeType ((ValueClass _ | ValueEnum _) as value_type, Stack) ->
get_native_marshalled_type value_type |> Printf.sprintf "::cpp::marshal::ValueType< %s >"

and build_type path pos params meta target parameter_handler =
let get_meta_field field =
match Meta.get target meta with
| _, [ (EObjectDecl decls, _) ], _ ->
List.find_opt (fun ((n, _, _), _) -> n = field) decls
| _ ->
None
in
let typeParams =
match params with
| [] -> ""
| _ -> "< " ^ String.concat "," (List.map parameter_handler params) ^ " >"
in
let namespace_error pos =
abort "CPP0006: Namespace field must be an array declaration of string literals" pos
in
let flag_error pos =
abort "CPP0008: Flags field must be an array of identifiers" pos
in
let namespace =
match get_meta_field "namespace" with
| Some (_,( EArrayDecl ([]), _)) -> ""
| Some (_,( EArrayDecl (els), _)) ->
"::" ^ (els
|> List.filter_map (fun (e, pos) -> match e with | EConst (String (s, _)) -> Some s | _ -> namespace_error pos)
|> String.concat "::")
| Some ((_, pos, _), _) ->
namespace_error pos
| _ ->
""
in
let t = match get_meta_field "type" with
| Some (_, (EConst (String (s, _)), _) ) ->
s ^ typeParams
| Some ((_, pos, _), _) ->
abort "CPP0007: Type field must be a string literal" pos
| _ ->
snd path ^ typeParams
in
let flags = match get_meta_field "flags" with
| Some (_, (EArrayDecl decls, _) ) ->
decls |> List.filter_map (fun (e, pos) -> match e with | EConst (Ident c) -> Some c | _ -> flag_error pos)
| Some ((_, pos, _), _) ->
flag_error pos
| _ ->
[]
in

Printf.sprintf "%s::%s" namespace t, flags

and get_native_marshalled_type value_type =
let marshal_type_parameter_to_string pos tcpp =
match tcpp with
| TCppMarshalNativeType (value_type, _) -> get_native_marshalled_type value_type
| TCppScalar _
| TCppPointer _
| TCppRawPointer _
| TCppStar _
| TCppVoidStar
| TCppStruct _ ->
tcpp_to_string_suffix "" tcpp
| _ ->
abort "CPP0003: Invalid parameter for a marshalling type" pos
in

match value_type with
| ValueClass (cls, params) ->
build_type cls.cl_path cls.cl_pos params cls.cl_meta Meta.CppValueType (marshal_type_parameter_to_string cls.cl_pos) |> fst
| ValueEnum abs ->
build_type abs.a_path abs.a_pos [] abs.a_meta Meta.CppValueType (marshal_type_parameter_to_string abs.a_pos) |> fst
| Pointer (cls, params) ->
build_type cls.cl_path cls.cl_pos params cls.cl_meta Meta.CppPointerType (marshal_type_parameter_to_string cls.cl_pos) |> fst

and tcpp_objc_block_struct argTypes retType =
let args = String.concat "," (List.map tcpp_to_string argTypes) in
let ret = tcpp_to_string retType in
Expand All @@ -424,15 +546,18 @@ and tcpp_objc_block_struct argTypes retType =
and tcpp_to_string tcpp = tcpp_to_string_suffix "" tcpp

and cpp_class_path_of klass params =
match get_meta_string klass.cl_meta Meta.Native with
| Some s ->
let typeParams =
match params with
| [] -> ""
| _ -> "< " ^ String.concat "," (List.map tcpp_to_string params) ^ " >"
in
" " ^ join_class_path_remap klass.cl_path "::" ^ typeParams
| None -> " ::" ^ join_class_path_remap klass.cl_path "::"
if is_marshalling_native_value_class klass then
get_native_marshalled_type (ValueClass (klass, params))
else
match get_meta_string klass.cl_meta Meta.Native with
| Some s ->
let typeParams =
match params with
| [] -> ""
| _ -> "< " ^ String.concat "," (List.map tcpp_to_string params) ^ " >"
in
" " ^ join_class_path_remap klass.cl_path "::" ^ typeParams
| None -> " ::" ^ join_class_path_remap klass.cl_path "::"

(* Get a string to represent a type.
The "suffix" will be nothing or "_obj", depending if we want the name of the
Expand Down Expand Up @@ -636,11 +761,13 @@ let rec cpp_is_struct_access t =
| TCppFunction _ -> true
| TCppStruct _-> false
| TCppInst (class_def, _) -> Meta.has Meta.StructAccess class_def.cl_meta
| TCppReference (TCppMarshalNativeType _) -> true
| TCppReference (r) -> cpp_is_struct_access r
| _ -> false

let rec cpp_is_native_array_access t =
match t with
| TCppMarshalNativeType _ -> true
| TCppStruct s -> cpp_is_native_array_access s
| TCppReference s -> cpp_is_native_array_access s
| TCppInst ({ cl_array_access = Some _ } as klass, _) when is_extern_class klass && Meta.has Meta.NativeArrayAccess klass.cl_meta -> true
Expand All @@ -654,6 +781,9 @@ let cpp_is_dynamic_type = function

let is_object_element member_type =
match member_type with
| TCppMarshalManagedType _
| TCppMarshalNativeType (_, Promoted) ->
true
| TCppInst (x, _)
| TCppInterface x
-> not (is_extern_class x)
Expand All @@ -678,6 +808,8 @@ let cpp_variant_type_of t = match t with
| TCppObjectPtr
| TCppReference _
| TCppStruct _
| TCppMarshalNativeType _
| TCppMarshalManagedType _
| TCppStar _
| TCppVoid
| TCppFastIterator _
Expand Down Expand Up @@ -719,7 +851,8 @@ let cpp_cast_variant_type_of t = match t with
| TCppDynamicArray
| TCppClass
| TCppEnum _
| TCppInst _ -> t
| TCppInst _
| TCppMarshalManagedType _ -> t
| _ -> cpp_variant_type_of t

let enum_getter_type t =
Expand Down
12 changes: 12 additions & 0 deletions src/generators/cpp/cppMarshalling.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
open CppAst
open CppAstTools

let get_extern_value_type_boxed value_type =
let p = get_native_marshalled_type value_type in
let suffix =
match value_type with
| Pointer _ -> "*"
| _ -> ""
in

Printf.sprintf "::cpp::marshal::Boxed< %s%s >" p suffix, Printf.sprintf "::cpp::marshal::Boxed_obj< %s%s >" p suffix
Loading
Loading