Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option_env support #3094

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions gcc/rust/ast/rust-ast-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ void
DefaultASTVisitor::visit (AST::PathInExpression &path)
{
visit_outer_attrs (path);

// Do not recurse into the segments of a lang-item path, since these paths
// have no segments.
if (path.is_lang_item ())
return;

for (auto &segment : path.get_segments ())
visit (segment);
}
Expand Down
10 changes: 9 additions & 1 deletion gcc/rust/ast/rust-collect-lang-items.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ template <typename T>
void
CollectLangItems::maybe_add_lang_item (const T &item)
{
if (auto lang_item = get_lang_item_attr (item))
if (tl::optional<LangItem::Kind> lang_item = get_lang_item_attr (item))
mappings.insert_lang_item_node (lang_item.value (), item.get_node_id ());
}

Expand Down Expand Up @@ -100,5 +100,13 @@ CollectLangItems::visit (AST::StructStruct &item)
DefaultASTVisitor::visit (item);
}

void
CollectLangItems::visit (AST::EnumItem &item)
{
maybe_add_lang_item (item);

DefaultASTVisitor::visit (item);
}

} // namespace AST
} // namespace Rust
1 change: 1 addition & 0 deletions gcc/rust/ast/rust-collect-lang-items.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class CollectLangItems : public DefaultASTVisitor
void visit (AST::TraitItemType &item) override;
void visit (AST::Function &item) override;
void visit (AST::StructStruct &item) override;
void visit (AST::EnumItem &item) override;

private:
template <typename T> void maybe_add_lang_item (const T &item);
Expand Down
3 changes: 2 additions & 1 deletion gcc/rust/ast/rust-path.h
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,6 @@ class Path : public Pattern

std::string as_string () const override;

