-
Notifications
You must be signed in to change notification settings - Fork 165
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
Collect lang items in the AST #3289
Merged
CohenArthur
merged 3 commits into
Rust-GCC:master
from
CohenArthur:collect-lang-items-ast
Dec 4, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright (C) 2024 Free Software Foundation, Inc. | ||
|
||
// This file is part of GCC. | ||
|
||
// GCC is free software; you can redistribute it and/or modify it under | ||
// the terms of the GNU General Public License as published by the Free | ||
// Software Foundation; either version 3, or (at your option) any later | ||
// version. | ||
|
||
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY | ||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
// for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with GCC; see the file COPYING3. If not see | ||
// <http://www.gnu.org/licenses/>. | ||
|
||
#include "rust-collect-lang-items.h" | ||
#include "optional.h" | ||
#include "rust-ast-collector.h" | ||
#include "rust-ast.h" | ||
#include "rust-attribute-values.h" | ||
#include "rust-attributes.h" | ||
#include "rust-hir-map.h" | ||
|
||
namespace Rust { | ||
namespace AST { | ||
|
||
template <typename T> | ||
tl::optional<LangItem::Kind> | ||
get_lang_item_attr (const T &maybe_lang_item) | ||
{ | ||
for (const auto &attr : maybe_lang_item.get_outer_attrs ()) | ||
{ | ||
const auto &str_path = attr.get_path ().as_string (); | ||
if (!Analysis::Attributes::is_known (str_path)) | ||
{ | ||
rust_error_at (attr.get_locus (), "unknown attribute"); | ||
continue; | ||
} | ||
|
||
bool is_lang_item = str_path == Values::Attributes::LANG | ||
&& attr.has_attr_input () | ||
&& attr.get_attr_input ().get_attr_input_type () | ||
== AST::AttrInput::AttrInputType::LITERAL; | ||
|
||
if (is_lang_item) | ||
{ | ||
auto &literal | ||
= static_cast<AST::AttrInputLiteral &> (attr.get_attr_input ()); | ||
const auto &lang_item_type_str = literal.get_literal ().as_string (); | ||
|
||
return LangItem::Parse (lang_item_type_str); | ||
} | ||
} | ||
|
||
return tl::nullopt; | ||
} | ||
|
||
template <typename T> | ||
void | ||
CollectLangItems::maybe_add_lang_item (const T &item) | ||
{ | ||
if (auto lang_item = get_lang_item_attr (item)) | ||
mappings.insert_lang_item_node (lang_item.value (), item.get_node_id ()); | ||
} | ||
|
||
void | ||
CollectLangItems::visit (AST::Trait &item) | ||
{ | ||
maybe_add_lang_item (item); | ||
|
||
DefaultASTVisitor::visit (item); | ||
} | ||
|
||
void | ||
CollectLangItems::visit (AST::TraitItemType &item) | ||
{ | ||
maybe_add_lang_item (item); | ||
|
||
DefaultASTVisitor::visit (item); | ||
} | ||
|
||
} // namespace AST | ||
} // namespace Rust |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (C) 2024 Free Software Foundation, Inc. | ||
|
||
// This file is part of GCC. | ||
|
||
// GCC is free software; you can redistribute it and/or modify it under | ||
// the terms of the GNU General Public License as published by the Free | ||
// Software Foundation; either version 3, or (at your option) any later | ||
// version. | ||
|
||
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY | ||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
// for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with GCC; see the file COPYING3. If not see | ||
// <http://www.gnu.org/licenses/>. | ||
|
||
#ifndef RUST_COLLECT_LANG_ITEMS_H | ||
#define RUST_COLLECT_LANG_ITEMS_H | ||
|
||
#include "rust-ast-visitor.h" | ||
#include "rust-ast.h" | ||
#include "rust-hir-map.h" | ||
#include "rust-item.h" | ||
|
||
namespace Rust { | ||
namespace AST { | ||
|
||
// This class collects lang items ahead of lowering, as they are now needed for | ||
// some parts of name resolution | ||
class CollectLangItems : public DefaultASTVisitor | ||
{ | ||
public: | ||
CollectLangItems () : mappings (Analysis::Mappings::get ()){}; | ||
|
||
void go (AST::Crate &crate) { DefaultASTVisitor::visit (crate); } | ||
|
||
Analysis::Mappings &mappings; | ||
|
||
// We must implement visitors for all constructs that could be lang items. | ||
// Lang items can be traits, but also enums, and even enum variants. | ||
// | ||
// https://github.com/rust-lang/rust/blob/master/compiler/rustc_hir/src/lang_items.rs | ||
|
||
using DefaultASTVisitor::visit; | ||
|
||
void visit (AST::Trait &item) override; | ||
void visit (AST::TraitItemType &item) override; | ||
|
||
private: | ||
template <typename T> void maybe_add_lang_item (const T &item); | ||
}; | ||
|
||
} // namespace AST | ||
} // namespace Rust | ||
|
||
#endif // ! RUST_COLLECT_LANG_ITEMS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1241,6 +1241,9 @@ Mappings::lookup_builtin_marker () | |
return builtinMarker; | ||
} | ||
|
||
// FIXME: Before merging: Should we remove the `locus` parameter here? since | ||
// lang items are looked up mostly for code generation, it doesn't make sense to | ||
// error out on the locus of the node trying to access an inexistant lang item | ||
Comment on lines
+1244
to
+1246
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would require changes in a lot of code which fetches lang items, such as the typechecker and codegen. It would be done in a later PR |
||
DefId | ||
Mappings::get_lang_item (LangItem::Kind item_type, location_t locus) | ||
{ | ||
|
@@ -1277,5 +1280,24 @@ Mappings::lookup_lang_item (LangItem::Kind item_type) | |
return it->second; | ||
} | ||
|
||
void | ||
Mappings::insert_lang_item_node (LangItem::Kind item_type, NodeId node_id) | ||
{ | ||
auto it = lang_item_nodes.find (item_type); | ||
rust_assert (it == lang_item_nodes.end ()); | ||
|
||
lang_item_nodes.insert ({item_type, node_id}); | ||
} | ||
|
||
tl::optional<NodeId &> | ||
Mappings::lookup_lang_item_node (LangItem::Kind item_type) | ||
{ | ||
auto it = lang_item_nodes.find (item_type); | ||
if (it == lang_item_nodes.end ()) | ||
return tl::nullopt; | ||
|
||
return it->second; | ||
} | ||
|
||
} // namespace Analysis | ||
} // namespace Rust |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should introduce some helper function here instead, I'm sure it would be reused elsewhere and it would ease this function a bit.
Probably within
rust-attributes.cc
since we already have some similar functions there ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think it'd be good but in a different PR. it would be a good
good-first-pr
issue too