Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,218 changes: 1,795 additions & 1,423 deletions gcc/rust/ast/rust-ast-collector.cc

Large diffs are not rendered by default.

49 changes: 48 additions & 1 deletion gcc/rust/ast/rust-ast-collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "rust-ast-visitor.h"
#include "rust-ast.h"
#include "rust-ast-full.h"
#include "rust-system.h"

namespace Rust {
namespace AST {
Expand All @@ -33,16 +34,40 @@ class CollectItem
enum class Kind
{
Comment,
InternalComment,
BeginNodeDescription,
EndNodeDescription,
Newline,
Indentation,
Token,
};

CollectItem (TokenPtr token) : token (token), kind (Kind::Token) {}
CollectItem (std::string comment) : comment (comment), kind (Kind::Comment) {}
CollectItem (Kind kind) : kind (kind) { rust_assert (kind != Kind::Token); }
CollectItem (size_t level) : indent_level (level), kind (Kind::Indentation) {}

static CollectItem make_internal_comment (const std::string &internal_comment)
{
return CollectItem (internal_comment, Kind::InternalComment);
}

static CollectItem make_comment (const std::string &comment)
{
return CollectItem (comment, Kind::Comment);
}

static CollectItem
make_begin_node_description (const std::string &node_description)
{
return CollectItem (node_description, Kind::BeginNodeDescription);
}

static CollectItem
make_end_node_description (const std::string &node_description)
{
return CollectItem (node_description, Kind::EndNodeDescription);
}

Kind get_kind () { return kind; }

TokenPtr get_token ()
Expand All @@ -63,11 +88,30 @@ class CollectItem
return indent_level;
}

std::string get_internal_comment ()
{
rust_assert (kind == Kind::InternalComment);
return comment;
}

std::string get_node_description ()
{
rust_assert (kind == Kind::BeginNodeDescription
|| kind == Kind::EndNodeDescription);
return comment;
}

bool is_debug () { return debug; }

private:
CollectItem (std::string comment, Kind kind) : comment (comment), kind (kind)
{}

TokenPtr token;
std::string comment;
size_t indent_level;
Kind kind;
bool debug = false;
};

class TokenCollector : public ASTVisitor
Expand Down Expand Up @@ -164,6 +208,9 @@ class TokenCollector : public ASTVisitor
}
}

void describe_node (const std::string &node_name,
std::function<void ()> visitor);