// TODO: this seems kinda dodgy
std::vector<PathExprSegment> &get_segments ()
{
rust_assert (kind == Kind::Regular);
Expand All @@ -643,6 +642,8 @@ class Path : public Pattern
Pattern::Kind get_pattern_kind () override { return Pattern::Kind::Path; }
Path::Kind get_path_kind () { return kind; }

bool is_lang_item () { return get_path_kind () == Kind::LangItem; }

protected:
std::vector<PathExprSegment> segments;
tl::optional<LangItem::Kind> lang_item;
Expand Down
22 changes: 20 additions & 2 deletions gcc/rust/backend/rust-compile-resolve-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,26 @@ ResolvePathRef::visit (HIR::QualifiedPathInExpression &expr)
void
ResolvePathRef::visit (HIR::PathInExpression &expr)
{
resolved = resolve (expr.get_final_segment ().get_segment (),
expr.get_mappings (), expr.get_locus (), false);
if (expr.is_lang_item ())
{
// FIXME: Currently this is written to handle lang-item paths which point
// to enum variants. If lang-item PathInExpression's refer to other types,
// this might need rewriting.
TyTy::BaseType *lookup = nullptr;
bool ok
= ctx->get_tyctx ()->lookup_type (expr.get_mappings ().get_hirid (),
&lookup);
rust_assert (ok);

tree t = attempt_constructor_expression_lookup (lookup, ctx,
expr.get_mappings (),
expr.get_locus ());
TREE_USED (t) = 1;
resolved = t;
Comment on lines +50 to +60
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CohenArthur That GLIBCXX assert CI/CD job caught some undefined behavior, where I pushed a random address into the ResolvePathRef::resolve function as the "final segment". I added code here to properly handle lang-item paths.

(Still working on fixing the AST::PathInExpression, I should be able to get that done soon)

}
else
resolved = resolve (expr.get_final_segment ().get_segment (),
expr.get_mappings (), expr.get_locus (), false);
}

tree
Expand Down
7 changes: 4 additions & 3 deletions gcc/rust/checks/lints/rust-lint-marklive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ MarkLive::visit (HIR::PathInExpression &expr)
{
// We should iterate every path segment in order to mark the struct which
// is used in expression like Foo::bar(), we should mark the Foo alive.
expr.iterate_path_segments ([&] (HIR::PathExprSegment &seg) -> bool {
return visit_path_segment (seg);
});
if (!expr.is_lang_item ())
expr.iterate_path_segments ([&] (HIR::PathExprSegment &seg) -> bool {
return visit_path_segment (seg);
});

// after iterate the path segments, we should mark functions and associated
// functions alive.
Expand Down
3 changes: 3 additions & 0 deletions gcc/rust/expand/rust-cfg-strip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,9 @@ CfgStrip::visit (AST::PathInExpression &path)
return;
}

if (path.is_lang_item ())
return;

for (auto &segment : path.get_segments ())
{
if (segment.has_generic_args ())
Expand Down
3 changes: 3 additions & 0 deletions gcc/rust/expand/rust-expand-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,9 @@ ExpandVisitor::visit (AST::MacroInvocation &macro_invoc)
void
ExpandVisitor::visit (AST::PathInExpression &path)
{
if (path.is_lang_item ())
return;

for (auto &segment : path.get_segments ())
if (segment.has_generic_args ())
expand_generic_args (segment.get_generic_args ());
Expand Down
78 changes: 78 additions & 0 deletions gcc/rust/expand/rust-macro-builtins-utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// <http://www.gnu.org/licenses/>.

#include "rust-fmt.h"
#include "rust-ast-builder.h"
#include "rust-macro-builtins.h"
#include "rust-macro-builtins-helpers.h"

Expand Down Expand Up @@ -226,6 +227,83 @@ MacroBuiltin::env_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
return AST::Fragment ({node}, std::move (tok));
}

/* Expand builtin macro option_env!(), which inspects an environment variable at
compile time. */
tl::optional<AST::Fragment>
MacroBuiltin::option_env_handler (location_t invoc_locus,
AST::MacroInvocData &invoc,
AST::InvocKind semicolon)
{
auto invoc_token_tree = invoc.get_delim_tok_tree ();
MacroInvocLexer lex (invoc_token_tree.to_token_stream ());
Parser<MacroInvocLexer> parser (lex);

auto last_token_id = macro_end_token (invoc_token_tree, parser);
std::unique_ptr<AST::LiteralExpr> lit_expr = nullptr;
bool has_error = false;

auto start = lex.get_offs ();
auto expanded_expr = try_expand_many_expr (parser, last_token_id,
invoc.get_expander (), has_error);
auto end = lex.get_offs ();

auto tokens = lex.get_token_slice (start, end);

if (has_error)
return AST::Fragment::create_error ();

auto pending = check_for_eager_invocations (expanded_expr);
if (!pending.empty ())
return make_eager_builtin_invocation (BuiltinMacro::OptionEnv, invoc_locus,
invoc_token_tree,
std::move (pending));

if (expanded_expr.size () != 1)
{
rust_error_at (invoc_locus, "%<option_env!%> takes 1 argument");
return AST::Fragment::create_error ();
}

if (expanded_expr.size () > 0)
if (!(lit_expr
= try_extract_string_literal_from_fragment (invoc_locus,
expanded_expr[0])))
return AST::Fragment::create_error ();

parser.skip_token (last_token_id);

auto env_value = getenv (lit_expr->as_string ().c_str ());
AST::Builder b (invoc_locus);

if (env_value == nullptr)
{
auto none_expr = std::unique_ptr<AST::Expr> (
new AST::PathInExpression (LangItem::Kind::OPTION_NONE, {},
invoc_locus));

auto node = AST::SingleASTNode (std::move (none_expr));
std::vector<AST::SingleASTNode> nodes;
nodes.push_back (node);

return AST::Fragment (nodes, std::vector<std::unique_ptr<AST::Token>> ());
}
std::vector<std::unique_ptr<AST::Expr>> args;
args.push_back (b.literal_string (env_value));

std::unique_ptr<AST::Expr> some_expr
= b.call (std::unique_ptr<AST::Expr> (
new AST::PathInExpression (LangItem::Kind::OPTION_SOME, {},
invoc_locus)),
std::move (args));

auto node = AST::SingleASTNode (std::move (some_expr));

std::vector<AST::SingleASTNode> nodes;
nodes.push_back (node);

return AST::Fragment (nodes, std::vector<std::unique_ptr<AST::Token>> ());
}

tl::optional<AST::Fragment>
MacroBuiltin::cfg_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
AST::InvocKind semicolon)
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/expand/rust-macro-builtins.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ std::unordered_map<std::string, AST::MacroTranscriberFunc>
{"format_args_nl", format_args_maker (AST::FormatArgs::Newline::Yes)},
{"asm", inline_asm_maker (AST::AsmKind::Inline)},
{"global_asm", inline_asm_maker (AST::AsmKind::Global)},
{"option_env", MacroBuiltin::option_env_handler},
/* Unimplemented macro builtins */
{"option_env", MacroBuiltin::sorry},
{"concat_idents", MacroBuiltin::sorry},
{"module_path", MacroBuiltin::sorry},
{"log_syntax", MacroBuiltin::sorry},
Expand Down
4 changes: 4 additions & 0 deletions gcc/rust/expand/rust-macro-builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ class MacroBuiltin
AST::MacroInvocData &invoc,
AST::InvocKind semicolon);

