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] Support type parameters on extern classes #10415

Merged
merged 6 commits into from
Aug 2, 2022

Conversation

Aidan63
Copy link
Contributor

@Aidan63 Aidan63 commented Oct 2, 2021

Hello,
I've added support for type parameters / generic extern classes for cpp. Main change is that TCppInst now carries a list of tcpp type parameters around with it which are used if the class is extern. This allows you to write externs like in the following example, where previously you'd need to write externs for each type of the native templated type (or do stuff like have a generic extern but then extern it with another extern specifying the type).

function main() {
	final ivec = new StdVector();
	ivec.push_back(1);

	final fvec = new StdVector();
	fvec.push_back(1.4685);
}

@:keep
@:unreflective
@:structAccess
@:include('vector')
@:native('std::vector')
extern class StdVector<T> {
	function new();

	function size() : Int;

	function push_back(_v : T) : Void;

	function data() : cpp.RawPointer<T>;

	function at(_idx : Int) : T;
}

which generates cpp code looking like this.

void Main_Fields__obj::main(){
            	HX_STACKFRAME(&_hx_pos_195033e4c87195a0_1_main)
HXLINE(   2)		std::vector<int> ivec = std::vector<int>();
HXLINE(   3)		ivec.push_back(1);
HXLINE(   5)		std::vector<Float> fvec = std::vector<Float>();
HXLINE(   6)		fvec.push_back(((Float)1.4685));
            	}

One thing you can't do with generic externs is something like @:native('::cpp::Struct<std::vector>') for templated classes like you can with non templated classes as the parameters are simply appened to the end (maybe we need a new meta like @:structWrap to auto wrap the type in a cpp.Struct?).

@Aidan63
Copy link
Contributor Author

Aidan63 commented Oct 2, 2021

Hmm, was hoping the failing cppia test was something being flakey, but seems I have broken something (and the mac ci seems stuck running as well)

@Aidan63
Copy link
Contributor Author

Aidan63 commented Oct 4, 2021

There we are, all green now. That's what I get for changing code I thought I understood.

@skial skial mentioned this pull request Oct 6, 2021
1 task
@Aidan63
Copy link
Contributor Author

Aidan63 commented Oct 7, 2021

Had an issue where if you tried to use another extern class for the T parameter, passing an object of that type into an extern function would cause it be casted to dynamic and refuse to compile. The issue seemed to be when a function call is retyped it uses the types of the TFun which in the case of generics will be something like StdVector.T if you want to use T as a function argument and cpp_type_of resorts to dynamic for KTypeParameter instances.

I worked around this by using the types of the arguments being passed in instead of the argument types of the function definition when retyping the function for InstStruct functions. Not sure if this is the correct solution, but it works.

Also, these two new CI failures seem unrelated.

@RealyUniqueName RealyUniqueName added the platform-cpp Everything related to CPP / C++ label Oct 25, 2021
@RealyUniqueName RealyUniqueName added this to the Backlog milestone Oct 25, 2021
@Simn Simn merged commit f2e23f6 into HaxeFoundation:development Aug 2, 2022
@skial skial mentioned this pull request Aug 3, 2022
1 task
@Aidan63 Aidan63 deleted the cpp-generic-extern-classes branch August 4, 2022 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform-cpp Everything related to CPP / C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants