gccrs icon indicating copy to clipboard operation
gccrs copied to clipboard

[WIP] Unify AST::ExternalFunctionItem with AST::Function

Open 0xn4utilus opened this issue 1 year ago • 9 comments

Added support for external function in AST::Function. It can be verified it in AST validation as follows. But in line 6113 of rust-parse-impl.h, parse_external_item() needs return type ExternalItem from parse_function() which returns Function, thus we cannot directly use parse_function(). How should I proceed ?

0xn4utilus avatar Feb 13 '24 20:02 0xn4utilus

You'd probably want to make Function inherit from ExternalItem

powerboat9 avatar Feb 13 '24 23:02 powerboat9

Wanted to do that, but will it be a good practice? Also it generates ambiguous functions, that can be probably tackled with virtual and override.

0xn4utilus avatar Feb 13 '24 23:02 0xn4utilus

You're right. We'll need to inherit from ExternalItem in order to return a function from this function but it'll be probably harder than expected.

At first I thought this would cause a lot of troubles, turns out we have a dead ExternalItem class in rust-item.h that should probably be deleted.

The real ExternalItem lies in rust-ast.h. It duplicates node_id with Stmt (Function -> VisItem -> Stmt). We could make ExternalItem inherit from item instead of Visitable but this surely have some unwanted outcome I can't see right now.

P-E-P avatar Feb 14 '24 14:02 P-E-P

Yes, will try out some things and see if it works.

0xn4utilus avatar Feb 14 '24 15:02 0xn4utilus

I'm pretty sure you can fix ambiguous functions by overriding them in Function, which wouldn't be ideal, but might be the best option.

powerboat9 avatar Feb 14 '24 18:02 powerboat9

Yes overriding works, now I have to make corresponding modifications in Rust::HIR::ASTLoweringExternItem.

0xn4utilus avatar Feb 14 '24 20:02 0xn4utilus

I am getting an unexpected bug. Where should I start looking? It is working fine for functions, and external-functions with no parameters.

extern "C"{
        fn fun(a:i32);
}

test.rs:2:25: error: unknown reference for resolved name: ‘i32’ 2 | fn fun(a:i32); | ^

0xn4utilus avatar Feb 15 '24 22:02 0xn4utilus

It doesn't look like the types of external function parameters are being resolved. Either that, or external items in the extern "C" {} block aren't being resolved. What happens if you have an external function that has no parameters but returns an i32?

powerboat9 avatar Feb 15 '24 23:02 powerboat9

extern "C"{
        fn fun()->i32;
}

has the same error. So probably external function items are not being resolved

0xn4utilus avatar Feb 16 '24 05:02 0xn4utilus

You'd also need to fix the build errors and format the commit messages

powerboat9 avatar Feb 26 '24 04:02 powerboat9

Requested changes made and removed AST::ExternalFunctionItem dead code.

0xn4utilus avatar Feb 28 '24 14:02 0xn4utilus

Looks good. You might want to split off some of these commits into separate pull requests, so that they can get reviewed/merged faster -- that's what I tend to do

powerboat9 avatar Mar 04 '24 19:03 powerboat9

Looks good. You might want to split off some of these commits into separate pull requests, so that they can get reviewed/merged faster -- that's what I tend to do

Sure

0xn4utilus avatar Mar 05 '24 04:03 0xn4utilus

I'm not a fan of the NodeId duplication, I'll take a deeper look in the upcoming days to be sure there's no way around.

Otherwise, it looks good.

Removing the NodeId requires reordering some inheritance and I don't feel comfortable doing that soon since I haven't got much time and we should probably focus on a stable GCC14 release.

P-E-P avatar Mar 05 '24 10:03 P-E-P

Rebased, now it should be able to merge

0xn4utilus avatar Mar 05 '24 12:03 0xn4utilus

Rebased, now it should be able to merge

Not entirely sure, sometimes the queue is doing weird things and we have to merge by hand. Let's try again.

P-E-P avatar Mar 05 '24 15:03 P-E-P