static tl::optional<AST::Fragment>
option_env_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
AST::InvocKind semicolon);

static tl::optional<AST::Fragment> cfg_handler (location_t invoc_locus,
AST::MacroInvocData &invoc,
AST::InvocKind semicolon);
Expand Down
15 changes: 11 additions & 4 deletions gcc/rust/hir/rust-ast-lower.cc
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,17 @@ void
ASTLowerPathInExpression::visit (AST::PathInExpression &expr)
{
std::vector<HIR::PathExprSegment> path_segments;
auto crate_num = mappings.get_current_crate ();
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
mappings.get_next_hir_id (crate_num),
UNKNOWN_LOCAL_DEFID);
if (expr.get_path_kind () == AST::Path::Kind::LangItem)
{
translated = new HIR::PathInExpression (mapping, expr.get_lang_item (),
expr.get_locus (),
expr.opening_scope_resolution ());
return;
}
auto &segments = expr.get_segments ();
for (auto &s : segments)
{
Expand All @@ -504,10 +515,6 @@ ASTLowerPathInExpression::visit (AST::PathInExpression &expr)
HIR::PathExprSegment *lowered_seg = &path_segments.back ();
mappings.insert_hir_path_expr_seg (lowered_seg);
}
auto crate_num = mappings.get_current_crate ();
Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
mappings.get_next_hir_id (crate_num),
UNKNOWN_LOCAL_DEFID);

translated = new HIR::PathInExpression (mapping, std::move (path_segments),
expr.get_locus (),
Expand Down
12 changes: 12 additions & 0 deletions gcc/rust/hir/tree/rust-hir-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,15 @@ PathExprSegment::operator= (PathExprSegment const &other)
void
PathPattern::iterate_path_segments (std::function<bool (PathExprSegment &)> cb)
{
rust_assert (!is_lang_item ());
for (auto it = segments.begin (); it != segments.end (); it++)
{
if (!cb (*it))
return;
}
}

// Path constructor
PathInExpression::PathInExpression (Analysis::NodeMapping mappings,
std::vector<PathExprSegment> path_segments,
location_t locus,
Expand All @@ -150,6 +152,16 @@ PathInExpression::PathInExpression (Analysis::NodeMapping mappings,
has_opening_scope_resolution (has_opening_scope_resolution), locus (locus)
{}

// Lang-item constructor.
PathInExpression::PathInExpression (Analysis::NodeMapping mappings,
LangItem::Kind lang_item, location_t locus,
bool has_opening_scope_resolution,
std::vector<AST::Attribute> outer_attrs)
: PathPattern (lang_item),
PathExpr (std::move (mappings), std::move (outer_attrs)),
has_opening_scope_resolution (has_opening_scope_resolution), locus (locus)
{}

bool
PathInExpression::is_self () const

Expand Down
Loading
Loading