Skip to content

Commit

Permalink
stash
Browse files Browse the repository at this point in the history
  • Loading branch information
powerboat9 committed Feb 28, 2024
1 parent 563e8ed commit 8de68b8
Show file tree
Hide file tree
Showing 24 changed files with 472 additions and 143 deletions.
4 changes: 4 additions & 0 deletions gcc/rust/ast/rust-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -2081,6 +2081,8 @@ class EnumItemDiscriminant : public EnumItem

void accept_vis (ASTVisitor &vis) override;

bool has_expr () { return expression != nullptr; }

// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Expr> &get_expr ()
{
Expand Down Expand Up @@ -3434,6 +3436,8 @@ class NamedFunctionParam
* '_'). */
bool has_name () const { return name != "_" && name != ""; }

bool has_type () const { return param_type != nullptr; }

bool has_outer_attrs () const { return !outer_attrs.empty (); }

// Returns whether the named function parameter is in an error state.
Expand Down
5 changes: 5 additions & 0 deletions gcc/rust/ast/rust-macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,11 @@ class MacroRulesDefinition : public VisItem

MacroKind get_kind () const { return kind; }

std::unique_ptr<MacroRulesDefinition> clone_macro_rules_def () const
{
return std::unique_ptr<MacroRulesDefinition> (clone_item_impl ());
}

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down
2 changes: 2 additions & 0 deletions gcc/rust/expand/rust-macro-builtins.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ std::unordered_map<std::string, AST::MacroTranscriberFunc>
{"test_case", MacroBuiltin::sorry},
{"global_allocator", MacroBuiltin::sorry},
{"cfg_accessible", MacroBuiltin::sorry},
{"rustc_const_stable", MacroBuiltin::sorry},
{"rustc_const_unstable", MacroBuiltin::sorry},
/* Derive builtins do not need a real transcriber, but still need one. It
should however never be called since builtin derive macros get expanded
differently, and benefit from knowing on what kind of items they are
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/hir/rust-ast-lower-pattern.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ ASTLoweringPattern::visit (AST::TupleStructPattern &pattern)
= ASTLowerPathInExpression::translate (&pattern.get_path ());

TupleStructItems *lowered = nullptr;

auto &items = pattern.get_items ();
switch (items->get_item_type ())
{
Expand Down
183 changes: 162 additions & 21 deletions gcc/rust/resolve/rust-default-resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ DefaultResolver::visit (AST::BlockExpr &expr)
void
DefaultResolver::visit (AST::Module &module)
{
// Parse the module's items if they haven't been expanded and the file
// should be parsed (i.e isn't hidden behind an untrue or impossible cfg
// directive
// TODO: make sure this is right
// This was copied from the old early resolver method
// 'accumulate_escaped_macros'
if (module.get_kind () == AST::Module::UNLOADED)
module.load_items ();

auto item_fn = [this, &module] () {
for (auto &item : module.get_items ())
item->accept_vis (*this);
Expand All @@ -62,12 +71,14 @@ DefaultResolver::visit (AST::Function &function)
if (p->is_variadic ())
{
auto param = static_cast<AST::VariadicParam *> (p.get ());
param->get_pattern ()->accept_vis (*this);
if (param->has_pattern ())
param->get_pattern ()->accept_vis (*this);
}
else if (p->is_self ())
{
auto param = static_cast<AST::SelfParam *> (p.get ());
param->get_type ()->accept_vis (*this);
if (param->has_type ())
param->get_type ()->accept_vis (*this);
param->get_lifetime ().accept_vis (*this);
}
else
Expand All @@ -78,6 +89,9 @@ DefaultResolver::visit (AST::Function &function)
}
}

if (function.has_return_type ())
function.get_return_type ()->accept_vis (*this);

if (function.has_body ())
function.get_definition ().value ()->accept_vis (*this);
};
Expand Down Expand Up @@ -154,6 +168,19 @@ DefaultResolver::visit (AST::StructStruct &type)
// FIXME: ???
}

void
DefaultResolver::visit (AST::TupleStruct &type)
{
// do we need to scope anything here? no, right?

// we also can't visit `StructField`s by default, so there's nothing to do -
// correct? or should we do something like

AST::DefaultASTVisitor::visit (type);

// FIXME: ???
}

void
DefaultResolver::visit (AST::Enum &type)
{
Expand All @@ -177,12 +204,43 @@ DefaultResolver::visit (AST::StructExprFieldIndexValue &)
{}

void
DefaultResolver::visit (AST::ClosureExprInner &)
{}
DefaultResolver::visit (AST::ClosureExprInner &expr)
{
if (expr.is_marked_for_strip ())
return;

for (auto &param : expr.get_params ())
{
if (param.is_error ())
continue;

param.get_pattern ()->accept_vis (*this);
if (param.has_type_given ())
param.get_type ()->accept_vis (*this);
}

expr.get_definition_expr ()->accept_vis (*this);
}

void
DefaultResolver::visit (AST::ClosureExprInnerTyped &)
{}
DefaultResolver::visit (AST::ClosureExprInnerTyped &expr)
{
if (expr.is_marked_for_strip ())
return;

for (auto &param : expr.get_params ())
{
if (param.is_error ())
continue;

param.get_pattern ()->accept_vis (*this);
if (param.has_type_given ())
param.get_type ()->accept_vis (*this);
}

expr.get_definition_block ()->accept_vis (*this);
expr.get_return_type ()->accept_vis (*this);
}

void
DefaultResolver::visit (AST::ContinueExpr &expr)
Expand Down Expand Up @@ -212,6 +270,36 @@ void
DefaultResolver::visit (AST::ReturnExpr &expr)
{}

void
DefaultResolver::visit (AST::CallExpr &expr)
{
expr.get_function_expr ()->accept_vis (*this);

for (auto &param : expr.get_params ())
param->accept_vis (*this);
}

void
DefaultResolver::visit (AST::MethodCallExpr &expr)
{
expr.get_receiver_expr ()->accept_vis (*this);

if (expr.get_method_name ().has_generic_args ())
{
auto &args = expr.get_method_name ().get_generic_args ();
for (auto &arg : args.get_generic_args ())
arg.accept_vis (*this);
for (auto &arg : args.get_binding_args ())
if (!arg.is_error ())
arg.get_type ()->accept_vis (*this);
for (auto &arg : args.get_lifetime_args ())
arg.accept_vis (*this);
}

for (auto &param : expr.get_params ())
param->accept_vis (*this);
}

void
DefaultResolver::visit (AST::UnsafeBlockExpr &expr)
{}
Expand All @@ -230,11 +318,18 @@ DefaultResolver::visit (AST::WhileLetLoopExpr &expr)

void
DefaultResolver::visit (AST::IfExpr &expr)
{}
{
expr.get_condition_expr ()->accept_vis (*this);
expr.get_if_block ()->accept_vis (*this);
}

void
DefaultResolver::visit (AST::IfExprConseqElse &)
{}
DefaultResolver::visit (AST::IfExprConseqElse &expr)
{
expr.get_condition_expr ()->accept_vis (*this);
expr.get_if_block ()->accept_vis (*this);
expr.get_else_block ()->accept_vis (*this);
}

void
DefaultResolver::visit (AST::IfLetExpr &expr)
Expand All @@ -246,7 +341,20 @@ DefaultResolver::visit (AST::IfLetExprConseqElse &)

void
DefaultResolver::visit (AST::MatchExpr &expr)
{}
{
if (expr.is_marked_for_strip ())
return;

expr.get_scrutinee_expr ()->accept_vis (*this);
for (auto &arm : expr.get_match_cases ())
{
arm.get_expr ()->accept_vis (*this);
for (auto &pat : arm.get_arm ().get_patterns ())
pat->accept_vis (*this);
if (arm.get_arm ().has_match_arm_guard ())
arm.get_arm ().get_guard_expr ()->accept_vis (*this);
}
}

void
DefaultResolver::visit (AST::AwaitExpr &expr)
Expand Down Expand Up @@ -277,8 +385,21 @@ DefaultResolver::visit (AST::ConstGenericParam &)
{}

void
DefaultResolver::visit (AST::PathInExpression &)
{}
DefaultResolver::visit (AST::PathInExpression &expr)
{
for (auto &seg : expr.get_segments ())
if (seg.has_generic_args ())
{
auto &args = seg.get_generic_args ();
for (auto &arg : args.get_generic_args ())
arg.accept_vis (*this);
for (auto &arg : args.get_binding_args ())
if (!arg.is_error ())
arg.get_type ()->accept_vis (*this);
for (auto &arg : args.get_lifetime_args ())
arg.accept_vis (*this);
}
}

void
DefaultResolver::visit (AST::TypePathSegmentGeneric &)
Expand Down Expand Up @@ -373,24 +494,34 @@ DefaultResolver::visit (AST::EnumItem &)
{}

void
DefaultResolver::visit (AST::EnumItemTuple &)
{}
DefaultResolver::visit (AST::EnumItemTuple &item)
{
for (auto &field : item.get_tuple_fields ())
field.get_field_type ()->accept_vis (*this);
}

void
DefaultResolver::visit (AST::EnumItemStruct &)
{}
DefaultResolver::visit (AST::EnumItemStruct &item)
{
for (auto &field : item.get_struct_fields ())
field.get_field_type ()->accept_vis (*this);
}

void
DefaultResolver::visit (AST::EnumItemDiscriminant &)
{}
DefaultResolver::visit (AST::EnumItemDiscriminant &item)
{
if (item.has_expr ())
item.get_expr ()->accept_vis (*this);
}

void
DefaultResolver::visit (AST::ConstantItem &item)
{
auto expr_vis = [this, &item] () { item.get_expr ()->accept_vis (*this); };

// FIXME: Why do we need a Rib here?
ctx.scoped (Rib::Kind::Item, item.get_node_id (), expr_vis);
if (item.has_expr ())
ctx.scoped (Rib::Kind::Item, item.get_node_id (), expr_vis);
}

void
Expand Down Expand Up @@ -419,8 +550,18 @@ DefaultResolver::visit (AST::ExternalStaticItem &)
{}

void
DefaultResolver::visit (AST::ExternalFunctionItem &)
{}
DefaultResolver::visit (AST::ExternalFunctionItem &function)
{
auto def_fn = [this, &function] () {
for (auto &p : function.get_function_params ())
if (p.has_type ())
p.get_type ()->accept_vis (*this);
if (function.has_return_type ())
function.get_return_type ()->accept_vis (*this);
};

ctx.scoped (Rib::Kind::Function, function.get_node_id (), def_fn);
}

void
DefaultResolver::visit (AST::MacroMatchRepetition &)
Expand Down
3 changes: 3 additions & 0 deletions gcc/rust/resolve/rust-default-resolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class DefaultResolver : public AST::DefaultASTVisitor

// type dec nodes, which visit their fields or variants by default
void visit (AST::StructStruct &);
void visit (AST::TupleStruct &);
void visit (AST::Enum &);

// Visitors that visit their expression node(s)
Expand All @@ -67,6 +68,8 @@ class DefaultResolver : public AST::DefaultASTVisitor
void visit (AST::RangeFromToInclExpr &);
void visit (AST::RangeToInclExpr &);
void visit (AST::ReturnExpr &);
void visit (AST::CallExpr &);
void visit (AST::MethodCallExpr &);
void visit (AST::UnsafeBlockExpr &);
void visit (AST::LoopExpr &);
void visit (AST::WhileLoopExpr &);
Expand Down
Loading

0 comments on commit 8de68b8

Please sign in to comment.