Skip to content

Commit

Permalink
early-name-resolver: Add simple macro name resolution
Browse files Browse the repository at this point in the history
This name resolver performs the same macro name resolution as what was
previously done by the AttrVisitor visitor and macro expander.

It also resolves macro expressions in builtin-macros properly, as well
as expanded AST nodes when necessary.
  • Loading branch information
CohenArthur authored and dkm committed Jan 5, 2023
1 parent a3368cf commit a5a7f32
Show file tree
Hide file tree
Showing 8 changed files with 1,320 additions and 35 deletions.
1 change: 1 addition & 0 deletions gcc/rust/Make-lang.in
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ GRS_OBJS = \
rust/rust-ast-lower-base.o \
rust/rust-ast-lower-pattern.o \
rust/rust-ast-lower-item.o \
rust/rust-early-name-resolver.o \
rust/rust-name-resolver.o \
rust/rust-ast-resolve.o \
rust/rust-ast-resolve-base.o \
Expand Down
10 changes: 0 additions & 10 deletions gcc/rust/expand/rust-attribute-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2877,16 +2877,6 @@ AttrVisitor::visit (AST::MacroRulesDefinition &rules_def)
rules_def.mark_for_strip ();
return;
}

// I don't think any macro rules can be stripped in any way

auto path = Resolver::CanonicalPath::new_seg (rules_def.get_node_id (),
rules_def.get_rule_name ());
expander.resolver->get_macro_scope ().insert (path, rules_def.get_node_id (),
rules_def.get_locus ());
expander.mappings->insert_macro_def (&rules_def);
rust_debug_loc (rules_def.get_locus (), "inserting macro def: [%s]",
path.get ().c_str ());
}

void
Expand Down
9 changes: 6 additions & 3 deletions gcc/rust/expand/rust-macro-builtins.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "rust-macro-invoc-lexer.h"
#include "rust-lex.h"
#include "rust-parse.h"
#include "rust-early-name-resolver.h"
#include "rust-attribute-visitor.h"

namespace Rust {
Expand Down Expand Up @@ -70,9 +71,11 @@ try_expand_macro_expression (AST::Expr *expr, MacroExpander *expander)
{
rust_assert (expander);

auto vis = Rust::AttrVisitor (*expander);
expr->accept_vis (vis);
return expander->take_expanded_fragment (vis);
auto attr_visitor = Rust::AttrVisitor (*expander);
auto early_name_resolver = Resolver::EarlyNameResolver ();
expr->accept_vis (early_name_resolver);
expr->accept_vis (attr_visitor);
return expander->take_expanded_fragment (attr_visitor);
}

/* Expand and then extract a string literal from the macro */
Expand Down
30 changes: 8 additions & 22 deletions gcc/rust/expand/rust-macro-expand.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "rust-diagnostics.h"
#include "rust-parse.h"
#include "rust-attribute-visitor.h"
#include "rust-early-name-resolver.h"

namespace Rust {
AST::ASTFragment
Expand Down Expand Up @@ -138,32 +139,17 @@ MacroExpander::expand_invoc (AST::MacroInvocation &invoc, bool has_semicolon)
// - else is unreachable
// - derive container macro - unreachable

// lookup the rules for this macro
NodeId resolved_node = UNKNOWN_NODEID;
NodeId source_node = UNKNOWN_NODEID;
if (has_semicolon)
source_node = invoc.get_macro_node_id ();
else
source_node = invoc.get_pattern_node_id ();
auto seg
= Resolver::CanonicalPath::new_seg (source_node,
invoc_data.get_path ().as_string ());

bool found = resolver->get_macro_scope ().lookup (seg, &resolved_node);
if (!found)
{
rust_error_at (invoc.get_locus (), "unknown macro: [%s]",
seg.get ().c_str ());
return;
}
auto fragment = AST::ASTFragment::create_error ();
invoc_data.set_expander (this);

// lookup the rules
AST::MacroRulesDefinition *rules_def = nullptr;
bool ok = mappings->lookup_macro_def (resolved_node, &rules_def);
rust_assert (ok);
bool ok = mappings->lookup_macro_invocation (invoc, &rules_def);

auto fragment = AST::ASTFragment::create_error ();
invoc_data.set_expander (this);
// If there's no rule associated with the invocation, we can simply return
// early. The early name resolver will have already emitted an error.
if (!ok)
return;

if (rules_def->is_builtin ())
fragment
Expand Down
4 changes: 4 additions & 0 deletions gcc/rust/expand/rust-macro-expand.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "rust-ast.h"
#include "rust-macro.h"
#include "rust-hir-map.h"
#include "rust-early-name-resolver.h"
#include "rust-name-resolver.h"
#include "rust-macro-invoc-lexer.h"

Expand Down Expand Up @@ -323,10 +324,13 @@ struct MacroExpander
AST::ASTFragment old_fragment = std::move (expanded_fragment);
auto accumulator = std::vector<AST::SingleASTNode> ();
expanded_fragment = AST::ASTFragment::create_error ();
auto early_name_resolver = Resolver::EarlyNameResolver ();

for (auto &node : old_fragment.get_nodes ())
{
expansion_depth++;

node.accept_vis (early_name_resolver);
node.accept_vis (vis);
// we'll decide the next move according to the outcome of the macro
// expansion
Expand Down
Loading

0 comments on commit a5a7f32

Please sign in to comment.