void trailing_comma ();
void newline ();
void indentation ();
Expand Down
15 changes: 14 additions & 1 deletion gcc/rust/ast/rust-ast-dump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,20 @@
namespace Rust {
namespace AST {

Dump::Dump (std::ostream &stream) : stream (stream), indentation (Indent ()) {}
Dump::Dump (std::ostream &stream)
: stream (stream), indentation (Indent ()),
configuration (Configuration{
Configuration::InternalComment::Hide,
Configuration::NodeDescription::Hide,
Configuration::Comment::Dump,
})
{}

Dump::Dump (std::ostream &stream, Configuration configuration,
std::set<std::string> excluded_node)
: stream (stream), indentation (Indent ()), configuration (configuration),
excluded_node (excluded_node)
{}

bool
Dump::require_spacing (TokenPtr previous, TokenPtr current)
Expand Down
53 changes: 52 additions & 1 deletion gcc/rust/ast/rust-ast-dump.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "rust-ast.h"
#include "rust-ast-full.h"
#include "rust-dump.h"
#include "rust-system.h"

#include "rust-ast-collector.h"

Expand All @@ -31,7 +32,28 @@ namespace AST {
class Dump
{
public:
struct Configuration
{
enum class InternalComment
{
Dump,
Hide,
} dump_internal_comments;
enum class NodeDescription
{
Dump,
Hide,
} dump_node_description;
enum class Comment
{
Dump,
Hide,
} dump_comments;
};

Dump (std::ostream &stream);
Dump (std::ostream &stream, Configuration configuration,
std::set<std::string> excluded_node);

/**
* Run the visitor on an entire crate and its items
Expand Down Expand Up @@ -59,7 +81,8 @@ class Dump
}
break;
case AST::CollectItem::Kind::Comment:
stream << " /* " << item.get_comment () << " */ ";
if (configuration.dump_comments == Configuration::Comment::Dump)
stream << " /* " << item.get_comment () << " */ ";
break;
case AST::CollectItem::Kind::Indentation:
for (size_t i = 0; i < item.get_indent_level (); i++)
Expand All @@ -71,6 +94,32 @@ class Dump
stream << "\n";
previous = nullptr;
break;
case AST::CollectItem::Kind::BeginNodeDescription:
if (configuration.dump_node_description
== Configuration::NodeDescription::Dump)
{
std::string comment = item.get_node_description ();
if (excluded_node.find (comment) == excluded_node.end ())
stream << " /* " << comment << " */ ";
}
break;
case AST::CollectItem::Kind::EndNodeDescription:
if (configuration.dump_node_description
== Configuration::NodeDescription::Dump)
{
std::string comment = item.get_node_description ();
if (excluded_node.find (comment) == excluded_node.end ())
stream << " /* !" << comment << " */ ";
}
break;
case AST::CollectItem::Kind::InternalComment:
if (configuration.dump_internal_comments
== Configuration::InternalComment::Dump)
{
std::string comment = item.get_internal_comment ();
stream << " /* " << comment << " */ ";
}
break;
default:
rust_unreachable ();
}
Expand All @@ -83,6 +132,8 @@ class Dump
private:
std::ostream &stream;
Indent indentation;
Configuration configuration;
std::set<std::string> excluded_node;

static bool require_spacing (TokenPtr previous, TokenPtr current);
};
Expand Down
14 changes: 8 additions & 6 deletions gcc/rust/ast/rust-ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3490,7 +3490,9 @@ Module::load_items ()

// we need to parse any possible inner attributes for this module
inner_attrs = parser.parse_inner_attributes ();
auto parsed_items = parser.parse_items ();
auto parsed_items = parser.parse_items ().value_or (
std::vector<std::unique_ptr<AST::Item>>{});

for (const auto &error : parser.get_errors ())
error.emit ();

Expand Down Expand Up @@ -3705,8 +3707,8 @@ AttributeParser::is_end_meta_item_tok (TokenId id) const
std::unique_ptr<MetaItem>
AttributeParser::parse_path_meta_item ()
{
SimplePath path = parser->parse_simple_path ();
if (path.is_empty ())
auto path = parser->parse_simple_path ();
if (!path)
{
rust_error_at (lexer->peek_token ()->get_locus (),
"failed to parse simple path in attribute");
Expand All @@ -3721,7 +3723,7 @@ AttributeParser::parse_path_meta_item ()
= parse_meta_item_seq ();

return std::unique_ptr<MetaItemSeq> (
new MetaItemSeq (std::move (path), std::move (meta_items)));
new MetaItemSeq (std::move (path.value ()), std::move (meta_items)));
}
case EQUAL:
{
Expand All @@ -3735,12 +3737,12 @@ AttributeParser::parse_path_meta_item ()
return nullptr;

return std::unique_ptr<MetaItemPathExpr> (
new MetaItemPathExpr (std::move (path), std::move (expr)));
new MetaItemPathExpr (std::move (path.value ()), std::move (expr)));
}
case COMMA:
// just simple path
return std::unique_ptr<MetaItemPath> (
new MetaItemPath (std::move (path)));
new MetaItemPath (std::move (path.value ())));
default:
rust_error_at (lexer->peek_token ()->get_locus (),
"unrecognised token '%s' in meta item",
Expand Down
23 changes: 3 additions & 20 deletions gcc/rust/ast/rust-ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,38 +477,21 @@ struct Visibility
SimplePath in_path;
location_t locus;

// should this store location info?

public:
// Creates a Visibility - TODO make constructor protected or private?
Visibility (VisType vis_type, SimplePath in_path, location_t locus)
: vis_type (vis_type), in_path (std::move (in_path)), locus (locus)
{}

public:
VisType get_vis_type () const { return vis_type; }

// Returns whether visibility is in an error state.
bool is_error () const
{
return vis_type == PUB_IN_PATH && in_path.is_empty ();
}

// Returns whether a visibility has a path
bool has_path () const { return !is_error () && vis_type >= PUB_CRATE; }
bool has_path () const { return vis_type >= PUB_CRATE; }

// Returns whether visibility is public or not.
bool is_public () const { return vis_type != PRIV && !is_error (); }
bool is_public () const { return vis_type != PRIV; }

location_t get_locus () const { return locus; }

// empty?
// Creates an error visibility.
static Visibility create_error ()
{
return Visibility (PUB_IN_PATH, SimplePath::create_empty (),
UNDEF_LOCATION);
}

// Unique pointer custom clone function
/*std::unique_ptr<Visibility> clone_visibility() const {
return std::unique_ptr<Visibility>(clone_visibility_impl());
Expand Down
10 changes: 1 addition & 9 deletions gcc/rust/ast/rust-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,7 @@ class MetaItemPathExpr : public MetaItem
return MetaItem::ItemKind::PathExpr;
}

// There are two Locations in MetaItemPathExpr (path and expr),
// we have no idea use which of them, just simply return UNKNOWN_LOCATION
// now.
// Maybe we will figure out when we really need the location in the future.
location_t get_locus () const override
{
rust_unreachable ();
return UNKNOWN_LOCATION;
}
location_t get_locus () const override { return path.get_locus (); }

void accept_vis (ASTVisitor &vis) override;

Expand Down
16 changes: 8 additions & 8 deletions gcc/rust/ast/rust-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ class Module : public VisItem
// Loaded module constructor, with items
Module (Identifier name, location_t locus,
std::vector<std::unique_ptr<Item>> items,
Visibility visibility = Visibility::create_error (),
Visibility visibility = Visibility::create_private (),
Unsafety safety = Unsafety::Normal,
std::vector<Attribute> inner_attrs = std::vector<Attribute> (),
std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
Expand Down Expand Up @@ -1743,7 +1743,7 @@ class StructField
bool has_outer_attributes () const { return !outer_attrs.empty (); }

// Returns whether struct field has a non-private (non-default) visibility.
bool has_visibility () const { return !visibility.is_error (); }
bool has_visibility () const { return true; }

StructField (Identifier field_name, std::unique_ptr<Type> field_type,
Visibility vis, location_t locus,
Expand Down Expand Up @@ -1797,8 +1797,8 @@ class StructField
// Creates an error state struct field.
static StructField create_error ()
{
return StructField (std::string (""), nullptr, Visibility::create_error (),
UNDEF_LOCATION);
return StructField (std::string (""), nullptr,
Visibility::create_private (), UNDEF_LOCATION);
}

std::string as_string () const;
Expand Down Expand Up @@ -1902,7 +1902,7 @@ class TupleField

/* Returns whether tuple field has a non-default visibility (i.e. a public
* one) */
bool has_visibility () const { return !visibility.is_error (); }
bool has_visibility () const { return true; }

// Complete constructor
TupleField (std::unique_ptr<Type> field_type, Visibility vis,
Expand Down Expand Up @@ -1952,7 +1952,7 @@ class TupleField
// Creates an error state tuple field.
static TupleField create_error ()
{
return TupleField (nullptr, Visibility::create_error (), UNDEF_LOCATION);
return TupleField (nullptr, Visibility::create_private (), UNDEF_LOCATION);
}

std::string as_string () const;
Expand Down Expand Up @@ -3389,7 +3389,7 @@ class ExternalTypeItem : public ExternalItem
bool has_outer_attrs () const { return !outer_attrs.empty (); }

// Returns whether item has non-default visibility.
bool has_visibility () const { return !visibility.is_error (); }
bool has_visibility () const { return true; }

location_t get_locus () const { return locus; }

Expand Down Expand Up @@ -3481,7 +3481,7 @@ class ExternalStaticItem : public ExternalItem
bool has_outer_attrs () const { return !outer_attrs.empty (); }

// Returns whether item has non-default visibility.
bool has_visibility () const { return !visibility.is_error (); }
bool has_visibility () const { return true; }

location_t get_locus () const { return locus; }

Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/ast/rust-macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ class MacroRulesDefinition : public VisItem
return std::make_unique<MacroRulesDefinition> (
MacroRulesDefinition (rule_name, delim_type, rules, outer_attrs, locus,
AST::MacroRulesDefinition::MacroKind::MBE,
AST::Visibility::create_error ()));
AST::Visibility::create_private ()));
}

static std::unique_ptr<MacroRulesDefinition>
Expand Down
3 changes: 2 additions & 1 deletion gcc/rust/expand/rust-macro-builtins-include.cc
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ MacroBuiltin::include_handler (location_t invoc_locus,
std::vector<std::unique_ptr<AST::Item>> parsed_items{};

if (is_semicoloned)
parsed_items = parser.parse_items ();
parsed_items = parser.parse_items ().value_or (
std::vector<std::unique_ptr<AST::Item>>{});
else
parsed_expr = parser.parse_expr ();

Expand Down
Loading