Skip to content

Commit

Permalink
Add unsafety member to modules
Browse files Browse the repository at this point in the history
The rust syntax allows unsafe module even if those are rejected at a
later stage.

gcc/rust/ChangeLog:

	* ast/rust-item.h: Add safety status to Modules in the AST.
	* parse/rust-parse-impl.h (Parser::parse_module): Adapt constructors.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
  • Loading branch information
P-E-P committed Nov 21, 2023
1 parent 34479e5 commit f343672
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
13 changes: 8 additions & 5 deletions gcc/rust/ast/rust-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ class Module : public VisItem
Identifier module_name;
location_t locus;
ModuleKind kind;
Unsafety safety;

// Name of the file including the module
std::string outer_filename;
Expand Down Expand Up @@ -766,11 +767,12 @@ class Module : public VisItem

// Unloaded module constructor
Module (Identifier module_name, Visibility visibility,
std::vector<Attribute> outer_attrs, location_t locus,
std::vector<Attribute> outer_attrs, location_t locus, Unsafety safety,
std::string outer_filename, std::vector<std::string> module_scope)
: VisItem (std::move (visibility), std::move (outer_attrs)),
module_name (module_name), locus (locus), kind (ModuleKind::UNLOADED),
outer_filename (outer_filename), inner_attrs (std::vector<Attribute> ()),
safety (safety), outer_filename (outer_filename),
inner_attrs (std::vector<Attribute> ()),
items (std::vector<std::unique_ptr<Item>> ()),
module_scope (std::move (module_scope))
{}
Expand All @@ -779,18 +781,19 @@ class Module : public VisItem
Module (Identifier name, location_t locus,
std::vector<std::unique_ptr<Item>> items,
Visibility visibility = Visibility::create_error (),
Unsafety safety = Unsafety::Normal,
std::vector<Attribute> inner_attrs = std::vector<Attribute> (),
std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
: VisItem (std::move (visibility), std::move (outer_attrs)),
module_name (name), locus (locus), kind (ModuleKind::LOADED),
outer_filename (std::string ()), inner_attrs (std::move (inner_attrs)),
items (std::move (items))
safety (safety), outer_filename (std::string ()),
inner_attrs (std::move (inner_attrs)), items (std::move (items))
{}

// Copy constructor with vector clone
Module (Module const &other)
: VisItem (other), module_name (other.module_name), locus (other.locus),
kind (other.kind), inner_attrs (other.inner_attrs),
kind (other.kind), safety (other.safety), inner_attrs (other.inner_attrs),
module_scope (other.module_scope)
{
// We need to check whether we are copying a loaded module or an unloaded
Expand Down
8 changes: 5 additions & 3 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
/* DO NOT INCLUDE ANYWHERE - this is automatically included with rust-parse.h
* This is also the reason why there are no include guards. */

#include "rust-common.h"
#include "rust-item.h"
#include "rust-token.h"
#define INCLUDE_ALGORITHM
Expand Down Expand Up @@ -2406,8 +2407,8 @@ Parser<ManagedTokenSource>::parse_module (AST::Visibility vis,
// Construct an external module
return std::unique_ptr<AST::Module> (
new AST::Module (std::move (name), std::move (vis),
std::move (outer_attrs), locus, lexer.get_filename (),
inline_module_stack));
std::move (outer_attrs), locus, Unsafety::Normal,
lexer.get_filename (), inline_module_stack));
case LEFT_CURLY: {
lexer.skip_token ();

Expand Down Expand Up @@ -2463,7 +2464,8 @@ Parser<ManagedTokenSource>::parse_module (AST::Visibility vis,

return std::unique_ptr<AST::Module> (
new AST::Module (std::move (name), locus, std::move (items),
std::move (vis), std::move (inner_attrs),
std::move (vis), Unsafety::Normal,
std::move (inner_attrs),
std::move (outer_attrs))); // module name?
}
default:
Expand Down

0 comments on commit f343672

Please sign in to comment.