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

Allow visibility modifiers for extern types #2897

Closed
Tracked by #1922
CohenArthur opened this issue Mar 1, 2024 · 4 comments · Fixed by #2911
Closed
Tracked by #1922

Allow visibility modifiers for extern types #2897

CohenArthur opened this issue Mar 1, 2024 · 4 comments · Fixed by #2911

Comments

@CohenArthur
Copy link
Member

CohenArthur commented Mar 1, 2024

Currently we do not lower/handle the visibility modifiers properly for ExternalTypeItem nodes.

@badumbatish
Copy link
Contributor

badumbatish commented Mar 2, 2024

Can i work on this issue with some help, I don't know where I should start?

@CohenArthur
Copy link
Member Author

@badumbatish I'll try writing a starting guide here today but I'm very busy, please ping me on Zulip if you get to working on this and I still haven't written anything

@badumbatish
Copy link
Contributor

@badumbatish I'll try writing a starting guide here today but I'm very busy, please ping me on Zulip if you get to working on this and I still haven't written anything

i'll probably work on this on the weekend, no worries

@CohenArthur
Copy link
Member Author

alright, so here's how I would go about solving this. first, we need to make sure that visibility modifiers are actually parsed for extern type nodes (AST::ExternalTypeItem) - you can do that by looking at our parser, in rust-parse-impl.h, and searching for ExternalTypeItem, as there will be only one function which will create and return these nodes.

if you're sure that visibility is parsed (e.g. if the parsing function is called with a visibility, or that we call parse_visibility in the parsing function), then you need to ensure that this visibility is being assigned to the ExternalTypeItem. Check the class definition, make sure a visibility member exists and is used, etc.

now finally, the next and last step is to make sure that the AST visibility is correctly lowered to an HIR visibility when we lower our AST::ExternalTypeItem into an HIR::ExternalTypeItem. you'll have to find the visitor responsible for performing that operation and make sure the visibility is lowered and transferred properly.

once that's done, in a later PR/issue we'll check that the visibility modifiers are actually working for external types and not just ignored - but that's for later and concerns another part of the compiler :)

badumbatish added a commit to badumbatish/gccrs that referenced this issue Mar 9, 2024
Check-list:
(x) Make sure that visibility modifier are parsed for extern type nodes:
I made sure that `parse_external_type_item` has AST::Visibility within
its declaration
```c++
// in rust-parse-impl.h
template <typename ManagedTokenSource>
std::unique_ptr<AST::ExternalTypeItem>
Parser<ManagedTokenSource>::parse_external_type_item (AST::Visibility vis,
						      AST::AttrVec outer_attrs);
```

(x) Make sure that visibility is assigned to `ExternalTypeItem`
I made sure that in in the function mentioned above,
its constructor, ExternalTypeItem(), handled visibility
correctly via its parent's constructor
```c++
// In rust-item.h
ExternalTypeItem (Identifier item_name, Visibility vis,
                  std::vector<Attribute> outer_attrs, location_t locus)
  : ExternalItem (), outer_attrs (std::move (outer_attrs)), visibility (vis),
    item_name (std::move (item_name)), locus (locus), marked_for_strip (false)
{}

```

(x) Make sure that the AST visibility is correctly lowered to an HIR visibility
I added a translate_visibility call via
`HIR::Visibility vis = translate_visibility (type.get_visibility ());`
in the visit() function.

I also changed the constructor of HIR::ExternalTypeItem to accept a
visibility and change the way it delegate visibility to its parent's
constructor.

gcc/rust/ChangeLog:

        * hir/rust-ast-lower-extern.h: Add translate_visiblity
        * hir/tree/rust-hir-item.h: Fix constructor of ExternalTypeItem
badumbatish added a commit to badumbatish/gccrs that referenced this issue Mar 9, 2024
Ran through git-clang-format, make check-rust passes as expected.
Check-list:
(x) Make sure that visibility modifier are parsed for extern type nodes:
I made sure that `parse_external_type_item` has AST::Visibility within
its declaration
```c++
// in rust-parse-impl.h
template <typename ManagedTokenSource>
std::unique_ptr<AST::ExternalTypeItem>
Parser<ManagedTokenSource>::parse_external_type_item (AST::Visibility vis,
						      AST::AttrVec outer_attrs);
```

(x) Make sure that visibility is assigned to `ExternalTypeItem`
I made sure that in in the function mentioned above,
its constructor, ExternalTypeItem(), handled visibility
correctly via its parent's constructor
```c++
// In rust-item.h
ExternalTypeItem (Identifier item_name, Visibility vis,
		  std::vector<Attribute> outer_attrs, location_t locus)
  : ExternalItem (), outer_attrs (std::move (outer_attrs)), visibility (vis),
    item_name (std::move (item_name)), locus (locus), marked_for_strip (false)
{}

```

(x) Make sure that the AST visibility is correctly lowered to an HIR visibility
I added a translate_visibility call via
`HIR::Visibility vis = translate_visibility (type.get_visibility ());`
in the visit() function.

I also changed the constructor of HIR::ExternalTypeItem to accept a
visibility and change the way it delegate visibility to its parent's
constructor.

gcc/rust/ChangeLog:

	* hir/rust-ast-lower-extern.h: Add translate_visiblity
	* hir/tree/rust-hir-item.h: Fix constructor of ExternalTypeItem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants