diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-08-04 14:26:10 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-04 14:26:10 +0000 |
commit | 15635e68bb65d5ce6963ce0c820719a06ca794cd (patch) | |
tree | a1b2dfdc3aa8c82367b4a71ae5cb3c633bd35292 /gcc/rust/ast/rust-ast-full-test.cc | |
parent | 7bdd8599b99a80b5930945bb87d1abffba8ee157 (diff) | |
parent | c4077cc2a8c9ade9bd9b61804d756fcfc2cc9931 (diff) | |
download | gcc-15635e68bb65d5ce6963ce0c820719a06ca794cd.zip gcc-15635e68bb65d5ce6963ce0c820719a06ca794cd.tar.gz gcc-15635e68bb65d5ce6963ce0c820719a06ca794cd.tar.bz2 |
Merge #605
605: Merge both module classes in one r=philberty a=CohenArthur
This PR merges both kinds of Modules (formerly `ModuleBodied` and `ModuleNoBody`) as one class with an enum. This is the [behavior used by rustc](https://github.com/rust-lang/rust/blob/2939249f294dd54a9ce78a8ee1f2922a44e7fb7c/compiler/rustc_ast/src/ast.rs#L2274), where both variants are kept in an enum with one holding a vector of items.
This change is important for multiple file parsing: An external mod (`mod foo; // defined in foo.rs or foo/mod.rs`) will see its items expanded during expansion, which occurs after parsing. This means that the previous directive will be "replaced" by `mod foo { <items> }` at the AST level. In order to achieve this, we need to be able to modify a previously parsed instance of an AST element.
In rustc, this is done [here](https://github.com/rust-lang/rust/blob/2939249f294dd54a9ce78a8ee1f2922a44e7fb7c/compiler/rustc_expand/src/expand.rs#L1427), where `mod_kind` was previously `ModKind::Unloaded` and becomes `ModKind::Loaded(parsed_items, ...)`.
Co-authored-by: CohenArthur <arthur.cohen@epita.fr>
Diffstat (limited to 'gcc/rust/ast/rust-ast-full-test.cc')
-rw-r--r-- | gcc/rust/ast/rust-ast-full-test.cc | 36 |
1 files changed, 9 insertions, 27 deletions
diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc index 5d32139..dfd9cdb 100644 --- a/gcc/rust/ast/rust-ast-full-test.cc +++ b/gcc/rust/ast/rust-ast-full-test.cc @@ -379,22 +379,20 @@ VisItem::as_string () const std::string Module::as_string () const { - std::string vis_item = VisItem::as_string (); + std::string str = VisItem::as_string () + "mod " + module_name; - return vis_item + "mod " + module_name; -} - -std::string -ModuleBodied::as_string () const -{ - // get module string for "[vis] mod [name]" - std::string str = Module::as_string (); + // Return early if we're dealing with an unloaded module as their body resides + // in a different file + if (kind == ModuleKind::UNLOADED) + return str + "\n no body (reference to external file)\n"; // inner attributes str += append_attributes (inner_attrs, INNER); // items str += "\n items: "; + + // This can still happen if the module is loaded but empty, i.e. `mod foo {}` if (items.empty ()) { str += "none"; @@ -419,16 +417,6 @@ ModuleBodied::as_string () const } std::string -ModuleNoBody::as_string () const -{ - std::string str = Module::as_string (); - - str += "\n no body (reference to external file)"; - - return str + "\n"; -} - -std::string StaticItem::as_string () const { std::string str = VisItem::as_string (); @@ -4053,7 +4041,7 @@ AttrInputMetaItemContainer::as_string () const /* Override that calls the function recursively on all items contained within * the module. */ void -ModuleBodied::add_crate_name (std::vector<std::string> &names) const +Module::add_crate_name (std::vector<std::string> &names) const { /* TODO: test whether module has been 'cfg'-ed out to determine whether to * exclude it from search */ @@ -5381,13 +5369,7 @@ Method::accept_vis (ASTVisitor &vis) } void -ModuleBodied::accept_vis (ASTVisitor &vis) -{ - vis.visit (*this); -} - -void -ModuleNoBody::accept_vis (ASTVisitor &vis) +Module::accept_vis (ASTVisitor &vis) { vis.visit (*this); } |